00001
00008 #include "RootCalBaseCnv.h"
00009
00010 #include "GaudiKernel/CnvFactory.h"
00011 #include "GaudiKernel/IOpaqueAddress.h"
00012 #include "GaudiKernel/DataObject.h"
00013 #include "GaudiKernel/IAddressCreator.h"
00014 #include "GaudiKernel/IDataProviderSvc.h"
00015 #include "GaudiKernel/IConversionSvc.h"
00016 #include "GaudiKernel/MsgStream.h"
00017
00018 #include "facilities/Util.h"
00019 #include "CalibDataSvc/ICalibRootSvc.h"
00020 #include "CalibDataSvc/ICalibMetaCnvSvc.h"
00021 #include "CalibDataSvc/IInstrumentName.h"
00022 #include "CalibData/CalibBase.h"
00023 #include "CalibData/CalibBase1.h"
00024
00025 #include "TROOT.h"
00026 #include "TFile.h"
00027 #include "TTree.h"
00028 #include "TObject.h"
00029
00030
00031 RootCalBaseCnv::~RootCalBaseCnv() {
00032
00033
00034
00035
00036
00037 }
00038
00039
00040
00041 RootCalBaseCnv::RootCalBaseCnv( ISvcLocator* svc, const CLID& clid) :
00042 Converter (CALIBROOT_StorageType, clid, svc),
00043 m_rootSvc (0), m_metaSvc(0), m_instrSvc(0), m_vstart(0), m_vend(0),
00044 m_outFile(0), m_ttree(0), m_inFile(0), m_saveDir(0) {}
00045
00046 StatusCode RootCalBaseCnv::initialize() {
00047 StatusCode status = Converter::initialize();
00048
00049 IDataProviderSvc* dp;
00050
00051
00052
00053
00054
00055
00056 serviceLocator()->getService ("CalibDataSvc",
00057 IDataProviderSvc::interfaceID(),
00058 (IInterface*&)dp);
00059 setDataProvider(dp);
00060
00061
00062 serviceLocator()->getService ("CalibRootCnvSvc",
00063 IID_ICalibRootSvc,
00064 (IInterface*&) m_rootSvc);
00065
00066
00067
00068
00069
00070
00071 serviceLocator()->getService("CalibMySQLCnvSvc",
00072 IID_ICalibMetaCnvSvc,
00073 (IInterface*&)m_metaSvc);
00074
00075 serviceLocator()->getService ("CalibDataSvc",
00076 IID_IInstrumentName,
00077 (IInterface*&)m_instrSvc);
00078
00079 return status;
00080 }
00081
00082 StatusCode RootCalBaseCnv::finalize() {
00083 return Converter::finalize();
00084 }
00085
00086
00087
00088
00089 StatusCode RootCalBaseCnv::createRoot(const std::string& ,
00090 CalibData::CalibBase1* ) {
00091 MsgStream log(msgSvc(), "RootCalBaseCnv");
00092 log << MSG::ERROR
00093 << "createRoot method not implemented for this calibration type"
00094 << endreq;
00095 return StatusCode::FAILURE;
00096 }
00097
00098 StatusCode RootCalBaseCnv::openRead(const std::string& fname) {
00099
00100 MsgStream log(msgSvc(), "RootCalBaseCnv");
00101
00102
00103 if (fname == std::string("")) return StatusCode::FAILURE;
00104
00105 if (doClean() ) {
00106 log << MSG::WARNING << "Previous operation didn't clean up! " << endreq;
00107 }
00108 m_saveDir = gDirectory;
00109
00110 std::string ourName(fname);
00111 facilities::Util::expandEnvVar(&ourName);
00112
00113 m_inFile = new TFile(ourName.c_str());
00114
00115 if (!m_inFile->IsOpen() ) {
00116 log << MSG::ERROR << "ROOT file " << ourName
00117 << "could not be opened for reading " << endreq;
00118 delete m_inFile;
00119 m_inFile = 0;
00120 return StatusCode::FAILURE;
00121 }
00122 else {
00123 log << MSG::INFO
00124 << "Successfully opened ROOT file " << fname << " aka " << ourName
00125 << " for reading " << endreq;
00126 }
00127
00128
00129 m_inFile->cd();
00130
00131
00132 return StatusCode::SUCCESS;
00133 }
00134
00135 StatusCode RootCalBaseCnv::closeRead() {
00136 m_inFile->Close();
00137
00138 delete m_inFile;
00139 m_inFile = 0;
00140
00141 if (m_saveDir) {
00142 m_saveDir->cd();
00143 m_saveDir = 0;
00144 }
00145 return StatusCode::SUCCESS;
00146 }
00147
00148 StatusCode RootCalBaseCnv::openWrite(const std::string& fname) {
00149
00150 MsgStream log(msgSvc(), "RootCalBaseCnv");
00151
00152
00153 if (fname == std::string("")) return StatusCode::FAILURE;
00154
00155 std::string ourName(fname);
00156 facilities::Util::expandEnvVar(&ourName);
00157
00158 if (doClean() ) {
00159 log << MSG::WARNING << "Previous operation didn't clean up! " << endreq;
00160 }
00161
00162 m_saveDir = gDirectory;
00163
00164
00165 m_outFile = new TFile(ourName.c_str(), "RECREATE");
00166 if (!m_outFile->IsOpen()) {
00167 log << MSG::ERROR << "ROOT file " << fname << " aka " << ourName
00168 << " could not be opened for writing" << endreq;
00169 delete m_outFile;
00170 m_outFile = 0;
00171 return StatusCode::FAILURE;
00172 }
00173 else {
00174 log << MSG::INFO
00175 << "Successfully opened ROOT file " << fname << " aka " << ourName
00176 << " for writing " << endreq;
00177 }
00178 m_outFile->cd();
00179 return StatusCode::SUCCESS;
00180 }
00181
00182 StatusCode RootCalBaseCnv::closeWrite() {
00183
00184 MsgStream log(msgSvc(), "RootCalBaseCnv");
00185
00186 StatusCode ret = StatusCode::SUCCESS;
00187
00188 m_outFile->cd();
00189 m_outFile->Close();
00190 delete m_outFile;
00191 m_outFile = 0;
00192 if (m_saveDir) m_saveDir->cd();
00193 m_saveDir = 0;
00194 return ret;
00195 }
00196
00197
00198 StatusCode RootCalBaseCnv::readRootObj(const std::string& treename,
00199 const std::string& branch,
00200 TObject*& pObj, unsigned ix){
00201 TTree* pTree = (TTree*)m_inFile->Get(treename.c_str());
00202
00203 return readRootObj(pTree, branch, pObj, ix);
00204 }
00205
00206 StatusCode RootCalBaseCnv::readRootObj(TTree* pTree,
00207 const std::string& branch,
00208 TObject*& pObj, unsigned ix){
00209 TBranch* pBranch=pTree->GetBranch(branch.c_str());
00210 pBranch->SetAddress(&pObj);
00211 int nBytes = pBranch->GetEntry(ix);
00212 return (nBytes > 0) ? StatusCode::SUCCESS : StatusCode::FAILURE;
00213 }
00214
00215 bool RootCalBaseCnv::doClean() {
00216 bool ret = false;
00217
00218 if (m_outFile) {
00219 m_outFile->Close();
00220 delete m_outFile;
00221 m_outFile = 0;
00222 ret = true;
00223 }
00224
00225 if (m_inFile) {
00226 m_inFile->Close();
00227 delete m_inFile;
00228 m_inFile = 0;
00229 ret = true;
00230 }
00231 m_ttree = 0;
00232 if (m_saveDir) {
00233 m_saveDir->cd();
00234 m_saveDir = 0;
00235 }
00236 return ret;
00237 }
00238
00239
00240
00241 StatusCode RootCalBaseCnv::fillRoot(CalibData::CalibBase* ,
00242 TObject* ) {
00243
00244
00245
00246
00247
00248 return StatusCode::SUCCESS;
00249 }
00250
00251
00252 StatusCode RootCalBaseCnv::createObj(IOpaqueAddress* addr,
00253 DataObject*& refpObject) {
00254
00255
00256
00257
00258 MsgStream log(msgSvc(), "RootCalBaseCnv");
00259 log << MSG::DEBUG<<"RootCalBaseCnv::createObj( starting ...."<<endreq;
00260 const std::string* par = addr->par();
00261
00262 std::string par0 = par[0];
00263
00264 return internalCreateObj(par0, refpObject, addr);
00265
00266 }
00267
00268 StatusCode RootCalBaseCnv::internalCreateObj(const std::string& fname,
00269 DataObject*& refpObject,
00270 IOpaqueAddress* address) {
00271 MsgStream log(msgSvc(), "RootCalBaseCnv");
00272 log << MSG::DEBUG<<"RootCalBaseCnv::internalCreateObj( starting ..... "<<endreq;
00273 RootCalBaseCnv* converter = this;
00274 CLID classId = address->clID();
00275
00276 IConverter* conv = this->conversionSvc()->converter(classId);
00277 if (0 == conv) {
00278 log << MSG::WARNING
00279 << "No proper converter found for classID " << classId
00280 << ", the default converter"
00281 << " will be used. " << endreq;
00282 } else {
00283 converter = dynamic_cast <RootCalBaseCnv*> (conv);
00284 if (0 == converter) {
00285 log << MSG::ERROR
00286 << "The converter found for classID " << classId
00287 << " was not a descendent of RootCalBaseCnv as it should be "
00288 << "( was of type " << typeid (*converter).name() << "). "
00289 << "The default converter will be used" << endreq;
00290 converter = this;
00291 }
00292 }
00293
00294 m_runfrm =*( address->ipar());
00295 m_runto =*( address->ipar()+1);
00296
00297 StatusCode sc = converter->i_createObj(fname, refpObject);
00298 if (sc.isFailure()) {
00299 return sc;
00300 }
00301 CalibData::CalibBase1* tmpObject = dynamic_cast <CalibData::CalibBase1*> (refpObject);
00302 setBaseInfo(tmpObject);
00303
00304 sc = converter->i_processObj(refpObject, address);
00305 if (sc.isSuccess()) {
00306 log << MSG::DEBUG << "Successfully created calib. object " << endreq;
00307 }
00308 closeRead();
00309 return sc;
00310 }
00311
00312
00313
00314
00315
00316 StatusCode RootCalBaseCnv::i_createObj (const std::string& ,
00317 DataObject*& ) {
00318 return StatusCode::FAILURE;
00319 }
00320
00321
00322 StatusCode RootCalBaseCnv::i_processObj(DataObject*,
00323 IOpaqueAddress* ) {
00324 return StatusCode::SUCCESS;
00325 }
00326
00328 void RootCalBaseCnv::setBaseInfo(CalibData::CalibBase1* pObj) {
00329 MsgStream log(msgSvc(), "RootCalBaseCnv");
00330 log << MSG::DEBUG<<"set the runfrm and runto Numbers in the converter"<<endreq;
00331 pObj->setrunfrm(m_runfrm);
00332 pObj->setrunto(m_runto);
00333 }