ECCE @ EIC Software
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
Fun4All_runEvaluators.C
Go to the documentation of this file. Or view the newest version in sPHENIX GitHub for file Fun4All_runEvaluators.C
1 #ifndef MACRO_FUN4ALLG4RUNEVALUATORS_C
2 #define MACRO_FUN4ALLG4RUNEVALUATORS_C
3 
4 #include <dirent.h>
5 #include <fstream>
6 #include <map>
7 #include <stdlib.h>
8 
9 #include <GlobalVariables.C>
10 
11 #include <G4Setup_EICDetector.C>
13 #include <G4_EventEvaluator.C>
14 #include <G4_FwdJets.C>
15 #include <G4_Global.C>
16 #include <G4_Input.C>
17 #include <G4_Production.C>
18 #include <G4_User.C>
19 
20 #include <fun4all/Fun4AllServer.h>
21 
22 using namespace std;
23 
24 R__LOAD_LIBRARY(libfun4all.so)
25 
26 bool checkForDir(string name)
27 {
28  DIR* dir = opendir(name.c_str());
29 
30  return dir == NULL ? 0 : 1;
31 }
32 
33 bool checkForFile(string dir, string base, map<string, string> extension)
34 {
35  bool fileExists = false;
36  string fileName;
37  for (auto const& key : extension)
38  {
39  fileName = dir + "/" + base + key.second;
40  ifstream file(fileName.c_str());
41  if (file.good()) fileExists = true;
42  }
43 
44  return fileExists;
45 }
46 
48  const int nEvents = 1,
49  const string &inputFile = "myInputFile.root",
50  const string &inputDir = ".",
51  const int skip = 0,
52  const string &outdir = ".")
53 {
55  se->Verbosity(0);
56 
57  Input::READHITS = true;
58  INPUTREADHITS::filename[0] = inputDir + "/" + inputFile;
59 
60  InputInit();
61 
62  //-----
63  // What to run
64  //-----
65 
66  Enable::DSTREADER = false;
67  Enable::USER = false; // Enable this to run custom code from G4_User.C
68 
69  Enable::EVENT_EVAL = true;
70  Enable::TRACKING_EVAL = true;
71  Enable::BECAL_EVAL = true;
72  Enable::HCALIN_EVAL = true;
73  Enable::HCALOUT_EVAL = true;
74  Enable::FEMC_EVAL = true;
75  Enable::LFHCAL_EVAL = true;
76  Enable::EEMCH_EVAL = true;
77  Enable::EHCAL_EVAL = true;
78  Enable::FWDJETS_EVAL = false;
79  Enable::FFR_EVAL = true;
80 
81  map<string, string> evaluatorNames;
82  evaluatorNames["event"] = "_g4event_eval.root";
83  evaluatorNames["tracking"] = "_g4tracking_eval.root";
84  evaluatorNames["becal"] = "_g4becal_eval.root";
85  evaluatorNames["hcalin"] = "_g4hcalin_eval.root";
86  evaluatorNames["hcalout"] = "_g4hcalout_eval.root";
87  evaluatorNames["femc"] = "_g4femc_eval.root";
88  evaluatorNames["fhcal"] = "_g4fhcal_eval.root";
89  evaluatorNames["eemch"] = "_g4eemch_eval.root";
90  evaluatorNames["ehcal"] = "_g4ehcal_eval.root";
91  evaluatorNames["farfwd"] = "_g4farfwd_eval.root";
92 
93  /*
94  * Enable extra features in the evaluators
95  * WARNING!!! Ensure your DST has the right nodes
96  */
97  Enable::TRACKING = true;
98  Enable::BECAL = true;
99  Enable::HCALIN = true;
100  Enable::HCALOUT = true;
101  Enable::FEMC = true;
102  Enable::LFHCAL = true;
103  Enable::EEMCH = true;
104  Enable::EHCAL = true;
105 
106  TRACKING::ProjectionNames = {"BECAL"
107  ,"CTTL_0"
108  ,"EEMC"
109  ,"EHCAL"
110  ,"ETTL_0"
111  ,"ETTL_1"
112  ,"FEMC"
113  ,"FTTL_0"
114  ,"FTTL_1"
115  ,"HCALOUT"
116  ,"LFHCAL"
117  ,"RICH"
118  ,"hpDIRC"
119  ,"mRICH"
120  };
121 
122  Enable::DIRC_RECO = true;
123  Enable::mRICH_RECO = true;
124  Enable::RICH_RECO = true;
125 
126  //-----
127  // Output file headers and path
128  //-----
129 
130  //Get base file name
131  string baseFile = inputFile;
132  string remove_this = ".root";
133  size_t pos = baseFile.find(remove_this);
134  if (pos != string::npos)
135  {
136  baseFile.erase(pos, remove_this.length());
137  }
138 
139  string evalDir = outdir;
140  string outdirLastChar = outdir.substr(outdir.size() - 1, 1);
141  if (outdirLastChar != "/") evalDir += "/";
142 
143  unsigned int revisionWidth = 5;
144  unsigned int revisionNumber = 0;
145  ostringstream evalRevision;
146  evalRevision << setfill('0') << setw(revisionWidth) << to_string(revisionNumber);
147  evalDir += "eval_" + evalRevision.str();
148 
149  while (checkForDir(evalDir))
150  {
151  bool evalFileStatus = checkForFile(evalDir, baseFile, evaluatorNames);
152  if (!evalFileStatus) break;
153  evalDir = evalDir.substr(0, evalDir.size() - revisionWidth);
154  revisionNumber++;
155  evalRevision.str("");
156  evalRevision.clear();
157  evalRevision << setfill('0') << setw(revisionWidth) << to_string(revisionNumber);
158  evalDir += evalRevision.str();
159  }
160 
161  string makeDirectory = "mkdir -p " + evalDir;
162  system(makeDirectory.c_str());
163 
164  string outputroot = evalDir + "/" + baseFile;
165 
166  //-----
167  // Reader and User analysis
168  //-----
169 
170  if (Enable::DSTREADER) G4DSTreader_EICDetector(outputroot + "_DSTReader.root");
172 
173  //-----
174  // Evaluators
175  //-----
176 
177  if (Enable::EVENT_EVAL) Event_Eval(outputroot + evaluatorNames.find("event")->second);
178  if (Enable::TRACKING_EVAL) Tracking_Eval(outputroot + evaluatorNames.find("tracking")->second);
179  if (Enable::BECAL_EVAL) BECAL_Eval(outputroot + evaluatorNames.find("becal")->second);
180  if (Enable::HCALIN_EVAL) HCALInner_Eval(outputroot + evaluatorNames.find("hcalin")->second);
181  if (Enable::HCALOUT_EVAL) HCALOuter_Eval(outputroot + evaluatorNames.find("hcalout")->second);
182  if (Enable::FEMC_EVAL) FEMC_Eval(outputroot + evaluatorNames.find("femc")->second);
183  if (Enable::FHCAL_EVAL) FHCAL_Eval(outputroot + evaluatorNames.find("fhcal")->second);
184  if (Enable::EEMCH_EVAL) EEMCH_Eval(outputroot + evaluatorNames.find("eemch")->second);
185  if (Enable::EHCAL_EVAL) EHCAL_Eval(outputroot + evaluatorNames.find("ehcal")->second);
186  if (Enable::FWDJETS_EVAL) Jet_FwdEval(outputroot);
187  if (Enable::FFR_EVAL) FFR_Eval(outputroot + evaluatorNames.find("farfwd")->second);
188 
189  //--------------
190  // Set up Input Managers
191  //--------------
192 
193  InputManagers();
194 
195  //-----
196  // Run
197  //-----
198 
199  se->skip(skip);
200  se->run(nEvents);
201 
202  //-----
203  // Exit
204  //-----
205 
206  se->End();
207  cout << "All done" << endl;
208  delete se;
209 
210  gSystem->Exit(0);
211  return 0;
212 }
213 
214 #endif