FairRoot
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
FairFileSource.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 //
10 // FairFileSource.cxx
11 // FAIRROOT
12 //
13 // Created by Mohammad Al-Turany on 08/02/14.
14 //
15 //
16 #include "FairFileSource.h"
17 
18 #include "FairEventHeader.h"
19 #include "FairLogger.h"
20 #include "FairMCEventHeader.h"
21 #include "FairRootManager.h"
22 #include "FairRuntimeDb.h" // for FairRuntimeDb
23 
24 #include <TChain.h>
25 #include <TChainElement.h>
26 #include <TClass.h>
27 #include <TCollection.h> // for TIter
28 #include <TF1.h>
29 #include <TFolder.h>
30 #include <TList.h>
31 #include <TObjArray.h>
32 #include <TObjString.h>
33 #include <TObject.h>
34 #include <TROOT.h>
35 #include <TRandom.h> // for TRandom, gRandom
36 #include <TString.h>
37 #include <TTree.h>
38 #include <algorithm> // for find
39 #include <cmath> // fmod
40 #include <cstdlib> // exit
41 #include <list> // for _List_iterator, list, etc
42 #include <map> // multimap
43 #include <set>
44 #include <typeinfo>
45 #include <vector>
46 
47 using std::set;
48 
49 FairFileSource::FairFileSource(TFile* f, const char* Title, UInt_t)
50  : FairSource()
51  , fInputTitle(Title)
52  , fRootFile(f)
53  , fCurrentEntryNr(0)
54  , fFriendFileList()
55  , fInputChainList()
56  , fFriendTypeList()
57  , fCheckInputBranches()
58  , fInputLevel()
59  , fRunIdInfoAll()
60  , fInChain(0)
61  , fInTree(0)
62  , fListFolder(new TObjArray(16))
63  , fRtdb(FairRuntimeDb::instance())
64  , fCbmout(0)
65  , fCbmroot(0)
66  , fSourceIdentifier(0)
67  , fNoOfEntries(-1)
68  , IsInitialized(kFALSE)
69  , fMCHeader(0)
70  , fEvtHeader(0)
71  , fFileHeader(0)
72  , fEventTimeInMCHeader(kTRUE)
73  , fEvtHeaderIsNew(kFALSE)
74  , fCurrentEntryNo(0)
75  , fTimeforEntryNo(-1)
76  , fEventTimeMin(0.)
77  , fEventTimeMax(0.)
78  , fEventTime(0.)
79  , fBeamTime(-1.)
80  , fGapTime(-1.)
81  , fEventMeanTime(0.)
82  , fTimeProb(0)
83  , fCheckFileLayout(kTRUE)
84 {
85  if (fRootFile->IsZombie()) {
86  LOG(fatal) << "Error opening the Input file";
87  }
88  LOG(debug) << "FairFileSource created------------";
89 }
90 
91 FairFileSource::FairFileSource(const TString* RootFileName, const char* Title, UInt_t)
92  : FairSource()
93  , fInputTitle(Title)
94  , fRootFile(0)
95  , fCurrentEntryNr(0)
96  , fFriendFileList()
97  , fInputChainList()
98  , fFriendTypeList()
99  , fCheckInputBranches()
100  , fInputLevel()
101  , fRunIdInfoAll()
102  , fInChain(0)
103  , fInTree(0)
104  , fListFolder(new TObjArray(16))
105  , fRtdb(FairRuntimeDb::instance())
106  , fCbmout(0)
107  , fCbmroot(0)
108  , fSourceIdentifier(0)
109  , fNoOfEntries(-1)
110  , IsInitialized(kFALSE)
111  , fMCHeader(0)
112  , fEvtHeader(0)
113  , fFileHeader(0)
114  , fEventTimeInMCHeader(kTRUE)
115  , fEvtHeaderIsNew(kFALSE)
116  , fCurrentEntryNo(0)
117  , fTimeforEntryNo(-1)
118  , fEventTimeMin(0.)
119  , fEventTimeMax(0.)
120  , fEventTime(0.)
121  , fBeamTime(-1.)
122  , fGapTime(-1.)
123  , fEventMeanTime(0.)
124  , fTimeProb(0)
125  , fCheckFileLayout(kTRUE)
126 {
127  fRootFile = TFile::Open(RootFileName->Data());
128  if (fRootFile->IsZombie()) {
129  LOG(fatal) << "Error opening the Input file";
130  }
131  LOG(debug) << "FairFileSource created------------";
132 }
133 
134 FairFileSource::FairFileSource(const TString RootFileName, const char* Title, UInt_t)
135  : FairSource()
136  , fInputTitle(Title)
137  , fRootFile(0)
138  , fCurrentEntryNr(0)
139  , fFriendFileList()
140  , fInputChainList()
141  , fFriendTypeList()
142  , fCheckInputBranches()
143  , fInputLevel()
144  , fRunIdInfoAll()
145  , fInChain(0)
146  , fInTree(0)
147  , fListFolder(new TObjArray(16))
148  , fRtdb(FairRuntimeDb::instance())
149  , fCbmout(0)
150  , fCbmroot(0)
151  , fSourceIdentifier(0)
152  , fNoOfEntries(-1)
153  , IsInitialized(kFALSE)
154  , fMCHeader(0)
155  , fEvtHeader(0)
156  , fFileHeader(0)
157  , fEventTimeInMCHeader(kTRUE)
158  , fEvtHeaderIsNew(kFALSE)
159  , fCurrentEntryNo(0)
160  , fTimeforEntryNo(-1)
161  , fEventTimeMin(0.)
162  , fEventTimeMax(0.)
163  , fEventTime(0.)
164  , fBeamTime(-1.)
165  , fGapTime(-1.)
166  , fEventMeanTime(0.)
167  , fTimeProb(0)
168  , fCheckFileLayout(kTRUE)
169 {
170  fRootFile = TFile::Open(RootFileName.Data());
171  if (fRootFile->IsZombie()) {
172  LOG(fatal) << "Error opening the Input file";
173  }
174  LOG(debug) << "FairFileSource created------------";
175 }
176 
178 
180 {
181  if (IsInitialized) {
182  LOG(info) << "FairFileSource already initialized";
183  return kTRUE;
184  }
185  if (!fInChain) {
186  fInChain = new TChain(FairRootManager::GetTreeName(), Form("/%s", FairRootManager::GetFolderName()));
187  LOG(debug) << "FairFileSource::Init() chain created";
189  }
190  fInChain->Add(fRootFile->GetName());
191 
192  // Get the folder structure from file which describes the input tree.
193  // There are two different names possible, so check both.
194  fCbmroot = dynamic_cast<TFolder*>(fRootFile->Get(FairRootManager::GetFolderName()));
195  if (!fCbmroot) {
196  fCbmroot = dynamic_cast<TFolder*>(fRootFile->Get("cbmroot"));
197  if (!fCbmroot) {
198  fCbmroot = dynamic_cast<TFolder*>(fRootFile->Get("cbmout"));
199  if (!fCbmroot) {
200  fCbmroot = gROOT->GetRootFolder()->AddFolder(FairRootManager::GetFolderName(), "Main Folder");
201  } else {
202  fCbmroot->SetName(FairRootManager::GetFolderName());
203  }
204  }
205  }
206 
207  // Get The list of branches from the input file and add it to the
208  // actual list of existing branches.
209  // Add this list of branches also to the map of input trees, which
210  // stores the information which branches belong to which input tree.
211  // There is at least one primary input tree, but there can be many
212  // additional friend trees.
213  // This information is needed to add new files to the correct friend
214  // tree. With this information it is also possible to check if the
215  // input files which are added to the input chain all have the same
216  // branch structure. Without this check it is possible to add trees
217  // with a different branch structure but the same tree name. ROOT
218  // probably only checks if the name of the tree is the same.
219  TList* list = dynamic_cast<TList*>(fRootFile->Get("BranchList"));
220  if (list == 0) {
221  LOG(fatal) << "No Branch list in input file";
222  }
223  TString chainName = fInputTitle;
224  TString ObjName;
225  fInputLevel.push_back(chainName);
226  fCheckInputBranches[chainName] = new std::list<TString>;
227  if (list) {
228  TObjString* Obj = 0;
229  LOG(debug) << "Enteries in the list " << list->GetEntries();
230  for (Int_t i = 0; i < list->GetEntries(); i++) {
231  Obj = dynamic_cast<TObjString*>(list->At(i));
232  if (Obj != 0) {
233  ObjName = Obj->GetString();
234  LOG(debug) << "Branch name " << ObjName.Data();
235  fCheckInputBranches[chainName]->push_back(ObjName.Data());
236 
237  FairRootManager::Instance()->AddBranchToList(ObjName.Data());
238  }
239  }
240  }
241 
242  gROOT->GetListOfBrowsables()->Add(fCbmroot);
243  fListFolder->Add(fCbmroot);
244 
245  // Store the information about the unique runids in the input file
246  // together with the filename and the number of events for each runid
247  // this information is needed later to check if inconsitencies exist
248  // between the main input chain and any of the friend chains.
249 
250  // GetRunIdInfo(fInFile->GetName(), chainName);
251 
252  // Add all additional input files to the input chain and do a
253  // consitency check
254  for (auto fileName : fInputChainList) {
255  // Store global gFile pointer for safety reasons.
256  // Set gFile to old value at the end of the routine.R
257  TFile* temp = gFile;
258 
259  // Temporarily open the input file to extract information which
260  // is needed to bring the friend trees in the correct order
261  TFile* inputFile = TFile::Open(fileName);
262  if (inputFile->IsZombie()) {
263  LOG(fatal) << "Error opening the file " << fileName.Data()
264  << " which should be added to the input chain or as friend chain";
265  }
266 
267  if (fCheckFileLayout) {
268  // Check if the branchlist is the same as for the first input file.
269  Bool_t isOk = CompareBranchList(inputFile, chainName);
270  if (!isOk) {
271  LOG(fatal) << "Branch structure of the input file " << fRootFile->GetName()
272  << " and the file to be added " << fileName.Data() << " are different.";
273  return kFALSE;
274  }
275  }
276 
277  // Add the runid information for all files in the chain.
278  // GetRunIdInfo(inputFile->GetName(), chainName);
279  // Add the file to the input chain
280  fInChain->Add(fileName);
281 
282  // Close the temporarly file and restore the gFile pointer.
283  inputFile->Close();
284  gFile = temp;
285  }
286  fNoOfEntries = fInChain->GetEntries();
287 
288  LOG(debug) << "Entries in this Source " << fNoOfEntries;
289 
290  for (Int_t i = 0; i < fListFolder->GetEntriesFast(); i++) {
291  TFolder* fold = static_cast<TFolder*>(fListFolder->At(i));
292  fEvtHeader = static_cast<FairEventHeader*>(fold->FindObjectAny("EventHeader."));
293  fMCHeader = static_cast<FairMCEventHeader*>(fold->FindObjectAny("MCEventHeader."));
294  if (fEvtHeader) {
295  ActivateObject(reinterpret_cast<TObject**>(&fEvtHeader), "EventHeader.");
296  }
297  if (fMCHeader) {
298  ActivateObject(reinterpret_cast<TObject**>(&fMCHeader), "MCEventHeader.");
299  }
300  }
301 
303 
305 
306  TList* timebasedlist = dynamic_cast<TList*>(fRootFile->Get("TimeBasedBranchList"));
307  if (timebasedlist == 0) {
308  LOG(warn) << "No time based branch list in input file";
309  } else {
311  }
312 
313  return kTRUE;
314 }
315 
316 void FairFileSource::SetInTree(TTree* tempTree)
317 {
318  fInTree = nullptr;
319  fInTree = tempTree;
320  fRootFile = static_cast<TFile*>(tempTree->GetCurrentFile());
321  fInChain->Reset();
322  IsInitialized = kFALSE;
323  Init();
324 }
325 
327 {
328  fCurrentEntryNo = i;
329  fEventTime = GetEventTime();
330  if (fInChain->GetEntry(i))
331  return 0;
332 
333  return 1;
334 }
335 
337 {
339  if (fInChain->GetEntry(0))
340  return true;
341 
342  return false;
343 }
344 
346 
348 
349 void FairFileSource::AddFriend(TString fName) { fFriendFileList.push_back(fName); }
350 
351 void FairFileSource::AddFile(TString FileName) { fInputChainList.push_back(FileName); }
352 
354 {
355  // Loop over all Friend files and extract the type. The type is defined by
356  // the tree which is stored in the file. If there is already a chain of with
357  // this type of tree then the file will be added to this chain.
358  // If there is no such chain it will be created.
359  //
360  // Check if the order of runids and the event numbers per runid for all
361  // friend chains is the same as the one defined by the input chain.
362  // TODO: Should the order be corrected or should the execution be stopped.
363  // The order in the input tree defined by the order in which the files have
364  // been added. A file is defined by the runid.
365 
366  // In the old way it was needed sometimes to add a freind file more
367  // than once. This is not needed any longer, so we remove deuplicates
368  // from the list and display a warning.
369  std::list<TString> friendList;
370  for (auto fileName : fFriendFileList) {
371  if (find(friendList.begin(), friendList.end(), fileName) == friendList.end()) {
372  friendList.push_back(fileName);
373  }
374  }
375  // TODO: print a warning if it was neccessary to remove a filname from the
376  // list. This can be chacked by comparing the size of both list
377 
378  TFile* temp = gFile;
379 
380  Int_t friendType = 1;
381  // Loop over all files which have been added as friends
382  for (auto fileName : friendList) {
383  TString inputLevel;
384  // Loop over all already defined input levels to check if this type
385  // of friend tree is already added.
386  // If this type of friend tree already exist add the file to the
387  // then already existing friend chain. If this type of friend tree
388  // does not exist already create a new friend chain and add the file.
389  Bool_t inputLevelFound = kFALSE;
390  TFile* inputFile;
391  for (auto level : fInputLevel) {
392  inputLevel = level;
393 
394  inputFile = TFile::Open(fileName);
395  if (inputFile->IsZombie()) {
396  LOG(fatal) << "Error opening the file " << level.Data()
397  << " which should be added to the input chain or as friend chain";
398  }
399 
400  // Check if the branchlist is already stored in the map. If it is
401  // already stored add the file to the chain.
402  Bool_t isOk = CompareBranchList(inputFile, inputLevel);
403  if (isOk) {
404  inputLevelFound = kTRUE;
405  inputFile->Close();
406  continue;
407  }
408  inputFile->Close();
409  }
410  if (!inputLevelFound) {
411  inputLevel = Form("FriendTree_%i", friendType);
412  CreateNewFriendChain(fileName, inputLevel);
413  friendType++;
414  }
415 
416  TChain* chain = static_cast<TChain*>(fFriendTypeList[inputLevel]);
417  chain->AddFile(fileName, 1234567890, FairRootManager::GetTreeName());
418  }
419  gFile = temp;
420 
421  // Check if all friend chains have the same runids and the same
422  // number of event numbers as the corresponding input chain
423  // CheckFriendChains();
424 
425  // Add all the friend chains which have been created to the main input chain.
426  for (const auto& mi : fFriendTypeList) {
427  TChain* chain = static_cast<TChain*>(mi.second);
428  fInChain->AddFriend(chain);
429  }
430 
431  // Print some output about the input structure
432  PrintFriendList();
433 }
434 
436 {
437  // Print information about the input structure
438  // List files from the input chain together with all files of
439  // all friend chains
440 
441  LOG(info) << "The input consists out of the following trees and files: ";
442  LOG(info) << " - " << fInChain->GetName();
443  TObjArray* fileElements = fInChain->GetListOfFiles();
444  TIter next(fileElements);
445  TChainElement* chEl = 0;
446  while ((chEl = static_cast<TChainElement*>(next()))) {
447  LOG(info) << " - " << chEl->GetTitle();
448  }
449 
450  for (const auto& mi : fFriendTypeList) {
451  TChain* chain = static_cast<TChain*>(mi.second);
452  LOG(info) << " - " << chain->GetName();
453  fileElements = chain->GetListOfFiles();
454  TIter next1(fileElements);
455  chEl = 0;
456  while ((chEl = static_cast<TChainElement*>(next1()))) {
457  LOG(info) << " - " << chEl->GetTitle();
458  }
459  }
460 }
461 
463 {
464  std::multimap<TString, std::multimap<TString, TArrayI>>::iterator it1;
465  std::multimap<TString, TArrayI> map1;
466 
467  // Get the structure from the input chain
468  it1 = fRunIdInfoAll.find("InputChain");
469  map1 = it1->second;
470  std::vector<Int_t> runid;
471  std::vector<Int_t> events;
472  for (auto& mmi : map1) {
473  TArrayI bla = mmi.second;
474  runid.push_back(bla[0]);
475  events.push_back(bla[1]);
476  }
477 
478  // Now loop over all chains except the input chain and comapare the
479  // runids and event numbers.
480  // If there is a mismatch stop the execution.
481  Int_t errorFlag = 0;
482  TString inputLevel;
483  for (auto level : fInputLevel) {
484  inputLevel = level;
485  if (!inputLevel.Contains("InputChain")) {
486  it1 = fRunIdInfoAll.find(inputLevel);
487  map1 = it1->second;
488  if (runid.size() != map1.size()) {
489  errorFlag = 1;
490  // goto error_label;
491  break;
492  }
493  Int_t counter = 0;
494  for (auto& mmi : map1) {
495  TArrayI bla = mmi.second;
496  if ((bla[0] != runid[counter]) || (bla[1] != events[counter])) {
497  errorFlag = 2;
498  // goto error_label;
499  break;
500  }
501  counter++;
502  }
503  if (errorFlag > 0) {
504  break;
505  }
506  }
507  }
508 
509  // Use goto to leave double loop at once in case of error
510  // error_label:
511  if (errorFlag > 0) {
512  LOG(error) << "The input chain and the friend chain " << inputLevel.Data() << " have a different structure:";
513  if (errorFlag == 1) {
514  LOG(error) << "The input chain has the following runids and event numbers:";
515  for (UInt_t i = 0; i < runid.size(); i++) {
516  LOG(error) << " - Runid " << runid[i] << " with " << events[i] << " events";
517  }
518  LOG(error) << "The " << inputLevel.Data() << " chain has the following runids and event numbers:";
519  for (auto& mmi : map1) {
520  TArrayI bla = mmi.second;
521  LOG(error) << " - Runid " << bla[0] << " with " << bla[1] << " events";
522  }
523  }
524  if (errorFlag == 2) {
525  Int_t counter = 0;
526  for (auto& mmi : map1) {
527  TArrayI bla = mmi.second;
528  LOG(error) << "Runid Input Chain, " << inputLevel.Data() << " chain: " << bla[0] << ", "
529  << runid[counter];
530  LOG(error) << "Event number Input Chain, " << inputLevel.Data() << " chain: " << bla[1] << ", "
531  << events[counter];
532  counter++;
533  }
534  }
535  LOG(fatal) << "Event structure mismatch";
536  }
537 }
538 
539 void FairFileSource::CreateNewFriendChain(TString inputFile, TString inputLevel)
540 {
541  TFile* temp = gFile;
542  TFile* f = TFile::Open(inputFile);
543 
544  TFolder* added = nullptr;
545  TString folderName1 = FairRootManager::GetFolderName();
546  TString folderName = Form("/%s", folderName1.Data());
547  added = dynamic_cast<TFolder*>(f->Get(folderName1));
548  if (!added) {
549  folderName = "/cbmout";
550  folderName1 = "cbmout";
551  added = dynamic_cast<TFolder*>(f->Get("cbmout"));
552  if (!added) {
553  folderName = "/cbmroot";
554  folderName1 = "cbmroot";
555  added = dynamic_cast<TFolder*>(f->Get("cbmroot"));
556  if (!added) {
557  LOG(fatal) << "Could not find folder cbmout nor cbmroot.";
558  exit(-1);
559  }
560  }
561  }
562  folderName1 = folderName1 + "_" + inputLevel;
563  added->SetName(folderName1);
564  fListFolder->Add(added);
565 
567  TList* list = dynamic_cast<TList*>(f->Get("BranchList"));
568  TString chainName = inputLevel;
569  fInputLevel.push_back(chainName);
570  fCheckInputBranches[chainName] = new std::list<TString>;
571  if (list) {
572  TObjString* Obj = 0;
573  for (Int_t i = 0; i < list->GetEntries(); i++) {
574  Obj = dynamic_cast<TObjString*>(list->At(i));
575  fCheckInputBranches[chainName]->push_back(Obj->GetString().Data());
576  FairRootManager::Instance()->AddBranchToList(Obj->GetString().Data());
577  }
578  }
579 
580  TChain* chain = new TChain(inputLevel, folderName);
581  fFriendTypeList[inputLevel] = chain;
582 
583  f->Close();
584  gFile = temp;
585 }
586 
587 Bool_t FairFileSource::CompareBranchList(TFile* fileHandle, TString inputLevel)
588 {
589  // fill a set with the original branch structure
590  // This allows to use functions find and erase
591  std::set<TString> branches;
592  for (auto li : *(fCheckInputBranches[inputLevel])) {
593  branches.insert(li);
594  }
595 
596  // To do so we have to loop over the branches in the file and to compare
597  // the branches in the file with the information stored in
598  // fCheckInputBranches["InputChain"]. If both lists are equal everything
599  // is okay
600 
601  // Get The list of branches from the input file one by one and compare
602  // it to the reference list of branches which is defined for this tree.
603  // If a branch with the same name is found, this branch is removed from
604  // the list. If in the end no branch is left in the list everything is
605  // fine.
606  set<TString>::iterator iter1;
607  TList* list = dynamic_cast<TList*>(fileHandle->Get("BranchList"));
608  if (list) {
609  TObjString* Obj = 0;
610  for (Int_t i = 0; i < list->GetEntries(); i++) {
611  Obj = dynamic_cast<TObjString*>(list->At(i));
612  iter1 = branches.find(Obj->GetString().Data());
613  if (iter1 != branches.end()) {
614  branches.erase(iter1);
615  } else {
616  // Not found is an error because branch structure is
617  // different. It is impossible to add to tree with a
618  // different branch structure
619  return kFALSE;
620  }
621  }
622  }
623  // If the size of branches is !=0 after removing all branches also in the
624  // reference list, this is also a sign that both branch list are not the
625  // same
626  if (branches.size() != 0) {
627  LOG(info) << "Compare Branch List will return kFALSE. The list has " << branches.size() << " branches:";
628  for (auto branchName : branches)
629  LOG(info) << " -> " << branchName;
630  return kFALSE;
631  }
632 
633  return kTRUE;
634 }
635 
636 Bool_t FairFileSource::ActivateObject(TObject** obj, const char* BrName)
637 {
638  if (fInTree) {
639  fInTree->SetBranchStatus(BrName, 1);
640  fInTree->SetBranchAddress(BrName, obj);
641  }
642  if (fInChain) {
643  fInChain->SetBranchStatus(BrName, 1);
644  fInChain->SetBranchAddress(BrName, obj);
645  }
646 
647  return kTRUE;
648 }
649 
650 Bool_t FairFileSource::ActivateObjectAny(void** obj, const std::type_info& info, const char* BrName)
651 {
652  if (fInTree) {
653  return ActivateObjectAnyImpl(fInTree, obj, info, BrName);
654  }
655  if (fInChain) {
656  return ActivateObjectAnyImpl(fInChain, obj, info, BrName);
657  }
658  return kFALSE;
659 }
660 
662 {
663  fRootFile = TFile::Open(name.Data());
664  if (fRootFile->IsZombie()) {
665  LOG(fatal) << "Error opening the Input file";
666  }
667  LOG(info) << "FairFileSource set------------";
668 }
669 
671 {
672  Int_t MaxEventNo = 0;
673  if (EvtEnd != 0) {
674  MaxEventNo = EvtEnd;
675  } else {
676  MaxEventNo = fInChain->GetEntries();
677  }
678 
679  return MaxEventNo;
680 }
681 
683 {
684  fEventMeanTime = mean;
685  /*
686  TString form="(1/";
687  form+= mean;
688  form+=")*exp(-x/";
689  form+=mean;
690  form+=")";
691  fTimeProb= new TF1("TimeProb.", form.Data(), 0., mean*10);
692  */
693  fTimeProb = new TF1("TimeProb", "(1/[0])*exp(-x/[0])", 0., mean * 10);
694  fTimeProb->SetParameter(0, mean);
695  fTimeProb->GetRandom();
696  fEventTimeInMCHeader = kFALSE;
697 }
698 
699 void FairFileSource::SetEventTimeInterval(Double_t min, Double_t max)
700 {
701  fEventTimeMin = min;
702  fEventTimeMax = max;
703  fEventMeanTime = (fEventTimeMin + fEventTimeMax) / 2;
704  fEventTimeInMCHeader = kFALSE;
705 }
706 
707 void FairFileSource::SetBeamTime(Double_t beamTime, Double_t gapTime)
708 {
709  fBeamTime = beamTime;
710  fGapTime = gapTime;
711 }
712 
714 {
715  // Check if the time for the current entry is already set
716  if (fTimeforEntryNo == fCurrentEntryNo)
717  return;
718  LOG(debug) << "Set event time for Entry = " << fTimeforEntryNo << " , where the current entry is "
719  << fCurrentEntryNo << " and eventTime is " << fEventTime;
720  if (fBeamTime < 0) {
721  fEventTime += GetDeltaEventTime();
722  } else {
723  do {
724  fEventTime += GetDeltaEventTime();
725  } while (fmod(fEventTime, fBeamTime + fGapTime) > fBeamTime);
726  }
727  LOG(debug) << "New time = " << fEventTime;
728  fTimeforEntryNo = fCurrentEntryNo;
729 }
730 
732 {
733  Double_t deltaTime = 0;
734  if (fTimeProb != 0) {
735  deltaTime = fTimeProb->GetRandom();
736  LOG(debug) << "Time set via sampling method : " << deltaTime;
737  } else {
738  deltaTime = gRandom->Uniform(fEventTimeMin, fEventTimeMax);
739  LOG(debug) << "Time set via Uniform Random : " << deltaTime;
740  }
741  return deltaTime;
742 }
743 
745 {
746  LOG(debug) << "-- Get Event Time --";
747  if (!fEvtHeaderIsNew && fEvtHeader != 0) {
748  Double_t EvtTime = fEvtHeader->GetEventTime();
749  if (!(EvtTime < 0)) {
750  return EvtTime;
751  }
752  }
753 
754  if (fEventTimeInMCHeader && !fMCHeader) {
755  LOG(debug) << "No MCEventHeader, time is set to 0";
756  return 0;
757  } else if (fEventTimeInMCHeader && fMCHeader) {
758  fEventTime = fMCHeader->GetT();
759  LOG(debug) << "Get event time from MCEventHeader : " << fEventTime << " ns";
760  return fEventTime;
761  } else {
762 
763  if (fTimeforEntryNo != fCurrentEntryNo) {
764  SetEventTime();
765  }
766  LOG(debug) << "Calculate event time from user input : " << fEventTime << " ns";
767  return fEventTime;
768  }
769 }
770 
771 void FairFileSource::ReadBranchEvent(const char* BrName)
772 {
774  if (fEvtHeader == 0) {
775  return;
776  } // No event header, Reading will start later
777  if (fInTree) {
778  fInTree->FindBranch(BrName)->GetEntry(fEvtHeader->GetMCEntryNumber());
779  fEventTime = GetEventTime();
780  return;
781  }
782  if (fInChain) {
783  fInChain->FindBranch(BrName)->GetEntry(fEvtHeader->GetMCEntryNumber());
784  fEventTime = GetEventTime();
785  return;
786  }
787  return;
788 }
789 void FairFileSource::ReadBranchEvent(const char* BrName, Int_t Entry)
790 {
791  fCurrentEntryNo = Entry;
792  if (fInTree) {
793  fInTree->FindBranch(BrName)->GetEntry(Entry);
794  fEventTime = GetEventTime();
795  return;
796  }
797  if (fInChain) {
798  fInChain->FindBranch(BrName)->GetEntry(Entry);
799  fEventTime = GetEventTime();
800  return;
801  }
802  return;
803 }
804 
806 {
807  feh->SetEventTime(GetEventTime());
808  if (fEvtHeader) {
809  feh->SetRunId(fEvtHeader->GetRunId());
810  feh->SetMCEntryNumber(fEvtHeader->GetMCEntryNumber());
811  }
812  if (fMCHeader) {
813  feh->SetRunId(fMCHeader->GetRunID());
814  feh->SetMCEntryNumber(fMCHeader->GetEventID());
815  }
816  feh->SetInputFileId(0);
817  return;
818 }
819 
static char * GetTreeName()
void SetBeamTime(Double_t beamTime, Double_t gapTime)
void SetMCEntryNumber(Int_t id)
void AddFile(TString FileName)
virtual Int_t CheckMaxEventNo(Int_t EvtEnd=0)
list of container factories
Definition: FairRuntimeDb.h:24
UInt_t GetRunID() const
void SetInTree(TTree *tempTree)
Int_t GetMCEntryNumber()
void SetEventTime(Double_t time)
Int_t ReadEvent(UInt_t i=0)
Int_t AddBranchToList(const char *name)
Double_t GetEventTime()
static FairRootManager * Instance()
ClassImp(FairEventBuilder)
const TString FileName
virtual void FillEventHeader(FairEventHeader *feh)
virtual Bool_t ActivateObjectAny(void **, const std::type_info &, const char *)
Double_t GetEventTime()
void SetInputFile(TString name)
void SetInChain(TChain *tempChain, Int_t ident=-1)
void SetTimeBasedBranchNameList(TList *list)
virtual Bool_t ActivateObject(TObject **obj, const char *BrName)
virtual void ReadBranchEvent(const char *BrName)
FairFileSource(TFile *f, const char *Title="InputRootFile", UInt_t identifier=0)
void SetListOfFolders(TObjArray *ta)
void SetInputFileId(Int_t id)
virtual ~FairFileSource()
Double_t GetDeltaEventTime()
void CreateNewFriendChain(TString inputFile, TString inputLevel)
Double_t GetT() const
vertex z [cm]
void AddFriend(TString FileName)
static char * GetFolderName()
Bool_t CompareBranchList(TFile *fileHandle, TString inputLevel)
void SetRunId(UInt_t runid)
void SetEventMeanTime(Double_t mean)
void SetEventTimeInterval(Double_t min, Double_t max)
UInt_t GetEventID() const
run identifier