00001 #include "EventWriter/EventWriter.h"
00002
00003 #include "DistBossUtil/DistBossCode.h"
00004 #include "DistBossUtil/GetPropertyValue.h"
00005 #include "RootEventData/TBossFullEvent.h"
00006
00007 #include "DataInfoSvc/DataInfoSvc.h"
00008 #include "DataInfoSvc/IDataInfoSvc.h"
00009 #include "TBufferFile.h"
00010
00011 #include "GaudiKernel/SmartIF.h"
00012 #include "GaudiKernel/IProperty.h"
00013 #include "GaudiKernel/Bootstrap.h"
00014 #include "GaudiKernel/IAppMgrUI.h"
00015 #include <algorithm>
00016 #include <iostream>
00017 #include <fstream>
00018
00020
00021
00022 EventWriter::EventWriter(const std::string& name, ISvcLocator* pSvcLocator) :
00023 Algorithm(name, pSvcLocator)
00024 {
00025 m_itemNames.push_back("/Event");
00026 m_itemNames.push_back("/Event/EventHeader");
00027 m_itemNames.push_back("/Event/Navigator");
00028
00029 m_itemNames.push_back("/Event/Digi");
00030 m_itemNames.push_back("/Event/Digi/MdcDigiCol");
00031 m_itemNames.push_back("/Event/Digi/TofDigiCol");
00032 m_itemNames.push_back("/Event/Digi/EmcDigiCol");
00033 m_itemNames.push_back("/Event/Digi/MucDigiCol");
00034
00035 m_itemNames.push_back("/Event/Hlt");
00036 m_itemNames.push_back("/Event/Hlt/HltRawCol");
00037 m_itemNames.push_back("/Event/Hlt/HltInf");
00038
00039
00040 m_itemNames.push_back("/Event/Recon");
00041
00042 m_itemNames.push_back("/Event/Recon/RecMdcTrackCol");
00043
00044 m_itemNames.push_back("/Event/Recon/RecMdcDedxCol");
00045
00046 m_itemNames.push_back("/Event/Recon/RecMdcKalTrackCol");
00047
00048 m_itemNames.push_back("/Event/Recon/RecEsTimeCol");
00049 m_itemNames.push_back("/Event/Recon/RecExtTrackCol");
00050
00051
00052 m_itemNames.push_back("/Event/Recon/RecTofTrackCol");
00053
00054
00055 m_itemNames.push_back("/Event/Recon/RecEmcHitCol");
00056 m_itemNames.push_back("/Event/Recon/RecEmcClusterCol");
00057 m_itemNames.push_back("/Event/Recon/RecEmcShowerCol");
00058
00059 m_itemNames.push_back("/Event/Recon/RecMucTrackCol");
00060 m_itemNames.push_back("/Event/Recon/EvtRecEvent");
00061 m_itemNames.push_back("/Event/Recon/EvtRecTrackCol");
00062
00063
00064
00065 declareProperty("digiRootOutputFile",m_dofileName = "event.rec");
00066 declareProperty("ItemList",m_itemNames);
00067 declareProperty("RunMode", m_mode = 2);
00068 }
00069
00070
00071 EventWriter::~EventWriter(){
00072
00073 MsgStream log(msgSvc(), name());
00074 log << MSG::INFO << " EventWriter ~EventWriter()" << endreq;
00075
00076
00077
00078 }
00079
00080
00081 StatusCode EventWriter::initialize(){
00082
00083
00084 MsgStream log(msgSvc(), name());
00085 log << MSG::INFO << " EventWriter initialize()" << endreq;
00086
00087
00088
00089
00090
00091
00092 IService* isvc = 0;
00093
00094 StatusCode status = serviceLocator()->service("RootCnvSvc", isvc, false);
00095
00096 if ( !status.isSuccess() ) status = serviceLocator()->service("EventCnvSvc", isvc, true);
00097
00098 if ( status.isSuccess() ) {
00099
00100 status = isvc->queryInterface(IID_IRootCnvSvc, (void**)&m_cnvSvc);
00101
00102 }
00103
00104
00105 status = serviceLocator()->service("EventCnvSvc", m_pConversionSvc, true);
00106 if ( !status.isSuccess() ) {
00107 log << MSG::FATAL << "Unable to locate IConversionSvc interface"
00108 << endreq;
00109 return status;
00110 }
00111
00112
00113 status = serviceLocator()->service("EventDataSvc", m_pDataProvider, true);
00114 if ( !status.isSuccess() ) {
00115 log << MSG::FATAL << "Unable to locate IDataProviderSvc interface"
00116 << endreq;
00117 return status;
00118 }
00119
00120
00121
00122 if ( m_mode == 2 ) {
00123 m_single_outputFiles = new TFile(m_dofileName.c_str(), "RECREATE");
00124 if(m_single_outputFiles->IsZombie()||(!m_single_outputFiles->IsWritable())){
00125 std::cout<<"EventWriter ERROR::Can't not open file"<<m_dofileName.c_str()<<std::endl;
00126 exit(1);
00127 }
00128
00129 m_single_outputTrees = new TTree("Event","Event");
00130 m_jobInfoTree = new TTree("JobInfoTree","Job info");
00131 jobInfo = new TJobInfo;
00132 }
00133 else if ( m_mode == 3 ) {
00134 status = DistBoss::GetPropertyValue<std::string>("DistBoss", "ServerName", m_svrName);
00135 m_TFullEvt = new TBossFullEvent;
00136 m_writer = new NetDataWriter(m_svrName+'/'+name());
00137 m_bufsize = 512*1024;
00138 m_cbuf = new char[m_bufsize];
00139 int code = DistBossCode::ClientReady;
00140 m_writer->writeEvent((void*)&code, 4);
00141 }
00142 else {
00143 log << MSG::FATAL << "Unvalid RunMode @ initialize(): " << m_mode << endreq;
00144 exit(1);
00145 }
00146
00147 status = getSvc();
00148 if ( !status.isSuccess() ) {
00149 log << MSG::FATAL << "can not getSvc" << endreq;
00150 return status;
00151 }
00152 st=0;
00153
00154 return StatusCode::SUCCESS;
00155 }
00156
00157
00158
00159 StatusCode EventWriter::getSvc(){
00160 MsgStream log(msgSvc(), name());
00161 log << MSG::INFO << "getSvc()" << endreq;
00162 StatusCode status = StatusCode::SUCCESS;
00163 if(m_itemNames.empty()) {
00164 log << MSG::ERROR << "no ItemList from the jobOption" << endreq;
00165 return StatusCode::FAILURE;
00166 }
00167
00168 clearItems(m_itemList);
00169 ItemNames::iterator i;
00170 for (i = m_itemNames.begin(); i != m_itemNames.end(); i++){
00171 addItem(m_itemList, *i);
00172 }
00173 return status;
00174 }
00175
00176
00177 StatusCode EventWriter::execute() {
00178
00179
00180 MsgStream log(msgSvc(), name());
00181 log << MSG::INFO << "EventWriter execute()" << endreq;
00182 collectObjects();
00183
00184 if ( m_mode == 2 ) {
00185 if(st!=1){
00186 if(m_common.m_rectrackEvt) m_single_outputTrees->Branch("TRecEvent","TRecTrackEvent",&m_common.m_rectrackEvt,3200000,1);
00187 if(m_common.m_evtRecObject) m_single_outputTrees->Branch("TEvtRecObject","TEvtRecObject",&m_common.m_evtRecObject,3200000,1);
00188 if(m_common.m_dstEvt) m_single_outputTrees->Branch("TDstEvent","TDstEvent",&m_common.m_dstEvt,3200000,1);
00189 if(m_common.m_recEvt) m_single_outputTrees->Branch("TDigiEvent","TDigiEvent",&m_common.m_recEvt,3200000,1);
00190 if(m_common.m_EvtHeader) m_single_outputTrees->Branch("TEvtHeader","TEvtHeader",&m_common.m_EvtHeader,3200000,1);
00191 if(m_common.m_EvtNavigator) m_single_outputTrees->Branch("TEvtNavigator","TEvtNavigator",&m_common.m_EvtNavigator,3200000,1);
00192 if(m_common.m_hltEvt) m_single_outputTrees->Branch("THltEvent","THltEvent",&m_common.m_hltEvt,3200000,1);
00193 if(m_common.m_mcEvt) m_single_outputTrees->Branch("TMcEvent","TMcEvent",&m_common.m_mcEvt,3200000,1);
00194 if(m_common.m_trigEvt) m_single_outputTrees->Branch("TTrigEvent","TTrigEvent",&m_common.m_trigEvt,3200000,1);
00195 m_jobInfoTree->Branch("JobInfo",&jobInfo);
00196 st=1;
00197 }
00198
00199 if(m_single_outputFiles->IsZombie()||(!m_single_outputFiles->IsOpen())){
00200 std::cout<<"EventWriter ERROR::The ROOT File:"<<m_dofileName.c_str()<<"status is false"<<std::endl;
00201 exit(1);
00202 }
00203 int nb = m_single_outputTrees->Fill();
00204 if(nb==-1)
00205 {
00206 log << MSG::FATAL << "Error in fill tree (EventWriter) "<<m_single_outputTrees->GetName() << " with "<<nb<<" bytes" <<endreq;
00207 exit(1);
00208 }
00209
00210 m_single_outputFiles = m_single_outputTrees->GetCurrentFile();
00211 }
00212 else if ( m_mode == 3 ) {
00213 m_TFullEvt->setEvtHeader(m_common.m_EvtHeader);
00214 m_TFullEvt->setDigiEvent(m_common.m_recEvt);
00215 m_TFullEvt->setDstEvent(m_common.m_dstEvt);
00216 m_TFullEvt->setMcEvent(m_common.m_mcEvt);
00217 m_TFullEvt->setTrigEvent(m_common.m_trigEvt);
00218 m_TFullEvt->setHltEvent(m_common.m_hltEvt);
00219 m_TFullEvt->setRecTrackEvent(m_common.m_rectrackEvt);
00220 m_TFullEvt->setEvtRecObject(m_common.m_evtRecObject);
00221
00222
00223 TBufferFile m_TBuffer(TBufferFile::kWrite, m_bufsize-12, m_cbuf+4, false);
00224 m_TBuffer.WriteObject(m_TFullEvt);
00225 if ( m_TBuffer.Buffer() != (m_cbuf+4) ) {
00226 m_bufsize = m_TBuffer.Length() + 12;
00227 m_cbuf = new char[m_bufsize];
00228 memcpy(m_cbuf+4, m_TBuffer.Buffer(), m_TBuffer.Length());
00229 m_TBuffer.SetBit(TBuffer::kIsOwner);
00230 }
00231 *((int*)m_cbuf) = m_common.m_EvtHeader->getEventId();
00232 m_writer->writeEvent( (void*)m_cbuf, m_TBuffer.Length()+4 );
00233
00234 m_TFullEvt->reset();
00235 }
00236
00237 m_common.clear();
00238
00239 return StatusCode::SUCCESS;
00240 }
00241
00242 void EventWriter::addItem(Items& itms, const std::string& descriptor) {
00243 MsgStream log(msgSvc(), name());
00244 int level = 0;
00245 size_t sep = descriptor.rfind("#");
00246 std::string obj_path (descriptor,0,sep);
00247 std::string slevel (descriptor,sep+1,descriptor.length());
00248 if ( slevel == "*" ) {
00249 level = 9999999;
00250 }
00251 else {
00252 level = atoi(slevel.c_str());
00253 }
00254 size_t idx = obj_path.find("/",1);
00255 while (idx != std::string::npos) {
00256 std::string sub_item = obj_path.substr(0,idx);
00257 if ( 0 == findItem(sub_item) ) {
00258 addItem(itms, sub_item+"#1");
00259 }
00260 idx = obj_path.find("/",idx+1);
00261 }
00262 DataStoreItem* item = new DataStoreItem(obj_path, level);
00263 log << MSG::DEBUG << "Adding OutputStream item " << item->path()
00264 << " with " << item->depth()
00265 << " level(s)." << endreq;
00266 itms.push_back( item );
00267 }
00268
00269
00270 void EventWriter::clearItems(Items& itms) {
00271 for ( Items::iterator i = itms.begin(); i != itms.end(); i++ ) {
00272 delete (*i);
00273 }
00274 itms.erase(itms.begin(), itms.end());
00275 }
00276
00277 DataStoreItem*
00278 EventWriter::findItem(const std::string& path) {
00279 for (Items::const_iterator i=m_itemList.begin(); i != m_itemList.end(); ++i) {
00280 if ( (*i)->path() == path ) return (*i);
00281 }
00282 return 0;
00283 }
00284
00285
00286
00287 StatusCode EventWriter::collectObjects(){
00288 MsgStream log(msgSvc(), name());
00289 StatusCode status = StatusCode::SUCCESS;
00290 Items::iterator i;
00291
00292
00293
00294
00295 for ( i = m_itemList.begin(); i != m_itemList.end(); i++ ) {
00296 DataObject* obj = 0;
00297 IOpaqueAddress *pAddress = 0;
00298 m_currentItem = (*i);
00299
00300 status = m_pDataProvider->retrieveObject(m_currentItem->path(), obj);
00301 if ( status.isSuccess() ) {
00302 status = m_pConversionSvc->createRep(obj, pAddress);
00303
00304
00305
00306 }
00307 else {
00308 log << MSG::ERROR << "Cannot write mandatory object(s) (Not found): "
00309 << m_currentItem->path() << endreq;
00310 }
00311 }
00312
00313
00314 return status;
00315 }
00316
00317
00318
00319
00320
00321 StatusCode EventWriter::finalize() {
00322
00323
00324 MsgStream log(msgSvc(), name());
00325 int st =1;
00326 std::cout<<"11111111111111111111111111"<<std::endl;
00327
00328 if ( m_mode == 2 ) {
00329 m_bossVer = getenv("BES_RELEASE");
00330 log << MSG::INFO << "fill boss version: "<<m_bossVer << endreq;
00331
00332 string tmpJobOptions = getJobOptions();
00333 m_jobOptions.push_back( tmpJobOptions );
00334
00335 if(m_decayOptions.size()==0)
00336 m_decayOptions = getDecayOptions();
00337
00338 jobInfo->setBossVer(m_bossVer);
00339 jobInfo->setJobOptions(m_jobOptions);
00340 jobInfo->setDecayOptions(m_decayOptions);
00341
00342 m_jobInfoTree->Fill();
00343
00344 st = m_single_outputFiles->Write();
00345 if(st==0)
00346 {
00347 log << MSG::FATAL <<"can not write to the file "<<m_dofileName.c_str()<<endreq;
00348 exit(1);
00349 }
00350 m_single_outputFiles->Close();
00351
00352 delete m_single_outputFiles;
00353 delete jobInfo;
00354 }
00355
00356 if ( m_mode == 3 ) {
00357 int code = DistBossCode::StatusFinalize;
00358 m_writer->writeEvent((void*)&code, 4);
00359 delete m_writer;
00360 delete m_TFullEvt;
00361 delete [] m_cbuf;
00362 }
00363
00364 log << MSG::INFO << "EventWriter finalize()" << endreq;
00365 return StatusCode::SUCCESS;
00366 }
00367
00368
00369
00370
00371
00372 std::string EventWriter::getJobOptions()
00373 {
00374 MsgStream log(msgSvc(), name());
00375 IInterface* iface = Gaudi::createApplicationMgr();
00376
00377 SmartIF<IProperty> propMgr ( iface );
00378 std::string path;
00379 propMgr->getProperty( "JobOptionsPath", path);
00380 log << MSG::INFO << "JobOptions file for current job: " <<path << endreq;
00381 ifstream fin(path.c_str());
00382 string jobOptions;
00383 string tempString;
00384 while(getline(fin,tempString))
00385 {
00386 if( tempString.size()>0 && tempString.find("//")>tempString.size() )
00387 {
00388 jobOptions += tempString;
00389 jobOptions += "\n";
00390 }
00391 }
00392 log << MSG::INFO << "JobOptions: " << endreq
00393 << jobOptions << endreq;
00394 return jobOptions;
00395 }
00396
00397 std::string EventWriter::getDecayOptions()
00398 {
00399 MsgStream log(msgSvc(), name());
00400 ISvcLocator* svcLocator = Gaudi::svcLocator();
00401 IDataInfoSvc *tmpInfoSvc;
00402 DataInfoSvc* jobInfoSvc;
00403 string decayOptions;
00404 StatusCode status = svcLocator->service("DataInfoSvc",tmpInfoSvc);
00405 if (status.isSuccess()) {
00406 log << MSG::INFO << "get the DataInfoSvc" << endreq;
00407 jobInfoSvc=dynamic_cast<DataInfoSvc *>(tmpInfoSvc);
00408 decayOptions = jobInfoSvc->getDecayOptions();
00409 log << MSG::INFO << "get decay options" << endreq
00410 << decayOptions << endreq;
00411 }else {
00412 log << MSG::WARNING << "could not get the DataInfoSvc. Ignore it." << endreq;
00413 }
00414 return decayOptions;
00415 }
00416
00417