// ESAF : Euso Simulation and Analysis Framework
// $Id: PixelMapBuilder.cc,v 1.14 2005/04/25 12:03:10 thea Exp $
// A.Thea created Apr, 15 2004
#include "PixelMapBuilder.hh"
#include "EusoDetector.hh"
#include "DetectorTransportManager.hh"
#include "EusoElectronics.hh"
#include "OAPxBunch.hh"
#include "Photon.hh"
#include "ParentPhoton.hh"
#include "TTree.h"
#include "TTimeStamp.h"
#include "TString.h"
ClassImp(PixelMapBuilder)
//______________________________________________________________________________
PixelMapBuilder::PixelMapBuilder() : fRootFile(0), fTree(0), fBunch(0){
// ctor
// FIXME: in the current configuration detector has already been build.
// When simuframework is gonna be implemented EusoDetector should be build
// here or in OpticaAnalysisModule;
fDetector = new EusoDetector();
fDetectorTransporter = fDetector->GetDetectorTransportManager();
fElectronics = fDetector->GetEusoElectronics();
fNumPhotons = (Long_t)Conf()->GetNum("PixelMapBuilder.fNumPhotons");
fPhPerBunch = (Long_t)Conf()->GetNum("PixelMapBuilder.fPhPerBunch");
fRootFileName = Conf()->GetStr("PixelMapBuilder.fRootFileName");
fNumPixels = fElectronics->NumOfCh();
}
//______________________________________________________________________________
PixelMapBuilder::~PixelMapBuilder() {
// dtor
}
//______________________________________________________________________________
Bool_t PixelMapBuilder::CloseRoot() {
// close root file
if ( fRootFile ) {
fRootFile->Write();
fRootFile->Close();
Msg(EsafMsg::Info) << "Root file closedn";
SafeDelete( fRootFile )
// FIXME: is it correct? However, it should be safe
fTree = 0;
}
return kTRUE;
}
//______________________________________________________________________________
Bool_t PixelMapBuilder::OpenRoot() {
// open rootfile with the correct name
CloseRoot();
Msg(EsafMsg::Info) << "Opening root file " << fRootFileName << MsgDispatch;
fRootFile = new TFile((fRootFileName+".root").c_str(),"CREATE");
if (fRootFile->IsZombie()) {
Msg(EsafMsg::Warning) << MsgDispatch;
Msg(EsafMsg::Warning) << "Error opening file " << fRootFileName << MsgDispatch;
Msg(EsafMsg::Warning) << "Probably it already exists or there is no " << MsgDispatch;
Msg(EsafMsg::Warning) << "write access permission to this directory" << MsgDispatch;
Msg(EsafMsg::Warning) << MsgDispatch;
// try adding date and time to the name
TTimeStamp time;
TString now = time.AsString("s");
now[now.Last(' ')]=':';
TString newFileName = Form("%s-", fRootFileName.c_str())+now+".root";
fRootFile = new TFile(newFileName,"CREATE");
if (fRootFile->IsZombie()) {
MsgForm(EsafMsg::Warning,"Cannot open root file");
return kFALSE;
}
}
Msg(EsafMsg::Info) << "Root file " << fRootFile->GetName() << " successfully openedn" << MsgDispatch;
;
return kTRUE;
}
//______________________________________________________________________________
void PixelMapBuilder::Go() {
// build the map
// disable electronics simulation, just find the photon's pad
fElectronics->EnableSimulation(kFALSE);
InitPxTree();
Msg(EsafMsg::Info) << "Photons to track: " << fNumPhotons << MsgDispatch;
Msg(EsafMsg::Info) << "Bunches to fill: " << fNumPhotons/fPhPerBunch+1 << MsgDispatch;
Msg(EsafMsg::Info) << "Each Bunch will contain " << fPhPerBunch << " photons." << MsgDispatch;
Msg(EsafMsg::Info) << "PixelMapBuilder: tracing..." << MsgDispatch;
Long_t hits = 0;
for(Long_t i(0); i<fNumPhotons; i++) {
Photon *p = fPupil.Get();
fDetectorTransporter->Transport(p);
if ( p->pixelUid > 0 ) { // FIXME: should be > 0
fBunch->AddHit(p->parent->Dir(), p->wl, p->pixelUid);
hits++;
if ( fPhPerBunch && (hits%fPhPerBunch)*100%fPhPerBunch == 0 ) {
Msg(EsafMsg::Info) <<'r'<< MsgFlush;
Msg(EsafMsg::Info) << Form("Current Bunch filled at %d%%, Overall %ld%%",
(hits%fPhPerBunch)*100/fPhPerBunch, i*100/fNumPhotons ) << MsgFlush;
}
if ( fPhPerBunch > 0 && (hits % fPhPerBunch) == 0 ) {
Msg(EsafMsg::Info) << 'r' << " " << 'r' << MsgFlush;
// move in a separate method
FillPxTree();
}
}
}
if ((hits % fPhPerBunch) != 0) FillPxTree();
ClosePxTree();
MsgForm(EsafMsg::Info, "%d photons traced.",fNumPhotons);
MsgForm(EsafMsg::Info, "%d photons hit the pixels.",hits);
// FIXME: should be false if it was before Go()
fElectronics->EnableSimulation(kTRUE);
}
//______________________________________________________________________________
void PixelMapBuilder::InitPxTree() {
// creates a new tree and a new photon container
fBunch = new OAPxBunch(fNumPixels);
fTree = new TTree("oapxtree","OAPxTree");
fTree->Branch("hitbunches","OAPxBunch", &fBunch);
}
//______________________________________________________________________________
void PixelMapBuilder::FillPxTree() {
// calls fTree->Fill() and then clears the photon container
fTree->Fill();
fBunch->Clear();
MsgForm(EsafMsg::Info,"Event %d saved",fTree->GetEntriesFast());
// check rootfile
if ( fRootFile != fTree->GetCurrentFile() ) {
fRootFile = fTree->GetCurrentFile();
MsgForm(EsafMsg::Info, "RootFile capacity limit reached. Moving to %s",
fRootFile->GetName());
}
}
//______________________________________________________________________________
void PixelMapBuilder::ClosePxTree() {
MsgForm(EsafMsg::Info,"Saving rootfile");
CloseRoot();
delete fTree; fTree = 0;
delete fBunch; fBunch = 0;
}