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