BOSS 7.1.0
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.8 2022/03/04 03:49:16 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
22#include "CalibData/CalibBase.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 // Locate the Root Conversion Service
61 serviceLocator()->getService ("CalibRootCnvSvc",
62 ICalibRootSvc::interfaceID(),
63 (IInterface*&) m_rootSvc);
64
65 // Locate meta conversion service
66 // Will anything need to be changed here to accommodate possibility
67 // of two concrete implementations of ICalibMetaCnvSvc? Would
68 // have different storage types. Could specify type desired
69 // as job option. Ditto for name of class?
70 serviceLocator()->getService("CalibMySQLCnvSvc",
71 ICalibMetaCnvSvc::interfaceID(),
72 (IInterface*&)m_metaSvc);
73 serviceLocator()->getService ("CalibDataSvc",
75 (IInterface*&)m_instrSvc);
76
77 return status;
78}
79
81 return Converter::finalize();
82}
83
84
85 /****** ROOT services *****/
86
87StatusCode RootCalBaseCnv::createRoot(const std::string& /* fname */,
88 CalibData::CalibBase1* /* pTDSObj */) {
89 MsgStream log(msgSvc(), "RootCalBaseCnv");
90 log << MSG::ERROR
91 << "createRoot method not implemented for this calibration type"
92 << endreq;
93 return StatusCode::FAILURE;
94}
95
96StatusCode RootCalBaseCnv::openRead(const std::string& fname) {
97
98 MsgStream log(msgSvc(), "RootCalBaseCnv");
99
100 // Check fname isn't empty
101 if (fname == std::string("")) return StatusCode::FAILURE;
102
103 if (doClean() ) {
104 log << MSG::WARNING << "Previous operation didn't clean up! " << endreq;
105 }
106 m_saveDir = gDirectory;
107
108 std::string ourName(fname);
110
111 m_inFile = new TFile(ourName.c_str());
112
113 if (!m_inFile->IsOpen() ) {
114 log << MSG::ERROR << "ROOT file " << ourName
115 << "could not be opened for reading " << endreq;
116 delete m_inFile;
117 m_inFile = 0;
118 return StatusCode::FAILURE;
119 }
120 else {
121 log << MSG::INFO
122 << "Successfully opened ROOT file " << fname << " aka " << ourName
123 << " for reading " << endreq;
124 }
125
126
127 m_inFile->cd(); // Maybe will need this
128
129
130 return StatusCode::SUCCESS;
131}
132
134 m_inFile->Close();
135
136 delete m_inFile;
137 m_inFile = 0;
138
139 if (m_saveDir) {
140 m_saveDir->cd();
141 m_saveDir = 0;
142 }
143 return StatusCode::SUCCESS;
144}
145
146StatusCode RootCalBaseCnv::openWrite(const std::string& fname) {
147
148 MsgStream log(msgSvc(), "RootCalBaseCnv");
149
150 // Check fname isn't empty
151 if (fname == std::string("")) return StatusCode::FAILURE;
152
153 std::string ourName(fname);
155
156 if (doClean() ) {
157 log << MSG::WARNING << "Previous operation didn't clean up! " << endreq;
158 }
159
160 m_saveDir = gDirectory;
161
162
163 m_outFile = new TFile(ourName.c_str(), "RECREATE");
164 if (!m_outFile->IsOpen()) {
165 log << MSG::ERROR << "ROOT file " << fname << " aka " << ourName
166 << " could not be opened for writing" << endreq;
167 delete m_outFile;
168 m_outFile = 0;
169 return StatusCode::FAILURE;
170 }
171 else {
172 log << MSG::INFO
173 << "Successfully opened ROOT file " << fname << " aka " << ourName
174 << " for writing " << endreq;
175 }
176 m_outFile->cd();
177 return StatusCode::SUCCESS;
178}
179
181
182 MsgStream log(msgSvc(), "RootCalBaseCnv");
183
184 StatusCode ret = StatusCode::SUCCESS;
185
186 m_outFile->cd();
187 m_outFile->Close();
188 delete m_outFile;
189 m_outFile = 0;
190 if (m_saveDir) m_saveDir->cd();
191 m_saveDir = 0;
192 return ret;
193}
194
195
196StatusCode RootCalBaseCnv::readRootObj(const std::string& treename,
197 const std::string& branch,
198 TObject*& pObj, unsigned ix){
199 TTree* pTree = (TTree*)m_inFile->Get(treename.c_str());
200
201 return readRootObj(pTree, branch, pObj, ix);
202 }
203
204StatusCode RootCalBaseCnv::readRootObj(TTree* pTree,
205 const std::string& branch,
206 TObject*& pObj, unsigned ix){
207 TBranch* pBranch=pTree->GetBranch(branch.c_str());
208 pBranch->SetAddress(&pObj);
209 int nBytes = pBranch->GetEntry(ix);
210 return (nBytes > 0) ? StatusCode::SUCCESS : StatusCode::FAILURE;
211 }
212
213bool RootCalBaseCnv::doClean() {
214 bool ret = false;
215
216 if (m_outFile) {
217 m_outFile->Close();
218 delete m_outFile;
219 m_outFile = 0;
220 ret = true;
221 }
222
223 if (m_inFile) {
224 m_inFile->Close();
225 delete m_inFile;
226 m_inFile = 0;
227 ret = true;
228 }
229 m_ttree = 0;
230 if (m_saveDir) {
231 m_saveDir->cd();
232 m_saveDir = 0;
233 }
234 return ret;
235}
236
237
238// Do our part to write out object -- which is nothing
240 TObject* /* pRootObj */) {
241
242 // Get instrument name from InstrumentName service Now handled by
243 // RootCalBaseCnv
244 // TString instr = TString((m_instrSvc->getInstrumentName()).c_str());
245 // pRootObj->setInstrument(instr);
246 return StatusCode::SUCCESS;
247}
248
249// (To TDS) Conversion stuff
250StatusCode RootCalBaseCnv::createObj(IOpaqueAddress* addr,
251 DataObject*& refpObject) {
252 // StatusCode ret;
253
254 // first do the things we always need:
255 // First string parameter of opaque address is file ident
256 MsgStream log(msgSvc(), "RootCalBaseCnv");
257 log << MSG::DEBUG<<"RootCalBaseCnv::createObj( starting ...."<<endreq;
258 const std::string* par = addr->par();
259
260 std::string par0 = par[0];
261
262 return internalCreateObj(par0, refpObject, addr);
263
264}
265
266StatusCode RootCalBaseCnv::internalCreateObj(const std::string& fname,
267 DataObject*& refpObject,
268 IOpaqueAddress* address) {
269 MsgStream log(msgSvc(), "RootCalBaseCnv");
270 log << MSG::DEBUG<<"RootCalBaseCnv::internalCreateObj( starting ..... "<<endreq;
271 RootCalBaseCnv* converter = this;
272 CLID classId = address->clID();
273
274 IConverter* conv = this->conversionSvc()->converter(classId);
275 if (0 == conv) {
276 log << MSG::WARNING
277 << "No proper converter found for classID " << classId
278 << ", the default converter"
279 << " will be used. " << endreq;
280 } else {
281 converter = dynamic_cast <RootCalBaseCnv*> (conv);
282 if (0 == converter) {
283 log << MSG::ERROR
284 << "The converter found for classID " << classId
285 << " was not a descendent of RootCalBaseCnv as it should be "
286 << "( was of type " << typeid (*converter).name() << "). "
287 << "The default converter will be used" << endreq;
288 converter = this;
289 }
290 }
291
292 m_runfrm =*( address->ipar());
293 m_runto =*( address->ipar()+1);
294 // creates an object for the node found
295 StatusCode sc = converter->i_createObj(fname, refpObject);
296 if (sc.isFailure()) {
297 return sc;
298 }
299 CalibData::CalibBase1* tmpObject = dynamic_cast <CalibData::CalibBase1*> (refpObject);
300 setBaseInfo(tmpObject);
301 // ends up the object construction
302 sc = converter->i_processObj(refpObject, address);
303 if (sc.isSuccess()) {
304 log << MSG::DEBUG << "Successfully created calib. object " << endreq;
305 }
306 closeRead();
307 return sc;
308}
309
310/*
311 Base class version of this routine shouldn't really be called
312 since it doesn't correspond to a TDS object.
313*/
314StatusCode RootCalBaseCnv::i_createObj (const std::string& /* fname */,
315 DataObject*& /* refpObject */) {
316 return StatusCode::FAILURE; // shouldn't ever get here
317}
318
319// Default is to do nothing. Derived classes may override.
320StatusCode RootCalBaseCnv::i_processObj(DataObject*, // pObject,
321 IOpaqueAddress* ) /* address */ {
322 return StatusCode::SUCCESS;
323}
324
325/// Another utility for derived classes to use
327 MsgStream log(msgSvc(), "RootCalBaseCnv");
328 log << MSG::DEBUG<<"set the runfrm and runto Numbers in the converter"<<endreq;
329 pObj->setrunfrm(m_runfrm);
330 pObj->setrunto(m_runto);
331}
unsigned const char CALIBROOT_StorageType
Definition: ICalibRootSvc.h:20
IMessageSvc * msgSvc()
void setrunto(int runto)
Definition: CalibBase1.h:56
void setrunfrm(int runfrm)
Definition: CalibBase1.h:55
static const InterfaceID & interfaceID()
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(")"))
#define ix(i)