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