EIC Software
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
SimpleAnalysis.cc
Go to the documentation of this file. Or view the newest version in sPHENIX GitHub for file SimpleAnalysis.cc
1 #include <TROOT.h>
2 #include <TFile.h>
3 #include <TChain.h>
4 #include <TTree.h>
5 #include <TString.h>
6 #include <TObjString.h>
7 #include "TInterpreter.h"
8 
9 #include <unistd.h>
10 #include <stdlib.h>
11 #include <iostream>
12 #include <stdio.h>
13 #include <getopt.h>
14 #include <glob.h>
15 #include <vector>
16 #include <map>
17 #include <any>
18 
19 #include "classes/DelphesClasses.h"
20 #include "external/ExRootAnalysis/ExRootTreeReader.h"
21 
22 #include "ModuleHandler.h"
23 #include "TreeHandler.h"
24 
25 static std::string input_dir = "";
26 static std::string output_file = "";
27 static std::string module_sequence = "";
28 static int nevents = -1;
29 
32 
33 
34 // HELPER METHODS
35 
36 void PrintHelp()
37 {
38  std::cout <<
39  "--input_dir <i>: Directory containing all the ROOT files you want to process\n"
40  "--output_file <o>: Output ROOT file to store results\n"
41  "--module_sequence <s>: A string comma-separated list of modules to load; order is preserved in execution.\n"
42  "--nevents <n>: The total number of events to process, starting from the zeroth event in the input.\n"
43  "--help: Show this helpful message!\n";
44  exit(1);
45 }
46 
47 std::vector<std::string> fileVector(const std::string& pattern){
48  glob_t glob_result;
49  glob(pattern.c_str(),GLOB_TILDE,NULL,&glob_result);
50  std::vector<std::string> files;
51  for(unsigned int i=0;i<glob_result.gl_pathc;++i){
52  files.push_back(std::string(glob_result.gl_pathv[i]));
53  }
54  globfree(&glob_result);
55  return files;
56 }
57 
58 // MAIN FUNCTION
59 
60 
61 int main(int argc, char *argv[])
62 {
63  std::cout <<
64  "===================== SIMPLEANALYSIS =====================" << std::endl;
65 
66 
67  // Handle complex TTree data storage types by defining them for ROOT
68  gInterpreter->GenerateDictionary("std::vector<std::vector<float>>","vector");
69 
70 
71 
72  if (argc <= 1) {
73  PrintHelp();
74  }
75 
76  const char* const short_opts = "i:o:h";
77  const option long_opts[] = {
78  {"input_dir", required_argument, nullptr, 'i'},
79  {"output_file", required_argument, nullptr, 'o'},
80  {"module_sequence", required_argument, nullptr, 's'},
81  {"nevents", optional_argument, nullptr, 'n'},
82  {"help", no_argument, nullptr, 'h'},
83  {nullptr, no_argument, nullptr, 0}
84  };
85 
86  while (true)
87  {
88  const auto opt = getopt_long(argc, argv, short_opts, long_opts, nullptr);
89 
90  if (-1 == opt)
91  break;
92 
93  switch (opt)
94  {
95  case 'i':
96  input_dir = optarg;
97  std::cout << "Input Directory: " << input_dir << std::endl;
98  break;
99 
100  case 'o':
101  output_file = optarg;
102  std::cout << "Output File: " << output_file << std::endl;
103  break;
104 
105  case 's':
106  module_sequence = optarg;
107  std::cout << "Module sequence: " << module_sequence << std::endl;
108  break;
109 
110  case 'n':
111  nevents = std::stoi(optarg);
112  std::cout << "Number of events to process: " << nevents << std::endl;
113  break;
114 
115 
116  case 'h': // -h or --help
117  case '?': // Unrecognized option
118  PrintHelp();
119  break;
120  default:
121  PrintHelp();
122  break;
123  }
124  }
125 
126 
127  auto data = new TChain("Delphes");
128 
129  auto files = fileVector(input_dir);
130 
131  for (auto file : files)
132  {
133  data->Add(file.c_str());
134  }
135 
136  ExRootTreeReader *treeReader = new ExRootTreeReader(data);
137  int n_entries = data->GetEntries();
138 
139  std::cout
140  << "The provided data set contains the following number of events: " << std::endl
141  << n_entries
142  << std::endl;
143 
144  // Load object pointers
145  TClonesArray *branchJet = treeReader->UseBranch("Jet");
146  TClonesArray *branchElectron = treeReader->UseBranch("Electron");
147  TClonesArray *branchPhoton = treeReader->UseBranch("EFlowPhoton");
148  TClonesArray *branchNeutralHadron = treeReader->UseBranch("EFlowNeutralHadron");
149  TClonesArray *branchGenJet = treeReader->UseBranch("GenJet");
150  TClonesArray *branchGenParticle = treeReader->UseBranch("Particle");
151  TClonesArray *branchRawTrack = treeReader->UseBranch("Track");
152  TClonesArray *branchEFlowTrack = treeReader->UseBranch("EFlowTrack");
153  TClonesArray *branchMET = treeReader->UseBranch("MissingET");
154 
155 
156  // Setup the module handler
157  ModuleHandler *module_handler = module_handler->getInstance(treeReader);
158  auto module_list = TString(module_sequence).Tokenize(",");
159 
160  // Setup the output storage
161  TreeHandler *tree_handler = tree_handler->getInstance(output_file.c_str(), "tree");
162  tree_handler->initialize();
163 
164  for (int i = 0; i < module_list->GetEntries(); i++) {
165  auto name = static_cast<TObjString*>(module_list->At(i))->GetString().Data();
166  std::cout << " Appending module " << name << std::endl;
167  module_handler->addModule(name);
168  }
169 
170  for (auto module : module_handler->getModules()) {
171  module->initialize();
172  }
173 
174  if (nevents < 0) {
175  std::cout
176  << "Processing all events in the sample..." << std::endl;
177  } else {
178  std::cout
179  << "Processing "<< nevents << " events in the sample..." << std::endl;
180  }
181 
182  for(int i=0; i < n_entries; ++i) {
183  // event number printout
184  if(i%1000==0) {
185  std::cout << "Processing Event " << i << std::endl;
186  }
187 
188  if (nevents >= 0 && i >= nevents)
189  break;
190 
191  // read the data for i-th event
192  // data->GetEntry(i);
193  // Load selected branches with data from specified event
194  treeReader->ReadEntry(i);
195 
196  std::map<std::string, std::any> DataStore;
197 
198 
199  for (auto module : module_handler->getModules()) {
200  module->setJets(branchJet);
201  module->setGenJets(branchGenJet);
202  module->setEFlowTracks(branchEFlowTrack);
203  module->setTracks(branchRawTrack);
204  module->setGenParticles(branchGenParticle);
205  module->setPhotons(branchPhoton);
206  module->setElectrons(branchElectron);
207  module->setNeutralHadrons(branchNeutralHadron);
208  module->setMET(branchMET);
209 
210  bool result = module->execute(&DataStore);
211  if (result == false)
212  break;
213  }
214 
215  tree_handler->execute();
216 
217 
218  // if (DataStore.find("CharmJets") != DataStore.end()) {
219  // std::vector<Jet*> charm_jets = std::any_cast<std::vector<Jet*>>(DataStore["CharmJets"]);
220  // }
221 
222  }
223 
224  for (auto module : module_handler->getModules()) {
225  module->finalize();
226  }
227  tree_handler->finalize();
228 
229 
230  std::cout <<
231  "========================== FINIS =========================" << std::endl;
232 
233  exit(EXIT_SUCCESS);
234 }