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
8 #include "GaudiKernel/SmartIF.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 //-----------------------------------------------------------------------------
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 //-----------------------------------------------------------------------------
163 //-----------------------------------------------------------------------------
164 {
165  std::string dir = gDirectory->GetPath();
166  return (dir);
167 }
168 
169 
170 //-----------------------------------------------------------------------------
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 //--------------------------------------------------------------------------
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 //--------------------------------------------------------------------------
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 //-----------------------------------------------------------------------------
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 //-----------------------------------------------------------------------------
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 //-----------------------------------------------------------------------------
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 //-----------------------------------------------------------------------------
303 {
304  return 0;
305 }
306 
307 
308 //-----------------------------------------------------------------------------
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 //-----------------------------------------------------------------------------
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 //-----------------------------------------------------------------------------
387 //-----------------------------------------------------------------------------
388 {
389  MsgStream log(msgSvc(), "RootHistCnv");
390  log << MSG::ERROR << msg << endmsg;
391  return StatusCode::FAILURE;
392 }
393 
std::string getDirectory()
Definition: RConverter.cpp:162
Definition of the MsgStream class used to transmit messages.
Definition: MsgStream.h:24
std::string diskDirectory(const std::string &loc)
Definition: RConverter.cpp:81
void setDirectory(const std::string &loc)
Definition: RConverter.cpp:120
bool isSuccess() const
Test for a status code of SUCCESS.
Definition: StatusCode.h:62
SmartIF< IMessageSvc > & msgSvc() const
Retrieve pointer to message service.
Definition: Converter.cpp:131
StatusCode regTFile(const std::string, const TFile *)
Definition: RConverter.cpp:309
virtual const name_type & name() const =0
Name of the directory (or key)
StatusCode findTFile(const std::string, TFile *&)
Definition: RConverter.cpp:331
GAUDI_API Property * getProperty(const IProperty *p, const std::string &name)
simple function which gets the property with given name from the component
Definition: Property.cpp:349
StatusCode createDirectory(const std::string &loc)
Definition: RConverter.cpp:26
IRegistry * registry() const
Get pointer to Registry.
Definition: DataObject.h:69
TDirectory * changeDirectory(DataObject *pObject)
Switch to object directory (=Parent directory)
Definition: RConverter.cpp:236
bool isValid() const
Allow for check if smart pointer is valid.
Definition: SmartIF.h:51
virtual TObject * createPersistent(DataObject *pObj)
Create the persistent representation of an object.
Definition: RConverter.cpp:302
This class is used for returning status codes from appropriate routines.
Definition: StatusCode.h:30
The IRegistry represents the entry door to the environment any data object residing in a transient da...
Definition: IRegistry.h:22
unsigned int CLID
Class ID definition.
Definition: ClassID.h:9
virtual IOpaqueAddress * address() const =0
Retrieve opaque storage address.
virtual bool assign(const Property &source)
get the value from another property
Definition: Property.h:283
std::string directory(const std::string &loc)
Definition: RConverter.cpp:113
StatusCode error(const std::string &msg)
Definition: RConverter.cpp:386
string s
Definition: gaudirun.py:210
std::string convertId(const std::string &) const
Definition: RConverter.cpp:370
virtual StatusCode readObject(IOpaqueAddress *pAddr, DataObject *&refpObj)
Create the transient representation of an object.
Definition: RConverter.cpp:289
virtual StatusCode createRep(DataObject *pObj, IOpaqueAddress *&refpAddr)
Convert the transient object to the requested representation.
Definition: RConverter.cpp:264
Opaque address interface definition.
A DataObject is the base class of any identifiable object on any data store.
Definition: DataObject.h:31
list i
Definition: ana.py:128
virtual const unsigned long * ipar() const =0
Access to generic link parameters.
MsgStream & endmsg(MsgStream &s)
MsgStream Modifier: endmsg. Calls the output method of the MsgStream.
Definition: MsgStream.h:243
StatusCode createAddress(DataObject *pObject, TDirectory *pDir, TObject *pTObject, IOpaqueAddress *&refpAddr)
Create address of the transient object according to the requested representation. ...
Definition: RConverter.cpp:171
void setDiskDirectory(const std::string &loc)
Definition: RConverter.cpp:155