All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
RNTupleCnv.cpp
Go to the documentation of this file.
1 // Include files
2 #include "NTupleInfo.h"
3 #include "RNTupleCnv.h"
4 #include "RootObjAddress.h"
5 
10 #include "GaudiKernel/INTupleSvc.h"
11 #include "GaudiKernel/IRegistry.h"
12 #include "GaudiKernel/MsgStream.h"
13 #include "GaudiKernel/SmartIF.h"
14 #include "GaudiKernel/NTuple.h"
15 
16 // Root
17 #include "TTree.h"
18 
19 #ifdef __ICC
20 // disable icc remark #1572: floating-point equality and inequality comparisons are unreliable
21 // they are intended
22 #pragma warning(disable:1572)
23 #endif
24 
25 //-----------------------------------------------------------------------------
26 //
27 // Implementation of class : RootHistCnv::RNTupleCnv
28 //
29 // Author : Charles Leggett
30 //
31 //-----------------------------------------------------------------------------
32 
33 //-----------------------------------------------------------------------------
36  : RConverter(clid, svc) {
37 }
38 //-----------------------------------------------------------------------------
39 
40 
41 //-----------------------------------------------------------------------------
44 }
45 //-----------------------------------------------------------------------------
46 
47 //-----------------------------------------------------------------------------
50  //-----------------------------------------------------------------------------
52  if ( status.isSuccess() ) {
53  m_ntupleSvc = serviceLocator()->service("NTupleSvc");
54  if (!m_ntupleSvc.isValid()) status = StatusCode::FAILURE;
55  }
56  return status;
57 }
58 
59 
60 //-----------------------------------------------------------------------------
63  //-----------------------------------------------------------------------------
65  m_ntupleSvc = 0; // release
66  return Converter::finalize();
67 }
68 
69 //-----------------------------------------------------------------------------
72  DataObject* pObject)
73 //-----------------------------------------------------------------------------
74 {
75  MsgStream log (msgSvc(), "RNTupleCnv");
76 
78 
79  RootObjAddress *rAddr = dynamic_cast<RootObjAddress*>(pAddress);
80 
81  if (rAddr == 0) {
82  log << MSG::ERROR << "Could not dynamic cast to RootObjAddress" << endmsg;
83  return StatusCode::FAILURE;
84  }
85 
86  TTree* rtree = (TTree*) rAddr->tObj();
87 
88  try {
89  unsigned long* info = (unsigned long*)pAddress->ipar();
90  setDirectory(pAddress->par()[0]);
91  status = readData(rtree, dynamic_cast<INTuple*>(pObject), info[1]++);
92  }
93  catch (...) {
94  }
95  return status;
96 }
97 
98 //-----------------------------------------------------------------------------
101  DataObject*& refpObject)
102 //-----------------------------------------------------------------------------
103 {
104  MsgStream log (msgSvc(), "RNTupleCnv");
105 
106  IRegistry* pReg = pAddress->registry();
107 
108  // log << MSG::WARNING << "adr: " << pAddress->par()[0] << " <> "
109  // << pAddress->par()[1] << " <> " << pAddress->ipar()[0] << " <> "
110  // << pAddress->ipar()[1] << " <> " << hex << rAddr->tObj()
111  // << dec << " <> " << pReg->identifier() << endmsg;
112 
113  std::string ident = pReg->identifier();
114 
115  StatusCode status = readObject(pAddress, refpObject); // Doesn't do anything
116  if ( status.isSuccess() ) {
117  RootObjAddress *rAddr = dynamic_cast<RootObjAddress*>( pAddress );
118  if (rAddr == 0) {
119  log << MSG::ERROR << "Could not cast to RootObjAddress" << endmsg;
120  return StatusCode::FAILURE;
121  }
122  INTuple* nt = 0;
123  TTree* tobj = (TTree*) rAddr->tObj();
124  status = load(tobj, nt);
125  if (status.isSuccess()) {
126  refpObject = dynamic_cast<DataObject*>(nt);
127  } else {
128  log << MSG::ERROR << "Problems loading ntuple id: " << pReg->identifier()
129  << endmsg;
130  }
131  }
132  return status;
133 }
134 
135 //-----------------------------------------------------------------------------
138  IOpaqueAddress*& pAddr)
139 //-----------------------------------------------------------------------------
140 {
141  GlobalDirectoryRestore restore;
142  pAddr = 0;
143  try {
144  IRegistry* pReg = pObject->registry();
145  if ( 0 != pReg ) {
146  pAddr = pReg->address();
147  if ( 0 == pAddr ) {
148  SmartIF<IDataManagerSvc> dataMgr(dataProvider());
149  if ( dataMgr.isValid() ) {
150  IRegistry* pParentReg = 0;
151  StatusCode status = dataMgr->objectParent(pReg, pParentReg);
152  if ( status.isSuccess() ) {
153  IOpaqueAddress* pParAddr = pParentReg->address();
154  if ( pParAddr ) {
155  TDirectory* pParentDir = (TDirectory*)pParAddr->ipar()[0];
156  if ( pParentDir ) {
157  TTree* pTree = 0;
158  std::string dsc = pReg->name().substr(1);
159  gDirectory = pParentDir;
160  status = book(dsc, dynamic_cast<INTuple*>(pObject), pTree);
161  if ( !status.isSuccess() ) {
162  return status;
163  }
164  status = createAddress(pObject, gDirectory, pTree, pAddr);
165  if ( !status.isSuccess() ) {
166  return status;
167  }
168  return writeData(pTree, dynamic_cast<INTuple*>(pObject));
169  }
170  }
171  }
172  }
173  }
174  else {
175  TDirectory* pDir = (TDirectory*)pAddr->ipar()[0];
176  RootObjAddress *rAddr = dynamic_cast<RootObjAddress*>( pAddr );
177  if (rAddr == 0) {
178  MsgStream log (msgSvc(), "RNTupleCnv");
179  log << MSG::ERROR << "Could not cast to RootObjAddress" << endmsg;
180  return StatusCode::FAILURE;
181  }
182  TTree* pTree = (TTree*) rAddr->tObj();
183  gDirectory = pDir;
184  return writeData(pTree, dynamic_cast<INTuple*>(pObject));
185  }
186  }
187  }
188  catch (...) {
189  }
190  MsgStream log (msgSvc(), "RNTupleCnv");
191  log << MSG::ERROR << "Failed to create persistent N-tuple!" << endmsg;
192  return StatusCode::FAILURE;
193 }
194 
195 //-----------------------------------------------------------------------------
198  DataObject* pObj )
199 //-----------------------------------------------------------------------------
200 {
201  MsgStream log (msgSvc(), "RNTupleCnv");
202  if ( 0 != pAddr ) {
203  GlobalDirectoryRestore restore;
204  TDirectory* pDir = (TDirectory*)pAddr->ipar()[0];
205  RootObjAddress *rAddr = dynamic_cast<RootObjAddress*>( pAddr );
206  if (rAddr == 0) {
207  log << MSG::ERROR << "Could not cast to RootObjAddress" << endmsg;
208  return StatusCode::FAILURE;
209  }
210  TTree* pTree = (TTree*) rAddr->tObj();
211  if ( 0 != pDir && 0 != pTree ) {
212  gDirectory->cd(pDir->GetPath());
213  pTree->Write("",TObject::kOverwrite);
214  return StatusCode::SUCCESS;
215  }
216  }
217  else {
218  log << MSG::WARNING << "empty ntuple: " << pObj->registry()->identifier()
219  << endmsg;
220  return ( createRep(pObj,pAddr) );
221  }
222  return StatusCode::FAILURE;
223 }
224 
225 
227 
228 //-----------------------------------------------------------------------------
230  //-----------------------------------------------------------------------------
231  switch( type ) {
232  case DataTypeInfo::BOOL: return "/O"; // bool
233  case DataTypeInfo::SHORT: return "/S"; // short
234  case DataTypeInfo::INT: return "/I"; // int
235  case DataTypeInfo::LONG: return "/I"; // long
236  case DataTypeInfo::LONGLONG: return "/L"; // longlong
237  case DataTypeInfo::USHORT: return "/s"; // ushort
238  case DataTypeInfo::UINT: return "/i"; // uint
239  case DataTypeInfo::ULONG: return "/i"; // ulong
240  case DataTypeInfo::ULONGLONG: return "/l"; // ulonglong
241  case DataTypeInfo::FLOAT: return "/F"; // float
242  case DataTypeInfo::DOUBLE: return "/D"; // double
243  case DataTypeInfo::CHAR: return "/B"; // char
244  case DataTypeInfo::UCHAR: return "/b"; // char
245  default: return "";
246  }
247  // cannot reach this point
248 }
249 
250 //-----------------------------------------------------------------------------
251 bool RootHistCnv::parseName(std::string full, std::string &blk, std::string &var) {
252  //-----------------------------------------------------------------------------
253  int sp;
254  if ( (sp=full.find("/")) != -1 ) {
255  blk = full.substr(0,sp);
256  var = full.substr(sp+1,full.length());
257  return true;
258  } else {
259  blk = "AUTO_BLK";
260  var = full;
261  return false;
262  }
263 
264 }
265 
266 //-----------------------------------------------------------------------------
268 
269 #define INSTANTIATE(TYP) \
270  template INTupleItem* createNTupleItem<TYP>(std::string itemName, std::string blockName, std::string index_name, int indexRange, int arraySize, TYP minimum, TYP maximum, INTuple* tuple)
271 
272 namespace RootHistCnv {
273 
274  template<class TYP>
275  INTupleItem* createNTupleItem (std::string itemName, std::string blockName,
276  std::string indexName,
277  int indexRange, int arraySize,
278  TYP min, TYP max,
279  INTuple* ntup) {
280 
281  std::string varName;
282  if (blockName != "") {
283  varName = blockName + "/" + itemName;
284  } else {
285  varName = itemName;
286  }
287 
288  TYP null = 0;
289  INTupleItem* col = 0;
290 
291  if (min == 0 && max == 0) {
292  min = NTuple::Range<TYP>::min();
293  max = NTuple::Range<TYP>::max();
294  }
295 
296 
297  if (indexName == "") {
298 
299  if (arraySize == 1) {
300  // simple items
301  col = NTuple::_Item<TYP>::create(ntup, varName,
302  typeid(TYP),
303  min, max, null);
304 
305  } else {
306  // Arrays of fixed size
307  col = NTuple::_Array<TYP>::create(ntup, varName,
308  typeid(TYP),
309  "", arraySize,
310  min, max, null);
311  }
312 
313  } else {
314 
315  if (arraySize == 1) {
316  // Arrays of variable size
317  col = NTuple::_Array<TYP>::create(ntup, varName,
318  typeid(TYP),
319  indexName, indexRange,
320  min, max, null);
321  } else {
322  // Matrices
323  col = NTuple::_Matrix<TYP>::create(ntup, varName,
324  typeid(TYP),
325  indexName,
326  indexRange, arraySize,
327  min, max, null);
328  }
329 
330  }
331 
332  return col;
333 
334  }
335 
336  INSTANTIATE(float);
337  INSTANTIATE(double);
338  INSTANTIATE(bool);
339  INSTANTIATE(char);
340  INSTANTIATE(int);
341  INSTANTIATE(short);
342  INSTANTIATE(long);
343  INSTANTIATE(long long);
344  INSTANTIATE(unsigned char);
345  INSTANTIATE(unsigned int);
346  INSTANTIATE(unsigned short);
347  INSTANTIATE(unsigned long);
348  INSTANTIATE(unsigned long long);
349 
350 }
virtual StatusCode updateRep(IOpaqueAddress *pAddr, DataObject *pObj)
Update the converted representation of a transient object.
Definition: RNTupleCnv.cpp:197
Root Converter.
Definition: RConverter.h:34
virtual const std::string * par() const =0
Retrieve String parameters.
Definition of the MsgStream class used to transmit messages.
Definition: MsgStream.h:24
The ISvcLocator is the interface implemented by the Service Factory in the Application Manager to loc...
Definition: ISvcLocator.h:26
virtual StatusCode finalize()
Initialize the converter.
Definition: Converter.cpp:76
GAUDI_API AIDA::IHistogram1D * book(IHistogramSvc *svc, const std::string &path, const Gaudi::Histo1DDef &hist)
helper function to book 1D-histogram
Definition: HistoDef.cpp:132
RNTupleCnv(ISvcLocator *svc, const CLID &clid)
Standard constructor.
Definition: RNTupleCnv.cpp:35
virtual StatusCode initialize()
Initialize the converter.
Definition: Converter.cpp:67
bool isSuccess() const
Test for a status code of SUCCESS.
Definition: StatusCode.h:62
static TYP min()
Minimal number of data.
Definition: NTuple.h:77
virtual const name_type & name() const =0
Name of the directory (or key)
IRegistry * registry() const
Get pointer to Registry.
Definition: DataObject.h:69
static _Array * create(INTuple *tup, const std::string &name, const std::type_info &info, const std::string &index, long len, TYP min, TYP max, TYP def)
Create instance.
Definition: NTupleItems.cpp:34
virtual StatusCode finalize()
Finalize the converter.
Definition: RNTupleCnv.cpp:62
NTuple interface class definition.
Definition: INTuple.h:80
virtual StatusCode initialize()
Initialize the converter.
Definition: RNTupleCnv.cpp:49
NTuple interface class definition.
Definition: INTuple.h:27
string type
Definition: gaudirun.py:126
bool isValid() const
Allow for check if smart pointer is valid.
Definition: SmartIF.h:51
virtual IRegistry * registry() const =0
Update branch name.
This class is used for returning status codes from appropriate routines.
Definition: StatusCode.h:30
virtual TObject * tObj() const
Retrieve TObject* ptr.
static TYP max()
Maximal number of data.
Definition: NTuple.h:79
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 StatusCode createRep(DataObject *pObj, IOpaqueAddress *&refpAddr)
Convert the transient object to the requested representation.
Definition: RNTupleCnv.cpp:137
#define min(a, b)
INSTANTIATE(float)
virtual ~RNTupleCnv()
Standard destructor.
Definition: RNTupleCnv.cpp:43
virtual const id_type & identifier() const =0
Full identifier (or key)
static _Item * create(INTuple *tup, const std::string &name, const std::type_info &info, TYP min, TYP max, TYP def)
Create instance.
Definition: NTupleItems.cpp:27
Opaque address interface definition.
A DataObject is the base class of any identifiable object on any data store.
Definition: DataObject.h:31
bool parseName(std::string full, std::string &blk, std::string &var)
Definition: RNTupleCnv.cpp:251
virtual const unsigned long * ipar() const =0
Access to generic link parameters.
virtual StatusCode createObj(IOpaqueAddress *pAddr, DataObject *&refpObj)
Create the transient representation of an object.
Definition: RNTupleCnv.cpp:100
MsgStream & endmsg(MsgStream &s)
MsgStream Modifier: endmsg. Calls the output method of the MsgStream.
Definition: MsgStream.h:243
virtual StatusCode updateObj(IOpaqueAddress *pAddr, DataObject *refpObj)
Update the transient object from the other representation.
Definition: RNTupleCnv.cpp:71
static _Matrix * create(INTuple *tup, const std::string &name, const std::type_info &info, const std::string &index, long ncol, long nrow, TYP min, TYP max, TYP def)
Create instance.
Definition: NTupleItems.cpp:41
INTupleItem * createNTupleItem(std::string itemName, std::string blockName, std::string indexName, int indexRange, int arraySize, TYP min, TYP max, INTuple *ntup)
Definition: RNTupleCnv.cpp:275
virtual std::string rootVarType(int)
Return ROOT type info:
Definition: RNTupleCnv.cpp:229