| The Gaudi Framework v28r2p1 (f1a77ff4)

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

- File List
- File Members

RConverter.cpp
Go to the documentation of this file.
1 #define ROOTHISTCNV_RCONVERTER_CPP
2
3 // Include files
4 #include "RConverter.h"
5 #include "GaudiKernel/DataObject.h"
6 #include "GaudiKernel/IDataManagerSvc.h"
7 #include "GaudiKernel/IDataProviderSvc.h"
8 #include "GaudiKernel/IRegistry.h"
9 #include "GaudiKernel/MsgStream.h"
10 #include "GaudiKernel/SmartIF.h"
11 #include "RootObjAddress.h"
12
13 #include "TDirectory.h"
14 #include "TFile.h"
15 #include "TTree.h"
16 #include <list>
17 #include <string>
18
19 namespace
20 {
21 std::map<std::string, TFile*> s_fileMap;
22 }
23
24 //-----------------------------------------------------------------------------
25 StatusCode RootHistCnv::RConverter::createDirectory( const std::string& loc )
26 //-----------------------------------------------------------------------------
27 {
28 MsgStream log( msgSvc(), "RConverter::createDir" );
29
30 // Get rid of leading /NTUPLES
31 std::string full = diskDirectory( loc );
32
33 std::string fil, cur, s;
34 TDirectory* gDir = gDirectory;
35
36 TFile* tf = nullptr;
37 if ( findTFile( loc, tf ).isSuccess() ) {
38 tf->cd();
39 }
40
41 std::vector<std::string> lpath;
42 int i = 1;
43
44 auto p = full.find( ":", 0 );
45 if ( p != std::string::npos ) {
46 fil = full.substr( 0, p );
47 i = p + 1;
48 fil += ":/";
49 gDirectory->cd( fil.c_str() );
50 }
51
52 while ( ( p = full.find( "/", i ) ) != std::string::npos ) {
53 s = full.substr( i, p - i );
54 lpath.push_back( s );
55 i = p + 1;
56 }
57 lpath.push_back( full.substr( i ) );
58
59 if ( full.compare( 0, 1, "/" ) == 0 ) gDirectory->cd( "/" );
60
61 for ( const auto& litr : lpath ) {
62 cur = litr;
63 if ( !gDirectory->GetKey( litr.c_str() ) ) {
64 gDirectory->mkdir( litr.c_str() );
65 }
66 gDirectory->cd( litr.c_str() );
67 }
68
69 gDirectory = gDir;
70
71 return StatusCode::SUCCESS;
72 }
73 //-----------------------------------------------------------------------------
74 std::string RootHistCnv::RConverter::diskDirectory( const std::string& loc )
75 //-----------------------------------------------------------------------------
76 {
77 // Get rid of leading /NTUPLES/{INPUT_STREAM} or /stat/{INPUT_STREAM}
78 std::string dir;
79 long lf1 = loc.find( "/NTUPLES/" );
80 long lf2 = loc.find( "/stat/" );
81 long ll;
82 if ( lf1 != -1 ) {
83 ll = loc.find( "/", lf1 + 9 );
84
85 } else if ( lf2 != -1 ) {
86 ll = loc.find( "/", lf2 + 6 );
87
88 } else {
89 MsgStream log( msgSvc(), "RConverter" );
90 log << MSG::ERROR << "diskDirectory(" << loc << ")"
91 << " --> no leading /NTUPLES/ or /stat/" << endmsg;
92 return loc;
93 }
94 // dir = loc.substr(ll+8);
95
96 if ( ll == -1 ) {
97 dir = "/";
98 } else {
99 dir = loc.substr( ll );
100 }
101
102 return dir;
103 }
104
105 //-----------------------------------------------------------------------------
106 std::string RootHistCnv::RConverter::directory( const std::string& loc )
107 //-----------------------------------------------------------------------------
108 {
109 return diskDirectory( loc );
110 }
111
112 //-----------------------------------------------------------------------------
113 void RootHistCnv::RConverter::setDirectory( const std::string& loc )
114 //-----------------------------------------------------------------------------
115 {
116 MsgStream log( msgSvc(), "RConverter" );
117 TFile* tf = nullptr;
118
119 std::string full = diskDirectory( loc );
120
121 // get associated TFile
122 if ( findTFile( loc, tf ).isSuccess() ) {
123 tf->cd();
124 } else {
125 log << MSG::ERROR << "error getting TFile name " << loc << endmsg;
126 }
127
128 int p, i = 1;
129 std::string cur, sdir;
130
131 gDirectory->cd( "/" );
132 while ( ( p = full.find( "/", i ) ) != -1 ) {
133 sdir = full.substr( i, p - i );
134 if ( !gDirectory->GetKey( sdir.c_str() ) ) {
135 log << MSG::ERROR << "cannot cd to " << full << " from " << gDirectory->GetPath() << endmsg;
136 return;
137 }
138 gDirectory->cd( sdir.c_str() );
139
140 i = p + 1;
141 }
142 gDirectory->cd( full.substr( i ).c_str() );
143 }
144
145 //-----------------------------------------------------------------------------
146 void RootHistCnv::RConverter::setDiskDirectory( const std::string& loc )
147 //-----------------------------------------------------------------------------
148 {
149 setDirectory( loc );
150 }
151
152 //-----------------------------------------------------------------------------
153 std::string RootHistCnv::RConverter::getDirectory()
154 //-----------------------------------------------------------------------------
155 {
156 return gDirectory->GetPath();
157 }
158
159 //-----------------------------------------------------------------------------
160 StatusCode RootHistCnv::RConverter::createAddress( DataObject* pObj, TDirectory* pDir, TObject* pTObj,
161 IOpaqueAddress*& refpAddr )
162 //-----------------------------------------------------------------------------
163 {
164 // Get address again....it does not change
165 IRegistry* pReg = pObj->registry();
166 if ( pReg ) {
167 refpAddr = pReg->address();
168 if ( !refpAddr ) {
169 refpAddr = new RootObjAddress( repSvcType(), objType(), pReg->name(), "", (unsigned long)( pDir ),
170 (unsigned long)( pTObj ), pTObj );
171
172 return StatusCode::SUCCESS;
173 }
174 }
175 return StatusCode::FAILURE;
176 }
177
178 //--------------------------------------------------------------------------
179 StatusCode RootHistCnv::RConverter::createAddress( const std::string& rzdir, const CLID& clid, const std::string& title,
180 TObject* pTObj, IOpaqueAddress*& refpAddress )
181 //--------------------------------------------------------------------------
182 {
183 auto pA = new RootObjAddress( repSvcType(), clid, rzdir, title, 0, 0, pTObj );
184
185 refpAddress = pA;
186 return StatusCode::SUCCESS;
187 }
188
189 //--------------------------------------------------------------------------
190 StatusCode RootHistCnv::RConverter::createAddress( const std::string& rzdir, const CLID& clid, long id, TObject* pTobj,
191 IOpaqueAddress*& refpAddress )
192 //--------------------------------------------------------------------------
193 {
194 auto obj = std::to_string( id );
195 StatusCode status = createAddress( rzdir, clid, obj, pTobj, refpAddress );
196 if ( status.isSuccess() ) {
197 unsigned long* ipar = (unsigned long*)refpAddress->ipar();
198 ipar[0] = id;
199 }
200 return status;
201 }
202
203 //-----------------------------------------------------------------------------
205 TDirectory* RootHistCnv::RConverter::changeDirectory( DataObject* pObject )
206 //-----------------------------------------------------------------------------
207 {
208 if ( pObject ) {
209 IRegistry* pReg = pObject->registry();
210 if ( pReg ) {
211 auto dataMgr = dataProvider().as<IDataManagerSvc>();
212 if ( dataMgr ) {
213 IRegistry* pParentReg = nullptr;
214 StatusCode status = dataMgr->objectParent( pReg, pParentReg );
215 if ( status.isSuccess() ) {
216 IOpaqueAddress* pParAddr = pParentReg->address();
217 if ( pParAddr ) {
218 TDirectory* pParentDir = (TDirectory*)pParAddr->ipar()[0];
219 if ( pParentDir ) {
220 gDirectory->cd( pParentDir->GetPath() );
221 return pParentDir;
222 }
223 }
224 }
225 }
226 }
227 }
228 return nullptr;
229 }
230
231 //-----------------------------------------------------------------------------
233 StatusCode RootHistCnv::RConverter::createRep( DataObject* pObject, IOpaqueAddress*& pAddr )
234 //-----------------------------------------------------------------------------
235 {
236 GlobalDirectoryRestore restore;
237 pAddr = nullptr;
238 try {
239 TDirectory* pParentDir = changeDirectory( pObject );
240 if ( pParentDir ) {
241 TObject* pTObj = createPersistent( pObject );
242 if ( pTObj ) {
243 pTObj->Write();
244 delete pTObj;
245 return createAddress( pObject, pParentDir, nullptr, pAddr );
246 }
247 }
248 } catch ( ... ) {
249 }
250 MsgStream log( msgSvc(), "RConverter" );
251 log << MSG::ERROR << "Failed to create persistent Object!" << endmsg;
252 return StatusCode::FAILURE;
253 }
254
255 //-----------------------------------------------------------------------------
256 StatusCode RootHistCnv::RConverter::readObject( IOpaqueAddress* /* pAddr */, DataObject*& /* refpObj */ )
257 {
258 // MsgStream log(msgSvc(), "RConverter::readObject");
259 // log << MSG::WARNING << pAddr->par()[0] << " <> " << pAddr->par()[1]
260 // << " <> "
261 // << pAddr->ipar()[0] << " <> " << pAddr->ipar()[1] << " <> "
262 // << pAddr->registry()->identifier() << endmsg;
263
264 return StatusCode::SUCCESS;
265 }
266
267 //-----------------------------------------------------------------------------
268 TObject* RootHistCnv::RConverter::createPersistent( DataObject* /* pObj */ ) { return nullptr; }
269
270 //-----------------------------------------------------------------------------
271 StatusCode RootHistCnv::RConverter::regTFile( const std::string id, const TFile* tfile )
272 //-----------------------------------------------------------------------------
273 {
274 auto imap = s_fileMap.find( id );
275 if ( imap != s_fileMap.end() ) {
276 MsgStream log( msgSvc(), "RConverter" );
277 log << MSG::ERROR << "cannot register TTree " << id << ": already exists" << endmsg;
278 return StatusCode::FAILURE;
279 }
280 s_fileMap[id] = const_cast<TFile*>( tfile );
281
282 return StatusCode::SUCCESS;
283 }
284
285 //-----------------------------------------------------------------------------
286 StatusCode RootHistCnv::RConverter::findTFile( const std::string id, TFile*& tfile )
287 //-----------------------------------------------------------------------------
288 {
289 MsgStream log( msgSvc(), "RConverter" );
290 tfile = nullptr;
291
292 std::string idm;
293
294 // make sure we only get first two parts of id
295 int i1, i2, i3;
296 i1 = id.find( "/", 0 );
297 if ( i1 != 0 ) {
298 log << MSG::ERROR << "Directory name does not start with \"/\": " << id << endmsg;
299 return StatusCode::FAILURE;
300 }
301 i2 = id.find( "/", i1 + 1 );
302 if ( i2 == -1 ) {
303 log << MSG::ERROR << "Directory name has only one part: " << id << endmsg;
304 return StatusCode::FAILURE;
305 }
306 i3 = id.find( "/", i2 + 1 );
307 if ( i3 == -1 ) {
308 idm = id;
309 } else {
310 idm = id.substr( 0, i3 );
311 }
312
313 auto imap = s_fileMap.find( idm );
314 if ( imap == s_fileMap.end() ) return StatusCode::FAILURE;
315 tfile = imap->second;
316 return StatusCode::SUCCESS;
317 }
318 //-----------------------------------------------------------------------------
319 std::string RootHistCnv::RConverter::convertId( const std::string& id ) const
320 //-----------------------------------------------------------------------------
321 {
322 bool forced = false;
323 if ( id.size() > 0 && isdigit( id[0] ) ) {
324 try {
325 Gaudi::Property<bool> tmp;
326 tmp.assign( conversionSvc().as<IProperty>()->getProperty( "ForceAlphaIds" ) );
327 forced = tmp.value();
328 } catch ( ... ) {
329 }
330 }
331 if ( forced )
332 return "h" + id;
333 else
334 return id;
335 }
336 //-----------------------------------------------------------------------------
337 StatusCode RootHistCnv::RConverter::error( const std::string& msg )
338 //-----------------------------------------------------------------------------
339 {
340 MsgStream log( msgSvc(), "RootHistCnv" );
341 log << MSG::ERROR << msg << endmsg;
342 return StatusCode::FAILURE;
343 }
RootHistCnv::RConverter::getDirectory
std::string getDirectory()
Definition: RConverter.cpp:153
MsgStream
Definition of the MsgStream class used to transmit messages.
Definition: MsgStream.h:24
StatusCode::SUCCESS
Definition: StatusCode.h:30
RootHistCnv::RConverter::diskDirectory
std::string diskDirectory(const std::string &loc)
Definition: RConverter.cpp:74
RootHistCnv::RConverter::setDirectory
void setDirectory(const std::string &loc)
Definition: RConverter.cpp:113
StatusCode::FAILURE
Definition: StatusCode.h:29
Gaudi::Property
Implementation of property with value of concrete type.
Definition: Property.h:314
MsgStream.h
Converter::objType
const CLID & objType() const override
Retrieve the class type of objects the converter produces.
Definition: Converter.cpp:14
RootHistCnv::RConverter::createRep
StatusCode createRep(DataObject *pObj, IOpaqueAddress *&refpAddr) override
Convert the transient object to the requested representation.
Definition: RConverter.cpp:233
StatusCode::isSuccess
bool isSuccess() const
Test for a status code of SUCCESS.
Definition: StatusCode.h:74
std::to_string
T to_string(T...args)
Converter::msgSvc
SmartIF< IMessageSvc > & msgSvc() const
Retrieve pointer to message service.
Definition: Converter.cpp:129
RootHistCnv::RConverter::regTFile
StatusCode regTFile(const std::string, const TFile *)
Definition: RConverter.cpp:271
IRegistry::name
virtual const name_type & name() const =0
Name of the directory (or key)
RootHistCnv::RootObjAddress
Definition: RootObjAddress.h:10
RConverter.h
Converter::conversionSvc
SmartIF< IConversionSvc > & conversionSvc() const override
Get conversion service the converter is connected to.
Definition: Converter.cpp:108
RootHistCnv::RConverter::findTFile
StatusCode findTFile(const std::string, TFile *&)
Definition: RConverter.cpp:286
RootHistCnv::RConverter::createDirectory
StatusCode createDirectory(const std::string &loc)
Definition: RConverter.cpp:25
std::map
STL class.
DataObject::registry
IRegistry * registry() const
Get pointer to Registry.
Definition: DataObject.h:74
RootHistCnv::RConverter::changeDirectory
TDirectory * changeDirectory(DataObject *pObject)
Switch to object directory (=Parent directory)
Definition: RConverter.cpp:205
std::string
STL class.
std::vector::push_back
T push_back(T...args)
SmartIF::as
SmartIF< IFace > as() const
return a new SmartIF instance to another interface
Definition: SmartIF.h:110
IDataManagerSvc.h
IDataProviderSvc.h
RootHistCnv::RConverter::repSvcType
long repSvcType() const override
Definition: RConverter.h:41
IOpaqueAddress::ipar
virtual const unsigned long * ipar() const =0
Access to generic link parameters.
RootHistCnv::RConverter::createPersistent
virtual TObject * createPersistent(DataObject *pObj)
Create the persistent representation of an object.
Definition: RConverter.cpp:268
StatusCode
This class is used for returning status codes from appropriate routines.
Definition: StatusCode.h:26
SmartIF.h
IRegistry.h
IRegistry
The IRegistry represents the entry door to the environment any data object residing in a transient da...
Definition: IRegistry.h:22
CLID
unsigned int CLID
Class ID definition.
Definition: ClassID.h:8
RootObjAddress.h
std::string::find
T find(T...args)
Converter::dataProvider
SmartIF< IDataProviderSvc > & dataProvider() const override
Get Data provider service.
Definition: Converter.cpp:92
RootHistCnv::RConverter::directory
std::string directory(const std::string &loc)
Definition: RConverter.cpp:106
std::vector< std::string >
RootHistCnv::RConverter::error
StatusCode error(const std::string &msg)
Definition: RConverter.cpp:337
IDataManagerSvc
Definition: IDataManagerSvc.h:45
GlobalDirectoryRestore
Definition: RConverter.h:16
std::string::c_str
T c_str(T...args)
GaudiMP.FdsRegistry.msg
msg
Definition: FdsRegistry.py:8
gaudirun.s
string s
Definition: gaudirun.py:245
RootHistCnv::RConverter::convertId
std::string convertId(const std::string &) const
Definition: RConverter.cpp:319
RootHistCnv::RConverter::readObject
virtual StatusCode readObject(IOpaqueAddress *pAddr, DataObject *&refpObj)
Create the transient representation of an object.
Definition: RConverter.cpp:256
MSG::ERROR
Definition: IMessageSvc.h:21
std::string::substr
T substr(T...args)
Gaudi::Property::assign
bool assign(const Details::PropertyBase &source) override
get the value from another property
Definition: Property.h:596
Gaudi::Property::value
const ValueType & value() const
Backward compatibility (.
Definition: Property.h:475
IOpaqueAddress
Opaque address interface definition.
Definition: IOpaqueAddress.h:23
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
endmsg
MsgStream & endmsg(MsgStream &s)
MsgStream Modifier: endmsg. Calls the output method of the MsgStream.
Definition: MsgStream.h:244
std::string::compare
T compare(T...args)
RootHistCnv::RConverter::createAddress
StatusCode createAddress(DataObject *pObject, TDirectory *pDir, TObject *pTObject, IOpaqueAddress *&refpAddr)
Create address of the transient object according to the requested representation. ...
Definition: RConverter.cpp:160
DataObject.h
Gaudi.Configuration.log
log
Definition: Configuration.py:13
Gaudi::Utils::getProperty
GAUDI_API Gaudi::Details::PropertyBase * getProperty(const IProperty *p, const std::string &name)
simple function which gets the property with given name from the component
Definition: Property.cpp:223
RootHistCnv::RConverter::setDiskDirectory
void setDiskDirectory(const std::string &loc)
Definition: RConverter.cpp:146