| The Gaudi Framework v32r1 (f65d50dc)

RFileCnv.cpp
Go to the documentation of this file.
1 // Include files
2 #include "GaudiKernel/Bootstrap.h"
3 #include "GaudiKernel/IJobOptionsSvc.h"
4 #include "GaudiKernel/IOpaqueAddress.h"
5 #include "GaudiKernel/IRegistry.h"
6 #include "GaudiKernel/ISvcLocator.h"
7 #include "GaudiKernel/MsgStream.h"
8
9 // ROOT
10 #include "RFileCnv.h"
11 #include "TFile.h"
12 #include "TROOT.h"
13
14 // local
15 #include "RootCompressionSettings.h"
16 #include <GaudiKernel/IMetaDataSvc.h>
17 #include <map>
18 #include <string>
19
20 // Instantiation of a static factory class used by clients to create
21 // instances of this service
22 DECLARE_CONVERTER( RootHistCnv::RFileCnv )
23
24 // Standard constructor
25 RootHistCnv::RFileCnv::RFileCnv( ISvcLocator* svc ) : RDirectoryCnv( svc, classID() ) {}
26
27 //------------------------------------------------------------------------------
28 StatusCode RootHistCnv::RFileCnv::initialize() {
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
36 return RDirectoryCnv::initialize();
37 }
38 //------------------------------------------------------------------------------
39
40 //------------------------------------------------------------------------------
41 StatusCode RootHistCnv::RFileCnv::createObj( IOpaqueAddress* pAddress, DataObject*& refpObject )
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 //------------------------------------------------------------------------------
135 StatusCode RootHistCnv::RFileCnv::createRep( DataObject* pObject, IOpaqueAddress*& refpAddress )
136 //------------------------------------------------------------------------------
137 {
138 refpAddress = pObject->registry()->address();
139 return RFileCnv::updateRep( refpAddress, pObject );
140 }
141
142 //-----------------------------------------------------------------------------
143 StatusCode RootHistCnv::RFileCnv::updateRep( IOpaqueAddress* pAddress, DataObject* pObject )
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 * */
169 SmartIF<IMetaDataSvc> mds;
170 mds = serviceLocator()->service( "Gaudi::MetaDataSvc", false );
171 // auto mds = service<IMetaDataSvc>("MetaDataSvc", false);
172 if ( mds ) {
173 std::map<std::string, std::string> m_metadata = mds->getMetaDataMap();
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 }
IOpaqueAddress::par
virtual const std::string * par() const =0
Retrieve String parameters.
MsgStream
Definition of the MsgStream class used to transmit messages.
Definition: MsgStream.h:24
SmartIF
Small smart pointer class with automatic reference counting for IInterface.
Definition: IConverter.h:15
RootCompressionSettings.h
NTuple::File::setOpen
void setOpen(bool flag)
Set "open" flag.
Definition: NTuple.h:944
ISvcLocator
The ISvcLocator is the interface implemented by the Service Factory in the Application Manager to loc...
Definition: ISvcLocator.h:25
DataObject::registry
IRegistry * registry() const
Get pointer to Registry.
Definition: DataObject.h:72
RootHistCnv::RootCompressionSettings::level
int level() const
Get the level.
Definition: RootCompressionSettings.h:28
RootHistCnv::RFileCnv::initialize
StatusCode initialize() override
Initialise.
Definition: RFileCnv.cpp:28
MsgStream.h
Converter::initialize
StatusCode initialize() override
Initialize the converter.
Definition: Converter.cpp:43
RootHistCnv::RFileCnv::createRep
StatusCode createRep(DataObject *pObject, IOpaqueAddress *&refpAddress) override
Convert the transient object to the requested representation.
Definition: RFileCnv.cpp:135
MSG::DEBUG
Definition: IMessageSvc.h:15
RootHistCnv::RFileCnv::updateRep
StatusCode updateRep(IOpaqueAddress *pAddress, DataObject *pObject) override
Convert the transient object to the requested representation.
Definition: RFileCnv.cpp:143
StatusCode::SUCCESS
constexpr static const auto SUCCESS
Definition: StatusCode.h:85
Bootstrap.h
IMetaDataSvc::getMetaDataMap
virtual std::map< std::string, std::string > getMetaDataMap() const =0
DECLARE_CONVERTER
#define DECLARE_CONVERTER(x)
Definition: Converter.h:150
MSG::VERBOSE
Definition: IMessageSvc.h:15
std::map< std::string, std::string >
AvalancheSchedulerErrorTest.msgSvc
msgSvc
Definition: AvalancheSchedulerErrorTest.py:67
std::string
STL class.
RootHistCnv::RFileCnv
NTuple converter class definition.
Definition: RFileCnv.h:22
RootHistCnv::RFileCnv::createObj
StatusCode createObj(IOpaqueAddress *pAddress, DataObject *&refpObject) override
Create the transient representation of an object.
Definition: RFileCnv.cpp:41
ISvcLocator::service
StatusCode service(const Gaudi::Utils::TypeNameString &name, T *&svc, bool createIf=true)
Templated method to access a service by name.
Definition: ISvcLocator.h:76
IJobOptionsSvc
Main interface for the JobOptions service.
Definition: IJobOptionsSvc.h:20
Gaudi::svcLocator
GAUDI_API ISvcLocator * svcLocator()
StatusCode
This class is used for returning status codes from appropriate routines.
Definition: StatusCode.h:50
RootHistCnv::RDirectoryCnv
Create persistent and transient representations of data store directories.
Definition: RDirectoryCnv.h:16
RootHistCnv
Definition: DirectoryCnv.h:17
IRegistry.h
IMetaDataSvc.h
RootHistCnv::RootCompressionSettings
Simple class to decode a ROOT compression settings string, of the form '<Alg>:<level>' into the integ...
Definition: RootCompressionSettings.h:19
RootHistCnv::RFileCnv::m_compLevel
std::string m_compLevel
Compression setting, property RFileCnv.GlobalCompression.
Definition: RFileCnv.h:45
IRegistry::address
virtual IOpaqueAddress * address() const =0
Retrieve opaque storage address.
IOpaqueAddress.h
IJobOptionsSvc::getClientProperty
virtual const Gaudi::Details::PropertyBase * getClientProperty(const std::string &client, const std::string &name) const =0
Get a property for a client.
std::string::c_str
T c_str(T... args)
StatusCode::FAILURE
constexpr static const auto FAILURE
Definition: StatusCode.h:86
MSG::ERROR
Definition: IMessageSvc.h:15
std::string::substr
T substr(T... args)
gaudiComponentHelp.root
root
Definition: gaudiComponentHelp.py:27
NTuple::File::isOpen
bool isOpen() const
Access "open" flag.
Definition: NTuple.h:946
IOpaqueAddress
Opaque address interface definition.
Definition: IOpaqueAddress.h:23
RFileCnv.h
MSG::INFO
Definition: IMessageSvc.h:15
DataObject
A DataObject is the base class of any identifiable object on any data store.
Definition: DataObject.h:30
ISvcLocator.h
IOpaqueAddress::ipar
virtual const unsigned long * ipar() const =0
Access to generic link parameters.
endmsg
MsgStream & endmsg(MsgStream &s)
MsgStream Modifier: endmsg. Calls the output method of the MsgStream.
Definition: MsgStream.h:192
NTuple::File
Small class representing an N tuple file in the transient store.
Definition: NTuple.h:910
Gaudi.Configuration.log
log
Definition: Configuration.py:13
IJobOptionsSvc.h