EIC Software
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
FairRootManager.cxx
Go to the documentation of this file. Or view the newest version in sPHENIX GitHub for file FairRootManager.cxx
1 // -------------------------------------------------------------------------
2 // ----- FairRootManager source file -----
3 // ----- Created 06/01/04 by M. Al-Turany/D. Bertini -----
4 // -------------------------------------------------------------------------
5 
6 // Class FairRootManager
7 // ------------------
8 // Class that takes care of Root IO.
9 #include "FairRootManager.h"
10 #include "FairDetector.h"
11 #include "FairRunAna.h"
12 #include "FairRun.h"
13 #include "FairMCApplication.h"
14 #include "FairGeoNode.h"
15 #include "FairTSBufferFunctional.h"
16 #include "FairLogger.h"
17 #include "FairMCEventHeader.h"
18 #include "FairEventHeader.h"
19 #include "FairFileHeader.h"
20 #include "FairEventHeader.h"
21 #include "FairWriteoutBuffer.h"
22 #include "FairLink.h"
23 #include "TFriendElement.h"
24 #include "TObjArray.h"
25 #include "TFolder.h"
26 #include "TCollection.h"
27 #include "TParticle.h"
28 #include "TList.h"
29 #include "TGeoManager.h"
30 #include "TROOT.h"
31 #include "TClonesArray.h"
32 #include "TList.h"
33 #include "TChainElement.h"
34 #include "TPRegexp.h"
35 #include "TArray.h"
36 #include "TF1.h"
37 #include "TRandom.h"
38 
39 #include <iostream>
40 #include <map>
41 #include <list>
42 #include <set>
43 #include <vector>
44 
45 using std::flush;
46 using std::cout;
47 using std::endl;
48 using std::map;
49 using std::list;
50 using std::pair;
51 using std::set;
52 
54 //_____________________________________________________________________________
56 {
57 // Returns singleton instance.
58 // ---
59  return fgInstance;
60 }
61 //_____________________________________________________________________________
62 
63 //_____________________________________________________________________________
65  : TObject(),
66  fOldEntryNr(-1),
67  fCbmout(NULL),
68  fCbmroot(NULL),
69  fCurrentTime(0),
70  fInFile(NULL),
71  fInChain( new TChain("cbmsim", "/cbmroot")),
72  fInTree(NULL),
73  fOutFile(NULL),
74  fOutTree(NULL),
75  fListFolder(0),
76  fObj2(new TObject*[1000]),
77  fNObj(-1),
78  fMap(),
79  fPtrTree(NULL),
80  fCurrentEntries(0),
81  fBranchSeqId(0),
82  fBranchNameList(new TList()),
83  fDataContainer(),
84  fActiveContainer(),
85  fTSBufferMap(),
86  fWriteoutBufferMap(),
87  fInputBranchMap(),
88  fCompressData(kFALSE),
89  fTimeStamps(kFALSE),
90  fBranchPerMap(kFALSE),
91  fBrPerMap(),
92  fBrPerMapIter(),
93  fFriendFileList(),
94  fInputFileName(""),
95  fInputChainList(),
96  fFriendTypeList(),
97  fCheckInputBranches(),
98  fInputLevel(),
99  fRunIdInfoAll(),
100  fLogger(FairLogger::GetLogger()),
101  fMixedInput(kFALSE),
102  fActualSignalIdentifier(0),
103  fNoOfSignals(0),
104  fSignalChainList(NULL),
105  fBackgroundChain(NULL),
106  fBackgroundFile(NULL),
107  fSignalTypeList(),
108  fEventTimeMin(0.),
109  fEventTimeMax(0.),
110  fEventTime(0.),
111  fEventMeanTime(0.),
112  fTimeProb(0),
113  fMCHeader(0),
114  fEvtHeader(0),
115  fFileHeader(0),
116  fSignalBGN(),
117  fEventTimeInMCHeader(kTRUE),
118  fSBRatiobyN(kFALSE),
119  fSBRatiobyT(kFALSE),
120  fCurrentEntryNo(0),
121  fTimeforEntryNo(0),
122  fNoOfBGEntries(0),
123  fCurrentEntry(),
124  fEvtHeaderIsNew(kFALSE),
125  fFillLastData(kFALSE),
126  fEntryNr(0)
127 {
128  if (fgInstance) {
129  Fatal("FairRootManager", "Singleton instance already exists.");
130  return;
131  }
132  fgInstance = this;
133 }
134 //_____________________________________________________________________________
135 
136 //_____________________________________________________________________________
138 {
139 //
140  fLogger->Debug(MESSAGE_ORIGIN,"Enter Destructor of FairRootManager");
141 // if(fCbmout) delete fCbmout;
142 // if(fCbmroot) delete fCbmroot;
143  if(fInFile) {
144  delete fInFile;
145  }
146  if(fInChain) {
147  delete fInChain;
148  }
149  if(fOutTree) {
150  delete fOutTree;
151  }
152  if(fOutFile) {
153  fOutFile->cd();
154  delete fOutFile;
155  }
156  // fObj2->Delete();
157  delete fObj2;
158 // if(fPtrTree)delete fPtrTree;
159  fBranchNameList->Delete();
160  delete fBranchNameList;
161  fgInstance = 0;
162  fLogger->Debug(MESSAGE_ORIGIN, "Leave Destructor of FairRootManager");
163 }
164 //_____________________________________________________________________________
165 
166 //_____________________________________________________________________________
167 void FairRootManager::SetSignalFile(TString name, UInt_t identifier )
168 {
169  TFile* SignalInFile = new TFile(name.Data());
170  if (SignalInFile->IsZombie()) {
171  fLogger->Fatal(MESSAGE_ORIGIN, "Error opening the Signal file");
172  } else {
174  if(fSignalTypeList[identifier]==0) {
175  TChain* chain = new TChain("cbmsim", "/cbmroot");
176  fSignalTypeList[identifier]=chain;
177  fCurrentEntry[identifier]= 0;
178  fNoOfSignals++;
179  fActualSignalIdentifier= identifier;
180  chain->AddFile(name.Data());
181  fFileHeader->AddInputFile(SignalInFile, identifier, 0); //First file in the Chain
182  } else {
183  TChain* CurrentChain=fSignalTypeList[identifier];
184  CurrentChain->AddFile(name.Data());
185  TObjArray* fileElements=CurrentChain->GetListOfFiles();
186  fFileHeader->AddInputFile(SignalInFile, identifier, fileElements->GetEntries());
187  }
188  fMixedInput=kTRUE;
189 
190  }
191 }
192 //_____________________________________________________________________________
193 
194 //_____________________________________________________________________________
195 void FairRootManager::AddSignalFile(TString name, UInt_t identifier )
196 {
197  SetSignalFile(name, identifier);
198 }
199 //_____________________________________________________________________________
200 
201 //_____________________________________________________________________________
203 {
204  if(i<<fNoOfSignals) {
205  return fSignalTypeList[i];
206  } else {
207  fLogger->Info(MESSAGE_ORIGIN, "Error signal identifier %i does not exist ", i);
208  return 0;
209  }
210 }
211 //_____________________________________________________________________________
212 
213 //_____________________________________________________________________________
215 {
216  if (name.IsNull() ) {
217  fLogger->Info(MESSAGE_ORIGIN, "No background file defined.");
218  }
219  fBackgroundFile = new TFile(name);
220  if (fBackgroundFile->IsZombie()) {
221  fLogger->Fatal(MESSAGE_ORIGIN, "Error opening the Background file %s ", name.Data());
222  } else {
223  fBackgroundChain = new TChain("cbmsim", "/cbmroot");
224  fBackgroundChain->AddFile(name.Data());
226  }
227 }
228 //_____________________________________________________________________________
229 
230 //_____________________________________________________________________________
232 {
233  if (name.IsNull() ) {
234  fLogger->Info(MESSAGE_ORIGIN, "No background file defined.");
235  }
236  TFile* BGFile = new TFile(name);
237  if (BGFile->IsZombie()) {
238  fLogger->Fatal(MESSAGE_ORIGIN, "Error opening the Background file %s ", name.Data());
239  } else {
240  if(fBackgroundChain!=0) {
241  fBackgroundChain->AddFile(name.Data());
242  TObjArray* fileElements=fBackgroundChain->GetListOfFiles();
243  fFileHeader->AddInputFile(BGFile,0, fileElements->GetEntries());
244  } else {
245  fLogger->Fatal(MESSAGE_ORIGIN, "Use SetBackGroundFile first, then add files to background");
246  }
247 
248  }
249 
250 }
251 //_____________________________________________________________________________
252 
253 //_____________________________________________________________________________
255 {
256  if(!fMixedInput) {
258  }
259 }
260 //_____________________________________________________________________________
261 
262 //_____________________________________________________________________________
264 {
265  // Get the folder structure from file which describes the input tree.
266  // There are two different names possible, so check both.
267  fCbmroot= dynamic_cast <TFolder*> (fBackgroundFile->Get("cbmroot"));
268  if(!fCbmroot) {
269  fCbmroot= dynamic_cast <TFolder*> (fBackgroundFile->Get("cbmout"));
270  if(!fCbmroot) {
271  fCbmroot= gROOT->GetRootFolder()->AddFolder("cbmroot", "Main Folder");
272  } else {
273  fCbmroot->SetName("cbmroot");
274  }
275  }
276 
277  // Get The list of branches from the input file and add it to the
278  // actual list of existing branches.
279  // Add this list of branches also to the map of input trees, which
280  // stores the information which branches belong to which input tree.
281  // There is at least one primary input tree, but there can be many
282  // additional friend trees.
283  // This information is needed to add new files to the correct friend
284  // tree. With this information it is also possible to check if the
285  // input files which are added to the input chain all have the same
286  // branch structure. Without this check it is possible to add trees
287  // with a different branch structure but the same tree name. ROOT
288  // probably only checks if the name of the tree is the same.
289 
290  TList* list= dynamic_cast <TList*> (fBackgroundFile->Get("BranchList"));
291  TString chainName = "BGInChain";
292  fInputLevel.push_back(chainName);
293  fCheckInputBranches[chainName] = new std::list<TString>;
294  if(list) {
295  TObjString* Obj=0;
296  for(Int_t i =0; i< list->GetEntries(); i++) {
297  Obj=dynamic_cast <TObjString*> (list->At(i));
298  fCheckInputBranches[chainName]->push_back(Obj->GetString().Data());
299  if(fBranchNameList->FindObject(Obj->GetString().Data())==0) {
300  fBranchNameList->AddLast(Obj);
301  fBranchSeqId++;
302  }
303  }
304  }
305 
306  gROOT->GetListOfBrowsables()->Add(fCbmroot);
307  fListFolder.Add( fCbmroot );
308  return kTRUE;
309 
310 }
311 //_____________________________________________________________________________
312 
313 //_____________________________________________________________________________
315 {
316  std::map<UInt_t, TChain*>::const_iterator iter;
317  for(iter = fSignalTypeList.begin(); iter != fSignalTypeList.end(); iter++) {
318  TChain* currentChain=iter->second;
319  // cout << "Signal chain is : " << currentChain->GetName()<< endl;
320  // currentChain->Dump();
321  TFile* ChainFirstFile = currentChain->GetFile();
322  //cout << "First file in signal chain is : " << ChainFirstFile << endl;
323  // Check if the branchlist is the same as for the first input file.
324  Bool_t isOk = CompareBranchList(ChainFirstFile,"BGInChain");
325  if ( !isOk ) {
326  fLogger->Fatal(MESSAGE_ORIGIN, "Branch structure of the signal chain is different than the back ground one");
327  }
328  }
329  return kTRUE;
330 }
331 //_____________________________________________________________________________
332 
333 //_____________________________________________________________________________
335 {
336  if(fMixedInput) {
337  return OpenBackgroundChain();
338  }
339  if ( fInputFileName.IsNull() ) {
340  fLogger->Info(MESSAGE_ORIGIN, "No input file defined.");
341  return kFALSE;
342  }
343 
344  // Temporarily open the input file to extract information which
345  // is needed to bring the friend trees in the correct order
346  fInFile = new TFile(fInputFileName);
347  if (fInFile->IsZombie()) {
348  fLogger->Fatal(MESSAGE_ORIGIN, "Error opening the Input file");
349  }
350 
351  if (!fInChain ) {
352  fInChain = new TChain("cbmsim", "/cbmroot");
353  }
354  fInChain->Add( fInFile->GetName() );
355 
356 
357 
358  // Get the folder structure from file which describes the input tree.
359  // There are two different names possible, so check both.
360  fCbmroot= dynamic_cast <TFolder*> (fInFile->Get("cbmroot"));
361  if(!fCbmroot) {
362  fCbmroot= dynamic_cast <TFolder*> (fInFile->Get("cbmout"));
363  if(!fCbmroot) {
364  fCbmroot= gROOT->GetRootFolder()->AddFolder("cbmroot", "Main Folder");
365  } else {
366  fCbmroot->SetName("cbmroot");
367  }
368  }
369 
370 
371 
372  // Get The list of branches from the input file and add it to the
373  // actual list of existing branches.
374  // Add this list of branches also to the map of input trees, which
375  // stores the information which branches belong to which input tree.
376  // There is at least one primary input tree, but there can be many
377  // additional friend trees.
378  // This information is needed to add new files to the correct friend
379  // tree. With this information it is also possible to check if the
380  // input files which are added to the input chain all have the same
381  // branch structure. Without this check it is possible to add trees
382  // with a different branch structure but the same tree name. ROOT
383  // probably only checks if the name of the tree is the same.
384  TList* list= dynamic_cast <TList*> (fInFile->Get("BranchList"));
385  TString chainName = "InputChain";
386  fInputLevel.push_back(chainName);
387  fCheckInputBranches[chainName] = new std::list<TString>;
388  if(list) {
389  TObjString* Obj=0;
390  for(Int_t i =0; i< list->GetEntries(); i++) {
391  Obj=dynamic_cast <TObjString*> (list->At(i));
392  fCheckInputBranches[chainName]->push_back(Obj->GetString().Data());
393  if(fBranchNameList->FindObject(Obj->GetString().Data())==0) {
394  fBranchNameList->AddLast(Obj);
395  fBranchSeqId++;
396  }
397  }
398  }
399 
400  gROOT->GetListOfBrowsables()->Add(fCbmroot);
401  fListFolder.Add( fCbmroot );
402 
403  // Store the information about the unique runids in the input file
404  // together with the filename and the number of events for each runid
405  // this information is needed later to check if inconsitencies exist
406  // between the main input chain and any of the friend chains.
407 
408  // GetRunIdInfo(fInFile->GetName(), chainName);
409 
410  // Add all additional input files to the input chain and do a
411  // consitency check
412  std::list<TString>::const_iterator iter;
413  for(iter = fInputChainList.begin(); iter != fInputChainList.end(); iter++) {
414  // Store global gFile pointer for safety reasons.
415  // Set gFile to old value at the end of the routine.R
416  TFile* temp = gFile;
417 
418  // Temporarily open the input file to extract information which
419  // is needed to bring the friend trees in the correct order
420  TFile* inputFile = new TFile((*iter));
421  if (inputFile->IsZombie()) {
422  fLogger->Fatal(MESSAGE_ORIGIN, "Error opening the file %s which should be added to the input chain or as friend chain", (*iter).Data());
423  }
424 
425  // Check if the branchlist is the same as for the first input file.
426  Bool_t isOk = CompareBranchList(inputFile, chainName);
427  if ( !isOk ) {
428  fLogger->Fatal(MESSAGE_ORIGIN, "Branch structure of the input file %s and the file to be added %s are different.", fInputFileName.Data(), (*iter).Data());
429  }
430 
431  // Add the runid information for all files in the chain.
432  //GetRunIdInfo(inputFile->GetName(), chainName);
433  // Add the file to the input chain
434  fInChain->Add( (*iter) );
435 
436  // Close the temporarly file and restore the gFile pointer.
437  inputFile->Close();
438  gFile = temp;
439 
440  }
441 
442 // fCurrentEntries = (Int_t) fInChain->GetTree()->GetEntries();
443 // cout<<"Input Chain has "<<fCurrentEntries<<" entries"<<endl;
444 
445  return kTRUE;
446 }
447 //_____________________________________________________________________________
448 
449 //_____________________________________________________________________________
451 {
452  cout << "-I- FairRootManager::OpenInTree() Opening fInTree: \"" << fInTree->GetCurrentFile()->GetName() << "\"" << endl;
453  // Temporarily open the input file to extract information which
454  // is needed to bring the friend trees in the correct order
455  fInputFileName = fInTree->GetCurrentFile()->GetName();
456  fInFile = fInTree->GetCurrentFile();
457  if (fInFile->IsZombie()) {
458  fLogger->Fatal(MESSAGE_ORIGIN, "Error opening the Input file");
459  }
460 
461  // Get the folder structure from file which describes the input tree.
462  // There are two different names possible, so check both.
463  fCbmroot= dynamic_cast <TFolder*> (fInFile->Get("cbmroot"));
464  if(!fCbmroot) {
465  fCbmroot= dynamic_cast <TFolder*> (fInFile->Get("cbmout"));
466  if(!fCbmroot) {
467  fCbmroot= gROOT->GetRootFolder()->AddFolder("cbmroot", "Main Folder");
468  } else {
469  fCbmroot->SetName("cbmroot");
470  }
471  }
472 
473  // Get The list of branches from the input file and add it to the
474  // actual list of existing branches.
475  // Add this list of branches also to the map of input trees, which
476  // stores the information which branches belong to which input tree.
477  // There is at least one primary input tree, but there can be many
478  // additional friend trees.
479  // This information is needed to add new files to the correct friend
480  // tree. With this information it is also possible to check if the
481  // input files which are added to the input chain all have the same
482  // branch structure. Without this check it is possible to add trees
483  // with a different branch structure but the same tree name. ROOT
484  // probably only checks if the name of the tree is the same.
485  TList* list= dynamic_cast <TList*> (fInFile->Get("BranchList"));
486  TString chainName = "InputChain";
487  fInputLevel.push_back(chainName);
488  fCheckInputBranches[chainName] = new std::list<TString>;
489  if(list) {
490  TObjString* Obj=0;
491  for(Int_t i =0; i< list->GetEntries(); i++) {
492  Obj=dynamic_cast <TObjString*> (list->At(i));
493  fCheckInputBranches[chainName]->push_back(Obj->GetString().Data());
494  if(fBranchNameList->FindObject(Obj->GetString().Data())==0) {
495  fBranchNameList->AddLast(Obj);
496  fBranchSeqId++;
497  }
498  }
499  }
500 
501  gROOT->GetListOfBrowsables()->Add(fCbmroot);
502  fListFolder.Add( fCbmroot );
503 
504  // Store the information about the unique runids in the input file
505  // together with the filename and the number of events for each runid
506  // this information is needed later to check if inconsitencies exist
507  // between the main input chain and any of the friend chains.
508 
509  // GetRunIdInfo(fInFile->GetName(), chainName);
510 
511  for ( Int_t iobj = 0 ; iobj <= fNObj ; iobj++ ) {
512  if ( fObj2[iobj] ) {
513  cout << "-I- FairRootManager::OpenInTree(): Updating branch \"" << fObj2[iobj]->GetName()
514  << "\" (\"" << fObj2[iobj]->GetTitle() << "\") in the new fInTree \"" << fInTree << "\"" << endl;
515  TString tempBranchName = fObj2[iobj]->GetName();
516  fInTree->SetBranchStatus (tempBranchName.Data(),1);
517  fInTree->SetBranchAddress(tempBranchName.Data(),&fObj2[iobj]);
518  }
519  }
520 
521  Int_t nofEnt = (Int_t) fInTree->GetEntries();
522 
523  cout << "-I- FairRootManager::OpenInTree() Input TREE has " << nofEnt << " entries." << endl;
524 
525  return kTRUE;
526 }
527 //_____________________________________________________________________________
528 
529 //_____________________________________________________________________________
531 {
532  if(!fMixedInput) {
533  fInputChainList.push_back(name);
534  } else {
535  fLogger->Fatal(MESSAGE_ORIGIN, "You cannot use this method with mixed input!");
536  }
537 
538 }
539 //_____________________________________________________________________________
540 
541 //_____________________________________________________________________________
543 {
544  // Print information about the input structure
545  // List files from the input chain together with all files of
546  // all friend chains
547 
549  "The input consists out of the following trees and files:");
550  fLogger->Info(MESSAGE_ORIGIN," - %s",fInChain->GetName());
551  TObjArray* fileElements=fInChain->GetListOfFiles();
552  TIter next(fileElements);
553  TChainElement* chEl=0;
554  while (( chEl=(TChainElement*)next() )) {
555  fLogger->Info(MESSAGE_ORIGIN," - %s",chEl->GetTitle());
556  }
557 
558  map< TString, TChain* >::iterator mapIterator;
559  for (mapIterator = fFriendTypeList.begin();
560  mapIterator != fFriendTypeList.end(); mapIterator++ ) {
561  TChain* chain = (TChain*)mapIterator->second;
562  fLogger->Info(MESSAGE_ORIGIN," - %s",chain->GetName());
563  fileElements=chain->GetListOfFiles();
564  TIter next1(fileElements);
565  chEl=0;
566  while (( chEl=(TChainElement*)next1() )) {
567  fLogger->Info(MESSAGE_ORIGIN," - %s",chEl->GetTitle());
568  }
569  }
570 
571 }
572 //_____________________________________________________________________________
573 
574 //_____________________________________________________________________________
576 {
577  std::multimap< TString, std::multimap<TString,TArrayI> >::iterator it1;
578  std::multimap<TString,TArrayI> map1;
579 
580  // Get the structure from the input chain
581  it1=fRunIdInfoAll.find("InputChain");
582  map1 = it1->second;
583  std::vector<Int_t> runid;
584  std::vector<Int_t> events;
585  std::multimap<TString,TArrayI>::iterator it;
586  for ( it=map1.begin() ; it != map1.end(); it++ ) {
587  TArrayI bla = (*it).second;
588  runid.push_back(bla[0]);
589  events.push_back(bla[1]);
590  }
591 
592  // Now loop over all chains except the input chain and comapare the
593  // runids and event numbers.
594  // If there is a mismatch stop the execution.
595  Int_t errorFlag = 0;
596  TString inputLevel;
597  std::list<TString>::iterator listit;
598  for ( listit=fInputLevel.begin() ; listit != fInputLevel.end(); listit++ ) {
599  inputLevel = (*listit);
600  if ( !inputLevel.Contains("InputChain") ) {
601  it1=fRunIdInfoAll.find(inputLevel);
602  map1 = it1->second;
603  if ( runid.size() != map1.size()) {
604  errorFlag = 1;
605 // goto error_label;
606  break;
607  }
608  Int_t counter = 0;
609  for ( it=map1.begin() ; it != map1.end(); it++ ) {
610  TArrayI bla = (*it).second;
611  if ( (bla[0] != runid[counter]) || (bla[1] != events[counter]) ) {
612  errorFlag = 2;
613 // goto error_label;
614  break;
615  }
616  counter++;
617  }
618  if (errorFlag>0) {
619  break;
620  }
621  }
622  }
623 
624  // Use goto to leave double loop at once in case of error
625 // error_label:
626  if (errorFlag>0) {
627  fLogger->Error(MESSAGE_ORIGIN,"The input chain and the friend chain %s have a different structure:", inputLevel.Data());
628  if (errorFlag == 1) {
629  fLogger->Error(MESSAGE_ORIGIN,"The input chain has the following runids and event numbers:");
630  for ( Int_t i=0; i<runid.size(); i++) {
631  fLogger->Error(MESSAGE_ORIGIN," - Runid %i with %i events", runid[i], events[i]);
632  }
633  fLogger->Error(MESSAGE_ORIGIN,"The %s chain has the following runids and event numbers:", inputLevel.Data());
634  for ( it=map1.begin() ; it != map1.end(); it++ ) {
635  TArrayI bla = (*it).second;
636  fLogger->Error(MESSAGE_ORIGIN," - Runid %i with %i events", bla[0], bla[1]);
637  }
638  }
639  if (errorFlag == 2) {
640  Int_t counter = 0;
641  for ( it=map1.begin() ; it != map1.end(); it++ ) {
642  TArrayI bla = (*it).second;
643  fLogger->Error(MESSAGE_ORIGIN,"Runid Input Chain, %s chain: %i, %i", inputLevel.Data(), bla[0], runid[counter]);
644  fLogger->Error(MESSAGE_ORIGIN,"Event number Input Chain, %s chain: %i, %i", inputLevel.Data(), bla[1], events[counter]);
645  counter++;
646  }
647  }
648  fLogger->Fatal(MESSAGE_ORIGIN,"Event structure mismatch");
649  }
650 }
651 //_____________________________________________________________________________
652 
653 //_____________________________________________________________________________
655 {
656  for(std::map<TString, std::queue<TClonesArray*> >::iterator it = fDataContainer.begin(); it != fDataContainer.end(); it++) {
657  if (it->second.empty() == false) {
658  return kFALSE;
659  }
660  }
661  return kTRUE;
662 }
663 //_____________________________________________________________________________
664 
665 //_____________________________________________________________________________
667 {
668  for(std::map<TString, std::queue<TClonesArray*> >::iterator it = fDataContainer.begin(); it != fDataContainer.end(); it++) {
669  if (it->second.empty() == true) {
670  return kFALSE;
671  }
672  }
673  return kTRUE;
674 }
675 //_____________________________________________________________________________
676 
677 //_____________________________________________________________________________
679 {
680 
681  fLogger->Debug(MESSAGE_ORIGIN,"Check the output file");
682  fOutFile=f;
684  if (fOutFile->IsZombie()) {
685  fLogger->Fatal(MESSAGE_ORIGIN,"FairRootManager: Error opening output file ");
686  exit(-1);
687  }
688  FairRun* fRun = FairRun::Instance();
690  if(!fRun->IsAna()) {
691  fCbmroot= gROOT->GetRootFolder()->AddFolder("cbmroot", "Main Folder");
692  gROOT->GetListOfBrowsables()->Add(fCbmroot);
693  } else {
694  fCbmout= gROOT->GetRootFolder()->AddFolder("cbmout", "Main Output Folder");
695  gROOT->GetListOfBrowsables()->Add(fCbmout);
696  }
697  return fOutFile;
698 }
699 //_____________________________________________________________________________
700 
701 //_____________________________________________________________________________
702 TFile* FairRootManager::OpenOutFile(const char* fname)
703 {
704  fLogger->Debug(MESSAGE_ORIGIN,"Opening output file, %s", fname);
705  if(fOutFile) {
706  CloseOutFile();
707  }
708  cout << "FairRootManager::OpenOutFile(\"" << fname << "\")" << endl;
709  fOutFile = new TFile(fname, "recreate");
710  return OpenOutFile(fOutFile);
711 }
712 //_____________________________________________________________________________
713 
714 //_____________________________________________________________________________
715 void FairRootManager::Register(const char* name, const char* folderName , TNamed* obj, Bool_t toFile)
716 {
717 
718  // Security check. If the the name is equal the folder name there are problems with reading
719  // back the data. Instead of the object inside the folder the RootManger will return a pointer
720  // to the folder. To avoid such problems we check here if both strings are equal and stop the
721  // execution with some error message if this is the case.
722  if (strcmp (name, folderName) == 0 ) {
723  fLogger->Fatal(MESSAGE_ORIGIN,"The names for the object name %s and the folder name %s are equal. This isn't allowed. So we stop the execution at this point. Pleae change either the name or the folder name.", name, folderName);
724  }
725 
726  if(toFile) {
727  TFolder* folder=0;
728  TFolder* f=0;
729  if(fCbmout==0) {
730  f=(TFolder*)fCbmroot->FindObjectAny(folderName);
731  if(f==0) {
732  folder= fCbmroot->AddFolder(folderName,folderName);
733  } else {
734  folder=f;
735  }
736  } else {
737  f=(TFolder*)fCbmout->FindObjectAny(folderName);
738  if(f==0) {
739  folder= fCbmout->AddFolder(folderName,folderName);
740  } else {
741  folder=f;
742  }
743  }
744  obj->SetName(name);
745  folder->Add(obj);
746  }
747  AddMemoryBranch(name, obj );
748  //cout << " FairRootManager::Register Adding branch:(Obj) " << name << " In folder : " << folderName << endl;
749  if(fBranchNameList->FindObject(name)==0) {
750  fBranchNameList->AddLast(new TObjString(name));
751  fBranchSeqId++;
752  }
753 }
754 //_____________________________________________________________________________
755 
756 //_____________________________________________________________________________
757 void FairRootManager::Register(const char* name,const char* Foldername ,TCollection* obj, Bool_t toFile)
758 {
762  // Security check. If the the name is equal the folder name there are problems with reading
763  // back the data. Instead of the object inside the folder the RootManger will return a pointer
764  // to the folder. To avoid such problems we check here if both strings are equal and stop the
765  // execution with some error message if this is the case.
766  if (strcmp (name, Foldername) == 0 ) {
767  fLogger->Fatal(MESSAGE_ORIGIN,"The names for the object name %s and the folder name %s are equal. This isn't allowed. So we stop the execution at this point. Pleae change either the name or the folder name.", name, Foldername);
768  }
769 
770  if(toFile) {
771  TFolder* folder=0;
772  TFolder* f=0;
773  if(fCbmout==0) {
774  f=(TFolder*)fCbmroot->FindObjectAny(Foldername);
775  if(f==0) {
776  folder= fCbmroot->AddFolder(Foldername,Foldername);
777  } else {
778  folder=f;
779  }
780  } else {
781  f=(TFolder*)fCbmout->FindObjectAny(Foldername);
782  if(f==0) {
783  folder= fCbmout->AddFolder(Foldername,Foldername);
784  } else {
785  folder=f;
786  }
787  }
788  obj->SetName(name);
789  folder->Add(obj);
790  }
792  AddMemoryBranch(name, obj );
793  if(fBranchNameList->FindObject(name)==0) {
794  fBranchNameList->AddLast(new TObjString(name));
795  fBranchSeqId++;
796  }
797 }
798 //_____________________________________________________________________________
799 
800 //_____________________________________________________________________________
801 TClonesArray* FairRootManager::Register(TString branchName, TString className, TString folderName, Bool_t toFile)
802 {
803 
804  TClonesArray* outputArray;
805  if (fActiveContainer.find(branchName) == fActiveContainer.end()) {
806  fActiveContainer[branchName] = new TClonesArray(className);
807  if (fCompressData) {
808  std::queue<TClonesArray*> myQueue;
809  fDataContainer[branchName] = myQueue;
810  outputArray = new TClonesArray(className);
811  } else {
812  outputArray = fActiveContainer[branchName];
813  }
814  Register(branchName, folderName, outputArray, toFile);
815  }
816  return fActiveContainer[branchName];
817 }
818 //_____________________________________________________________________________
819 TClonesArray* FairRootManager::GetEmptyTClonesArray(TString branchName)
820 {
821  if (fActiveContainer.find(branchName) != fActiveContainer.end()) { //if a TClonesArray is registered in the active container
822  if (fActiveContainer[branchName] == 0) { //the address of the TClonesArray is still valid
823  std::cout << "-E- FairRootManager::GetEmptyTClonesArray: Container deleted outside FairRootManager!" << std::endl;
824  } else if (!fCompressData) { //if the data is not compressed the existing TClonesArray is just emptied
825  fActiveContainer[branchName]->Delete();
826  } else if (fActiveContainer[branchName]->GetEntries() > 0) { //if the container is not empty push it into the DataContainer storage and create a new one
827  fDataContainer[branchName].push(fActiveContainer[branchName]);
828  fLogger->Info(MESSAGE_ORIGIN, "GetEmptyTClonesArray moved %s with %i to data container ",
829  branchName.Data(), fActiveContainer[branchName]->GetEntries());
830  fActiveContainer[branchName] = new TClonesArray(fActiveContainer[branchName]->GetClass()->GetName());
831  } else {
832  fLogger->Info(MESSAGE_ORIGIN, "GetEmptyTClonesArray not moved %s %s with %i to data container ",
833  branchName.Data(),fActiveContainer[branchName] , fActiveContainer[branchName]->GetEntries());
834  }
835  return fActiveContainer[branchName]; // return the container
836  } else {
837  std::cout << "-E- Branch: " << branchName << " not registered!" << std::endl; // error if the branch is not registered
838  }
839  return 0;
840 }
841 //_____________________________________________________________________________
842 
843 //_____________________________________________________________________________
844 TClonesArray* FairRootManager::GetTClonesArray(TString branchName)
845 {
846  if (fActiveContainer.find(branchName) != fActiveContainer.end()) {
848  if (fCompressData && fActiveContainer[branchName]->GetEntries() > 0) {
850  fDataContainer[branchName].push(fActiveContainer[branchName]);
851  fActiveContainer[branchName] = new TClonesArray(fActiveContainer[branchName]->GetClass()->GetName());
852  }
853  return fActiveContainer[branchName]; // return the container
854  } else {
855  fLogger->Info(MESSAGE_ORIGIN, "Branch: %s not registered!" , branchName.Data());
856  }
857  // error if the branch is not registered
858  return 0;
859 }
860 //_____________________________________________________________________________
861 
862 //_____________________________________________________________________________
863 TClonesArray* FairRootManager::GetDataContainer(TString branchName)
864 {
865  if (DataContainersFilled()) {
866  return fDataContainer[branchName].front();
867  }
868  return 0;
869 }
870 //_____________________________________________________________________________
871 
872 //_____________________________________________________________________________
873 TClonesArray* FairRootManager::ForceGetDataContainer(TString branchName)
874 {
875  TClonesArray* result = 0;
876  if (fDataContainer.find(branchName)!= fDataContainer.end()) {
877  if (!fDataContainer[branchName].empty()) {
878  result = fDataContainer[branchName].front();
879  fDataContainer[branchName].pop();
880  }
881  }
882  return result;
883 }
884 //_____________________________________________________________________________
885 
886 //_____________________________________________________________________________
888 {
890  if(id < fBranchSeqId) {
891  TObjString* ObjStr= (TObjString*) fBranchNameList->At(id);
892  return ObjStr->GetString();
893  } else {
894  TString NotFound("Branch not found");
895  return NotFound;
896  }
897 }
898 //_____________________________________________________________________________
899 
900 //_____________________________________________________________________________
901 Int_t FairRootManager::GetBranchId(TString BrName)
902 {
904  TObjString* ObjStr;
905  Int_t Id=-1;
906  for(Int_t t=0; t<fBranchNameList->GetEntries(); t++) {
907  ObjStr= (TObjString*) fBranchNameList->At(t);
908  if(BrName==ObjStr->GetString()) {
909  Id=t;
910  break;
911  }
912  }
913  return Id;
914 
915 }
916 //_____________________________________________________________________________
917 
918 //_____________________________________________________________________________
919 TClonesArray* FairRootManager::GetData(TString branchName, BinaryFunctor* function, Double_t parameter)
920 {
921  if (fTSBufferMap[branchName] == 0) {
922  fTSBufferMap[branchName] = new FairTSBufferFunctional(branchName, GetInTree(), function);
923  }
924  fTSBufferMap[branchName]->SetStopFunction(function);
925  return fTSBufferMap[branchName]->GetData(parameter);
926 }
927 //_____________________________________________________________________________
928 
929 //_____________________________________________________________________________
930 TClonesArray* FairRootManager::GetData(TString branchName, BinaryFunctor* startFunction, Double_t startParameter, BinaryFunctor* stopFunction, Double_t stopParameter)
931 {
932  if (fTSBufferMap[branchName] == 0) {
933  fTSBufferMap[branchName] = new FairTSBufferFunctional(branchName, GetInTree(), stopFunction, startFunction);
934  }
935  fTSBufferMap[branchName]->SetStopFunction(stopFunction);
936  fTSBufferMap[branchName]->SetStartFunction(startFunction);
937  return fTSBufferMap[branchName]->GetData(startParameter, stopParameter);
938 }
939 //_____________________________________________________________________________
940 
941 //_____________________________________________________________________________
943 {
944  for(std::map<TString, FairTSBufferFunctional*>::iterator it = fTSBufferMap.begin(); it != fTSBufferMap.end(); it++) {
945  if (it->second->AllDataProcessed() == kFALSE && it->second->TimeOut() == kFALSE) {
946  return kFALSE;
947  }
948  }
949  return kTRUE;
950 }
951 //_____________________________________________________________________________
952 
953 //_____________________________________________________________________________
955 {
956 // Fills the tree.
957 // ---
958  if (fCompressData) {
959  if (DataContainersFilled()) {
961  ForceFill();
962  }
963  } else {
964  ForceFill();
965  }
966 }
967 //_____________________________________________________________________________
968 
969 //_____________________________________________________________________________
971 {
972  if (fOutTree != 0) {
973  fOutTree->Fill();
974  } else {
975  fLogger->Info(MESSAGE_ORIGIN, " No Output Tree");
976  }
977 }
978 //_____________________________________________________________________________
979 
980 //_____________________________________________________________________________
982 {
983  if (fFillLastData) {
984  Fill();
985  }
986 }
987 //_____________________________________________________________________________
988 
989 //_____________________________________________________________________________
991 {
993  if (fCompressData) {
995  }
996  if(fOutTree!=0) {
1002  fOutFile = fOutTree->GetCurrentFile();
1003  fOutFile->cd();
1004  fOutTree->Write();
1005  } else {
1006  fLogger->Info(MESSAGE_ORIGIN, "No Output Tree" );
1007  }
1008 }
1009 //_____________________________________________________________________________
1010 
1011 //_____________________________________________________________________________
1013 {
1016  if(fOutTree!=0) {
1017  fOutFile = fOutTree->GetCurrentFile();
1018  fOutFile->cd();
1019  gGeoManager->Write();
1020  }
1021 }
1022 //_____________________________________________________________________________
1023 
1024 //_____________________________________________________________________________
1025 void FairRootManager::CreateGeometryFile(const char* geofile)
1026 {
1032  TFile* oldfile=gFile;
1033  TFile* file=new TFile(geofile,"RECREATE");
1034  file->cd();
1035  gGeoManager->Write();
1036  file->Close();
1037  file->Delete();
1038  gFile=oldfile;
1039 }
1040 //_____________________________________________________________________________
1041 
1042 //_____________________________________________________________________________
1044 {
1045  fOutFile->cd();
1046  if(fCbmroot!=0 && fInFile==0) {
1047  fCbmroot->Write();
1048  }
1049  if(fCbmout!=0) {
1050  fCbmout->Write();
1051  }
1052  fBranchNameList->Write("BranchList", TObject::kSingleKey);
1053 }
1054 //_____________________________________________________________________________
1055 
1056 //_____________________________________________________________________________
1058 {
1059  SetEntryNr(i);
1060  if ( fInTree ) {
1061  cout << "FairRootManager::ReadEvent(" << i << "): FROM THE TREE " << fInTree << endl;
1062  if(0==fCurrentEntryNo) {
1063  Int_t totEnt = fInTree->GetEntries();
1064  fLogger->Info(MESSAGE_ORIGIN,"The number of entries in the tree is %i",totEnt);
1065  cout << "FairRootManager::ReadEvent(" << i << "): The tree has " << totEnt << " entries" << endl;
1066 
1067  fEvtHeader = (FairEventHeader*) GetObject("EventHeader.");
1068 
1069  SetEventTime();
1070  }
1071  fCurrentEntryNo=i;
1072  fInTree->GetEntry(i);
1073 
1074  if(fEvtHeader !=0) {
1078  } else { fLogger->Info(MESSAGE_ORIGIN, " No event Header was found!!!"); }
1079  return;
1080  }
1081 
1082  if(!fMixedInput) {
1084  if(0==fCurrentEntryNo) {
1085  Int_t totEnt = fInChain->GetEntries();
1086  fLogger->Info(MESSAGE_ORIGIN,"The number of entries in chain is %i",totEnt);
1087 
1088  fEvtHeader = (FairEventHeader*) GetObject("EventHeader.");
1089 
1090  SetEventTime();
1091  }
1092  fCurrentEntryNo=i;
1093  fInChain->GetEntry(i);
1094 
1095  if(fEvtHeader !=0) {
1099  } else {
1100  fLogger->Info(MESSAGE_ORIGIN, " No event Header was found!!!");
1101  }
1102 
1103  } else {
1104  fLogger->Info(MESSAGE_ORIGIN,"Read mixed event number %i", i);
1105  ReadMixedEvent(i);
1106 
1107  }
1108 }
1109 //_____________________________________________________________________________
1110 
1111 //_____________________________________________________________________________
1113 {
1117  if(0==fCurrentEntryNo) {
1118  fEvtHeader = (FairEventHeader*) GetObject("EventHeader.");
1119  SetEventTime();
1120  }
1121  Double_t SBratio=gRandom->Uniform(0,1);
1122  Bool_t GetASignal=kFALSE;
1123 
1124  if(fSBRatiobyN || fSBRatiobyT ) {
1125  std::map<UInt_t, Double_t>::const_iterator iterN;
1126  Double_t ratio=0;
1127  if(fCurrentEntryNo==0) {
1128  for(iterN = fSignalBGN.begin(); iterN != fSignalBGN.end(); iterN++) {
1129  ratio+=iterN->second;
1130  fSignalBGN[iterN->first]=ratio;
1131  fLogger->Debug(MESSAGE_ORIGIN,"--------------Set signal no. %i weight %f ", iterN->first, ratio);
1132  }
1133  }
1134  ratio=0;
1135  for(iterN = fSignalBGN.begin(); iterN != fSignalBGN.end(); iterN++) {
1136  ratio=iterN->second;
1137  fLogger->Debug(MESSAGE_ORIGIN,"---Check signal no. %i SBratio %f : ratio %f ", iterN->first , SBratio, ratio);
1138  if(SBratio <=ratio) {
1139  TChain* chain = fSignalTypeList[iterN->first];
1140  UInt_t entry = fCurrentEntry[iterN->first];
1141  chain->GetEntry(entry);
1142  fEvtHeader->SetMCEntryNumber(entry);
1143  fEvtHeader->SetInputFileId(iterN->first);
1145  GetASignal=kTRUE;
1146  fCurrentEntry[iterN->first]=entry+1;
1147  fLogger->Debug(MESSAGE_ORIGIN,"---Get entry No. %i from signal chain number --- %i --- ",entry, iterN->first);
1148  break;
1149  }
1150  }
1151  if(!GetASignal) {
1152  fBackgroundChain->GetEntry(i);
1154  fEvtHeader->SetInputFileId(0); //Background files has always 0 as Id
1156  fLogger->Debug(MESSAGE_ORIGIN,"---Get entry from background chain --- ");
1157  }
1158 
1159  }
1160  fCurrentEntryNo=i;
1162  fLogger->Debug(MESSAGE_ORIGIN,"--Event number --- %i with time ----%f",fCurrentEntryNo, GetEventTime());
1163 }
1164 //_____________________________________________________________________________
1165 
1166 //_____________________________________________________________________________
1168 {
1169  if(fMixedInput) {
1170  if(0==i) {
1171  Int_t totEnt = fBackgroundChain->GetEntries();
1172  fLogger->Info(MESSAGE_ORIGIN,"The number of entries in background chain is %i",totEnt);
1173  }
1174  fBackgroundChain->GetEntry(i);
1175  }
1176 }
1177 //_____________________________________________________________________________
1178 
1179 //_____________________________________________________________________________
1181 {
1182  Bool_t readentry=kFALSE;
1184  return readentry;
1185 }
1186 //_____________________________________________________________________________
1187 
1188 //_____________________________________________________________________________
1190 {
1192  TObject* Obj =NULL;
1193  fLogger->Debug2(MESSAGE_ORIGIN, " Try to find if the object %s already activated by other task or call", BrName);
1195  if(fCbmout) {
1196  Obj = fCbmout->FindObjectAny(BrName);
1197  if (Obj) {
1198  fLogger->Debug2(MESSAGE_ORIGIN, "object %s was already activated by another task", BrName);
1199  }
1200  }
1201 
1203  if(!Obj) {
1204  fLogger->Debug2(MESSAGE_ORIGIN, " Try to find if the object %s is a memory branch", BrName);
1205  Obj=GetMemoryBranch(BrName);
1206  if (Obj) {
1207  fLogger->Debug2(MESSAGE_ORIGIN, "Object %s is a memory branch", BrName);
1208  }
1209  }
1211  if(fCbmroot && !Obj) {
1213  fLogger->Debug2(MESSAGE_ORIGIN, " Object %s is not a memory branch and not yet activated, try the Input Tree (Chain)", BrName);
1214  Obj=fCbmroot->FindObjectAny(BrName);
1215  Obj=ActivateBranch(BrName);
1216  }
1217  if(!Obj) {
1218  Obj=ActivateBranch(BrName);
1219  }
1220  return Obj;
1221 }
1222 //_____________________________________________________________________________
1223 
1224 //_____________________________________________________________________________
1226 {
1227  if ( !fInTree ) {
1228  return GetObject(BrName);
1229  }
1231  TObject* Obj =NULL;
1232  fLogger->Debug2(MESSAGE_ORIGIN, " Try to find if the object %s already activated by other task or call", BrName);
1233 
1236  if(fCbmroot && !Obj) {
1238  fLogger->Debug2(MESSAGE_ORIGIN, " Object %s is not a memory branch and not yet activated, try the Input Tree (Chain)", BrName);
1239  Obj=fCbmroot->FindObjectAny(BrName);
1240  Obj=ActivateBranchInInTree(BrName);
1241  }
1242  if(!Obj) {
1243  Obj=ActivateBranch(BrName);
1244  }
1245  return Obj;
1246 }
1247 //_____________________________________________________________________________
1248 
1250 {
1251  TObject* result = 0;
1252 
1253 // std::cout << "GetCloneOfLinkData: Link " << link << std::endl;
1254  Int_t fileId = link.GetFile();
1255  Int_t entryNr = link.GetEntry();
1256  Int_t type = link.GetType();
1257  Int_t index = link.GetIndex();
1258 
1259  Int_t oldEntryNr = GetEntryNr();
1260 
1261 // std::cout << "OldEntryNr: " << GetEntryNr();
1262 
1263 // std::cout << "GetLinkData: " << link << std::endl;
1264 
1265  TTree* dataTree; //get the correct Tree
1266  if (fileId < 0) {
1267  dataTree = GetInTree();
1268  } else if (fileId == 0) {
1269  dataTree = GetBGChain();
1270  } else {
1271  dataTree = GetSignalChainNo(fileId);
1272  }
1273 
1274  if (dataTree == 0) {
1275  dataTree = GetInTree();
1276  }
1277 
1278  if (type < 0) {
1279  return 0;
1280  }
1281 
1282  TBranch* dataBranch = 0;
1283 
1284 // std::cout << "DataType: " << GetBranchName(type) << std::endl;
1285 
1286  if (fileId < 0 && fInputBranchMap[type] != 0) {
1287  dataBranch = fInputBranchMap[type];
1288  } else if (fileId < 0) {
1289  fInputBranchMap[type] = dataTree->GetBranch(GetBranchName(type));
1290  dataBranch = fInputBranchMap[type];
1291  } else {
1292  dataBranch = dataTree->GetBranch(GetBranchName(type));
1293  }
1294 
1295  if (dataBranch == 0) {
1296  return 0;
1297  }
1298 
1299  if (entryNr > -1) { //get the right entry (if entryNr < 0 then the current entry is taken
1300  if (entryNr < dataBranch->GetEntries()) {
1301  dataBranch->GetEntry(entryNr);
1302  } else {
1303  return 0;
1304  }
1305  } else { //the link entry nr is negative --> take the actual one
1306 
1307 // std::cout << "EntryNr: " << GetEntryNr() << std::endl;
1308 // dataBranch->GetEntry(GetEntryNr());
1309  }
1310 
1311  if (index < 0) { //if index is -1 then this is not a TClonesArray so only the Object is returned
1312  result = GetObject(GetBranchName(type))->Clone();
1313  } else {
1314  TClonesArray* dataArray = (TClonesArray*)GetObject(GetBranchName(type));
1315 
1316 // std::cout << "dataArray size: " << dataArray->GetEntriesFast() << std::endl;
1317  if (index < dataArray->GetEntriesFast()) {
1318 // std::cout << "DataArray at index " << index << " has Link: " << ((FairMultiLinkedData*)dataArray->At(index))->GetNLinks() << std::cout;
1319  result = dataArray->At(index)->Clone();
1320 // std::cout << "Result: " << *((FairMultiLinkedData*)result) << std::endl;
1321  }
1322  }
1323  if (entryNr > -1) {
1324  dataBranch->GetEntry(oldEntryNr); //reset the dataBranch to the original entry
1325  }
1326  return result;
1327 }
1328 
1329 //_____________________________________________________________________________
1330 
1332 {
1333  TClonesArray* result = 0;
1334 
1335  // std::cout << "GetCloneOfLinkData: Link " << link << std::endl;
1336  Int_t fileId = link.GetFile();
1337  Int_t entryNr = link.GetEntry();
1338  Int_t type = link.GetType();
1339  Int_t index = link.GetIndex();
1340 
1341  Int_t oldEntryNr = GetEntryNr();
1342 
1343  // std::cout << "OldEntryNr: " << GetEntryNr();
1344 
1345  // std::cout << "GetLinkData: " << link << std::endl;
1346 
1347  TTree* dataTree; //get the correct Tree
1348  if (fileId < 0) {
1349  dataTree = GetInTree();
1350  } else if (fileId == 0) {
1351  dataTree = GetBGChain();
1352  } else {
1353  dataTree = GetSignalChainNo(fileId);
1354  }
1355 
1356  if (dataTree == 0) {
1357  dataTree = GetInTree();
1358  }
1359 
1360  if (type < 0) {
1361  return 0;
1362  }
1363 
1364  TBranch* dataBranch = 0;
1365 
1366  // std::cout << "DataType: " << GetBranchName(type) << std::endl;
1367 
1368  if (fileId < 0 && fInputBranchMap[type] != 0) {
1369  dataBranch = fInputBranchMap[type];
1370  } else if (fileId < 0) {
1371  fInputBranchMap[type] = dataTree->GetBranch(GetBranchName(type));
1372  dataBranch = fInputBranchMap[type];
1373  } else {
1374  dataBranch = dataTree->GetBranch(GetBranchName(type));
1375  }
1376 
1377  if (dataBranch == 0) {
1378  return 0;
1379  }
1380 
1381  if (entryNr > -1) { //get the right entry (if entryNr < 0 then the current entry is taken
1382  if (entryNr < dataBranch->GetEntries()) {
1383  dataBranch->GetEntry(entryNr);
1384  } else {
1385  return 0;
1386  }
1387  } else { //the link entry nr is negative --> take the actual one
1388 
1389  // std::cout << "EntryNr: " << GetEntryNr() << std::endl;
1390  // dataBranch->GetEntry(GetEntryNr());
1391  }
1392 
1393  if (index < 0) { //if index is -1 then this is not a TClonesArray so only the Object is returned
1394  result = 0;
1395  } else {
1396  result = (TClonesArray*) GetObject(GetBranchName(type))->Clone();
1397  }
1398  if (entryNr > -1) {
1399  dataBranch->GetEntry(oldEntryNr); //reset the dataBranch to the original entry
1400  }
1401  return result;
1402 
1403 }
1404 //_____________________________________________________________________________
1405 FairGeoNode* FairRootManager::GetGeoParameter(const char* detname, const char* gname)
1406 {
1407  TFolder* detf= NULL;
1408  FairGeoNode* node = NULL;
1409  TList* lgeo=NULL;
1410  TString lname(detname);
1411  lname+="GeoPar";
1412  TFolder* froot = dynamic_cast<TFolder*> (gROOT->FindObject("cbmroot"));
1413  if ( froot ) {
1414  detf = dynamic_cast<TFolder*> (froot->FindObjectAny( detname ));
1415  } else {
1416  fLogger->Info(MESSAGE_ORIGIN, "GetGeoParameter() Main Folder not found ! ");
1417  }
1418  if ( detf ) {
1419  lgeo = dynamic_cast<TList*> (detf->FindObjectAny( lname.Data() ));
1420  } else {
1421  fLogger->Info(MESSAGE_ORIGIN, "GetGeoParameter() GeoPar: %s " , lname.Data() );
1422  }
1423  if ( lgeo ) {
1424  node = dynamic_cast<FairGeoNode*> (lgeo->FindObject(gname));
1425  } else {
1426  fLogger->Info(MESSAGE_ORIGIN, "GetGeoParameter() GeoList not found " );
1427  }
1428  return node;
1429 }
1430 //_____________________________________________________________________________
1431 
1432 //_____________________________________________________________________________
1433 void FairRootManager::TruncateBranchNames(TTree* fTree, const char* folderName)
1434 {
1445  TFolder* cbm=(TFolder*)gROOT->FindObjectAny(folderName);
1446  if(cbm) {
1447  TCollection* lf=cbm->GetListOfFolders();
1448  TIterator* iter= lf->MakeIterator();
1449  TObjArray* Br= fTree->GetListOfBranches();
1450  TIterator* BrIter= Br->MakeIterator();
1451  TObject* obj;
1452  TObject* BrObj;
1453 
1455  while((obj=iter->Next())) {
1456 
1462  TString ffn=cbm->GetName();
1463  ffn=ffn+".";
1464  ffn=ffn+obj->GetName();
1465  ffn=ffn+".";
1466 
1473  BrIter->Reset();
1474 
1475  while((BrObj=BrIter->Next())) {
1476  TBranch* b=(TBranch*)BrObj;
1477  TruncateBranchNames(b, ffn);
1478  }
1479  }
1480 
1481  // Remove all occurence of FairMCEventHeader and FairEventHeader from
1482  // all branches containing that string.
1483  // This is not the correct way to do it, but up tonow we don't understand
1484  // why this part comes in when storing a derrived class from
1485  // FairMCEventHeader or FairEventHeader.
1486 
1487  iter->Reset();
1488  while((obj=iter->Next())) {
1489 
1490  TString ffn=".FairMCEventHeader";
1491  BrIter->Reset();
1492 
1493  while((BrObj=BrIter->Next())) {
1494  TBranch* b=(TBranch*)BrObj;
1495  TruncateBranchNames(b, ffn);
1496  }
1497  }
1498 
1499  iter->Reset();
1500  while((obj=iter->Next())) {
1501 
1502  TString ffn=".FairEventHeader";
1503  BrIter->Reset();
1504 
1505  while((BrObj=BrIter->Next())) {
1506  TBranch* b=(TBranch*)BrObj;
1507  TruncateBranchNames(b, ffn);
1508  }
1509  }
1510 
1511  delete iter;
1512  delete BrIter;
1513  }
1514 }
1515 //_____________________________________________________________________________
1516 
1517 //_____________________________________________________________________________
1518 void FairRootManager::TruncateBranchNames(TBranch* b, TString ffn)
1519 {
1526  TObject* BrObj;
1527  TString nn= b->GetName();
1528  // cout<<"nn.Data before: "<<nn.Data()<<endl;
1529  nn.ReplaceAll(ffn.Data(),"");
1530  // cout<<"nn.Data after: "<<nn.Data()<<endl;
1531  // cout <<"##################"<<endl;
1532  b->SetName(nn.Data());
1533  TObjArray* Br= b->GetListOfBranches();
1534  TIterator* BrIter= Br->MakeIterator();
1535  BrIter->Reset();
1536 
1537  while((BrObj=BrIter->Next())) {
1538  TBranch* bb=(TBranch*)BrObj;
1539  TruncateBranchNames(bb, ffn);
1540  }
1541  delete BrIter;
1542 }
1543 //_____________________________________________________________________________
1544 
1545 //_____________________________________________________________________________
1546 Int_t FairRootManager::CheckBranch(const char* BrName)
1547 {
1549  if(!fBranchPerMap) {
1550  CreatePerMap();
1551  return CheckBranchSt(BrName);
1552  } else {
1553  fBrPerMapIter=fBrPerMap.find(BrName);
1554  if(fBrPerMapIter!=fBrPerMap.end()) {
1555  return fBrPerMapIter->second;
1556  } else {
1557  return 0;
1558  }
1559  }
1560 }
1561 //_____________________________________________________________________________
1562 
1563 //_____________________________________________________________________________
1565 {
1566  for(Int_t t=0; t<list->GetEntries(); t++) {
1567  fBranchNameList->AddAt(list->At(t),t);
1568  }
1569 }
1570 
1571 
1572 
1573 
1574 
1575 
1576 //_____________________________________________________________________________
1578 //_____________________________________________________________________________
1579 
1580 //_____________________________________________________________________________
1582 {
1589  fNObj++;
1590  fObj2[fNObj] = GetMemoryBranch ( BrName );
1591  if ( fObj2[fNObj] ) {
1592  return fObj2[fNObj];
1593  }
1595  fLogger->Debug2(MESSAGE_ORIGIN, " Try to find an object %s decribing the branch in the folder structure in file", BrName);
1596  for(Int_t i=0; i<fListFolder.GetEntriesFast(); i++) {
1597  TFolder* fold = (TFolder*) fListFolder.At(i);
1598  fObj2[fNObj] = fold->FindObjectAny(BrName);
1599  if (fObj2[fNObj] ) {
1600  fLogger->Debug(MESSAGE_ORIGIN, "object %s decribing the branch in the folder structure was found", BrName);
1601  break;
1602  }
1603  }
1604 
1605  if(!fObj2[fNObj]) {
1609  fLogger->Info(MESSAGE_ORIGIN, " Branch: %s not found in Tree ", BrName);
1610  //Fatal(" No Branch in the tree", BrName );
1611  return 0;
1612  } else {
1613  if(fMixedInput) {
1619  fLogger->Debug2(MESSAGE_ORIGIN, "Set the Branch address for background branch %s", BrName);
1620  fBackgroundChain->SetBranchStatus(BrName,1);
1621  fBackgroundChain->SetBranchAddress(BrName,&fObj2[fNObj]);
1622 
1623  std::map<UInt_t, TChain*>::const_iterator iter;
1624  Int_t no=0;
1625  for(iter = fSignalTypeList.begin(); iter != fSignalTypeList.end(); iter++) {
1626  TChain* currentChain=iter->second;
1627  fLogger->Debug2(MESSAGE_ORIGIN, "Set the Branch address for signal file number %i and branch %s ", no++ , BrName);
1628  currentChain->SetBranchStatus(BrName,1);
1629  currentChain->SetBranchAddress(BrName,&fObj2[fNObj]);
1630  }
1631 
1632 
1633  } else {
1634 
1635  if ( fInTree ) {
1636  fInTree->SetBranchStatus (BrName,1);
1637  fInTree->SetBranchAddress(BrName,&fObj2[fNObj]);
1638  } else {
1639  fInChain->SetBranchStatus(BrName,1);
1640  fInChain->SetBranchAddress(BrName,&fObj2[fNObj]);
1641  }
1642  }
1643  }
1644 
1645  AddMemoryBranch( BrName , fObj2[fNObj] );
1646  return fObj2[fNObj];
1647 
1648 }
1649 
1650 //_____________________________________________________________________________
1651 
1652 //_____________________________________________________________________________
1654 {
1655  fNObj++;
1656 
1657  for(Int_t i=0; i<fListFolder.GetEntriesFast(); i++) {
1658  TFolder* fold = (TFolder*) fListFolder.At(i);
1659  fObj2[fNObj] = fold->FindObjectAny(BrName);
1660  if (fObj2[fNObj] ) {
1661  break;
1662  }
1663  }
1664 
1665  if(!fObj2[fNObj]) {
1669  fLogger->Info(MESSAGE_ORIGIN, " Branch: %s not found in Tree ", BrName);
1670  //Fatal(" No Branch in the tree", BrName );
1671  return 0;
1672  } else {
1673  fInTree->SetBranchStatus (BrName,1);
1674  fInTree->SetBranchAddress(BrName,&fObj2[fNObj]);
1675  }
1676 
1677  AddMemoryBranch( BrName , fObj2[fNObj] );
1678  return fObj2[fNObj];
1679 
1680 }
1681 //_____________________________________________________________________________
1682 
1683 //_____________________________________________________________________________
1684 void FairRootManager::AddMemoryBranch( const char* fName, TObject* pObj )
1685 {
1687  map < TString, TObject*>::iterator p;
1688  TString BrName=fName;
1689  p=fMap.find(BrName);
1690  if(p!=fMap.end()) {
1691  } else {
1692  fMap.insert(pair<TString, TObject*> (BrName, pObj));
1693  }
1694 }
1695 //_____________________________________________________________________________
1696 
1697 //_____________________________________________________________________________
1699 {
1700  for(std::map<TString, std::queue<TClonesArray*> >::iterator it = fDataContainer.begin(); it != fDataContainer.end(); it++) {
1701  AssignTClonesArray(it->first);
1702  }
1703 }
1704 //_____________________________________________________________________________
1705 void FairRootManager::AssignTClonesArray(TString branchName)
1706 {
1707  TClonesArray* output = (TClonesArray*)GetObject(branchName);
1708  TClonesArray* input = ForceGetDataContainer(branchName);
1709  output->Clear();
1710  if (input != 0) {
1711  output->AbsorbObjects(input, 0, input->GetEntries() - 1);
1712  }
1713 }
1714 //_____________________________________________________________________________
1715 Int_t FairRootManager::CheckBranchSt(const char* BrName)
1716 {
1717  Int_t returnvalue=0;
1718  TObject* Obj1 =NULL;
1719  if (fCbmroot) {
1720  Obj1 = fCbmroot->FindObjectAny(BrName);
1721  }
1722  if(fCbmout && !Obj1) {
1723  Obj1 = fCbmout->FindObjectAny(BrName); //Branch in output folder
1724  }
1725  if(!Obj1) {
1726  for(Int_t i=0; i<fListFolder.GetEntriesFast(); i++) {
1727 // cout << "Search in Folder: " << i << " " << listFolder.At(i) << endl;
1728  TFolder* fold = dynamic_cast<TFolder*> (fListFolder.At(i));
1729  if(fold!=0) {
1730  Obj1= fold->FindObjectAny(BrName);
1731  }
1732  if (Obj1) {
1733  break;
1734  }
1735  }
1736  }
1737  TObject* Obj2 =NULL;
1738  Obj2=GetMemoryBranch(BrName); // Branch in Memory
1739  if (Obj1!=0) {
1740  returnvalue=1;
1741  } else if(Obj2!=0) {
1742  returnvalue=2;
1743  } else {
1744  returnvalue= 0;
1745  }
1746 
1751  return returnvalue;
1752 }
1753 //_____________________________________________________________________________
1754 
1755 //_____________________________________________________________________________
1757 {
1758 // cout << " FairRootManager::CreatePerMap() " << endl;
1759  fBranchPerMap=kTRUE;
1760  for (Int_t i=0; i<fBranchSeqId; i++) {
1761  TObjString* name= (TObjString*)(fBranchNameList->At(i));
1762 // cout << " FairRootManager::CreatePerMap() Obj At " << i << " is " << name->GetString() << endl;
1763  TString BrName=name->GetString();
1764  fBrPerMap.insert(pair<TString, Int_t> (BrName, CheckBranchSt(BrName.Data())));
1765  }
1766 
1767 }
1768 //_____________________________________________________________________________
1769 
1770 //_____________________________________________________________________________
1772 {
1773 
1774 //return fMap[BrName];
1775  TString BrName=fName;
1776  map < TString, TObject*>::iterator p;
1777  p=fMap.find(BrName);
1778 
1779  if(p!=fMap.end()) {
1780  return p->second;
1781  } else {
1782  return 0;
1783  }
1784 }
1785 //_____________________________________________________________________________
1786 
1787 //_____________________________________________________________________________
1789 {
1790  while(!DataContainersEmpty()) {
1792  ForceFill();
1793  }
1794 }
1795 //_____________________________________________________________________________
1796 
1797 //_____________________________________________________________________________
1799 {
1800  fFriendFileList.push_back(Name);
1801 }
1802 //_____________________________________________________________________________
1803 
1804 //_____________________________________________________________________________
1806 {
1807  // Loop over all Friend files and extract the type. The type is defined by
1808  // the tree which is stored in the file. If there is already a chain of with
1809  // this type of tree then the file will be added to this chain.
1810  // If there is no such chain it will be created.
1811  //
1812  // Check if the order of runids and the event numbers per runid for all
1813  // friend chains is the same as the one defined by the input chain.
1814  // TODO: Should the order be corrected or should the execution be stopped.
1815  // The order in the input tree defined by the order in which the files have
1816  // been added. A file is defined by the runid.
1817 
1818  // In the old way it was needed sometimes to add a freind file more
1819  // than once. This is not needed any longer, so we remove deuplicates
1820  // from the list and display a warning.
1821  std::list<TString> friendList;
1822  std::list<TString>::iterator iter1;
1823  for(iter1 = fFriendFileList.begin();
1824  iter1 != fFriendFileList.end(); iter1++) {
1825  if (find(friendList.begin(), friendList.end(), (*iter1))
1826  == friendList.end()) {
1827  friendList.push_back(*iter1);
1828  }
1829  }
1830  // TODO: print a warning if it was neccessary to remove a filname from the
1831  // list. This can be chacked by comparing the size of both list
1832 
1833  TFile* temp = gFile;
1834 
1835  Int_t friendType = 1;
1836  // Loop over all files which have been added as friends
1837  for(iter1 = friendList.begin();
1838  iter1 != friendList.end(); iter1++) {
1839  std::list<TString>::iterator iter;
1840  TString inputLevel;
1841  // Loop over all already defined input levels to check if this type
1842  // of friend tree is already added.
1843  // If this type of friend tree already exist add the file to the
1844  // then already existing friend chain. If this type of friend tree
1845  // does not exist already create a new friend chain and add the file.
1846  Bool_t inputLevelFound = kFALSE;
1847  TFile* inputFile;
1848  for ( iter = fInputLevel.begin(); iter !=fInputLevel.end(); iter++ ) {
1849  inputLevel = (*iter);
1850 
1851  inputFile = new TFile((*iter1));
1852  if (inputFile->IsZombie()) {
1853  fLogger->Fatal(MESSAGE_ORIGIN, "Error opening the file %s which should be added to the input chain or as friend chain", (*iter).Data());
1854  }
1855 
1856  // Check if the branchlist is already stored in the map. If it is
1857  // already stored add the file to the chain.
1858  Bool_t isOk = CompareBranchList(inputFile, inputLevel);
1859  if ( isOk ) {
1860  inputLevelFound = kTRUE;
1861  inputFile->Close();
1862  continue;
1863  }
1864  inputFile->Close();
1865  }
1866  if (!inputLevelFound) {
1867  inputLevel= Form("FriendTree_%i",friendType);
1868  CreateNewFriendChain((*iter1), inputLevel);
1869  friendType++;
1870  }
1871 
1872  TChain* chain = (TChain*) fFriendTypeList[inputLevel];
1873  chain->AddFile((*iter1), 1234567890, "cbmsim");
1874  }
1875  gFile=temp;
1876 
1877  // Check if all friend chains have the same runids and the same
1878  // number of event numbers as the corresponding input chain
1879 // CheckFriendChains();
1880 
1881  // Add all the friend chains which have been created to the
1882  // main input chain.
1883  map< TString, TChain* >::iterator mapIterator;
1884  for (mapIterator = fFriendTypeList.begin();
1885  mapIterator != fFriendTypeList.end(); mapIterator++ ) {
1886 
1887  TChain* chain = (TChain*)mapIterator->second;
1888  fInChain->AddFriend(chain);
1889  }
1890 
1891  // Print some output about the input structure
1892  PrintFriendList();
1893 
1894 }
1895 //_____________________________________________________________________________
1896 
1897 //_____________________________________________________________________________
1898 void FairRootManager::CreateNewFriendChain(TString inputFile, TString inputLevel)
1899 {
1900 
1901  TFile* temp = gFile;
1902  TFile* f = new TFile(inputFile);
1903 
1904  TFolder* added=NULL;
1905  TString folderName = "/cbmout";
1906  TString folderName1 = "cbmout";
1907  added = dynamic_cast <TFolder*> (f->Get("cbmout"));
1908  if(added==0) {
1909  folderName = "/cbmroot";
1910  folderName1 = "cbmroot";
1911  added = dynamic_cast <TFolder*> (f->Get("cbmroot"));
1912  }
1913  folderName1=folderName1+"_"+inputLevel;
1914  added->SetName(folderName1);
1915  fListFolder.Add( added );
1916 
1918  TList* list= dynamic_cast <TList*> (f->Get("BranchList"));
1919  TString chainName = inputLevel;
1920  fInputLevel.push_back(chainName);
1921  fCheckInputBranches[chainName] = new std::list<TString>;
1922  if(list) {
1923  TObjString* Obj=0;
1924  for(Int_t i =0; i< list->GetEntries(); i++) {
1925  Obj=dynamic_cast <TObjString*> (list->At(i));
1926  fCheckInputBranches[chainName]->push_back(Obj->GetString().Data());
1927  if(fBranchNameList->FindObject(Obj->GetString().Data())==0) {
1928  fBranchNameList->AddLast(Obj);
1929  fBranchSeqId++;
1930  }
1931  }
1932  }
1933 
1934  TChain* chain = new TChain(inputLevel,folderName);
1935  fFriendTypeList[inputLevel]=chain;
1936 
1937  f->Close();
1938  gFile = temp;
1939 
1940 }
1941 //_____________________________________________________________________________
1942 
1943 //_____________________________________________________________________________
1944 Bool_t FairRootManager::CompareBranchList(TFile* fileHandle, TString inputLevel)
1945 {
1946  // fill a set with the original branch structure
1947  // This allows to use functions find and erase
1948  std::set<TString> branches;
1949  list<TString>::const_iterator iter;
1950  for(iter = fCheckInputBranches[inputLevel]->begin();
1951  iter != fCheckInputBranches[inputLevel]->end(); iter++) {
1952  branches.insert(*iter);
1953  }
1954 
1955  // To do so we have to loop over the branches in the file and to compare
1956  // the branches in the file with the information stored in
1957  // fCheckInputBranches["InputChain"]. If both lists are equal everything
1958  // is okay
1959 
1960  // Get The list of branches from the input file one by one and compare
1961  // it to the reference list of branches which is defined for this tree.
1962  // If a branch with the same name is found, this branch is removed from
1963  // the list. If in the end no branch is left in the list everything is
1964  // fine.
1965  set<TString>::iterator iter1;
1966  TList* list= dynamic_cast <TList*> (fileHandle->Get("BranchList"));
1967  if(list) {
1968  TObjString* Obj=0;
1969  for(Int_t i =0; i< list->GetEntries(); i++) {
1970  Obj=dynamic_cast <TObjString*> (list->At(i));
1971  iter1=branches.find(Obj->GetString().Data());
1972  if (iter1 != branches.end() ) {
1973  branches.erase (iter1);
1974  } else {
1975  // Not found is an error because branch structure is
1976  // different. It is impossible to add to tree with a
1977  // different branch structure
1978  return kFALSE;
1979  }
1980  }
1981  }
1982  // If the size of branches is !=0 after removing all branches also in the
1983  // reference list, this is also a sign that both branch list are not the
1984  // same
1985  if (branches.size() != 0 ) {
1986  return kFALSE;
1987  }
1988 
1989  return kTRUE;
1990 }
1991 //_____________________________________________________________________________
1992 
1993 //_____________________________________________________________________________
1994 //void FairRootManager::GetRunIdInfo(TFile* fileHandle, TString inputLevel)
1995 void FairRootManager::GetRunIdInfo(TString fileName, TString inputLevel)
1996 {
1997  TFile* temp=gFile;
1998  TFile* fileHandle = new TFile(fileName);
1999 
2000  TTree* testTree = dynamic_cast<TTree*>(fileHandle->Get("cbmsim"));
2001  TFolder* folder = dynamic_cast<TFolder*>(fileHandle->Get("cbmroot"));
2002  if (!folder) {
2003  folder = dynamic_cast<TFolder*>(fileHandle->Get("cbmout"));
2004  }
2005 
2006  // Get the information about runid and start and stop event number
2007  // If there is a branch with this information use this information
2008  // directly. If not loop over all events and read the MCEventHeader
2009  // information to extract all different runids and start/stop event
2010  // numbers.
2011 
2012  FairEventHeader* header =
2013  (FairEventHeader*)folder->FindObjectAny("EventHeader.");
2014  // With the follwing two lines the MCEventHeader is not filled
2015  // correctely. The runid is correct but the event numbers are
2016  // zero all the time. This must be a bug.
2017  // TODO: Create example and submit a bug report to the ROOT team
2018  // testTree->SetBranchStatus("*",0); //disable all branches
2019  // testTree->SetBranchStatus("MCEventHeader.",1);
2020  testTree->SetBranchAddress("EventHeader.", &header);
2021 
2022  Int_t entries = (Int_t) testTree->GetEntriesFast();
2023 
2024 
2025  TArrayI runidInfo(2);
2026 
2027  testTree->GetEntry(0);
2028 
2029  Int_t runid;
2030  Int_t counter = 1;
2031  runid = header->GetRunId();
2032  runidInfo.AddAt(runid,0);
2033 
2034  std::map<TString, std::multimap<TString,TArrayI> >::iterator it;
2035 
2036  std::multimap<TString, TArrayI> myRunIdInfo;
2037  it=fRunIdInfoAll.find(inputLevel);
2038  if ( it != fRunIdInfoAll.end()) {
2039  myRunIdInfo=it->second;
2040  }
2041 
2042  for (Int_t i=1; i<entries ; i++) {
2043  testTree->GetEntry(i);
2044  runid = header->GetRunId();
2045  if ( runid != runidInfo[0] ) {
2046  runidInfo.AddAt(counter,1);
2047  // Fill info to structure
2048  myRunIdInfo.insert(pair<TString,TArrayI>(fileName,runidInfo));
2049  runidInfo.Reset();
2050  runid = header->GetRunId();
2051  runidInfo.AddAt(runid,0);
2052  counter = 0;
2053  }
2054  counter++;
2055  }
2056 
2057  runidInfo.AddAt(counter,1);
2058  myRunIdInfo.insert(pair<TString,TArrayI>(fileName,runidInfo));
2059  fRunIdInfoAll.erase(inputLevel);
2060  fRunIdInfoAll.insert(pair<TString, std::multimap<TString,TArrayI> >
2061  (inputLevel, myRunIdInfo));
2062 
2063  fileHandle->Close();
2064  gFile=temp;
2065 }
2066 //_____________________________________________________________________________
2067 
2068 //_____________________________________________________________________________
2070 {
2071  fLogger->Debug(MESSAGE_ORIGIN,"-- Get Event Time --");
2072  if(!fEvtHeaderIsNew && fEvtHeader!=0) {
2073  Double_t EvtTime=fEvtHeader->GetEventTime();
2074  if( !(EvtTime<0)) {
2075  return EvtTime;
2076  }
2077  }
2078 
2079  if (fEventTimeInMCHeader && !fMCHeader) {
2080  fLogger->Debug(MESSAGE_ORIGIN," No MCEventHeader, time is set to 0");
2081  return 0;
2082  } else if(fEventTimeInMCHeader && fMCHeader) {
2084  fLogger->Debug(MESSAGE_ORIGIN," Get event time from MCEventHeader : %f ns", fEventTime);
2085  return fEventTime;
2086  } else {
2087 
2089  SetEventTime();
2090  }
2091  fLogger->Debug(MESSAGE_ORIGIN," Calculate event time from user input : %f ns", fEventTime);
2092  return fEventTime;
2093  }
2094 }
2095 //_____________________________________________________________________________
2096 
2097 //_____________________________________________________________________________
2099 {
2100  fOutFile->cd();
2101  f->SetName(fOutFile->GetName());
2102  f->Write("FileHeader", TObject::kSingleKey);
2103 }
2104 //_____________________________________________________________________________
2105 
2106 //_____________________________________________________________________________
2108 {
2112  fEventTimeInMCHeader=kFALSE;
2113 }
2114 //_____________________________________________________________________________
2115 
2116 //_____________________________________________________________________________
2118 {
2119  fEventMeanTime =mean;
2120  TString form="(1/";
2121  form+= mean;
2122  form+=")*exp(-x/";
2123  form+=mean;
2124  form+=")";
2125  fTimeProb= new TF1("TimeProb.", form.Data(), 0., mean*10);
2126  fTimeProb->GetRandom();
2127  fEventTimeInMCHeader=kFALSE;
2128 }
2129 //_____________________________________________________________________________
2130 
2131 //_____________________________________________________________________________
2133 {
2134  fLogger->Debug(MESSAGE_ORIGIN, "Set event time for Entry = %i , where the current entry is %i",
2136  if(fTimeProb!=0) {
2137  fLogger->Debug(MESSAGE_ORIGIN, "Time will be set via sampling method : old time = %f ", fEventTime);
2138  fEventTime += fTimeProb->GetRandom();
2139  fLogger->Debug(MESSAGE_ORIGIN, "Time set via sampling method : %f ", fEventTime);
2140  } else {
2141  fEventTime += gRandom->Uniform( fEventTimeMin, fEventTimeMax);
2142  fLogger->Debug(MESSAGE_ORIGIN, "Time set via Uniform Random : %f ", fEventTime);
2143 
2144  }
2146 }
2147 //_____________________________________________________________________________
2148 
2149 //_____________________________________________________________________________
2150 void FairRootManager::BGWindowWidthNo(UInt_t background, UInt_t Signalid)
2151 {
2152  fLogger->Info(MESSAGE_ORIGIN, "SetSignal rate for signal %i : %i ", Signalid , background );
2153  fSBRatiobyN=kTRUE;
2154  if(fSBRatiobyT) {
2155  fLogger->Fatal(MESSAGE_ORIGIN, "Signal rate already set by TIME!!");
2156  }
2157  Double_t value=1.0/background;
2158  if(background!=0) {
2159  fSignalBGN[Signalid]=value;
2160  } else {
2161  fLogger->Fatal(MESSAGE_ORIGIN, "Background cannot be Zero when setting the signal rate!!");
2162  }
2163 }
2164 //_____________________________________________________________________________
2165 
2166 //_____________________________________________________________________________
2167 void FairRootManager::BGWindowWidthTime(Double_t background, UInt_t Signalid)
2168 {
2169  fSBRatiobyT=kTRUE;
2170  if(fSBRatiobyN) {
2171  fLogger->Fatal(MESSAGE_ORIGIN, "Signal rate already set by NUMBER!!");
2172  }
2173  if(fEventTimeInMCHeader) {
2174  fLogger->Fatal(MESSAGE_ORIGIN, "You have to Set the Event mean time before using SetSignalRateTime!");
2175  }
2176  if(fEventMeanTime==0) {
2177  fLogger->Fatal(MESSAGE_ORIGIN, "Event mean time cannot be zero when using signal rate with time ");
2178  }
2180  Double_t value=fEventMeanTime/background;
2181  if(background!=0) {
2182  fSignalBGN[Signalid]=value;
2183  } else {
2184  fLogger->Fatal(MESSAGE_ORIGIN, "Background cannot be Zero when setting the signal rate!!");
2185  }
2186 
2187 }
2188 //_____________________________________________________________________________
2189 
2190 //_____________________________________________________________________________
2192 {
2193 
2194  fLogger->Info(MESSAGE_ORIGIN, "Maximum No of Event was set manually to : %i , we will check if there is enough entries for this!! ", EvtEnd);
2195  Int_t MaxEventNo=0;
2196  if(EvtEnd!=0) {
2197  MaxEventNo=EvtEnd;
2198  }
2199  Int_t localMax=0;
2200  if(!fMixedInput) {
2201  MaxEventNo=fInChain->GetEntries();
2202  } else {
2203  Int_t MaxBG=fBackgroundChain->GetEntries();
2204  Int_t MaxS=0;
2205  Double_t ratio=0.;
2206  std::map<UInt_t, Double_t>::const_iterator iterN;
2207  for(iterN = fSignalBGN.begin(); iterN != fSignalBGN.end(); iterN++) {
2208  TChain* chain = fSignalTypeList[iterN->first];
2209  MaxS=chain->GetEntries();
2210  fLogger->Info(MESSAGE_ORIGIN, "Signal chain No %i has : %i entries ", iterN->first, MaxS);
2211  ratio=iterN->second;
2212  cout<< " ratio = "<< ratio << "floor(MaxS/ratio) "<<floor(MaxS/ratio) << " MaxBG = " << MaxBG << endl;
2213  if(floor(MaxS/ratio) > MaxBG) {
2214  localMax=MaxBG+(Int_t)floor(MaxBG*ratio);
2215  fLogger->Warning(MESSAGE_ORIGIN, "No of Event in Background chain is not enough for all signals in chain %i ", iterN->first);
2216  } else {
2217  localMax=(Int_t)floor(MaxS/ratio);
2218  fLogger->Warning(MESSAGE_ORIGIN, "No of Event in signal chain %i is not enough, the maximum event number will be reduced to : %i ", iterN->first,localMax );
2219  }
2220  if(MaxEventNo==0 || MaxEventNo > localMax) {
2221  MaxEventNo=localMax;
2222  }
2223  }
2224  fLogger->Info(MESSAGE_ORIGIN, "Maximum No of Event will be set to : %i ", MaxEventNo);
2225  }
2226  return MaxEventNo;
2227 }
2228 //_____________________________________________________________________________
2229 
2230 //_____________________________________________________________________________
2232 {
2233  if (fWriteoutBufferMap[branchName] == 0) {
2234  fWriteoutBufferMap[branchName] = buffer;
2235  } else {
2236  fLogger->Warning(MESSAGE_ORIGIN, "Branch %s is already registered in WriteoutBufferMap", branchName.Data());
2237  delete buffer;
2238  }
2239  return fWriteoutBufferMap[branchName];
2240 }
2241 //_____________________________________________________________________________
2242 
2243 //_____________________________________________________________________________
2245 {
2246  if (fWriteoutBufferMap.count(branchName) > 0) {
2247  return fWriteoutBufferMap[branchName];
2248  } else {
2249  return 0;
2250  }
2251 }
2252 //_____________________________________________________________________________
2253 
2254 //_____________________________________________________________________________
2256 {
2257  for(std::map<TString, FairWriteoutBuffer*>::const_iterator iter = fWriteoutBufferMap.begin(); iter != fWriteoutBufferMap.end(); iter++) {
2258  iter->second->WriteOutData(eventTime);
2259  }
2260 }
2261 //_____________________________________________________________________________
2262 
2263 //_____________________________________________________________________________
2265 {
2266  Bool_t dataInBuffer = kFALSE;
2267  for(std::map<TString, FairWriteoutBuffer*>::const_iterator iter = fWriteoutBufferMap.begin(); iter != fWriteoutBufferMap.end(); iter++) {
2268  if (iter->second->GetNData() > 0) {
2269  dataInBuffer = kTRUE;
2270  }
2271  iter->second->WriteOutAllData();
2272  }
2273  fFillLastData = dataInBuffer;
2274 }
2275 //_____________________________________________________________________________
2276 
2277 //_____________________________________________________________________________
2279 {
2280  for(std::map<TString, FairWriteoutBuffer*>::const_iterator iter = fWriteoutBufferMap.begin(); iter != fWriteoutBufferMap.end(); iter++) {
2281  iter->second->DeleteOldData();
2282  }
2283 }
2284 //_____________________________________________________________________________
2285 
2287 
2288 
2289 
2290 
2291 
2292 
2293 
2294