/home/bes3soft/bes3soft/Boss/7.0.2/dist/7.0.2/Event/RootIO/RootIO-00-01-31/RootIO/digiRootWriterAlg.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 "TDirectory.h"
00018 
00019 #include "DigiRootData/DigiEvent.h"
00020 
00021 #include "RootCnvSvc/commonData.h"
00022 
00023 #include "RootIO/IRootIoSvc.h"
00024 
00031 class digiRootWriterAlg : public Algorithm
00032 {       
00033 public:
00034     
00035     digiRootWriterAlg(const std::string& name, ISvcLocator* pSvcLocator);
00036     
00038     StatusCode initialize();
00039    
00041     StatusCode execute();
00042     
00044     StatusCode finalize();
00045 
00046 private:
00047 
00049     StatusCode writeDigiEvent();
00050 
00051 
00054     StatusCode writeMdcDigi();
00055 
00057     void writeEvent();
00059     void close();
00060    
00062     TFile *m_digiFile;
00064     TTree *m_digiTree;
00066     DigiEvent *m_digiEvt;
00068     std::string m_fileName;
00070     std::string m_treeName;
00072     int m_splitMode;
00074     int m_bufSize;
00076     int m_compressionLevel;
00078     int m_autoSaveEvents;
00079     
00080     commonData m_common;
00081     IRootIoSvc* m_rootIoSvc;
00082 
00083 };
00084 
00085 static const AlgFactory<digiRootWriterAlg>  Factory;
00086 const IAlgFactory& digiRootWriterAlgFactory = Factory;
00087 
00088 digiRootWriterAlg::digiRootWriterAlg(const std::string& name, 
00089                                  ISvcLocator* pSvcLocator) : 
00090 Algorithm(name, pSvcLocator)
00091 {
00092     // Input parameters available to be set via the jobOptions file
00093     declareProperty("digiRootFile",m_fileName="digi.root");
00094     declareProperty("splitMode", m_splitMode=1);
00095     declareProperty("bufferSize", m_bufSize=64000);
00096     declareProperty("compressionLevel", m_compressionLevel=1);
00097     //declareProperty("treeName", m_treeName="Digi");
00098     declareProperty("treeName", m_treeName="Rec"); //wensp modified on 20050515 for test
00099     declareProperty("autoSave", m_autoSaveEvents=1000);
00100 
00101 }
00102 
00103 StatusCode digiRootWriterAlg::initialize()
00104 {
00105     // Purpose and Method:  Called once before the run begins.  This method
00106     //    opens a new ROOT file and prepares for writing.
00107 
00108     StatusCode sc = StatusCode::SUCCESS;
00109     MsgStream log(msgSvc(), name());
00110     
00111     // Use the Job options service to set the Algorithm's parameters
00112     // This will retrieve parameters set in the job options file
00113     setProperties();
00114 
00115     if ( service("RootIoSvc", m_rootIoSvc, true).isFailure() ){
00116         log << MSG::INFO << "Couldn't find the RootIoSvc!" << endreq;
00117         log << MSG::INFO << "No Auto Saving" << endreq;
00118         m_rootIoSvc = 0;
00119     } 
00120 
00121     facilities::Util::expandEnvVar(&m_fileName);
00122 
00123     // Save the current directory for the ntuple writer service
00124     TDirectory *saveDir = gDirectory;   
00125     // Create the new ROOT file
00126     m_digiFile = new TFile(m_fileName.c_str(), "RECREATE");
00127     if (!m_digiFile->IsOpen()) {
00128         log << MSG::ERROR << "ROOT file " << m_fileName 
00129             << " could not be opened for writing." << endreq;
00130         return StatusCode::FAILURE;
00131     }
00132     m_digiFile->cd();
00133     m_digiFile->SetCompressionLevel(m_compressionLevel);
00134     m_digiTree = new TTree(m_treeName.c_str(), "Bes Digitization Data");
00135     m_digiEvt = new DigiEvent();
00136     m_common.m_digiEvt = m_digiEvt;
00137     m_digiTree->Branch("DigiEvent","DigiEvent", &m_digiEvt, m_bufSize, m_splitMode);
00138     saveDir->cd();
00139     return sc;
00140     
00141 }
00142 
00143 StatusCode digiRootWriterAlg::execute()
00144 {
00145     // Purpose and Method:  Called once per event.  This method calls
00146     //   the appropriate methods to read data from the TDS and write data
00147     //   to the ROOT file.
00148 
00149     MsgStream log(msgSvc(), name());
00150 
00151     StatusCode sc = StatusCode::SUCCESS;
00152     
00153     if (!m_digiFile->IsOpen()) {
00154         log << MSG::ERROR << "ROOT file " << m_fileName 
00155             << " could not be opened for writing." << endreq;
00156         return StatusCode::FAILURE;
00157     }
00158     
00159     m_digiEvt->Clear();
00160 
00161     sc = writeDigiEvent();
00162     if (sc.isFailure()) {
00163         log << MSG::ERROR << "Failed to write DigiEvent" << endreq;
00164         return sc;
00165     }
00166 
00167 
00168     sc = writeMdcDigi();
00169     if (sc.isFailure()) {
00170         log << MSG::ERROR << "Failed to write Tkr Digi Collection" << endreq;
00171         return sc;
00172     }
00173   
00174     writeEvent();
00175     return sc;
00176 }
00177 
00178 
00179 StatusCode digiRootWriterAlg::writeDigiEvent() {
00180     // Purpose and Method:  Retrieve the Event object from the TDS and set the
00181     //    event and run numbers in the DigiEvent ROOT object
00182 
00183     MsgStream log(msgSvc(), name());
00184     StatusCode sc = StatusCode::SUCCESS;
00185 
00186     // Retrieve the Event data for this event
00187  SmartDataPtr<Event::EventHeader> eventHeader(eventSvc(),"/Event");
00188     if (!eventHeader) return sc;
00189 
00190     Short_t   runId = eventHeader->runNumber();
00191     Short_t  evtId  = eventHeader->eventNumber();
00192     Bool_t fromMc = true;
00193 
00194 
00195     m_digiEvt->initialize(evtId, runId, fromMc);
00196    // m_digiEvt->Print();
00197     
00198     return sc;
00199 }
00200 
00201 StatusCode digiRootWriterAlg::writeMdcDigi() {
00202     // Purpose and Method:  Retrieve the TkrDigi collection from the TDS and set the
00203     //    TkrDigi ROOT collection
00204 
00205     MsgStream log(msgSvc(), name());
00206     StatusCode sc = StatusCode::SUCCESS;
00207     
00208     SmartDataPtr<MdcDigiCol> mdcDigiColTds(eventSvc(), EventModel::Digi::MdcDigiCol);
00209     if (!mdcDigiColTds) return sc;
00210     MdcDigiCol::const_iterator mdcDigiTds;
00211 
00212     for (mdcDigiTds = mdcDigiColTds->begin(); mdcDigiTds != mdcDigiColTds->end(); mdcDigiTds++) {
00213           UInt_t overflow  = (*mdcDigiTds)->getOverflow();
00214           UInt_t time      = (*mdcDigiTds)->getTimeChannel();
00215           UInt_t charge    = (*mdcDigiTds)->getChargeChannel();
00216           UInt_t id        = (*mdcDigiTds)->getIntId();
00217           TMdcDigi *mdcDigiRoot = new TMdcDigi();
00218           m_common.m_mdcDigiMap[(*mdcDigiTds)] = mdcDigiRoot;
00219 
00220          mdcDigiRoot->initialize(id, time ,charge);
00221          mdcDigiRoot->setOverflow(overflow);
00222          m_digiEvt->addMdcDigi(mdcDigiRoot);
00223 //       mdcDigiRoot->Print();
00224     }
00225 
00226     return sc;
00227 }
00228 
00229 void digiRootWriterAlg::writeEvent() 
00230 {
00231     // Purpose and Method:  Stores the DigiEvent data for this event in the ROOT
00232     //    tree.  The m_digiEvt object is cleared for the next event.
00233     static int eventCounter = 0;
00234     TDirectory *saveDir = gDirectory;
00235     m_digiTree->GetCurrentFile()->cd();
00236     //m_digiFile->cd();
00237     m_digiTree->Fill();
00238     //m_digiEvt->Clear();
00239     saveDir->cd();
00240     ++eventCounter;
00241     if (m_rootIoSvc)
00242         if (eventCounter % m_rootIoSvc->getAutoSaveInterval() == 0) m_digiTree->AutoSave();
00243 
00244     return;
00245 }
00246 
00247 void digiRootWriterAlg::close() 
00248 {
00249     // Purpose and Method:  Writes the ROOT file at the end of the run.
00250     //    The TObject::kWriteDelete parameter is used in the Write method
00251     //    replacing TObject::kOverwrite - supposed to be safer
00252     //    since ROOT will periodically write to the ROOT file when the bufSize
00253     //    is filled.  Writing would create 2 copies of the same tree to be
00254     //    stored in the ROOT file, if we did not specify kOverwrite.
00255 
00256     TDirectory *saveDir = gDirectory;
00257     TFile *f = m_digiTree->GetCurrentFile();
00258     //m_digiFile->cd();
00259     f->cd();
00260     m_digiTree->BuildIndex("m_runId", "m_eventId");
00261     f->Write(0, TObject::kWriteDelete);
00262     f->Close();
00263     saveDir->cd();
00264     return;
00265 }
00266 
00267 StatusCode digiRootWriterAlg::finalize()
00268 {
00269     close();
00270     
00271     StatusCode sc = StatusCode::SUCCESS;
00272     return sc;
00273 }
00274 

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