CGEM BOSS 6.6.5.i
BESIII Offline Software System
Loading...
Searching...
No Matches
digiRootReaderAlg.h
Go to the documentation of this file.
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"
6
7#include "EventModel/Event.h"
10#include "MdcRawEvent/MdcDigi.h"
12
13#include "RootCnvSvc/Util.h"
14
15#include "TROOT.h"
16#include "TFile.h"
17#include "TTree.h"
18#include "TChain.h"
19#include "TDirectory.h"
20#include "TObjArray.h"
21#include "TCollection.h" // Declares TIter
22#include "DigiRootData/DigiEvent.h"
23
24//#include "RootCnvSvc/commonData.h"
25
26#include "RootIO/IRootIoSvc.h"
27
28/** @class digiRootReaderAlg
29 * @brief Reads Digitization data from a persistent ROOT file and stores the
30 * the data in the TDS.
31 * Based on digiRootReaderAlg of Glast.
32 *
33 */
34
35class digiRootReaderAlg : public Algorithm
36{
37public:
38
39 digiRootReaderAlg(const std::string& name, ISvcLocator* pSvcLocator);
40
41 /// Handles setup by opening ROOT file in read mode and creating a new TTree
42 StatusCode initialize();
43
44 /// Orchastrates reading from ROOT file and storing the data on the TDS for each event
45 StatusCode execute();
46
47 /// Closes the ROOT file and cleans up
48 StatusCode finalize();
49
50private:
51
52 /// Reads top-level DigiEvent
53 StatusCode readDigiEvent();
54
55
56 /// Reads TKR digi data from ROOT and puts it on the TDS
57 StatusCode readMdcDigi();
58 StatusCode readCgemDigi();
59
60 /// Closes the ROOT file
61 void close();
62
63 /// ROOT file pointer
64 TFile *m_digiFile;
65 /// ROOT tree pointer
66 TChain *m_digiTree;
67 /// Top-level Monte Carlo ROOT object
68 DigiEvent *m_digiEvt;
69 /// name of the input ROOT file
70 std::string m_fileName;
71 /// Array of input file names
72 StringArrayProperty m_fileList;
73 /// name of the Monte Carlo TTree stored in the ROOT file
74 std::string m_treeName;
75 /// Stores number of events available in the input ROOT TTree
76 int m_numEvents;
77
78// commonData m_common;
79 IRootIoSvc* m_rootIoSvc;
80
81};
82
83static const AlgFactory<digiRootReaderAlg> Factory;
84const IAlgFactory& digiRootReaderAlgFactory = Factory;
85
86
87digiRootReaderAlg::digiRootReaderAlg(const std::string& name, ISvcLocator* pSvcLocator) :
88Algorithm(name, pSvcLocator)
89{
90 // Input pararmeters that may be set via the jobOptions file
91 // Input ROOT file name
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);
98 // Input TTree name
99 initVec.clear();
100 declareProperty("digiTreeName", m_treeName="Rec");// wensp midify for test 2005/05/14
101
102}
103
105{
106 // Purpose and Method: Called once before the run begins. This method
107 // opens a new ROOT file and prepares for reading.
108
109 StatusCode sc = StatusCode::SUCCESS;
110 MsgStream log(msgSvc(), name());
111
112 // Use the Job options service to set the Algorithm's parameters
113 // This will retrieve parameters set in the job options file
114 setProperties();
115
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;
119 m_rootIoSvc = 0;
120 //return StatusCode::FAILURE;
121 }
122
124
125 // Save the current directory for the ntuple writer service
126 TDirectory *saveDir = gDirectory;
127
128 m_digiTree = new TChain(m_treeName.c_str());
129
130 std::string emptyStr("");
131 if (m_fileName.compare(emptyStr) != 0) {
132 TFile f(m_fileName.c_str());
133 if (!f.IsOpen()) {
134 log << MSG::ERROR << "ROOT file " << m_fileName.c_str()
135 << " could not be opened for reading." << endreq;
136 return StatusCode::FAILURE;
137 }
138 f.Close();
139 m_digiTree->Add(m_fileName.c_str());
140 log << MSG::INFO << "Opened file: " << m_fileName.c_str() << endreq;
141 } else {
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());
148 if (!f.IsOpen()) {
149 log << MSG::ERROR << "ROOT file " << theFile.c_str()
150 << " could not be opened for reading." << endreq;
151 return StatusCode::FAILURE;
152 }
153 f.Close();
154 m_digiTree->Add(theFile.c_str());
155 log << MSG::INFO << "Opened file: " << theFile.c_str() << endreq;
156 }
157 }
158
159
160 m_digiEvt = 0;
161 m_digiTree->SetBranchAddress("DigiEvent", &m_digiEvt);
162 //m_common.m_digiEvt = m_digiEvt;
163 m_numEvents = m_digiTree->GetEntries();
164
165 if (m_rootIoSvc) {
166 m_rootIoSvc->setRootEvtMax(m_numEvents);
167 if (!m_digiTree->GetIndex()) m_digiTree->BuildIndex("m_runId", "m_eventId");
168 m_rootIoSvc->registerRootTree(m_digiTree);
169 }
170
171
172 saveDir->cd();
173 return sc;
174
175}
176
178{
179 // Purpose and Method: Called once per event. This method calls
180 // the appropriate methods to read data from the ROOT file and store
181 // data on the TDS.
182
183 MsgStream log(msgSvc(), name());
184
185 StatusCode sc = StatusCode::SUCCESS;
186
187 if (m_digiEvt) m_digiEvt->Clear();
188
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);
192
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);
199 } else {
200 readInd = evtId;
201 }
202
203 if (readInd >= m_numEvents) {
204 log << MSG::WARNING << "Requested index is out of bounds - no digi data loaded" << endreq;
205 return StatusCode::SUCCESS;
206 }
207
208 numBytes = m_digiTree->GetEvent(readInd);
209
210 if ((numBytes <= 0) || (!m_digiEvt)) {
211 log << MSG::WARNING << "Failed to load digi event" << endreq;
212 return StatusCode::SUCCESS;
213 }
214
215
216 sc = readDigiEvent();
217 if (sc.isFailure()) {
218 log << MSG::ERROR << "Failed to read top level DigiEvent" << endreq;
219 return sc;
220 }
221
222 sc = readMdcDigi();
223 if (sc.isFailure()) {
224 log << MSG::ERROR << "Failed to load MdcDigi" << endreq;
225 return sc;
226 }
227
228 sc = readCgemDigi();
229 if (sc.isFailure()) {
230 log << MSG::ERROR << "Failed to load CgemDigi" << endreq;
231 return sc;
232 }
233
234 evtId = readInd+1;
235 return sc;
236}
237
238
239StatusCode digiRootReaderAlg::readDigiEvent() {
240
241 MsgStream log(msgSvc(), name());
242
243 StatusCode sc = StatusCode::SUCCESS;
244
245 // Retrieve the Event data for this event
246 SmartDataPtr<Event::EventHeader> evt(eventSvc(), EventModel::EventHeader);
247 if (!evt) {
248 log << MSG::ERROR << "Failed to retrieve Event" << endreq;
249 return StatusCode::FAILURE;
250 }
251
252 unsigned int eventIdTds = evt->eventNumber();
253 unsigned int runIdTds = evt->runNumber();
254
255 unsigned int eventIdRoot = m_digiEvt->getEventId();
256 unsigned int runIdRoot = m_digiEvt->getRunId();
257
258 // Check to see if the event and run ids have already been set.
259 if (eventIdTds != eventIdRoot) evt->setEventNumber(eventIdRoot);
260 if (runIdTds != runIdRoot) evt->setRunNumber(runIdRoot);
261
262
263 Event::DigiEvent* digiEventTds =
264 SmartDataPtr<Event::DigiEvent>(eventSvc(), EventModel::Digi::Event);
265 if (!digiEventTds) {
266 sc = eventSvc()->registerObject(EventModel::Digi::Event /*"/Event/Digi"*/,new DataObject);
267 if( sc.isFailure() ) {
268 log << MSG::ERROR << "could not register " << EventModel::Digi::Event /*<< /Event/Digi "*/ << endreq;
269 return sc;
270 }
271 } else {
272 bool fromMc = m_digiEvt->getFromMc();
273 digiEventTds->initialize(fromMc);
274 }
275 return sc;
276}
277
278
279StatusCode digiRootReaderAlg::readMdcDigi() {
280 MsgStream log(msgSvc(), name());
281
282 StatusCode sc = StatusCode::SUCCESS;
283 const TObjArray *mdcDigiRootCol = m_digiEvt->getMdcDigiCol();
284 if (!mdcDigiRootCol) return sc;
285 TIter mdcDigiIter(mdcDigiRootCol);
286
287 // create the TDS location for the EmcDigi Collection
288 MdcDigiCol* mdcDigiTdsCol = new MdcDigiCol;
289 sc = eventSvc()->registerObject(EventModel::Digi::MdcDigiCol, mdcDigiTdsCol);
290 if (sc.isFailure()) {
291 log << "Failed to register MdcDigi Collection" << endreq;
292 return StatusCode::FAILURE;
293 }
294
295
296
297 TMdcDigi *mdcDigiRoot = 0;
298 while ((mdcDigiRoot = (TMdcDigi*)mdcDigiIter.Next())!=0) {
299 mdcDigiRoot->Print();
300 }
301
302 return sc;
303}
304
305
306StatusCode digiRootReaderAlg::readCgemDigi() {
307 MsgStream log(msgSvc(), name());
308
309 StatusCode sc = StatusCode::SUCCESS;
310 const TObjArray *cgemDigiRootCol = m_digiEvt->getCgemDigiCol();
311 if (!cgemDigiRootCol) return sc;
312 TIter cgemDigiIter(cgemDigiRootCol);
313
314 // create the TDS location for the EmcDigi Collection
315 CgemDigiCol* cgemDigiTdsCol = new CgemDigiCol;
316 sc = eventSvc()->registerObject(EventModel::Digi::CgemDigiCol, cgemDigiTdsCol);
317 if (sc.isFailure()) {
318 log << "Failed to register CgemDigi Collection" << endreq;
319 return StatusCode::FAILURE;
320 }
321
322
323
324 TCgemDigi *cgemDigiRoot = 0;
325 while ((cgemDigiRoot = (TCgemDigi*)cgemDigiIter.Next())!=0) {
326 cgemDigiRoot->Print();
327 }
328
329 return sc;
330}
331
332void digiRootReaderAlg::close()
333{
334 // Purpose and Method: Writes the ROOT file at the end of the run.
335 // The TObject::kOverWrite parameter is used in the Write method
336 // since ROOT will periodically write to the ROOT file when the bufSize
337 // is filled. Writing would create 2 copies of the same tree to be
338 // stored in the ROOT file, if we did not specify kOverwrite.
339
340 if (m_digiTree) delete m_digiTree;
341}
342
344{
345 close();
346
347 StatusCode sc = StatusCode::SUCCESS;
348 return sc;
349}
350
ObjectVector< CgemDigi > CgemDigiCol
Definition CgemDigi.h:43
definition of the interface for IRootIoSvc
ObjectVector< MdcDigi > MdcDigiCol
Definition MdcDigi.h:39
IMessageSvc * msgSvc()
void initialize(bool fromMc)
Definition DigiEvent.h:30
The RootIoSvc gaudi service interface.
Definition IRootIoSvc.h:23
virtual void registerRootTree(TChain *ch)=0
virtual int index()=0
virtual std::pair< int, int > runEventPair()=0
virtual void setRootEvtMax(unsigned int max)=0
void Print(Option_t *option="") const
Definition TRawData.cxx:25
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(")"))
const IAlgFactory & digiRootReaderAlgFactory
_EXTERN_ std::string Event
Definition EventModel.h:60
_EXTERN_ std::string MdcDigiCol
Definition EventModel.h:61
_EXTERN_ std::string CgemDigiCol
Definition EventModel.h:67