31 #include <TClonesArray.h>
32 #include <TCollection.h>
35 #include <TGeoManager.h>
36 #include <TIterator.h>
38 #include <TMCAutoLock.h>
40 #include <TObjArray.h>
41 #include <TObjString.h>
42 #include <TRefArray.h>
63 TMCMutex createMutex = TMCMUTEX_INITIALIZER;
64 TMCMutex deleteMutex = TMCMUTEX_INITIALIZER;
67 Int_t FairRootManager::fgCounter = 0;
77 FairRootManager::FairRootManager()
83 , fObj2(new TObject*[1000])
87 , fBranchNameList(new TList())
88 , fMCTrackBranchId(-1)
89 , fTimeBasedBranchNameList(new TList())
92 , fWriteoutBufferMap()
95 , fBranchPerMap(kFALSE)
100 , fFillLastData(kFALSE)
104 , fSourceChain(new TChain(GetTreeName(), Form(
"/%s", GetFolderName())))
108 , fUseFairLinks(kFALSE)
110 , fListOfBranchesFromInput(0)
111 , fListOfBranchesFromInputIter(0)
112 , fListOfNonTimebasedBranches(new TRefArray())
113 , fListOfNonTimebasedBranchesIter(0)
116 LOG(debug) <<
"FairRootManager::FairRootManager: going to lock " <<
this;
118 TMCAutoLock lk(&createMutex);
128 LOG(debug) <<
"Released lock and done FairRootManager::FairRootManager in " << fId <<
" " <<
this;
134 LOG(debug) <<
"Enter Destructor of FairRootManager";
137 fBranchNameList->Delete();
138 delete fBranchNameList;
139 LOG(debug) <<
"Leave Destructor of FairRootManager";
148 TMCAutoLock lk(&deleteMutex);
150 LOG(debug) <<
"FairRootManager::~FairRootManager: going to lock " << fId <<
" " <<
this;
157 LOG(debug) <<
"Released lock and done FairRootManager::~FairRootManager in " << fId <<
" " <<
this;
163 LOG(debug) <<
"Call the initialiazer for the FairSource in FairRootManager ";
165 Bool_t sourceInitBool = fSource->
Init();
166 fListOfBranchesFromInput = fSourceChain->GetListOfBranches();
168 if (fListOfBranchesFromInput) {
169 fListOfBranchesFromInputIter = fListOfBranchesFromInput->MakeIterator();
170 while ((obj = fListOfBranchesFromInputIter->Next())) {
171 if ((fTimeBasedBranchNameList->FindObject(obj->GetName())) == 0)
172 fListOfNonTimebasedBranches->Add(obj);
175 LOG(debug) <<
"Source is intialized and the list of branches is created in FairRootManager ";
176 fListOfNonTimebasedBranchesIter = fListOfNonTimebasedBranches->MakeIterator();
177 return sourceInitBool;
191 void FairRootManager::RegisterImpl(
const char* name,
const char* folderName, T* obj, Bool_t toFile)
200 if (strcmp(name, folderName) == 0) {
201 LOG(fatal) <<
"The names for the object name " << name <<
" and the folder name " << folderName
202 <<
" are equal. This isn't allowed. So we stop the execution at this point. Pleae change either the "
203 "name or the folder name.";
211 LOG(fatal) <<
"The sink does not exist to store persistent branches.";
214 AddMemoryBranch(name, obj);
217 if (toFile == kFALSE) {
224 RegisterImpl(name, folderName, obj, toFile);
229 if (fBranchNameList->FindObject(name) == 0) {
230 fBranchNameList->AddLast(
new TObjString(name));
232 if (strcmp(name,
"MCTrack") == 0) {
233 fMCTrackBranchId = fBranchSeqId;
242 RegisterImpl(name, foldername, obj, toFile);
247 AddMemoryBranch(name, obj);
255 TClonesArray* outputArray;
256 if (fActiveContainer.find(branchName) == fActiveContainer.end()) {
257 fActiveContainer[branchName] =
new TClonesArray(className);
258 outputArray = fActiveContainer[branchName];
259 Register(branchName, folderName, outputArray, toFile);
262 return fActiveContainer[branchName];
267 if (fActiveContainer.find(branchName)
268 != fActiveContainer.end()) {
269 if (fActiveContainer[branchName] == 0) {
270 std::cout <<
"-E- FairRootManager::GetEmptyTClonesArray: Container deleted outside FairRootManager!"
273 fActiveContainer[branchName]->Delete();
275 return fActiveContainer[branchName];
277 std::cout <<
"-E- Branch: " << branchName <<
" not registered!"
285 if (fActiveContainer.find(branchName) != fActiveContainer.end()) {
286 return fActiveContainer[branchName];
288 LOG(info) <<
"Branch: " << branchName.Data() <<
" not registered!";
297 if (
id < fBranchSeqId) {
298 TObjString* ObjStr =
static_cast<TObjString*
>(fBranchNameList->At(
id));
299 return ObjStr->GetString();
301 TString NotFound(
"Branch not found");
311 for (Int_t t = 0; t < fBranchNameList->GetEntries(); t++) {
312 ObjStr =
static_cast<TObjString*
>(fBranchNameList->TList::At(t));
313 if (BrName == ObjStr->GetString()) {
328 if (fTSBufferMap[branchName] == 0) {
331 fTSBufferMap[branchName]->SetStopFunction(
function);
332 return fTSBufferMap[branchName]->GetData(parameter);
337 Double_t startParameter,
339 Double_t stopParameter)
341 if (fTSBufferMap[branchName] == 0) {
344 fTSBufferMap[branchName]->SetStopFunction(stopFunction);
345 fTSBufferMap[branchName]->SetStartFunction(startFunction);
346 return fTSBufferMap[branchName]->GetData(startParameter, stopParameter);
351 if (fTSBufferMap.count(branchName) > 0) {
352 fTSBufferMap[branchName]->Terminate();
358 for (
auto& mi : fTSBufferMap) {
359 mi.second->Terminate();
365 for (
auto& mi : fTSBufferMap) {
366 if (mi.second->AllDataProcessed() == kFALSE && mi.second->TimeOut() == kFALSE) {
392 LOG(debug) <<
"FairRootManager::Write " <<
this;
395 return (fSink->
Write());
414 TFile* oldfile = gFile;
415 TFile* file = TFile::Open(geofile,
"RECREATE");
417 gGeoManager->Write();
427 fSink->
WriteObject(fBranchNameList,
"BranchList", TObject::kSingleKey);
428 fSink->
WriteObject(fTimeBasedBranchNameList,
"TimeBasedBranchList", TObject::kSingleKey);
434 LOG(fatal) <<
"No Source available";
439 LOG(info) <<
"---FairRootManager::SpecifyRunId --- ";
452 LOG(fatal) <<
"No Source available";
458 Int_t readEventResult = fSource->
ReadEvent(i);
463 LOG(debug) <<
"--Event number --- " << fCurrentEntryNo <<
" with t0 time ---- " << fCurrentTime;
465 return readEventResult;
502 fListOfNonTimebasedBranchesIter->Reset();
503 while ((Obj = fListOfNonTimebasedBranchesIter->Next())) {
516 Bool_t readentry = kFALSE;
523 fReqBrNames.emplace_back(BrName);
525 TObject* Obj =
nullptr;
526 LOG(debug2) <<
" Try to find if the object " << BrName <<
" is already activated by another task or call";
529 Obj = fOutFolder->FindObjectAny(BrName);
531 LOG(debug2) <<
"Object " << BrName <<
" was already activated by another task";
536 LOG(debug2) <<
"Try to find if the object " << BrName <<
" is a memory branch";
537 Obj = GetMemoryBranch(BrName);
539 LOG(debug2) <<
"Object " << BrName <<
" is a memory branch";
543 if (fRootFolder && !Obj) {
545 LOG(debug2) <<
"Object " << BrName
546 <<
" is not a memory branch and not yet activated, try the Input Tree (Chain)";
547 Obj = fRootFolder->FindObjectAny(BrName);
548 Obj = ActivateBranch(BrName);
551 Obj = ActivateBranch(BrName);
577 }
else if (fileId == 0) {
591 TBranch* dataBranch = 0;
595 if (fileId < 0 && fInputBranchMap[type] != 0) {
596 dataBranch = fInputBranchMap[type];
597 }
else if (fileId < 0) {
598 fInputBranchMap[type] = dataTree->GetBranch(
GetBranchName(type));
599 dataBranch = fInputBranchMap[type];
604 if (dataBranch == 0) {
609 if (entryNr < dataBranch->GetEntries()) {
610 dataBranch->GetEntry(entryNr);
627 if (index < dataArray->GetEntriesFast()) {
630 result = dataArray->At(index)->Clone();
635 dataBranch->GetEntry(oldEntryNr);
642 TClonesArray* result = 0;
659 }
else if (fileId == 0) {
673 TBranch* dataBranch = 0;
677 if (fileId < 0 && fInputBranchMap[type] != 0) {
678 dataBranch = fInputBranchMap[type];
679 }
else if (fileId < 0) {
680 fInputBranchMap[type] = dataTree->GetBranch(
GetBranchName(type));
681 dataBranch = fInputBranchMap[type];
686 if (dataBranch == 0) {
691 if (entryNr < dataBranch->GetEntries()) {
692 dataBranch->GetEntry(entryNr);
708 dataBranch->GetEntry(oldEntryNr);
716 if (!fBranchPerMap) {
718 return CheckBranchSt(BrName);
720 fBrPerMapIter = fBrPerMap.find(BrName);
721 if (fBrPerMapIter != fBrPerMap.end()) {
722 return fBrPerMapIter->second;
734 fBranchNameList->Clear();
735 fMCTrackBranchId = -1;
736 for (Int_t t = 0; t < list->GetEntries(); t++) {
737 AddBranchToList(static_cast<TObjString*>(list->At(t))->GetString().Data());
744 fSourceChain = tempChain;
746 fSignalChainList[ident] = tempChain;
753 for (Int_t iobj = 0; iobj <= fNObj; iobj++) {
755 LOG(info) <<
"FairRootManager::UpdateBranches \"" << fObj2[iobj]->GetName() <<
"\" (\""
756 << fObj2[iobj]->GetTitle() <<
"\")";
757 TString tempBranchName = fObj2[iobj]->GetName();
765 TObject* FairRootManager::ActivateBranch(
const char* BrName)
774 fObj2[fNObj] = GetMemoryBranch(BrName);
779 LOG(debug) <<
"Try to find an object " << BrName <<
" describing the branch in the folder structure in file";
781 for (Int_t i = 0; i < fListFolder->GetEntriesFast(); i++) {
782 TFolder* fold =
static_cast<TFolder*
>(fListFolder->At(i));
783 fObj2[fNObj] = fold->FindObjectAny(BrName);
785 LOG(info) <<
"Object " << BrName <<
" describing the branch in the folder structure was found";
795 LOG(info) <<
" Branch: " << BrName <<
" not found in Tree.";
803 AddMemoryBranch(BrName, fObj2[fNObj]);
807 void FairRootManager::AddMemoryBranch(
const char* fName, TObject* pObj)
810 map<TString, TObject*>::iterator p;
811 TString BrName = fName;
812 p = fMap.find(BrName);
813 if (p != fMap.end()) {
815 fMap.insert(pair<TString, TObject*>(BrName, pObj));
819 Int_t FairRootManager::CheckBranchSt(
const char* BrName)
822 Int_t returnvalue = 0;
823 TObject* Obj1 =
nullptr;
825 if (fListFolder == 0) {
826 fListFolder =
new TObjArray(16);
831 fListFolder->Add(fRootFolder);
832 Obj1 = fRootFolder->FindObjectAny(BrName);
834 if (fOutFolder && !Obj1) {
835 fListFolder->Add(fOutFolder);
836 Obj1 = fOutFolder->FindObjectAny(BrName);
839 for (Int_t i = 0; i < fListFolder->GetEntriesFast(); i++) {
841 TFolder* fold =
dynamic_cast<TFolder*
>(fListFolder->At(i));
843 Obj1 = fold->FindObjectAny(BrName);
850 TObject* Obj2 =
nullptr;
851 Obj2 = GetMemoryBranch(BrName);
854 }
else if (Obj2 != 0) {
867 void FairRootManager::CreatePerMap()
870 fBranchPerMap = kTRUE;
871 for (Int_t i = 0; i < fBranchSeqId; i++) {
872 TObjString* name =
static_cast<TObjString*
>(fBranchNameList->At(i));
874 TString BrName = name->GetString();
875 fBrPerMap.insert(pair<TString, Int_t>(BrName, CheckBranchSt(BrName.Data())));
879 TObject* FairRootManager::GetMemoryBranch(
const char* fName)
883 TString BrName = fName;
884 map<TString, TObject*>::iterator p;
885 p = fMap.find(BrName);
887 if (p != fMap.end()) {
897 fSink->
WriteObject(f,
"FileHeader", TObject::kSingleKey);
909 if (fWriteoutBufferMap[branchName] == 0) {
910 fWriteoutBufferMap[branchName] = buffer;
912 LOG(warn) <<
"Branch " << branchName.Data() <<
" is already registered in WriteoutBufferMap";
916 return fWriteoutBufferMap[branchName];
925 for (
auto& mi : fWriteoutBufferMap) {
926 if (mi.second->IsBufferingActivated())
927 fTimeBasedBranchNameList->AddLast(
new TObjString(mi.first.Data()));
938 fTimeBasedBranchNameList->Delete();
939 delete fTimeBasedBranchNameList;
940 fTimeBasedBranchNameList = list;
946 if (fWriteoutBufferMap.count(branchName) > 0) {
947 return fWriteoutBufferMap[branchName];
955 for (
auto& mi : fWriteoutBufferMap) {
956 mi.second->WriteOutData(eventTime);
962 Bool_t dataInBuffer = kFALSE;
963 for (
auto& mi : fWriteoutBufferMap) {
964 if (mi.second->GetNData() > 0) {
965 dataInBuffer = kTRUE;
967 mi.second->WriteOutAllData();
969 fFillLastData = dataInBuffer;
974 for (
auto& mi : fWriteoutBufferMap) {
975 mi.second->DeleteOldData();
981 char* default_name = (
char*)
"cbmsim";
982 char* workdir = getenv(
"VMCWORKDIR");
983 if (
nullptr == workdir) {
988 FILE* file = fopen(Form(
"%s/config/rootmanager.dat", workdir),
"r");
990 if (
nullptr == file) {
995 while (
nullptr != fgets(str, 100, file)) {
996 if (TString(str).Contains(
"treename")) {
997 char* treename =
new char[100];
998 if (1 == sscanf(str,
"treename=%s", treename)) {
1008 return default_name;
1013 char* default_name = (
char*)
"cbmout";
1015 default_name = (
char*)
"cbmroot";
1017 char* workdir = getenv(
"VMCWORKDIR");
1018 if (
nullptr == workdir) {
1019 return default_name;
1023 FILE* file = fopen(Form(
"%s/config/rootmanager.dat", workdir),
"r");
1025 if (
nullptr == file) {
1026 return default_name;
1030 while (
nullptr != fgets(str, 100, file)) {
1031 if (TString(str).Contains(
"foldername")) {
1032 char* foldername =
new char[100];
1033 if (1 == sscanf(str,
"foldername=%s", foldername)) {
1037 delete[] foldername;
1043 return default_name;
1046 void FairRootManager::EmitMemoryBranchWrongTypeWarning(
const char* brname,
const char* type1,
const char* type2)
const
1048 LOG(warn) <<
"Trying to read from memory branch " << brname <<
" with wrong type " << type1
1049 <<
" (expexted: " << type2 <<
" )";
1056 fileName.Insert(fileName.Index(
".root"), tid);
1061 LOG(
WARNING) <<
"FairRootManager::GetOutFile() deprecated. Use separate file to store additional data.";
1063 assert(sink->GetSinkType() ==
kFILESINK);
1070 LOG(
WARNING) <<
"FairRootManager::GetOutTree() deprecated. Use separate file to store additional data.";
1072 assert(sink->GetSinkType() ==
kFILESINK);
static char * GetTreeName()
void CreateGeometryFile(const char *geofile)
virtual Int_t ReadEvent(UInt_t=0)=0
Int_t CheckMaxEventNo(Int_t EvtEnd=0)
void SetBranchNameList(TList *list)
TObject * GetCloneOfLinkData(const FairLink link)
virtual Int_t Write(const char *name=0, Int_t option=0, Int_t bufsize=0)=0
Int_t ReadEvent(Int_t i=0)
virtual void FillEventHeader(FairEventHeader *feh)
TClonesArray * GetCloneOfTClonesArray(const FairLink link)
FairWriteoutBuffer * RegisterWriteoutBuffer(TString branchName, FairWriteoutBuffer *buffer)
virtual void ReadBranchEvent(const char *)
Int_t AddBranchToList(const char *name)
static FairRun * Instance()
void InitTSBuffer(TString branchName, BinaryFunctor *function)
static FairMonitor * GetMonitor()
void TerminateAllTSBuffer()
void WriteFileHeader(FairFileHeader *f)
Bool_t AllDataProcessed()
void SetEntryNr(Int_t val)
static FairRootManager * Instance()
ClassImp(FairEventBuilder)
void RecordGetting(const char *name)
virtual void WriteFolder()=0
TObject * GetObject(const char *BrName)
Int_t ReadNonTimeBasedEventFromBranches(Int_t i=0)
TClonesArray * GetData(TString branchName, BinaryFunctor *function, Double_t parameter)
void RecordRegister(const char *name, const char *folderName, Bool_t toFile)
Int_t Write(const char *name=0, Int_t option=0, Int_t bufsize=0)
void SetInChain(TChain *tempChain, Int_t ident=-1)
TClonesArray * GetTClonesArray(TString branchName)
virtual Bool_t SpecifyRunId()=0
void SetTimeBasedBranchNameList(TList *list)
TChain * GetSignalChainNo(UInt_t i)
static FairLinkManager * Instance()
void StoreAllWriteoutBufferData()
virtual Int_t CheckMaxEventNo(Int_t=0)
virtual void AddIgnoreType(Int_t type)
void RegisterInputObject(const char *name, TObject *obj)
Int_t GetInstanceId() const
TString GetBranchName(Int_t id)
void TerminateTSBuffer(TString branchName)
virtual Bool_t InitSink()=0
void Register(const char *name, const char *Foldername, TNamed *obj, Bool_t toFile)
TClonesArray * GetEmptyTClonesArray(TString branchName)
Bool_t ReadNextEvent(Double_t dt)
static char * GetFolderName()
virtual void RegisterImpl(const char *, const char *, void *)=0
void UpdateFileName(TString &fileName)
void UpdateListOfTimebasedBranches()
virtual void WriteGeometry()=0
Int_t CheckBranch(const char *BrName)
void DeleteOldWriteoutBufferData()
virtual void WriteObject(TObject *f, const char *, Int_t option=0)=0
Base class for all functors which are used in the FairTSBufferFunctional.
virtual Bool_t ActivateObject(TObject **, const char *)
A container class to store digi data during events.
void StoreWriteoutBufferData(Double_t eventTime)
void ReadBranchEvent(const char *BrName)
A class to access time ordered data in a root branch.
Int_t GetBranchId(TString const &BrName)
virtual ~FairRootManager()