The Gaudi Framework  master (da3d77e1)
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 \***********************************************************************************/
11 #include <GaudiMP/TESSerializer.h>
12 
13 // Framework include files
14 #include <GaudiKernel/DataObject.h>
19 #include <GaudiKernel/IRegistry.h>
21 #include <GaudiKernel/System.h>
22 
23 #include <GaudiKernel/ClassID.h>
27 
28 #include <GaudiKernel/MsgStream.h>
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 
41 namespace {
42  struct DataObjectPush {
43  DataObjectPush( DataObject*& p ) { Gaudi::pushCurrentDataObject( &p ); }
44  ~DataObjectPush() { Gaudi::popCurrentDataObject(); }
45  };
46 } // namespace
47 
48 using 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 
70 void 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
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 
169 void 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
302 void 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 
349  // #notation supported
350  addItem( m_itemList, path );
351 }
352 
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 }
SERIALIZER_END
#define SERIALIZER_END
Definition: TESSerializer.cpp:39
IAddressCreator
Definition: IAddressCreator.h:38
std::string
STL class.
DataStoreItem.h
IOpaqueAddress::par
virtual const std::string * par() const =0
Retrieve String parameters.
IDataManagerSvc
Definition: IDataManagerSvc.h:55
AtlasMCRecoFullPrecedenceDump.path
path
Definition: AtlasMCRecoFullPrecedenceDump.py:49
compareOutputFiles.cp
cp
Definition: compareOutputFiles.py:508
AlgSequencer.p2
p2
Definition: AlgSequencer.py:30
StatusCode::isSuccess
bool isSuccess() const
Definition: StatusCode.h:314
System.h
std::vector::reserve
T reserve(T... args)
GaudiException.h
GaudiMP::TESSerializer::loadBuffer
void loadBuffer(TBufferFile &)
Rebuild TES from items in a TBufferFile.
Definition: TESSerializer.cpp:169
IOpaqueAddress
Definition: IOpaqueAddress.h:33
std::vector
STL class.
std::string::find
T find(T... args)
std::vector::size
T size(T... args)
IOpaqueAddress::svcType
virtual long svcType() const =0
Retrieve service type.
ClassID.h
GaudiException
Definition: GaudiException.h:31
GaudiMP::TESSerializer::checkItems
void checkItems()
print out the contents of m_itemList and m_optItemList (std::cout)
Definition: TESSerializer.cpp:360
Gaudi::popCurrentDataObject
GAUDI_API void popCurrentDataObject()
GaudiMP::TESSerializer::findItem
DataStoreItem * findItem(const std::string &path)
Find single item identified by its path (exact match)
Definition: TESSerializer.cpp:372
GenericAddress::clID
const CLID & clID() const override
Access : Retrieve class ID of the link.
Definition: GenericAddress.h:81
IRegistry
Definition: IRegistry.h:32
GenericAddress::svcType
long svcType() const override
Access : retrieve the storage type of the class id.
Definition: GenericAddress.h:85
KeyedContainer.h
IDataProviderSvc.h
GenericAddress.h
std::vector::push_back
T push_back(T... args)
compareOutputFiles.par
par
Definition: compareOutputFiles.py:477
GenericAddress
Definition: GenericAddress.h:30
std::stoi
T stoi(T... args)
GaudiMP::TESSerializer::addItem
void addItem(const std::string &path)
add an item to the TESSerializer's list (#notation)
Definition: TESSerializer.cpp:348
Gaudi::Utils::begin
AttribStringParser::Iterator begin(const AttribStringParser &parser)
Definition: AttribStringParser.h:136
StatusCode
Definition: StatusCode.h:65
Gaudi::cxx::for_each
void for_each(ContainerOfSynced &c, Fun &&f)
Definition: SynchronizedValue.h:105
ProduceConsume.j
j
Definition: ProduceConsume.py:104
std::cout
DataStoreItem
Definition: DataStoreItem.h:27
IOpaqueAddress.h
std::string::c_str
T c_str(T... args)
std::string::compare
T compare(T... args)
IOpaqueAddress::clID
virtual const CLID & clID() const =0
Retrieve class information from link.
CLID
unsigned int CLID
Class ID definition.
Definition: ClassID.h:18
IRegistry::setAddress
virtual void setAddress(IOpaqueAddress *pAddress)=0
Set/Update Opaque storage address.
TESSerializer.h
gaudirun.level
level
Definition: gaudirun.py:364
IRegistry.h
IOpaqueAddress::registry
virtual IRegistry * registry() const =0
Update branch name.
StatusCode::ignore
const StatusCode & ignore() const
Allow discarding a StatusCode without warning.
Definition: StatusCode.h:139
std::string::substr
T substr(T... args)
Gaudi::pushCurrentDataObject
GAUDI_API void pushCurrentDataObject(DataObject **pobjAddr)
IRegistry::address
virtual IOpaqueAddress * address() const =0
Retrieve opaque storage address.
StatusCode::isFailure
bool isFailure() const
Definition: StatusCode.h:129
std::vector::emplace_back
T emplace_back(T... args)
GaudiMP::TESSerializer::dumpBuffer
void dumpBuffer(TBufferFile &)
Dump TES contents listed in m_itemList/m_optItemList to a TBufferFile.
Definition: TESSerializer.cpp:70
std::endl
T endl(T... args)
GaudiMP::TESSerializer::analyse
bool analyse(IRegistry *dir, int level) override
Analysis callback.
Definition: TESSerializer.cpp:50
DataObject.h
std
STL namespace.
GenericAddress::par
const std::string * par() const override
Retrieve string parameters.
Definition: GenericAddress.h:89
GaudiPython.Bindings.getClass
def getClass(name, libs=[])
Definition: Bindings.py:204
IRegistry::object
virtual DataObject * object() const =0
Retrieve object behind the link.
DataObject
Definition: DataObject.h:36
IDataProviderSvc
Definition: IDataProviderSvc.h:53
IOTest.end
end
Definition: IOTest.py:125
GaudiMP::TESSerializer::addOptItem
void addOptItem(const std::string &path)
add an item to the TESSerializer's optional list (#notation)
Definition: TESSerializer.cpp:354
StatusCode::FAILURE
constexpr static const auto FAILURE
Definition: StatusCode.h:101
DataObject::linkMgr
LinkManager * linkMgr()
Retrieve Link manager.
Definition: DataObject.h:80
DataObject::registry
IRegistry * registry() const
Get pointer to Registry.
Definition: DataObject.h:78
GenericAddress::ipar
const unsigned long * ipar() const override
Retrieve integer parameters.
Definition: GenericAddress.h:91
IDataManagerSvc.h
std::string::rfind
T rfind(T... args)
GaudiMP::TESSerializer::TESSerializer
TESSerializer(IDataProviderSvc *svc, IAddressCreator *ac)
Constructor.
Definition: TESSerializer.cpp:61
MsgStream.h