1#include "GaudiKernel/AlgFactory.h"
2#include "GaudiKernel/Algorithm.h"
3#include "GaudiKernel/IDataProviderSvc.h"
4#include "GaudiKernel/MsgStream.h"
5#include "GaudiKernel/SmartDataPtr.h"
7#include "EventModel/Event.h"
8#include "EventModel/EventModel.h"
9#include "MdcRawEvent/MdcDigi.h"
10#include "RawEvent/DigiEvent.h"
12#include "RootCnvSvc/Util.h"
14#include "DigiRootData/DigiEvent.h"
16#include "TCollection.h"
17#include "TDirectory.h"
25#include "RootIO/IRootIoSvc.h"
49 StatusCode readDigiEvent();
52 StatusCode readMdcDigi();
64 std::string m_fileName;
66 StringArrayProperty m_fileList;
68 std::string m_treeName;
80 : Algorithm( name, pSvcLocator ) {
83 declareProperty(
"digiRootFile", m_fileName =
"" );
84 StringArrayProperty initList;
85 std::vector<std::string> initVec;
86 initVec.push_back(
"digicopy.root" );
87 initList.setValue( initVec );
88 declareProperty(
"digiRootFileList", m_fileList = initList );
91 declareProperty(
"digiTreeName", m_treeName =
"Rec" );
98 StatusCode sc = StatusCode::SUCCESS;
99 MsgStream log(
msgSvc(), name() );
105 if ( service(
"RootIoSvc", m_rootIoSvc,
true ).isFailure() )
107 log << MSG::INFO <<
"Couldn't find the RootIoSvc!" << endmsg;
108 log << MSG::INFO <<
"Event loop will not terminate gracefully" << endmsg;
116 TDirectory* saveDir = gDirectory;
118 m_digiTree =
new TChain( m_treeName.c_str() );
120 std::string emptyStr(
"" );
121 if ( m_fileName.compare( emptyStr ) != 0 )
123 TFile
f( m_fileName.c_str() );
126 log << MSG::ERROR <<
"ROOT file " << m_fileName.c_str()
127 <<
" could not be opened for reading." << endmsg;
128 return StatusCode::FAILURE;
131 m_digiTree->Add( m_fileName.c_str() );
132 log << MSG::INFO <<
"Opened file: " << m_fileName.c_str() << endmsg;
136 const std::vector<std::string> fileList = m_fileList.value();
137 std::vector<std::string>::const_iterator it;
138 std::vector<std::string>::const_iterator itend = fileList.end();
139 for ( it = fileList.begin(); it != itend; it++ )
141 std::string theFile = ( *it );
142 TFile
f( theFile.c_str() );
145 log << MSG::ERROR <<
"ROOT file " << theFile.c_str()
146 <<
" could not be opened for reading." << endmsg;
147 return StatusCode::FAILURE;
150 m_digiTree->Add( theFile.c_str() );
151 log << MSG::INFO <<
"Opened file: " << theFile.c_str() << endmsg;
156 m_digiTree->SetBranchAddress(
"DigiEvent", &m_digiEvt );
158 m_numEvents = m_digiTree->GetEntries();
162 m_rootIoSvc->setRootEvtMax( m_numEvents );
163 if ( !m_digiTree->GetIndex() ) m_digiTree->BuildIndex(
"m_runId",
"m_eventId" );
164 m_rootIoSvc->registerRootTree( m_digiTree );
176 MsgStream log(
msgSvc(), name() );
178 StatusCode sc = StatusCode::SUCCESS;
180 if ( m_digiEvt ) m_digiEvt->Clear();
182 static Int_t evtId = 0;
183 int readInd, numBytes;
184 std::pair<int, int> runEventPair =
185 ( m_rootIoSvc ) ? m_rootIoSvc->runEventPair() : std::pair<int, int>( -1, -1 );
187 if ( ( m_rootIoSvc ) && ( m_rootIoSvc->index() >= 0 ) ) { readInd = m_rootIoSvc->index(); }
188 else if ( ( m_rootIoSvc ) && ( runEventPair.first != -1 ) && ( runEventPair.second != -1 ) )
190 int run = runEventPair.first;
191 int evt = runEventPair.second;
192 readInd = m_digiTree->GetEntryNumberWithIndex( run, evt );
194 else { readInd = evtId; }
196 if ( readInd >= m_numEvents )
198 log << MSG::WARNING <<
"Requested index is out of bounds - no digi data loaded" << endmsg;
199 return StatusCode::SUCCESS;
202 numBytes = m_digiTree->GetEvent( readInd );
204 if ( ( numBytes <= 0 ) || ( !m_digiEvt ) )
206 log << MSG::WARNING <<
"Failed to load digi event" << endmsg;
207 return StatusCode::SUCCESS;
210 sc = readDigiEvent();
211 if ( sc.isFailure() )
213 log << MSG::ERROR <<
"Failed to read top level DigiEvent" << endmsg;
218 if ( sc.isFailure() )
220 log << MSG::ERROR <<
"Failed to load MdcDigi" << endmsg;
228StatusCode digiRootReaderAlg::readDigiEvent() {
230 MsgStream log(
msgSvc(), name() );
232 StatusCode sc = StatusCode::SUCCESS;
235 SmartDataPtr<Event::EventHeader> evt( eventSvc(), EventModel::EventHeader );
238 log << MSG::ERROR <<
"Failed to retrieve Event" << endmsg;
239 return StatusCode::FAILURE;
242 unsigned int eventIdTds = evt->eventNumber();
243 unsigned int runIdTds = evt->runNumber();
245 unsigned int eventIdRoot = m_digiEvt->getEventId();
246 unsigned int runIdRoot = m_digiEvt->getRunId();
249 if ( eventIdTds != eventIdRoot ) evt->setEventNumber( eventIdRoot );
250 if ( runIdTds != runIdRoot ) evt->setRunNumber( runIdRoot );
252 Event::DigiEvent* digiEventTds =
258 if ( sc.isFailure() )
260 log << MSG::ERROR <<
"could not register "
267 bool fromMc = m_digiEvt->getFromMc();
268 digiEventTds->initialize( fromMc );
273StatusCode digiRootReaderAlg::readMdcDigi() {
274 MsgStream log(
msgSvc(), name() );
276 StatusCode sc = StatusCode::SUCCESS;
277 const TObjArray* mdcDigiRootCol = m_digiEvt->getMdcDigiCol();
278 if ( !mdcDigiRootCol )
return sc;
279 TIter mdcDigiIter( mdcDigiRootCol );
284 if ( sc.isFailure() )
286 log <<
"Failed to register MdcDigi Collection" << endmsg;
287 return StatusCode::FAILURE;
290 TMdcDigi* mdcDigiRoot = 0;
291 while ( ( mdcDigiRoot = (TMdcDigi*)mdcDigiIter.Next() ) != 0 ) { mdcDigiRoot->
Print(); }
296void digiRootReaderAlg::close() {
303 if ( m_digiTree )
delete m_digiTree;
309 StatusCode sc = StatusCode::SUCCESS;
TFile f("ana_bhabha660a_dqa_mcPat_zy_old.root")
ObjectVector< MdcDigi > MdcDigiCol
void Print(Option_t *option="") const
StatusCode execute()
Orchastrates reading from ROOT file and storing the data on the TDS for each event.
StatusCode finalize()
Closes the ROOT file and cleans up.
digiRootReaderAlg(const std::string &name, ISvcLocator *pSvcLocator)
StatusCode initialize()
Handles setup by opening ROOT file in read mode and creating a new TTree.
static int expandEnvVar(std::string *toExpand, const std::string &openDel=std::string("$("), const std::string &closeDel=std::string(")"))
const IAlgFactory & digiRootReaderAlgFactory
_EXTERN_ std::string Event
_EXTERN_ std::string MdcDigiCol