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