FairRoot
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
FairRunAnaProof.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 // ----- FairRunAnaProof source file -----
10 // ----- Created 30/04/13 by R. Karabowicz -----
11 // -------------------------------------------------------------------------
12 
13 #include "FairRunAnaProof.h"
14 
15 #include "FairBaseParSet.h"
16 #include "FairEventHeader.h"
17 #include "FairFieldFactory.h"
18 #include "FairFileHeader.h"
19 #include "FairLogger.h"
20 #include "FairParIo.h"
21 #include "FairRunIdGenerator.h"
22 #include "FairRuntimeDb.h"
23 #include "FairSink.h"
24 #include "FairTask.h"
25 #include "FairTrajFilter.h"
26 
27 #include <TGeoManager.h>
28 #include <TKey.h>
29 #include <TProof.h>
30 #include <TROOT.h>
31 
33 
35 
36 FairRunAnaProof::FairRunAnaProof(const char* proofName)
37  : FairRunAna()
38  , fProof(nullptr)
39  , fRunOnProofWorker(kFALSE)
40  , fProofServerName(proofName)
41  , fProofParName("$VMCWORKDIR/gconfig/libFairRoot.par")
42  , fOutputDirectory("")
43  , fProofOutputStatus("copy")
44  , fProofFileSource(0)
45 {
46  if (strcmp(proofName, "RunOnProofWorker") == 0) {
47  fRunOnProofWorker = kTRUE;
48  } else {
49  LOG(info) << "+++++++ T P R O O F +++++++++++++++++++++++++++++++++";
50  LOG(info) << "creating TProof* proof = TProof::Open(\"" << fProofServerName.Data() << "\");";
51  TProof::AddEnvVar("LOCALDATASERVER", "file://");
52  // TProof* proof = TProof::Open("lite:///?workers=1");
53  fProof = TProof::Open(fProofServerName.Data());
54  LOG(info) << "+++++++ C R E A T E D +++++++++++++++++++++++++++++++";
55  }
56 
57  fRAPInstance = this;
58  fAna = kTRUE;
59 }
60 
62 
64 {
66 
67  if (fIsInitialized) {
68  LOG(fatal) << "Error Init is already called before!";
69  exit(-1);
70  } else {
71  fIsInitialized = kTRUE;
72  }
73 
74  if (!fRunOnProofWorker) {
75  // Open the input file and add other input files added by AddFile to the
76  // input chain. Do a check if the added files are of the same type
77  // as the the input file. Same type means check if they contain the
78  // same branch.
79  }
80  // Load Geometry from user file
81 
82  if (fLoadGeo) {
83  if (fInputGeoFile != 0) { // First check if the user has a separate Geo file!
84  TIter next(fInputGeoFile->GetListOfKeys());
85  TKey* key;
86  while ((key = static_cast<TKey*>(next()))) {
87  if (strcmp(key->GetClassName(), "TGeoManager") != 0) {
88  continue;
89  }
90  gGeoManager = static_cast<TGeoManager*>(key->ReadObj());
91  break;
92  }
93  }
94  } else {
95  // FairGeoParSet* geopar=dynamic_cast<FairGeoParSet*>(fRtdb->getContainer("FairGeoParSet"));
96  fRtdb->getContainer("FairGeoParSet");
97  }
98 
99  if (fInFileIsOpen) {
100  if (fRunOnProofWorker) {
101  if (fLoadGeo && gGeoManager == 0) {
102  fProofFileSource->GetInTree()->GetCurrentFile()->Get("FAIRGeom");
103  }
104  } else {
105 
106  // Add all friend files defined by AddFriend to the correct chain
107  if (fLoadGeo && gGeoManager == 0) {
108  // Check if the geometry in the first file of the Chain
109  fProofFileSource->GetInTree()->GetCurrentFile()->Get("FAIRGeom");
110  }
111  // check that the geometry was loaded if not try all connected files!
112  if (fLoadGeo && gGeoManager == 0) {
113  LOG(info) << "Geometry was not found in the input file we will look in the friends if any!";
114  TFile* currentfile = gFile;
115  TFile* nextfile = 0;
116  TSeqCollection* fileList = gROOT->GetListOfFiles();
117  for (Int_t k = 0; k < fileList->GetEntries(); k++) {
118  nextfile = static_cast<TFile*>(fileList->At(k));
119  if (nextfile) {
120  nextfile->Get("FAIRGeom");
121  }
122  if (gGeoManager) {
123  break;
124  }
125  }
126  gFile = currentfile;
127  }
128  }
129  } else { // if(fInputFile )
130  // NO input file but there is a geometry file
131  if (fLoadGeo) {
132  if (fInputGeoFile != 0) { // First check if the user has a separate Geo file!
133  TIter next(fInputGeoFile->GetListOfKeys());
134  TKey* key;
135  while ((key = static_cast<TKey*>(next()))) {
136  if (strcmp(key->GetClassName(), "TGeoManager") != 0) {
137  continue;
138  }
139  gGeoManager = static_cast<TGeoManager*>(key->ReadObj());
140  break;
141  }
142  }
143  }
144  }
145  // Init the Chain ptr
146  // fcurrent = fChainList.begin();
147  // fOutFile = fRootManager->OpenOutFile(fOutname);
148 
149  if (!fRunOnProofWorker) {
150  LOG(warn) << "QUITTING, CAUSE IT'S not running on proof worker";
151  return;
152  }
153  gROOT->GetListOfBrowsables()->Add(fTask);
154 
155  // Init the RTDB containers
156  fRtdb = GetRuntimeDb();
157  FairBaseParSet* par = static_cast<FairBaseParSet*>(fRtdb->getContainer("FairBaseParSet"));
158 
160  if (fTimeStamps) {
162  }
163 
164  // create the output tree after tasks initialisation
166 
167  // Assure that basic info is there for the run
168 
169  if (par && fInFileIsOpen) {
170 
171  LOG(info) << "Parameter and input file are available, Assure that basic info is there for the run!";
173 
174  GetEventHeader();
175 
177 
179 
180  // Copy the Event Header Info to Output
181  fEvtHeader->Register(kTRUE);
182 
183  // Copy the Event Header Info to Output
184  fEvtHeader->Register();
185 
186  // Init the containers in Tasks
187 
189  fTask->SetParTask();
190 
192  if (gGeoManager == 0) {}
193  // fRootManager->SetBranchNameList(par->GetBranchNameList());
194 
195  } else {
196  LOG(info) << "Initializing without input file or Mixed input";
198  evt->Register();
199  FairRunIdGenerator genid;
200  fRunId = genid.generateId();
201  fRtdb->addRun(fRunId);
202  evt->SetRunId(fRunId);
203  fTask->SetParTask();
205  }
207  if (fieldfact) {
208  fieldfact->SetParm();
209  }
210 
213 
214  // create a field
215  // <DB>
216  // Add test for external FairField settings
217  if (fieldfact && !fField) {
218  fField = fieldfact->createFairField();
219  }
220  // Now call the User initialize for Tasks
221  fTask->InitTask();
222  // if the vis manager is available then initialize it!
223  FairTrajFilter* fTrajFilter = FairTrajFilter::Instance();
224  if (fTrajFilter) {
225  fTrajFilter->Init();
226  }
227 
229 
232 }
233 
235 {
236  fRtdb = GetRuntimeDb();
237  FairBaseParSet* par = dynamic_cast<FairBaseParSet*>(fRtdb->getContainer("FairBaseParSet"));
238 
239  if (par && fInFileIsOpen) {
241 
242  fEvtHeader = dynamic_cast<FairEventHeader*>(fRootManager->GetObject("EventHeader."));
243 
244  if (nullptr == fEvtHeader)
245  LOG(fatal) << "Could not read event header.";
246 
248 
250 
251  // Copy the Event Header Info to Output
252  fEvtHeader->Register();
253 
254  // Init the containers in Tasks
256  fTask->ReInitTask();
257  // fTask->SetParTask();
259  if (gGeoManager == 0) {
260  // par->GetGeometry();
261  }
262  }
263 }
264 
266 {
267  // FairRunAnaProof should accept only FairFileSource
268  if (strncmp(tempSource->GetName(), "FairFileSource", 14) != 0) {
269  LOG(warn) << "FairRunAnaProof. Seems you are trying to set different source than FairFileSource";
270  }
271  fRootManager->SetSource(tempSource);
272  fProofFileSource = static_cast<FairFileSource*>(tempSource);
273 }
274 
275 void FairRunAnaProof::Run(Int_t Ev_start, Int_t Ev_end)
276 {
277  RunOnProof(Ev_start, Ev_end);
278  return;
279 }
280 
281 void FairRunAnaProof::RunOneEvent(Long64_t entry)
282 {
283  if (fTimeStamps) {
284  RunTSBuffers();
285  } else {
286  UInt_t tmpId = 0;
287  fRootManager->ReadEvent(entry);
288 
290 
291  tmpId = fEvtHeader->GetRunId();
292  if (tmpId != fRunId) {
293  fRunId = tmpId;
294  if (!fStatic) {
295  Reinit(fRunId);
296  fTask->ReInitTask();
297  }
298  }
300 
301  fTask->ExecuteTask("");
302  fRootManager->Fill();
304  fTask->FinishEvent();
305  }
306 }
307 
308 void FairRunAnaProof::RunOnProof(Int_t NStart, Int_t NStop)
309 {
310  fProofOutputStatus.ToLower();
311  if (!fProofOutputStatus.Contains("copy") && !fProofOutputStatus.Contains("merge")) {
312  LOG(warn) << "FairRunAnaProof::RunOnProof. Do not know how to create output \"" << fProofOutputStatus.Data()
313  << "\".";
314  LOG(warn) << "FairRunAnaProof::RunOnProof. Please use SetProofOutputStatus to either \"copy\" or \"merge\".";
315  LOG(warn) << "FairRunAnaProof::RunOnProof. For the current run using the \"merge\" setting.";
316  fProofOutputStatus = "merge";
317  }
318 
319  // TChain* inChain = static_cast<TChain*>(fRootManager->GetInChain());
320  TChain* inChain = static_cast<TChain*>(fProofFileSource->GetInChain());
321  TString par1File = "";
322  TString par2File = "";
323  if (fRtdb->getFirstInput()) {
324  par1File = fRtdb->getFirstInput()->getFilename();
325  }
326  if (fRtdb->getSecondInput()) {
327  par2File = fRtdb->getSecondInput()->getFilename();
328  }
329 
330  TString outDir = (fOutputDirectory.Length() > 1 ? fOutputDirectory.Data() : gSystem->WorkingDirectory());
331 
332  fProof->AddInput(fTask);
333 
334  // get file name from FairSink
335  TString fileName = fRootManager->GetSink()->GetFileName();
336  LOG(info) << " outputFileName = " << fileName.Data();
337  if (fileName.Length() < 5)
338  fileName = "proofOutput.root";
339 
340  fProof->AddInput(new TNamed("FAIRRUNANA_fContainerStatic", (fStatic ? "kTRUE" : "kFALSE")));
341  fProof->AddInput(new TNamed("FAIRRUNANA_fProofOutputStatus", fProofOutputStatus.Data()));
342  fProof->AddInput(new TNamed("FAIRRUNANA_fOutputDirectory", outDir.Data()));
343  fProof->AddInput(new TNamed("FAIRRUNANA_fOutputFileName", fileName.Data()));
344  fProof->AddInput(new TNamed("FAIRRUNANA_fParInput1FName", par1File.Data()));
345  fProof->AddInput(new TNamed("FAIRRUNANA_fParInput2FName", par2File.Data()));
346 
347  Int_t nofChainEntries = inChain->GetEntries();
348  LOG(info) << "FairRunAnaProof::RunOnProof(): The chain seems to have " << nofChainEntries << " entries.";
349 
350  TObjArray* listOfFiles = inChain->GetListOfFiles();
351  LOG(info) << "FairRunAnaProof::RunOnProof(): There are " << listOfFiles->GetEntries() << " files in the chain.";
352 
353  inChain->SetProof();
354 
355  Int_t nofEventsToAnalyze = NStop - NStart;
356 
357  if (nofEventsToAnalyze <= 0) {
358  LOG(info) << "You requested to analyze events from " << NStart << " to " << NStop << " that is "
359  << nofEventsToAnalyze << " events!!!";
360  nofEventsToAnalyze = nofChainEntries - NStart;
361  LOG(info) << "It will be changed to analyze all events from " << NStart << " to the end of chain ("
362  << nofChainEntries << "), that is to analyze " << nofEventsToAnalyze << " events.";
363  }
364 
365  LOG(info) << "FairRunAnaProof::RunOnProof(): Starting inChain->Process(\"FairAnaSelector\",\"\","
366  << nofEventsToAnalyze << "," << NStart << ")";
367  inChain->Process("FairAnaSelector", "", nofEventsToAnalyze, NStart);
368  LOG(info) << "FairRunAnaProof::RunOnProof(): inChain->Process DONE";
369 
370  return;
371 }
372 
373 // // void FairRunAnaProof::SetOutputFile(const char* fname)
374 // {
375 // fOutname=fname;
376 // }
377 //
378 // // void FairRunAnaProof::SetOutputFile(TFile* f)
379 // {
380 // if (! fRootManager) return;
381 
382 // fOutname=f->GetName();
383 // fRootManager->OpenOutFile(f);
384 // fOutFile = f;
385 
386 // }
387 //
Bool_t fRunOnProofWorker
virtual ~FairRunAnaProof()
Bool_t fTimeStamps
Definition: FairRunAna.h:121
virtual FairField * createFairField()
Bool_t fAna
Definition: FairRun.h:203
virtual void ExecuteTask(Option_t *option="0")
Definition: FairTask.cxx:118
Int_t ReadEvent(Int_t i=0)
void Reinit(UInt_t runId)
Definition: FairRunAna.cxx:617
unsigned int generateId(void)
Bool_t fLoadGeo
Definition: FairRunAna.h:117
void ReInitTask()
Definition: FairTask.cxx:58
void RunOnProof(Int_t NStart, Int_t NStop)
FairRootManager * fRootManager
Definition: FairRun.h:193
void SetRunId(UInt_t runid)
FairTask * fTask
Definition: FairRun.h:191
void WriteFileHeader(FairFileHeader *f)
FairEventHeader * GetEventHeader()
Definition: FairRun.cxx:112
Bool_t fInFileIsOpen
Definition: FairRunAna.h:122
void RunTSBuffers()
Definition: FairRunAna.cxx:524
FairEventHeader * fEvtHeader
Definition: FairRun.h:205
ClassImp(FairEventBuilder)
TObject * GetObject(const char *BrName)
static FairFieldFactory * Instance()
virtual void SetParm()
FairRunAnaProof(const char *proofName="")
void Run(Int_t NStart=0, Int_t NStop=0)
static FairRunAnaProof * fRAPInstance
FairField * fField
Definition: FairRunAna.h:120
FairParSet * getContainer(const Text_t *)
FairSink * GetSink()
Bool_t initContainers(Int_t runId, Int_t refId=-1, const Text_t *fileName="")
TString fProofServerName
virtual void Register(Bool_t Persistance=kTRUE)
FairMQExParamsParOne * par
FairFileHeader * fFileHeader
Definition: FairRun.h:207
Double_t GetEventTime()
static FairTrajFilter * Instance()
FairParIo * getSecondInput(void)
TString fProofOutputStatus
void InitTask()
Definition: FairTask.cxx:41
FairRuntimeDb * GetRuntimeDb(void)
Definition: FairRun.h:80
const char * getFilename()
Definition: FairParIo.h:36
static FairRunAnaProof * Instance()
Bool_t fStatic
Definition: FairRunAna.h:119
FairRtdbRun * addRun(Int_t runId, Int_t refId=-1)
void SetSource(FairSource *tempSource)
TChain * GetInChain()
UInt_t fRunId
Definition: FairRun.h:201
void UpdateListOfTimebasedBranches()
TTree * GetInTree()
virtual void FinishEvent()
Definition: FairTask.cxx:82
FairRuntimeDb * fRtdb
Definition: FairRun.h:189
void SetRunId(UInt_t runid)
void DeleteOldWriteoutBufferData()
TFile * fInputGeoFile
Definition: FairRunAna.h:115
TString fOutputDirectory
virtual void SetSource(FairSource *tempSource)
void StoreWriteoutBufferData(Double_t eventTime)
virtual TString GetFileName()
Definition: FairSink.h:47
Bool_t fIsInitialized
Definition: FairRunAna.h:114
FairParIo * getFirstInput(void)
void RunOneEvent(Long64_t entry)
void Init(TString brName="GeoTracks", TString folderName="MCGeoTrack")
void SetParTask()
Definition: FairTask.cxx:73
void FillEventHeader(FairEventHeader *feh)