All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
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 
17 // Instantiation of a static factory class used by clients to create
18 // instances of this service
19 DECLARE_NAMESPACE_CONVERTER_FACTORY(RootHistCnv,RFileCnv)
20 
21 // Standard constructor
22 RootHistCnv::RFileCnv::RFileCnv( ISvcLocator* svc )
23 : RDirectoryCnv ( svc, classID() ),
24  m_compLevel ( "" )
25 { }
26 
27 //------------------------------------------------------------------------------
29 {
30  // Set compression level property ...
31  std::auto_ptr<PropertyMgr> pmgr ( new PropertyMgr() );
32  pmgr->declareProperty( "GlobalCompression", m_compLevel );
33  ISvcLocator * svcLoc = Gaudi::svcLocator();
34  SmartIF<IJobOptionsSvc> jobSvc =
35  svcLoc->service<IJobOptionsSvc>("JobOptionsSvc");
36  const StatusCode sc = ( jobSvc.isValid() &&
37  jobSvc->setMyProperties("RFileCnv",&*pmgr) );
38 
39  // initialise base class
40  return ( sc && RDirectoryCnv::initialize() );
41 }
42 //------------------------------------------------------------------------------
43 
44 //------------------------------------------------------------------------------
46  DataObject*& refpObject )
47 //------------------------------------------------------------------------------
48 {
49  MsgStream log(msgSvc(), "RFileCnv");
50  unsigned long* ipar = (unsigned long*)pAddress->ipar();
51  char mode[2] = { char(ipar[1]), 0 };
52 
53  std::string fname = pAddress->par()[0]; // Container name
54  std::string ooname = pAddress->par()[1]; // Object name
55 
56  const std::string* spar = pAddress->par();
57  // Strip of store name to get the top level RZ directory
58  std::string oname = spar[1].substr(spar[1].find("/",1)+1, spar[1].length());
59 
60  // Protect against multiple instances of TROOT
61  if ( 0 == gROOT ) {
62  static TROOT root("root","ROOT I/O");
63  // gDebug = 99;
64  } else {
65  log << MSG::VERBOSE << "ROOT already initialized, debug = "
66  << gDebug<< endmsg;
67  }
68 
69  // Determine access mode:
70 
71  if ( mode[0] == 'O' ) {
72 
73  if (findTFile(ooname,rfile).isFailure()) {
74 
75  log << MSG::INFO << "opening Root file \"" << fname << "\" for reading"
76  << endmsg;
77 
78  rfile = TFile::Open(fname.c_str(),"READ");
79  if ( rfile != 0 && rfile->IsOpen() ) {
80  regTFile(ooname,rfile).ignore();
81 
82  ipar[0] = (unsigned long)rfile;
83  NTuple::File* pFile = new NTuple::File(objType(), fname, oname);
84  pFile->setOpen(true);
85  refpObject = pFile;
86 
87  return StatusCode::SUCCESS;
88 
89  } else {
90  log << MSG::ERROR << "Couldn't open \"" << fname << "\" for reading"
91  << endmsg;
92  return StatusCode::FAILURE;
93  }
94 
95  } else {
96  log << MSG::DEBUG << "Root file \"" << fname << "\" already opened"
97  << endmsg;
98  return StatusCode::SUCCESS;
99  }
100 
101 
102  } else if ( mode[0] == 'U' ) {
103 
104  log << MSG::INFO << "opening Root file \"" << fname << "\" for updating"
105  << endmsg;
106 
107  log << MSG::ERROR << "don't know how to do this yet. Aborting." << endmsg;
108  exit(1);
109 
110  } else if ( mode[0] == 'N' ) {
111 
112  log << MSG::INFO << "opening Root file \"" << fname << "\" for writing";
113  if ( !m_compLevel.empty() )
114  { log << ", CompressionLevel='" << m_compLevel << "'"; }
115  log << endmsg;
116 
117  rfile = TFile::Open( fname.c_str(), "RECREATE", "Gaudi Trees" );
118  if ( ! ( rfile && rfile->IsOpen() ) ) {
119  log << MSG::ERROR << "Could not open file " << fname << " for writing"
120  << endmsg;
121  return StatusCode::FAILURE;
122  }
123  if ( !m_compLevel.empty() )
124  {
125  const RootCompressionSettings settings(m_compLevel);
126  rfile->SetCompressionSettings(settings.level());
127  }
128 
129  regTFile(ooname,rfile).ignore();
130 
131  log << MSG::DEBUG << "creating ROOT file " << fname << endmsg;
132 
133  ipar[0] = (unsigned long)rfile;
134  NTuple::File* pFile = new NTuple::File(objType(), fname, oname);
135  pFile->setOpen(true);
136  refpObject = pFile;
137  return StatusCode::SUCCESS;
138 
139  } else {
140 
141  log << MSG::ERROR << "Uknown mode to access ROOT file" << endmsg;
142  return StatusCode::FAILURE;
143 
144  }
145 
146  return StatusCode::FAILURE;
147 }
148 
149 //------------------------------------------------------------------------------
151  IOpaqueAddress*& refpAddress )
152 //------------------------------------------------------------------------------
153 {
154  refpAddress = pObject->registry()->address();
155  return RFileCnv::updateRep( refpAddress, pObject );
156 }
157 
158 //-----------------------------------------------------------------------------
160  DataObject* pObject )
161 //-----------------------------------------------------------------------------
162 {
163  MsgStream log(msgSvc(), "RFileCnv");
164  std::string ooname = pAddress->par()[1];
165 
166  NTuple::File* pFile = dynamic_cast<NTuple::File*>(pObject);
167  if ( pFile != 0 && pFile->isOpen() ) {
168 
169  unsigned long* ipar = (unsigned long*)pAddress->ipar();
170  if (findTFile(ooname,rfile).isFailure()) {
171  log << MSG::ERROR << "Problems closing TFile " << ooname << endmsg;
172  return StatusCode::FAILURE;
173  }
174 
175  rfile->Write(0,TObject::kOverwrite);
176  if ( log.level() <= MSG::INFO ) {
177  log << MSG::INFO << "dumping contents of " << ooname << endmsg;
178  rfile->Print();
179  }
180  rfile->Close();
181  delete rfile;
182 
183  ipar[0] = 0;
184  pFile->setOpen(false);
185  return StatusCode::SUCCESS;
186 
187  } else {
188  log << MSG::ERROR << "TFile " << ooname << " is not open" << endmsg;
189  }
190  return StatusCode::FAILURE;
191 }
192 
193 //-----------------------------------------------------------------------------
195 //-----------------------------------------------------------------------------
196 {
197 }
virtual const std::string * par() const =0
Retrieve String parameters.
Definition of the MsgStream class used to transmit messages.
Definition: MsgStream.h:24
void setOpen(bool flag)
Set "open" flag.
Definition: NTuple.h:1138
The ISvcLocator is the interface implemented by the Service Factory in the Application Manager to loc...
Definition: ISvcLocator.h:26
virtual ~RFileCnv()
Standard destructor.
Definition: RFileCnv.cpp:194
MSG::Level level()
Retrieve output level.
Definition: MsgStream.h:112
virtual StatusCode initialize()
Initialize the converter.
Definition: Converter.cpp:67
Property manager helper class.
Definition: PropertyMgr.h:38
IRegistry * registry() const
Get pointer to Registry.
Definition: DataObject.h:69
virtual StatusCode updateRep(IOpaqueAddress *pAddress, DataObject *pObject)
Convert the transient object to the requested representation.
Definition: RFileCnv.cpp:159
NTuple converter class definition.
Definition: RFileCnv.h:23
StatusCode service(const Gaudi::Utils::TypeNameString &name, T *&svc, bool createIf=true)
Templated method to access a service by name.
Definition: ISvcLocator.h:82
Main interface for the JobOptions service.
GAUDI_API ISvcLocator * svcLocator()
bool isValid() const
Allow for check if smart pointer is valid.
Definition: SmartIF.h:51
Property * declareProperty(const std::string &name, TYPE &value, const std::string &doc="none")
Declare a property (templated)
Definition: PropertyMgr.h:177
This class is used for returning status codes from appropriate routines.
Definition: StatusCode.h:30
Create persistent and transient representations of data store directories.
Definition: RDirectoryCnv.h:17
virtual StatusCode createObj(IOpaqueAddress *pAddress, DataObject *&refpObject)
Create the transient representation of an object.
Definition: RFileCnv.cpp:45
Simple class to decode a ROOT compression settings string, of the form ':' into the integ...
bool isOpen() const
Access "open" flag.
Definition: NTuple.h:1142
std::string m_compLevel
Compression setting.
Definition: RFileCnv.h:56
virtual IOpaqueAddress * address() const =0
Retrieve opaque storage address.
virtual StatusCode initialize()
Initialise.
Definition: RFileCnv.cpp:28
#define DECLARE_NAMESPACE_CONVERTER_FACTORY(n, x)
Definition: Converter.h:184
tuple root
Definition: IOTest.py:42
virtual StatusCode createRep(DataObject *pObject, IOpaqueAddress *&refpAddress)
Convert the transient object to the requested representation.
Definition: RFileCnv.cpp:150
Opaque address interface definition.
A DataObject is the base class of any identifiable object on any data store.
Definition: DataObject.h:31
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:1085