| # 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
RConverter.cpp
Go to the documentation of this file.
1 // $Id: RConverter.cpp,v 1.13 2007/01/08 17:16:02 mato Exp $
2 #define ROOTHISTCNV_RCONVERTER_CPP
3
4 // Include files
5 #include "GaudiKernel/IDataProviderSvc.h"
6 #include "GaudiKernel/IDataManagerSvc.h"
7 #include "GaudiKernel/IRegistry.h"
8 #include "GaudiKernel/SmartIF.h"
9 #include "GaudiKernel/DataObject.h"
10 #include "GaudiKernel/MsgStream.h"
11 #include "GaudiKernel/xtoa.h"
12 #include "RConverter.h"
13 #include "RootObjAddress.h"
14
15 #include "TDirectory.h"
16 #include "TFile.h"
17 #include "TTree.h"
18 #include <string>
19 #include <list>
20
21 namespace {
22 std::map<std::string,TFile*> s_fileMap;
23 }
24
25 //-----------------------------------------------------------------------------
26 StatusCode RootHistCnv::RConverter::createDirectory(const std::string& loc)
27 //-----------------------------------------------------------------------------
28 {
29 MsgStream log(msgSvc(), "RConverter::createDir");
30
31 // Get rid of leading /NTUPLES
32 std::string full;
33 full = diskDirectory( loc );
34
35 int p,i;
36 std::string fil,cur,s;
37 TDirectory *gDir;
38
39 gDir = gDirectory;
40
41 TFile *tf;
42 if ( findTFile(loc,tf).isSuccess() ) {
43 tf->cd();
44 }
45
46 std::list<std::string> lpath;
47 i = 1;
48
49 if ( (p=full.find(":",0)) != -1 ) {
50 fil = full.substr(0,p);
51 i = p+1;
52 fil += ":/";
53 gDirectory->cd(fil.c_str());
54 }
55
56 while ( (p = full.find("/",i)) != -1) {
57 s = full.substr(i,p-i);
58 lpath.push_back(s);
59 i = p+1;
60 }
61 lpath.push_back( full.substr(i,full.length()-i) );
62
63 if ( full.substr(0,1) == "/") {
64 gDirectory->cd("/");
65 }
66
67 std::list<std::string>::const_iterator litr;
68 for(litr=lpath.begin(); litr!=lpath.end(); ++litr) {
69 cur = *litr;
70 if (! gDirectory->GetKey(litr->c_str()) ) {
71 gDirectory->mkdir(litr->c_str());
72 }
73 gDirectory->cd(litr->c_str());
74 }
75
76 gDirectory = gDir;
77
78 return StatusCode::SUCCESS;
79 }
80 //-----------------------------------------------------------------------------
81 std::string RootHistCnv::RConverter::diskDirectory(const std::string& loc)
82 //-----------------------------------------------------------------------------
83 {
84 // Get rid of leading /NTUPLES/{INPUT_STREAM} or /stat/{INPUT_STREAM}
85 std::string dir;
86 long lf1 = loc.find("/NTUPLES/");
87 long lf2 = loc.find("/stat/");
88 long ll;
89 if (lf1 != -1) {
90 ll = loc.find("/",lf1+9);
91
92 } else if (lf2 != -1) {
93 ll = loc.find("/",lf2+6);
94
95 } else {
96 MsgStream log(msgSvc(), "RConverter");
97 log << MSG::ERROR << "diskDirectory(" << loc << ")"
98 << " --> no leading /NTUPLES/ or /stat/" << endmsg;
99 return loc;
100 }
101 // dir = loc.substr(ll+8,loc.length()-ll-8);
102
103 if (ll == -1) {
104 dir = "/";
105 } else {
106 dir = loc.substr(ll,loc.length()-ll);
107 }
108
109 return dir;
110 }
111
112 //-----------------------------------------------------------------------------
113 std::string RootHistCnv::RConverter::directory(const std::string& loc)
114 //-----------------------------------------------------------------------------
115 {
116 return ( diskDirectory(loc) );
117 }
118
119 //-----------------------------------------------------------------------------
120 void RootHistCnv::RConverter::setDirectory(const std::string& loc)
121 //-----------------------------------------------------------------------------
122 {
123 MsgStream log(msgSvc(), "RConverter");
124 std::string full, id;
125 TFile *tf;
126
127 full = diskDirectory( loc );
128
129 // get associated TFile
130 if ( findTFile(loc,tf).isSuccess() ) {
131 tf->cd();
132 } else {
133 log << MSG::ERROR << "error getting TFile name " << loc << endmsg;
134 }
135
136 int p,i=1;
137 std::string cur,sdir;
138
139 gDirectory->cd("/");
140 while ( (p = full.find("/",i)) != -1) {
141 sdir = full.substr(i,p-i);
142 if (! gDirectory->GetKey(sdir.c_str()) ) {
143 log << MSG::ERROR << "cannot cd to " << full << " from "
144 << gDirectory->GetPath() << endmsg;
145 return;
146 }
147 gDirectory->cd(sdir.c_str());
148
149 i = p+1;
150 }
151 gDirectory->cd( full.substr(i,full.length()-i).c_str() );
152 }
153
154 //-----------------------------------------------------------------------------
155 void RootHistCnv::RConverter::setDiskDirectory(const std::string& loc)
156 //-----------------------------------------------------------------------------
157 {
158 setDirectory(loc);
159 }
160
161 //-----------------------------------------------------------------------------
162 std::string RootHistCnv::RConverter::getDirectory()
163 //-----------------------------------------------------------------------------
164 {
165 std::string dir = gDirectory->GetPath();
166 return (dir);
167 }
168
169
170 //-----------------------------------------------------------------------------
171 StatusCode RootHistCnv::RConverter::createAddress(DataObject* pObj,
172 TDirectory* pDir,
173 TObject* pTObj,
174 IOpaqueAddress*& refpAddr)
175 //-----------------------------------------------------------------------------
176 {
177 // Get address again....it does not change
178 IRegistry* pReg = pObj->registry();
179 if ( 0 != pReg ) {
180 refpAddr = pReg->address();
181 if ( 0 == refpAddr ) {
182 refpAddr = new RootObjAddress(repSvcType(),
183 objType(),
184 pReg->name(),
185 "",
186 (unsigned long)(pDir),
187 (unsigned long)(pTObj),
188 pTObj);
189
190 return StatusCode::SUCCESS;
191 }
192 }
193 return StatusCode::FAILURE;
194 }
195
196 //--------------------------------------------------------------------------
197 StatusCode RootHistCnv::RConverter::createAddress(const std::string& rzdir,
198 const CLID& clid,
199 const std::string& title,
200 TObject* pTObj,
201 IOpaqueAddress*& refpAddress)
202 //--------------------------------------------------------------------------
203 {
204 RootObjAddress* pA = new RootObjAddress(repSvcType(),
205 clid,
206 rzdir,
207 title,
208 0,
209 0,
210 pTObj );
211
212 refpAddress = pA;
213 return StatusCode::SUCCESS;
214 }
215
216
217 //--------------------------------------------------------------------------
218 StatusCode RootHistCnv::RConverter::createAddress(const std::string& rzdir,
219 const CLID& clid,
220 long id,
221 TObject* pTobj,
222 IOpaqueAddress*& refpAddress)
223 //--------------------------------------------------------------------------
224 {
225 std::ostringstream obj; obj << id;
226 StatusCode status = createAddress(rzdir, clid, obj.str(), pTobj, refpAddress);
227 if ( status.isSuccess() ) {
228 unsigned long* ipar = (unsigned long*)refpAddress->ipar();
229 ipar[0] = id;
230 }
231 return status;
232 }
233
234 //-----------------------------------------------------------------------------
236 TDirectory* RootHistCnv::RConverter::changeDirectory(DataObject* pObject)
237 //-----------------------------------------------------------------------------
238 {
239 if ( pObject ) {
240 IRegistry* pReg = pObject->registry();
241 if ( pReg ) {
242 SmartIF<IDataManagerSvc> dataMgr(dataProvider());
243 if ( dataMgr.isValid() ) {
244 IRegistry* pParentReg = 0;
245 StatusCode status = dataMgr->objectParent(pReg, pParentReg);
246 if ( status.isSuccess() ) {
247 IOpaqueAddress* pParAddr = pParentReg->address();
248 if ( pParAddr ) {
249 TDirectory* pParentDir = (TDirectory*)pParAddr->ipar()[0];
250 if ( pParentDir ) {
251 gDirectory->cd(pParentDir->GetPath());
252 return pParentDir;
253 }
254 }
255 }
256 }
257 }
258 }
259 return 0;
260 }
261
262 //-----------------------------------------------------------------------------
264 StatusCode RootHistCnv::RConverter::createRep(DataObject* pObject,
265 IOpaqueAddress*& pAddr)
266 //-----------------------------------------------------------------------------
267 {
268 GlobalDirectoryRestore restore;
269 pAddr = 0;
270 try {
271 TDirectory* pParentDir = changeDirectory(pObject);
272 if ( pParentDir ) {
273 TObject* pTObj = createPersistent(pObject);
274 if ( pTObj ) {
275 pTObj->Write();
276 delete pTObj;
277 return createAddress(pObject, pParentDir, 0, pAddr);
278 }
279 }
280 }
281 catch (...) {
282 }
283 MsgStream log (msgSvc(), "RConverter");
284 log << MSG::ERROR << "Failed to create persistent Object!" << endmsg;
285 return StatusCode::FAILURE;
286 }
287
288 //-----------------------------------------------------------------------------
289 StatusCode RootHistCnv::RConverter::readObject(IOpaqueAddress* /* pAddr */ ,
290 DataObject*& /* refpObj */ )
291 {
292 // MsgStream log(msgSvc(), "RConverter::readObject");
293 // log << MSG::WARNING << pAddr->par()[0] << " <> " << pAddr->par()[1]
294 // << " <> "
295 // << pAddr->ipar()[0] << " <> " << pAddr->ipar()[1] << " <> "
296 // << pAddr->registry()->identifier() << endmsg;
297
298 return StatusCode::SUCCESS;
299 }
300
301 //-----------------------------------------------------------------------------
302 TObject* RootHistCnv::RConverter::createPersistent(DataObject* /* pObj */)
303 {
304 return 0;
305 }
306
307
308 //-----------------------------------------------------------------------------
309 StatusCode RootHistCnv::RConverter::regTFile(const std::string id,
310 const TFile* tfile)
311 //-----------------------------------------------------------------------------
312 {
313
314 MsgStream log(msgSvc(), "RConverter");
315
316 std::map<std::string,TFile*>::const_iterator imap;
317 imap = s_fileMap.find(id);
318
319 if ( imap != s_fileMap.end() ) {
320 log << MSG::ERROR << "cannot register TTree " << id
321 << ": already exists" << endmsg;
322 return StatusCode::FAILURE;
323 }
324
325 s_fileMap[id] = const_cast<TFile*>(tfile);
326
327 return StatusCode::SUCCESS;
328 }
329
330 //-----------------------------------------------------------------------------
331 StatusCode RootHistCnv::RConverter::findTFile(const std::string id,
332 TFile*& tfile)
333 //-----------------------------------------------------------------------------
334 {
335 MsgStream log(msgSvc(), "RConverter");
336 tfile = 0;
337
338 std::string idm;
339
340 // make sure we only get first two parts of id
341 int i1,i2,i3;
342 i1 = id.find("/",0);
343 if (i1 != 0) {
344 log << MSG::ERROR << "Directory name does not start with \"/\": "
345 << id << endmsg;
346 return StatusCode::FAILURE;
347 }
348 i2 = id.find("/",i1+1);
349 if (i2 == -1) {
350 log << MSG::ERROR << "Directory name has only one part: " << id << endmsg;
351 return StatusCode::FAILURE;
352 }
353 i3 = id.find("/",i2+1);
354 if (i3 == -1) {
355 idm = id;
356 } else {
357 idm = id.substr(0,i3);
358 }
359
360 std::map<std::string,TFile*>::const_iterator imap;
361 imap = s_fileMap.find(idm);
362
363 if ( imap == s_fileMap.end() ) {
364 return StatusCode::FAILURE;
365 }
366 tfile = (*imap).second;
367 return StatusCode::SUCCESS;
368 }
369 //-----------------------------------------------------------------------------
370 std::string RootHistCnv::RConverter::convertId(const std::string& id ) const
371 //-----------------------------------------------------------------------------
372 {
373 bool forced = false;
374 if ( id.size() > 0 && isdigit(id[0]) ) {
375 try {
376 BooleanProperty tmp;
377 tmp.assign(SmartIF<IProperty>(conversionSvc())->getProperty( "ForceAlphaIds"));
378 forced = (bool)tmp;
379 }
380 catch ( ... ) { }
381 }
382 if (forced ) return std::string("h") + id;
383 else return id;
384 }
385 //-----------------------------------------------------------------------------
386 StatusCode RootHistCnv::RConverter::error(const std::string& msg)
387 //-----------------------------------------------------------------------------
388 {
389 MsgStream log(msgSvc(), "RootHistCnv");
390 log << MSG::ERROR << msg << endmsg;
391 return StatusCode::FAILURE;
392 }
393