| The Gaudi Framework v32r1 (f65d50dc)

TESSerializer.cpp
Go to the documentation of this file.
1 #include "GaudiMP/TESSerializer.h"
2
3 // Framework include files
4 #include "GaudiKernel/DataObject.h"
5 #include "GaudiKernel/DataStoreItem.h"
6 #include "GaudiKernel/GaudiException.h"
7 #include "GaudiKernel/IDataManagerSvc.h"
8 #include "GaudiKernel/IDataProviderSvc.h"
9 #include "GaudiKernel/IRegistry.h"
10 #include "GaudiKernel/LinkManager.h"
11 #include "GaudiKernel/System.h"
12
13 #include "GaudiKernel/ClassID.h"
14 #include "GaudiKernel/GenericAddress.h"
15 #include "GaudiKernel/IOpaqueAddress.h"
16 #include "GaudiKernel/KeyedContainer.h"
17
18 #include "GaudiKernel/MsgStream.h"
19
20 // ROOT include files
21 #include "TBufferFile.h"
22 #include "TClass.h"
23 #include "TInterpreter.h"
24 #include "TROOT.h"
25
26 #include <map>
27
28 // a constant to guard against seg-faults in loadBuffer
29 #define SERIALIZER_END "EOF"
30
31 namespace {
32 struct DataObjectPush {
33 DataObjectPush( DataObject*& p ) { Gaudi::pushCurrentDataObject( &p ); }
34 ~DataObjectPush() { Gaudi::popCurrentDataObject(); }
35 };
36 } // namespace
37
38 using namespace std;
39
40 bool GaudiMP::TESSerializer::analyse( IRegistry* dir, int level ) {
41 if ( level < m_currentItem->depth() ) {
42 if ( dir->object() != 0 ) {
43 m_objects.push_back( dir->object() );
44 return true;
45 }
46 }
47 return false;
48 }
49
51 GaudiMP::TESSerializer::TESSerializer( IDataProviderSvc* svc, IAddressCreator* ac )
52 : m_TES( svc )
53 , m_TESMgr( dynamic_cast<IDataManagerSvc*>( svc ) )
54 , m_currentItem( 0 )
55 , m_verifyItems( false )
56 , m_strict( false )
57 , m_addressCreator( ac ) {}
58
60 void GaudiMP::TESSerializer::dumpBuffer( TBufferFile& buffer ) {
61 //
62 // Write all valid objects to the TBufferFile provided in the argument
63 // As objects are collected, the member variable m_classMap is filled
64 // with ROOT Class data, and is kept by the Serializer for future
65 // reference
66 //
67 // @paramTBufferFile& buffer : TBufferFile passed by reference
68 // Cannot be declared inside the method, as repeated calls
69 // can cause confusion and buffer wiping.
70 //
71 StatusCode status;
72 DataObject* obj;
73
74 // Clear current selection
75 m_objects.erase( m_objects.begin(), m_objects.end() );
76
77 // Traverse the tree and collect the requested objects
78 for ( Items::iterator i = m_itemList.begin(); i != m_itemList.end(); i++ ) {
79 m_currentItem = ( *i );
80 // cout << "Retrieving Mandatory Object : " << m_currentItem->path() << endl;
81 status = m_TES->retrieveObject( m_currentItem->path(), obj );
82 if ( status.isSuccess() ) {
83 m_TESMgr->traverseSubTree( obj, this );
84 } else {
85 string text( "WARNING: Cannot retrieve TES object(s) for serialisation: " );
86 text += m_currentItem->path();
87 if ( m_strict ) {
88 throw GaudiException( text + m_currentItem->path(), "", status );
89 } else {
90 cout << text << endl;
91 // return StatusCode::FAILURE;
92 }
93 }
94 }
95 // Traverse the tree and collect the requested objects (tolerate missing items here)
96 for ( Items::iterator i = m_optItemList.begin(); i != m_optItemList.end(); i++ ) {
97 m_currentItem = ( *i );
98 // cout << "Retrieving Optional Object : " << m_currentItem->path() << endl;
99 status = m_TES->retrieveObject( m_currentItem->path(), obj );
100 if ( status.isSuccess() ) { m_TESMgr->traverseSubTree( obj, this ); }
101 }
102
103 // cout << "TESSerializer : Beginning loop to write to TBufferFile for nObjects : " << m_objects.size() << endl;
104 buffer.WriteInt( m_objects.size() );
105
106 for ( Objects::iterator i = m_objects.begin(); i != m_objects.end(); ++i ) {
107 DataObject* pObj = ( *i ); /* define pointer !pObj! to a data object */
108 DataObjectPush p( pObj ); /* add the data object to the list... */
109
110 // We build a map so gROOT has to access the whole class database as little as possible
111 TClass* cl; /* announce a TClass */
112 const type_info& objClass = typeid( *pObj ); /* get the type of the data object */
113 // cout << "TES Object : " << pObj->registry()->identifier() << endl;
114 string objClassName = System::typeinfoName( objClass ); /* and then get the descriptive string from System */
115
116 /* First go : populate the class map
117 Subsequent : refer to class map */
118 if ( m_classMap[objClassName] ) {
119 cl = m_classMap[objClassName];
120 } else {
121 /* Map new object : pull the class name from the objects c_str() method */
122 const char* clName = objClassName.c_str();
123 /* Find the relevant Tclass (cl) in gROOT, and fill the map entry */
124 cl = gROOT->GetClass( clName );
125 m_classMap[objClassName] = cl;
126 }
127
128 /* Now, check if clname was valid... */
129 if ( cl == 0 ) {
130 if ( m_strict ) {
131 throw GaudiException( "gROOT->GetClass cannot find clName", objClassName, StatusCode::FAILURE );
132 } else {
133 cout << "WARNING: gROOT->GetClass fails for clname : " << objClassName.c_str() << endl;
134 cout << "WARNING: Disregarding " << objClassName.c_str() << "erasing from object list" << endl;
135 m_objects.erase( i );
136 continue;
137 }
138 }
139
140 // write object to buffer in order location-name-object
141 std::string loc = pObj->registry()->identifier();
142 buffer.WriteString( loc.c_str() );
143 buffer.WriteString( cl->GetName() );
144 cl->Streamer( pObj, buffer );
145
146 /* take care of links */
147 LinkManager* linkMgr = pObj->linkMgr();
148 int numLinks = linkMgr->size();
149 buffer.WriteInt( numLinks );
150 // now write each link
151 for ( int it = 0; it != numLinks; it++ ) {
152 const string& link = linkMgr->link( it )->path();
153 buffer.WriteString( link.c_str() );
154 }
155
156 // now do the thing with the opaqueAddress
157 // to go from string->object when recovering, will need svc_type, and clid, aswell as the string version
158 IOpaqueAddress* iop = pObj->registry()->address();
159 if ( iop ) {
160 buffer.WriteInt( 1 );
161 const string* par = iop->par();
162 long svcType = iop->svcType();
163 long clid = iop->clID();
164 buffer.WriteLong( svcType );
165 buffer.WriteLong( clid );
166 buffer.WriteString( par->c_str() );
167 } else {
168 buffer.WriteInt( 0 );
169 }
170 // object complete, continue in for-loop
171 }
172
173 // Final Actions
174 // Write the End Flag, to avoid potential SegFaults on loadBuffer
175 buffer.WriteString( SERIALIZER_END );
176 // return StatusCode::SUCCESS;
177 }
178
180 void GaudiMP::TESSerializer::loadBuffer( TBufferFile& buffer ) {
181
182 // reverse mechanism of dumps
183 // buffer is: length of DataObjects vector
184 // location string
185 // type name string
186 // the object itself
187 // count of links
188 // list of links (conditional on count)
189 // flag indicating Opaque Address presence
190 // Opaque Address svcType (conditional on flag)
191 // Opaque Address clID (conditional on flag)
192 // Opaque Address par (conditional on flag)
193
194 int nObjects;
195 // 3 StatusCodes... for :
196 // general use : registering objects : creating OpaqueAddresses
197 StatusCode sc, registerStat, createAddressStat;
198
199 // Prepare for Reading
200 buffer.SetReadMode();
201 buffer.SetBufferOffset();
202
203 buffer.ReadInt( nObjects );
204 for ( int i = 0; i < nObjects; ++i ) {
205 char text[4096];
206 buffer.ReadString( text, sizeof( text ) );
207 string location( text );
208 if ( !location.compare( "EOF" ) ) {
209 /* There was an error in serialization, but the EOF
210 flag marks the endpoint in any case */
211 break;
212 }
213 buffer.ReadString( text, sizeof( text ) );
214 TClass* cl = gROOT->GetClass( text );
215 if ( cl == 0 ) {
216 if ( m_strict ) {
217 throw GaudiException( "gROOT->GetClass cannot find clName", text, StatusCode::FAILURE );
218 } else {
219 cout << "TESSerializer WARNING : gROOT->GetClass fails for clname : " << location.c_str() << endl;
220 continue;
221 }
222 }
223
225 DataObject* obj = (DataObject*)cl->New();
226 DataObjectPush push( obj ); // This is magic!
227 cl->Streamer( obj, buffer );
228
229 // now restore links
230 if ( obj ) {
231 int nlink = 0;
232 LinkManager* lnkMgr = obj->linkMgr();
233 buffer.ReadInt( nlink );
234
235 for ( int j = 0; j < nlink; ++j ) {
236 buffer.ReadString( text, sizeof( text ) );
237 lnkMgr->addLink( text, 0 );
238 }
239 }
240
241 // Re-register...
242 registerStat = m_TES->registerObject( location, obj );
243 if ( registerStat.isFailure() ) {
244 DataObject* dummy = NULL;
245 if ( location == "/Event" ) {
246 sc = m_TESMgr->setRoot( location, obj );
247 if ( sc.isFailure() ) throw GaudiException( "Cannot set root at location " + location, "", sc );
248 } else {
249 m_TES->findObject( location, dummy );
250 if ( !dummy )
251 m_TES->registerObject( location, obj );
252 else {
253 // skipping to the next object
254 // (flush the remaining metadata in the buffer)
255 int flag( 0 );
256 buffer.ReadInt( flag );
257 if ( flag ) {
258 long svcType;
259 buffer.ReadLong( svcType );
260 long clid;
261 buffer.ReadLong( clid );
262 buffer.ReadString( text, sizeof( text ) );
263 }
264 continue;
265 }
266 }
267 }
268 // next is the opaque address information
269 // create Generic Address using the info from the TBufferFile,
270 // then create an IOpaqueAddress object using the Persistency Svc
271 // IOpaque Address pointer (blank... pass the ref to the createAddress Fn)
272
273 int flag( 0 );
274 buffer.ReadInt( flag );
275 // flag will be 0 or 1 to indicate OpaqueAddress Info
276 if ( flag == 1 ) {
277 // will need an IOpaqueAddress and its ref
278 IOpaqueAddress* iop;
279 IOpaqueAddress*& iopref = iop;
280 // Read svcType, clID and par from buffer
281 long svcType;
282 buffer.ReadLong( svcType );
283
284 long clid;
285 buffer.ReadLong( clid );
286 const CLID classid( clid );
287
288 char* cp;
289 cp = buffer.ReadString( text, sizeof( text ) );
290 const string opaque( cp );
291 // create Generic address
292 // already have svcType, clID, par1.. just make dummy variables for par2, and ipar1 and 2
293 const string& p2 = "";
294 unsigned long ip1( 0 );
295 unsigned long ip2( 0 );
296 GenericAddress gadd( svcType, classid, opaque, p2, ip1, ip2 );
297
298 // now create the address
299 createAddressStat =
300 m_addressCreator->createAddress( gadd.svcType(), gadd.clID(), gadd.par(), gadd.ipar(), iopref );
301 if ( createAddressStat.isFailure() ) {
302 throw GaudiException( "Failure in creating OpaqueAddress for reconstructed registry", "", createAddressStat );
303 }
304 // And finally, set this address
305 obj->registry()->setAddress( iop );
306 }
307 // all done
308 }
309 }
310
311 // Protected
313 void GaudiMP::TESSerializer::addItem( Items& itms, const std::string& descriptor ) {
314 // supports # notation
315 int level = 0;
316
317 std::string slevel;
318 std::string obj_path;
319
320 // Process the incoming string
321 size_t sep = descriptor.rfind( "#" );
322 if ( sep > descriptor.length() ) {
323 // invalid sep case (# not found in string)
324 obj_path = descriptor;
325 slevel = "1";
326 } else {
327 // valid sep case
328 obj_path = descriptor.substr( 0, sep );
329 slevel = descriptor.substr( sep + 1 );
330 }
331
332 // Convert the level string to an integer
333 if ( slevel == "*" ) {
334 level = 9999999;
335 } else {
336 level = std::stoi( slevel );
337 }
338
339 // Are we verifying?
340 if ( m_verifyItems ) {
341 size_t idx = obj_path.find( "/", 1 );
342 while ( idx != std::string::npos ) {
343 std::string sub_item = obj_path.substr( 0, idx );
344 if ( 0 == findItem( sub_item ) ) {
345 cout << "... calling addItem with arg : " << sub_item << endl;
346 addItem( itms, sub_item );
347 }
348 idx = obj_path.find( "/", idx + 1 );
349 }
350 }
351 DataStoreItem* item = new DataStoreItem( obj_path, level );
352 // cout << "Adding TESSerializer item " << item->path()
353 // << " with " << item->depth()
354 // << " level(s)." << endl;
355 itms.push_back( item );
356 }
357
359 void GaudiMP::TESSerializer::addItem( const std::string& path ) {
360 // #notation supported
361 addItem( m_itemList, path );
362 }
363
365 void GaudiMP::TESSerializer::addOptItem( const std::string& path ) {
366 // #notation supported
367 addItem( m_optItemList, path );
368 }
369
371 void GaudiMP::TESSerializer::checkItems() {
372 cout << "TESSerializer m_itemList : " << m_itemList.size() << " Items" << endl;
373 for ( Items::const_iterator i = m_itemList.begin(); i != m_itemList.end(); ++i ) {
374 cout << "\tItem : " << ( *i )->path() << endl;
375 }
376 cout << "TESSerializer m_optItemList : " << m_optItemList.size() << " Items" << endl;
377 for ( Items::const_iterator i = m_optItemList.begin(); i != m_optItemList.end(); ++i ) {
378 cout << "\tItem : " << ( *i )->path() << endl;
379 }
380 }
381
383 DataStoreItem* GaudiMP::TESSerializer::findItem( const std::string& path ) {
384 for ( Items::const_iterator i = m_itemList.begin(); i != m_itemList.end(); ++i ) {
385 if ( ( *i )->path() == path ) return ( *i );
386 }
387 for ( Items::const_iterator j = m_optItemList.begin(); j != m_optItemList.end(); ++j ) {
388 if ( ( *j )->path() == path ) return ( *j );
389 }
390 return 0;
391 }
System.h
IOpaqueAddress::par
virtual const std::string * par() const =0
Retrieve String parameters.
GaudiMP::TESSerializer::checkItems
void checkItems()
print out the contents of m_itemList and m_optItemList (std::cout)
Definition: TESSerializer.cpp:371
ClassID.h
GaudiException
Define general base for Gaudi exception.
Definition: GaudiException.h:21
DataObject::registry
IRegistry * registry() const
Get pointer to Registry.
Definition: DataObject.h:72
LinkManager.h
GaudiPython.HistoUtils.path
path
Definition: HistoUtils.py:932
GenericAddress::par
const std::string * par() const override
Retrieve string parameters.
Definition: GenericAddress.h:77
MsgStream.h
System::typeinfoName
GAUDI_API const std::string typeinfoName(const std::type_info &)
Get platform independent information about the class type.
Definition: System.cpp:298
IDataManagerSvc
Definition: IDataManagerSvc.h:45
GaudiMP::TESSerializer::loadBuffer
void loadBuffer(TBufferFile &)
Rebuild TES from items in a TBufferFile.
Definition: TESSerializer.cpp:180
Gaudi::popCurrentDataObject
GAUDI_API void popCurrentDataObject()
Definition: RootIOHandler.cpp:38
DataObject::linkMgr
LinkManager * linkMgr() const
Retrieve Link manager.
Definition: DataObject.h:74
GaudiMP::TESSerializer::TESSerializer
TESSerializer(IDataProviderSvc *svc, IAddressCreator *ac)
Constructor.
Definition: TESSerializer.cpp:51
std::string::rfind
T rfind(T... args)
GaudiPython.HistoUtils.location
location
Definition: HistoUtils.py:936
std::endl
T endl(T... args)
IAddressCreator
IAddressCreator interface definition.
Definition: IAddressCreator.h:28
std
STL namespace.
GenericAddress::clID
const CLID & clID() const override
Access : Retrieve class ID of the link.
Definition: GenericAddress.h:69
GenericAddress
Generic Transient Address.
Definition: GenericAddress.h:20
IDataProviderSvc
Data provider interface definition.
Definition: IDataProviderSvc.h:43
DataStoreItem
Description of the DataStoreItem class.
Definition: DataStoreItem.h:17
IOpaqueAddress::svcType
virtual long svcType() const =0
Retrieve service type.
GenericAddress::svcType
long svcType() const override
Access : retrieve the storage type of the class id.
Definition: GenericAddress.h:73
std::type_info
std::string
STL class.
std::vector::push_back
T push_back(T... args)
DataStoreItem.h
IDataManagerSvc.h
compareOutputFiles.par
string par
Definition: compareOutputFiles.py:465
IDataProviderSvc.h
GaudiMP::TESSerializer::analyse
bool analyse(IRegistry *dir, int level) override
Analysis callback.
Definition: TESSerializer.cpp:40
IOpaqueAddress::clID
virtual const CLID & clID() const =0
Retrieve class information from link.
GaudiMP::TESSerializer::dumpBuffer
void dumpBuffer(TBufferFile &)
Dump TES contents listed in m_itemList/m_optItemList to a TBufferFile.
Definition: TESSerializer.cpp:60
StatusCode
This class is used for returning status codes from appropriate routines.
Definition: StatusCode.h:50
LinkManager::Link::path
const std::string & path() const
Access to path of object.
Definition: LinkManager.h:61
GaudiException.h
LinkManager::link
Link * link(long id)
Retrieve symbolic link identified by ID.
Definition: LinkManager.cpp:29
IRegistry.h
IRegistry
The IRegistry represents the entry door to the environment any data object residing in a transient da...
Definition: IRegistry.h:22
GaudiMP::TESSerializer::addItem
void addItem(const std::string &path)
add an item to the TESSerializer's list (#notation)
Definition: TESSerializer.cpp:359
CLID
unsigned int CLID
Class ID definition.
Definition: ClassID.h:8
Gaudi::pushCurrentDataObject
GAUDI_API void pushCurrentDataObject(DataObject **pobjAddr)
Definition: RootIOHandler.cpp:34
StatusCode::isSuccess
bool isSuccess() const
Definition: StatusCode.h:267
compareOutputFiles.cp
cp
Definition: compareOutputFiles.py:496
IRegistry::address
virtual IOpaqueAddress * address() const =0
Retrieve opaque storage address.
IRegistry::object
virtual DataObject * object() const =0
Retrieve object behind the link.
IOpaqueAddress.h
std::string::find
T find(T... args)
std::string::length
T length(T... args)
std::vector< DataStoreItem * >
TESSerializer.h
IRegistry::setAddress
virtual void setAddress(IOpaqueAddress *pAddress)=0
Set/Update Opaque storage address.
std::string::c_str
T c_str(T... args)
LinkManager::size
long size() const
Retrieve number of link present.
Definition: LinkManager.h:100
StatusCode::FAILURE
constexpr static const auto FAILURE
Definition: StatusCode.h:86
std::string::substr
T substr(T... args)
GenericAddress::ipar
const unsigned long * ipar() const override
Retrieve integer parameters.
Definition: GenericAddress.h:79
gaudirun.level
level
Definition: gaudirun.py:334
IRegistry::identifier
virtual const id_type & identifier() const =0
Full identifier (or key)
StatusCode::isFailure
bool isFailure() const
Definition: StatusCode.h:130
GaudiMP::TESSerializer::addOptItem
void addOptItem(const std::string &path)
add an item to the TESSerializer's optional list (#notation)
Definition: TESSerializer.cpp:365
GaudiMP::TESSerializer::findItem
DataStoreItem * findItem(const std::string &path)
Find single item identified by its path (exact match)
Definition: TESSerializer.cpp:383
IOpaqueAddress
Opaque address interface definition.
Definition: IOpaqueAddress.h:23
SERIALIZER_END
#define SERIALIZER_END
Definition: TESSerializer.cpp:29
DataObject
A DataObject is the base class of any identifiable object on any data store.
Definition: DataObject.h:30
std::cout
GenericAddress.h
std::stoi
T stoi(T... args)
LinkManager
A LinkManager is the object aggregated into a DataObject, which is responsible for the handling of no...
Definition: LinkManager.h:21
KeyedContainer.h
DataObject.h
LinkManager::addLink
long addLink(const std::string &path, const DataObject *pObject) const
Add link by object reference and path.
Definition: LinkManager.cpp:54