00001 #define ROOTHISTCNV_RFILECNV_CPP
00002
00003
00004
00005 #include "GaudiKernel/CnvFactory.h"
00006 #include "GaudiKernel/GenericAddress.h"
00007 #include "GaudiKernel/MsgStream.h"
00008 #include "GaudiKernel/NTuple.h"
00009
00010 #include "RFileCnv.h"
00011
00012 #include "TROOT.h"
00013 #include "TFile.h"
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026 static CnvFactory<RootHistCnv::RFileCnv> s_factory;
00027 const ICnvFactory& RootHistRFileCnvFactory = s_factory;
00028
00029
00030 StatusCode RootHistCnv::RFileCnv::createObj(IOpaqueAddress* pAddress, DataObject*& refpObject) {
00031 MsgStream log(msgSvc(), "RFileCnv");
00032
00033
00034 std::string fname = pAddress->dbName();
00035 std::string ooname = pAddress->objectName();
00036
00037
00038 std::string oname = ooname.substr(ooname.find("/",1)+1, ooname.length());
00039
00040
00041 if ( 0 == gROOT ) {
00042 static TROOT root("root","ROOT I/O");
00043
00044 } else {
00045 log << MSG::INFO << "ROOT already initialized, debug = " << gDebug
00046 << endreq;
00047 }
00048 rfile = new TFile(fname.c_str(),"RECREATE","Athena Trees");
00049 m_rootfile = rfile;
00050 if (! rfile->IsOpen() ) {
00051 log << MSG::ERROR << "Could not open file " << fname << " for writing"
00052 << endreq;
00053 return StatusCode::FAILURE;
00054 }
00055
00056
00057
00058
00059 long lun = 1;
00060
00061 NTuple::File* pFile = new NTuple::File(objType(), fname, oname);
00062 pAddress->genericLink()->genericInfo()->m_info[0] = lun;
00063 pFile->setOpen(true);
00064 refpObject = pFile;
00065 return StatusCode::SUCCESS;
00066 }
00067
00068
00070 StatusCode RootHistCnv::RFileCnv::createRep( DataObject* pObject, IOpaqueAddress*& refpAddress ) {
00071 MsgStream log(msgSvc(), "RFileCnv");
00072
00073 refpAddress = pObject->address();
00074 return RFileCnv::updateRep( refpAddress, pObject );
00075 }
00076
00077
00079 StatusCode RootHistCnv::RFileCnv::updateRep( IOpaqueAddress* , DataObject* pObject ) {
00080 MsgStream log(msgSvc(), "RFileCnv");
00081
00082
00083 NTuple::File* pFile = dynamic_cast<NTuple::File*>(pObject);
00084 if ( pFile->isOpen() ) {
00085 std::string oname = pFile->logicalName();
00086
00087 log << MSG::INFO << "Closing ROOT file \"" << pFile->name() << "\""
00088 << endreq;
00089
00090 rfile->Write();
00091
00092 if ( log.isActive() ) {
00093 log << MSG::INFO;
00094 rfile->Print();
00095 log << endreq;
00096 }
00097
00098 rfile->Close();
00099
00100 pFile->setOpen(false);
00101 return StatusCode::SUCCESS;
00102 }
00103 return StatusCode::FAILURE;
00104 }
00105
00106 RootHistCnv::RFileCnv::~RFileCnv() {
00107
00108 if (rfile != 0) {
00109 delete rfile;
00110 rfile = 0;
00111 }
00112
00113 }