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