datamc_ND_numu_kinematics_RHC_REW.C
Go to the documentation of this file.
1 
5 
6 // Core
9 #include "CAFAna/Core/Spectrum.h"
11 #include "CAFAna/Core/HistAxis.h"
12 #include "CAFAna/Core/Binning.h"
13 #include "CAFAna/Core/Cut.h"
14 
15 // Cuts
16 #include "CAFAna/Cuts/Cuts.h"
17 #include "CAFAna/Cuts/SpillCuts.h"
18 #include "CAFAna/Cuts/NueCutsFirstAna.h"
19 #include "CAFAna/Cuts/NueCutsSecondAna.h"
24 #include "CAFAna/Cuts/TruthCuts.h"
25 
26 // Vars
27 #include "CAFAna/Vars/Vars.h"
33 #include "CAFAna/Vars/HistAxes.h"
34 
35 // Systs
36 #include "CAFAna/Systs/Systs.h"
37 #include "CAFAna/Analysis/Style.h"
38 #include "CAFAna/Decomp/IDecomp.h"
40 
41 #include "OscLib/OscCalcPMNSOpt.h"
42 
43 
44 #include "CAFAna/Core/SystShifts.h"
51 #include "TFile.h"
52 #include "OscLib/OscCalcPMNSOpt.h"
53 #include "OscLib/OscCalcDumb.h"
54 #include "CAFAna/Core/Spectrum.h"
55 #include "TH1.h"
57 #include "TCanvas.h"
59 #include "CAFAna/Analysis/Plots.h"
61 #include "CAFAna/Vars/FitVars.h"
62 #include "CAFAna/Cuts/Cuts.h"
64 
65 #include "TStopwatch.h"
66 #include "CAFAna/Core/Utilities.h"
67 
68 
69 
70 #include <fstream>
71 #include <iostream>
72 #include <cmath>
73 #include <string>
74 
75 using namespace ana;
76 
77 #include "OscLib/IOscCalc.h"
78 
79 // ROOT
80 #include "TCanvas.h"
81 #include "TFile.h"
82 #include "TH1.h"
83 #include "TLegend.h"
84 #include "TPad.h"
85 #include "TMath.h"
86 #include "TH2.h"
87 #include "TLine.h"
88 #include "TText.h"
89 #include "TString.h"
90 
91 #include "Utilities/func/MathUtil.h"
92 
93 
94 //define some useful variables:
95 
97 {
98 
99  std::string nameData = "prod_caf_R17-09-05-prod4recopreview.f_nd_numi_rhc_full_v1_addShortSimpleCVN_goodruns";
100  std::string nameMC = "prod_caf_R17-11-14-prod4reco.e_nd_genie_nonswap_rhc_nova_v08_full_v1";
101 
102  //choose your samweb datasets or concat files:
103  SpectrumLoader loaderData(nameData);
104  SpectrumLoader loaderMC(nameMC);
105 
106  loaderData.SetSpillCut(kStandardSpillCuts);
107  loaderMC.SetSpillCut(kStandardDQCuts);
108 
109  //which variables do you want to look at?
110  const int kNumVars = 8;
111 
112  const HistDef defs[kNumVars] = {
113  {"numu", kNumuCCAxis},
114  {"recoE", {"E_{reco} (GeV)", Binning::Simple(100, 0, 5), kCCE}},
115  {"trueQ2", {"true Q^{2} (GeV^{2})", Binning::Simple(500, 0, 5), kTrueQ2}},
116  {"recoQ2", {"reco Q^{2} (GeV^{2})", Binning::Simple(500, 0, 5), kRecoQ2}},
117  {"trueW2", {"true W^{2} (GeV^{2})", Binning::Simple(100, 0, 2), kTrueW}},
118  {"trueE", {"true E (GeV)", Binning::Simple(100, 0, 5), kTrueE}},
119  {"PtP", {"p_{t}/p", Binning::Simple(500,0,1), kPtP}},
120  {"CosNumi", {"CosNumi", Binning::Simple(500,0,1), kCosNumi}}
121  };
122 
123  const int kNumSels = 1;
124  const Cut sels[kNumSels] = {
126  };
127  const std::string selNames[kNumSels] = {
128  sample
129  };
130 
131  TFile *weight_file = new TFile(("/nova/app/users/nostom66/nue/ExtrapSyst/NDFDkinematics/RHC/Weights_"+sample+"_RHC.root").c_str(),"READ");
132 
133  //trueQ2 weight
134  TH1 *h;
135  gDirectory->GetObject(("trueQ2_weight_"+sample).c_str(),h);
136 
137  std::cout << h->GetNbinsX() << endl;
138  int nbins = h->GetNbinsX();
139  double bins[nbins], binslow[nbins], binshigh[nbins];
140 
141  for(int i = 1; i <= nbins; ++i){
142  bins[i-1] = h->GetBinContent(i);
143  binslow[i-1] = h->GetBinLowEdge(i);
144  binshigh[i-1] = h->GetBinLowEdge(i) + h->GetBinWidth(i);
145  }
146 
147  const Var kTrueQ2Weight([nbins, &binslow, &binshigh, &bins](const caf::SRProxy *sr)
148  {
149  for(int i = 0; i < nbins; ++i){
150  if((kRecoQ2(sr) > binslow[i]) && (kRecoQ2(sr) < binshigh[i])){return bins[i];}
151  }
152  return 1.0;
153  }
154  );
155 
156  //PtP weight
157  TH1 *hptp;;
158  gDirectory->GetObject(("PtP_weight_"+sample).c_str(),hptp);
159 
160  std::cout << hptp->GetNbinsX() << endl;
161  int nbins_ptp = hptp->GetNbinsX();
162  double bins_ptp[nbins_ptp], binslow_ptp[nbins_ptp], binshigh_ptp[nbins_ptp];
163 
164  for(int i = 1; i <= nbins_ptp; ++i){
165  bins_ptp[i-1] = hptp->GetBinContent(i);
166  binslow_ptp[i-1] = hptp->GetBinLowEdge(i);
167  binshigh_ptp[i-1] = hptp->GetBinLowEdge(i) + hptp->GetBinWidth(i);
168  }
169 
170  const Var kPtPWeight([nbins_ptp, &binslow_ptp, &binshigh_ptp, &bins_ptp](const caf::SRProxy *sr)
171  {
172  for(int i = 0; i < nbins_ptp; ++i){
173  if((kPtP(sr) > binslow_ptp[i]) && (kPtP(sr) < binshigh_ptp[i])){return bins_ptp[i];}
174  }
175  return 1.0;
176  }
177  );
178 
179  //Cos weight
180  TH1 *hcos;
181  gDirectory->GetObject(("CosNumi_weight_"+sample).c_str(),hcos);
182 
183  std::cout << hcos->GetNbinsX() << endl;
184  int nbins_cos = hptp->GetNbinsX();
185  double bins_cos[nbins_cos], binslow_cos[nbins_cos], binshigh_cos[nbins_cos];
186 
187  for(int i = 1; i <= nbins_cos; ++i){
188  bins_cos[i-1] = hcos->GetBinContent(i);
189  binslow_cos[i-1] = hcos->GetBinLowEdge(i);
190  binshigh_cos[i-1] = hcos->GetBinLowEdge(i) + hcos->GetBinWidth(i);
191  }
192 
193  const Var kCosWeight([nbins_cos, &binslow_cos, &binshigh_cos, &bins_cos](const caf::SRProxy *sr)
194  {
195  for(int i = 0; i < nbins_cos; ++i){
196  if((kCosNumi(sr) > binslow_cos[i]) && (kCosNumi(sr) < binshigh_cos[i])){return bins_cos[i];}
197  }
198  return 1.0;
199  }
200  );
201 
202  weight_file->Close();
203 
204  Spectrum* spects[kNumSels][kNumVars];
205  Spectrum* spects_trueQ2[kNumSels][kNumVars];
206  Spectrum* spects_PtP[kNumSels][kNumVars];
207  Spectrum* spects_Cos[kNumSels][kNumVars];
208  IPrediction* preds[kNumSels][kNumVars];
209  IPrediction* preds_trueQ2[kNumSels][kNumVars];
210  IPrediction* preds_PtP[kNumSels][kNumVars];
211  IPrediction* preds_Cos[kNumSels][kNumVars];
212 
213  //loop over the selectors and variables to create a full selection
214  for(int selIdx = 0; selIdx < kNumSels; ++selIdx){
215  for(int varIdx = 0; varIdx < kNumVars; ++varIdx){
216  const HistAxis& axis = defs[varIdx].axis;
217 
218  // non-reweighted
219  spects[selIdx][varIdx] = new Spectrum(loaderData, axis, sels[selIdx], kNoShift);
220  preds[selIdx][varIdx] = new PredictionNoExtrap(loaderMC, kNullLoader, kNullLoader,
221  axis.GetLabels()[0], axis.GetBinnings()[0], axis.GetVars()[0],
222  sels[selIdx], kNoShift, kXSecCVWgt2018*kPPFXFluxCVWgt);
223 
224  // trueQ2 reweighted
225  spects_trueQ2[selIdx][varIdx] = new Spectrum(loaderData, axis, sels[selIdx], kNoShift, kTrueQ2Weight);
226  preds_trueQ2[selIdx][varIdx] = new PredictionNoExtrap(loaderMC, kNullLoader, kNullLoader,
227  axis.GetLabels()[0], axis.GetBinnings()[0], axis.GetVars()[0],
228  sels[selIdx], kNoShift, kXSecCVWgt2018*kPPFXFluxCVWgt*kTrueQ2Weight);
229  // PtP reweighted
230  spects_PtP[selIdx][varIdx] = new Spectrum(loaderData, axis, sels[selIdx], kNoShift, kPtPWeight);
231  preds_PtP[selIdx][varIdx] = new PredictionNoExtrap(loaderMC, kNullLoader, kNullLoader,
232  axis.GetLabels()[0], axis.GetBinnings()[0], axis.GetVars()[0],
233  sels[selIdx], kNoShift, kXSecCVWgt2018*kPPFXFluxCVWgt*kPtPWeight);
234  // CosTheta reweighted
235  spects_Cos[selIdx][varIdx] = new Spectrum(loaderData, axis, sels[selIdx], kNoShift, kCosWeight);
236  preds_Cos[selIdx][varIdx] = new PredictionNoExtrap(loaderMC, kNullLoader, kNullLoader,
237  axis.GetLabels()[0], axis.GetBinnings()[0], axis.GetVars()[0],
238  sels[selIdx], kNoShift, kXSecCVWgt2018*kPPFXFluxCVWgt*kCosWeight);
239 
240  }
241  }
242 
243  loaderMC.Go();
244  loaderData.Go();
245 
246  TString fname = "datamc_ND_numu_kinematics_REW.root";
247  TFile* fout = new TFile(fname, "RECREATE");
248  for(int selIdx = 0; selIdx < kNumSels; ++selIdx){
249  TDirectory* d = fout->mkdir(selNames[selIdx].c_str());
250  for(int varIdx = 0; varIdx < kNumVars; ++varIdx){
251  const char* name = defs[varIdx].name.c_str();
252  spects[selIdx][varIdx]->SaveTo(d, TString::Format("spect_noweight_%s", name));
253  preds[selIdx][varIdx]->SaveTo(d, TString::Format("pred_noweight_%s", name));
254 
255  spects_trueQ2[selIdx][varIdx]->SaveTo(d, TString::Format("spect_trueQ2_%s", name));
256  preds_trueQ2[selIdx][varIdx]->SaveTo(d, TString::Format("pred_trueQ2_%s", name));
257 
258  spects_PtP[selIdx][varIdx]->SaveTo(d, TString::Format("spect_PtP_%s", name));
259  preds_PtP[selIdx][varIdx]->SaveTo(d, TString::Format("pred_PtP_%s", name));
260 
261  spects_Cos[selIdx][varIdx]->SaveTo(d, TString::Format("spect_Cos_%s", name));
262  preds_Cos[selIdx][varIdx]->SaveTo(d, TString::Format("pred_Cos_%s", name));
263  }
264  }
265 }
266 
const XML_Char * name
Definition: expat.h:151
const ana::Var kRecoQ2([](const caf::SRProxy *sr){const double M_mu_sqrd=util::sqr(0.1056);double E_mu=kMuE(sr);double p_mu=sqrt(util::sqr(E_mu)-M_mu_sqrd);return 2 *kCCE(sr)*(E_mu-p_mu *kCosNumi(sr))-M_mu_sqrd;})
Reconstructed four-momentum transfer invariant (Q^2)
Definition: NumuVars.h:146
const int kNumVars
Definition: vars.h:14
Cuts and Vars for the 2020 FD DiF Study.
Definition: vars.h:6
const std::vector< T > & GetVars() const
Definition: HistAxis.h:92
const Var kPPFXFluxCVWgt
Definition: PPFXWeights.h:16
const Cut kNumuCutND2018
Definition: NumuCuts2018.h:41
Proxy for caf::StandardRecord.
Definition: SRProxy.h:2085
std::string name
Definition: NuePlotLists.h:12
virtual void SaveTo(TDirectory *dir, const std::string &name) const
void SetSpillCut(const SpillCut &cut)
const Var kTrueQ2
Definition: TruthVars.h:27
Representation of a spectrum in any variable, with associated POT.
Definition: Spectrum.h:40
const int nbins
Definition: cellShifts.C:15
const Var kPtP
Transverse momentum fraction in slice.
Definition: NueVars.cxx:90
const Var kTrueE([](const caf::SRProxy *sr){return(sr->mc.nnu==0)?0.:float(sr->mc.nu[0].E);})
Definition: Vars.cxx:85
const Cut sels[kNumSels]
Definition: vars.h:44
const HistAxis kNumuCCAxis("Reconstructed Neutrino Energy (GeV)", kNumuEnergyBinning, kCCE)
Definition: HistAxes.h:8
const SpillCut kStandardDQCuts([](const caf::SRSpillProxy *spill){if(spill->dcmedgematchfrac==0 &&spill->fracdcm3hits==0 &&spill->nmissingdcmslg==0) return bool(spill->isgoodspill); if(spill->det==caf::kNEARDET && (spill->fracdcm3hits > 0.45|| spill->nmissingdcms > 0)) return false; if(spill->eventincomplete) return false; if(spill->det==caf::kFARDET && spill->nmissingdcmslg > 0) return false; if(spill->det==caf::kFARDET && !spill->ismc && spill->dcmedgematchfrac<=0.2) return false;return true;})
Cut out events with a noisy detector or with parts missing.
Definition: SpillCuts.h:16
const Var kCCE
Definition: NumuVars.h:21
Float_t d
Definition: plot.C:236
caf::StandardRecord * sr
virtual void Go() override
Load all the registered spectra.
void SaveTo(TDirectory *dir, const std::string &name) const
Definition: Spectrum.cxx:517
const HistDef defs[kNumVars]
Definition: vars.h:15
const int kNumSels
Definition: vars.h:43
std::vector< float > Spectrum
Definition: Constants.h:570
HistAxis axis
Definition: NuePlotLists.h:13
const SystShifts kNoShift
Definition: SystShifts.cxx:21
OStream cout
Definition: OStream.cxx:6
const std::vector< Binning > & GetBinnings() const
Definition: LabelsAndBins.h:69
const Binning bins
Definition: NumuCC_CPiBin.h:8
::xsd::cxx::tree::string< char, simple_type > string
Definition: Database.h:154
void datamc_ND_numu_kinematics_RHC_REW(const std::string sample)
Collaborates with Spectrum and OscillatableSpectrum to fill spectra from CAF files.
const Var kXSecCVWgt2018
Definition: XsecTunes.h:49
Standard interface to all prediction techniques.
Definition: IPrediction.h:57
const SpillCut kStandardSpillCuts
Apply this unless you&#39;re doing something special.
Definition: SpillCuts.h:49
void Format(TGraph *gr, int lcol, int lsty, int lwid, int mcol, int msty, double msiz)
Definition: Style.cxx:154
Prediction that just uses FD MC, with no extrapolation.
const std::string selNames[kNumSels]
Definition: vars.h:46
static Binning Simple(int n, double lo, double hi, const std::vector< std::string > &labels={})
Definition: Binning.cxx:114
const Var kTrueW
Definition: TruthVars.h:22
const Var kCosNumi([](const caf::SRProxy *sr){if(sr->trk.kalman.ntracks > 0 &&sr->trk.kalman.idxremid!=999){if(sr->hdr.det==1){return sr->trk.kalman.tracks[0].dir.Dot(beamDirND);}if(sr->hdr.det==2){return sr->trk.kalman.tracks[0].dir.Dot(beamDirFD);}}return-5.f;})
Definition: NumuVars.h:43
static NullLoader kNullLoader
Dummy loader that doesn&#39;t load any files.
const std::vector< std::string > & GetLabels() const
Definition: LabelsAndBins.h:68