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