RFileCnv.cpp
Go to the documentation of this file.
1 // Include files
8 
9 // ROOT
10 #include "TROOT.h"
11 #include "RFileCnv.h"
12 #include "TFile.h"
13 
14 // local
16 #include <map>
17 #include <string>
19 
20 // Instantiation of a static factory class used by clients to create
21 // instances of this service
23 
24 // Standard constructor
26 : RDirectoryCnv ( svc, classID() )
27 { }
28 
29 //------------------------------------------------------------------------------
31 {
32  // Set compression level property ...
33  ISvcLocator * svcLoc = Gaudi::svcLocator();
34  auto jobSvc = svcLoc->service<IJobOptionsSvc>("JobOptionsSvc");
35  auto prop = jobSvc->getClientProperty("RFileCnv", "GlobalCompression");
36  if (prop) m_compLevel = prop->toString();
37 
38  // initialise base class
40 }
41 //------------------------------------------------------------------------------
42 
43 //------------------------------------------------------------------------------
45  DataObject*& refpObject )
46 //------------------------------------------------------------------------------
47 {
48  MsgStream log(msgSvc(), "RFileCnv");
49  unsigned long* ipar = (unsigned long*)pAddress->ipar();
50  char mode[2] = { char(ipar[1]), 0 };
51 
52  std::string fname = pAddress->par()[0]; // Container name
53  std::string ooname = pAddress->par()[1]; // Object name
54 
55  const std::string* spar = pAddress->par();
56  // Strip of store name to get the top level RZ directory
57  std::string oname = spar[1].substr(spar[1].find("/",1)+1);
58 
59  // Protect against multiple instances of TROOT
60  if ( !gROOT ) {
61  static TROOT root("root","ROOT I/O");
62  // gDebug = 99;
63  } else {
64  log << MSG::VERBOSE << "ROOT already initialized, debug = "
65  << gDebug<< endmsg;
66  }
67 
68  // Determine access mode:
69 
70  if ( mode[0] == 'O' ) {
71 
72  if (findTFile(ooname,rfile).isFailure()) {
73 
74  log << MSG::INFO << "opening Root file \"" << fname << "\" for reading"
75  << endmsg;
76 
77  rfile = TFile::Open(fname.c_str(),"READ");
78  if ( rfile && rfile->IsOpen() ) {
79  regTFile(ooname,rfile).ignore();
80 
81  ipar[0] = (unsigned long)rfile;
82  NTuple::File* pFile = new NTuple::File(objType(), fname, oname);
83  pFile->setOpen(true);
84  refpObject = pFile;
85 
86  return StatusCode::SUCCESS;
87 
88  } else {
89  log << MSG::ERROR << "Couldn't open \"" << fname << "\" for reading"
90  << endmsg;
91  return StatusCode::FAILURE;
92  }
93 
94  } else {
95  log << MSG::DEBUG << "Root file \"" << fname << "\" already opened"
96  << endmsg;
97  return StatusCode::SUCCESS;
98  }
99 
100 
101  } else if ( mode[0] == 'U' ) {
102 
103  log << MSG::INFO << "opening Root file \"" << fname << "\" for updating"
104  << endmsg;
105 
106  log << MSG::ERROR << "don't know how to do this yet. Aborting." << endmsg;
107  exit(1);
108 
109  } else if ( mode[0] == 'N' ) {
110 
111  log << MSG::INFO << "opening Root file \"" << fname << "\" for writing";
112  if ( !m_compLevel.empty() )
113  { log << ", CompressionLevel='" << m_compLevel << "'"; }
114  log << endmsg;
115 
116  rfile = TFile::Open( fname.c_str(), "RECREATE", "Gaudi Trees" );
117  if ( ! ( rfile && rfile->IsOpen() ) ) {
118  log << MSG::ERROR << "Could not open file " << fname << " for writing"
119  << endmsg;
120  return StatusCode::FAILURE;
121  }
122  if ( !m_compLevel.empty() )
123  {
124  const RootCompressionSettings settings(m_compLevel);
125  rfile->SetCompressionSettings(settings.level());
126  }
127 
128  regTFile(ooname,rfile).ignore();
129 
130  log << MSG::DEBUG << "creating ROOT file " << fname << endmsg;
131 
132  ipar[0] = (unsigned long)rfile;
133  NTuple::File* pFile = new NTuple::File(objType(), fname, oname);
134  pFile->setOpen(true);
135  refpObject = pFile;
136  return StatusCode::SUCCESS;
137 
138  } else {
139 
140  log << MSG::ERROR << "Uknown mode to access ROOT file" << endmsg;
141  return StatusCode::FAILURE;
142 
143  }
144 
145  return StatusCode::FAILURE;
146 }
147 
148 //------------------------------------------------------------------------------
150  IOpaqueAddress*& refpAddress )
151 //------------------------------------------------------------------------------
152 {
153  refpAddress = pObject->registry()->address();
154  return RFileCnv::updateRep( refpAddress, pObject );
155 }
156 
157 //-----------------------------------------------------------------------------
159  DataObject* pObject )
160 //-----------------------------------------------------------------------------
161 {
162  MsgStream log(msgSvc(), "RFileCnv");
163  std::string ooname = pAddress->par()[1];
164 
165  NTuple::File* pFile = dynamic_cast<NTuple::File*>(pObject);
166  if ( pFile && pFile->isOpen() ) {
167 
168  unsigned long* ipar = (unsigned long*)pAddress->ipar();
169  if (findTFile(ooname,rfile).isFailure()) {
170  log << MSG::ERROR << "Problems closing TFile " << ooname << endmsg;
171  return StatusCode::FAILURE;
172  }
173 
174  rfile->Write(nullptr,TObject::kOverwrite);
175  if ( log.level() <= MSG::INFO ) {
176  log << MSG::INFO << "dumping contents of " << ooname << endmsg;
177  rfile->Print();
178  }
179 
180  /*
181  * MetaData
182  * Ana Trisovic
183  * March 2015
184  * */
186  mds = serviceLocator()->service("Gaudi::MetaDataSvc", false);
187  //auto mds = service<IMetaDataSvc>("MetaDataSvc", false);
188  if (mds) {
190  if(!rfile->WriteObject(&m_metadata, "info")){
191  return StatusCode::FAILURE;
192  }
193  }
194  /* */
195 
196  rfile->Close();
197  delete rfile;
198 
199  ipar[0] = 0;
200  pFile->setOpen(false);
201  return StatusCode::SUCCESS;
202 
203  } else {
204  log << MSG::ERROR << "TFile " << ooname << " is not open" << endmsg;
205  }
206  return StatusCode::FAILURE;
207 }
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:14
T empty(T...args)
void setOpen(bool flag)
Set "open" flag.
Definition: NTuple.h:1140
TFile * rfile
Pointer to ROOT file.
Definition: RFileCnv.h:54
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:30
MSG::Level level()
Retrieve output level.
Definition: MsgStream.h:112
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:65
StatusCode createRep(DataObject *pObject, IOpaqueAddress *&refpAddress) override
Convert the transient object to the requested representation.
Definition: RFileCnv.cpp:149
StatusCode updateRep(IOpaqueAddress *pAddress, DataObject *pObject) override
Convert the transient object to the requested representation.
Definition: RFileCnv.cpp:158
SmartIF< IMessageSvc > & msgSvc() const
Retrieve pointer to message service.
Definition: Converter.cpp:129
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:74
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:44
StatusCode service(const Gaudi::Utils::TypeNameString &name, T *&svc, bool createIf=true)
Templated method to access a service by name.
Definition: ISvcLocator.h:78
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:26
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:1144
std::string m_compLevel
Compression setting, property RFileCnv.GlobalCompression.
Definition: RFileCnv.h:55
SmartIF< ISvcLocator > & serviceLocator() const
Retrieve pointer to service locator.
Definition: Converter.cpp:124
#define DECLARE_NAMESPACE_CONVERTER_FACTORY(n, x)
Definition: Converter.h:184
T c_str(T...args)
T substr(T...args)
Opaque address interface definition.
void ignore() const
Definition: StatusCode.h:106
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:244
Small class representing an N tuple file in the transient store.
Definition: NTuple.h:1088