BOSS 8.0.0
BESIII Offline Software System
Loading...
Searching...
No Matches
EventWriter.cxx
Go to the documentation of this file.
1#include "EventWriter.h"
2#include "DataInfoSvc/IDataInfoSvc.h"
3
4#include "GaudiKernel/Bootstrap.h"
5#include "GaudiKernel/IAppMgrUI.h"
6#include "GaudiKernel/IProperty.h"
7#include "GaudiKernel/SmartIF.h"
8#include <algorithm> // for find
9#include <fstream>
10#include <iostream>
11
12/////////////////////////////////////////////////////////////////////////////
13// int EventWriter::counter = 0; // static data member
15EventWriter::EventWriter( const std::string& name, ISvcLocator* pSvcLocator )
16 : Algorithm( name, pSvcLocator ) {
17 m_itemNames.push_back( "/Event" );
18 m_itemNames.push_back( "/Event/EventHeader" );
19 m_itemNames.push_back( "/Event/Navigator" );
20
21 m_itemNames.push_back( "/Event/Digi" );
22 m_itemNames.push_back( "/Event/Digi/MdcDigiCol" );
23 m_itemNames.push_back( "/Event/Digi/TofDigiCol" );
24 m_itemNames.push_back( "/Event/Digi/EmcDigiCol" );
25 m_itemNames.push_back( "/Event/Digi/MucDigiCol" );
26
27 m_itemNames.push_back( "/Event/Hlt" );
28 m_itemNames.push_back( "/Event/Hlt/HltRawCol" );
29 m_itemNames.push_back( "/Event/Hlt/HltInf" );
30 // m_itemNames.push_back("/Event/Hlt/DstHltInf");
31
32 m_itemNames.push_back( "/Event/Recon" );
33 // m_itemNames.push_back("/Event/Recon/RecMdcHitCol");
34 m_itemNames.push_back( "/Event/Recon/RecMdcTrackCol" );
35 // m_itemNames.push_back("/Event/Recon/MdcHOTCol");
36 m_itemNames.push_back( "/Event/Recon/RecMdcDedxCol" );
37 // m_itemNames.push_back("/Event/Recon/RecMdcDedxHitCol");
38 m_itemNames.push_back( "/Event/Recon/RecMdcKalTrackCol" );
39 // m_itemNames.push_back("/Event/Recon/RecMdcKalHelixSegCol");
40 m_itemNames.push_back( "/Event/Recon/RecEsTimeCol" );
41 m_itemNames.push_back( "/Event/Recon/RecExtTrackCol" );
42 // m_itemNames.push_back("/Event/Recon/RecBTofHitCol");
43 // m_itemNames.push_back("/Event/Recon/RecETofHitCol");
44 m_itemNames.push_back( "/Event/Recon/RecTofTrackCol" );
45 // m_itemNames.push_back("/Event/Recon/RecBTofCalHitCol");
46 // m_itemNames.push_back("/Event/Recon/RecETofCalHitCol");
47 m_itemNames.push_back( "/Event/Recon/RecEmcHitCol" );
48 m_itemNames.push_back( "/Event/Recon/RecEmcClusterCol" );
49 m_itemNames.push_back( "/Event/Recon/RecEmcShowerCol" );
50 // m_itemNames.push_back("/Event/Recon/MucRecHitCol");
51 m_itemNames.push_back( "/Event/Recon/RecMucTrackCol" );
52 m_itemNames.push_back( "/Event/Recon/EvtRecEvent" );
53 m_itemNames.push_back( "/Event/Recon/EvtRecTrackCol" );
54 // m_itemNames.push_back("/Event/Recon/EvtRecVertexCol");
55
56 // Part 1: Declare the properties
57 declareProperty( "digiRootOutputFile", m_dofileName = "event.rec" );
58 declareProperty( "ItemList", m_itemNames );
59 declareProperty( "RunMode", m_mode = 2 );
60}
61
62// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
64 // Part 1: Get the messaging service, print where you are
65 MsgStream log( msgSvc(), name() );
66 log << MSG::INFO << " EventWriter ~EventWriter()" << endmsg;
67}
68
69//**************************************************************************
71
72 // Part 1: Get the messaging service, print where you are
73 MsgStream log( msgSvc(), name() );
74 log << MSG::INFO << " EventWriter initialize()" << endmsg;
75
76 StatusCode status = serviceLocator()->service( "RootCnvSvc", m_cnvSvc, false );
77
78 if ( !status.isSuccess() )
79 status = serviceLocator()->service( "EventCnvSvc", m_cnvSvc, true );
80
81 status = serviceLocator()->service( "EventCnvSvc", m_pConversionSvc, true );
82 if ( !status.isSuccess() )
83 {
84 log << MSG::FATAL << "Unable to locate IConversionSvc interface" << endmsg;
85 return status;
86 }
87
88 status = serviceLocator()->service( "EventDataSvc", m_pDataProvider, true );
89 if ( !status.isSuccess() )
90 {
91 log << MSG::FATAL << "Unable to locate IDataProviderSvc interface" << endmsg;
92 return status;
93 }
94
95 if ( m_mode == 2 )
96 { // OfflineMode
97 m_single_outputFiles = new TFile( m_dofileName.c_str(), "RECREATE" );
98 if ( m_single_outputFiles->IsZombie() || ( !m_single_outputFiles->IsWritable() ) )
99 {
100 std::cout << "EventWriter ERROR::Can't not open file" << m_dofileName.c_str()
101 << std::endl;
102 exit( 1 );
103 }
104
105 m_single_outputTrees = new TTree( "Event", "Event" );
106 m_jobInfoTree = new TTree( "JobInfoTree", "Job info" );
107 jobInfo = new TJobInfo;
108 }
109 else if ( m_mode == 3 )
110 { // DistBossMode
111 log << MSG::FATAL << "The DistBossMode has been abandoned!" << endmsg;
112 return StatusCode::FAILURE;
113 }
114 else
115 {
116 log << MSG::FATAL << "Unvalid RunMode @ initialize(): " << m_mode << endmsg;
117 exit( 1 );
118 }
119
120 status = getSvc();
121 if ( !status.isSuccess() )
122 {
123 log << MSG::FATAL << "can not getSvc" << endmsg;
124 return status;
125 }
126 st = 0;
127
128 return StatusCode::SUCCESS;
129}
130
131// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
132
133StatusCode EventWriter::getSvc() {
134 MsgStream log( msgSvc(), name() );
135 log << MSG::INFO << "getSvc()" << endmsg;
136 StatusCode status = StatusCode::SUCCESS;
137 if ( m_itemNames.empty() )
138 {
139 log << MSG::ERROR << "no ItemList from the jobOption" << endmsg;
140 return StatusCode::FAILURE;
141 }
142 // ItemNames m_itemNames;
143 clearItems( m_itemList );
144 ItemNames::iterator i;
145 for ( i = m_itemNames.begin(); i != m_itemNames.end(); i++ ) { addItem( m_itemList, *i ); }
146 return status;
147}
148
150
151 // Part 1: Get the messaging service, print where you are
152 MsgStream log( msgSvc(), name() );
153 log << MSG::INFO << "EventWriter execute()" << endmsg;
154 collectObjects().ignore();
155
156 if ( m_mode == 2 )
157 { // OfflineMode
158 if ( st != 1 )
159 {
160 if ( m_common.m_rectrackEvt )
161 m_single_outputTrees->Branch( "TRecEvent", "TRecTrackEvent", &m_common.m_rectrackEvt,
162 3200000, 1 );
163 if ( m_common.m_evtRecObject )
164 m_single_outputTrees->Branch( "TEvtRecObject", "TEvtRecObject",
165 &m_common.m_evtRecObject, 3200000, 1 );
166 if ( m_common.m_dstEvt )
167 m_single_outputTrees->Branch( "TDstEvent", "TDstEvent", &m_common.m_dstEvt, 3200000,
168 1 );
169 if ( m_common.m_recEvt )
170 m_single_outputTrees->Branch( "TDigiEvent", "TDigiEvent", &m_common.m_recEvt, 3200000,
171 1 );
172 if ( m_common.m_EvtHeader )
173 m_single_outputTrees->Branch( "TEvtHeader", "TEvtHeader", &m_common.m_EvtHeader,
174 3200000, 1 );
175 if ( m_common.m_EvtNavigator )
176 m_single_outputTrees->Branch( "TEvtNavigator", "TEvtNavigator",
177 &m_common.m_EvtNavigator, 3200000, 1 );
178 if ( m_common.m_hltEvt )
179 m_single_outputTrees->Branch( "THltEvent", "THltEvent", &m_common.m_hltEvt, 3200000,
180 1 );
181 if ( m_common.m_mcEvt )
182 m_single_outputTrees->Branch( "TMcEvent", "TMcEvent", &m_common.m_mcEvt, 3200000, 1 );
183 if ( m_common.m_trigEvt )
184 m_single_outputTrees->Branch( "TTrigEvent", "TTrigEvent", &m_common.m_trigEvt, 3200000,
185 1 );
186 m_jobInfoTree->Branch( "JobInfo", &jobInfo );
187 st = 1;
188 }
189
190 if ( m_single_outputFiles->IsZombie() || ( !m_single_outputFiles->IsOpen() ) )
191 {
192 std::cout << "EventWriter ERROR::The ROOT File:" << m_dofileName.c_str()
193 << "status is false" << std::endl;
194 exit( 1 );
195 }
196 int nb = m_single_outputTrees->Fill();
197 if ( nb == -1 )
198 {
199 log << MSG::FATAL << "Error in fill tree (EventWriter) "
200 << m_single_outputTrees->GetName() << " with " << nb << " bytes" << endmsg;
201 exit( 1 );
202 }
203
204 m_single_outputFiles = m_single_outputTrees->GetCurrentFile();
205 }
206 else if ( m_mode == 3 )
207 { // DistBossMode
208 log << MSG::FATAL << "The DistBossMode has been abandoned!" << endmsg;
209 return StatusCode::FAILURE;
210 }
211
212 m_common.clear();
213
214 return StatusCode::SUCCESS;
215}
216
217void EventWriter::addItem( Items& itms, const std::string& descriptor ) {
218 MsgStream log( msgSvc(), name() );
219 int level = 0;
220 size_t sep = descriptor.rfind( "#" );
221 std::string obj_path( descriptor, 0, sep );
222 std::string slevel( descriptor, sep + 1, descriptor.length() );
223 if ( slevel == "*" ) { level = 9999999; }
224 else { level = atoi( slevel.c_str() ); }
225 size_t idx = obj_path.find( "/", 1 );
226 while ( idx != std::string::npos )
227 {
228 std::string sub_item = obj_path.substr( 0, idx );
229 if ( 0 == findItem( sub_item ) ) { addItem( itms, sub_item + "#1" ); }
230 idx = obj_path.find( "/", idx + 1 );
231 }
232 DataStoreItem* item = new DataStoreItem( obj_path, level );
233 log << MSG::DEBUG << "Adding OutputStream item " << item->path() << " with " << item->depth()
234 << " level(s)." << endmsg;
235 itms.push_back( item );
236}
237
238// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
239void EventWriter::clearItems( Items& itms ) {
240 for ( Items::iterator i = itms.begin(); i != itms.end(); i++ ) { delete ( *i ); }
241 itms.erase( itms.begin(), itms.end() );
242}
243
244DataStoreItem* EventWriter::findItem( const std::string& path ) {
245 for ( Items::const_iterator i = m_itemList.begin(); i != m_itemList.end(); ++i )
246 {
247 if ( ( *i )->path() == path ) return ( *i );
248 }
249 return 0;
250}
251
252StatusCode EventWriter::collectObjects() {
253 MsgStream log( msgSvc(), name() );
254 StatusCode status = StatusCode::SUCCESS;
255 Items::iterator i;
256
257 // Traverse the tree and collect the requested objects
258 // status = m_pConversionSvc->connectOutput(f_rootOutputFile.c_str(), "recreate");
259
260 for ( i = m_itemList.begin(); i != m_itemList.end(); i++ )
261 {
262 DataObject* obj = 0;
263 IOpaqueAddress* pAddress = 0;
264 m_currentItem = ( *i );
265
266 status = m_pDataProvider->retrieveObject( m_currentItem->path(), obj );
267 if ( status.isSuccess() )
268 {
269 status = m_pConversionSvc->createRep( obj, pAddress );
270 // IRegistry *pReg = obj->registry();
271 // pReg->setAddress(pAddress);
272 // status = m_pConversionSvc->fillRepRefs(pReg->address(), *i);
273 }
274 else
275 {
276 log << MSG::ERROR
277 << "Cannot write mandatory object(s) (Not found): " << m_currentItem->path()
278 << endmsg;
279 }
280 }
281
282 return status;
283}
284
285// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
286
288
289 // Part 1: Get the messaging service, print where you are
290 MsgStream log( msgSvc(), name() );
291 int st = 1;
292 std::cout << "11111111111111111111111111" << std::endl;
293
294 if ( m_mode == 2 )
295 { // OfflineMode
296 m_bossVer = getenv( "BES_RELEASE" );
297 log << MSG::INFO << "fill boss version: " << m_bossVer << endmsg;
298
299 string tmpJobOptions = getJobOptions();
300 m_jobOptions.push_back( tmpJobOptions );
301
302 if ( m_decayOptions.size() == 0 ) m_decayOptions = getDecayOptions();
303
304 jobInfo->setBossVer( m_bossVer );
305 jobInfo->setJobOptions( m_jobOptions );
306 jobInfo->setDecayOptions( m_decayOptions );
307
308 m_jobInfoTree->Fill();
309
310 st = m_single_outputFiles->Write();
311 if ( st == 0 )
312 {
313 log << MSG::FATAL << "can not write to the file " << m_dofileName.c_str() << endmsg;
314 exit( 1 );
315 }
316 m_single_outputFiles->Close();
317
318 delete m_single_outputFiles;
319 delete jobInfo;
320 }
321
322 if ( m_mode == 3 )
323 { // DistBossMode
324 log << MSG::FATAL << "The DistBossMode has been abandoned!" << endmsg;
325 return StatusCode::FAILURE;
326 }
327
328 log << MSG::INFO << "EventWriter finalize()" << endmsg;
329 return StatusCode::SUCCESS;
330}
331
332// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
333// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
334
335std::string EventWriter::getJobOptions() {
336 MsgStream log( msgSvc(), name() );
337 IInterface* iface = Gaudi::createApplicationMgr();
338 SmartIF<IProperty> propMgr( iface );
339 std::string path;
340 propMgr->getProperty( "JobOptionsPath", path ).ignore();
341 log << MSG::INFO << "JobOptions file for current job: " << path << endmsg;
342 ifstream fin( path.c_str() );
343 string jobOptions;
344 string tempString;
345 while ( getline( fin, tempString ) )
346 {
347 if ( tempString.size() > 0 && tempString.find( "//" ) > tempString.size() )
348 {
349 jobOptions += tempString;
350 jobOptions += "\n";
351 }
352 }
353 log << MSG::INFO << "JobOptions: " << endmsg << jobOptions << endmsg;
354 return jobOptions;
355}
356
357std::string EventWriter::getDecayOptions() {
358 MsgStream log( msgSvc(), name() );
359 ISvcLocator* svcLocator = Gaudi::svcLocator();
360 IDataInfoSvc* jobInfoSvc;
361 string decayOptions;
362 StatusCode status = svcLocator->service( "DataInfoSvc", jobInfoSvc );
363 if ( status.isSuccess() )
364 {
365 log << MSG::INFO << "get the DataInfoSvc" << endmsg;
366 decayOptions = jobInfoSvc->getDecayOptions();
367 log << MSG::INFO << "get decay options" << endmsg << decayOptions << endmsg;
368 }
369 else { log << MSG::WARNING << "could not get the DataInfoSvc. Ignore it." << endmsg; }
370 return decayOptions;
371}
DECLARE_COMPONENT(BesBdkRc)
IMessageSvc * msgSvc()
EventWriter(const std::string &name, ISvcLocator *pSvcLocator)
StatusCode finalize()
StatusCode execute()
StatusCode initialize()
virtual string getDecayOptions()=0