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"
12#include "RootCnvSvc/Util.h"
18#include "TDirectory.h"
20#include "TCollection.h"
21#include "DigiRootData/DigiEvent.h"
25#include "RootIO/IRootIoSvc.h"
52 StatusCode readDigiEvent();
56 StatusCode readMdcDigi();
68 std::string m_fileName;
70 StringArrayProperty m_fileList;
72 std::string m_treeName;
86Algorithm(name, pSvcLocator)
90 declareProperty(
"digiRootFile",m_fileName=
"");
91 StringArrayProperty initList;
92 std::vector<std::string> initVec;
93 initVec.push_back(
"digicopy.root");
94 initList.setValue(initVec);
95 declareProperty(
"digiRootFileList",m_fileList=initList);
98 declareProperty(
"digiTreeName", m_treeName=
"Rec");
107 StatusCode sc = StatusCode::SUCCESS;
108 MsgStream log(
msgSvc(), name());
114 if ( service(
"RootIoSvc", m_rootIoSvc,
true).isFailure() ){
115 log << MSG::INFO <<
"Couldn't find the RootIoSvc!" << endreq;
116 log << MSG::INFO <<
"Event loop will not terminate gracefully" << endreq;
124 TDirectory *saveDir = gDirectory;
126 m_digiTree =
new TChain(m_treeName.c_str());
128 std::string emptyStr(
"");
129 if (m_fileName.compare(emptyStr) != 0) {
130 TFile
f(m_fileName.c_str());
132 log << MSG::ERROR <<
"ROOT file " << m_fileName.c_str()
133 <<
" could not be opened for reading." << endreq;
134 return StatusCode::FAILURE;
137 m_digiTree->Add(m_fileName.c_str());
138 log << MSG::INFO <<
"Opened file: " << m_fileName.c_str() << endreq;
140 const std::vector<std::string> fileList = m_fileList.value( );
141 std::vector<std::string>::const_iterator it;
142 std::vector<std::string>::const_iterator itend = fileList.end( );
143 for (it = fileList.begin(); it != itend; it++) {
144 std::string theFile = (*it);
145 TFile
f(theFile.c_str());
147 log << MSG::ERROR <<
"ROOT file " << theFile.c_str()
148 <<
" could not be opened for reading." << endreq;
149 return StatusCode::FAILURE;
152 m_digiTree->Add(theFile.c_str());
153 log << MSG::INFO <<
"Opened file: " << theFile.c_str() << endreq;
159 m_digiTree->SetBranchAddress(
"DigiEvent", &m_digiEvt);
161 m_numEvents = m_digiTree->GetEntries();
165 if (!m_digiTree->GetIndex()) m_digiTree->BuildIndex(
"m_runId",
"m_eventId");
181 MsgStream log(
msgSvc(), name());
183 StatusCode sc = StatusCode::SUCCESS;
185 if (m_digiEvt) m_digiEvt->Clear();
187 static Int_t evtId = 0;
188 int readInd, numBytes;
189 std::pair<int,int> runEventPair = (m_rootIoSvc) ? m_rootIoSvc->
runEventPair() : std::pair<int,int>(-1,-1);
191 if ((m_rootIoSvc) && (m_rootIoSvc->
index() >= 0)) {
192 readInd = m_rootIoSvc->
index();
193 }
else if ((m_rootIoSvc) && (runEventPair.first != -1) && (runEventPair.second != -1)) {
194 int run = runEventPair.first;
195 int evt = runEventPair.second;
196 readInd = m_digiTree->GetEntryNumberWithIndex(run, evt);
201 if (readInd >= m_numEvents) {
202 log << MSG::WARNING <<
"Requested index is out of bounds - no digi data loaded" << endreq;
203 return StatusCode::SUCCESS;
206 numBytes = m_digiTree->GetEvent(readInd);
208 if ((numBytes <= 0) || (!m_digiEvt)) {
209 log << MSG::WARNING <<
"Failed to load digi event" << endreq;
210 return StatusCode::SUCCESS;
214 sc = readDigiEvent();
215 if (sc.isFailure()) {
216 log << MSG::ERROR <<
"Failed to read top level DigiEvent" << endreq;
221 if (sc.isFailure()) {
222 log << MSG::ERROR <<
"Failed to load MdcDigi" << endreq;
231StatusCode digiRootReaderAlg::readDigiEvent() {
233 MsgStream log(
msgSvc(), name());
235 StatusCode sc = StatusCode::SUCCESS;
238 SmartDataPtr<Event::EventHeader> evt(eventSvc(), EventModel::EventHeader);
240 log << MSG::ERROR <<
"Failed to retrieve Event" << endreq;
241 return StatusCode::FAILURE;
244 unsigned int eventIdTds = evt->eventNumber();
245 unsigned int runIdTds = evt->runNumber();
247 unsigned int eventIdRoot = m_digiEvt->getEventId();
248 unsigned int runIdRoot = m_digiEvt->getRunId();
251 if (eventIdTds != eventIdRoot) evt->setEventNumber(eventIdRoot);
252 if (runIdTds != runIdRoot) evt->setRunNumber(runIdRoot);
255 Event::DigiEvent* digiEventTds =
259 if( sc.isFailure() ) {
264 bool fromMc = m_digiEvt->getFromMc();
271StatusCode digiRootReaderAlg::readMdcDigi() {
272 MsgStream log(
msgSvc(), name());
274 StatusCode sc = StatusCode::SUCCESS;
275 const TObjArray *mdcDigiRootCol = m_digiEvt->getMdcDigiCol();
276 if (!mdcDigiRootCol)
return sc;
277 TIter mdcDigiIter(mdcDigiRootCol);
282 if (sc.isFailure()) {
283 log <<
"Failed to register MdcDigi Collection" << endreq;
284 return StatusCode::FAILURE;
290 while ((mdcDigiRoot = (
TMdcDigi*)mdcDigiIter.Next())!=0) {
291 mdcDigiRoot->
Print();
297void digiRootReaderAlg::close()
305 if (m_digiTree)
delete m_digiTree;
312 StatusCode sc = StatusCode::SUCCESS;
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(")"))
TFile f("ana_bhabha660a_dqa_mcPat_zy_old.root")
_EXTERN_ std::string Event
_EXTERN_ std::string MdcDigiCol