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