ExampleCode.C
Go to the documentation of this file.
1 // Example script for plotting variables using the PredictionHandler
2 //
3 // author: Adam Lister
4 // date : 2020-05-13
5 // email : adam.lister@wisc.edu
6 
7 // framework includes
9 #include "CAFAna/Core/Loaders.h"
10 #include "CAFAna/Core/Binning.h"
11 #include "CAFAna/Core/Spectrum.h"
13 #include "CAFAna/Core/EventList.h"
14 #include "CAFAna/Core/Utilities.h"
16 #include "CAFAna/Cuts/Cuts.h"
17 #include "CAFAna/Cuts/SpillCuts.h"
18 #include "CAFAna/Vars/Vars.h"
19 
22 
27 
28 #include "NuXAna/Cuts/NusCuts20.h"
29 #include "NuXAna/Cuts/NusCuts.h"
30 #include "NuXAna/Vars/NusVars.h"
32 
33 #include "TMVA/Reader.h"
34 #include "TMVA/IMethod.h"
35 #include "TString.h"
36 
37 // selection includes
40 
41 // root includes
42 #include "TFile.h"
43 #include "TCanvas.h"
44 #include "TH1.h"
45 #include "TH2.h"
46 
47 #include <cassert>
48 
49 using namespace ana;
50 
51 // setup variables we want to look at
52 // ----------------------------------------------------------------------------
54 
55 std::vector<VarContainer> varsFD = {
56  VarContainer(ana::kPartPtp, "kPartPtp", "Tranverse Momentum Fraction", kPartPtpBins) ,
57 };
58 
59 std::vector<VarContainer> varsND = {
60  VarContainer(ana::kPartPtp, "kPartPtp", "Tranverse Momentum Fraction", kPartPtpBins) ,
61 };
62 
63 // setup cuts we want to place
64 // ----------------------------------------------------------------------------
65 std::vector<CutContainer> cutsFD {
66  CutContainer(kNus20FDCuts, "FullSelection")
67 };
68 
69 std::vector<CutContainer> cutsND {
70  CutContainer(kNus20NDCuts, "FullSelection")
71 };
72 
73 
74 // function to add variables to the roster of spectra we want to produce
75 // ----------------------------------------------------------------------------
77 
78 // main function
79 // ----------------------------------------------------------------------------
80 void ExampleCode(TString opt){
81 
82  bool isCaf = opt.Contains("caf" , TString::kIgnoreCase);
83  bool isConcat = opt.Contains("concat", TString::kIgnoreCase);
84  assert(isCaf or isConcat);
85 
86  bool isND = opt.Contains("nd" , TString::kIgnoreCase);
87  bool isFD = opt.Contains("fd" , TString::kIgnoreCase);
88  bool isFDData = opt.Contains("fddata" , TString::kIgnoreCase);
89  bool isNDData = opt.Contains("nddata" , TString::kIgnoreCase);
90 
91  bool isCosmic = opt.Contains("cos" , TString::kIgnoreCase);
92  bool isSysts = opt.Contains("syst" , TString::kIgnoreCase);
93 
94  // ----------------------
95  // set up loaders
96  // ----------------------
97  // - prod5Nom loaders to take advantage of using both nonswap and swap files
98  // - FD data loader (obviously not useful right now)
99  // - ND data loader. Set to be ND burner Set
100  // - cosmic loader for cosmics files at the far detector
101 
102  // placeholders...
103  std::string kFDDataFHCNus20Concat = "";
104  std::string kFDDataFHCNus20CAF = "";
105  std::string kFDCosmicFHCProd5 = "prod_sumdecaf_R19-11-18-prod5reco.i_fd_cosmic_fhc_full_v1_goodruns_nus2020";
106  std::string kNDBurnerData = "nux_burner_prod_caf_R19-11-18-prod5reco.d.f.h_nd_numi_fhc_period235910_v1_goodruns";
107 
108  Loaders* loadersMC = new Prod5NomLoaders(
111  loadersMC->SetSpillCut(kStandardSpillCuts);
112 
113  SpectrumLoader* loadersFDData = nullptr;
114  if (isFDData) {
115  const std::string dataDef =
116  isConcat? kFDDataFHCNus20Concat : kFDDataFHCNus20CAF;
117  loadersFDData = new SpectrumLoader(dataDef);
118  loadersFDData->SetSpillCut(kStandardSpillCuts);
119  }
120 
121  SpectrumLoader* loadersNDData = nullptr;
122  if (isNDData) {
123  loadersNDData = new SpectrumLoader(kNDBurnerData);
124  loadersNDData->SetSpillCut(kStandardSpillCuts);
125  }
126 
127  SpectrumLoader* loadersCosmic = nullptr;
128  if (isCosmic){
129  loadersCosmic = new SpectrumLoader(kFDCosmicFHCProd5, kCosmic);
130  loadersCosmic->SetSpillCut(kStandardSpillCuts_FD_Prod4MotivatedDQ); // uses good runs list from prod4
131  }
132 
133  // --------------------------
134  // setup prediction handler
135  // --------------------------
136  // Need one prediction handler for the FD, one for the ND
137  // Set to use noextrap as default in the fd, decomp in the nd
138 
139  FDPredictionHandler fdPredH("NoExtrap");
140  fdPredH.AddLoaders(loadersMC);
141  if (isFDData)
142  fdPredH.AddData(loadersFDData);
143  if (isCosmic)
144  fdPredH.AddCosmics(loadersCosmic);
145 
146  NDPredictionHandler ndPredH("Decomp");
147  ndPredH.AddLoaders(loadersMC);
148  if (isNDData)
149  ndPredH.AddData(loadersNDData);
150 
151  if (isFD)
152  addVars(&fdPredH, "fd");
153 
154  if (isND)
155  addVars(&ndPredH, "nd");
156 
157  //--------------------------
158  // GO GO GO
159  // -------------------------
160 
161  loadersMC->Go();
162  if (isFDData)
163  loadersFDData->Go();
164  if (isNDData)
165  loadersNDData->Go();
166  if (isCosmic)
167  loadersCosmic->Go();
168 
169  std::cout << "got all spectra" << std::endl;
170 
171  // setup output file
172  std::string fileOutName = "SelectionSpectr_Unosc.root";
173  TFile* fileOut = new TFile(fileOutName.c_str(), "RECREATE");
174 
175  if (isFD)
176  fdPredH.Save(fileOut);
177  if (isND)
178  ndPredH.Save(fileOut);
179 
180  fileOut->Close();
181 
182 }
183 
185 
186  std::vector<VarContainer> vars;
187  std::vector<CutContainer> cuts;
188 
189  if (det == "nd"){
190  vars = varsND;
191  cuts = cutsND;
192  }
193  else if (det == "fd"){
194  vars = varsFD;
195  cuts = cutsFD;
196  }
197  else {
198  throw std::logic_error("options for detector are nd or fd");
199  }
200 
201  for (size_t iCut = 0; iCut < cuts.size(); ++iCut){
202 
203  ana::Cut fullCut = cuts.at(iCut).cut;
204  std::string fullName = cuts.at(iCut).name;
205 
206  for (size_t iVar = 0; iVar < vars.size(); ++iVar){
207 
208  std::cout << vars.at(iVar).name+fullName << std::endl;
209 
210  predH->AddVar((vars.at(iVar).name+fullName).c_str(),
211  vars.at(iVar).label.c_str(),
212  vars.at(iVar).var,
213  vars.at(iVar).bins,
214  fullCut,
215  kNoShift,
216  kUnweighted,
217  cuts.at(iCut).cutdata);
218  }
219  }
220 }
const SpillCut kStandardSpillCuts_FD_Prod4MotivatedDQ
Definition: SpillCuts.h:69
const Var kPartPtp([](const caf::SRProxy *sr){if(std::isnan(1.*sr->sel.nuecosrej.partptp)|| std::isinf(1.*sr->sel.nuecosrej.partptp)) return-5.f;return float(sr->sel.nuecosrej.partptp);})
Definition: NusVars.h:74
Represent the binning of a Spectrum&#39;s x-axis.
Definition: Binning.h:16
Cuts and Vars for the 2020 FD DiF Study.
Definition: vars.h:6
const ana::Binning kPartPtpBins
Definition: ExampleCode.C:53
Collection of SpectrumLoaders for many configurations.
Definition: Loaders.h:23
void addVars(PredictionHandler *predH, std::string det)
Definition: ExampleCode.C:184
void Save(TFile *outFile)
void AddData(SpectrumLoader *loadersData)
void SetSpillCut(const SpillCut &cut)
std::vector< VarContainer > varsFD
Definition: ExampleCode.C:55
const Cut kNus20NDCuts
Definition: NusCuts20.h:102
void Go()
Call Go() on all the loaders.
Definition: Loaders.cxx:162
void SetSpillCut(const SpillCut &cut)
Definition: Loaders.cxx:121
For nominal spectra and reweighting systs (xsec/flux)
Definition: Prod5Loaders.h:101
void AddLoaders(Loaders *loaders)
virtual void AddVar(std::string name, HistAxis axis, Cut cuts, SystShifts shift, Var weight, bool cutdata=true)=0
Class to handle ND predictions.
virtual void Go() override
Load all the registered spectra.
const std::map< std::pair< std::string, std::string >, Variable > vars
std::vector< CutContainer > cutsFD
Definition: ExampleCode.C:65
void AddCosmics(SpectrumLoader *loaders_cosmics)
const SystShifts kNoShift
Definition: SystShifts.cxx:22
OStream cout
Definition: OStream.cxx:6
std::vector< VarContainer > varsND
Definition: ExampleCode.C:59
void Save(TFile *outFile)
void ExampleCode(TString opt)
Definition: ExampleCode.C:80
void AddLoaders(Loaders *loaders)
Collaborates with Spectrum and OscillatableSpectrum to fill spectra from CAF files.
assert(nhit_max >=nhit_nbins)
const Cut kNus20FDCuts
Definition: NusCuts20.h:174
const SpillCut kStandardSpillCuts
Apply this unless you&#39;re doing something special.
Definition: SpillCuts.h:49
Class to handle FD predictions.
std::vector< CutContainer > cutsND
Definition: ExampleCode.C:69
static Binning Simple(int n, double lo, double hi, const std::vector< std::string > &labels={})
Definition: Binning.cxx:107
const Var kUnweighted
The simplest possible Var, always 1. Used as a default weight.
Definition: Var.h:96
void AddData(SpectrumLoader *loaders_data)
enum BeamMode string