1#include "GaudiKernel/MsgStream.h"
2#include "GaudiKernel/AlgFactory.h"
3#include "GaudiKernel/IDataProviderSvc.h"
4#include "GaudiKernel/SmartDataPtr.h"
5#include "GaudiKernel/Algorithm.h"
7#include "EventModel/Event.h"
8#include "EventModel/EventModel.h"
9#include "RawEvent/DigiEvent.h"
10#include "MDCRawEvent/MdcDigi.h"
12#include "RootCnvSvc/Util.h"
17#include "TDirectory.h"
19#include "DigiRootData/DigiEvent.h"
21#include "RootCnvSvc/commonData.h"
23#include "RootIO/IRootIoSvc.h"
49 StatusCode writeDigiEvent();
54 StatusCode writeMdcDigi();
68 std::string m_fileName;
70 std::string m_treeName;
76 int m_compressionLevel;
89 ISvcLocator* pSvcLocator) :
90Algorithm(name, pSvcLocator)
93 declareProperty(
"digiRootFile",m_fileName=
"digi.root");
94 declareProperty(
"splitMode", m_splitMode=1);
95 declareProperty(
"bufferSize", m_bufSize=64000);
96 declareProperty(
"compressionLevel", m_compressionLevel=1);
98 declareProperty(
"treeName", m_treeName=
"Rec");
99 declareProperty(
"autoSave", m_autoSaveEvents=1000);
108 StatusCode sc = StatusCode::SUCCESS;
109 MsgStream log(
msgSvc(), name());
115 if ( service(
"RootIoSvc", m_rootIoSvc,
true).isFailure() ){
116 log << MSG::INFO <<
"Couldn't find the RootIoSvc!" << endreq;
117 log << MSG::INFO <<
"No Auto Saving" << endreq;
124 TDirectory *saveDir = gDirectory;
126 m_digiFile =
new TFile(m_fileName.c_str(),
"RECREATE");
127 if (!m_digiFile->IsOpen()) {
128 log << MSG::ERROR <<
"ROOT file " << m_fileName
129 <<
" could not be opened for writing." << endreq;
130 return StatusCode::FAILURE;
133 m_digiFile->SetCompressionLevel(m_compressionLevel);
134 m_digiTree =
new TTree(m_treeName.c_str(),
"Bes Digitization Data");
136 m_common.m_digiEvt = m_digiEvt;
137 m_digiTree->Branch(
"DigiEvent",
"DigiEvent", &m_digiEvt, m_bufSize, m_splitMode);
149 MsgStream log(
msgSvc(), name());
151 StatusCode sc = StatusCode::SUCCESS;
153 if (!m_digiFile->IsOpen()) {
154 log << MSG::ERROR <<
"ROOT file " << m_fileName
155 <<
" could not be opened for writing." << endreq;
156 return StatusCode::FAILURE;
161 sc = writeDigiEvent();
162 if (sc.isFailure()) {
163 log << MSG::ERROR <<
"Failed to write DigiEvent" << endreq;
169 if (sc.isFailure()) {
170 log << MSG::ERROR <<
"Failed to write Tkr Digi Collection" << endreq;
179StatusCode digiRootWriterAlg::writeDigiEvent() {
183 MsgStream log(
msgSvc(), name());
184 StatusCode sc = StatusCode::SUCCESS;
187 SmartDataPtr<Event::EventHeader> eventHeader(eventSvc(),
"/Event");
188 if (!eventHeader)
return sc;
190 Short_t runId = eventHeader->runNumber();
191 Short_t evtId = eventHeader->eventNumber();
192 Bool_t fromMc =
true;
201StatusCode digiRootWriterAlg::writeMdcDigi() {
205 MsgStream log(
msgSvc(), name());
206 StatusCode sc = StatusCode::SUCCESS;
209 if (!mdcDigiColTds)
return sc;
210 MdcDigiCol::const_iterator mdcDigiTds;
212 for (mdcDigiTds = mdcDigiColTds->begin(); mdcDigiTds != mdcDigiColTds->end(); mdcDigiTds++) {
213 UInt_t overflow = (*mdcDigiTds)->getOverflow();
214 UInt_t
time = (*mdcDigiTds)->getTimeChannel();
215 UInt_t
charge = (*mdcDigiTds)->getChargeChannel();
216 UInt_t
id = (*mdcDigiTds)->getIntId();
222 m_digiEvt->addMdcDigi(mdcDigiRoot);
229void digiRootWriterAlg::writeEvent()
233 static int eventCounter = 0;
234 TDirectory *saveDir = gDirectory;
235 m_digiTree->GetCurrentFile()->cd();
247void digiRootWriterAlg::close()
256 TDirectory *saveDir = gDirectory;
257 TFile *
f = m_digiTree->GetCurrentFile();
260 m_digiTree->BuildIndex(
"m_runId",
"m_eventId");
261 f->Write(0, TObject::kWriteDelete);
271 StatusCode sc = StatusCode::SUCCESS;
const IAlgFactory & digiRootWriterAlgFactory
void initialize(bool fromMc)
The RootIoSvc gaudi service interface.
virtual int getAutoSaveInterval()=0
void setOverflow(const UInt_t overflow)
void initialize(UInt_t id, UInt_t time=0, UInt_t charge=0)
static std::map< const MdcDigi *, TRef > m_mdcDigiMap
Create a set of maps between Digi data in the TDS and the TRefs in the ROOT file.
Writes Digi TDS data to a persistent ROOT file. @Based on the digiRootWriterAlg of GLAST.
StatusCode initialize()
Handles setup by opening ROOT file in write mode and creating a new TTree.
StatusCode execute()
Orchastrates reading from TDS and writing to ROOT for each event.
digiRootWriterAlg(const std::string &name, ISvcLocator *pSvcLocator)
StatusCode finalize()
Closes the ROOT file and cleans up.
static int expandEnvVar(std::string *toExpand, const std::string &openDel=std::string("$("), const std::string &closeDel=std::string(")"))
TFile f("ana_bhabha660a_dqa_mcPat_zy_old.root")
_EXTERN_ std::string MdcDigiCol