Gaudi Framework, version v24r2

Home   Generated: Wed Dec 4 2013
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
RNTupleCnv.cpp
Go to the documentation of this file.
1 #define ROOTHISTCNV_RNTUPLECNV_CPP
2 
3 // Include files
4 #include "NTupleInfo.h"
5 #include "RNTupleCnv.h"
6 #include "RootObjAddress.h"
7 
13 #include "GaudiKernel/INTupleSvc.h"
14 #include "GaudiKernel/IRegistry.h"
15 #include "GaudiKernel/MsgStream.h"
16 #include "GaudiKernel/SmartIF.h"
17 #include "GaudiKernel/NTuple.h"
18 
19 // Root
20 #include "TTree.h"
21 
22 #ifdef __ICC
23 // disable icc remark #1572: floating-point equality and inequality comparisons are unreliable
24 // they are intended
25 #pragma warning(disable:1572)
26 #endif
27 
28 //-----------------------------------------------------------------------------
29 //
30 // Implementation of class : RootHistCnv::RNTupleCnv
31 //
32 // Author : Charles Leggett
33 //
34 //-----------------------------------------------------------------------------
35 
36 //-----------------------------------------------------------------------------
39  : RConverter(clid, svc) {
40 }
41 //-----------------------------------------------------------------------------
42 
43 
44 //-----------------------------------------------------------------------------
47 }
48 //-----------------------------------------------------------------------------
49 
50 //-----------------------------------------------------------------------------
53  //-----------------------------------------------------------------------------
55  if ( status.isSuccess() ) {
56  m_ntupleSvc = serviceLocator()->service("NTupleSvc");
57  if (!m_ntupleSvc.isValid()) status = StatusCode::FAILURE;
58  }
59  return status;
60 }
61 
62 
63 //-----------------------------------------------------------------------------
66  //-----------------------------------------------------------------------------
68  m_ntupleSvc = 0; // release
69  return Converter::finalize();
70 }
71 
72 //-----------------------------------------------------------------------------
75  DataObject* pObject)
76 //-----------------------------------------------------------------------------
77 {
78  MsgStream log (msgSvc(), "RNTupleCnv");
79 
81 
82  RootObjAddress *rAddr = dynamic_cast<RootObjAddress*>(pAddress);
83 
84  if (rAddr == 0) {
85  log << MSG::ERROR << "Could not dynamic cast to RootObjAddress" << endmsg;
86  return StatusCode::FAILURE;
87  }
88 
89  TTree* rtree = (TTree*) rAddr->tObj();
90 
91  try {
92  unsigned long* info = (unsigned long*)pAddress->ipar();
93  setDirectory(pAddress->par()[0]);
94  status = readData(rtree, dynamic_cast<INTuple*>(pObject), info[1]++);
95  }
96  catch (...) {
97  }
98  return status;
99 }
100 
101 //-----------------------------------------------------------------------------
104  DataObject*& refpObject)
105 //-----------------------------------------------------------------------------
106 {
107  MsgStream log (msgSvc(), "RNTupleCnv");
108 
109  IRegistry* pReg = pAddress->registry();
110 
111  // log << MSG::WARNING << "adr: " << pAddress->par()[0] << " <> "
112  // << pAddress->par()[1] << " <> " << pAddress->ipar()[0] << " <> "
113  // << pAddress->ipar()[1] << " <> " << hex << rAddr->tObj()
114  // << dec << " <> " << pReg->identifier() << endmsg;
115 
116  std::string ident = pReg->identifier();
117 
118  StatusCode status = readObject(pAddress, refpObject); // Doesn't do anything
119  if ( status.isSuccess() ) {
120  RootObjAddress *rAddr = dynamic_cast<RootObjAddress*>( pAddress );
121  if (rAddr == 0) {
122  log << MSG::ERROR << "Could not cast to RootObjAddress" << endmsg;
123  return StatusCode::FAILURE;
124  }
125  INTuple* nt = 0;
126  TTree* tobj = (TTree*) rAddr->tObj();
127  status = load(tobj, nt);
128  if (status.isSuccess()) {
129  refpObject = dynamic_cast<DataObject*>(nt);
130  } else {
131  log << MSG::ERROR << "Problems loading ntuple id: " << pReg->identifier()
132  << endmsg;
133  }
134  }
135  return status;
136 }
137 
138 //-----------------------------------------------------------------------------
141  IOpaqueAddress*& pAddr)
142 //-----------------------------------------------------------------------------
143 {
144  GlobalDirectoryRestore restore;
145  pAddr = 0;
146  try {
147  IRegistry* pReg = pObject->registry();
148  if ( 0 != pReg ) {
149  pAddr = pReg->address();
150  if ( 0 == pAddr ) {
151  SmartIF<IDataManagerSvc> dataMgr(dataProvider());
152  if ( dataMgr.isValid() ) {
153  IRegistry* pParentReg = 0;
154  StatusCode status = dataMgr->objectParent(pReg, pParentReg);
155  if ( status.isSuccess() ) {
156  IOpaqueAddress* pParAddr = pParentReg->address();
157  if ( pParAddr ) {
158  TDirectory* pParentDir = (TDirectory*)pParAddr->ipar()[0];
159  if ( pParentDir ) {
160  TTree* pTree = 0;
161  std::string dsc = pReg->name().substr(1);
162  gDirectory = pParentDir;
163  status = book(dsc, dynamic_cast<INTuple*>(pObject), pTree);
164  if ( !status.isSuccess() ) {
165  return status;
166  }
167  status = createAddress(pObject, gDirectory, pTree, pAddr);
168  if ( !status.isSuccess() ) {
169  return status;
170  }
171  return writeData(pTree, dynamic_cast<INTuple*>(pObject));
172  }
173  }
174  }
175  }
176  }
177  else {
178  TDirectory* pDir = (TDirectory*)pAddr->ipar()[0];
179  RootObjAddress *rAddr = dynamic_cast<RootObjAddress*>( pAddr );
180  if (rAddr == 0) {
181  MsgStream log (msgSvc(), "RNTupleCnv");
182  log << MSG::ERROR << "Could not cast to RootObjAddress" << endmsg;
183  return StatusCode::FAILURE;
184  }
185  TTree* pTree = (TTree*) rAddr->tObj();
186  gDirectory = pDir;
187  return writeData(pTree, dynamic_cast<INTuple*>(pObject));
188  }
189  }
190  }
191  catch (...) {
192  }
193  MsgStream log (msgSvc(), "RNTupleCnv");
194  log << MSG::ERROR << "Failed to create persistent N-tuple!" << endmsg;
195  return StatusCode::FAILURE;
196 }
197 
198 //-----------------------------------------------------------------------------
201  DataObject* pObj )
202 //-----------------------------------------------------------------------------
203 {
204  MsgStream log (msgSvc(), "RNTupleCnv");
205  if ( 0 != pAddr ) {
206  GlobalDirectoryRestore restore;
207  TDirectory* pDir = (TDirectory*)pAddr->ipar()[0];
208  RootObjAddress *rAddr = dynamic_cast<RootObjAddress*>( pAddr );
209  if (rAddr == 0) {
210  log << MSG::ERROR << "Could not cast to RootObjAddress" << endmsg;
211  return StatusCode::FAILURE;
212  }
213  TTree* pTree = (TTree*) rAddr->tObj();
214  if ( 0 != pDir && 0 != pTree ) {
215  gDirectory->cd(pDir->GetPath());
216  pTree->Write("",TObject::kOverwrite);
217  return StatusCode::SUCCESS;
218  }
219  }
220  else {
221  log << MSG::WARNING << "empty ntuple: " << pObj->registry()->identifier()
222  << endmsg;
223  return ( createRep(pObj,pAddr) );
224  }
225  return StatusCode::FAILURE;
226 }
227 
228 
230 
231 //-----------------------------------------------------------------------------
233  //-----------------------------------------------------------------------------
234  switch( type ) {
235  case DataTypeInfo::BOOL: return "/O"; // bool
236  case DataTypeInfo::SHORT: return "/S"; // short
237  case DataTypeInfo::INT: return "/I"; // int
238  case DataTypeInfo::LONG: return "/I"; // long
239  case DataTypeInfo::LONGLONG: return "/L"; // longlong
240  case DataTypeInfo::USHORT: return "/s"; // ushort
241  case DataTypeInfo::UINT: return "/i"; // uint
242  case DataTypeInfo::ULONG: return "/i"; // ulong
243  case DataTypeInfo::ULONGLONG: return "/l"; // ulonglong
244  case DataTypeInfo::FLOAT: return "/F"; // float
245  case DataTypeInfo::DOUBLE: return "/D"; // double
246  case DataTypeInfo::CHAR: return "/B"; // char
247  case DataTypeInfo::UCHAR: return "/b"; // char
248  default: return "";
249  }
250  // cannot reach this point
251 }
252 
253 //-----------------------------------------------------------------------------
255  //-----------------------------------------------------------------------------
256  int sp;
257  if ( (sp=full.find("/")) != -1 ) {
258  blk = full.substr(0,sp);
259  var = full.substr(sp+1,full.length());
260  return true;
261  } else {
262  blk = "AUTO_BLK";
263  var = full;
264  return false;
265  }
266 
267 }
268 
269 //-----------------------------------------------------------------------------
271 
272 #define INSTANTIATE(TYP) \
273  template INTupleItem* createNTupleItem<TYP>(std::string itemName, std::string blockName, std::string index_name, int indexRange, int arraySize, TYP minimum, TYP maximum, INTuple* tuple)
274 
275 namespace RootHistCnv {
276 
277  template<class TYP>
279  std::string indexName,
280  int indexRange, int arraySize,
281  TYP min, TYP max,
282  INTuple* ntup) {
283 
284  std::string varName;
285  if (blockName != "") {
286  varName = blockName + "/" + itemName;
287  } else {
288  varName = itemName;
289  }
290 
291  TYP null = 0;
292  INTupleItem* col = 0;
293 
294  if (min == 0 && max == 0) {
295  min = NTuple::Range<TYP>::min();
296  max = NTuple::Range<TYP>::max();
297  }
298 
299 
300  if (indexName == "") {
301 
302  if (arraySize == 1) {
303  // simple items
304  col = NTuple::_Item<TYP>::create(ntup, varName,
305  typeid(TYP),
306  min, max, null);
307 
308  } else {
309  // Arrays of fixed size
310  col = NTuple::_Array<TYP>::create(ntup, varName,
311  typeid(TYP),
312  "", arraySize,
313  min, max, null);
314  }
315 
316  } else {
317 
318  if (arraySize == 1) {
319  // Arrays of variable size
320  col = NTuple::_Array<TYP>::create(ntup, varName,
321  typeid(TYP),
322  indexName, indexRange,
323  min, max, null);
324  } else {
325  // Matrices
326  col = NTuple::_Matrix<TYP>::create(ntup, varName,
327  typeid(TYP),
328  indexName,
329  indexRange, arraySize,
330  min, max, null);
331  }
332 
333  }
334 
335  return col;
336 
337  }
338 
339  INSTANTIATE(float);
340  INSTANTIATE(double);
341  INSTANTIATE(bool);
342  INSTANTIATE(char);
343  INSTANTIATE(int);
344  INSTANTIATE(short);
345  INSTANTIATE(long);
346  INSTANTIATE(long long);
347  INSTANTIATE(unsigned char);
348  INSTANTIATE(unsigned int);
349  INSTANTIATE(unsigned short);
350  INSTANTIATE(unsigned long);
351  INSTANTIATE(unsigned long long);
352 
353 }

Generated at Wed Dec 4 2013 14:33:12 for Gaudi Framework, version v24r2 by Doxygen version 1.8.2 written by Dimitri van Heesch, © 1997-2004