CGEM BOSS 6.6.5.f
BESIII Offline Software System
Loading...
Searching...
No Matches
RootCalBaseCnv.cxx
Go to the documentation of this file.
1// $Header: /bes/bes/BossCvs/Calibration/CalibSvc/CalibROOTCnv/src/cnv/RootCalBaseCnv.cxx,v 1.3 2011/02/17 05:12:53 maqm Exp $
2/**
3 @file RootCalBaseCnv.cxx
4
5 Implementation file for Root calibration converter base class
6*/
7
8#include "RootCalBaseCnv.h"
9
10#include "GaudiKernel/CnvFactory.h"
11#include "GaudiKernel/IOpaqueAddress.h"
12#include "GaudiKernel/DataObject.h"
13#include "GaudiKernel/IAddressCreator.h"
14#include "GaudiKernel/IDataProviderSvc.h"
15#include "GaudiKernel/IConversionSvc.h"
16#include "GaudiKernel/MsgStream.h"
17
18#include "facilities/Util.h" // for translating env variables
19#include "CalibDataSvc/ICalibRootSvc.h"
20#include "CalibDataSvc/ICalibMetaCnvSvc.h"
21#include "CalibDataSvc/IInstrumentName.h"
22#include "CalibData/CalibBase.h"
23#include "CalibData/CalibBase1.h"
24// Guessing at needed Root includes
25#include "TROOT.h" // need this for cd??
26#include "TFile.h"
27#include "TTree.h"
28#include "TObject.h"
29
30
32 // release TFile, TTree if they need releasing. With normal
33 // termination they should already have been released.
34
35 // doClean();
36
37}
38
39// static CnvFactory<RootCalBaseCnv> s_factory;
40// const ICnvFactory& RootCalBaseCnvFactory = s_factory;
41RootCalBaseCnv::RootCalBaseCnv( ISvcLocator* svc, const CLID& clid) :
43 m_rootSvc (0), m_metaSvc(0), m_instrSvc(0), m_vstart(0), m_vend(0),
44 m_outFile(0), m_ttree(0), m_inFile(0), m_saveDir(0) {}
45
47 StatusCode status = Converter::initialize();
48
49 IDataProviderSvc* dp;
50
51 // I guess the service names are assigned in jobOptions?
52
53 /*serviceLocator()->getService ("CalibDataSvc",
54 IID_IDataProviderSvc,
55 (IInterface*&)dp);*/
56 serviceLocator()->getService ("CalibDataSvc",
57 IDataProviderSvc::interfaceID(),
58 (IInterface*&)dp);
59 setDataProvider(dp);
60
61 // Locate the Root Conversion Service
62 serviceLocator()->getService ("CalibRootCnvSvc",
63 IID_ICalibRootSvc,
64 (IInterface*&) m_rootSvc);
65
66 // Locate meta conversion service
67 // Will anything need to be changed here to accommodate possibility
68 // of two concrete implementations of ICalibMetaCnvSvc? Would
69 // have different storage types. Could specify type desired
70 // as job option. Ditto for name of class?
71 serviceLocator()->getService("CalibMySQLCnvSvc",
72 IID_ICalibMetaCnvSvc,
73 (IInterface*&)m_metaSvc);
74
75 serviceLocator()->getService ("CalibDataSvc",
76 IID_IInstrumentName,
77 (IInterface*&)m_instrSvc);
78
79 return status;
80}
81
83 return Converter::finalize();
84}
85
86
87 /****** ROOT services *****/
88
89StatusCode RootCalBaseCnv::createRoot(const std::string& /* fname */,
90 CalibData::CalibBase1* /* pTDSObj */) {
91 MsgStream log(msgSvc(), "RootCalBaseCnv");
92 log << MSG::ERROR
93 << "createRoot method not implemented for this calibration type"
94 << endreq;
95 return StatusCode::FAILURE;
96}
97
98StatusCode RootCalBaseCnv::openRead(const std::string& fname) {
99
100 MsgStream log(msgSvc(), "RootCalBaseCnv");
101
102 // Check fname isn't empty
103 if (fname == std::string("")) return StatusCode::FAILURE;
104
105 if (doClean() ) {
106 log << MSG::WARNING << "Previous operation didn't clean up! " << endreq;
107 }
108 m_saveDir = gDirectory;
109
110 std::string ourName(fname);
112
113 m_inFile = new TFile(ourName.c_str());
114
115 if (!m_inFile->IsOpen() ) {
116 log << MSG::ERROR << "ROOT file " << ourName
117 << "could not be opened for reading " << endreq;
118 delete m_inFile;
119 m_inFile = 0;
120 return StatusCode::FAILURE;
121 }
122 else {
123 log << MSG::INFO
124 << "Successfully opened ROOT file " << fname << " aka " << ourName
125 << " for reading " << endreq;
126 }
127
128
129 m_inFile->cd(); // Maybe will need this
130
131
132 return StatusCode::SUCCESS;
133}
134
136 m_inFile->Close();
137
138 delete m_inFile;
139 m_inFile = 0;
140
141 if (m_saveDir) {
142 m_saveDir->cd();
143 m_saveDir = 0;
144 }
145 return StatusCode::SUCCESS;
146}
147
148StatusCode RootCalBaseCnv::openWrite(const std::string& fname) {
149
150 MsgStream log(msgSvc(), "RootCalBaseCnv");
151
152 // Check fname isn't empty
153 if (fname == std::string("")) return StatusCode::FAILURE;
154
155 std::string ourName(fname);
157
158 if (doClean() ) {
159 log << MSG::WARNING << "Previous operation didn't clean up! " << endreq;
160 }
161
162 m_saveDir = gDirectory;
163
164
165 m_outFile = new TFile(ourName.c_str(), "RECREATE");
166 if (!m_outFile->IsOpen()) {
167 log << MSG::ERROR << "ROOT file " << fname << " aka " << ourName
168 << " could not be opened for writing" << endreq;
169 delete m_outFile;
170 m_outFile = 0;
171 return StatusCode::FAILURE;
172 }
173 else {
174 log << MSG::INFO
175 << "Successfully opened ROOT file " << fname << " aka " << ourName
176 << " for writing " << endreq;
177 }
178 m_outFile->cd();
179 return StatusCode::SUCCESS;
180}
181
183
184 MsgStream log(msgSvc(), "RootCalBaseCnv");
185
186 StatusCode ret = StatusCode::SUCCESS;
187
188 m_outFile->cd();
189 m_outFile->Close();
190 delete m_outFile;
191 m_outFile = 0;
192 if (m_saveDir) m_saveDir->cd();
193 m_saveDir = 0;
194 return ret;
195}
196
197
198StatusCode RootCalBaseCnv::readRootObj(const std::string& treename,
199 const std::string& branch,
200 TObject*& pObj, unsigned ix){
201 TTree* pTree = (TTree*)m_inFile->Get(treename.c_str());
202
203 return readRootObj(pTree, branch, pObj, ix);
204 }
205
206StatusCode RootCalBaseCnv::readRootObj(TTree* pTree,
207 const std::string& branch,
208 TObject*& pObj, unsigned ix){
209 TBranch* pBranch=pTree->GetBranch(branch.c_str());
210 pBranch->SetAddress(&pObj);
211 int nBytes = pBranch->GetEntry(ix);
212 return (nBytes > 0) ? StatusCode::SUCCESS : StatusCode::FAILURE;
213 }
214
215bool RootCalBaseCnv::doClean() {
216 bool ret = false;
217
218 if (m_outFile) {
219 m_outFile->Close();
220 delete m_outFile;
221 m_outFile = 0;
222 ret = true;
223 }
224
225 if (m_inFile) {
226 m_inFile->Close();
227 delete m_inFile;
228 m_inFile = 0;
229 ret = true;
230 }
231 m_ttree = 0;
232 if (m_saveDir) {
233 m_saveDir->cd();
234 m_saveDir = 0;
235 }
236 return ret;
237}
238
239
240// Do our part to write out object -- which is nothing
242 TObject* /* pRootObj */) {
243
244 // Get instrument name from InstrumentName service Now handled by
245 // RootCalBaseCnv
246 // TString instr = TString((m_instrSvc->getInstrumentName()).c_str());
247 // pRootObj->setInstrument(instr);
248 return StatusCode::SUCCESS;
249}
250
251// (To TDS) Conversion stuff
252StatusCode RootCalBaseCnv::createObj(IOpaqueAddress* addr,
253 DataObject*& refpObject) {
254 // StatusCode ret;
255
256 // first do the things we always need:
257 // First string parameter of opaque address is file ident
258 MsgStream log(msgSvc(), "RootCalBaseCnv");
259 log << MSG::DEBUG<<"RootCalBaseCnv::createObj( starting ...."<<endreq;
260 const std::string* par = addr->par();
261
262 std::string par0 = par[0];
263
264 return internalCreateObj(par0, refpObject, addr);
265
266}
267
268StatusCode RootCalBaseCnv::internalCreateObj(const std::string& fname,
269 DataObject*& refpObject,
270 IOpaqueAddress* address) {
271 MsgStream log(msgSvc(), "RootCalBaseCnv");
272 log << MSG::DEBUG<<"RootCalBaseCnv::internalCreateObj( starting ..... "<<endreq;
273 RootCalBaseCnv* converter = this;
274 CLID classId = address->clID();
275
276 IConverter* conv = this->conversionSvc()->converter(classId);
277 if (0 == conv) {
278 log << MSG::WARNING
279 << "No proper converter found for classID " << classId
280 << ", the default converter"
281 << " will be used. " << endreq;
282 } else {
283 converter = dynamic_cast <RootCalBaseCnv*> (conv);
284 if (0 == converter) {
285 log << MSG::ERROR
286 << "The converter found for classID " << classId
287 << " was not a descendent of RootCalBaseCnv as it should be "
288 << "( was of type " << typeid (*converter).name() << "). "
289 << "The default converter will be used" << endreq;
290 converter = this;
291 }
292 }
293
294 m_runfrm =*( address->ipar());
295 m_runto =*( address->ipar()+1);
296 // creates an object for the node found
297 StatusCode sc = converter->i_createObj(fname, refpObject);
298 if (sc.isFailure()) {
299 return sc;
300 }
301 CalibData::CalibBase1* tmpObject = dynamic_cast <CalibData::CalibBase1*> (refpObject);
302 setBaseInfo(tmpObject);
303 // ends up the object construction
304 sc = converter->i_processObj(refpObject, address);
305 if (sc.isSuccess()) {
306 log << MSG::DEBUG << "Successfully created calib. object " << endreq;
307 }
308 closeRead();
309 return sc;
310}
311
312/*
313 Base class version of this routine shouldn't really be called
314 since it doesn't correspond to a TDS object.
315*/
316StatusCode RootCalBaseCnv::i_createObj (const std::string& /* fname */,
317 DataObject*& /* refpObject */) {
318 return StatusCode::FAILURE; // shouldn't ever get here
319}
320
321// Default is to do nothing. Derived classes may override.
322StatusCode RootCalBaseCnv::i_processObj(DataObject*, // pObject,
323 IOpaqueAddress* ) /* address */ {
324 return StatusCode::SUCCESS;
325}
326
327/// Another utility for derived classes to use
329 MsgStream log(msgSvc(), "RootCalBaseCnv");
330 log << MSG::DEBUG<<"set the runfrm and runto Numbers in the converter"<<endreq;
331 pObj->setrunfrm(m_runfrm);
332 pObj->setrunto(m_runto);
333}
virtual StatusCode i_createObj(const std::string &fname, DataObject *&refpObject)
virtual StatusCode createRoot(const std::string &fname, CalibData::CalibBase1 *pTDSObj)
StatusCode openRead(const std::string &fname)
IInstrumentName * m_instrSvc
virtual StatusCode i_processObj(DataObject *pObject, IOpaqueAddress *address)
In case there is additional work to do on the created object.
virtual StatusCode finalize()
StatusCode closeRead()
virtual StatusCode createObj(IOpaqueAddress *addr, DataObject *&refpObject)
virtual StatusCode readRootObj(const std::string &treename, const std::string &branch, TObject *&pCalib, unsigned index=0)
virtual StatusCode internalCreateObj(const std::string &fname, DataObject *&refpObject, IOpaqueAddress *address)
ICalibMetaCnvSvc * m_metaSvc
RootCalBaseCnv(ISvcLocator *svc, const CLID &clid)
StatusCode closeWrite()
virtual StatusCode initialize()
virtual StatusCode fillRoot(CalibData::CalibBase *pTDSObj, TObject *pRootObj)
TDirectory * m_saveDir
virtual ~RootCalBaseCnv()
ICalibRootSvc * m_rootSvc
virtual StatusCode openWrite(const std::string &fname)
void setBaseInfo(CalibData::CalibBase1 *pObj)
Another utility for derived classes to use.
static int expandEnvVar(std::string *toExpand, const std::string &openDel=std::string("$("), const std::string &closeDel=std::string(")"))