| The Gaudi Framework v29r0 (ff2e7097)

- Main Page
- Related Pages
- Modules
- Namespaces
- Classes
- Files

- File List
- File Members

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_NAMESPACE_CONVERTER_FACTORY( RootHistCnv, RFileCnv )
23
24 // Standard constructor
25 RootHistCnv::RFileCnv::RFileCnv( ISvcLocator* svc ) : RDirectoryCnv( svc, classID() ) {}
26
27 //------------------------------------------------------------------------------
28 StatusCode RootHistCnv::RFileCnv::initialize()
29 {
30 // Set compression level property ...
31 ISvcLocator* svcLoc = Gaudi::svcLocator();
32 auto jobSvc = svcLoc->service<IJobOptionsSvc>( "JobOptionsSvc" );
33 auto prop = jobSvc->getClientProperty( "RFileCnv", "GlobalCompression" );
34 if ( prop ) m_compLevel = prop->toString();
35
36 // initialise base class
37 return RDirectoryCnv::initialize();
38 }
39 //------------------------------------------------------------------------------
40
41 //------------------------------------------------------------------------------
42 StatusCode RootHistCnv::RFileCnv::createObj( IOpaqueAddress* pAddress, DataObject*& refpObject )
43 //------------------------------------------------------------------------------
44 {
45 MsgStream log( msgSvc(), "RFileCnv" );
46 unsigned long* ipar = (unsigned long*)pAddress->ipar();
47 char mode[2] = {char( ipar[1] ), 0};
48
49 std::string fname = pAddress->par()[0]; // Container name
50 std::string ooname = pAddress->par()[1]; // Object name
51
52 const std::string* spar = pAddress->par();
53 // Strip of store name to get the top level RZ directory
54 std::string oname = spar[1].substr( spar[1].find( "/", 1 ) + 1 );
55
56 // Protect against multiple instances of TROOT
57 if ( !gROOT ) {
58 static TROOT root( "root", "ROOT I/O" );
59 // gDebug = 99;
60 } else {
61 log << MSG::VERBOSE << "ROOT already initialized, debug = " << gDebug << endmsg;
62 }
63
64 // Determine access mode:
65
66 if ( mode[0] == 'O' ) {
67
68 if ( findTFile( ooname, rfile ).isFailure() ) {
69
70 log << MSG::INFO << "opening Root file \"" << fname << "\" for reading" << endmsg;
71
72 rfile = TFile::Open( fname.c_str(), "READ" );
73 if ( rfile && rfile->IsOpen() ) {
74 regTFile( ooname, rfile ).ignore();
75
76 ipar[0] = (unsigned long)rfile;
77 NTuple::File* pFile = new NTuple::File( objType(), fname, oname );
78 pFile->setOpen( true );
79 refpObject = pFile;
80
81 return StatusCode::SUCCESS;
82
83 } else {
84 log << MSG::ERROR << "Couldn't open \"" << fname << "\" for reading" << endmsg;
85 return StatusCode::FAILURE;
86 }
87
88 } else {
89 log << MSG::DEBUG << "Root file \"" << fname << "\" already opened" << endmsg;
90 return StatusCode::SUCCESS;
91 }
92
93 } else if ( mode[0] == 'U' ) {
94
95 log << MSG::INFO << "opening Root file \"" << fname << "\" for updating" << endmsg;
96
97 log << MSG::ERROR << "don't know how to do this yet. Aborting." << endmsg;
98 exit( 1 );
99
100 } else if ( mode[0] == 'N' ) {
101
102 log << MSG::INFO << "opening Root file \"" << fname << "\" for writing";
103 if ( !m_compLevel.empty() ) {
104 log << ", CompressionLevel='" << m_compLevel << "'";
105 }
106 log << endmsg;
107
108 rfile = TFile::Open( fname.c_str(), "RECREATE", "Gaudi Trees" );
109 if ( !( rfile && rfile->IsOpen() ) ) {
110 log << MSG::ERROR << "Could not open file " << fname << " for writing" << endmsg;
111 return StatusCode::FAILURE;
112 }
113 if ( !m_compLevel.empty() ) {
114 const RootCompressionSettings settings( m_compLevel );
115 rfile->SetCompressionSettings( settings.level() );
116 }
117
118 regTFile( ooname, rfile ).ignore();
119
120 log << MSG::DEBUG << "creating ROOT file " << fname << endmsg;
121
122 ipar[0] = (unsigned long)rfile;
123 NTuple::File* pFile = new NTuple::File( objType(), fname, oname );
124 pFile->setOpen( true );
125 refpObject = pFile;
126 return StatusCode::SUCCESS;
127
128 } else {
129
130 log << MSG::ERROR << "Uknown mode to access ROOT file" << endmsg;
131 return StatusCode::FAILURE;
132 }
133
134 return StatusCode::FAILURE;
135 }
136
137 //------------------------------------------------------------------------------
138 StatusCode RootHistCnv::RFileCnv::createRep( DataObject* pObject, IOpaqueAddress*& refpAddress )
139 //------------------------------------------------------------------------------
140 {
141 refpAddress = pObject->registry()->address();
142 return RFileCnv::updateRep( refpAddress, pObject );
143 }
144
145 //-----------------------------------------------------------------------------
146 StatusCode RootHistCnv::RFileCnv::updateRep( IOpaqueAddress* pAddress, DataObject* pObject )
147 //-----------------------------------------------------------------------------
148 {
149 MsgStream log( msgSvc(), "RFileCnv" );
150 std::string ooname = pAddress->par()[1];
151
152 NTuple::File* pFile = dynamic_cast<NTuple::File*>( pObject );
153 if ( pFile && pFile->isOpen() ) {
154
155 unsigned long* ipar = (unsigned long*)pAddress->ipar();
156 if ( findTFile( ooname, rfile ).isFailure() ) {
157 log << MSG::ERROR << "Problems closing TFile " << ooname << endmsg;
158 return StatusCode::FAILURE;
159 }
160
161 rfile->Write( nullptr, TObject::kOverwrite );
162 if ( log.level() <= MSG::INFO ) {
163 log << MSG::INFO << "dumping contents of " << ooname << endmsg;
164 rfile->Print();
165 }
166
167 /*
168 * MetaData
169 * Ana Trisovic
170 * March 2015
171 * */
172 SmartIF<IMetaDataSvc> mds;
173 mds = serviceLocator()->service( "Gaudi::MetaDataSvc", false );
174 // auto mds = service<IMetaDataSvc>("MetaDataSvc", false);
175 if ( mds ) {
176 std::map<std::string, std::string> m_metadata = mds->getMetaDataMap();
177 if ( !rfile->WriteObject( &m_metadata, "info" ) ) {
178 return StatusCode::FAILURE;
179 }
180 }
181 /* */
182
183 rfile->Close();
184 delete rfile;
185
186 ipar[0] = 0;
187 pFile->setOpen( false );
188 return StatusCode::SUCCESS;
189
190 } else {
191 log << MSG::ERROR << "TFile " << ooname << " is not open" << endmsg;
192 }
193 return StatusCode::FAILURE;
194 }
MsgStream
Definition of the MsgStream class used to transmit messages.
Definition: MsgStream.h:24
StatusCode::SUCCESS
Definition: StatusCode.h:31
SmartIF
Small smart pointer class with automatic reference counting for IInterface.
Definition: IConverter.h:15
RootCompressionSettings.h
std::string::empty
T empty(T...args)
NTuple::File::setOpen
void setOpen(bool flag)
Set "open" flag.
Definition: NTuple.h:1041
RootHistCnv::RFileCnv::rfile
TFile * rfile
Pointer to ROOT file.
Definition: RFileCnv.h:49
ISvcLocator
The ISvcLocator is the interface implemented by the Service Factory in the Application Manager to loc...
Definition: ISvcLocator.h:25
StatusCode::FAILURE
Definition: StatusCode.h:31
RootHistCnv::RFileCnv::initialize
StatusCode initialize() override
Initialise.
Definition: RFileCnv.cpp:28
MsgStream.h
Converter::objType
const CLID & objType() const override
Retrieve the class type of objects the converter produces.
Definition: Converter.cpp:14
Converter::initialize
StatusCode initialize() override
Initialize the converter.
Definition: Converter.cpp:44
RootHistCnv::RFileCnv::createRep
StatusCode createRep(DataObject *pObject, IOpaqueAddress *&refpAddress) override
Convert the transient object to the requested representation.
Definition: RFileCnv.cpp:138
MSG::DEBUG
Definition: IMessageSvc.h:16
RootHistCnv::RFileCnv::updateRep
StatusCode updateRep(IOpaqueAddress *pAddress, DataObject *pObject) override
Convert the transient object to the requested representation.
Definition: RFileCnv.cpp:146
Converter::msgSvc
SmartIF< IMessageSvc > & msgSvc() const
Retrieve pointer to message service.
Definition: Converter.cpp:103
Bootstrap.h
RootHistCnv::RConverter::regTFile
StatusCode regTFile(const std::string, const TFile *)
Definition: RConverter.cpp:271
RootHistCnv::RConverter::findTFile
StatusCode findTFile(const std::string, TFile *&)
Definition: RConverter.cpp:286
MSG::VERBOSE
Definition: IMessageSvc.h:16
IOpaqueAddress::par
virtual const std::string * par() const =0
Retrieve String parameters.
std::map< std::string, std::string >
DataObject::registry
IRegistry * registry() const
Get pointer to Registry.
Definition: DataObject.h:72
std::string
STL class.
RootHistCnv::RFileCnv
NTuple converter class definition.
Definition: RFileCnv.h:23
RootHistCnv::RFileCnv::createObj
StatusCode createObj(IOpaqueAddress *pAddress, DataObject *&refpObject) override
Create the transient representation of an object.
Definition: RFileCnv.cpp:42
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:79
IMetaDataSvc::getMetaDataMap
virtual std::map< std::string, std::string > getMetaDataMap()=0
IJobOptionsSvc
Main interface for the JobOptions service.
Definition: IJobOptionsSvc.h:20
Gaudi::svcLocator
GAUDI_API ISvcLocator * svcLocator()
IOpaqueAddress::ipar
virtual const unsigned long * ipar() const =0
Access to generic link parameters.
StatusCode
This class is used for returning status codes from appropriate routines.
Definition: StatusCode.h:28
RootHistCnv::RDirectoryCnv
Create persistent and transient representations of data store directories.
Definition: RDirectoryCnv.h:17
RootHistCnv
Definition: DirectoryCnv.h:17
IRegistry.h
IMetaDataSvc.h
RootHistCnv::RootCompressionSettings
Simple class to decode a ROOT compression settings string, of the form &#39;<Alg>:<level>&#39; into the integ...
Definition: RootCompressionSettings.h:20
NTuple::File::isOpen
bool isOpen() const
Access "open" flag.
Definition: NTuple.h:1043
RootHistCnv::RFileCnv::m_compLevel
std::string m_compLevel
Compression setting, property RFileCnv.GlobalCompression.
Definition: RFileCnv.h:50
IOpaqueAddress.h
Converter::serviceLocator
SmartIF< ISvcLocator > & serviceLocator() const
Retrieve pointer to service locator.
Definition: Converter.cpp:100
DECLARE_NAMESPACE_CONVERTER_FACTORY
#define DECLARE_NAMESPACE_CONVERTER_FACTORY(n, x)
Definition: Converter.h:177
RootHistCnv::RootCompressionSettings::level
int level() const
Get the level.
Definition: RootCompressionSettings.h:30
std::string::c_str
T c_str(T...args)
MSG::ERROR
Definition: IMessageSvc.h:16
std::string::substr
T substr(T...args)
gaudiComponentHelp.root
root
Definition: gaudiComponentHelp.py:18
MsgStream::level
MSG::Level level() const
Retrieve output level.
Definition: MsgStream.h:108
IOpaqueAddress
Opaque address interface definition.
Definition: IOpaqueAddress.h:23
StatusCode::ignore
void ignore() const
Definition: StatusCode.h:109
RFileCnv.h
MSG::INFO
Definition: IMessageSvc.h:16
IJobOptionsSvc::getClientProperty
virtual const Gaudi::Details::PropertyBase * getClientProperty(const std::string &client, const std::string &name) const =0
Get a property for a client.
IRegistry::address
virtual IOpaqueAddress * address() const =0
Retrieve opaque storage address.
DataObject
A DataObject is the base class of any identifiable object on any data store.
Definition: DataObject.h:29
ISvcLocator.h
endmsg
MsgStream & endmsg(MsgStream &s)
MsgStream Modifier: endmsg. Calls the output method of the MsgStream.
Definition: MsgStream.h:209
NTuple::File
Small class representing an N tuple file in the transient store.
Definition: NTuple.h:1002
Gaudi.Configuration.log
log
Definition: Configuration.py:13
IJobOptionsSvc.h