// ESAF : Euso Simulation and Analysis Framework
// $Id: ElecTestDetTransManager.cc,v 1.22 2005/05/15 13:19:31 thea Exp $
// M. Pallavicini created May, 12 2003
#include "ElecTestDetTransManager.hh"
#include "Photon.hh"
#include "EusoDetector.hh"
#include "EusoElectronics.hh"
#include "MacroCell.hh"
#include "Photomultiplier.hh"
#include "EDetectorPhotonAdder.hh"
#include "PhotonsOnPupil.hh"
#include "ConfigFileParser.hh"
#include "Config.hh"
#include "TMath.h"
ClassImp(ElecTestDetTransManager)
// ctor
ElecTestDetTransManager::ElecTestDetTransManager()
: DetectorTransportManager() {
// total number of photons generated
fNumPhotons = (Int_t)Conf()->GetNum("ElecTestDetTransManager.fNumPhotons");
// starting pixel
fMacroCellId = (Int_t)Conf()->GetNum("ElecTestDetTransManager.fMacrocellId");
fPmtId = (Int_t)Conf()->GetNum("ElecTestDetTransManager.fPmtId");
fChanId = (Int_t)Conf()->GetNum("ElecTestDetTransManager.fChanId");
fUniqueChanId = (Int_t)Conf()->GetNum("ElecTestDetTransManager.fUniqueChanId");
// start and end time
fStartTime = Conf()->GetNum("ElecTestDetTransManager.fStartTime");
fEndTime = Conf()->GetNum("ElecTestDetTransManager.fEndTime");
if ( fStartTime > fEndTime )
Msg(EsafMsg::Panic) << "In ElecTestDetTransManager must be EndTime > StartTime " << MsgDispatch;
// if not zero, number of pixel
// positive: row
// negative: column
fTrackLength = (Int_t)Conf()->GetNum("ElecTestDetTransManager.fTrackLength");
if ( fTrackLength == 0 )
Msg(EsafMsg::Panic) << "You must specify a not null track length" << MsgDispatch;
// hole in the track
fHole = Conf()->GetBool("ElecTestDetTransManager.fHole");
fHolePosition = (Int_t)Conf()->GetNum("ElecTestDetTransManager.fHolePosition");
if ( fHole && (fHolePosition <=1 || fHolePosition >= TMath::Abs(fTrackLength)) )
Msg(EsafMsg::Panic) << "In ElecTestDetTransManager fHolePosition is not valid." << MsgDispatch;
}
// dtor
ElecTestDetTransManager::~ElecTestDetTransManager() {
}
// fill electronics ignoring photons
void ElecTestDetTransManager::Go( PhotonsOnPupil* photons ) {
if ( Conf()->GetBool("ElecTestDetTransManager.fBackgroundStudy") ) {
BackgroundStudy();
return;
}
if ( Conf()->GetBool("ElecTestDetTransManager.fTestSinglePMT") ) {
SinglePmt();
return;
}
MacroCell *cell = 0;
Photomultiplier *pmt = 0;
Int_t ch = 0;
if ( fUniqueChanId > 0 ) {
GetEusoDetector()->GetEusoElectronics()->ElectronicsMap(&cell,&pmt,ch,fUniqueChanId);
}
else {
cell = GetEusoDetector()->GetEusoElectronics()->Cell( fMacroCellId-1 );
if ( cell ) {
if ( fPmtId < ((Int_t)(cell->Pmts()->size())) ) {
pmt = cell->GetPmt( fPmtId-1 );
}
else {
MsgForm(EsafMsg::Panic, "Wrong pmt identifier: Macrocell %d has less than %d pmts",
fMacroCellId, fPmtId);
}
}
ch = fChanId-1;
}
// generate the required number of photons equally distributed in time
// in the required time interval
Double_t start = fStartTime;
ConfigFileParser *pConfig = Config::Get()->GetCF("Electronics","MacroCell");
Double_t gtu = pConfig->GetNum("MacroCell.GtuTimeLength");
Int_t chstep = 0;
Int_t chint = 1;
Int_t counter = 0;
Int_t trackcounter = 0;
if ( fTrackLength ) {
if ( fTrackLength > 0 ) {
chstep = 1;
chint = fNumPhotons / fTrackLength;
if ( fHole ) chint = fNumPhotons / (fTrackLength-1);
counter = ch % pmt->Geometry()->Rows();
}
else {
chstep = pmt->Geometry()->Rows();
chint = - fNumPhotons / fTrackLength;
if ( fHole ) chint = - fNumPhotons / (fTrackLength-1);
counter = ch / pmt->Geometry()->Rows();
}
} else {
Msg(EsafMsg::Panic) << "You must specify a not null track length" << MsgDispatch;
}
Double_t step = gtu/chint;
#ifdef DEBUG
Msg(EsafMsg::Debug) << "Rows: " << cell->Rows() << MsgDispatch;
Msg(EsafMsg::Debug) << "Columns: " << cell->Columns() << MsgDispatch;
Msg(EsafMsg::Debug) << "Chips: " << cell->Chips() << MsgDispatch;
Msg(EsafMsg::Debug) << "Step: " << step << MsgDispatch;
Msg(EsafMsg::Debug) << "ChStep: " << chstep << MsgDispatch;
Msg(EsafMsg::Debug) << "ChInt: " << chint << MsgDispatch;
#endif /* DEBUG */
for(Int_t i=0; i < TMath::Abs(fTrackLength)*chint; i++) {
if ( fHole && trackcounter == (fHolePosition-1) ) {
ch += chstep;
start += gtu;
counter++;
trackcounter++;
if ( trackcounter == TMath::Abs(fTrackLength) ) return;
}
if ( counter < (pmt->Geometry()->Rows()) ) {
pmt->AddTest( start, ch ); // use a special add function of the PMT
start += step;
if ( (i % chint) == chint-1 ) {
ch += chstep;
counter++;
trackcounter++;
if ( trackcounter == TMath::Abs(fTrackLength) ) return;
}
}
if ( counter >= (pmt->Geometry()->Rows()) ) {
pair<Int_t, Int_t> rcEnd = cell->GetRowColUniqueId(pmt->GetUniqueId(ch-chstep));
pair<Int_t, Int_t> rcN = cell->GetRowColUniqueId(pmt->GetUniqueId(ch-chstep*2));
Int_t new_row = rcEnd.first;
Int_t new_col = rcEnd.second;
Int_t step_row(0), step_col(0);
if ((rcEnd.first-rcN.first)) step_row = TMath::Sign(1,(rcEnd.first-rcN.first));
if ((rcEnd.second-rcN.second)) step_col = TMath::Sign(1,(rcEnd.second-rcN.second));
Photomultiplier *pmt_new = 0;
MacroCell *cell_new = 0;
Int_t kk(0);
while( (!pmt_new || pmt_new->Id()==pmt->Id()) && kk<12 ) {
new_row += step_row;
new_col += step_col;
if ( new_row > (cell->GetRows()*pmt->Geometry()->Rows()) || new_row < 1 ||
new_col > (cell->GetColumns()*pmt->Geometry()->Rows()) || new_col < 1 ) {
Msg(EsafMsg::Debug) << "New row and/or column not valid. Stop" << MsgDispatch;
return;
}
if (cell->GetUniqueIdRowCol(new_row,new_col))
GetEusoDetector()->GetEusoElectronics()->ElectronicsMap(&cell_new, &pmt_new, ch, cell->GetUniqueIdRowCol(new_row,new_col));
kk++;
}
if ( !cell_new || cell_new->Id() != cell->Id() ) {
Msg(EsafMsg::Debug) << "Reached end of macrocell. Stop." << MsgDispatch;
return;
}
//cout << new_row << " " << new_col << " numpmts " << cell->Pmts()->size() << endl;
if (pmt_new && pmt_new->Id()!=pmt->Id()) {
//cout << pmt->Id() << " " << pmt_new->Id() << endl;
pmt = pmt_new;
fChanId = ch + 1;
counter = 0;
} else {
Msg(EsafMsg::Debug) << "Not found new pmt. Stop." << MsgDispatch;
return;
}
}
}
}
void ElecTestDetTransManager::SinglePmt() {
MacroCell *cell=0;
Photomultiplier *pmt=0;
Int_t ch;
if ( fUniqueChanId > 0 ) {
GetEusoDetector()->GetEusoElectronics()->ElectronicsMap(&cell,&pmt,ch,fUniqueChanId);
}
else {
cell = GetEusoDetector()->GetEusoElectronics()->Cell( fMacroCellId-1 );
if ( cell ) {
if ( fPmtId < ((Int_t)(cell->Pmts()->size())) ) {
pmt = cell->GetPmt( fPmtId-1 );
}
else {
MsgForm(EsafMsg::Panic, "ElecTestDetTransManager: Wrong pmt identifier: Macrocell %d has less than %d pmts",
fMacroCellId, fPmtId);
}
}
ch = fChanId-1;
}
//ConfigFileParser *pConfig = Config::Get()->GetCF("Electronics","MacroCell");
//Double_t gtu = pConfig->GetNum("MacroCell.GtuTimeLength");
// generate the required number of photons equally distributed in time
// in the required time interval
Double_t start = fStartTime;
Double_t step = (fEndTime-fStartTime)/fNumPhotons;
Int_t chstep = 0;
Int_t chint = 1;
if ( fTrackLength ) {
if ( fTrackLength > 0 ) {
chstep = 1;
chint = fNumPhotons / fTrackLength;
}
else {
chstep = pmt->Geometry()->Rows();
chint = - fNumPhotons / fTrackLength;
}
}
//Double_t step = gtu/chint;
Msg(EsafMsg::Debug) << "Step: " << step << MsgDispatch;
Msg(EsafMsg::Debug) << "ChStep: " << chstep << MsgDispatch;
Msg(EsafMsg::Debug) << "ChInt: " << chint << MsgDispatch;
for(Int_t i=0; i < TMath::Abs(fTrackLength)*chint; i++) {
pmt->AddTest( start, ch ); // use a special add function of the PMT
// pmt->AddTest( start, ch+8 ); // use a special add function of the PMT
start += step;
if (( i % chint ) == (chint-1))
ch += chstep;
}
}
void ElecTestDetTransManager::BackgroundStudy() {
//
// method for studying background olnly. To be used with background enabled
//
ConfigFileParser *pElConfig = Config::Get()->GetCF("Electronics","EusoElectronics");
if ( pElConfig->GetStr(string("EusoElectronics.fNightGlow")) == "none" )
Msg(EsafMsg::Warning) << "You are studying background but background is not enabled in EusoElectronics.cfg" << MsgDispatch;
MacroCell *cell=0;
Photomultiplier *pmt=0;
Int_t ch;
if ( fUniqueChanId > 0 ) {
GetEusoDetector()->GetEusoElectronics()->ElectronicsMap(&cell,&pmt,ch,fUniqueChanId);
}
else {
cell = GetEusoDetector()->GetEusoElectronics()->Cell( fMacroCellId-1 );
if ( cell ) {
if ( fPmtId < ((Int_t)(cell->Pmts()->size())) ) {
pmt = cell->GetPmt( fPmtId-1 );
}
else {
MsgForm(EsafMsg::Panic, "ElecTestDetTransManager: Wrong pmt identifier: Macrocell %d has less than %d pmts",
fMacroCellId, fPmtId);
}
}
ch = fChanId-1;
}
ConfigFileParser *pConfig = Config::Get()->GetCF("Electronics","MacroCell");
Double_t gtu = pConfig->GetNum("MacroCell.GtuTimeLength");
Double_t start = fStartTime;
//Double_t step = (fEndTime-fStartTime)/fNumPhotons;
Int_t numgtu = (Int_t)((fEndTime-fStartTime)/gtu);
Int_t numphgtu = (Int_t) (fNumPhotons / numgtu);
for(Int_t i(0); i<numgtu; i++) {
for(Int_t j(0); j<numphgtu; j++ ) {
pmt->AddTest(start, ch);
}
start += gtu;
fPmtId++;
if ( fPmtId >= ((Int_t)(cell->Pmts()->size())) ) fPmtId = 1;
pmt = cell->GetPmt( fPmtId-1 );
}
}