00001 #include "GaudiKernel/MsgStream.h"
00002 #include "GaudiKernel/AlgFactory.h"
00003 #include "GaudiKernel/ISvcLocator.h"
00004 #include "GaudiKernel/SmartDataPtr.h"
00005 #include "GaudiKernel/IDataProviderSvc.h"
00006 #include "GaudiKernel/PropertyMgr.h"
00007 #include "GaudiKernel/Bootstrap.h"
00008 #include "GaudiKernel/ISvcLocator.h"
00009
00010 #include "EventModel/EventModel.h"
00011 #include "EventModel/Event.h"
00012
00013 #include "EvtRecEvent/EvtRecEvent.h"
00014 #include "EvtRecEvent/EvtRecTrack.h"
00015 #include "EventModel/EventHeader.h"
00016
00017 #include "GaudiKernel/INTupleSvc.h"
00018 #include "GaudiKernel/NTuple.h"
00019 #include "GaudiKernel/Bootstrap.h"
00020 #include "GaudiKernel/IHistogramSvc.h"
00021 #include "CLHEP/Vector/ThreeVector.h"
00022 #include "CLHEP/Vector/LorentzVector.h"
00023 #include "CLHEP/Vector/TwoVector.h"
00024 #include "CLHEP/Geometry/Point3D.h"
00025
00026 #include "TagWriterAlg/TagWriterAlg.h"
00027
00028 #include "RootCnvSvc/RootInterface.h"
00029 #include "GaudiKernel/SmartIF.h"
00030 #include "GaudiKernel/IJobOptionsSvc.h"
00031
00032
00033 #include "TMath.h"
00034 #include <vector>
00035
00037
00038 TagWriterAlg::TagWriterAlg(const std::string& name, ISvcLocator* pSvcLocator) :
00039 Algorithm(name, pSvcLocator) {
00040
00041
00042 declareProperty("OutputTagFile", m_outputTagFile);
00043
00044 }
00045
00046
00047 StatusCode TagWriterAlg::initialize(){
00048 MsgStream log(msgSvc(), name());
00049
00050 log << MSG::INFO << "in initialize()" << endmsg;
00051
00052 SmartIF<IJobOptionsSvc> iSvc(serviceLocator()->service("JobOptionsSvc"));
00053 if ( iSvc.isValid() ) {
00054 std::string dll;
00055 const std::vector<const Property*>* ps = 0;
00056 if ( iSvc->getProperties("RootCnvSvc") != 0 )
00057 ps = iSvc->getProperties("RootCnvSvc");
00058 if ( iSvc->getProperties("EventCnvSvc") != 0 )
00059 ps = iSvc->getProperties("EventCnvSvc");
00060 if ( iSvc->getProperties("WriteDst") != 0 )
00061 ps = iSvc->getProperties("WriteDst");
00062
00063 if( ps !=0 )
00064 {
00065 for(int i=0;i<ps->size();i++)
00066 {
00067 if( (*ps)[i]->name() == "digiRootOutputFile" )
00068
00069 {
00070 m_dstOutput = (*ps)[i]->toString();
00071 std::cout<<"read from JobOptionSvc, dstOutput: "<< m_dstOutput<<std::endl;
00072 }
00073 }
00074 }
00075 }
00076
00077
00078
00079 m_rootInterface=RootInterface::Instance(log);
00080 log << MSG::INFO <<"RootInteface TotalFileNum: "<<m_rootInterface->getTotalFileNum()<<endreq;
00081 std::cout<<"getTotalFileNum: "<<m_rootInterface->getTotalFileNum()<<std::endl;
00082
00083 if(m_rootInterface->getTotalFileNum()>0)
00084 {
00085 if(m_rootInterface->getTotalFileNum() != m_outputTagFile.size() )
00086 {
00087 std::cout<<"the input file num != output file number, please check your jobOptions"<<std::endl;
00088 exit(-1);
00089 }
00090 }
00091
00092 m_entry = -1;
00093 m_entry_in_alg = -1;
00094 m_dstFile = "";
00095 m_fileNum=-1;
00096 return StatusCode::SUCCESS;
00097 }
00098
00099
00100 StatusCode TagWriterAlg::execute() {
00101
00102 MsgStream log(msgSvc(), name());
00103 log << MSG::INFO << "in execute()" << endreq;
00104
00105
00106 if(m_dstOutput!="")
00107 {
00108 if(m_entry==-1)
00109 {
00110 std::cout << "TagWriterAlg: write tag during reconstruction" << std::endl;
00111 m_dstFile = m_dstOutput;
00112 std::cout<< "dst file: "<< m_dstFile << std::endl;
00113 openOutputTagFile(m_outputTagFile[0]);
00114 m_oFileTree->Fill();
00115 }
00116 }
00117 else
00118 {
00119 if( m_dstFile != m_rootInterface->getCurrentFileName() )
00120 {
00121 std::cout<<"TagWriterAlg, current file: "<<m_rootInterface->getCurrentFileName()<<std::endl;
00122 m_fileNum++;
00123 m_dstFile = m_rootInterface->getCurrentFileName();
00124
00125 m_entry=-1;
00126 if(m_fileNum>0)
00127 {
00128 m_oFile->Write();
00129 delete m_oTree;
00130 delete m_oFileTree;
00131 delete m_oFile;
00132 }
00133
00134 log << MSG::INFO << "open ROOT output TAG file: "<<m_outputTagFile[m_fileNum]<<endreq;
00135 openOutputTagFile(m_outputTagFile[m_fileNum]);
00136 m_oFileTree->Fill();
00137 }
00138 }
00139
00140 m_entry++;
00141 getTagInfo();
00142 log << MSG::INFO << "writing TAG information to ROOT TAG file"<<endreq;
00143 m_oTree->Fill();
00144
00145 m_entry_in_alg++;
00146 return StatusCode::SUCCESS;
00147 }
00148
00149
00150
00151 StatusCode TagWriterAlg::getTagInfo()
00152 {
00153 MsgStream log(msgSvc(), name());
00154 log << MSG::INFO << "reading event data from DST"<<endreq;
00155
00156 SmartDataPtr<Event::EventHeader> eventHeader(eventSvc(),"/Event/EventHeader");
00157 if(!eventHeader)
00158 {
00159 log << MSG::FATAL << "Could not find EventHeader." << endreq;
00160 return StatusCode::FAILURE;
00161 }
00162
00163 m_runNo=eventHeader->runNumber();
00164 m_eventId=eventHeader->eventNumber();
00165 log << MSG::DEBUG <<"run, evtnum = " << m_runNo << " , "<< m_eventId <<endreq;
00166
00167 SmartDataPtr<EvtRecEvent> evtRecEvent(eventSvc(), EventModel::EvtRec::EvtRecEvent);
00168 if(!evtRecEvent)
00169 {
00170 log << MSG::FATAL << "Could not find EvtRecEvent." << endreq;
00171 return StatusCode::FAILURE;
00172 }
00173 log << MSG::DEBUG <<"ncharg, nneu, tottks = "
00174 << evtRecEvent->totalCharged() << " , "
00175 << evtRecEvent->totalNeutral() << " , "
00176 << evtRecEvent->totalTracks() <<endreq;
00177 m_totalCharged = evtRecEvent->totalCharged();
00178 m_totalNeutral = evtRecEvent->totalNeutral();
00179 m_totalTrks = evtRecEvent->totalTracks();
00180 return StatusCode::SUCCESS;
00181 }
00182
00183
00184 StatusCode TagWriterAlg::finalize() {
00185
00186 MsgStream log(msgSvc(), name());
00187
00188 m_oFile->Write();
00189
00190 log << MSG::INFO << "in finalize()" << endmsg;
00191 return StatusCode::SUCCESS;
00192 }
00193
00194
00195
00196 void TagWriterAlg::openOutputTagFile(std::string ofile)
00197 {
00198 m_oFile = new TFile(ofile.c_str(),"RECREATE","ROOT file for TAG");
00199 m_oTree = new TTree("Tag","Tags for DST file");
00200 m_oTree->Branch("entry",&m_entry, "entry/I");
00201 m_oTree->Branch("runNo",&m_runNo, "runNo/I");
00202 m_oTree->Branch("eventId",&m_eventId, "eventId/I");
00203 m_oTree->Branch("totalCharged",&m_totalCharged, "totalCharged/I");
00204 m_oTree->Branch("totalNeutral",&m_totalNeutral, "totalNeutral/I");
00205 m_oTree->Branch("totalTrks",&m_totalTrks, "totalTrks/I");
00206 m_oFileTree = new TTree("File", "DST file names");
00207 m_oFileTree->Branch("fileName", &m_dstFile);
00208
00209 }
00210
00211
00212
00213
00214
00215
00216
00217
00218
00219
00220
00221
00222