NDPosWeights.cxx
Go to the documentation of this file.
2 
4 
6 
7 #include "TFile.h"
8 #include "TH2.h"
9 #include "TSystem.h"
10 
11 #include <iostream>
12 #include <map>
13 
14 namespace ana
15 {
16 
18  {
19  public:
21  const std::vector<std::string>& histNameVec,
22  const std::string& histDivisorName = "");
23 
24  TH1* Hist(const std::string& key) const;
25 
26  protected:
28  std::vector<std::string> fHistNameVec;
30 
31  mutable std::map<std::string, TH1*> fHistMap;
32  };
33 
34 
36  const std::vector<std::string>& histNameVec,
37  const std::string& histDivisorName)
38  : fFileName(filename),
39  fHistNameVec(histNameVec),
40  fHistDivisorName(histDivisorName)
41  {
42  }
43 
45  {
46  if(!fHistMap.empty()){
47  auto it = fHistMap.find(key);
48  if(it != fHistMap.end()){
49  return it->second;
50  }
51  else{
52  std::cout << "MultiHistContainer: unknown key " << key << std::endl;
53  abort();
54  }
55  }
56 
57  if(!gSystem->AccessPathName(fFileName.c_str())){
58  const std::unique_ptr<TFile> file(new TFile(fFileName.c_str(), "read"));
59  DontAddDirectory guard;
60  for(size_t index = 0; index < fHistNameVec.size(); ++index){
61  std::string histname = fHistNameVec.at(index);
62  TH1* hist = dynamic_cast<TH1*>(file->Get(histname.c_str()));
63  if(!hist)
64  throw std::runtime_error(Form("NDPosWeights.h: Couldn't load histogram '%s' from file: '%s'", histname.c_str(), fFileName.c_str()) );
65  fHistMap.insert(std::pair<std::string, TH1*>(histname, hist));
66  }//loop over hist name vec
67  }//if can accesspathname
68  else
69  throw std::runtime_error( Form("NDPosWeights.h: purported histogram file '%s' doesn't exist", fFileName.c_str()) );
70 
71  if(fHistDivisorName != ""){
72  std::map<std::string, TH1*>::iterator it = fHistMap.find(fHistDivisorName);
73  if(it == fHistMap.end())
74  throw std::runtime_error(Form("NDPosWeights.h: Couldn't find divisor histogram '%s' from file: '%s'", fHistDivisorName.c_str(), fFileName.c_str()));
75  TH1* histDivisor = it->second;
76 
77  for(it = fHistMap.begin(); it != fHistMap.end(); ++it){
78  if(it->first == fHistDivisorName) continue;
79  TH1* hist = it->second;
80  hist->Divide(histDivisor);
81  }//loop through histMap
82  }//if histDivisorName isn't blank
83 
84  // Map is filled now, search again
85  return Hist(key);
86  }
87 
88 
89  const MultiHistContainer kNDDetPosHistNumuCC( "/pnfs/nova/persistent/analysis/beam/nd_position/acceptanceCheck_forNorm_varyx.root",
90  { "enu1/NOvA-ND-Smear/enu1_numu_allpar_tot_cc_NOvA-ND-Smear",
91  "enu1/NOvA-ND-Smear-Off1/enu1_numu_allpar_tot_cc_NOvA-ND-Smear-Off1",
92  "enu1/NOvA-ND-Smear-Off2/enu1_numu_allpar_tot_cc_NOvA-ND-Smear-Off2",
93  "enu1/NOvA-ND-Smear-Off3/enu1_numu_allpar_tot_cc_NOvA-ND-Smear-Off3",
94  "enu1/NOvA-ND-Smear-Off4/enu1_numu_allpar_tot_cc_NOvA-ND-Smear-Off4"
95  },
96  "enu1/NOvA-ND-Smear/enu1_numu_allpar_tot_cc_NOvA-ND-Smear"
97  );
98 
99 
100  const Var kNDDetPosWeightNumuCCOff1([] (const caf::SRProxy *sr)
101  {
102  if(!sr->hdr.ismc)
103  return 1.;
104  if (sr->mc.nnu != 1)
105  return 1.;
106 
107  if(sr->mc.nu[0].iscc==1 &&
108  abs(sr->mc.nu[0].pdg)!=16 )
109  {
110  TH1* hist = kNDDetPosHistNumuCC.Hist("enu1/NOvA-ND-Smear-Off1/enu1_numu_allpar_tot_cc_NOvA-ND-Smear-Off1");
111 
112  return 1. * hist->GetBinContent(hist->FindBin(sr->mc.nu[0].E));
113  }//is CC nu mu
114  else{
115  return 1.;
116  }
117  }
118  );
119 
120  const Var kNDDetPosWeightNumuCCOff2([] (const caf::SRProxy *sr)
121  {
122  if(!sr->hdr.ismc)
123  return 1.;
124  if (sr->mc.nnu != 1)
125  return 1.;
126 
127  if(sr->mc.nu[0].iscc==1 &&
128  abs(sr->mc.nu[0].pdg)!=16 )
129  {
130  TH1* hist = kNDDetPosHistNumuCC.Hist("enu1/NOvA-ND-Smear-Off2/enu1_numu_allpar_tot_cc_NOvA-ND-Smear-Off2");
131 
132  return 1. * hist->GetBinContent(hist->FindBin(sr->mc.nu[0].E));
133  }//is CC nu mu
134  else{
135  return 1.;
136  }
137  }
138  );
139 
140  const Var kNDDetPosWeightNumuCCOff3([] (const caf::SRProxy *sr)
141  {
142  if(!sr->hdr.ismc)
143  return 1.;
144  if (sr->mc.nnu != 1)
145  return 1.;
146 
147  if(sr->mc.nu[0].iscc==1 &&
148  abs(sr->mc.nu[0].pdg)!=16 )
149  {
150  TH1* hist = kNDDetPosHistNumuCC.Hist("enu1/NOvA-ND-Smear-Off3/enu1_numu_allpar_tot_cc_NOvA-ND-Smear-Off3");
151 
152  return 1. * hist->GetBinContent(hist->FindBin(sr->mc.nu[0].E));
153  }//is CC nu mu
154  else{
155  return 1.;
156  }
157  }
158  );
159 
160  const Var kNDDetPosWeightNumuCCOff4([] (const caf::SRProxy *sr)
161  {
162  if(!sr->hdr.ismc)
163  return 1.;
164  if (sr->mc.nnu != 1)
165  return 1.;
166 
167  if(sr->mc.nu[0].iscc==1 &&
168  abs(sr->mc.nu[0].pdg)!=16 )
169  {
170  TH1* hist = kNDDetPosHistNumuCC.Hist("enu1/NOvA-ND-Smear-Off4/enu1_numu_allpar_tot_cc_NOvA-ND-Smear-Off4");
171 
172  return 1. * hist->GetBinContent(hist->FindBin(sr->mc.nu[0].E));
173  }//is CC nu mu
174  else{
175  return 1.;
176  }
177  }
178  );
179 
180 }//namespace
const Var kNDDetPosWeightNumuCCOff4([](const caf::SRProxy *sr){if(!sr->hdr.ismc) return 1.;if(sr->mc.nnu!=1) return 1.;if(sr->mc.nu[0].iscc==1 && abs(sr->mc.nu[0].pdg)!=16){TH1 *hist=kNDDetPosHistNumuCC.Hist("enu1/NOvA-ND-Smear-Off4/enu1_numu_allpar_tot_cc_NOvA-ND-Smear-Off4");return 1.*hist->GetBinContent(hist->FindBin(sr->mc.nu[0].E));}else{return 1.;}})
Definition: NDPosWeights.h:12
Cuts and Vars for the 2020 FD DiF Study.
Definition: vars.h:6
set< int >::iterator it
const Var kNDDetPosWeightNumuCCOff2([](const caf::SRProxy *sr){if(!sr->hdr.ismc) return 1.;if(sr->mc.nnu!=1) return 1.;if(sr->mc.nu[0].iscc==1 && abs(sr->mc.nu[0].pdg)!=16){TH1 *hist=kNDDetPosHistNumuCC.Hist("enu1/NOvA-ND-Smear-Off2/enu1_numu_allpar_tot_cc_NOvA-ND-Smear-Off2");return 1.*hist->GetBinContent(hist->FindBin(sr->mc.nu[0].E));}else{return 1.;}})
Definition: NDPosWeights.h:10
caf::Proxy< caf::SRHeader > hdr
Definition: SRProxy.h:2137
const Var kNDDetPosWeightNumuCCOff1([](const caf::SRProxy *sr){if(!sr->hdr.ismc) return 1.;if(sr->mc.nnu!=1) return 1.;if(sr->mc.nu[0].iscc==1 && abs(sr->mc.nu[0].pdg)!=16){TH1 *hist=kNDDetPosHistNumuCC.Hist("enu1/NOvA-ND-Smear-Off1/enu1_numu_allpar_tot_cc_NOvA-ND-Smear-Off1");return 1.*hist->GetBinContent(hist->FindBin(sr->mc.nu[0].E));}else{return 1.;}})
Definition: NDPosWeights.h:9
Proxy for caf::StandardRecord.
Definition: SRProxy.h:2126
caf::Proxy< std::vector< caf::SRNeutrino > > nu
Definition: SRProxy.h:618
void abs(TH1 *hist)
caf::Proxy< short int > nnu
Definition: SRProxy.h:617
string filename
Definition: shutoffs.py:106
TH1 * Hist(const std::string &key) const
if(dump)
std::vector< std::string > fHistNameVec
caf::StandardRecord * sr
const Var kNDDetPosWeightNumuCCOff3([](const caf::SRProxy *sr){if(!sr->hdr.ismc) return 1.;if(sr->mc.nnu!=1) return 1.;if(sr->mc.nu[0].iscc==1 && abs(sr->mc.nu[0].pdg)!=16){TH1 *hist=kNDDetPosHistNumuCC.Hist("enu1/NOvA-ND-Smear-Off3/enu1_numu_allpar_tot_cc_NOvA-ND-Smear-Off3");return 1.*hist->GetBinContent(hist->FindBin(sr->mc.nu[0].E));}else{return 1.;}})
Definition: NDPosWeights.h:11
MultiHistContainer(const std::string &filename, const std::vector< std::string > &histNameVec, const std::string &histDivisorName="")
std::string fHistDivisorName
OStream cout
Definition: OStream.cxx:6
const MultiHistContainer kNDDetPosHistNumuCC("/pnfs/nova/persistent/analysis/beam/nd_position/acceptanceCheck_forNorm_varyx.root",{"enu1/NOvA-ND-Smear/enu1_numu_allpar_tot_cc_NOvA-ND-Smear","enu1/NOvA-ND-Smear-Off1/enu1_numu_allpar_tot_cc_NOvA-ND-Smear-Off1","enu1/NOvA-ND-Smear-Off2/enu1_numu_allpar_tot_cc_NOvA-ND-Smear-Off2","enu1/NOvA-ND-Smear-Off3/enu1_numu_allpar_tot_cc_NOvA-ND-Smear-Off3","enu1/NOvA-ND-Smear-Off4/enu1_numu_allpar_tot_cc_NOvA-ND-Smear-Off4"},"enu1/NOvA-ND-Smear/enu1_numu_allpar_tot_cc_NOvA-ND-Smear")
caf::Proxy< caf::SRTruthBranch > mc
Definition: SRProxy.h:2138
std::map< std::string, TH1 * > fHistMap
caf::Proxy< bool > ismc
Definition: SRProxy.h:242
TFile * file
Definition: cellShifts.C:17
Prevent histograms being added to the current directory.
Definition: UtilsExt.h:46
enum BeamMode string