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