The Gaudi Framework  v29r0 (ff2e7097)
RFileCnv.cpp
Go to the documentation of this file.
1 // Include files
8 
9 // ROOT
10 #include "RFileCnv.h"
11 #include "TFile.h"
12 #include "TROOT.h"
13 
14 // local
17 #include <map>
18 #include <string>
19 
20 // Instantiation of a static factory class used by clients to create
21 // instances of this service
23 
24 // Standard constructor
25 RootHistCnv::RFileCnv::RFileCnv( ISvcLocator* svc ) : RDirectoryCnv( svc, classID() ) {}
26 
27 //------------------------------------------------------------------------------
29 {
30  // Set compression level property ...
31  ISvcLocator* svcLoc = Gaudi::svcLocator();
32  auto jobSvc = svcLoc->service<IJobOptionsSvc>( "JobOptionsSvc" );
33  auto prop = jobSvc->getClientProperty( "RFileCnv", "GlobalCompression" );
34  if ( prop ) m_compLevel = prop->toString();
35 
36  // initialise base class
38 }
39 //------------------------------------------------------------------------------
40 
41 //------------------------------------------------------------------------------
43 //------------------------------------------------------------------------------
44 {
45  MsgStream log( msgSvc(), "RFileCnv" );
46  unsigned long* ipar = (unsigned long*)pAddress->ipar();
47  char mode[2] = {char( ipar[1] ), 0};
48 
49  std::string fname = pAddress->par()[0]; // Container name
50  std::string ooname = pAddress->par()[1]; // Object name
51 
52  const std::string* spar = pAddress->par();
53  // Strip of store name to get the top level RZ directory
54  std::string oname = spar[1].substr( spar[1].find( "/", 1 ) + 1 );
55 
56  // Protect against multiple instances of TROOT
57  if ( !gROOT ) {
58  static TROOT root( "root", "ROOT I/O" );
59  // gDebug = 99;
60  } else {
61  log << MSG::VERBOSE << "ROOT already initialized, debug = " << gDebug << endmsg;
62  }
63 
64  // Determine access mode:
65 
66  if ( mode[0] == 'O' ) {
67 
68  if ( findTFile( ooname, rfile ).isFailure() ) {
69 
70  log << MSG::INFO << "opening Root file \"" << fname << "\" for reading" << endmsg;
71 
72  rfile = TFile::Open( fname.c_str(), "READ" );
73  if ( rfile && rfile->IsOpen() ) {
74  regTFile( ooname, rfile ).ignore();
75 
76  ipar[0] = (unsigned long)rfile;
77  NTuple::File* pFile = new NTuple::File( objType(), fname, oname );
78  pFile->setOpen( true );
79  refpObject = pFile;
80 
81  return StatusCode::SUCCESS;
82 
83  } else {
84  log << MSG::ERROR << "Couldn't open \"" << fname << "\" for reading" << endmsg;
85  return StatusCode::FAILURE;
86  }
87 
88  } else {
89  log << MSG::DEBUG << "Root file \"" << fname << "\" already opened" << endmsg;
90  return StatusCode::SUCCESS;
91  }
92 
93  } else if ( mode[0] == 'U' ) {
94 
95  log << MSG::INFO << "opening Root file \"" << fname << "\" for updating" << endmsg;
96 
97  log << MSG::ERROR << "don't know how to do this yet. Aborting." << endmsg;
98  exit( 1 );
99 
100  } else if ( mode[0] == 'N' ) {
101 
102  log << MSG::INFO << "opening Root file \"" << fname << "\" for writing";
103  if ( !m_compLevel.empty() ) {
104  log << ", CompressionLevel='" << m_compLevel << "'";
105  }
106  log << endmsg;
107 
108  rfile = TFile::Open( fname.c_str(), "RECREATE", "Gaudi Trees" );
109  if ( !( rfile && rfile->IsOpen() ) ) {
110  log << MSG::ERROR << "Could not open file " << fname << " for writing" << endmsg;
111  return StatusCode::FAILURE;
112  }
113  if ( !m_compLevel.empty() ) {
114  const RootCompressionSettings settings( m_compLevel );
115  rfile->SetCompressionSettings( settings.level() );
116  }
117 
118  regTFile( ooname, rfile ).ignore();
119 
120  log << MSG::DEBUG << "creating ROOT file " << fname << endmsg;
121 
122  ipar[0] = (unsigned long)rfile;
123  NTuple::File* pFile = new NTuple::File( objType(), fname, oname );
124  pFile->setOpen( true );
125  refpObject = pFile;
126  return StatusCode::SUCCESS;
127 
128  } else {
129 
130  log << MSG::ERROR << "Uknown mode to access ROOT file" << endmsg;
131  return StatusCode::FAILURE;
132  }
133 
134  return StatusCode::FAILURE;
135 }
136 
137 //------------------------------------------------------------------------------
139 //------------------------------------------------------------------------------
140 {
141  refpAddress = pObject->registry()->address();
142  return RFileCnv::updateRep( refpAddress, pObject );
143 }
144 
145 //-----------------------------------------------------------------------------
147 //-----------------------------------------------------------------------------
148 {
149  MsgStream log( msgSvc(), "RFileCnv" );
150  std::string ooname = pAddress->par()[1];
151 
152  NTuple::File* pFile = dynamic_cast<NTuple::File*>( pObject );
153  if ( pFile && pFile->isOpen() ) {
154 
155  unsigned long* ipar = (unsigned long*)pAddress->ipar();
156  if ( findTFile( ooname, rfile ).isFailure() ) {
157  log << MSG::ERROR << "Problems closing TFile " << ooname << endmsg;
158  return StatusCode::FAILURE;
159  }
160 
161  rfile->Write( nullptr, TObject::kOverwrite );
162  if ( log.level() <= MSG::INFO ) {
163  log << MSG::INFO << "dumping contents of " << ooname << endmsg;
164  rfile->Print();
165  }
166 
167  /*
168  * MetaData
169  * Ana Trisovic
170  * March 2015
171  * */
173  mds = serviceLocator()->service( "Gaudi::MetaDataSvc", false );
174  // auto mds = service<IMetaDataSvc>("MetaDataSvc", false);
175  if ( mds ) {
177  if ( !rfile->WriteObject( &m_metadata, "info" ) ) {
178  return StatusCode::FAILURE;
179  }
180  }
181  /* */
182 
183  rfile->Close();
184  delete rfile;
185 
186  ipar[0] = 0;
187  pFile->setOpen( false );
188  return StatusCode::SUCCESS;
189 
190  } else {
191  log << MSG::ERROR << "TFile " << ooname << " is not open" << endmsg;
192  }
193  return StatusCode::FAILURE;
194 }
Definition of the MsgStream class used to transmit messages.
Definition: MsgStream.h:24
Small smart pointer class with automatic reference counting for IInterface.
Definition: IConverter.h:15
T empty(T...args)
void setOpen(bool flag)
Set "open" flag.
Definition: NTuple.h:1041
TFile * rfile
Pointer to ROOT file.
Definition: RFileCnv.h:49
The ISvcLocator is the interface implemented by the Service Factory in the Application Manager to loc...
Definition: ISvcLocator.h:25
StatusCode initialize() override
Initialise.
Definition: RFileCnv.cpp:28
const CLID & objType() const override
Retrieve the class type of objects the converter produces.
Definition: Converter.cpp:14
StatusCode initialize() override
Initialize the converter.
Definition: Converter.cpp:44
StatusCode createRep(DataObject *pObject, IOpaqueAddress *&refpAddress) override
Convert the transient object to the requested representation.
Definition: RFileCnv.cpp:138
StatusCode updateRep(IOpaqueAddress *pAddress, DataObject *pObject) override
Convert the transient object to the requested representation.
Definition: RFileCnv.cpp:146
SmartIF< IMessageSvc > & msgSvc() const
Retrieve pointer to message service.
Definition: Converter.cpp:103
StatusCode regTFile(const std::string, const TFile *)
Definition: RConverter.cpp:271
StatusCode findTFile(const std::string, TFile *&)
Definition: RConverter.cpp:286
virtual const std::string * par() const =0
Retrieve String parameters.
IRegistry * registry() const
Get pointer to Registry.
Definition: DataObject.h:72
STL class.
NTuple converter class definition.
Definition: RFileCnv.h:23
StatusCode createObj(IOpaqueAddress *pAddress, DataObject *&refpObject) override
Create the transient representation of an object.
Definition: RFileCnv.cpp:42
StatusCode service(const Gaudi::Utils::TypeNameString &name, T *&svc, bool createIf=true)
Templated method to access a service by name.
Definition: ISvcLocator.h:79
virtual std::map< std::string, std::string > getMetaDataMap()=0
Main interface for the JobOptions service.
GAUDI_API ISvcLocator * svcLocator()
virtual const unsigned long * ipar() const =0
Access to generic link parameters.
This class is used for returning status codes from appropriate routines.
Definition: StatusCode.h:28
Create persistent and transient representations of data store directories.
Definition: RDirectoryCnv.h:17
Simple class to decode a ROOT compression settings string, of the form &#39;<Alg>:<level>&#39; into the integ...
bool isOpen() const
Access "open" flag.
Definition: NTuple.h:1043
std::string m_compLevel
Compression setting, property RFileCnv.GlobalCompression.
Definition: RFileCnv.h:50
SmartIF< ISvcLocator > & serviceLocator() const
Retrieve pointer to service locator.
Definition: Converter.cpp:100
#define DECLARE_NAMESPACE_CONVERTER_FACTORY(n, x)
Definition: Converter.h:177
T c_str(T...args)
T substr(T...args)
MSG::Level level() const
Retrieve output level.
Definition: MsgStream.h:108
Opaque address interface definition.
void ignore() const
Definition: StatusCode.h:109
virtual const Gaudi::Details::PropertyBase * getClientProperty(const std::string &client, const std::string &name) const =0
Get a property for a client.
virtual IOpaqueAddress * address() const =0
Retrieve opaque storage address.
A DataObject is the base class of any identifiable object on any data store.
Definition: DataObject.h:29
MsgStream & endmsg(MsgStream &s)
MsgStream Modifier: endmsg. Calls the output method of the MsgStream.
Definition: MsgStream.h:209
Small class representing an N tuple file in the transient store.
Definition: NTuple.h:1002