CMS 3D CMS Logo

helper.cc
Go to the documentation of this file.
1 #include "helper.h"
2 
3 // Global variable
5 
6 void parseArguments(int argc, char** argv) {
7  //********************************************************************************
8  //
9  // 1. Parsing options
10  //
11  //********************************************************************************
12 
13  // cxxopts is just a tool to parse argc, and argv easily
14 
15  // Grand option setting
16  cxxopts::Options options("\n $ doAnalysis",
17  "\n **********************\n * *\n * "
18  "Looper *\n * *\n **********************\n");
19 
20  // Read the options
21  options.add_options()("i,input",
22  "Comma separated input file list OR if just a directory is provided it will glob all in the "
23  "directory BUT must end with '/' for the path",
24  cxxopts::value<std::string>())("o,output", "Output file name", cxxopts::value<std::string>())(
25  "n,nevents", "N events to loop over", cxxopts::value<int>()->default_value("-1"))(
26  "j,nsplit_jobs", "Enable splitting jobs by N blocks (--job_index must be set)", cxxopts::value<int>())(
27  "I,job_index",
28  "job_index of split jobs (--nsplit_jobs must be set. index starts from 0. i.e. 0, 1, 2, 3, etc...)",
29  cxxopts::value<int>())(
30  "g,pdgid", "additional pdgid filtering to use (must be comma separated)", cxxopts::value<std::string>())(
31  "p,pt_cut", "Transverse momentum cut", cxxopts::value<float>()->default_value("0.9"))(
32  "e,eta_cut", "Pseudorapidity cut", cxxopts::value<float>()->default_value("4.5"))(
33  "d,debug", "Run debug job. i.e. overrides output option to 'debug.root' and 'recreate's the file.")("h,help",
34  "Print help");
35 
36  auto result = options.parse(argc, argv);
37 
38  // NOTE: When an option was provided (e.g. -i or --input), then the result.count("<option name>") is more than 0
39  // Therefore, the option can be parsed easily by asking the condition if (result.count("<option name>");
40  // That's how the several options are parsed below
41 
42  //_______________________________________________________________________________
43  // --help
44  if (result.count("help")) {
45  std::cout << options.help() << std::endl;
46  exit(1);
47  }
48 
49  //_______________________________________________________________________________
50  // --input
51  if (result.count("input")) {
53  } else {
54  std::cout << options.help() << std::endl;
55  std::cout << "ERROR: Input list is not provided! Check your arguments" << std::endl;
56  exit(1);
57  }
58 
59  ana.input_tree_name = "tree";
60 
61  //_______________________________________________________________________________
62  // --pdgid
63  if (result.count("pdgid")) {
64  std::vector<TString> pdgid_strs = RooUtil::StringUtil::split(result["pdgid"].as<std::string>(), ",");
65  for (auto& pdgid_str : pdgid_strs) {
66  ana.pdgids.push_back(pdgid_str.Atoi());
67  }
68  }
69 
70  //_______________________________________________________________________________
71  // --pt_cut
72  ana.pt_cut = result["pt_cut"].as<float>();
73 
74  //_______________________________________________________________________________
75  // --eta_cut
76  ana.eta_cut = result["eta_cut"].as<float>();
77 
78  //_______________________________________________________________________________
79  // --debug
80  if (result.count("debug")) {
81  ana.output_tfile = new TFile("debug.root", "recreate");
82  } else {
83  //_______________________________________________________________________________
84  // --output
85  if (result.count("output")) {
86  ana.output_tfile = new TFile(result["output"].as<std::string>().c_str(), "create");
87  if (not ana.output_tfile->IsOpen()) {
88  std::cout << options.help() << std::endl;
89  std::cout << "ERROR: output already exists! provide new output name or delete old file. OUTPUTFILE="
90  << result["output"].as<std::string>() << std::endl;
91  exit(1);
92  }
93  } else {
94  std::cout << options.help() << std::endl;
95  std::cout << "ERROR: Output file name is not provided! Check your arguments" << std::endl;
96  exit(1);
97  }
98  }
99 
100  //_______________________________________________________________________________
101  // --nevents
102  ana.n_events = result["nevents"].as<int>();
103 
104  //_______________________________________________________________________________
105  // --nsplit_jobs
106  if (result.count("nsplit_jobs")) {
107  ana.nsplit_jobs = result["nsplit_jobs"].as<int>();
108  if (ana.nsplit_jobs <= 0) {
109  std::cout << options.help() << std::endl;
110  std::cout << "ERROR: option string --nsplit_jobs" << ana.nsplit_jobs << " has zero or negative value!"
111  << std::endl;
112  std::cout << "I am not sure what this means..." << std::endl;
113  exit(1);
114  }
115  } else {
116  ana.nsplit_jobs = -1;
117  }
118 
119  //_______________________________________________________________________________
120  // --nsplit_jobs
121  if (result.count("job_index")) {
122  ana.job_index = result["job_index"].as<int>();
123  if (ana.job_index < 0) {
124  std::cout << options.help() << std::endl;
125  std::cout << "ERROR: option string --job_index" << ana.job_index << " has negative value!" << std::endl;
126  std::cout << "I am not sure what this means..." << std::endl;
127  exit(1);
128  }
129  } else {
130  ana.job_index = -1;
131  }
132 
133  // Sanity check for split jobs (if one is set the other must be set too)
134  if (result.count("job_index") or result.count("nsplit_jobs")) {
135  // If one is not provided then throw error
136  if (not(result.count("job_index") and result.count("nsplit_jobs"))) {
137  std::cout << options.help() << std::endl;
138  std::cout << "ERROR: option string --job_index and --nsplit_jobs must be set at the same time!" << std::endl;
139  exit(1);
140  }
141  // If it is set then check for sanity
142  else {
143  if (ana.job_index >= ana.nsplit_jobs) {
144  std::cout << options.help() << std::endl;
145  std::cout << "ERROR: --job_index >= --nsplit_jobs ! This does not make sense..." << std::endl;
146  exit(1);
147  }
148  }
149  }
150 
151  //
152  // Printing out the option settings overview
153  //
154  std::cout << "=========================================================" << std::endl;
155  std::cout << " Setting of the analysis job based on provided arguments " << std::endl;
156  std::cout << "---------------------------------------------------------" << std::endl;
157  std::cout << " ana.input_file_list_tstring: " << ana.input_file_list_tstring << std::endl;
158  std::cout << " ana.output_tfile: " << ana.output_tfile->GetName() << std::endl;
159  std::cout << " ana.n_events: " << ana.n_events << std::endl;
160  std::cout << " ana.pt_cut: " << ana.pt_cut << std::endl;
161  std::cout << " ana.eta_cut: " << ana.eta_cut << std::endl;
162  std::cout << " ana.nsplit_jobs: " << ana.nsplit_jobs << std::endl;
163  std::cout << " ana.job_index: " << ana.job_index << std::endl;
164  std::cout << "=========================================================" << std::endl;
165 }
166 
167 AnalysisConfig::AnalysisConfig() : tx("variable", "variable") {}
168 
170  int pdgid_,
171  int q_,
172  std::function<bool(unsigned int)> pass_,
173  std::function<bool(unsigned int)> sel_) {
174  set_name = set_name_;
175  pdgid = pdgid_;
176  q = q_;
177  pass = pass_;
178  sel = sel_;
179 }
180 
182  std::function<bool(unsigned int)> pass_,
183  std::function<bool(unsigned int)> sel_,
184  std::function<const std::vector<float>()> pt_,
185  std::function<const std::vector<float>()> eta_,
186  std::function<const std::vector<float>()> phi_,
187  std::function<const std::vector<int>()> type_)
188  : pt(pt_), eta(eta_), phi(phi_), type(type_) {
189  set_name = set_name_;
190  pass = pass_;
191  sel = sel_;
192 }
193 
195  // Create the TChain that holds the TTree's of the baby ntuples
197 
198  // This is a standard thing SNT does pretty much every looper we use
200 
201  // Set the cutflow object output file
203 
204  // Copy the information of the code
205  std::vector<TString> vstr = RooUtil::StringUtil::split(ana.input_file_list_tstring, ",");
206  TFile* firstFile = new TFile(vstr[0]);
207 
208  // Obtain the info from the input file
209  TString code_tag_data = firstFile->Get("code_tag_data")->GetTitle();
210  TString gitdiff = firstFile->Get("gitdiff")->GetTitle();
211  TString input = firstFile->Get("input")->GetTitle();
212 
213  // cd to output file
214  ana.output_tfile->cd();
215 
216  // Write the githash after parsing whether there is any gitdiff
217  TString githash = RooUtil::StringUtil::split(code_tag_data, "\n")[0];
218  githash = githash(0, 6);
219  std::cout << " gitdiff.Length(): " << gitdiff.Length() << std::endl;
220  if (gitdiff.Length() > 0)
221  githash += "D";
222  std::cout << " githash: " << githash << std::endl;
223  TNamed githash_tnamed("githash", githash.Data());
224  githash_tnamed.Write();
225 
226  // Write the sample information
227  if (input.Contains("PU200"))
228  input = "PU200";
229  std::cout << " input: " << input << std::endl;
230  TNamed input_tnamed("input", input.Data());
231  input_tnamed.Write();
232 
233  ana.do_lower_level = false; // default is false
234  TObjArray* brobjArray = ana.events_tchain->GetListOfBranches();
235  for (unsigned int ibr = 0; ibr < (unsigned int)brobjArray->GetEntries(); ++ibr) {
236  TString brname = brobjArray->At(ibr)->GetName();
237  if (brname.EqualTo("t5_pt"))
238  ana.do_lower_level = true; // if it has the branch it is set to true
239  }
240 }
241 
242 std::vector<float> getPtBounds(int mode) {
243  std::vector<float> pt_boundaries;
244  if (mode == 0)
245  pt_boundaries = {0, 0.5, 0.6, 0.7, 0.8, 0.9, 1.0, 1.1, 1.2, 1.5, 2.0, 3.0, 5.0, 10, 15., 25, 50};
246  else if (mode == 1)
247  pt_boundaries = {0.988, 0.99, 0.992, 0.994, 0.996, 0.998, 1.0, 1.002, 1.004, 1.006, 1.008, 1.01, 1.012}; // lowpt
248  else if (mode == 2)
249  pt_boundaries = {0.955, 0.96, 0.965, 0.97, 0.975, 0.98, 0.985, 0.99, 0.995, 1.00,
250  1.005, 1.01, 1.015, 1.02, 1.025, 1.03, 1.035, 1.04, 1.045, 1.05}; // pt 0p95 1p05
251  else if (mode == 3)
252  pt_boundaries = {
253  0.5, 0.6, 0.7, 0.8, 0.9, 0.92, 0.94, 0.96, 0.98, 1.0, 1.02, 1.04, 1.06, 1.08, 1.1, 1.2, 1.5}; // lowpt
254  else if (mode == 4)
255  pt_boundaries = {0.5, 0.52, 0.54, 0.56, 0.58, 0.6, 0.62, 0.64, 0.66, 0.68, 0.7, 0.72, 0.74,
256  0.76, 0.78, 0.8, 0.82, 0.84, 0.86, 0.88, 0.9, 0.92, 0.94, 0.96, 0.98, 1.0,
257  1.02, 1.04, 1.06, 1.08, 1.1, 1.12, 1.14, 1.16, 1.18, 1.2, 1.22, 1.24, 1.26,
258  1.28, 1.3, 1.32, 1.34, 1.36, 1.38, 1.4, 1.5, 1.6, 1.7, 1.8, 1.9, 2.0}; // lowpt
259  else if (mode == 5)
260  pt_boundaries = {0.5, 0.52, 0.54, 0.56, 0.58, 0.6, 0.62, 0.64, 0.66, 0.68, 0.7, 0.72, 0.74,
261  0.76, 0.78, 0.8, 0.82, 0.84, 0.86, 0.88, 0.9, 0.92, 0.94, 0.96, 0.98, 1.0,
262  1.02, 1.04, 1.06, 1.08, 1.1, 1.12, 1.14, 1.16, 1.18, 1.2, 1.24, 1.28, 1.32,
263  1.36, 1.4, 1.6, 1.8, 2.0, 2.2, 2.4, 2.6, 2.8, 3.0}; // lowpt
264  else if (mode == 6)
265  pt_boundaries = {0.5, 0.6, 0.7, 0.8, 0.9, 1.0, 1.1, 1.2, 1.3, 1.4, 1.5, 2.0, 3.0, 4.0, 5.0}; // lowpt
266  else if (mode == 7)
267  pt_boundaries = {0.5, 0.52, 0.54, 0.56, 0.58, 0.6, 0.62, 0.64, 0.66, 0.68, 0.7, 0.72, 0.74, 0.76, 0.78,
268  0.8, 0.82, 0.84, 0.86, 0.88, 0.9, 0.92, 0.94, 0.96, 0.98, 1.0, 1.02, 1.04, 1.06, 1.08,
269  1.1, 1.12, 1.14, 1.16, 1.18, 1.2, 1.22, 1.24, 1.26, 1.28, 1.3, 1.32, 1.34, 1.36, 1.38,
270  1.4, 1.5, 1.6, 1.7, 1.8, 1.9, 2.0, 2.2, 2.4, 2.6, 2.8, 3, 4, 5, 6,
271  7, 8, 9, 10, 15, 20, 25, 30, 35, 40, 45, 50}; // lowpt
272  else if (mode == 8)
273  pt_boundaries = {0, 0.5, 1.0, 1.5, 2.0, 3.0, 5.0, 10, 15., 25, 50};
274  else if (mode == 9) {
275  for (int i = 0; i < 41; ++i) {
276  pt_boundaries.push_back(pow(10., -1. + 4. * i / 40.));
277  }
278  }
279  return pt_boundaries;
280 }
std::vector< float > getPtBounds(int mode)
Definition: helper.cc:242
RooUtil::Cutflow cutflow
AnalysisConfig ana
Definition: helper.cc:4
void init(TChain *chain, TREECLASS *treeclass, int nEventsToProcess)
Definition: looper.h:245
vecTString split(TString in, TString separator=" ")
Definition: stringutil.cc:31
RecoTrackSetDefinition(TString, std::function< bool(unsigned int)>, std::function< bool(unsigned int)>, std::function< const std::vector< float >()>, std::function< const std::vector< float >()>, std::function< const std::vector< float >()>, std::function< const std::vector< int >()>)
Definition: helper.cc:181
static std::string const input
Definition: EdmProvDump.cc:50
TChain * events_tchain
std::function< bool(unsigned int)> pass
Definition: helper.h:75
TString input_tree_name
The Signals That Services Can Subscribe To This is based on ActivityRegistry and is current per Services can connect to the signals distributed by the ActivityRegistry in order to monitor the activity of the application Each possible callback has some defined which we here list in angle e< void, edm::EventID const &, edm::Timestamp const & > We also list in braces which AR_WATCH_USING_METHOD_ is used for those or
Definition: Activities.doc:12
void setTFile(TFile *o)
Definition: anautil.h:148
std::function< bool(unsigned int)> pass
Definition: helper.h:83
RooUtil::Looper< Trktree > looper
SimTrackSetDefinition(TString, int, int, std::function< bool(unsigned int)>, std::function< bool(unsigned int)>)
Definition: helper.cc:169
std::function< bool(unsigned int)> sel
Definition: helper.h:84
TString input_file_list_tstring
LSTEff lstEff
Definition: LSTEff.cc:2
float pt_cut
Definition: helper.h:23
std::vector< int > pdgids
Definition: helper.h:60
TFile * output_tfile
TChain * createTChain(TString, TString)
Definition: fileutil.cc:6
void initializeInputsAndOutputs()
Definition: helper.cc:194
TString set_name
Definition: helper.h:72
Power< A, B >::type pow(const A &a, const B &b)
Definition: Power.h:29
std::function< bool(unsigned int)> sel
Definition: helper.h:76
float eta_cut
Definition: helper.h:26
def exit(msg="")
void parseArguments(int argc, char **argv)
Definition: helper.cc:6