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"
18#include "TDirectory.h"
20#include "DigiRootData/DigiEvent.h"
50 StatusCode writeDigiEvent();
55 StatusCode writeMdcDigi();
56 StatusCode writeCgemDigi();
70 std::string m_fileName;
72 std::string m_treeName;
78 int m_compressionLevel;
91 ISvcLocator* pSvcLocator) :
92Algorithm(name, pSvcLocator)
95 declareProperty(
"digiRootFile",m_fileName=
"digi.root");
96 declareProperty(
"splitMode", m_splitMode=1);
97 declareProperty(
"bufferSize", m_bufSize=64000);
98 declareProperty(
"compressionLevel", m_compressionLevel=1);
100 declareProperty(
"treeName", m_treeName=
"Rec");
101 declareProperty(
"autoSave", m_autoSaveEvents=1000);
110 StatusCode sc = StatusCode::SUCCESS;
111 MsgStream log(
msgSvc(), name());
117 if ( service(
"RootIoSvc", m_rootIoSvc,
true).isFailure() ){
118 log << MSG::INFO <<
"Couldn't find the RootIoSvc!" << endreq;
119 log << MSG::INFO <<
"No Auto Saving" << endreq;
126 TDirectory *saveDir = gDirectory;
128 m_digiFile =
new TFile(m_fileName.c_str(),
"RECREATE");
129 if (!m_digiFile->IsOpen()) {
130 log << MSG::ERROR <<
"ROOT file " << m_fileName
131 <<
" could not be opened for writing." << endreq;
132 return StatusCode::FAILURE;
135 m_digiFile->SetCompressionLevel(m_compressionLevel);
136 m_digiTree =
new TTree(m_treeName.c_str(),
"Bes Digitization Data");
138 m_common.m_digiEvt = m_digiEvt;
139 m_digiTree->Branch(
"DigiEvent",
"DigiEvent", &m_digiEvt, m_bufSize, m_splitMode);
151 MsgStream log(
msgSvc(), name());
153 StatusCode sc = StatusCode::SUCCESS;
155 if (!m_digiFile->IsOpen()) {
156 log << MSG::ERROR <<
"ROOT file " << m_fileName
157 <<
" could not be opened for writing." << endreq;
158 return StatusCode::FAILURE;
163 sc = writeDigiEvent();
164 if (sc.isFailure()) {
165 log << MSG::ERROR <<
"Failed to write DigiEvent" << endreq;
171 if (sc.isFailure()) {
172 log << MSG::ERROR <<
"Failed to write Tkr Digi Collection" << endreq;
176 sc = writeCgemDigi();
177 if (sc.isFailure()) {
178 log << MSG::ERROR <<
"Failed to write Cgem Tkr Digi Collection" << endreq;
187StatusCode digiRootWriterAlg::writeDigiEvent() {
191 MsgStream log(
msgSvc(), name());
192 StatusCode sc = StatusCode::SUCCESS;
195 SmartDataPtr<Event::EventHeader> eventHeader(eventSvc(),
"/Event");
196 if (!eventHeader)
return sc;
198 Short_t runId = eventHeader->runNumber();
199 Short_t evtId = eventHeader->eventNumber();
200 Bool_t fromMc =
true;
209StatusCode digiRootWriterAlg::writeMdcDigi() {
213 MsgStream log(
msgSvc(), name());
214 StatusCode sc = StatusCode::SUCCESS;
217 if (!mdcDigiColTds)
return sc;
218 MdcDigiCol::const_iterator mdcDigiTds;
220 for (mdcDigiTds = mdcDigiColTds->begin(); mdcDigiTds != mdcDigiColTds->end(); mdcDigiTds++) {
221 UInt_t overflow = (*mdcDigiTds)->getOverflow();
222 UInt_t
time = (*mdcDigiTds)->getTimeChannel();
223 UInt_t charge = (*mdcDigiTds)->getChargeChannel();
224 UInt_t
id = (*mdcDigiTds)->getIntId();
230 m_digiEvt->addMdcDigi(mdcDigiRoot);
237StatusCode digiRootWriterAlg::writeCgemDigi() {
241 MsgStream log(
msgSvc(), name());
242 StatusCode sc = StatusCode::SUCCESS;
245 if (!cgemDigiColTds)
return sc;
246 CgemDigiCol::const_iterator cgemDigiTds;
248 for (cgemDigiTds = cgemDigiColTds->begin(); cgemDigiTds != cgemDigiColTds->end(); cgemDigiTds++) {
249 UInt_t overflow = (*cgemDigiTds)->getOverflow();
250 UInt_t
time = (*cgemDigiTds)->getTimeChannel();
251 UInt_t charge = (*cgemDigiTds)->getChargeChannel();
252 UInt_t idMSB = (*cgemDigiTds)->getIntIdMSB();
253 UInt_t idLSB = (*cgemDigiTds)->getIntIdLSB();
259 m_digiEvt->addCgemDigi(cgemDigiRoot);
266void digiRootWriterAlg::writeEvent()
270 static int eventCounter = 0;
271 TDirectory *saveDir = gDirectory;
272 m_digiTree->GetCurrentFile()->cd();
284void digiRootWriterAlg::close()
293 TDirectory *saveDir = gDirectory;
294 TFile *f = m_digiTree->GetCurrentFile();
297 m_digiTree->BuildIndex(
"m_runId",
"m_eventId");
298 f->Write(0, TObject::kWriteDelete);
308 StatusCode sc = StatusCode::SUCCESS;
definition of the interface for IRootIoSvc
void initialize(bool fromMc)
The RootIoSvc gaudi service interface.
virtual int getAutoSaveInterval()=0
void setOverflow(const UInt_t overflow)
void setOverflow(const UInt_t overflow)
void initialize(UInt_t id, UInt_t time=0, UInt_t charge=0)
static std::map< const CgemDigi *, TRef > m_cgemDigiMap
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(")"))
const IAlgFactory & digiRootWriterAlgFactory
_EXTERN_ std::string MdcDigiCol
_EXTERN_ std::string CgemDigiCol