Loading [MathJax]/extensions/tex2jax.js
The Gaudi Framework  v31r0 (aeb156f0)
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Modules Pages
TESSerializer.cpp
Go to the documentation of this file.
2 
3 // Framework include files
11 #include "GaudiKernel/System.h"
12 
13 #include "GaudiKernel/ClassID.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 
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 
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
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 
360  // #notation supported
361  addItem( m_itemList, path );
362 }
363 
366  // #notation supported
367  addItem( m_optItemList, path );
368 }
369 
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 
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 }
void checkItems()
print out the contents of m_itemList and m_optItemList (std::cout)
Define general base for Gaudi exception.
virtual const CLID & clID() const =0
Retrieve class information from link.
GAUDI_API const std::string typeinfoName(const std::type_info &)
Get platform independent information about the class type.
Definition: System.cpp:309
void loadBuffer(TBufferFile &)
Rebuild TES from items in a TBufferFile.
virtual StatusCode createAddress(long svc_type, const CLID &clid, const std::string *par, const unsigned long *ipar, IOpaqueAddress *&refpAddress)=0
Create a Generic address using explicit arguments to identify a single object.
GAUDI_API void popCurrentDataObject()
virtual StatusCode findObject(IRegistry *pDirectory, boost::string_ref path, DataObject *&pObject)=0
Find object identified by its directory entry.
bool isSuccess() const
Definition: StatusCode.h:267
virtual StatusCode setRoot(std::string root_name, DataObject *pObject)=0
Initialize data store for new event by giving new event path.
TESSerializer(IDataProviderSvc *svc, IAddressCreator *ac)
Constructor.
T rfind(T...args)
T endl(T...args)
IAddressCreator interface definition.
Items m_optItemList
Vector of optional items to be saved to this stream (DataStoreItem ptrs)
Definition: TESSerializer.h:79
STL namespace.
bool m_verifyItems
Boolean Flag as used by GaudiSvc/PersistencySvc/OutputStreamer.
Definition: TESSerializer.h:88
T end(T...args)
Generic Transient Address.
Data provider interface definition.
virtual const std::string * par() const =0
Retrieve String parameters.
Description of the DataStoreItem class.
Definition: DataStoreItem.h:17
bool isFailure() const
Definition: StatusCode.h:130
IAddressCreator * m_addressCreator
IAddress Creator for Opaque Addresses.
Definition: TESSerializer.h:92
IRegistry * registry() const
Get pointer to Registry.
Definition: DataObject.h:72
virtual StatusCode traverseSubTree(boost::string_ref sub_tree_path, IDataStoreAgent *pAgent)=0
Analyse by traversing all data objects below the sub tree identified by its full path name...
Items m_itemList
Vector of items to be saved to this stream (DataStoreItem ptrs)
Definition: TESSerializer.h:75
bool m_strict
Boolean Flag used to determine error tolerance.
Definition: TESSerializer.h:90
LinkManager * linkMgr() const
Retrieve Link manager.
Definition: DataObject.h:74
const std::string & path() const
Accessor: Retrieve load path.
Definition: DataStoreItem.h:55
STL class.
T push_back(T...args)
IDataManagerSvc * m_TESMgr
TES pointer.
Definition: TESSerializer.h:71
bool analyse(IRegistry *dir, int level) override
Analysis callback.
virtual const id_type & identifier() const =0
Full identifier (or key)
void dumpBuffer(TBufferFile &)
Dump TES contents listed in m_itemList/m_optItemList to a TBufferFile.
This class is used for returning status codes from appropriate routines.
Definition: StatusCode.h:50
const unsigned long * ipar() const override
Retrieve integer parameters.
const std::string * par() const override
Retrieve string parameters.
T erase(T...args)
The IRegistry represents the entry door to the environment any data object residing in a transient da...
Definition: IRegistry.h:22
virtual long svcType() const =0
Retrieve service type.
void addItem(const std::string &path)
add an item to the TESSerializer&#39;s list (#notation)
unsigned int CLID
Class ID definition.
Definition: ClassID.h:8
virtual StatusCode retrieveObject(IRegistry *pDirectory, boost::string_ref path, DataObject *&pObject)=0
Retrieve object identified by its directory entry.
GAUDI_API void pushCurrentDataObject(DataObject **pobjAddr)
long svcType() const override
Access : retrieve the storage type of the class id.
virtual DataObject * object() const =0
Retrieve object behind the link.
T find(T...args)
T size(T...args)
StatusCode registerObject(boost::string_ref fullPath, DataObject *pObject)
Register object with the data store.
T begin(T...args)
virtual void setAddress(IOpaqueAddress *pAddress)=0
Set/Update Opaque storage address.
Objects m_objects
Selected list of Objects to be serialized (DataObject ptrs)
Definition: TESSerializer.h:83
T c_str(T...args)
constexpr static const auto FAILURE
Definition: StatusCode.h:86
T substr(T...args)
void addOptItem(const std::string &path)
add an item to the TESSerializer&#39;s optional list (#notation)
DataStoreItem * findItem(const std::string &path)
Find single item identified by its path (exact match)
Opaque address interface definition.
DataStoreItem * m_currentItem
Current item while traversing the TES tree.
Definition: TESSerializer.h:81
virtual IOpaqueAddress * address() const =0
Retrieve opaque storage address.
const CLID & clID() const override
Access : Retrieve class ID of the link.
#define SERIALIZER_END
A DataObject is the base class of any identifiable object on any data store.
Definition: DataObject.h:30
T stoi(T...args)
IDataProviderSvc * m_TES
TES pointer.
Definition: TESSerializer.h:69
T compare(T...args)
std::map< std::string, TClass * > m_classMap
Map of gROOT class information.
Definition: TESSerializer.h:86