/home/bes3soft/bes3soft/Boss/7.0.2/dist/7.0.2/Event/RootIO/RootIO-00-01-31/RootIO/digiRootReaderAlg.h

Go to the documentation of this file.
00001 #include "GaudiKernel/MsgStream.h"
00002 #include "GaudiKernel/AlgFactory.h"
00003 #include "GaudiKernel/IDataProviderSvc.h"
00004 #include "GaudiKernel/SmartDataPtr.h"
00005 #include "GaudiKernel/Algorithm.h"
00006 
00007 #include "EventModel/Event.h"
00008 #include "EventModel/EventModel.h"
00009 #include "RawEvent/DigiEvent.h"
00010 #include "MdcRawEvent/MdcDigi.h"
00011 
00012 #include "RootCnvSvc/Util.h"
00013 
00014 #include "TROOT.h"
00015 #include "TFile.h"
00016 #include "TTree.h"
00017 #include "TChain.h"
00018 #include "TDirectory.h"
00019 #include "TObjArray.h"
00020 #include "TCollection.h"  // Declares TIter
00021 #include "DigiRootData/DigiEvent.h"
00022 
00023 //#include "RootCnvSvc/commonData.h"
00024 
00025 #include "RootIO/IRootIoSvc.h"
00026 
00034 class digiRootReaderAlg : public Algorithm
00035 {       
00036 public:
00037     
00038     digiRootReaderAlg(const std::string& name, ISvcLocator* pSvcLocator);
00039     
00041     StatusCode initialize();
00042    
00044     StatusCode execute();
00045     
00047     StatusCode finalize();
00048             
00049 private:
00050 
00052     StatusCode readDigiEvent();
00053 
00054 
00056     StatusCode readMdcDigi();
00057 
00059     void close();
00060    
00062     TFile *m_digiFile;
00064     TChain *m_digiTree;
00066     DigiEvent *m_digiEvt;
00068     std::string m_fileName;
00070     StringArrayProperty m_fileList;
00072     std::string m_treeName;
00074     int m_numEvents;
00075   
00076 //    commonData m_common;
00077     IRootIoSvc* m_rootIoSvc;
00078 
00079 };
00080 
00081 static const AlgFactory<digiRootReaderAlg>  Factory;
00082 const IAlgFactory& digiRootReaderAlgFactory = Factory;
00083 
00084 
00085 digiRootReaderAlg::digiRootReaderAlg(const std::string& name, ISvcLocator* pSvcLocator) : 
00086 Algorithm(name, pSvcLocator)
00087 {
00088     // Input pararmeters that may be set via the jobOptions file
00089     // Input ROOT file name
00090     declareProperty("digiRootFile",m_fileName="");
00091     StringArrayProperty initList;
00092     std::vector<std::string> initVec;
00093     initVec.push_back("digicopy.root");
00094     initList.setValue(initVec);
00095     declareProperty("digiRootFileList",m_fileList=initList);
00096     // Input TTree name
00097     initVec.clear();
00098     declareProperty("digiTreeName", m_treeName="Rec");// wensp midify for test 2005/05/14
00099 
00100 }
00101 
00102 StatusCode digiRootReaderAlg::initialize()
00103 {
00104     // Purpose and Method:  Called once before the run begins.  This method
00105     //    opens a new ROOT file and prepares for reading.
00106 
00107     StatusCode sc = StatusCode::SUCCESS;
00108     MsgStream log(msgSvc(), name());
00109     
00110     // Use the Job options service to set the Algorithm's parameters
00111     // This will retrieve parameters set in the job options file
00112     setProperties();
00113    
00114     if ( service("RootIoSvc", m_rootIoSvc, true).isFailure() ){
00115         log << MSG::INFO << "Couldn't find the RootIoSvc!" << endreq;
00116         log << MSG::INFO << "Event loop will not terminate gracefully" << endreq;
00117         m_rootIoSvc = 0;
00118         //return StatusCode::FAILURE;
00119     } 
00120 
00121     facilities::Util::expandEnvVar(&m_fileName);
00122     
00123     // Save the current directory for the ntuple writer service
00124     TDirectory *saveDir = gDirectory;   
00125 
00126     m_digiTree = new TChain(m_treeName.c_str());
00127 
00128     std::string emptyStr("");
00129     if (m_fileName.compare(emptyStr) != 0) {
00130           TFile f(m_fileName.c_str());
00131       if (!f.IsOpen()) {
00132         log << MSG::ERROR << "ROOT file " << m_fileName.c_str()
00133             << " could not be opened for reading." << endreq;
00134         return StatusCode::FAILURE;
00135       }
00136           f.Close();
00137           m_digiTree->Add(m_fileName.c_str());
00138           log << MSG::INFO << "Opened file: " << m_fileName.c_str() << endreq;
00139     } else {
00140       const std::vector<std::string> fileList = m_fileList.value( );
00141       std::vector<std::string>::const_iterator it;
00142       std::vector<std::string>::const_iterator itend = fileList.end( );
00143       for (it = fileList.begin(); it != itend; it++) {
00144         std::string theFile = (*it);
00145             TFile f(theFile.c_str());
00146         if (!f.IsOpen()) {
00147           log << MSG::ERROR << "ROOT file " << theFile.c_str()
00148               << " could not be opened for reading." << endreq;
00149           return StatusCode::FAILURE;
00150         }
00151           f.Close();
00152           m_digiTree->Add(theFile.c_str());
00153           log << MSG::INFO << "Opened file: " << theFile.c_str() << endreq;
00154           }
00155     }
00156 
00157 
00158     m_digiEvt = 0;
00159     m_digiTree->SetBranchAddress("DigiEvent", &m_digiEvt);
00160     //m_common.m_digiEvt = m_digiEvt;
00161     m_numEvents = m_digiTree->GetEntries();
00162     
00163     if (m_rootIoSvc) {
00164       m_rootIoSvc->setRootEvtMax(m_numEvents);
00165       if (!m_digiTree->GetIndex()) m_digiTree->BuildIndex("m_runId", "m_eventId");
00166       m_rootIoSvc->registerRootTree(m_digiTree);
00167     }
00168 
00169 
00170     saveDir->cd();
00171     return sc;
00172     
00173 }
00174 
00175 StatusCode digiRootReaderAlg::execute()
00176 {
00177     // Purpose and Method:  Called once per event.  This method calls
00178     //   the appropriate methods to read data from the ROOT file and store
00179     //   data on the TDS.
00180 
00181     MsgStream log(msgSvc(), name());
00182 
00183     StatusCode sc = StatusCode::SUCCESS;
00184     
00185     if (m_digiEvt) m_digiEvt->Clear();
00186 
00187     static Int_t evtId = 0;
00188         int readInd, numBytes;
00189         std::pair<int,int> runEventPair = (m_rootIoSvc) ? m_rootIoSvc->runEventPair() : std::pair<int,int>(-1,-1);
00190         
00191         if ((m_rootIoSvc) && (m_rootIoSvc->index() >= 0)) {
00192                 readInd = m_rootIoSvc->index();
00193         } else if ((m_rootIoSvc) && (runEventPair.first != -1) && (runEventPair.second != -1)) {
00194                 int run = runEventPair.first;
00195                 int evt = runEventPair.second;
00196                 readInd = m_digiTree->GetEntryNumberWithIndex(run, evt);
00197         } else {
00198                 readInd = evtId;
00199         }
00200 
00201     if (readInd >= m_numEvents) {
00202         log << MSG::WARNING << "Requested index is out of bounds - no digi data loaded" << endreq;
00203         return StatusCode::SUCCESS;
00204     }
00205 
00206     numBytes = m_digiTree->GetEvent(readInd);
00207         
00208         if ((numBytes <= 0) || (!m_digiEvt)) {
00209             log << MSG::WARNING << "Failed to load digi event" << endreq;
00210             return StatusCode::SUCCESS;
00211         }
00212 
00213 
00214     sc = readDigiEvent();
00215     if (sc.isFailure()) {
00216         log << MSG::ERROR << "Failed to read top level DigiEvent" << endreq;
00217         return sc;
00218     }
00219 
00220     sc = readMdcDigi();
00221     if (sc.isFailure()) {
00222         log << MSG::ERROR << "Failed to load MdcDigi" << endreq;
00223         return sc;
00224     }
00225 
00226         evtId = readInd+1;
00227     return sc;
00228 }
00229 
00230 
00231 StatusCode digiRootReaderAlg::readDigiEvent() {
00232 
00233     MsgStream log(msgSvc(), name());
00234 
00235     StatusCode sc = StatusCode::SUCCESS;
00236     
00237     // Retrieve the Event data for this event
00238     SmartDataPtr<Event::EventHeader> evt(eventSvc(), EventModel::EventHeader);
00239     if (!evt) {
00240         log << MSG::ERROR << "Failed to retrieve Event" << endreq;
00241         return StatusCode::FAILURE;
00242     }
00243 
00244     unsigned int eventIdTds = evt->eventNumber();
00245     unsigned int runIdTds = evt->runNumber();
00246 
00247     unsigned int eventIdRoot = m_digiEvt->getEventId();
00248     unsigned int runIdRoot = m_digiEvt->getRunId();
00249 
00250     // Check to see if the event and run ids have already been set.
00251     if (eventIdTds != eventIdRoot) evt->setEventNumber(eventIdRoot);
00252     if (runIdTds != runIdRoot) evt->setRunNumber(runIdRoot);
00253 
00254 
00255     Event::DigiEvent* digiEventTds = 
00256         SmartDataPtr<Event::DigiEvent>(eventSvc(), EventModel::Digi::Event);
00257     if (!digiEventTds) {
00258         sc = eventSvc()->registerObject(EventModel::Digi::Event /*"/Event/Digi"*/,new DataObject);
00259         if( sc.isFailure() ) {
00260             log << MSG::ERROR << "could not register " << EventModel::Digi::Event /*<< /Event/Digi "*/ << endreq;
00261             return sc;
00262         }
00263     } else {
00264         bool fromMc = m_digiEvt->getFromMc();
00265         digiEventTds->initialize(fromMc);
00266     }
00267     return sc;
00268 }
00269 
00270 
00271 StatusCode digiRootReaderAlg::readMdcDigi() {
00272     MsgStream log(msgSvc(), name());
00273 
00274     StatusCode sc = StatusCode::SUCCESS;
00275     const TObjArray *mdcDigiRootCol = m_digiEvt->getMdcDigiCol();
00276     if (!mdcDigiRootCol) return sc;
00277     TIter mdcDigiIter(mdcDigiRootCol);
00278 
00279     // create the TDS location for the EmcDigi Collection
00280     MdcDigiCol* mdcDigiTdsCol = new MdcDigiCol;
00281     sc = eventSvc()->registerObject(EventModel::Digi::MdcDigiCol, mdcDigiTdsCol);
00282     if (sc.isFailure()) {
00283         log << "Failed to register MdcDigi Collection" << endreq;
00284         return StatusCode::FAILURE;
00285     }
00286 
00287 
00288     
00289     TMdcDigi *mdcDigiRoot = 0;
00290     while ((mdcDigiRoot = (TMdcDigi*)mdcDigiIter.Next())!=0) {
00291             mdcDigiRoot->Print();
00292     }
00293 
00294     return sc;
00295 }
00296 
00297 void digiRootReaderAlg::close() 
00298 {
00299     // Purpose and Method:  Writes the ROOT file at the end of the run.
00300     //    The TObject::kOverWrite parameter is used in the Write method
00301     //    since ROOT will periodically write to the ROOT file when the bufSize
00302     //    is filled.  Writing would create 2 copies of the same tree to be
00303     //    stored in the ROOT file, if we did not specify kOverwrite.
00304 
00305     if (m_digiTree) delete m_digiTree;
00306 }
00307 
00308 StatusCode digiRootReaderAlg::finalize()
00309 {
00310     close();
00311     
00312     StatusCode sc = StatusCode::SUCCESS;
00313     return sc;
00314 }
00315 

Generated on Tue Nov 29 23:11:42 2016 for BOSS_7.0.2 by  doxygen 1.4.7