EIC Software
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
FairAnaSelector.cxx
Go to the documentation of this file. Or view the newest version in sPHENIX GitHub for file FairAnaSelector.cxx
1 // -------------------------------------------------------------------------
2 // ----- FairAnaSelector source file -----
3 // ----- Created 14/10/11 by R. Karabowicz -----
4 // ----- Updated 01/02/12 by R. Karabowicz -----
5 // -------------------------------------------------------------------------
6 
7 #include "FairAnaSelector.h"
8 
9 
10 #include "TFile.h"
11 #include "TProofOutputFile.h"
12 #include "TH2.h"
13 #include "TSystem.h"
14 #include "TStyle.h"
15 #include "TCanvas.h"
16 
17 #include "FairTask.h"
18 #include "FairRunAna.h"
19 #include "FairRuntimeDb.h"
20 #include "FairParRootFileIo.h"
21 #include "FairParAsciiFileIo.h"
22 
23 #include <iostream>
24 #include <iomanip>
25 #include <map>
26 
27 using std::cout;
28 using std::cerr;
29 using std::endl;
30 using std::pair;
31 using std::setw;
32 using std::left;
33 using std::right;
34 using std::fixed;
35 using std::setprecision;
36 using std::map;
37 
38 //_____________________________________________________________________________
39 void FairAnaSelector::Init(TTree* tree)
40 {
41  // The Init() function is called when the selector needs to initialize
42  // a new tree or chain. Typically here the branch addresses and branch
43  // pointers of the tree will be set.
44  // It is normally not necessary to make changes to the generated
45  // code, but the routine can be extended by the user if needed.
46  // Init() will be called many times when running on PROOF
47  // (once per file to be processed).
48  if (!tree) {
49  cout << "-W- FairAnaSelector::Init(): There is no tree." << endl;
50  return;
51  } else {
52  cout << "-I- FairAnaSelector::Init(): Got tree : \"" << tree << "\"" << endl;
53  cout << "-I- FairAnaSelector::Init(): Tree name : \"" << tree->GetName() << "\"" << endl;
54  cout << "-I- FairAnaSelector::Init(): Tree title : \"" << tree->GetTitle() << "\"" << endl;
55  cout << "-I- FairAnaSelector::Init(): Tree filename: \"" << tree->GetCurrentFile()->GetName() << "\"" << endl;
56  }
57 
58  if ( fRunAna ) {
59  cout << "-I- FairAnaSelector::Init(): Already have fRunAna." << endl;
60 
61  cout << "-I- FairAnaSelector::Init(): SetInTree(" << tree << ")" << endl;
62  fRunAna->SetInTree(tree);
63  cout << "-I- FairAnaSelector::Init(): SetInTree done" << endl;
64 
66  ioman->OpenInTree();
67 
68  cout << "Containers static? " << (fRunAna->GetContainerStatic()?"YES":"NO") << endl;
69  if ( !fRunAna->GetContainerStatic() ) {
71  }
72  } else {
73  cout << "-I- FairAnaSelector::Init(): Have to create fRunAna." << endl;
74 
75  TString vmcPath = gSystem->Getenv("VMCWORKDIR");
76 
77  TNamed* contStat = (TNamed*) fInput->FindObject("FAIRRUNANA_fContainerStatic");
78  TNamed* outStat = (TNamed*) fInput->FindObject("FAIRRUNANA_fProofOutputStatus");
79  TNamed* outFile = (TNamed*) fInput->FindObject("FAIRRUNANA_fOutputFileName");
80  TNamed* outDir = (TNamed*) fInput->FindObject("FAIRRUNANA_fOutputDirectory");
81  TNamed* par1Name = (TNamed*) fInput->FindObject("FAIRRUNANA_fParInput1FName");
82  TNamed* par2Name = (TNamed*) fInput->FindObject("FAIRRUNANA_fParInput2FName");
83  TString containerS = contStat->GetTitle();
84  TString outputStat = outStat->GetTitle();
85  TString par1Str = par1Name->GetTitle();
86  TString par2Str = par2Name->GetTitle();
87  TString outFileName = outFile->GetTitle();
88  TString outDirName = outDir->GetTitle();
89 
90  cout << "-I- FairAnaSelector::Init(): out status : \"" << outputStat.Data() << "\"" << endl;
91  cout << "-I- FairAnaSelector::Init(): par1 file : \"" << par1Str.Data() << "\"" << endl;
92  cout << "-I- FairAnaSelector::Init(): par2 file : \"" << par2Str.Data() << "\"" << endl;
93 
94  if ( outputStat != "copy" ) {
95  cout << "-I- FairAnaSelector::Init(): OutputFile option \"" << outputStat.Data() << "\" not yet supported, will \"copy\" anyways" << endl;
96  }
97 
98  cout << "-I- FairAnaSelector::Init(): OutputFile option \"" << outputStat.Data() << "\" RECOGNIZED" << endl;
99 
100  cout << "-I- FairAnaSelector::Init(): gSystem->WorkingDirectory() = \""
101  << gSystem->WorkingDirectory() << "\"" << endl;
102  TString workDir = gSystem->WorkingDirectory();
103 
104  Int_t workerNumber = -1; // is there better way to get it?
105 
106  for ( Int_t iposWork = 0 ; iposWork < 100 ; iposWork++ ) {
107  TString tempStr = Form("-0.%d",iposWork);
108  if ( workDir.Contains(tempStr.Data()) ) {
109  workerNumber = iposWork;
110  break;
111  }
112  }
113  if ( workerNumber == -1 ) {
114  cout << "-E- FairAnaSelector::Init(): Did not recognize worker number." << endl;
115  } else {
116  cout << "-I- FairAnaSelector::Init(): worker number = " << workerNumber << endl;
117  }
118 
119  outFileName.Remove(outFileName.Length()-5);
120  TString outputFileName = Form("%s/%s_worker_0.%d.root",outDirName.Data(),outFileName.Data(),workerNumber);
121 
122  cout << "-I- FairAnaSelector::Init(): output will go to file: \"" << outputFileName.Data() << "\"" << endl;
123  fFile = TFile::Open(outputFileName.Data(),"RECREATE");
124 
125  fRunAna = new FairRunAna();
126  fRunAna->SetRunOnProofWorker();
127 
128  cout << "-I- FairAnaSelector::Init(): SetInTree(" << tree << ")" << endl;
129  fRunAna->SetInTree(tree);
130  cout << "-I- FairAnaSelector::Init(): SetInTree done" << endl;
131 
132  fRunAna->SetOutputFile(fFile);
133  if ( containerS == "kTRUE" ) {
134  fRunAna->SetContainerStatic(kTRUE);
135  } else {
136  fRunAna->SetContainerStatic(kFALSE);
137  }
138 
139  // ----- Parameter database --------------------------------------------
140  FairRuntimeDb* rtdb = fRunAna->GetRuntimeDb();
141 
142  if ( par1Str.Contains(".root") ) {
143  FairParRootFileIo* parInput1 = new FairParRootFileIo();
144  parInput1->open(par1Str.Data());
145  rtdb->setFirstInput (parInput1);
146  }
147  if ( par1Str.Contains(".par") ) {
148  FairParAsciiFileIo* parInput1 = new FairParAsciiFileIo();
149  parInput1->open(par1Str.Data(),"in");
150  rtdb->setFirstInput (parInput1);
151  }
152 
153  if ( par2Str.Contains(".root") ) {
154  FairParRootFileIo* parInput2 = new FairParRootFileIo();
155  parInput2->open(par2Str.Data());
156  rtdb->setSecondInput(parInput2);
157  }
158  if ( par2Str.Contains(".par") ) {
159  FairParAsciiFileIo* parInput2 = new FairParAsciiFileIo();
160  parInput2->open(par2Str.Data(),"in");
161  rtdb->setSecondInput(parInput2);
162  }
163 
164  // ------------------------------------------------------------------------
165 
166  FairTask* fairTaskList = dynamic_cast<FairTask*>(fInput->FindObject("FairTaskList"));
167  cout << "-I- FairAnaSelector::Init(): FairTask = \"" << fairTaskList << "\"" << endl;
168 
169  if ( 1 == 0 ) {
170  cout << "fairTaskList->Print()" << endl;
171  fairTaskList->Print();
172  cout << "fairTaskList->ls()" << endl;
173  fairTaskList->ls();
174  cout << "fairTaskList finished" << endl;
175  }
176 
177  fRunAna->SetTask(fairTaskList);
178 
179  cout << "-I- FairAnaSelector::Init(): vvvvv fRunAna->Init() vvvvv" << endl;
180  fRunAna->Init();
181  cout << "-I- FairAnaSelector::Init(): ^^^^^ fRunAna->Init() ^^^^^" << endl;
182  }
183 
184 }
185 //_____________________________________________________________________________
186 
187 //_____________________________________________________________________________
189 {
190  // The Notify() function is called when a new file is opened. This
191  // can be either for a new TTree in a TChain or when when a new TTree
192  // is started when using PROOF. It is normally not necessary to make changes
193  // to the generated code, but the routine can be extended by the
194  // user if needed. The return value is currently not used.
195  cout << "-I- FairAnaSelector::Notify()" << endl;
196 
197  return kTRUE;
198 }
199 //_____________________________________________________________________________
200 
201 //_____________________________________________________________________________
202 void FairAnaSelector::Begin(TTree* /*tree*/)
203 {
204  // The Begin() function is called at the start of the query.
205  // When running with PROOF Begin() is only called on the client.
206  // The tree argument is deprecated (on PROOF 0 is passed).
207  cout << "-I- FairAnaSelector::Begin()" << endl;
208 
209 }
210 //_____________________________________________________________________________
211 
212 //_____________________________________________________________________________
214 {
215  // The SlaveBegin() function is called after the Begin() function.
216  // When running with PROOF SlaveBegin() is called on each slave server.
217  // The tree argument is deprecated (on PROOF 0 is passed).
218  cout << "-I- FairAnaSelector::SlaveBegin(): Tree address : \"" << tree << "\"" << endl;
219 
220  // useless, because have no tree anyways in slavebegin, init will be anyways called whenever a new tree comes
221  // Init(tree);
222 
223  cout << "-I- FairAnaSelector::SlaveBegin(): finishing" << endl;
224 }
225 //_____________________________________________________________________________
226 
227 //_____________________________________________________________________________
228 Bool_t FairAnaSelector::Process(Long64_t entry)
229 {
230  // The Process() function is called for each entry in the tree (or possibly
231  // keyed object in the case of PROOF) to be processed. The entry argument
232  // specifies which entry in the currently loaded tree is to be processed.
233  // It can be passed to either FairAnaSelector::GetEntry() or TBranch::GetEntry()
234  // to read either all or the required parts of the data. When processing
235  // keyed objects with PROOF, the object is already loaded and is available
236  // via the fObject pointer.
237  //
238  // This function should contain the "body" of the analysis. It can contain
239  // simple or elaborate selection criteria, run algorithms on the data
240  // of the event and typically fill histograms.
241  //
242  // The processing can be stopped by calling Abort().
243  //
244  // Use fStatus to set the return value of TTree::Process().
245  //
246  // The return value is currently not used.
247  // cout << "-I- FairAnaSelector::Process(): Proceeding to analyze event " << entry << "." << endl;
248 
249  fRunAna->RunOneEvent(entry);
250 
251  // cout << "-I- FairAnaSelector::Process(): Event " << entry << " analyzed." << endl;
252  return kTRUE;
253 }
254 //_____________________________________________________________________________
255 
256 //_____________________________________________________________________________
258 {
259  // The SlaveTerminate() function is called after all entries or objects
260  // have been processed. When running with PROOF SlaveTerminate() is called
261  // on each slave server.
262  if ( !fRunAna ) { return; }
263 
264  if ( !fProofFile ) {
265  cout << "-I- FairAnaSelector::SlaveTerminate(): Calling fRunAna->TerminateRun()" << endl;
267  }
268 
269  cout << "-I- FairAnaSelector::SlaveTerminate(): fProofFile = \"" << fProofFile << "\"" << endl;
270  cout << "-I- FairAnaSelector::SlaveTerminate(): fFile = \"" << fFile << "\"" << endl;
271 
272  cout << "-I- FairAnaSelector::SlaveTerminate(): WorkingDirectory = \"" << gSystem->WorkingDirectory() << "\"" << endl;
273 
274  if ( fProofFile ) {
275 
276  cout << "-------------------------------- fOutput->ls()" << endl;
277  fOutput->ls();
278  cout << "-------------------------------- fOutput->Print()" << endl;
279  fOutput->Print();
280  cout << "-------------------------------- fProofFile->Print()" << endl;
281  fProofFile->Print();
282  cout << "--------------------------------" << endl;
283 
284  cout << "-I- FairAnaSelector::SlaveTerminate(): fOutput->Add(fProofFile);" << endl;
285  fOutput->Add(fProofFile);
286 
287  cout << "-------------------------------- fProofFile->Print()" << endl;
288  fProofFile->Print();
289  cout << "--------------------------------" << endl;
290 
291  cout << "-I- FairAnaSelector::SlaveTerminate(): fFile->Close();" << endl;
293  // fFile->Close();
294 
295  }
296 
297  cout << "-I- FairAnaSelector::SlaveTerminate(): Finishing..." << endl;
298 }
299 //_____________________________________________________________________________
300 
301 //_____________________________________________________________________________
303 {
304  // The Terminate() function is the last function to be called during
305  // a query. It always runs on the client, it can be used to present
306  // the results graphically or save the results to file.
307  cout << "-I- FairAnaSelector::Terminate(): fOutput->ls()" << endl;
308  fOutput->ls();
309  cout << "-I- FairAnaSelector::Terminate(): -------------" << endl;
310 }
311 //_____________________________________________________________________________