datamc_ND_numu_kinematics_REW.C
Go to the documentation of this file.
4 
5 // Core
8 #include "CAFAna/Core/Spectrum.h"
10 #include "CAFAna/Core/HistAxis.h"
11 #include "CAFAna/Core/Binning.h"
12 #include "CAFAna/Core/Cut.h"
13 
14 // Cuts
15 #include "CAFAna/Cuts/Cuts.h"
16 #include "CAFAna/Cuts/SpillCuts.h"
20 #include "CAFAna/Cuts/NumuCuts.h"
21 #include "CAFAna/Cuts/TruthCuts.h"
22 
23 // Vars
24 #include "CAFAna/Vars/Vars.h"
27 #include "CAFAna/Vars/NueVars.h"
28 #include "CAFAna/Vars/NumuVars.h"
30 
31 // Systs
32 #include "CAFAna/Systs/Systs.h"
33 #include "CAFAna/Analysis/Style.h"
34 #include "CAFAna/Decomp/IDecomp.h"
36 
38 
39 
40 #include "CAFAna/Core/SystShifts.h"
41 #include "CAFAna/Cuts/NumuCuts.h"
47 #include "TFile.h"
50 #include "CAFAna/Core/Spectrum.h"
51 #include "TH1.h"
53 #include "TCanvas.h"
55 #include "CAFAna/Analysis/Plots.h"
56 #include "CAFAna/Vars/FitVars.h"
57 #include "CAFAna/Cuts/Cuts.h"
58 #include "CAFAna/Cuts/NumuCuts.h"
59 
60 #include "TStopwatch.h"
61 #include "CAFAna/Core/Utilities.h"
62 
63 
64 
65 #include <fstream>
66 #include <iostream>
67 #include <cmath>
68 #include <string>
69 
70 using namespace ana;
71 
73 
74 // ROOT
75 #include "TCanvas.h"
76 #include "TFile.h"
77 #include "TH1.h"
78 #include "TLegend.h"
79 #include "TPad.h"
80 #include "TMath.h"
81 #include "TH2.h"
82 #include "TLine.h"
83 #include "TText.h"
84 #include "TString.h"
85 
86 #include "Utilities/func/MathUtil.h"
87 
88 
89 //define some useful variables:
90 
92 {
93 
94  std::string nameData = "defname: prod_decaf_R17-03-01-prod3reco.d_nd_numi_fhc_full_nue_or_numu_or_nus_contain_v1_goodruns";
95  std::string nameMC = "defname: prod_decaf_R17-03-01-prod3reco.d_nd_genie_nonswap_fhc_nova_v08_full_nue_or_numu_or_nus_contain_v1";
96 
97  //choose your samweb datasets or concat files:
98  SpectrumLoader loaderData(nameData);
99  SpectrumLoader loaderMC(nameMC);
100 
101  loaderData.SetSpillCut(kStandardSpillCuts);
102  loaderMC.SetSpillCut(kStandardDQCuts);
103 
104  //which variables do you want to look at?
105  const int kNumVars = 7;
106 
107  const HistDef defs[kNumVars] = {
108  {"recoE", {"E_{reco} (GeV)", Binning::Simple(100, 0, 5), kCCE}},
109  {"trueQ2", {"true Q^{2} (GeV^{2})", Binning::Simple(500, 0, 5), kTrueQ2}},
110  {"recoQ2", {"reco Q^{2} (GeV^{2})", Binning::Simple(500, 0, 5), kRecoQ2}},
111  {"trueW2", {"true W^{2} (GeV^{2})", Binning::Simple(100, 0, 2), kTrueW}},
112  {"trueE", {"true E (GeV)", Binning::Simple(100, 0, 5), kTrueE}},
113  {"PtP", {"p_{t}/p", Binning::Simple(500,0,1), kPtP}},
114  {"CosNumi", {"CosNumi", Binning::Simple(500,0,1), kCosNumi}}
115  };
116 
117  TFile *weight_file = new TFile("/nova/app/users/nostom66/nue/extrapsysts/combined/Weights.root","READ");
118 
119  //trueQ2 weight
120  TH1 *h = new TH1();
121  gDirectory->GetObject("trueQ2_weight_AllSamples",h);
122 
123  std::cout << h->GetNbinsX() << endl;
124  int nbins = h->GetNbinsX();
125  double bins[nbins], binslow[nbins], binshigh[nbins];
126 
127  for(int i = 1; i <= nbins; ++i){
128  bins[i-1] = h->GetBinContent(i);
129  binslow[i-1] = h->GetBinLowEdge(i);
130  binshigh[i-1] = h->GetBinLowEdge(i) + h->GetBinWidth(i);
131  }
132 
133  const Var kTrueQ2Weight([nbins, &binslow, &binshigh, &bins](const caf::SRProxy *sr)
134  {
135  for(int i = 0; i < nbins; ++i){
136  if((kRecoQ2(sr) > binslow[i]) && (kRecoQ2(sr) < binshigh[i])){return bins[i];}
137  }
138  return 1.0;
139  }
140  );
141 
142  //PtP weight
143  TH1 *hptp = new TH1();
144  gDirectory->GetObject("PtP_weight_AllSamples",hptp);
145 
146  std::cout << hptp->GetNbinsX() << endl;
147  int nbins_ptp = hptp->GetNbinsX();
148  double bins_ptp[nbins_ptp], binslow_ptp[nbins_ptp], binshigh_ptp[nbins_ptp];
149 
150  for(int i = 1; i <= nbins_ptp; ++i){
151  bins_ptp[i-1] = hptp->GetBinContent(i);
152  binslow_ptp[i-1] = hptp->GetBinLowEdge(i);
153  binshigh_ptp[i-1] = hptp->GetBinLowEdge(i) + hptp->GetBinWidth(i);
154  }
155 
156  const Var kPtPWeight([nbins_ptp, &binslow_ptp, &binshigh_ptp, &bins_ptp](const caf::SRProxy *sr)
157  {
158  for(int i = 0; i < nbins_ptp; ++i){
159  if((kPtP(sr) > binslow_ptp[i]) && (kPtP(sr) < binshigh_ptp[i])){return bins_ptp[i];}
160  }
161  return 1.0;
162  }
163  );
164 
165  //Cos weight
166  TH1 *hcos = new TH1();
167  gDirectory->GetObject("CosNumi_weight_AllSamples",hcos);
168 
169  std::cout << hcos->GetNbinsX() << endl;
170  int nbins_cos = hptp->GetNbinsX();
171  double bins_cos[nbins_cos], binslow_cos[nbins_cos], binshigh_cos[nbins_cos];
172 
173  for(int i = 1; i <= nbins_cos; ++i){
174  bins_cos[i-1] = hcos->GetBinContent(i);
175  binslow_cos[i-1] = hcos->GetBinLowEdge(i);
176  binshigh_cos[i-1] = hcos->GetBinLowEdge(i) + hcos->GetBinWidth(i);
177  }
178 
179  const Var kCosWeight([nbins_cos, &binslow_cos, &binshigh_cos, &bins_cos](const caf::SRProxy *sr)
180  {
181  for(int i = 0; i < nbins_cos; ++i){
182  if((kCosNumi(sr) > binslow_cos[i]) && (kCosNumi(sr) < binshigh_cos[i])){return bins_cos[i];}
183  }
184  return 1.0;
185  }
186  );
187 
188  weight_file->Close();
189 
190  const Cut kIsRock(
191  [](const caf::SRProxy* sr)
192  {
193  if (sr->mc.nnu == 0)
194  return false;
195  if (fabs(sr->mc.nu[0].vtx.X())>180 ||
196  fabs(sr->mc.nu[0].vtx.Y())>180 ||
197  sr->mc.nu[0].vtx.Z()<0 ||
198  sr->mc.nu[0].vtx.Z()>1250)
199  return true;
200  return false;
201  });
202 
203  const int kNumSels = 1;
204  const Cut sels[kNumSels] = {
205  kNumuNDCvn
206  };
207  const std::string selNames[kNumSels] = {
208  "numuND"
209  };
210 
211  Spectrum* spects_trueQ2[kNumSels][kNumVars];
212  Spectrum* spects_PtP[kNumSels][kNumVars];
213  Spectrum* spects_Cos[kNumSels][kNumVars];
214  IPrediction* preds_trueQ2[kNumSels][kNumVars];
215  IPrediction* preds_PtP[kNumSels][kNumVars];
216  IPrediction* preds_Cos[kNumSels][kNumVars];
217 
218  //loop over the selectors and variables to create a full selection
219  for(int selIdx = 0; selIdx < kNumSels; ++selIdx){
220  for(int varIdx = 0; varIdx < kNumVars; ++varIdx){
221  const HistAxis& axis = defs[varIdx].axis;
222 
223  // trueQ2 reweighted
224  spects_trueQ2[selIdx][varIdx] = new Spectrum(loaderData, axis, sels[selIdx], kNoShift, kTrueQ2Weight);
225  preds_trueQ2[selIdx][varIdx] = new PredictionNoExtrap(loaderMC, kNullLoader, kNullLoader,
226  axis.GetLabels()[0], axis.GetBinnings()[0], axis.GetVars()[0],
227  sels[selIdx], kNoShift, kXSecCVWgt2017*kPPFXFluxCVWgt*kTrueQ2Weight);
228  // PtP reweighted
229  spects_PtP[selIdx][varIdx] = new Spectrum(loaderData, axis, sels[selIdx], kNoShift, kPtPWeight);
230  preds_PtP[selIdx][varIdx] = new PredictionNoExtrap(loaderMC, kNullLoader, kNullLoader,
231  axis.GetLabels()[0], axis.GetBinnings()[0], axis.GetVars()[0],
232  sels[selIdx], kNoShift, kXSecCVWgt2017*kPPFXFluxCVWgt*kPtPWeight);
233  // CosTheta reweighted
234  spects_Cos[selIdx][varIdx] = new Spectrum(loaderData, axis, sels[selIdx], kNoShift, kCosWeight);
235  preds_Cos[selIdx][varIdx] = new PredictionNoExtrap(loaderMC, kNullLoader, kNullLoader,
236  axis.GetLabels()[0], axis.GetBinnings()[0], axis.GetVars()[0],
237  sels[selIdx], kNoShift, kXSecCVWgt2017*kPPFXFluxCVWgt*kCosWeight);
238 
239  }
240  }
241 
242  loaderMC.Go();
243  loaderData.Go();
244 
245  TString fname = "datamc_ND_numu_kinematics_REW.root";
246  TFile* fout = new TFile(fname, "RECREATE");
247  for(int selIdx = 0; selIdx < kNumSels; ++selIdx){
248  TDirectory* d = fout->mkdir(selNames[selIdx].c_str());
249  for(int varIdx = 0; varIdx < kNumVars; ++varIdx){
250  const char* name = defs[varIdx].name.c_str();
251  spects_trueQ2[selIdx][varIdx]->SaveTo(d->mkdir(TString::Format("spect_trueQ2_%s", name)));
252  preds_trueQ2[selIdx][varIdx]->SaveTo(d->mkdir(TString::Format("pred_trueQ2_%s", name)));
253 
254  spects_PtP[selIdx][varIdx]->SaveTo(d->mkdir(TString::Format("spect_PtP_%s", name)));
255  preds_PtP[selIdx][varIdx]->SaveTo(d->mkdir(TString::Format("pred_PtP_%s", name)));
256 
257  spects_Cos[selIdx][varIdx]->SaveTo(d->mkdir(TString::Format("spect_Cos_%s", name)));
258  preds_Cos[selIdx][varIdx]->SaveTo(d->mkdir(TString::Format("pred_Cos_%s", name)));
259  }
260  }
261 }
const XML_Char * name
Definition: expat.h:151
const int kNumVars
Definition: vars.h:14
Oscillation analysis framework, runs over CAF files outside of ART.
fvar< T > fabs(const fvar< T > &x)
Definition: fabs.hpp:15
const Var kPPFXFluxCVWgt
Definition: PPFXWeights.h:16
VectorProxy< SRNeutrinoProxy > nu
Definition: SRProxy.h:1890
virtual void SaveTo(TDirectory *dir) const
std::vector< T > GetVars() const
Definition: HistAxis.h:79
HistAxis axis
Definition: NuePlotLists.h:13
Proxy for StandardRecord.
Definition: SRProxy.h:2237
void SetSpillCut(const SpillCut &cut)
void datamc_ND_numu_kinematics_REW()
const Var kTrueQ2
Definition: TruthVars.h:27
Representation of a spectrum in any variable, with associated POT.
Definition: Spectrum.h:37
const int nbins
Definition: cellShifts.C:15
const Var kPtP
Transverse momentum fraction in slice.
Definition: NueVars.cxx:90
Proxy< short int > nnu
Definition: SRProxy.h:1899
const Var kTrueE([](const caf::SRProxy *sr){return(sr->mc.nnu==0)?0.:float(sr->mc.nu[0].E);})
Definition: Vars.cxx:87
if(dump)
const Cut sels[kNumSels]
Definition: vars.h:44
const Cut kNumuNDCvn
Definition: NumuCuts.h:61
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:12
const Var kCCE
Definition: Vars.h:90
Float_t d
Definition: plot.C:236
virtual void Go() override
Load all the registered spectra.
const HistDef defs[kNumVars]
Definition: vars.h:15
const int kNumSels
Definition: vars.h:43
std::string name
Definition: NuePlotLists.h:12
const SystShifts kNoShift
Definition: SystShifts.h:112
OStream cout
Definition: OStream.cxx:6
const Binning bins
Definition: NumuCC_CPiBin.h:8
const ana::Var kRecoQ2([](const caf::SRProxy *sr){const double M_mu_sqrd=util::sqr(0.1056);double E_mu=kNumuTrackE(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:128
::xsd::cxx::tree::string< char, simple_type > string
Definition: Database.h:154
Collaborates with Spectrum and OscillatableSpectrum to fill spectra from CAF files.
SRTruthBranchProxy mc
Definition: SRProxy.h:2253
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:28
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
const Var kXSecCVWgt2017
Definition: XsecTunes.h:38
static Binning Simple(int n, double lo, double hi, const std::vector< std::string > &labels={})
Definition: Binning.cxx:38
const Var kTrueW
Definition: TruthVars.h:22
std::vector< std::string > GetLabels() const
Definition: HistAxis.h:77
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:27
static NullLoader kNullLoader
Dummy loader that doesn&#39;t load any files.
std::vector< Binning > GetBinnings() const
Definition: HistAxis.h:78
static constexpr Double_t sr
Definition: Munits.h:164
h
Definition: demo3.py:41
void SaveTo(TDirectory *dir) const
Definition: Spectrum.cxx:1029