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
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
00098 declareProperty("treeName", m_treeName="Rec");
00099 declareProperty("autoSave", m_autoSaveEvents=1000);
00100
00101 }
00102
00103 StatusCode digiRootWriterAlg::initialize()
00104 {
00105
00106
00107
00108 StatusCode sc = StatusCode::SUCCESS;
00109 MsgStream log(msgSvc(), name());
00110
00111
00112
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
00124 TDirectory *saveDir = gDirectory;
00125
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
00146
00147
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
00181
00182
00183 MsgStream log(msgSvc(), name());
00184 StatusCode sc = StatusCode::SUCCESS;
00185
00186
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
00197
00198 return sc;
00199 }
00200
00201 StatusCode digiRootWriterAlg::writeMdcDigi() {
00202
00203
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
00224 }
00225
00226 return sc;
00227 }
00228
00229 void digiRootWriterAlg::writeEvent()
00230 {
00231
00232
00233 static int eventCounter = 0;
00234 TDirectory *saveDir = gDirectory;
00235 m_digiTree->GetCurrentFile()->cd();
00236
00237 m_digiTree->Fill();
00238
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
00250
00251
00252
00253
00254
00255
00256 TDirectory *saveDir = gDirectory;
00257 TFile *f = m_digiTree->GetCurrentFile();
00258
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