// ------------------------------------------------------------------------- // ----- FairRunOnline source file ----- // ----- Created 06/01/04 by M. Al-Turany ----- // ------------------------------------------------------------------------- #include "FairRunOnline.h" #include "FairRootManager.h" #include "FairTask.h" #include "FairBaseParSet.h" #include "FairEventHeader.h" #include "FairFieldFactory.h" #include "FairRuntimeDb.h" #include "FairRunIdGenerator.h" #include "FairLogger.h" #include "FairFileHeader.h" #include "FairParIo.h" #include "FairField.h" #include "FairSource.h" #include "FairGeoInterface.h" #include "FairGeoLoader.h" #include "FairGeoParSet.h" #include "TROOT.h" #include "TTree.h" #include "TSeqCollection.h" #include "TGeoManager.h" #include "TKey.h" #include "TF1.h" #include "TSystem.h" #include "TFolder.h" #include "TH1F.h" #include "TH2F.h" #include #include #include using std::cout; using std::endl; using std::list; //_____________________________________________________________________________ FairRunOnline* FairRunOnline::fgRinstance = 0; //_____________________________________________________________________________ FairRunOnline* FairRunOnline::Instance() { return fgRinstance; } FairRunOnline::FairRunOnline() :FairRun(), fAutomaticFinish(kTRUE), fIsInitialized(kFALSE), fEvtHeader(0), fStatic(kFALSE), fField(0), fSource(NULL), fFolder(new TFolder("HISTO", "HISTO")), fGenerateHtml(kFALSE) { fgRinstance = this; fAna = kTRUE; LOG(INFO)<Delete(); delete fFolder; } } //_____________________________________________________________________________ //_____________________________________________________________________________ void FairRunOnline::Init() { LOG(INFO)<<"FairRunOnline::Init"<Fatal(MESSAGE_ORIGIN,"Error Init is already called before!"); exit(-1); } else { fIsInitialized = kTRUE; } // FairGeoLoader* loader = new FairGeoLoader("TGeo", "Geo Loader"); // FairGeoInterface* GeoInterFace = loader->getGeoInterface(); // GeoInterFace->SetNoOfSets(ListOfModules->GetEntries()); // GeoInterFace->setMediaFile(MatFname.Data()); // GeoInterFace->readMedia(); // Add a Generated run ID to the FairRunTimeDb FairRunIdGenerator genid; fRunId = genid.generateId(); fRtdb->addRun(fRunId); fFileHeader->SetRunId(fRunId); FairBaseParSet* par = dynamic_cast(fRtdb->getContainer("FairBaseParSet")); FairGeoParSet* geopar = dynamic_cast(fRtdb->getContainer("FairGeoParSet")); geopar->SetGeometry(gGeoManager); if(fField) { fField->Init(); fField->FillParContainer(); } TList* containerList = fRtdb->getListOfContainers(); TIter next(containerList); FairParSet* cont; TObjArray* ContList= new TObjArray(); while ((cont=dynamic_cast(next()))) { ContList->Add(new TObjString(cont->GetName())); } par->SetContListStr(ContList); par->setChanged(); par->setInputVersion(fRunId,1); geopar->setChanged(); geopar->setInputVersion(fRunId,1); fRootManager->WriteFileHeader(fFileHeader); fTask->SetParTask(); fRtdb->initContainers(fRunId); // InitContainers(); // --- Get event header from Run fEvtHeader = dynamic_cast (FairRunOnline::Instance()->GetEventHeader()); if ( ! fEvtHeader ) { LOG(FATAL) << "FairRunOnline::InitContainers:No event header in run!" << FairLogger::endl; return; } LOG(INFO) << "FairRunOnline::InitContainers: event header at " << fEvtHeader << FairLogger::endl; fRootManager->Register("EventHeader.", "Event", fEvtHeader, kTRUE); // Initialize the source if(! fSource->Init()) { LOG(INFO) << "FairRunOnline::Init()" << FairLogger::endl; LOG(FATAL) << "- initialization of data source failed!" << FairLogger::endl; } // Now call the User initialize for Tasks fTask->InitTask(); // create the output tree after tasks initialisation fOutFile->cd(); TTree* outTree =new TTree("cbmsim", "/cbmout", 99); fRootManager->TruncateBranchNames(outTree, "cbmout"); fRootManager->SetOutTree(outTree); fRootManager->WriteFolder(); } //_____________________________________________________________________________ //_____________________________________________________________________________ void FairRunOnline::InitContainers() { fRtdb = GetRuntimeDb(); FairBaseParSet* par=(FairBaseParSet*) (fRtdb->getContainer("FairBaseParSet")); LOG(INFO)<GetObjectFromInTree("EventHeader."); fRunId = fEvtHeader->GetRunId(); //Copy the Event Header Info to Output fEvtHeader->Register(); // Init the containers in Tasks fRtdb->initContainers(fRunId); fTask->ReInitTask(); // fTask->SetParTask(); fRtdb->initContainers( fRunId ); // if (gGeoManager==0) { // par->GetGeometry(); // } } else { // --- Get event header from Run fEvtHeader = dynamic_cast (FairRunOnline::Instance()->GetEventHeader()); if ( ! fEvtHeader ) { LOG(FATAL) << "FairRunOnline::InitContainers:No event header in run!" << FairLogger::endl; return; } LOG(INFO) << "FairRunOnline::InitContainers: event header at " << fEvtHeader << FairLogger::endl; fRootManager->Register("EventHeader.", "Event", fEvtHeader, kTRUE); } } //_____________________________________________________________________________ Int_t FairRunOnline::EventLoop() { fSource->Reset(); Int_t status = fSource->ReadEvent(); if(1 == status) { return 1; } Int_t tmpId = GetEventHeader()->GetRunId(); if ( tmpId != fRunId ) { // LOG(INFO) << "Call Reinit due to changed RunID" << FairLogger::endl; // fRunId = tmpId; // Reinit( fRunId ); // fTask->ReInitTask(); } // LOG(ERROR) << "Event time: " << fRootManager->GetEventTime() << FairLogger::endl; // fRootManager->StoreWriteoutBufferData(fRootManager->GetEventTime()); // ????????? fTask->ExecuteTask(""); fRootManager->Fill(); // fRootManager->DeleteOldWriteoutBufferData(); // ????????? // fTask->FinishEvent(); return 0; } //_____________________________________________________________________________ void FairRunOnline::Run(Int_t nev, Int_t dummy) { fOutFile->cd(); Int_t status; if(nev < 0) { while(kTRUE) { status = EventLoop(); if ( 1 == status) { break; } } } else { for (Int_t i = 0; i < nev; i++) { status = EventLoop(); if ( 1 == status) { break; } } } fRootManager->StoreAllWriteoutBufferData(); if (fAutomaticFinish) { Finish(); } } //_____________________________________________________________________________ //_____________________________________________________________________________ void FairRunOnline::Finish() { fTask->FinishTask(); // fRootManager->LastFill(); fRootManager->Write(); fSource->Close(); fRootManager->CloseOutFile(); // WriteObjects(); if(fGenerateHtml) { GenerateHtml(); } } //_____________________________________________________________________________ void FairRunOnline::SetGenerateHtml(Bool_t flag) { fGenerateHtml = flag; } //_____________________________________________________________________________ //_____________________________________________________________________________ void FairRunOnline::GenerateHtml() { TString htmlName = TString(fOutname); TString rootName = TString(fOutname); Int_t last = htmlName.Last('/'); if(-1 == last) { htmlName = "index.html"; } else { htmlName.Remove(last+1, htmlName.Length()-last-1); htmlName += TString("index.html"); rootName.Remove(0, last+1); } ofstream* ofile = new ofstream(htmlName); (*ofile) << "" << endl << "" << endl << "" << endl << "" << endl << "Read a ROOT file in Javascript (Demonstration)" << endl << "" << endl << "" << endl << "" << endl << "" << endl << "" << endl << "
" << endl << "" << endl << "" << endl; ofile->close(); delete ofile; } //_____________________________________________________________________________ //_____________________________________________________________________________ void FairRunOnline::WriteObjects() { // Create iterator with the folder content TIter iter(fFolder->GetListOfFolders()); // Pointer to an object TObject* object; // Class name of the object TString className; // Histogram pointers TH1* h1; TH2* h2; // Loop over objects in the folder while((object = iter())) { // Get class name className = object->ClassName(); // Recognise objects if(0 == className.CompareTo("TH1F")) { // If a histogram - plot it and save canvas h1 = (TH1F*) object; h1->Write(); } else if(0 == className.CompareTo("TH2F")) { // If a histogram - plot it and save canvas h2 = (TH2F*) object; h2->Write(); } else if(0 == className.CompareTo("TH1D")) { // If a histogram - plot it and save canvas h1 = (TH1D*) object; h1->Write(); } else if(0 == className.CompareTo("TH2D")) { // If a histogram - plot it and save canvas h2 = (TH2D*) object; h2->Write(); } } } //_____________________________________________________________________________ //_____________________________________________________________________________ void FairRunOnline::Reinit(UInt_t runId) { // reinit procedure fRtdb->initContainers( runId ); } //_____________________________________________________________________________ //_____________________________________________________________________________ void FairRunOnline::SetContainerStatic(Bool_t tempBool) { fStatic=tempBool; if ( fStatic ) { fLogger->Info(MESSAGE_ORIGIN, "Parameter Cont. initialisation is static"); } else { fLogger->Info(MESSAGE_ORIGIN, "Parameter Cont. initialisation is NOT static"); } } //_____________________________________________________________________________ //_____________________________________________________________________________ void FairRunOnline::AddObject(TObject* object) { if(! fFolder) { return; } fFolder->Add(object); } //_____________________________________________________________________________ ClassImp(FairRunOnline)