00001 #define ROOTHISTCNV_RCONVERTER_CPP
00002
00003
00004
00005 #include "GaudiKernel/xtoa.h"
00006 #include "GaudiKernel/CnvFactory.h"
00007 #include "GaudiKernel/GenericAddress.h"
00008 #include "GaudiKernel/IDataProviderSvc.h"
00009
00010 #include "GaudiKernel/MsgStream.h"
00011 #include "GaudiKernel/RegistryEntry.h"
00012 #include "GaudiKernel/NTuple.h"
00013 #include "RConverter.h"
00014
00015 #include "TDirectory.h"
00016 #include "TFile.h"
00017 #include "TTree.h"
00018 #include <string>
00019 #include <list>
00020
00021
00022 TFile *RootHistCnv::RConverter::m_rootfile = 0;
00023 std::map<std::string,TTree*> RootHistCnv::RConverter::m_treeMap;
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038 StatusCode RootHistCnv::RConverter::createSubDirs(const std::string& , const std::string& ) {
00039 MsgStream log(msgSvc(), "RConverter");
00040
00041
00042 return StatusCode::SUCCESS;
00043 }
00044
00045
00047 StatusCode RootHistCnv::RConverter::createDirectory(const std::string& loc) {
00048 MsgStream log(msgSvc(), "RConverter");
00049
00050
00051 std::string full;
00052 full = diskDirectory( loc );
00053
00054
00055 int p,i;
00056 std::string fil,cur,s;
00057 TDirectory *gDir;
00058
00059 gDir = gDirectory;
00060
00061 std::list<std::string> lpath;
00062 i = 1;
00063
00064 if ( (p=full.find(":",0)) != -1 ) {
00065 fil = full.substr(0,p);
00066 i = p+1;
00067 fil += ":/";
00068 gDirectory->cd(fil.c_str());
00069 }
00070
00071 while ( (p = full.find("/",i)) != -1) {
00072 s = full.substr(i,p-i);
00073 lpath.push_back(s);
00074 i = p+1;
00075 }
00076 lpath.push_back( full.substr(i,full.length()-i) );
00077
00078 if ( full.substr(0,1) == "/") {
00079 gDirectory->cd("/");
00080 }
00081
00082 std::list<std::string>::const_iterator litr;
00083 for(litr=lpath.begin(); litr!=lpath.end(); ++litr) {
00084 cur = *litr;
00085 if (! gDirectory->GetKey(litr->c_str()) ) {
00086 gDirectory->mkdir(litr->c_str());
00087 }
00088 gDirectory->cd(litr->c_str());
00089 }
00090
00091 gDirectory = gDir;
00092
00093 return StatusCode::SUCCESS;
00094 }
00095
00096
00099 std::string RootHistCnv::RConverter::diskDirectory(const std::string& loc) {
00100
00101
00102 std::string dir;
00103 long ll = loc.find("/NTUPLES/");
00104 if (ll == -1) {
00105 MsgStream log(msgSvc(), "RConverter");
00106 log << MSG::ERROR << "diskDirectory(" << loc << ")"
00107 << " --> no leading /NTUPLES/" << endreq;
00108 return loc;
00109 }
00110 dir = loc.substr(ll+8,loc.length()-ll-8);
00111
00112 return dir;
00113 }
00114
00115
00117 std::string RootHistCnv::RConverter::directory(const std::string& loc) {
00118
00119 return ( diskDirectory(loc) );
00120
00121 }
00122
00123
00124
00125 void RootHistCnv::RConverter::setDirectory(const std::string& loc) {
00126 MsgStream log(msgSvc(), "RConverter");
00127
00128
00129
00130 std::string full;
00131
00132
00133 full = diskDirectory( loc );
00134
00135 int p,i=1;
00136 std::string cur,sdir;
00137
00138 gDirectory->cd("/");
00139 while ( (p = full.find("/",i)) != -1) {
00140 sdir = full.substr(i,p-i);
00141 if (! gDirectory->GetKey(sdir.c_str()) ) {
00142 log << MSG::ERROR << "cannot cd to " << full << endreq;
00143 return;
00144 }
00145 gDirectory->cd(sdir.c_str());
00146
00147 i = p+1;
00148 }
00149 gDirectory->cd( full.substr(i,full.length()-i).c_str() );
00150
00151
00152
00153 }
00154
00155
00156
00157 void RootHistCnv::RConverter::setDiskDirectory(const std::string& loc) {
00158 MsgStream log(msgSvc(), "RConverter");
00159
00160
00161
00162 setDirectory(loc);
00163
00164
00165 }
00166
00168 std::string RootHistCnv::RConverter::getDirectory() {
00169
00170 std::string dir = gDirectory->GetPath();
00171
00172 return (dir);
00173
00174 }
00175
00176
00178 StatusCode RootHistCnv::RConverter::createAddress(DataObject* pObject, long id, IOpaqueAddress*& refpAddress) {
00179
00180 refpAddress = pObject->address();
00181 if ( 0 == refpAddress ) {
00182 std::string db;
00183 DataObject* parent = pObject->parent();
00184 if ( parent ) {
00185 IOpaqueAddress* pA = parent->address();
00186 if ( pA ) {
00187 db = pA->dbName();
00188 }
00189 }
00190 return createAddress(objType(), db, pObject->fullpath(), id, refpAddress);
00191 }
00192 return StatusCode::SUCCESS;
00193 }
00194
00195
00197 StatusCode RootHistCnv::RConverter::createAddress(DataObject* pObject, const std::string& title, IOpaqueAddress*& refpAddress) {
00198
00199 const std::string loc = pObject->location();
00200 refpAddress = pObject->address();
00201 if ( 0 == refpAddress ) {
00202 std::string db;
00203 const std::string& path = pObject->fullpath();
00204 DataObject* parent = pObject->parent();
00205 if ( parent ) {
00206 IOpaqueAddress* pA = parent->address();
00207 if ( pA ) {
00208 db = pA->dbName();
00209 }
00210 }
00211 return createAddress(objType(), db, path, title, refpAddress);
00212 }
00213 return StatusCode::SUCCESS;
00214 }
00215
00216
00218 StatusCode RootHistCnv::RConverter::createAddress(const CLID& clid, const std::string& , const std::string& loc, const std::string& obj, IOpaqueAddress*& refpAddress) {
00219 GenericAddress* pA = new GenericAddress(repSvcType() , clid);
00220 std::string fullname = loc;
00221 if ( obj[0] != '/' ) fullname += "/";
00222 fullname += obj;
00223 pA->genericInfo()->m_info[0] = 0;
00224 pA->setContainerName(loc);
00225 pA->setObjectName(fullname);
00226 refpAddress = pA;
00227 return StatusCode::SUCCESS;
00228 }
00229
00230
00232 StatusCode RootHistCnv::RConverter::createAddress(const CLID& clid, const std::string& db, const std::string& loc, long id, IOpaqueAddress*& refpAddress) {
00233 char obj[32];
00234 StatusCode status = createAddress(clid, db, loc, ::_itoa(id, obj, 10), refpAddress);
00235 if ( status.isSuccess() ) {
00236 refpAddress->genericLink()->genericInfo()->m_info[0] = id;
00237 }
00238 return status;
00239 }
00240
00241
00243 StatusCode RootHistCnv::RConverter::readObject(const std::string& loc, long ) {
00244 if ( createDirectory(loc).isSuccess() ) {
00245 setDiskDirectory(loc);
00246 setDirectory(loc);
00247
00248
00249
00250
00251 }
00252 return StatusCode::FAILURE;
00253 }
00254
00256 StatusCode RootHistCnv::RConverter::writeObject(const std::string& loc, long id) {
00257
00258 TTree *tree;
00259
00260 MsgStream log(msgSvc(), "RConverter");
00261
00262
00263
00264 setDirectory( loc );
00265 setDiskDirectory( loc );
00266
00267 char tmp[80];
00268 std::string rt_fid = loc + "/" + _itoa(id,tmp,10);
00269 if (findNTuple(rt_fid,tree).isSuccess()) {
00270
00273
00274
00275
00276
00277
00278
00279
00280
00281 }
00282
00283 return StatusCode::SUCCESS;
00284 }
00285
00286 StatusCode RootHistCnv::RConverter::regNTuple(const std::string id, const TTree* tree) {
00287
00288 MsgStream log(msgSvc(), "RConverter");
00289
00290
00291 std::map<std::string,TTree*>::const_iterator imap;
00292 imap = m_treeMap.find(id);
00293
00294 if ( imap != m_treeMap.end() ) {
00295 log << MSG::ERROR << "cannot register Ntuple " << id << ": already exists"
00296 << endreq;
00297 return StatusCode::FAILURE;
00298 }
00299
00300 m_treeMap[id] = const_cast<TTree*>(tree);
00301
00302 return StatusCode::SUCCESS;
00303
00304 }
00305
00306 StatusCode RootHistCnv::RConverter::findNTuple(const std::string id, TTree*& tree) {
00307
00308 MsgStream log(msgSvc(), "RConverter");
00309
00310 std::map<std::string,TTree*>::const_iterator imap;
00311 imap = m_treeMap.find(id);
00312
00313 if ( imap == m_treeMap.end() ) {
00314
00315 tree = 0;
00316 return StatusCode::FAILURE;
00317 }
00318
00319 tree = (*imap).second;
00320
00321 return StatusCode::SUCCESS;
00322 }
00323
00324
00326 const unsigned char RootHistCnv::RConverter::storageType() {
00327 return ROOT_StorageType;
00328 }
00329
00330
00332 RootHistCnv::RConverter::RConverter( const CLID& clid, ISvcLocator* svc )
00333 : Converter(RootHistCnv::RConverter::storageType(), clid, svc) {
00334 }
00335
00336
00338 RootHistCnv::RConverter::~RConverter() {
00339 }