23 #include "TFriendElement.h"
24 #include "TObjArray.h"
26 #include "TCollection.h"
27 #include "TParticle.h"
29 #include "TGeoManager.h"
31 #include "TClonesArray.h"
33 #include "TChainElement.h"
71 fInChain( new TChain(
"cbmsim",
"/cbmroot")),
82 fBranchNameList(new TList()),
88 fCompressData(kFALSE),
90 fBranchPerMap(kFALSE),
97 fCheckInputBranches(),
102 fActualSignalIdentifier(0),
104 fSignalChainList(NULL),
105 fBackgroundChain(NULL),
106 fBackgroundFile(NULL),
117 fEventTimeInMCHeader(kTRUE),
124 fEvtHeaderIsNew(kFALSE),
125 fFillLastData(kFALSE),
129 Fatal(
"FairRootManager",
"Singleton instance already exists.");
169 TFile* SignalInFile =
new TFile(name.Data());
170 if (SignalInFile->IsZombie()) {
175 TChain* chain =
new TChain(
"cbmsim",
"/cbmroot");
180 chain->AddFile(name.Data());
184 CurrentChain->AddFile(name.Data());
185 TObjArray* fileElements=CurrentChain->GetListOfFiles();
216 if (name.IsNull() ) {
233 if (name.IsNull() ) {
236 TFile* BGFile =
new TFile(name);
237 if (BGFile->IsZombie()) {
271 fCbmroot= gROOT->GetRootFolder()->AddFolder(
"cbmroot",
"Main Folder");
291 TString chainName =
"BGInChain";
296 for(Int_t i =0; i< list->GetEntries(); i++) {
297 Obj=dynamic_cast <TObjString*> (list->At(i));
306 gROOT->GetListOfBrowsables()->Add(
fCbmroot);
316 std::map<UInt_t, TChain*>::const_iterator iter;
318 TChain* currentChain=iter->second;
321 TFile* ChainFirstFile = currentChain->GetFile();
352 fInChain =
new TChain(
"cbmsim",
"/cbmroot");
364 fCbmroot= gROOT->GetRootFolder()->AddFolder(
"cbmroot",
"Main Folder");
384 TList*
list= dynamic_cast <TList*> (
fInFile->Get(
"BranchList"));
385 TString chainName =
"InputChain";
390 for(Int_t i =0; i< list->GetEntries(); i++) {
391 Obj=dynamic_cast <TObjString*> (list->At(i));
400 gROOT->GetListOfBrowsables()->Add(
fCbmroot);
412 std::list<TString>::const_iterator iter;
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());
452 cout <<
"-I- FairRootManager::OpenInTree() Opening fInTree: \"" <<
fInTree->GetCurrentFile()->GetName() <<
"\"" << endl;
467 fCbmroot= gROOT->GetRootFolder()->AddFolder(
"cbmroot",
"Main Folder");
485 TList*
list= dynamic_cast <TList*> (
fInFile->Get(
"BranchList"));
486 TString chainName =
"InputChain";
491 for(Int_t i =0; i< list->GetEntries(); i++) {
492 Obj=dynamic_cast <TObjString*> (list->At(i));
501 gROOT->GetListOfBrowsables()->Add(
fCbmroot);
511 for ( Int_t iobj = 0 ; iobj <=
fNObj ; 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]);
521 Int_t nofEnt = (Int_t)
fInTree->GetEntries();
523 cout <<
"-I- FairRootManager::OpenInTree() Input TREE has " << nofEnt <<
" entries." << endl;
549 "The input consists out of the following trees and files:");
551 TObjArray* fileElements=
fInChain->GetListOfFiles();
552 TIter next(fileElements);
553 TChainElement* chEl=0;
554 while (( chEl=(TChainElement*)next() )) {
558 map< TString, TChain* >::iterator mapIterator;
561 TChain* chain = (TChain*)mapIterator->second;
563 fileElements=chain->GetListOfFiles();
564 TIter next1(fileElements);
566 while (( chEl=(TChainElement*)next1() )) {
577 std::multimap< TString, std::multimap<TString,TArrayI> >::iterator it1;
578 std::multimap<TString,TArrayI> map1;
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]);
597 std::list<TString>::iterator listit;
599 inputLevel = (*listit);
600 if ( !inputLevel.Contains(
"InputChain") ) {
603 if ( runid.size() != map1.size()) {
609 for ( it=map1.begin() ; it != map1.end(); it++ ) {
610 TArrayI bla = (*it).second;
611 if ( (bla[0] != runid[counter]) || (bla[1] != events[counter]) ) {
628 if (errorFlag == 1) {
630 for ( Int_t i=0; i<runid.size(); i++) {
634 for ( it=map1.begin() ; it != map1.end(); it++ ) {
635 TArrayI bla = (*it).second;
639 if (errorFlag == 2) {
641 for ( it=map1.begin() ; it != map1.end(); it++ ) {
642 TArrayI bla = (*it).second;
657 if (
it->second.empty() ==
false) {
669 if (
it->second.empty() ==
true) {
691 fCbmroot= gROOT->GetRootFolder()->AddFolder(
"cbmroot",
"Main Folder");
692 gROOT->GetListOfBrowsables()->Add(
fCbmroot);
694 fCbmout= gROOT->GetRootFolder()->AddFolder(
"cbmout",
"Main Output Folder");
695 gROOT->GetListOfBrowsables()->Add(
fCbmout);
708 cout <<
"FairRootManager::OpenOutFile(\"" << fname <<
"\")" << endl;
709 fOutFile =
new TFile(fname,
"recreate");
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);
730 f=(TFolder*)
fCbmroot->FindObjectAny(folderName);
732 folder=
fCbmroot->AddFolder(folderName,folderName);
737 f=(TFolder*)
fCbmout->FindObjectAny(folderName);
739 folder=
fCbmout->AddFolder(folderName,folderName);
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);
774 f=(TFolder*)
fCbmroot->FindObjectAny(Foldername);
776 folder=
fCbmroot->AddFolder(Foldername,Foldername);
781 f=(TFolder*)
fCbmout->FindObjectAny(Foldername);
783 folder=
fCbmout->AddFolder(Foldername,Foldername);
804 TClonesArray* outputArray;
808 std::queue<TClonesArray*> myQueue;
810 outputArray =
new TClonesArray(className);
814 Register(branchName, folderName, outputArray, toFile);
823 std::cout <<
"-E- FairRootManager::GetEmptyTClonesArray: Container deleted outside FairRootManager!" << std::endl;
837 std::cout <<
"-E- Branch: " << branchName <<
" not registered!" << std::endl;
875 TClonesArray* result = 0;
892 return ObjStr->GetString();
894 TString NotFound(
"Branch not found");
908 if(BrName==ObjStr->GetString()) {
935 fTSBufferMap[branchName]->SetStopFunction(stopFunction);
936 fTSBufferMap[branchName]->SetStartFunction(startFunction);
937 return fTSBufferMap[branchName]->GetData(startParameter, stopParameter);
945 if (
it->second->AllDataProcessed() == kFALSE &&
it->second->TimeOut() == kFALSE) {
1019 gGeoManager->Write();
1032 TFile* oldfile=gFile;
1033 TFile*
file=
new TFile(geofile,
"RECREATE");
1035 gGeoManager->Write();
1061 cout <<
"FairRootManager::ReadEvent(" << i <<
"): FROM THE TREE " <<
fInTree << endl;
1063 Int_t totEnt =
fInTree->GetEntries();
1065 cout <<
"FairRootManager::ReadEvent(" << i <<
"): The tree has " << totEnt <<
" entries" << endl;
1085 Int_t totEnt =
fInChain->GetEntries();
1121 Double_t SBratio=gRandom->Uniform(0,1);
1122 Bool_t GetASignal=kFALSE;
1125 std::map<UInt_t, Double_t>::const_iterator iterN;
1129 ratio+=iterN->second;
1136 ratio=iterN->second;
1138 if(SBratio <=ratio) {
1141 chain->GetEntry(entry);
1182 Bool_t readentry=kFALSE;
1196 Obj =
fCbmout->FindObjectAny(BrName);
1214 Obj=
fCbmroot->FindObjectAny(BrName);
1239 Obj=
fCbmroot->FindObjectAny(BrName);
1254 Int_t fileId = link.
GetFile();
1268 }
else if (fileId == 0) {
1274 if (dataTree == 0) {
1282 TBranch* dataBranch = 0;
1288 }
else if (fileId < 0) {
1295 if (dataBranch == 0) {
1300 if (entryNr < dataBranch->GetEntries()) {
1301 dataBranch->GetEntry(entryNr);
1317 if (index < dataArray->GetEntriesFast()) {
1319 result = dataArray->At(index)->Clone();
1324 dataBranch->GetEntry(oldEntryNr);
1333 TClonesArray* result = 0;
1336 Int_t fileId = link.
GetFile();
1350 }
else if (fileId == 0) {
1356 if (dataTree == 0) {
1364 TBranch* dataBranch = 0;
1370 }
else if (fileId < 0) {
1377 if (dataBranch == 0) {
1382 if (entryNr < dataBranch->GetEntries()) {
1383 dataBranch->GetEntry(entryNr);
1399 dataBranch->GetEntry(oldEntryNr);
1407 TFolder* detf= NULL;
1410 TString lname(detname);
1412 TFolder* froot =
dynamic_cast<TFolder*
> (gROOT->FindObject(
"cbmroot"));
1414 detf =
dynamic_cast<TFolder*
> (froot->FindObjectAny( detname ));
1419 lgeo =
dynamic_cast<TList*
> (detf->FindObjectAny( lname.Data() ));
1424 node =
dynamic_cast<FairGeoNode*
> (lgeo->FindObject(gname));
1445 TFolder* cbm=(TFolder*)gROOT->FindObjectAny(folderName);
1447 TCollection* lf=cbm->GetListOfFolders();
1448 TIterator* iter= lf->MakeIterator();
1449 TObjArray* Br= fTree->GetListOfBranches();
1450 TIterator* BrIter= Br->MakeIterator();
1455 while((obj=iter->Next())) {
1462 TString ffn=cbm->GetName();
1464 ffn=ffn+obj->GetName();
1475 while((BrObj=BrIter->Next())) {
1476 TBranch* b=(TBranch*)BrObj;
1488 while((obj=iter->Next())) {
1490 TString ffn=
".FairMCEventHeader";
1493 while((BrObj=BrIter->Next())) {
1494 TBranch* b=(TBranch*)BrObj;
1500 while((obj=iter->Next())) {
1502 TString ffn=
".FairEventHeader";
1505 while((BrObj=BrIter->Next())) {
1506 TBranch* b=(TBranch*)BrObj;
1527 TString nn= b->GetName();
1529 nn.ReplaceAll(ffn.Data(),
"");
1532 b->SetName(nn.Data());
1533 TObjArray* Br= b->GetListOfBranches();
1534 TIterator* BrIter= Br->MakeIterator();
1537 while((BrObj=BrIter->Next())) {
1538 TBranch* bb=(TBranch*)BrObj;
1566 for(Int_t
t=0;
t<list->GetEntries();
t++) {
1596 for(Int_t i=0; i<
fListFolder.GetEntriesFast(); i++) {
1599 if (
fObj2[fNObj] ) {
1623 std::map<UInt_t, TChain*>::const_iterator iter;
1626 TChain* currentChain=iter->second;
1628 currentChain->SetBranchStatus(BrName,1);
1629 currentChain->SetBranchAddress(BrName,&
fObj2[fNObj]);
1636 fInTree->SetBranchStatus (BrName,1);
1639 fInChain->SetBranchStatus(BrName,1);
1657 for(Int_t i=0; i<
fListFolder.GetEntriesFast(); i++) {
1673 fInTree->SetBranchStatus (BrName,1);
1687 map < TString, TObject*>::iterator
p;
1688 TString BrName=fName;
1689 p=
fMap.find(BrName);
1692 fMap.insert(pair<TString, TObject*> (BrName, pObj));
1707 TClonesArray* output = (TClonesArray*)
GetObject(branchName);
1711 output->AbsorbObjects(input, 0, input->GetEntries() - 1);
1717 Int_t returnvalue=0;
1720 Obj1 =
fCbmroot->FindObjectAny(BrName);
1723 Obj1 =
fCbmout->FindObjectAny(BrName);
1726 for(Int_t i=0; i<
fListFolder.GetEntriesFast(); i++) {
1728 TFolder* fold =
dynamic_cast<TFolder*
> (
fListFolder.At(i));
1730 Obj1= fold->FindObjectAny(BrName);
1741 }
else if(Obj2!=0) {
1763 TString BrName=name->GetString();
1775 TString BrName=fName;
1776 map < TString, TObject*>::iterator
p;
1777 p=
fMap.find(BrName);
1821 std::list<TString> friendList;
1822 std::list<TString>::iterator iter1;
1825 if (find(friendList.begin(), friendList.end(), (*iter1))
1826 == friendList.end()) {
1827 friendList.push_back(*iter1);
1833 TFile* temp = gFile;
1835 Int_t friendType = 1;
1837 for(iter1 = friendList.begin();
1838 iter1 != friendList.end(); iter1++) {
1839 std::list<TString>::iterator iter;
1846 Bool_t inputLevelFound = kFALSE;
1849 inputLevel = (*iter);
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());
1860 inputLevelFound = kTRUE;
1866 if (!inputLevelFound) {
1867 inputLevel= Form(
"FriendTree_%i",friendType);
1873 chain->AddFile((*iter1), 1234567890,
"cbmsim");
1883 map< TString, TChain* >::iterator mapIterator;
1887 TChain* chain = (TChain*)mapIterator->second;
1901 TFile* temp = gFile;
1902 TFile* f =
new TFile(inputFile);
1904 TFolder* added=NULL;
1905 TString folderName =
"/cbmout";
1906 TString folderName1 =
"cbmout";
1907 added = dynamic_cast <TFolder*> (f->Get(
"cbmout"));
1909 folderName =
"/cbmroot";
1910 folderName1 =
"cbmroot";
1911 added = dynamic_cast <TFolder*> (f->Get(
"cbmroot"));
1913 folderName1=folderName1+
"_"+inputLevel;
1914 added->SetName(folderName1);
1918 TList*
list= dynamic_cast <TList*> (f->Get(
"BranchList"));
1919 TString chainName = inputLevel;
1924 for(Int_t i =0; i< list->GetEntries(); i++) {
1925 Obj=dynamic_cast <TObjString*> (list->At(i));
1934 TChain* chain =
new TChain(inputLevel,folderName);
1948 std::set<TString> branches;
1949 list<TString>::const_iterator iter;
1952 branches.insert(*iter);
1965 set<TString>::iterator iter1;
1966 TList*
list= dynamic_cast <TList*> (fileHandle->Get(
"BranchList"));
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);
1985 if (branches.size() != 0 ) {
1998 TFile* fileHandle =
new TFile(fileName);
2000 TTree* testTree =
dynamic_cast<TTree*
>(fileHandle->Get(
"cbmsim"));
2001 TFolder* folder =
dynamic_cast<TFolder*
>(fileHandle->Get(
"cbmroot"));
2003 folder =
dynamic_cast<TFolder*
>(fileHandle->Get(
"cbmout"));
2020 testTree->SetBranchAddress(
"EventHeader.", &header);
2022 Int_t entries = (Int_t) testTree->GetEntriesFast();
2025 TArrayI runidInfo(2);
2027 testTree->GetEntry(0);
2032 runidInfo.AddAt(runid,0);
2034 std::map<TString, std::multimap<TString,TArrayI> >::iterator
it;
2036 std::multimap<TString, TArrayI> myRunIdInfo;
2039 myRunIdInfo=it->second;
2042 for (Int_t i=1; i<entries ; i++) {
2043 testTree->GetEntry(i);
2045 if ( runid != runidInfo[0] ) {
2046 runidInfo.AddAt(counter,1);
2048 myRunIdInfo.insert(pair<TString,TArrayI>(fileName,runidInfo));
2051 runidInfo.AddAt(runid,0);
2057 runidInfo.AddAt(counter,1);
2058 myRunIdInfo.insert(pair<TString,TArrayI>(fileName,runidInfo));
2060 fRunIdInfoAll.insert(pair<TString, std::multimap<TString,TArrayI> >
2061 (inputLevel, myRunIdInfo));
2063 fileHandle->Close();
2102 f->Write(
"FileHeader", TObject::kSingleKey);
2125 fTimeProb=
new TF1(
"TimeProb.", form.Data(), 0., mean*10);
2157 Double_t
value=1.0/background;
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);
2206 std::map<UInt_t, Double_t>::const_iterator iterN;
2209 MaxS=chain->GetEntries();
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);
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 );
2220 if(MaxEventNo==0 || MaxEventNo > localMax) {
2221 MaxEventNo=localMax;
2258 iter->second->WriteOutData(eventTime);
2266 Bool_t dataInBuffer = kFALSE;
2268 if (iter->second->GetNData() > 0) {
2269 dataInBuffer = kTRUE;
2271 iter->second->WriteOutAllData();
2281 iter->second->DeleteOldData();