1#include "GaudiKernel/MsgStream.h"
2#include "GaudiKernel/AlgFactory.h"
3#include "GaudiKernel/IDataProviderSvc.h"
4#include "GaudiKernel/SmartDataPtr.h"
5#include "GaudiKernel/Algorithm.h"
7#include "EventModel/Event.h"
8#include "EventModel/EventModel.h"
9#include "RawEvent/DigiEvent.h"
10#include "MdcRawEvent/MdcDigi.h"
11#include "CgemRawEvent/CgemDigi.h"
13#include "RootCnvSvc/Util.h"
19#include "TDirectory.h"
21#include "TCollection.h"
22#include "DigiRootData/DigiEvent.h"
26#include "RootIO/IRootIoSvc.h"
53 StatusCode readDigiEvent();
57 StatusCode readMdcDigi();
58 StatusCode readCgemDigi();
70 std::string m_fileName;
72 StringArrayProperty m_fileList;
74 std::string m_treeName;
88Algorithm(name, pSvcLocator)
92 declareProperty(
"digiRootFile",m_fileName=
"");
93 StringArrayProperty initList;
94 std::vector<std::string> initVec;
95 initVec.push_back(
"digicopy.root");
96 initList.setValue(initVec);
97 declareProperty(
"digiRootFileList",m_fileList=initList);
100 declareProperty(
"digiTreeName", m_treeName=
"Rec");
109 StatusCode sc = StatusCode::SUCCESS;
110 MsgStream log(
msgSvc(), name());
116 if ( service(
"RootIoSvc", m_rootIoSvc,
true).isFailure() ){
117 log << MSG::INFO <<
"Couldn't find the RootIoSvc!" << endreq;
118 log << MSG::INFO <<
"Event loop will not terminate gracefully" << endreq;
126 TDirectory *saveDir = gDirectory;
128 m_digiTree =
new TChain(m_treeName.c_str());
130 std::string emptyStr(
"");
131 if (m_fileName.compare(emptyStr) != 0) {
132 TFile f(m_fileName.c_str());
134 log << MSG::ERROR <<
"ROOT file " << m_fileName.c_str()
135 <<
" could not be opened for reading." << endreq;
136 return StatusCode::FAILURE;
139 m_digiTree->Add(m_fileName.c_str());
140 log << MSG::INFO <<
"Opened file: " << m_fileName.c_str() << endreq;
142 const std::vector<std::string> fileList = m_fileList.value( );
143 std::vector<std::string>::const_iterator it;
144 std::vector<std::string>::const_iterator itend = fileList.end( );
145 for (it = fileList.begin(); it != itend; it++) {
146 std::string theFile = (*it);
147 TFile f(theFile.c_str());
149 log << MSG::ERROR <<
"ROOT file " << theFile.c_str()
150 <<
" could not be opened for reading." << endreq;
151 return StatusCode::FAILURE;
154 m_digiTree->Add(theFile.c_str());
155 log << MSG::INFO <<
"Opened file: " << theFile.c_str() << endreq;
161 m_digiTree->SetBranchAddress(
"DigiEvent", &m_digiEvt);
163 m_numEvents = m_digiTree->GetEntries();
167 if (!m_digiTree->GetIndex()) m_digiTree->BuildIndex(
"m_runId",
"m_eventId");
183 MsgStream log(
msgSvc(), name());
185 StatusCode sc = StatusCode::SUCCESS;
187 if (m_digiEvt) m_digiEvt->Clear();
189 static Int_t evtId = 0;
190 int readInd, numBytes;
191 std::pair<int,int> runEventPair = (m_rootIoSvc) ? m_rootIoSvc->
runEventPair() : std::pair<int,int>(-1,-1);
193 if ((m_rootIoSvc) && (m_rootIoSvc->
index() >= 0)) {
194 readInd = m_rootIoSvc->
index();
195 }
else if ((m_rootIoSvc) && (runEventPair.first != -1) && (runEventPair.second != -1)) {
196 int run = runEventPair.first;
197 int evt = runEventPair.second;
198 readInd = m_digiTree->GetEntryNumberWithIndex(run, evt);
203 if (readInd >= m_numEvents) {
204 log << MSG::WARNING <<
"Requested index is out of bounds - no digi data loaded" << endreq;
205 return StatusCode::SUCCESS;
208 numBytes = m_digiTree->GetEvent(readInd);
210 if ((numBytes <= 0) || (!m_digiEvt)) {
211 log << MSG::WARNING <<
"Failed to load digi event" << endreq;
212 return StatusCode::SUCCESS;
216 sc = readDigiEvent();
217 if (sc.isFailure()) {
218 log << MSG::ERROR <<
"Failed to read top level DigiEvent" << endreq;
223 if (sc.isFailure()) {
224 log << MSG::ERROR <<
"Failed to load MdcDigi" << endreq;
229 if (sc.isFailure()) {
230 log << MSG::ERROR <<
"Failed to load CgemDigi" << endreq;
239StatusCode digiRootReaderAlg::readDigiEvent() {
241 MsgStream log(
msgSvc(), name());
243 StatusCode sc = StatusCode::SUCCESS;
246 SmartDataPtr<Event::EventHeader> evt(eventSvc(), EventModel::EventHeader);
248 log << MSG::ERROR <<
"Failed to retrieve Event" << endreq;
249 return StatusCode::FAILURE;
252 unsigned int eventIdTds = evt->eventNumber();
253 unsigned int runIdTds = evt->runNumber();
255 unsigned int eventIdRoot = m_digiEvt->getEventId();
256 unsigned int runIdRoot = m_digiEvt->getRunId();
259 if (eventIdTds != eventIdRoot) evt->setEventNumber(eventIdRoot);
260 if (runIdTds != runIdRoot) evt->setRunNumber(runIdRoot);
263 Event::DigiEvent* digiEventTds =
267 if( sc.isFailure() ) {
272 bool fromMc = m_digiEvt->getFromMc();
279StatusCode digiRootReaderAlg::readMdcDigi() {
280 MsgStream log(
msgSvc(), name());
282 StatusCode sc = StatusCode::SUCCESS;
283 const TObjArray *mdcDigiRootCol = m_digiEvt->getMdcDigiCol();
284 if (!mdcDigiRootCol)
return sc;
285 TIter mdcDigiIter(mdcDigiRootCol);
290 if (sc.isFailure()) {
291 log <<
"Failed to register MdcDigi Collection" << endreq;
292 return StatusCode::FAILURE;
298 while ((mdcDigiRoot = (
TMdcDigi*)mdcDigiIter.Next())!=0) {
299 mdcDigiRoot->
Print();
306StatusCode digiRootReaderAlg::readCgemDigi() {
307 MsgStream log(
msgSvc(), name());
309 StatusCode sc = StatusCode::SUCCESS;
310 const TObjArray *cgemDigiRootCol = m_digiEvt->getCgemDigiCol();
311 if (!cgemDigiRootCol)
return sc;
312 TIter cgemDigiIter(cgemDigiRootCol);
317 if (sc.isFailure()) {
318 log <<
"Failed to register CgemDigi Collection" << endreq;
319 return StatusCode::FAILURE;
325 while ((cgemDigiRoot = (
TCgemDigi*)cgemDigiIter.Next())!=0) {
326 cgemDigiRoot->
Print();
332void digiRootReaderAlg::close()
340 if (m_digiTree)
delete m_digiTree;
347 StatusCode sc = StatusCode::SUCCESS;
ObjectVector< CgemDigi > CgemDigiCol
const IAlgFactory & digiRootReaderAlgFactory
ObjectVector< MdcDigi > MdcDigiCol
void initialize(bool fromMc)
The RootIoSvc gaudi service interface.
virtual void registerRootTree(TChain *ch)=0
virtual std::pair< int, int > runEventPair()=0
virtual void setRootEvtMax(unsigned int max)=0
void Print(Option_t *option="") const
Reads Digitization data from a persistent ROOT file and stores the the data in the TDS....
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(")"))
_EXTERN_ std::string Event
_EXTERN_ std::string MdcDigiCol
_EXTERN_ std::string CgemDigiCol