| # Gaudi Framework, version v25r0 |

Home |  | Generated: Mon Feb 17 2014

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

- File List
- File Members

All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
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 = atoi(slevel.c_str());
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