// ESAF : Euso Simulation and Analysis Framework
// $Id: RecoFramework.cc,v 1.25 2005/03/30 09:19:52 naumov Exp $
// Marco Pallavicini created Oct, 16 2003
#include "RecoFramework.hh"
#include "Config.hh"
#include "ModuleFactory.hh"
#include "RecoModule.hh"
#include "InputModule.hh"
#include "RecoEvent.hh"
#include "RecoRootEvent.hh"
#include "TruthModule.hh"
#include "EsafMsgDispatcher.hh"
#include <TTree.h>
#include <TFile.h>
#include <TObjectTable.h>
ClassImp(RecoFramework)
/*****************************************************************************
* ESAF: Euso Simulation and Analysis Framework *
* *
* Id: RecoFramework *
* Package: framework *
* Coordinator: Marco.Pallavicini *
* *
*****************************************************************************/
//_____________________________________________________________________________
// ctor
RecoFramework::RecoFramework() : EsafConfigurable(), EsafMsgSource(), fRootFile(NULL) {
EsafMsgDispatcher::Get()->LoadConfig();
// get file name with module list
string sName = Conf()->GetStr("RecoFramework.ModuleFile");
sName = "./config/Reco/"+sName;
// build factory
ModuleFactory factory( sName );
// get unique input module
fInputModule = factory.GetInputModule();
if ( fInputModule == NULL ) {
Msg(EsafMsg::Panic) << "Cannot run without input module"<< MsgDispatch;
}
// get modules
Int_t counter=0;
while ( RecoModule *aModule = factory.GetModule() ) {
Msg(EsafMsg::Info) << "Loading module : " << aModule->GetName() << MsgDispatch;
fModules.push_back(aModule);
counter++;
}
Msg(EsafMsg::Info) << "RecoFramework: " << counter << " modules loaded" << MsgDispatch;
fCurrentEvent = 0;
}
//_____________________________________________________________________________
// dtor
RecoFramework::~RecoFramework() {
}
//_____________________________________________________________________________
// parse a command line
void RecoFramework::ParseCommandLine(const int& argc, const char** argv) {
}
//_____________________________________________________________________________
// execute a complete run
void RecoFramework::Execute() {
string dumpRootEvent = Conf()->GetStr("RecoFramework.DumpRootEvent");
// init input module
fInputModule->Init();
// init all modules
for( Int_t i=0; i<(Int_t)fModules.size(); i++ ) {
if ( !fModules[i]->Init() ) {
Msg(EsafMsg::Panic) << "Module " << fModules[i]->GetName() << " failed" << MsgDispatch;
}
}
OpenRoot();
// run
while ( RecoEvent *anEvent = fInputModule->GetEvent() ) {
fCurrentEvent++;
// if (fCurrentEvent==0 || fCurrentEvent==10) gObjectTable->Print(); // FIXME to think
fRecoRootEvent->Clear();
if (fInputModule->GetName() == "Root")
fInputModule->SaveRootData(fRecoRootEvent);
if ( anEvent->GetHeader().GetNumActiveFee() != 0 ) {
// execute all modules
for( Int_t i=0; i<(Int_t)fModules.size(); i++) {
if ( ! fModules[i]->PreProcess() )
break;
if ( ! fModules[i]->Process( anEvent ) )
break;
if ( ! fModules[i]->PostProcess() )
break;
if ( ! fModules[i]->SaveRootData(fRecoRootEvent) )
break;
// write the module data in the event
anEvent->PutRecoModuleData(fModules[i]->GetName(), fModules[i]->GetData());
}
if (dumpRootEvent == "yes" ) fRecoRootEvent->Dump();
fRecoTree->Fill();
// clean up
fInputModule->DestroyEvent(); // delete input event
for( Int_t i=0; i<(Int_t)fModules.size(); i++) {
fModules[i]->UserMemoryClean(); // delete user objects
fModules[i]->StandardMemoryClean(); // delete Int_t and Double_t maps
}
}
else {
fInputModule->DestroyEvent();
}
} // end event loop
// end all modules
fInputModule->Done();
for( Int_t i=0; i<(Int_t)fModules.size(); i++) {
fModules[i]->Done();
}
CloseRoot();
}
//_____________________________________________________________________________
// dump the module list
void RecoFramework::Dump( ostream& os ) const {
Msg(EsafMsg::Debug) << "Framework Dump" << MsgDispatch;
Msg(EsafMsg::Debug) << "The following Modules where loaded:" << MsgDispatch;
Msg(EsafMsg::Debug) << "InputModule: " << fInputModule->GetName() << MsgDispatch;
for( Int_t i=0; i<(Int_t)fModules.size(); i++ ) {
Msg(EsafMsg::Debug) << "Module: " << i+1 << "t" << fModules[i]->GetName()<< MsgDispatch;
}
}
//_____________________________________________________________________________
// open output rootfile
void RecoFramework::OpenRoot() {
string filename;
filename = Form("output/reco.root");
fRootFile = new TFile(filename.c_str(), "CREATE");
if ( fRootFile->IsZombie() ) {
delete fRootFile;
// try adding date and time to the name
char timestr[100];
time_t tt=time(NULL);
strftime(timestr,99,"%d-%b-%G:%H:%M:%S",localtime(&tt));
filename = Form("output/reco.%s.root",timestr);
fRootFile = new TFile(filename.c_str(),"CREATE");
if (fRootFile->IsZombie())
throw runtime_error("Cannot open root file");
}
Msg(EsafMsg::Info) << "Root file " << filename << " successfully opened" << MsgDispatch;
fRecoTree = new TTree("recotree","RecoTree");
CreateRecoRootEvent();
fRecoTree->Branch("events","RecoRootEvent",&fRecoRootEvent);
}
//_____________________________________________________________________________
// Create RecoRootEvent
void RecoFramework::CreateRecoRootEvent() {
fRecoRootEvent = new RecoRootEvent();
}
//_____________________________________________________________________________
// close output rootfile
void RecoFramework::CloseRoot() {
if ( fRootFile ) {
// fRootFile->Write();
fRecoTree->Write();
fRootFile->Close();
delete fRootFile;
}
}