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