The Gaudi Framework  v36r13 (995e4364)
RFileCnv.cpp
Go to the documentation of this file.
1 /***********************************************************************************\
2 * (c) Copyright 1998-2019 CERN for the benefit of the LHCb and ATLAS collaborations *
3 * *
4 * This software is distributed under the terms of the Apache version 2 licence, *
5 * copied verbatim in the file "LICENSE". *
6 * *
7 * In applying this licence, CERN does not waive the privileges and immunities *
8 * granted to it by virtue of its status as an Intergovernmental Organization *
9 * or submit itself to any jurisdiction. *
10 \***********************************************************************************/
11 // Include files
12 #include "GaudiKernel/Bootstrap.h"
14 #include "GaudiKernel/IRegistry.h"
16 #include "GaudiKernel/MsgStream.h"
17 
19 
20 // ROOT
21 #include "RFileCnv.h"
22 #include "TFile.h"
23 #include "TROOT.h"
24 
25 // local
28 #include <map>
29 #include <string>
30 
31 // Instantiation of a static factory class used by clients to create
32 // instances of this service
34 
35 // Standard constructor
36 RootHistCnv::RFileCnv::RFileCnv( ISvcLocator* svc ) : RDirectoryCnv( svc, classID() ) {}
37 
38 //------------------------------------------------------------------------------
40  // Set compression level property ...
41  const auto& optsSvc = serviceLocator()->getOptsSvc();
42  if ( optsSvc.has( "RFileCnv.GlobalCompression" ) ) {
43  auto sc = Gaudi::Parsers::parse( m_compLevel, optsSvc.get( "RFileCnv.GlobalCompression" ) );
44  if ( !sc ) return sc;
45  }
46  // initialise base class
48 }
49 //------------------------------------------------------------------------------
50 
51 //------------------------------------------------------------------------------
53 //------------------------------------------------------------------------------
54 {
55  MsgStream log( msgSvc(), "RFileCnv" );
56  unsigned long* ipar = (unsigned long*)pAddress->ipar();
57  char mode[2] = { char( ipar[1] ), 0 };
58 
59  std::string fname = pAddress->par()[0]; // Container name
60  std::string ooname = pAddress->par()[1]; // Object name
61 
62  const std::string* spar = pAddress->par();
63  // Strip of store name to get the top level RZ directory
64  std::string oname = spar[1].substr( spar[1].find( "/", 1 ) + 1 );
65 
66  // Protect against multiple instances of TROOT
67  if ( !gROOT ) {
68  static TROOT root( "root", "ROOT I/O" );
69  // gDebug = 99;
70  } else {
71  log << MSG::VERBOSE << "ROOT already initialized, debug = " << gDebug << endmsg;
72  }
73 
74  // Determine access mode:
75 
76  if ( mode[0] == 'O' ) {
77 
78  if ( findTFile( ooname, rfile ).isFailure() ) {
79 
80  log << MSG::INFO << "opening Root file \"" << fname << "\" for reading" << endmsg;
81 
82  rfile = TFile::Open( fname.c_str(), "READ" );
83  if ( rfile && rfile->IsOpen() ) {
84  regTFile( ooname, rfile ).ignore();
85 
86  ipar[0] = (unsigned long)rfile;
87  NTuple::File* pFile = new NTuple::File( objType(), fname, oname );
88  pFile->setOpen( true );
89  refpObject = pFile;
90 
91  return StatusCode::SUCCESS;
92 
93  } else {
94  log << MSG::ERROR << "Couldn't open \"" << fname << "\" for reading" << endmsg;
95  return StatusCode::FAILURE;
96  }
97 
98  } else {
99  log << MSG::DEBUG << "Root file \"" << fname << "\" already opened" << endmsg;
100  return StatusCode::SUCCESS;
101  }
102 
103  } else if ( mode[0] == 'U' ) {
104 
105  log << MSG::INFO << "opening Root file \"" << fname << "\" for updating" << 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() ) { 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" << endmsg;
119  return StatusCode::FAILURE;
120  }
121  if ( !m_compLevel.empty() ) {
122  const RootCompressionSettings settings( m_compLevel );
123  rfile->SetCompressionSettings( settings.level() );
124  }
125 
126  regTFile( ooname, rfile ).ignore();
127 
128  log << MSG::DEBUG << "creating ROOT file " << fname << endmsg;
129 
130  ipar[0] = (unsigned long)rfile;
131  NTuple::File* pFile = new NTuple::File( objType(), fname, oname );
132  pFile->setOpen( true );
133  refpObject = pFile;
134  return StatusCode::SUCCESS;
135 
136  } else {
137 
138  log << MSG::ERROR << "Uknown mode to access ROOT file" << endmsg;
139  return StatusCode::FAILURE;
140  }
141 
142  return StatusCode::FAILURE;
143 }
144 
145 //------------------------------------------------------------------------------
147 //------------------------------------------------------------------------------
148 {
149  refpAddress = pObject->registry()->address();
150  return RFileCnv::updateRep( refpAddress, pObject );
151 }
152 
153 //-----------------------------------------------------------------------------
155 //-----------------------------------------------------------------------------
156 {
157  MsgStream log( msgSvc(), "RFileCnv" );
158  std::string ooname = pAddress->par()[1];
159 
160  NTuple::File* pFile = dynamic_cast<NTuple::File*>( pObject );
161  if ( pFile && pFile->isOpen() ) {
162 
163  unsigned long* ipar = (unsigned long*)pAddress->ipar();
164  if ( findTFile( ooname, rfile ).isFailure() ) {
165  log << MSG::ERROR << "Problems closing TFile " << ooname << endmsg;
166  return StatusCode::FAILURE;
167  }
168 
169  rfile->Write( nullptr, TObject::kOverwrite );
170  if ( log.level() <= MSG::INFO ) {
171  log << MSG::INFO << "dumping contents of " << ooname << endmsg;
172  rfile->Print();
173  }
174 
175  /*
176  * MetaData
177  * Ana Trisovic
178  * March 2015
179  * */
181  mds = serviceLocator()->service( "Gaudi::MetaDataSvc", false );
182  // auto mds = service<IMetaDataSvc>("MetaDataSvc", false);
183  if ( mds ) {
184  std::map<std::string, std::string> m_metadata = mds->getMetaDataMap();
185  if ( !rfile->WriteObject( &m_metadata, "info" ) ) { return StatusCode::FAILURE; }
186  }
187  /* */
188 
189  rfile->Close();
190  delete rfile;
191 
192  ipar[0] = 0;
193  pFile->setOpen( false );
194  return StatusCode::SUCCESS;
195 
196  } else {
197  log << MSG::ERROR << "TFile " << ooname << " is not open" << endmsg;
198  }
199  return StatusCode::FAILURE;
200 }
MSG::DEBUG
@ DEBUG
Definition: IMessageSvc.h:25
RootHistCnv::RFileCnv::updateRep
StatusCode updateRep(IOpaqueAddress *pAddress, DataObject *pObject) override
Convert the transient object to the requested representation.
Definition: RFileCnv.cpp:154
NTuple::File
Small class representing an N tuple file in the transient store.
Definition: NTuple.h:922
std::string
STL class.
IOpaqueAddress::par
virtual const std::string * par() const =0
Retrieve String parameters.
Gaudi.Configuration.log
log
Definition: Configuration.py:30
MSG::INFO
@ INFO
Definition: IMessageSvc.h:25
RootHistCnv::RFileCnv::initialize
StatusCode initialize() override
Initialise.
Definition: RFileCnv.cpp:39
Properties.long
long
(c) Copyright 1998-2020 CERN for the benefit of the LHCb and ATLAS collaborations # # This software i...
Definition: Properties.py:15
IOpaqueAddress
Definition: IOpaqueAddress.h:33
DECLARE_CONVERTER
#define DECLARE_CONVERTER(x)
Definition: Converter.h:160
ISvcLocator
Definition: ISvcLocator.h:46
gaudiComponentHelp.root
root
Definition: gaudiComponentHelp.py:43
RootHistCnv::RDirectoryCnv
Definition: RDirectoryCnv.h:26
RootHistCnv::RFileCnv::createRep
StatusCode createRep(DataObject *pObject, IOpaqueAddress *&refpAddress) override
Convert the transient object to the requested representation.
Definition: RFileCnv.cpp:146
RootHistCnv
Definition: DirectoryCnv.h:27
RootHistCnv::RFileCnv
Definition: RFileCnv.h:32
StatusCode
Definition: StatusCode.h:65
RootCompressionSettings.h
RootHistCnv::RFileCnv::createObj
StatusCode createObj(IOpaqueAddress *pAddress, DataObject *&refpObject) override
Create the transient representation of an object.
Definition: RFileCnv.cpp:52
IOpaqueAddress.h
std::string::c_str
T c_str(T... args)
RFileCnv.h
Converter::serviceLocator
SmartIF< ISvcLocator > & serviceLocator() const
Retrieve pointer to service locator.
Definition: Converter.cpp:102
SmartIF
Definition: IConverter.h:25
endmsg
MsgStream & endmsg(MsgStream &s)
MsgStream Modifier: endmsg. Calls the output method of the MsgStream.
Definition: MsgStream.h:203
std::map< std::string, std::string >
IRegistry.h
MsgStream
Definition: MsgStream.h:34
RootHistCnv::RFileCnv::m_compLevel
std::string m_compLevel
Compression setting, property RFileCnv.GlobalCompression.
Definition: RFileCnv.h:55
std::string::substr
T substr(T... args)
IRegistry::address
virtual IOpaqueAddress * address() const =0
Retrieve opaque storage address.
MSG::VERBOSE
@ VERBOSE
Definition: IMessageSvc.h:25
StatusCode::SUCCESS
constexpr static const auto SUCCESS
Definition: StatusCode.h:100
MSG::ERROR
@ ERROR
Definition: IMessageSvc.h:25
DataObject
Definition: DataObject.h:40
Bootstrap.h
Gaudi::Parsers::parse
StatusCode parse(GaudiUtils::HashMap< K, V > &result, const std::string &input)
Basic parser for the types of HashMap used in DODBasicMapper.
Definition: DODBasicMapper.cpp:21
NTuple::File::setOpen
void setOpen(bool flag)
Set "open" flag.
Definition: NTuple.h:956
NTuple::File::isOpen
bool isOpen() const
Access "open" flag.
Definition: NTuple.h:958
RootHistCnv::RootCompressionSettings::level
int level() const
Get the level.
Definition: RootCompressionSettings.h:38
AsyncIncidents.msgSvc
msgSvc
Definition: AsyncIncidents.py:34
Converter::initialize
StatusCode initialize() override
Initialize the converter.
Definition: Converter.cpp:53
RootHistCnv::RootCompressionSettings
Definition: RootCompressionSettings.h:29
StatusCode::FAILURE
constexpr static const auto FAILURE
Definition: StatusCode.h:101
ISvcLocator.h
IMetaDataSvc.h
IOptionsSvc.h
DataObject::registry
IRegistry * registry() const
Get pointer to Registry.
Definition: DataObject.h:82
MsgStream.h
IOpaqueAddress::ipar
virtual const unsigned long * ipar() const =0
Access to generic link parameters.