| # Gaudi Framework, version v21r7p1 |

Home |  | Generated: 15 Feb 2010

# RFileCnv.cpp

Go to the documentation of this file.

    00001 // $Id: RFileCnv.cpp,v 1.14 2007/03/15 15:53:15 hmd Exp $
    00002 #define ROOTHISTCNV_RFILECNV_CPP
    00003
    00004 // Include files
    00005 #include "GaudiKernel/CnvFactory.h"
    00006 #include "GaudiKernel/IOpaqueAddress.h"
    00007 #include "GaudiKernel/IRegistry.h"
    00008 #include "GaudiKernel/MsgStream.h"
    00009
    00010 #include "TROOT.h"
    00011 #include "RFileCnv.h"
    00012 #include "TFile.h"
    00013
    00014 // Instantiation of a static factory class used by clients to create
    00015 // instances of this service
    00016 DECLARE_NAMESPACE_CONVERTER_FACTORY(RootHistCnv,RFileCnv)
    00017
    00018 //------------------------------------------------------------------------------
    00019 StatusCode RootHistCnv::RFileCnv::createObj(IOpaqueAddress* pAddress,
    00020                                             DataObject*& refpObject)
    00021 //------------------------------------------------------------------------------
    00022 {
    00023   MsgStream log(msgSvc(), "RFileCnv");
    00024   unsigned long*     ipar = (unsigned long*)pAddress->ipar();
    00025   char mode[2] = { char(ipar[1]), 0 };
    00026
    00027   std::string fname  = pAddress->par()[0]; // Container name
    00028   std::string ooname = pAddress->par()[1]; // Object name
    00029
    00030   const std::string* spar = pAddress->par();
    00031   // Strip of store name to get the top level RZ directory
    00032   std::string oname = spar[1].substr(spar[1].find("/",1)+1, spar[1].length());
    00033
    00034   // Protect against multiple instances of TROOT
    00035   if ( 0 == gROOT )   {
    00036     static TROOT root("root","ROOT I/O");
    00037     //    gDebug = 99;
    00038   } else {
    00039     log << MSG::VERBOSE << "ROOT already initialized, debug = "
    00040         << gDebug<< endmsg;
    00041   }
    00042
    00043
    00044   // Determine access mode:
    00045
    00046   if ( mode[0] == 'O' ) {
    00047
    00048     if (findTFile(ooname,rfile).isFailure()) {
    00049
    00050       log << MSG::INFO << "opening Root file \"" << fname << "\" for reading"
    00051           << endmsg;
    00052
    00053       rfile = TFile::Open(fname.c_str(),"READ");
    00054       if ( rfile != 0 && rfile->IsOpen() ) {
    00055         regTFile(ooname,rfile).ignore();
    00056
    00057         ipar[0] = (unsigned long)rfile;
    00058         NTuple::File* pFile = new NTuple::File(objType(), fname, oname);
    00059         pFile->setOpen(true);
    00060         refpObject = pFile;
    00061
    00062         return StatusCode::SUCCESS;
    00063
    00064       } else {
    00065         log << MSG::ERROR << "Couldn't open \"" << fname << "\" for reading"
    00066             << endmsg;
    00067         return StatusCode::FAILURE;
    00068       }
    00069
    00070     } else {
    00071       log << MSG::DEBUG << "Root file \"" << fname << "\" already opened"
    00072           << endmsg;
    00073       return StatusCode::SUCCESS;
    00074     }
    00075
    00076
    00077   } else if ( mode[0] == 'U' ) {
    00078     log << MSG::INFO << "opening Root file \"" << fname << "\" for updating"
    00079         << endmsg;
    00080
    00081     log << MSG::ERROR << "don't know how to do this yet. Aborting." << endmsg;
    00082     exit(1);
    00083
    00084
    00085   } else if ( mode[0] == 'N' ) {
    00086     log << MSG::INFO << "opening Root file \"" << fname << "\" for writing"
    00087         << endmsg;
    00088
    00089
    00090     rfile = TFile::Open(fname.c_str(),"RECREATE","Gaudi Trees");
    00091     if (! rfile->IsOpen() ) {
    00092       log << MSG::ERROR << "Could not open file " << fname << " for writing"
    00093           << endmsg;
    00094       return StatusCode::FAILURE;
    00095     }
    00096
    00097     regTFile(ooname,rfile).ignore();
    00098
    00099     log << MSG::DEBUG << "creating ROOT file " << fname << endmsg;
    00100
    00101     ipar[0] = (unsigned long)rfile;
    00102     NTuple::File* pFile = new NTuple::File(objType(), fname, oname);
    00103     pFile->setOpen(true);
    00104     refpObject = pFile;
    00105     return StatusCode::SUCCESS;
    00106
    00107   } else {
    00108
    00109     log << MSG::ERROR << "Uknown mode to access ROOT file" << endmsg;
    00110     return StatusCode::FAILURE;
    00111
    00112   }
    00113
    00114   return StatusCode::FAILURE;
    00115
    00116
    00117   //    TFile* rfile = TFile::Open(spar[0].c_str(),"RECREATE","Gaudi ROOT Ntuples");
    00118   //    if (! rfile->IsOpen() ) {
    00119   //      log << MSG::ERROR << "Could not open file " << spar[0] << " for writing" << endmsg;
    00120   //      return StatusCode::FAILURE;
    00121   //    }
    00122   //    ipar[0] = (unsigned long)rfile;
    00123   //    NTuple::File* pFile = new NTuple::File(objType(), spar[0], oname);
    00124   //    pFile->setOpen(true);
    00125   //    refpObject = pFile;
    00126   //    return StatusCode::SUCCESS;
    00127
    00128 }
    00129
    00130
    00131 //------------------------------------------------------------------------------
    00132 StatusCode RootHistCnv::RFileCnv::createRep( DataObject* pObject,
    00133                                              IOpaqueAddress*& refpAddress )
    00134 //------------------------------------------------------------------------------
    00135 {
    00136   refpAddress = pObject->registry()->address();
    00137   return RFileCnv::updateRep( refpAddress, pObject );
    00138 }
    00139
    00140 //-----------------------------------------------------------------------------
    00141 StatusCode RootHistCnv::RFileCnv::updateRep( IOpaqueAddress* pAddress,
    00142                                              DataObject* pObject )
    00143 //-----------------------------------------------------------------------------
    00144 {
    00145   MsgStream log(msgSvc(), "RFileCnv");
    00146   std::string ooname = pAddress->par()[1];
    00147
    00148   NTuple::File* pFile = dynamic_cast<NTuple::File*>(pObject);
    00149   if ( pFile != 0 && pFile->isOpen() )    {
    00150
    00151     unsigned long* ipar = (unsigned long*)pAddress->ipar();
    00152     if (findTFile(ooname,rfile).isFailure()) {
    00153       log << MSG::ERROR << "Problems closing TFile " << ooname << endmsg;
    00154       return StatusCode::FAILURE;
    00155     }
    00156
    00157     rfile->Write(0,TObject::kOverwrite);
    00158     if ( log.level() <= MSG::INFO ) {
    00159       log << MSG::INFO << "dumping contents of " << ooname << endmsg;
    00160       rfile->Print();
    00161     }
    00162     rfile->Close();
    00163     delete rfile;
    00164
    00165     ipar[0] = 0;
    00166     pFile->setOpen(false);
    00167     return StatusCode::SUCCESS;
    00168
    00169   } else {
    00170     log << MSG::ERROR << "TFile " << ooname << " is not open" << endmsg;
    00171   }
    00172   return StatusCode::FAILURE;
    00173 }
    00174
    00175 //-----------------------------------------------------------------------------
    00176 RootHistCnv::RFileCnv::~RFileCnv()
    00177 //-----------------------------------------------------------------------------
    00178 {
    00179 }