| The Gaudi Framework v28r2-patches (bc4ad17a)

- Main Page
- Related Pages
- Modules
- Namespaces
- Classes
- Files

- File List
- File Members

TESSerializer.cpp
Go to the documentation of this file.
1 #include "GaudiMP/TESSerializer.h"
2
3 // Framework include files
4 #include "GaudiKernel/IRegistry.h"
5 #include "GaudiKernel/IDataManagerSvc.h"
6 #include "GaudiKernel/IDataProviderSvc.h"
7 #include "GaudiKernel/LinkManager.h"
8 #include "GaudiKernel/DataObject.h"
9 #include "GaudiKernel/DataStoreItem.h"
10 #include "GaudiKernel/System.h"
11 #include "GaudiKernel/GaudiException.h"
12
13
14 #include "GaudiKernel/IOpaqueAddress.h"
15 #include "GaudiKernel/GenericAddress.h"
16 #include "GaudiKernel/ClassID.h"
17 #include "GaudiKernel/KeyedContainer.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) {
35 Gaudi::pushCurrentDataObject(&p);
36 }
37 ~DataObjectPush() {
38 Gaudi::popCurrentDataObject();
39 }
40 };
41 }
42
43 using namespace std;
44
45 bool GaudiMP::TESSerializer::analyse(IRegistry* dir, int level) {
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
56 GaudiMP::TESSerializer::TESSerializer( IDataProviderSvc* svc, IAddressCreator* ac )
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
82 m_objects.erase(m_objects.begin(), m_objects.end());
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
374 void GaudiMP::TESSerializer::addItem(const std::string& path) {
375 // #notation supported
376 addItem( m_itemList, path );
377 }
378
380 void GaudiMP::TESSerializer::addOptItem(const std::string& path) {
381 // #notation supported
382 addItem( m_optItemList, path);
383 }
384
386 void GaudiMP::TESSerializer::checkItems( ) {
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
398 DataStoreItem*
399 GaudiMP::TESSerializer::findItem(const std::string& path) {
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
System.h
LinkManager::Link::path
const std::string & path() const
Access to path of object.
Definition: LinkManager.h:69
GaudiMP::TESSerializer::checkItems
void checkItems()
print out the contents of m_itemList and m_optItemList (std::cout)
Definition: TESSerializer.cpp:386
ClassID.h
GaudiException
Define general base for Gaudi exception.
Definition: GaudiException.h:23
IOpaqueAddress::clID
virtual const CLID & clID() const =0
Retrieve class information from link.
LinkManager.h
GaudiPython.HistoUtils.path
path
Definition: HistoUtils.py:772
StatusCode::FAILURE
Definition: StatusCode.h:29
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:299
GaudiMP::TESSerializer::loadBuffer
void loadBuffer(TBufferFile &)
Rebuild TES from items in a TBufferFile.
Definition: TESSerializer.cpp:192
IAddressCreator::createAddress
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::popCurrentDataObject
GAUDI_API void popCurrentDataObject()
Definition: RootIOHandler.cpp:38
StatusCode::isSuccess
bool isSuccess() const
Test for a status code of SUCCESS.
Definition: StatusCode.h:74
GaudiMP::TESSerializer::TESSerializer
TESSerializer(IDataProviderSvc *svc, IAddressCreator *ac)
Constructor.
Definition: TESSerializer.cpp:56
std::string::rfind
T rfind(T...args)
GaudiPython.HistoUtils.location
location
Definition: HistoUtils.py:774
std::endl
T endl(T...args)
IAddressCreator
IAddressCreator interface definition.
Definition: IAddressCreator.h:28
GaudiMP::TESSerializer::m_optItemList
Items m_optItemList
Vector of optional items to be saved to this stream (DataStoreItem ptrs)
Definition: TESSerializer.h:78
std
STL namespace.
GaudiMP::TESSerializer::m_verifyItems
bool m_verifyItems
Boolean Flag as used by GaudiSvc/PersistencySvc/OutputStreamer.
Definition: TESSerializer.h:87
std::vector::end
T end(T...args)
LinkManager::addLink
long addLink(const std::string &path, const DataObject *pObject) const
Add link by object reference and path.
Definition: LinkManager.cpp:58
GenericAddress
Generic Transient Address.
Definition: GenericAddress.h:20
IDataProviderSvc
Data provider interface definition.
Definition: IDataProviderSvc.h:45
IOpaqueAddress::par
virtual const std::string * par() const =0
Retrieve String parameters.
DataStoreItem
Description of the DataStoreItem class.
Definition: DataStoreItem.h:17
StatusCode::isFailure
bool isFailure() const
Test for a status code of FAILURE.
Definition: StatusCode.h:84
GaudiMP::TESSerializer::m_addressCreator
IAddressCreator * m_addressCreator
IAddress Creator for Opaque Addresses.
Definition: TESSerializer.h:91
DataObject::registry
IRegistry * registry() const
Get pointer to Registry.
Definition: DataObject.h:74
GaudiMP::TESSerializer::m_itemList
Items m_itemList
Vector of items to be saved to this stream (DataStoreItem ptrs)
Definition: TESSerializer.h:74
GaudiMP::TESSerializer::m_strict
bool m_strict
Boolean Flag used to determine error tolerance.
Definition: TESSerializer.h:89
DataObject::linkMgr
LinkManager * linkMgr() const
Retrieve Link manager.
Definition: DataObject.h:78
std::type_info
DataStoreItem::path
const std::string & path() const
Accessor: Retrieve load path.
Definition: DataStoreItem.h:65
std::string
STL class.
std::vector::push_back
T push_back(T...args)
DataStoreItem.h
IDataManagerSvc.h
compareOutputFiles.par
string par
Definition: compareOutputFiles.py:386
IDataProviderSvc.h
HiveAlgSequencer.p2
p2
Definition: HiveAlgSequencer.py:23
GaudiMP::TESSerializer::m_TESMgr
IDataManagerSvc * m_TESMgr
TES pointer.
Definition: TESSerializer.h:70
IDataProviderSvc::findObject
virtual StatusCode findObject(IRegistry *pDirectory, const std::string &path, DataObject *&pObject)=0
Find object identified by its directory entry.
GaudiMP::TESSerializer::analyse
bool analyse(IRegistry *dir, int level) override
Analysis callback.
Definition: TESSerializer.cpp:45
IRegistry::identifier
virtual const id_type & identifier() const =0
Full identifier (or key)
GaudiMP::TESSerializer::dumpBuffer
void dumpBuffer(TBufferFile &)
Dump TES contents listed in m_itemList/m_optItemList to a TBufferFile.
Definition: TESSerializer.cpp:67
StatusCode
This class is used for returning status codes from appropriate routines.
Definition: StatusCode.h:26
GenericAddress::ipar
const unsigned long * ipar() const override
Retrieve integer parameters.
Definition: GenericAddress.h:108
GenericAddress::par
const std::string * par() const override
Retrieve string parameters.
Definition: GenericAddress.h:104
GaudiException.h
std::vector::erase
T erase(T...args)
LinkManager::link
Link * link(long id)
Retrieve symbolic link identified by ID.
Definition: LinkManager.cpp:33
IRegistry.h
IRegistry
The IRegistry represents the entry door to the environment any data object residing in a transient da...
Definition: IRegistry.h:22
IOpaqueAddress::svcType
virtual long svcType() const =0
Retrieve service type.
GaudiMP::TESSerializer::addItem
void addItem(const std::string &path)
add an item to the TESSerializer&#39;s list (#notation)
Definition: TESSerializer.cpp:374
CLID
unsigned int CLID
Class ID definition.
Definition: ClassID.h:8
Gaudi::pushCurrentDataObject
GAUDI_API void pushCurrentDataObject(DataObject **pobjAddr)
Definition: RootIOHandler.cpp:34
GenericAddress::svcType
long svcType() const override
Access : retrieve the storage type of the class id.
Definition: GenericAddress.h:96
compareOutputFiles.cp
cp
Definition: compareOutputFiles.py:418
IDataManagerSvc::setRoot
virtual StatusCode setRoot(std::string root_name, DataObject *pObject)=0
Initialize data store for new event by giving new event path.
IRegistry::object
virtual DataObject * object() const =0
Retrieve object behind the link.
IOpaqueAddress.h
std::string::find
T find(T...args)
std::vector::size
T size(T...args)
std::vector< DataStoreItem * >
IDataManagerSvc
Definition: IDataManagerSvc.h:45
std::vector::begin
T begin(T...args)
TESSerializer.h
IRegistry::setAddress
virtual void setAddress(IOpaqueAddress *pAddress)=0
Set/Update Opaque storage address.
GaudiMP::TESSerializer::m_objects
Objects m_objects
Selected list of Objects to be serialized (DataObject ptrs)
Definition: TESSerializer.h:82
std::string::c_str
T c_str(T...args)
std::string::substr
T substr(T...args)
gaudirun.level
level
Definition: gaudirun.py:261
GaudiMP::TESSerializer::addOptItem
void addOptItem(const std::string &path)
add an item to the TESSerializer&#39;s optional list (#notation)
Definition: TESSerializer.cpp:380
GaudiMP::TESSerializer::findItem
DataStoreItem * findItem(const std::string &path)
Find single item identified by its path (exact match)
Definition: TESSerializer.cpp:399
IOpaqueAddress
Opaque address interface definition.
Definition: IOpaqueAddress.h:23
IDataProviderSvc::registerObject
virtual StatusCode registerObject(const std::string &fullPath, DataObject *pObject)=0
Register object with the data store.
GaudiMP::TESSerializer::m_currentItem
DataStoreItem * m_currentItem
Current item while traversing the TES tree.
Definition: TESSerializer.h:80
IRegistry::address
virtual IOpaqueAddress * address() const =0
Retrieve opaque storage address.
GenericAddress::clID
const CLID & clID() const override
Access : Retrieve class ID of the link.
Definition: GenericAddress.h:88
IDataManagerSvc::traverseSubTree
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...
SERIALIZER_END
#define SERIALIZER_END
Definition: TESSerializer.cpp:30
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
GaudiMP::TESSerializer::m_TES
IDataProviderSvc * m_TES
TES pointer.
Definition: TESSerializer.h:68
std::string::compare
T compare(T...args)
DataObject.h
LinkManager::size
long size() const
Retrieve number of link present.
Definition: LinkManager.h:112
GaudiMP::TESSerializer::m_classMap
std::map< std::string, TClass * > m_classMap
Map of gROOT class information.
Definition: TESSerializer.h:85
IDataProviderSvc::retrieveObject
virtual StatusCode retrieveObject(IRegistry *pDirectory, const std::string &path, DataObject *&pObject)=0
Retrieve object identified by its directory entry.