| The Gaudi Framework v28r2-patches (bc4ad17a)

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