25 #include <TChainElement.h>
27 #include <TCollection.h>
31 #include <TObjArray.h>
32 #include <TObjString.h>
57 , fCheckInputBranches()
62 , fListFolder(new TObjArray(16))
66 , fSourceIdentifier(0)
68 , IsInitialized(kFALSE)
72 , fEventTimeInMCHeader(kTRUE)
73 , fEvtHeaderIsNew(kFALSE)
83 , fCheckFileLayout(kTRUE)
85 if (fRootFile->IsZombie()) {
86 LOG(fatal) <<
"Error opening the Input file";
88 LOG(debug) <<
"FairFileSource created------------";
99 , fCheckInputBranches()
104 , fListFolder(new TObjArray(16))
108 , fSourceIdentifier(0)
110 , IsInitialized(kFALSE)
114 , fEventTimeInMCHeader(kTRUE)
115 , fEvtHeaderIsNew(kFALSE)
117 , fTimeforEntryNo(-1)
125 , fCheckFileLayout(kTRUE)
127 fRootFile = TFile::Open(RootFileName->Data());
128 if (fRootFile->IsZombie()) {
129 LOG(fatal) <<
"Error opening the Input file";
131 LOG(debug) <<
"FairFileSource created------------";
142 , fCheckInputBranches()
147 , fListFolder(new TObjArray(16))
151 , fSourceIdentifier(0)
153 , IsInitialized(kFALSE)
157 , fEventTimeInMCHeader(kTRUE)
158 , fEvtHeaderIsNew(kFALSE)
160 , fTimeforEntryNo(-1)
168 , fCheckFileLayout(kTRUE)
170 fRootFile = TFile::Open(RootFileName.Data());
171 if (fRootFile->IsZombie()) {
172 LOG(fatal) <<
"Error opening the Input file";
174 LOG(debug) <<
"FairFileSource created------------";
182 LOG(info) <<
"FairFileSource already initialized";
187 LOG(debug) <<
"FairFileSource::Init() chain created";
190 fInChain->Add(fRootFile->GetName());
196 fCbmroot =
dynamic_cast<TFolder*
>(fRootFile->Get(
"cbmroot"));
198 fCbmroot =
dynamic_cast<TFolder*
>(fRootFile->Get(
"cbmout"));
219 TList* list =
dynamic_cast<TList*
>(fRootFile->Get(
"BranchList"));
221 LOG(fatal) <<
"No Branch list in input file";
223 TString chainName = fInputTitle;
225 fInputLevel.push_back(chainName);
226 fCheckInputBranches[chainName] =
new std::list<TString>;
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));
233 ObjName = Obj->GetString();
234 LOG(debug) <<
"Branch name " << ObjName.Data();
235 fCheckInputBranches[chainName]->push_back(ObjName.Data());
242 gROOT->GetListOfBrowsables()->Add(fCbmroot);
243 fListFolder->Add(fCbmroot);
254 for (
auto fileName : fInputChainList) {
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";
267 if (fCheckFileLayout) {
271 LOG(fatal) <<
"Branch structure of the input file " << fRootFile->GetName()
272 <<
" and the file to be added " << fileName.Data() <<
" are different.";
280 fInChain->Add(fileName);
286 fNoOfEntries = fInChain->GetEntries();
288 LOG(debug) <<
"Entries in this Source " << fNoOfEntries;
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."));
295 ActivateObject(reinterpret_cast<TObject**>(&fEvtHeader),
"EventHeader.");
298 ActivateObject(reinterpret_cast<TObject**>(&fMCHeader),
"MCEventHeader.");
306 TList* timebasedlist =
dynamic_cast<TList*
>(fRootFile->Get(
"TimeBasedBranchList"));
307 if (timebasedlist == 0) {
308 LOG(warn) <<
"No time based branch list in input file";
320 fRootFile =
static_cast<TFile*
>(tempTree->GetCurrentFile());
322 IsInitialized = kFALSE;
330 if (fInChain->GetEntry(i))
339 if (fInChain->GetEntry(0))
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);
380 Int_t friendType = 1;
382 for (
auto fileName : friendList) {
389 Bool_t inputLevelFound = kFALSE;
391 for (
auto level : fInputLevel) {
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";
404 inputLevelFound = kTRUE;
410 if (!inputLevelFound) {
411 inputLevel = Form(
"FriendTree_%i", friendType);
416 TChain* chain =
static_cast<TChain*
>(fFriendTypeList[inputLevel]);
426 for (
const auto& mi : fFriendTypeList) {
427 TChain* chain =
static_cast<TChain*
>(mi.second);
428 fInChain->AddFriend(chain);
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();
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);
456 while ((chEl = static_cast<TChainElement*>(next1()))) {
457 LOG(info) <<
" - " << chEl->GetTitle();
464 std::multimap<TString, std::multimap<TString, TArrayI>>::iterator it1;
465 std::multimap<TString, TArrayI> map1;
468 it1 = fRunIdInfoAll.find(
"InputChain");
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]);
483 for (
auto level : fInputLevel) {
485 if (!inputLevel.Contains(
"InputChain")) {
486 it1 = fRunIdInfoAll.find(inputLevel);
488 if (runid.size() != map1.size()) {
494 for (
auto& mmi : map1) {
495 TArrayI bla = mmi.second;
496 if ((bla[0] != runid[counter]) || (bla[1] != events[counter])) {
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";
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";
524 if (errorFlag == 2) {
526 for (
auto& mmi : map1) {
527 TArrayI bla = mmi.second;
528 LOG(error) <<
"Runid Input Chain, " << inputLevel.Data() <<
" chain: " << bla[0] <<
", "
530 LOG(error) <<
"Event number Input Chain, " << inputLevel.Data() <<
" chain: " << bla[1] <<
", "
535 LOG(fatal) <<
"Event structure mismatch";
542 TFile* f = TFile::Open(inputFile);
544 TFolder* added =
nullptr;
546 TString folderName = Form(
"/%s", folderName1.Data());
547 added =
dynamic_cast<TFolder*
>(f->Get(folderName1));
549 folderName =
"/cbmout";
550 folderName1 =
"cbmout";
551 added =
dynamic_cast<TFolder*
>(f->Get(
"cbmout"));
553 folderName =
"/cbmroot";
554 folderName1 =
"cbmroot";
555 added =
dynamic_cast<TFolder*
>(f->Get(
"cbmroot"));
557 LOG(fatal) <<
"Could not find folder cbmout nor cbmroot.";
562 folderName1 = folderName1 +
"_" + inputLevel;
563 added->SetName(folderName1);
564 fListFolder->Add(added);
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>;
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());
580 TChain* chain =
new TChain(inputLevel, folderName);
581 fFriendTypeList[inputLevel] = chain;
591 std::set<TString> branches;
592 for (
auto li : *(fCheckInputBranches[inputLevel])) {
606 set<TString>::iterator iter1;
607 TList* list =
dynamic_cast<TList*
>(fileHandle->Get(
"BranchList"));
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);
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;
639 fInTree->SetBranchStatus(BrName, 1);
640 fInTree->SetBranchAddress(BrName, obj);
643 fInChain->SetBranchStatus(BrName, 1);
644 fInChain->SetBranchAddress(BrName, obj);
653 return ActivateObjectAnyImpl(fInTree, obj, info, BrName);
656 return ActivateObjectAnyImpl(fInChain, obj, info, BrName);
663 fRootFile = TFile::Open(name.Data());
664 if (fRootFile->IsZombie()) {
665 LOG(fatal) <<
"Error opening the Input file";
667 LOG(info) <<
"FairFileSource set------------";
672 Int_t MaxEventNo = 0;
676 MaxEventNo = fInChain->GetEntries();
684 fEventMeanTime = mean;
693 fTimeProb =
new TF1(
"TimeProb",
"(1/[0])*exp(-x/[0])", 0., mean * 10);
694 fTimeProb->SetParameter(0, mean);
695 fTimeProb->GetRandom();
696 fEventTimeInMCHeader = kFALSE;
703 fEventMeanTime = (fEventTimeMin + fEventTimeMax) / 2;
704 fEventTimeInMCHeader = kFALSE;
709 fBeamTime = beamTime;
716 if (fTimeforEntryNo == fCurrentEntryNo)
718 LOG(debug) <<
"Set event time for Entry = " << fTimeforEntryNo <<
" , where the current entry is "
719 << fCurrentEntryNo <<
" and eventTime is " << fEventTime;
725 }
while (fmod(fEventTime, fBeamTime + fGapTime) > fBeamTime);
727 LOG(debug) <<
"New time = " << fEventTime;
728 fTimeforEntryNo = fCurrentEntryNo;
733 Double_t deltaTime = 0;
734 if (fTimeProb != 0) {
735 deltaTime = fTimeProb->GetRandom();
736 LOG(debug) <<
"Time set via sampling method : " << deltaTime;
738 deltaTime = gRandom->Uniform(fEventTimeMin, fEventTimeMax);
739 LOG(debug) <<
"Time set via Uniform Random : " << deltaTime;
746 LOG(debug) <<
"-- Get Event Time --";
747 if (!fEvtHeaderIsNew && fEvtHeader != 0) {
749 if (!(EvtTime < 0)) {
754 if (fEventTimeInMCHeader && !fMCHeader) {
755 LOG(debug) <<
"No MCEventHeader, time is set to 0";
757 }
else if (fEventTimeInMCHeader && fMCHeader) {
758 fEventTime = fMCHeader->
GetT();
759 LOG(debug) <<
"Get event time from MCEventHeader : " << fEventTime <<
" ns";
763 if (fTimeforEntryNo != fCurrentEntryNo) {
766 LOG(debug) <<
"Calculate event time from user input : " << fEventTime <<
" ns";
774 if (fEvtHeader == 0) {
791 fCurrentEntryNo = Entry;
793 fInTree->FindBranch(BrName)->GetEntry(Entry);
798 fInChain->FindBranch(BrName)->GetEntry(Entry);
static char * GetTreeName()
void SetBeamTime(Double_t beamTime, Double_t gapTime)
void AddFile(TString FileName)
virtual Int_t CheckMaxEventNo(Int_t EvtEnd=0)
list of container factories
void SetInTree(TTree *tempTree)
Int_t ReadEvent(UInt_t i=0)
Int_t AddBranchToList(const char *name)
static FairRootManager * Instance()
ClassImp(FairEventBuilder)
virtual void FillEventHeader(FairEventHeader *feh)
virtual Bool_t ActivateObjectAny(void **, const std::type_info &, const char *)
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)
virtual ~FairFileSource()
Double_t GetDeltaEventTime()
void CreateNewFriendChain(TString inputFile, TString inputLevel)
void AddFriend(TString FileName)
static char * GetFolderName()
Bool_t CompareBranchList(TFile *fileHandle, TString inputLevel)
void SetEventMeanTime(Double_t mean)
void SetEventTimeInterval(Double_t min, Double_t max)