Geant4ReweightSysts.cc
Go to the documentation of this file.
1 #include <iostream>
2 #include <fstream>
3 #include <sstream>
4 #include <string>
5 #include <vector>
6 
9 
11 
12 #include "TFile.h"
13 
14 //Necessary defs
17 //////////////////////////////////////////
18 
19 //Functions
20 bool parseArgs(int argc, char ** argv);
22 void saveHists(std::vector<TH1 *> & vec/*, std::string addToName*/);
23 void makeFracErrorHist(TH1 * variation, TH1 * nominal);
24 /////////////////////////////////////////
25 
26 typedef std::vector<std::string> string_v;
27 typedef std::vector<int> int_v;
28 typedef std::vector<double> double_v;
29 
30 struct systConfig {
31 
33  : MCFileNames(pset.get<string_v>("MCFileNames")),
35  pset.get<string_v>("BackgroundTopologyName")),
37  pset.get<string_v>("SignalTopologyName")),
38  ChannelNames(pset.get<string_v>("ChannelNames")),
40  pset.get<string_v>("IncidentMCFileNames")),
42  pset.get<string_v>("IncidentTopologyName")),
43  RecoBinning(pset.get<double_v>("RecoBinning")),
44  TruthBinning(pset.get<double_v>("TruthBinning")),
45  RecoTreeName(pset.get<std::string>("RecoTreeName")),
46  TruthTreeName(pset.get<std::string>("TruthTreeName")),
47  SignalTopology(pset.get<int_v>("SignalTopology")),
48  BackgroundTopology(pset.get<int_v>("BackgroundTopology")),
49  IncidentTopology(pset.get<int_v>("IncidentTopology")),
50  SystToConsider(pset.get<string_v>("SystToConsider")),
51  EndZCut(pset.get<double>("EndZCut")) {};
52 
63 
67 
69 
70  double EndZCut;
71 };
72 
73 
74 int main(int argc, char ** argv) {
75 
76  if (!parseArgs(argc, argv)) return 0;
77 
79 
80  systConfig cfg(pset);
81  double tmin = cfg.TruthBinning[0];
82  double tmax = cfg.TruthBinning.back();
83 
84  std::vector<TH1 *> bg_syst_hists;
85  std::vector<TH1 *> bg_nom_hists;
86  std::vector<TH1 *> sig_syst_hists;
87  std::vector<TH1 *> sig_nom_hists;
88  std::vector<TH1 *> inc_nom_hists;
89  std::vector<TH1 *> inc_syst_hists;
90 
91  for (size_t i = 0; i < cfg.ChannelNames.size(); ++i) {
92 
93  //Backgrounds
94  for (size_t j = 0; j < cfg.BackgroundTopology.size(); ++j) {
95  int topo = cfg.BackgroundTopology[j];
96 
97  //Get nominal
98  bg_nom_hists.push_back(
100  cfg.MCFileNames[i], cfg.RecoTreeName, cfg.RecoBinning,
101  cfg.ChannelNames[i], cfg.BackgroundTopologyName[j], topo,
102  cfg.EndZCut, tmin, tmax, false/*cfg.DoNegativeReco*/, 0));
103 
104  //Get Variations
105  for (size_t k = 0; k < cfg.SystToConsider.size(); ++k) {
106  std::cout << "Considering " << cfg.SystToConsider[k] << std::endl;
107  for (auto const m : {-1, 1}) {
108  bg_syst_hists.push_back(
110  cfg.MCFileNames[i], cfg.RecoTreeName, cfg.RecoBinning,
111  cfg.ChannelNames[i], cfg.BackgroundTopologyName[j], topo,
112  cfg.EndZCut, tmin, tmax, false/*cfg.DoNegativeReco*/,
113  /*doSyst=*/m, /*systName=*/cfg.SystToConsider[k]));
114 
115  //Turn into frac errors
116  makeFracErrorHist(bg_syst_hists.back(), bg_nom_hists.back());
117  }
118  }
119  }
120 
121  //Signals
122  for (size_t j = 0; j < cfg.SignalTopology.size(); ++j) {
123  int topo = cfg.SignalTopology[j];
124  for (size_t k = 1; k < cfg.TruthBinning.size(); ++k) {
125 
126  //Get Nominal
127  sig_nom_hists.push_back(
129  cfg.MCFileNames[i], cfg.RecoTreeName, cfg.RecoBinning,
130  cfg.ChannelNames[i], cfg.SignalTopologyName[j], topo,
131  cfg.TruthBinning[k-1], cfg.TruthBinning[k], cfg.EndZCut,
132  false/*cfg.DoNegativeReco*/));
133 
134  //Get Variations
135  for (size_t m = 0; m < cfg.SystToConsider.size(); ++m) {
136  for (auto const n : {-1, 1}) {
137  std::cout << "Considering " << cfg.SystToConsider[m] << std::endl;
138  sig_syst_hists.push_back(
140  cfg.MCFileNames[i], cfg.RecoTreeName, cfg.RecoBinning,
141  cfg.ChannelNames[i], cfg.SignalTopologyName[j], topo,
142  cfg.TruthBinning[k-1], cfg.TruthBinning[k], cfg.EndZCut,
143  false/*cfg.DoNegativeReco*/,
144  /*doSyst=*/n, /*systName=*/cfg.SystToConsider[m]));
145 
146  //Turn into frac errors
147  makeFracErrorHist(sig_syst_hists.back(), sig_nom_hists.back());
148  }
149  }
150  }
151  }
152 
153  }
154 
155  //Incident
156  for (size_t j = 0; j < cfg.IncidentTopology.size(); ++j) {
157  //Form nominal
158  TH1 * temp_hist =
161  /*cfg.ChannelNames[0],*/ cfg.IncidentTopologyName[j], cfg.IncidentTopology[j],
162  0., 10000., cfg.EndZCut, false);
163  for (size_t k = 1; k < cfg.IncidentMCFileNames.size(); ++k) {
164  std::cout << "Trying " << k << std::endl;
165  temp_hist->Add(
168  /*cfg.ChannelNames[k],*/ cfg.IncidentTopologyName[j], cfg.IncidentTopology[j],
169  0., 10000., cfg.EndZCut, false));
170  }
171  inc_nom_hists.push_back(temp_hist);
172 
173  //GetVariations
174  for (size_t m = 0; m < cfg.SystToConsider.size(); ++m) {
175  for (auto const n : {-1, 1}) {
176  std::cout << "Considering " << cfg.SystToConsider[m] << std::endl;
177 
178  TH1 * temp_hist =
181  /*cfg.ChannelNames[0],*/ cfg.IncidentTopologyName[j], cfg.IncidentTopology[j],
182  0., 10000., cfg.EndZCut, false, n, cfg.SystToConsider[m]);
183  for (size_t k = 1; k < cfg.IncidentMCFileNames.size(); ++k) {
184  temp_hist->Add(
187  /*cfg.ChannelNames[k],*/ cfg.IncidentTopologyName[j], cfg.IncidentTopology[j],
188  0., 10000., cfg.EndZCut, false, n, cfg.SystToConsider[m]));
189  }
190  inc_syst_hists.push_back(temp_hist);
191 
192  //Turn into frac errors
193  makeFracErrorHist(inc_syst_hists.back(), inc_nom_hists.back());
194  }
195  }
196  }
197 
198 
199 
200 
201 
202  TFile output(output_file_name.c_str(), "RECREATE");
203  output.cd();
204  saveHists(bg_syst_hists/*, cfg.SystToConsider*/);
205  saveHists(sig_syst_hists/*, cfg.SystToConsider*/);
206  saveHists(inc_syst_hists);
207  output.Close();
208  return 0;
209 }
210 
211 void saveHists(std::vector<TH1 *> & vec/*, std::string addToName*/) {
212  for (auto * h : vec) {
213  //std::string name = h->GetName();
214  //h->SetName((name + "_" + addToName).c_str());
215  h->Write(/*(name + "_" + addToName).c_str()*/);
216  }
217 }
218 
219 void makeFracErrorHist(TH1 * variation, TH1 * nominal) {
220  // Scale by -1 to subtract
221  nominal->Scale(-1.);
222 
223  // Subtract from var
224  variation->Add(nominal);
225 
226  // Scale back to normal
227  nominal->Scale(-1.);
228 
229  // Divide to get fractional error
230  variation->Divide(nominal);
231 }
232 
233 bool parseArgs(int argc, char ** argv) {
234 
235  bool found_fcl_file = false;
236  bool found_output_file = false;
237 
238  for (int i = 1; i < argc; ++i) {
239  if ((strcmp(argv[i], "--help") == 0) || (strcmp(argv[i], "-h") == 0)) {
240  std::cout << "Usage: ./Geant4ReweightSysts -c <fitter_config>.fcl " <<
241  " -o <output_file>.root" << std::endl;
242  std::cout << std::endl;
243 
244  return false;
245  }
246 
247  else if (strcmp(argv[i], "-c") == 0) {
248  fcl_file_name = argv[i+1];
249  found_fcl_file = true;
250  }
251 
252  else if (strcmp(argv[i], "-o") == 0) {
253  output_file_name = argv[i+1];
254  found_output_file = true;
255  }
256 
257  }
258 
259  if (!found_fcl_file || !found_output_file) {
260  std::cout << "Error: Must provide fcl file with options '-c' and '-o'" <<
261  std::endl;
262  return false;
263  }
264 
265  return true;
266 }
267 
269  fhicl::ParameterSet pset;
270 
271  // Configuration file lookup policy.
272  char const* fhicl_env = getenv("FHICL_FILE_PATH");
273  std::string search_path;
274 
275  if (fhicl_env == nullptr) {
276  std::cerr << "Expected environment variable FHICL_FILE_PATH is missing" <<
277  " or empty: using \".\"\n";
278  search_path = ".";
279  }
280  else {
281  search_path = std::string{fhicl_env};
282  }
283 
284  cet::filepath_first_absolute_or_lookup_with_dot lookupPolicy{search_path};
285  return fhicl::ParameterSet::make(file, lookupPolicy);
286 }
TH1 * FillMCIncidentHistogram_Pions(std::string filename, std::string treename, std::vector< double > recoBins, std::string topo, int toponum, double reco_beam_endZ_cut, double minval=0., double maxval=100000., bool doNegativeReco=false, int doSyst=0, std::string systName="", double weight=1.0, std::pair< double, double > PiMuScale={1., 1.})
string_v IncidentMCFileNames
string_v SystToConsider
std::string fcl_file_name
TH1 * FillMCBackgroundHistogram_Pions(std::string filename, std::string treename, std::vector< double > recoBins, std::string channel, std::string topo, int toponum, double endZ_cut, double minval=0.0, double maxval=100000.0, bool doNegativeReco=false, int doSyst=0, std::string systName="", double weight=1.0, std::pair< double, double > PiMuScale={1., 1.})
fhicl::ParameterSet makeParameterSet(std::string file)
std::string RecoTreeName
std::vector< int > int_v
std::string string
Definition: nybbler.cc:12
static ParameterSet make(intermediate_table const &tbl)
Definition: ParameterSet.cc:68
double_v TruthBinning
string_v ChannelNames
STL namespace.
std::vector< std::string > string_v
bool parseArgs(int argc, char **argv)
string_v MCFileNames
string_v BackgroundTopologyName
string_v IncidentTopologyName
void saveHists(std::vector< TH1 * > &vec)
std::string TruthTreeName
std::void_t< T > n
std::string getenv(std::string const &name)
Definition: getenv.cc:15
std::vector< double > double_v
double_v RecoBinning
std::string output_file_name
void makeFracErrorHist(TH1 *variation, TH1 *nominal)
int main(int argc, char **argv)
systConfig(const fhicl::ParameterSet &pset)
int strcmp(const String &s1, const String &s2)
Definition: relates.cpp:14
string_v SignalTopologyName
auto const & get(AssnsNode< L, R, D > const &r)
Definition: AssnsNode.h:115
TH1 * FillMCSignalHistogram_Pions(std::string filename, std::string treename, std::vector< double > recoBins, std::string channel, std::string topo, int toponum, double minval, double maxval, double endZ_cut, bool doNegativeReco=false, int doSyst=0, std::string systName="", double weight=1.0, std::pair< double, double > PiMuScale={1., 1.})
QTextStream & endl(QTextStream &s)