| The Gaudi Framework v27r1

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