Loading [MathJax]/extensions/tex2jax.js
The Gaudi Framework  v31r0 (aeb156f0)
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Modules Pages
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  // Set compression level property ...
30  ISvcLocator* svcLoc = Gaudi::svcLocator();
31  auto jobSvc = svcLoc->service<IJobOptionsSvc>( "JobOptionsSvc" );
32  auto prop = jobSvc->getClientProperty( "RFileCnv", "GlobalCompression" );
33  if ( prop ) m_compLevel = prop->toString();
34 
35  // initialise base class
37 }
38 //------------------------------------------------------------------------------
39 
40 //------------------------------------------------------------------------------
42 //------------------------------------------------------------------------------
43 {
44  MsgStream log( msgSvc(), "RFileCnv" );
45  unsigned long* ipar = (unsigned long*)pAddress->ipar();
46  char mode[2] = {char( ipar[1] ), 0};
47 
48  std::string fname = pAddress->par()[0]; // Container name
49  std::string ooname = pAddress->par()[1]; // Object name
50 
51  const std::string* spar = pAddress->par();
52  // Strip of store name to get the top level RZ directory
53  std::string oname = spar[1].substr( spar[1].find( "/", 1 ) + 1 );
54 
55  // Protect against multiple instances of TROOT
56  if ( !gROOT ) {
57  static TROOT root( "root", "ROOT I/O" );
58  // gDebug = 99;
59  } else {
60  log << MSG::VERBOSE << "ROOT already initialized, debug = " << gDebug << endmsg;
61  }
62 
63  // Determine access mode:
64 
65  if ( mode[0] == 'O' ) {
66 
67  if ( findTFile( ooname, rfile ).isFailure() ) {
68 
69  log << MSG::INFO << "opening Root file \"" << fname << "\" for reading" << endmsg;
70 
71  rfile = TFile::Open( fname.c_str(), "READ" );
72  if ( rfile && rfile->IsOpen() ) {
73  regTFile( ooname, rfile ).ignore();
74 
75  ipar[0] = (unsigned long)rfile;
76  NTuple::File* pFile = new NTuple::File( objType(), fname, oname );
77  pFile->setOpen( true );
78  refpObject = pFile;
79 
80  return StatusCode::SUCCESS;
81 
82  } else {
83  log << MSG::ERROR << "Couldn't open \"" << fname << "\" for reading" << endmsg;
84  return StatusCode::FAILURE;
85  }
86 
87  } else {
88  log << MSG::DEBUG << "Root file \"" << fname << "\" already opened" << endmsg;
89  return StatusCode::SUCCESS;
90  }
91 
92  } else if ( mode[0] == 'U' ) {
93 
94  log << MSG::INFO << "opening Root file \"" << fname << "\" for updating" << endmsg;
95 
96  log << MSG::ERROR << "don't know how to do this yet. Aborting." << endmsg;
97  exit( 1 );
98 
99  } else if ( mode[0] == 'N' ) {
100 
101  log << MSG::INFO << "opening Root file \"" << fname << "\" for writing";
102  if ( !m_compLevel.empty() ) { log << ", CompressionLevel='" << m_compLevel << "'"; }
103  log << endmsg;
104 
105  rfile = TFile::Open( fname.c_str(), "RECREATE", "Gaudi Trees" );
106  if ( !( rfile && rfile->IsOpen() ) ) {
107  log << MSG::ERROR << "Could not open file " << fname << " for writing" << endmsg;
108  return StatusCode::FAILURE;
109  }
110  if ( !m_compLevel.empty() ) {
111  const RootCompressionSettings settings( m_compLevel );
112  rfile->SetCompressionSettings( settings.level() );
113  }
114 
115  regTFile( ooname, rfile ).ignore();
116 
117  log << MSG::DEBUG << "creating ROOT file " << fname << endmsg;
118 
119  ipar[0] = (unsigned long)rfile;
120  NTuple::File* pFile = new NTuple::File( objType(), fname, oname );
121  pFile->setOpen( true );
122  refpObject = pFile;
123  return StatusCode::SUCCESS;
124 
125  } else {
126 
127  log << MSG::ERROR << "Uknown mode to access ROOT file" << endmsg;
128  return StatusCode::FAILURE;
129  }
130 
131  return StatusCode::FAILURE;
132 }
133 
134 //------------------------------------------------------------------------------
136 //------------------------------------------------------------------------------
137 {
138  refpAddress = pObject->registry()->address();
139  return RFileCnv::updateRep( refpAddress, pObject );
140 }
141 
142 //-----------------------------------------------------------------------------
144 //-----------------------------------------------------------------------------
145 {
146  MsgStream log( msgSvc(), "RFileCnv" );
147  std::string ooname = pAddress->par()[1];
148 
149  NTuple::File* pFile = dynamic_cast<NTuple::File*>( pObject );
150  if ( pFile && pFile->isOpen() ) {
151 
152  unsigned long* ipar = (unsigned long*)pAddress->ipar();
153  if ( findTFile( ooname, rfile ).isFailure() ) {
154  log << MSG::ERROR << "Problems closing TFile " << ooname << endmsg;
155  return StatusCode::FAILURE;
156  }
157 
158  rfile->Write( nullptr, TObject::kOverwrite );
159  if ( log.level() <= MSG::INFO ) {
160  log << MSG::INFO << "dumping contents of " << ooname << endmsg;
161  rfile->Print();
162  }
163 
164  /*
165  * MetaData
166  * Ana Trisovic
167  * March 2015
168  * */
170  mds = serviceLocator()->service( "Gaudi::MetaDataSvc", false );
171  // auto mds = service<IMetaDataSvc>("MetaDataSvc", false);
172  if ( mds ) {
174  if ( !rfile->WriteObject( &m_metadata, "info" ) ) { return StatusCode::FAILURE; }
175  }
176  /* */
177 
178  rfile->Close();
179  delete rfile;
180 
181  ipar[0] = 0;
182  pFile->setOpen( false );
183  return StatusCode::SUCCESS;
184 
185  } else {
186  log << MSG::ERROR << "TFile " << ooname << " is not open" << endmsg;
187  }
188  return StatusCode::FAILURE;
189 }
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:944
TFile * rfile
Pointer to ROOT file.
Definition: RFileCnv.h:44
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:13
StatusCode initialize() override
Initialize the converter.
Definition: Converter.cpp:43
StatusCode createRep(DataObject *pObject, IOpaqueAddress *&refpAddress) override
Convert the transient object to the requested representation.
Definition: RFileCnv.cpp:135
StatusCode updateRep(IOpaqueAddress *pAddress, DataObject *pObject) override
Convert the transient object to the requested representation.
Definition: RFileCnv.cpp:143
constexpr static const auto SUCCESS
Definition: StatusCode.h:85
SmartIF< IMessageSvc > & msgSvc() const
Retrieve pointer to message service.
Definition: Converter.cpp:95
#define DECLARE_CONVERTER(x)
Definition: Converter.h:150
StatusCode regTFile(const std::string, const TFile *)
Definition: RConverter.cpp:264
StatusCode findTFile(const std::string, TFile *&)
Definition: RConverter.cpp:279
virtual const std::string * par() const =0
Retrieve String parameters.
IRegistry * registry() const
Get pointer to Registry.
Definition: DataObject.h:72
virtual std::map< std::string, std::string > getMetaDataMap() const =0
STL class.
NTuple converter class definition.
Definition: RFileCnv.h:22
StatusCode createObj(IOpaqueAddress *pAddress, DataObject *&refpObject) override
Create the transient representation of an object.
Definition: RFileCnv.cpp:41
StatusCode service(const Gaudi::Utils::TypeNameString &name, T *&svc, bool createIf=true)
Templated method to access a service by name.
Definition: ISvcLocator.h:76
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:50
Create persistent and transient representations of data store directories.
Definition: RDirectoryCnv.h:16
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:946
std::string m_compLevel
Compression setting, property RFileCnv.GlobalCompression.
Definition: RFileCnv.h:45
SmartIF< ISvcLocator > & serviceLocator() const
Retrieve pointer to service locator.
Definition: Converter.cpp:92
const StatusCode & ignore() const
Ignore/check StatusCode.
Definition: StatusCode.h:153
T c_str(T...args)
constexpr static const auto FAILURE
Definition: StatusCode.h:86
T substr(T...args)
MSG::Level level() const
Retrieve output level.
Definition: MsgStream.h:103
Opaque address interface definition.
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:30
MsgStream & endmsg(MsgStream &s)
MsgStream Modifier: endmsg. Calls the output method of the MsgStream.
Definition: MsgStream.h:192
Small class representing an N tuple file in the transient store.
Definition: NTuple.h:910