FairRoot
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
FairAnaSelector.cxx
Go to the documentation of this file.
1 /********************************************************************************
2  * Copyright (C) 2014 GSI Helmholtzzentrum fuer Schwerionenforschung GmbH *
3  * *
4  * This software is distributed under the terms of the *
5  * GNU Lesser General Public Licence (LGPL) version 3, *
6  * copied verbatim in the file "LICENSE" *
7  ********************************************************************************/
8 // -------------------------------------------------------------------------
9 // ----- FairAnaSelector source file -----
10 // ----- Created 14/10/11 by R. Karabowicz -----
11 // ----- Updated 01/02/12 by R. Karabowicz -----
12 // -------------------------------------------------------------------------
13 
14 #include "FairAnaSelector.h"
15 
16 #include "FairFileSource.h" // for FairFileSource
17 #include "FairLogger.h" // for FairLogger, MESSAGE_ORIGIN
18 #include "FairParAsciiFileIo.h" // for FairParAsciiFileIo
19 #include "FairParRootFileIo.h" // for FairParRootFileIo
20 #include "FairRootFileSink.h" // for FairRootFileSink
21 #include "FairRootManager.h" // for FairRootManager
22 #include "FairRunAnaProof.h" // for FairRunAnaProof
23 #include "FairRuntimeDb.h" // for FairRuntimeDb
24 #include "FairTask.h" // for FairTask
25 
26 #include <TFile.h> // for TFile
27 #include <TList.h> // for TList
28 #include <TNamed.h> // for TNamed
29 #include <TObject.h> // for TObject
30 #include <TProofOutputFile.h> // for TProofOutputFile
31 #include <TProofServ.h> // for TProofServ
32 #include <TSystem.h> // for TSystem, gSystem
33 
34 void FairAnaSelector::Init(TTree* tree)
35 {
36  // The Init() function is called when the selector needs to initialize
37  // a new tree or chain. Typically here the branch addresses and branch
38  // pointers of the tree will be set.
39  // It is normally not necessary to make changes to the generated
40  // code, but the routine can be extended by the user if needed.
41  // Init() will be called many times when running on PROOF
42  // (once per file to be processed).
43  if (!tree) {
44  LOG(warn) << "FairAnaSelector::Init(): There is no tree.";
45  return;
46  } else {
47  LOG(info) << "FairAnaSelector::Init(): Got tree : \"" << tree << "\"";
48  LOG(info) << "FairAnaSelector::Init(): Tree name : \"" << tree->GetName() << "\"";
49  LOG(info) << "FairAnaSelector::Init(): Tree title : \"" << tree->GetTitle() << "\"";
50  LOG(info) << "FairAnaSelector::Init(): Tree filename: \"" << tree->GetCurrentFile()->GetName() << "\"";
51  }
52 
53  if (fRunAna) {
54  LOG(info) << "FairAnaSelector::Init(): Already have fRunAna.";
55 
56  LOG(info) << "FairAnaSelector::Init(): SetInTree(" << tree << ")";
57  fProofSource->SetInTree(tree);
58  LOG(info) << "FairAnaSelector::Init(): SetInTree done";
59 
60  LOG(info) << "FairAnaSelector::Init(): Containers static? " << (fRunAna->GetContainerStatic() ? "YES" : "NO");
61 
62  if (!fRunAna->GetContainerStatic()) {
64  }
66  } else {
67  LOG(info) << "FairAnaSelector::Init(): Have to create fRunAna.";
68 
69  TString vmcPath = gSystem->Getenv("VMCWORKDIR");
70 
71  TNamed* contStat = static_cast<TNamed*>(fInput->FindObject("FAIRRUNANA_fContainerStatic"));
72  TNamed* outStat = static_cast<TNamed*>(fInput->FindObject("FAIRRUNANA_fProofOutputStatus"));
73  TNamed* outFile = static_cast<TNamed*>(fInput->FindObject("FAIRRUNANA_fOutputFileName"));
74  TNamed* outDir = static_cast<TNamed*>(fInput->FindObject("FAIRRUNANA_fOutputDirectory"));
75  TNamed* par1Name = static_cast<TNamed*>(fInput->FindObject("FAIRRUNANA_fParInput1FName"));
76  TNamed* par2Name = static_cast<TNamed*>(fInput->FindObject("FAIRRUNANA_fParInput2FName"));
77  TString containerS = contStat->GetTitle();
78  TString outputStat = outStat->GetTitle();
79  TString par1Str = par1Name->GetTitle();
80  TString par2Str = par2Name->GetTitle();
81  TString outFileName = outFile->GetTitle();
82  TString outDirName = outDir->GetTitle();
83 
84  LOG(info) << "FairAnaSelector::Init(): out status : \"" << outputStat.Data() << "\"";
85  LOG(info) << "FairAnaSelector::Init(): par1 file : \"" << par1Str.Data() << "\"";
86  LOG(info) << "FairAnaSelector::Init(): par2 file : \"" << par2Str.Data() << "\"";
87 
88  LOG(info) << "FairAnaSelector::Init(): OutputFile option \"" << outputStat.Data() << "\" RECOGNIZED";
89 
90  if (outputStat.Contains("copy")) {
91  TString outputFileName = outFile->GetTitle();
92  if (outputFileName[0] != '/') {
93  outputFileName = Form("%s/%s", outDir->GetTitle(), outFile->GetTitle());
94  }
95  outputFileName.Remove(outputFileName.Length() - 5);
96  outputFileName = Form("%s_worker_%s.root", outputFileName.Data(), gProofServ->GetOrdinal());
97  // outputFileName = outputFileName(outputFileName.Last('/')+1,outputFileName.Length());
98  LOG(info) << "the file name = \"" << outputFileName.Data() << "\"";
99  fFile = TFile::Open(outputFileName.Data(), "RECREATE");
100  } else if (outputStat.Contains("merge")) {
101  TString outputFileName = outFile->GetTitle();
102  if (outputFileName[0] != '/') {
103  outputFileName = Form("%s/%s", outDir->GetTitle(), outFile->GetTitle());
104  }
105  // outputFileName = outputFileName(outputFileName.Last('/')+1,outputFileName.Length());
106  fProofFile = new TProofOutputFile(outputFileName.Data());
107  if (!(fFile = fProofFile->OpenFile("RECREATE"))) {
108  Warning("SlaveBegin", "problems opening file: %s/%s", fProofFile->GetDir(), fProofFile->GetFileName());
109  }
110  }
111 
112  fRunAna = new FairRunAnaProof("RunOnProofWorker");
113 
114  LOG(info) << "FairAnaSelector::Init(): SetInTree(" << tree << ")";
115  fProofSource = new FairFileSource(tree->GetCurrentFile());
116  fRunAna->SetSource(fProofSource);
117  LOG(info) << "FairAnaSelector::Init(): SetInTree done";
118 
120  if (containerS == "kTRUE") {
121  fRunAna->SetContainerStatic(kTRUE);
122  } else {
123  fRunAna->SetContainerStatic(kFALSE);
124  }
125 
126  // ----- Parameter database --------------------------------------------
128 
129  if (par1Str.Contains(".root")) {
130  FairParRootFileIo* parInput1 = new FairParRootFileIo();
131  parInput1->open(par1Str.Data());
132  rtdb->setFirstInput(parInput1);
133  }
134  if (par1Str.Contains(".par")) {
135  FairParAsciiFileIo* parInput1 = new FairParAsciiFileIo();
136  parInput1->open(par1Str.Data(), "in");
137  rtdb->setFirstInput(parInput1);
138  }
139 
140  if (par2Str.Contains(".root")) {
141  FairParRootFileIo* parInput2 = new FairParRootFileIo();
142  parInput2->open(par2Str.Data());
143  rtdb->setSecondInput(parInput2);
144  }
145  if (par2Str.Contains(".par")) {
146  FairParAsciiFileIo* parInput2 = new FairParAsciiFileIo();
147  parInput2->open(par2Str.Data(), "in");
148  rtdb->setSecondInput(parInput2);
149  }
150 
151  // ------------------------------------------------------------------------
152 
153  FairTask* fairTaskList = dynamic_cast<FairTask*>(fInput->FindObject("FairTaskList"));
154 
155  if (fairTaskList != 0) {
156  LOG(info) << "FairAnaSelector::Init(): FairTask = \"" << fairTaskList << "\"";
157  fRunAna->SetTask(fairTaskList);
158  }
159 
160  LOG(info) << "FairAnaSelector::Init(): vvvvv fRunAna->Init() vvvvv";
161  fRunAna->Init();
162  LOG(info) << "FairAnaSelector::Init(): ^^^^^ fRunAna->Init() ^^^^^";
163  }
164 }
165 
167 {
168  // The Notify() function is called when a new file is opened. This
169  // can be either for a new TTree in a TChain or when when a new TTree
170  // is started when using PROOF. It is normally not necessary to make changes
171  // to the generated code, but the routine can be extended by the
172  // user if needed. The return value is currently not used.
173  LOG(info) << "FairAnaSelector::Notify()";
174 
175  return kTRUE;
176 }
177 
178 void FairAnaSelector::Begin(TTree* /*tree*/)
179 {
180  // The Begin() function is called at the start of the query.
181  // When running with PROOF Begin() is only called on the client.
182  // The tree argument is deprecated (on PROOF 0 is passed).
183  LOG(info) << "FairAnaSelector::Begin()";
184 
185  fCurrentDirectory = gSystem->pwd();
186  TNamed* outFile = static_cast<TNamed*>(fInput->FindObject("FAIRRUNANA_fOutputFileName"));
187  TNamed* outDir = static_cast<TNamed*>(fInput->FindObject("FAIRRUNANA_fOutputDirectory"));
188 
189  TString outputFileName = outFile->GetTitle();
190  if (outputFileName[0] != '/') {
191  outputFileName = Form("%s/%s", outDir->GetTitle(), outFile->GetTitle());
192  }
193  TString outputDir = outputFileName(0, outputFileName.Last('/') + 1);
194  fCurrentDirectory = gSystem->pwd();
195  gSystem->cd(outputDir.Data());
196 }
197 
199 {
200  // The SlaveBegin() function is called after the Begin() function.
201  // When running with PROOF SlaveBegin() is called on each slave server.
202  // The tree argument is deprecated (on PROOF 0 is passed).
203  LOG(info) << "FairAnaSelector::SlaveBegin(): Tree address : \"" << tree << "\"";
204 
205  // useless, because have no tree anyways in slavebegin, init will be anyways called whenever a new tree comes
206  // Init(tree);
207 
208  LOG(info) << "FairAnaSelector::SlaveBegin(): finishing";
209 }
210 
211 Bool_t FairAnaSelector::Process(Long64_t entry)
212 {
213  // The Process() function is called for each entry in the tree (or possibly
214  // keyed object in the case of PROOF) to be processed. The entry argument
215  // specifies which entry in the currently loaded tree is to be processed.
216  // It can be passed to either FairAnaSelector::GetEntry() or TBranch::GetEntry()
217  // to read either all or the required parts of the data. When processing
218  // keyed objects with PROOF, the object is already loaded and is available
219  // via the fObject pointer.
220  //
221  // This function should contain the "body" of the analysis. It can contain
222  // simple or elaborate selection criteria, run algorithms on the data
223  // of the event and typically fill histograms.
224  //
225  // The processing can be stopped by calling Abort().
226  //
227  // Use fStatus to set the return value of TTree::Process().
228  //
229  // The return value is currently not used.
230  // LOG(info) << "FairAnaSelector::Process(): Proceeding to analyze event " << entry << ".";
231 
232  fRunAna->RunOneEvent(entry);
233 
234  // LOG(info) << "FairAnaSelector::Process(): Event " << entry << " analyzed.";
235  return kTRUE;
236 }
237 
239 {
240  // The SlaveTerminate() function is called after all entries or objects
241  // have been processed. When running with PROOF SlaveTerminate() is called
242  // on each slave server.
243  if (!fRunAna) {
244  return;
245  }
246 
247  if (!fProofFile) {
248  LOG(info) << "FairAnaSelector::SlaveTerminate(): Calling fRunAna->TerminateRun()";
250  }
251 
252  LOG(info) << "FairAnaSelector::SlaveTerminate(): fProofFile = \"" << fProofFile << "\"";
253  if (fProofFile)
254  LOG(info) << "FairAnaSelector::SlaveTerminate(): fProofFile = \"" << fProofFile->GetName() << "\"";
255  LOG(info) << "FairAnaSelector::SlaveTerminate(): fFile = \"" << fFile << "\"";
256  if (fFile)
257  LOG(info) << "FairAnaSelector::SlaveTerminate(): fFile = \"" << fFile->GetName() << "\"";
258 
259  LOG(info) << "FairAnaSelector::SlaveTerminate(): WorkingDirectory = \"" << gSystem->WorkingDirectory() << "\"";
260 
261  if (fProofFile) {
262  // fOutput->ls();
263  // fOutput->Print();
264  // fProofFile->Print();
265 
266  LOG(info) << "FairAnaSelector::SlaveTerminate(): fOutput->Add(fProofFile);";
267  fOutput->Add(fProofFile);
268 
269  // fProofFile->Print();
270 
271  LOG(info) << "FairAnaSelector::SlaveTerminate(): fFile->Close();";
273 
274  // fFile->Close();
275  }
276  LOG(info) << "FairAnaSelector::SlaveTerminate(): Finishing...";
277 }
278 
280 {
281  // The Terminate() function is the last function to be called during
282  // a query. It always runs on the client, it can be used to present
283  // the results graphically or save the results to file.
284  LOG(info) << "FairAnaSelector::Terminate(): fOutput->ls()";
285  gSystem->cd(fCurrentDirectory.Data());
286  LOG(info) << "FairAnaSelector::Terminate(): -------------";
287 }
list of container factories
Definition: FairRuntimeDb.h:24
Bool_t open(const Text_t *fname, const Text_t *status="in")
virtual void SlaveBegin(TTree *tree)
void TerminateRun()
Definition: FairRunAna.cxx:602
void SetInTree(TTree *tempTree)
TProofOutputFile * fProofFile
virtual void SlaveTerminate()
static FairRootManager * Instance()
Bool_t setSecondInput(FairParIo *)
Bool_t GetContainerStatic()
Definition: FairRunAna.h:90
void SetSink(FairSink *tempSink)
Definition: FairRun.h:84
FairRunAnaProof * fRunAna
pointer to the analyzed TTree or TChain
virtual void Init(TTree *tree)
virtual void Terminate()
void SetContainerStatic(Bool_t tempBool=kTRUE)
Definition: FairRunAna.cxx:637
virtual void Begin(TTree *tree)
virtual Bool_t Process(Long64_t entry)
FairRuntimeDb * GetRuntimeDb(void)
Definition: FairRun.h:80
Bool_t open(const Text_t *fname, Option_t *option="READ", const Text_t *ftitle="", Int_t compress=1)
virtual void SetSource(FairSource *tempSource)
Bool_t setFirstInput(FairParIo *)
virtual void SetTask(FairTask *t)
Definition: FairRun.cxx:91
virtual Bool_t Notify()
void RunOneEvent(Long64_t entry)