Loading [MathJax]/extensions/tex2jax.js
The Gaudi Framework  v36r16 (ea80daf8)
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Modules Pages
RNTupleCnv.cpp
Go to the documentation of this file.
1 /***********************************************************************************\
2 * (c) Copyright 1998-2019 CERN for the benefit of the LHCb and ATLAS collaborations *
3 * *
4 * This software is distributed under the terms of the Apache version 2 licence, *
5 * copied verbatim in the file "LICENSE". *
6 * *
7 * In applying this licence, CERN does not waive the privileges and immunities *
8 * granted to it by virtue of its status as an Intergovernmental Organization *
9 * or submit itself to any jurisdiction. *
10 \***********************************************************************************/
11 // Include files
12 #include "RNTupleCnv.h"
13 #include "RootObjAddress.h"
14 
15 #include "GaudiKernel/DataObject.h"
18 #include "GaudiKernel/INTupleSvc.h"
20 #include "GaudiKernel/IRegistry.h"
21 #include "GaudiKernel/MsgStream.h"
22 #include "GaudiKernel/NTuple.h"
23 #include "GaudiKernel/SmartIF.h"
24 
25 // Root
26 #include "TTree.h"
27 
28 #ifdef __ICC
29 // disable icc remark #1572: floating-point equality and inequality comparisons are unreliable
30 // they are intended
31 # pragma warning( disable : 1572 )
32 #endif
33 
34 //-----------------------------------------------------------------------------
35 //
36 // Implementation of class : RootHistCnv::RNTupleCnv
37 //
38 // Author : Charles Leggett
39 //
40 //-----------------------------------------------------------------------------
41 
42 //-----------------------------------------------------------------------------
44 RootHistCnv::RNTupleCnv::RNTupleCnv( ISvcLocator* svc, const CLID& clid ) : RConverter( clid, svc ) {}
45 //-----------------------------------------------------------------------------
46 
47 //-----------------------------------------------------------------------------
50  //-----------------------------------------------------------------------------
52  if ( status.isSuccess() ) {
53  m_ntupleSvc = serviceLocator()->service( "NTupleSvc" );
54  if ( !m_ntupleSvc ) status = StatusCode::FAILURE;
55  }
56  return status;
57 }
58 
59 //-----------------------------------------------------------------------------
62  //-----------------------------------------------------------------------------
63  m_ntupleSvc.reset();
64  return Converter::finalize();
65 }
66 
67 //-----------------------------------------------------------------------------
70 //-----------------------------------------------------------------------------
71 {
72  MsgStream log( msgSvc(), "RNTupleCnv" );
73 
75 
76  RootObjAddress* rAddr = dynamic_cast<RootObjAddress*>( pAddress );
77 
78  if ( !rAddr ) {
79  log << MSG::ERROR << "Could not dynamic cast to RootObjAddress" << endmsg;
80  return StatusCode::FAILURE;
81  }
82 
83  TTree* rtree = (TTree*)rAddr->tObj();
84 
85  try {
86  unsigned long* info = (unsigned long*)pAddress->ipar();
87  setDirectory( pAddress->par()[0] );
88  status = readData( rtree, dynamic_cast<INTuple*>( pObject ), info[1]++ );
89  } catch ( ... ) {}
90  return status;
91 }
92 
93 //-----------------------------------------------------------------------------
96 //-----------------------------------------------------------------------------
97 {
98  MsgStream log( msgSvc(), "RNTupleCnv" );
99 
100  IRegistry* pReg = pAddress->registry();
101 
102  // log << MSG::WARNING << "adr: " << pAddress->par()[0] << " <> "
103  // << pAddress->par()[1] << " <> " << pAddress->ipar()[0] << " <> "
104  // << pAddress->ipar()[1] << " <> " << hex << rAddr->tObj()
105  // << dec << " <> " << pReg->identifier() << endmsg;
106 
107  std::string ident = pReg->identifier();
108 
109  StatusCode status = readObject( pAddress, refpObject ); // Doesn't do anything
110  if ( status.isSuccess() ) {
111  RootObjAddress* rAddr = dynamic_cast<RootObjAddress*>( pAddress );
112  if ( !rAddr ) {
113  log << MSG::ERROR << "Could not cast to RootObjAddress" << endmsg;
114  return StatusCode::FAILURE;
115  }
116  INTuple* nt = nullptr;
117  TTree* tobj = (TTree*)rAddr->tObj();
118  status = load( tobj, nt );
119  if ( status.isSuccess() ) {
120  refpObject = dynamic_cast<DataObject*>( nt );
121  } else {
122  log << MSG::ERROR << "Problems loading ntuple id: " << pReg->identifier() << endmsg;
123  }
124  }
125  return status;
126 }
127 
128 //-----------------------------------------------------------------------------
131 //-----------------------------------------------------------------------------
132 {
133  GlobalDirectoryRestore restore;
134  pAddr = nullptr;
135  try {
136  IRegistry* pReg = pObject->registry();
137  if ( pReg ) {
138  pAddr = pReg->address();
139  if ( !pAddr ) {
140  auto dataMgr = dataProvider().as<IDataManagerSvc>();
141  if ( dataMgr ) {
142  IRegistry* pParentReg = nullptr;
143  StatusCode status = dataMgr->objectParent( pReg, pParentReg );
144  if ( status.isSuccess() ) {
145  IOpaqueAddress* pParAddr = pParentReg->address();
146  if ( pParAddr ) {
147  TDirectory* pParentDir = (TDirectory*)pParAddr->ipar()[0];
148  if ( pParentDir ) {
149  TTree* pTree = nullptr;
150  auto dsc = pReg->name().substr( 1 );
151  gDirectory = pParentDir;
152  status = book( dsc, dynamic_cast<INTuple*>( pObject ), pTree );
153  if ( !status.isSuccess() ) { return status; }
154  status = createAddress( pObject, gDirectory, pTree, pAddr );
155  if ( !status.isSuccess() ) { return status; }
156  return writeData( pTree, dynamic_cast<INTuple*>( pObject ) );
157  }
158  }
159  }
160  }
161  } else {
162  TDirectory* pDir = (TDirectory*)pAddr->ipar()[0];
163  RootObjAddress* rAddr = dynamic_cast<RootObjAddress*>( pAddr );
164  if ( !rAddr ) {
165  MsgStream log( msgSvc(), "RNTupleCnv" );
166  log << MSG::ERROR << "Could not cast to RootObjAddress" << endmsg;
167  return StatusCode::FAILURE;
168  }
169  TTree* pTree = (TTree*)rAddr->tObj();
170  gDirectory = pDir;
171  return writeData( pTree, dynamic_cast<INTuple*>( pObject ) );
172  }
173  }
174  } catch ( ... ) {}
175  MsgStream log( msgSvc(), "RNTupleCnv" );
176  log << MSG::ERROR << "Failed to create persistent N-tuple!" << endmsg;
177  return StatusCode::FAILURE;
178 }
179 
180 //-----------------------------------------------------------------------------
183 //-----------------------------------------------------------------------------
184 {
185  MsgStream log( msgSvc(), "RNTupleCnv" );
186  if ( pAddr ) {
187  GlobalDirectoryRestore restore;
188  TDirectory* pDir = (TDirectory*)pAddr->ipar()[0];
189  RootObjAddress* rAddr = dynamic_cast<RootObjAddress*>( pAddr );
190  if ( !rAddr ) {
191  log << MSG::ERROR << "Could not cast to RootObjAddress" << endmsg;
192  return StatusCode::FAILURE;
193  }
194  TTree* pTree = (TTree*)rAddr->tObj();
195  if ( pDir && pTree ) {
196  gDirectory->cd( pDir->GetPath() );
197  pTree->Write( "", TObject::kOverwrite );
198  return StatusCode::SUCCESS;
199  }
200  } else {
201  log << MSG::WARNING << "empty ntuple: " << pObj->registry()->identifier() << endmsg;
202  return createRep( pObj, pAddr );
203  }
204  return StatusCode::FAILURE;
205 }
206 
208 
209 //-----------------------------------------------------------------------------
211  //-----------------------------------------------------------------------------
212  switch ( type ) {
213  case DataTypeInfo::BOOL:
214  return "/O"; // bool
215  case DataTypeInfo::SHORT:
216  return "/S"; // short
217  case DataTypeInfo::INT:
218  return "/I"; // int
219  case DataTypeInfo::LONG:
220  return "/I"; // long
222  return "/L"; // long long
224  return "/s"; // ushort
225  case DataTypeInfo::UINT:
226  return "/i"; // uint
227  case DataTypeInfo::ULONG:
228  return "/i"; // ulong
230  return "/l"; // unsigned long long
231  case DataTypeInfo::FLOAT:
232  return "/F"; // float
234  return "/D"; // double
235  case DataTypeInfo::CHAR:
236  return "/B"; // char
237  case DataTypeInfo::UCHAR:
238  return "/b"; // char
239  default:
240  return "";
241  }
242  // cannot reach this point
243 }
244 
245 //-----------------------------------------------------------------------------
247  //-----------------------------------------------------------------------------
248  int sp;
249  if ( ( sp = full.find( "/" ) ) != -1 ) {
250  blk = full.substr( 0, sp );
251  var = full.substr( sp + 1 );
252  return true;
253  } else {
254  blk = "AUTO_BLK";
255  var = full;
256  return false;
257  }
258 }
259 
260 //-----------------------------------------------------------------------------
262 
263 #define INSTANTIATE( TYP ) \
264  template INTupleItem* createNTupleItem<TYP>( std::string itemName, std::string blockName, std::string index_name, \
265  int indexRange, int arraySize, TYP minimum, TYP maximum, \
266  INTuple* tuple )
267 
268 namespace RootHistCnv {
269 
270  template <class TYP>
271  INTupleItem* createNTupleItem( std::string itemName, std::string blockName, std::string indexName, int indexRange,
272  int arraySize, TYP min, TYP max, INTuple* ntup ) {
273 
274  std::string varName;
275  if ( blockName != "" ) {
276  varName = blockName + "/" + itemName;
277  } else {
278  varName = itemName;
279  }
280 
281  TYP null = 0;
282  INTupleItem* col = nullptr;
283 
284  if ( min == 0 && max == 0 ) {
287  }
288 
289  if ( indexName == "" ) {
290 
291  if ( arraySize == 1 ) {
292  // simple items
293  col = NTuple::_Item<TYP>::create( ntup, varName, typeid( TYP ), min, max, null );
294 
295  } else {
296  // Arrays of fixed size
297  col = NTuple::_Array<TYP>::create( ntup, varName, typeid( TYP ), "", arraySize, min, max, null );
298  }
299 
300  } else {
301 
302  if ( arraySize == 1 ) {
303  // Arrays of variable size
304  col = NTuple::_Array<TYP>::create( ntup, varName, typeid( TYP ), indexName, indexRange, min, max, null );
305  } else {
306  // Matrices
307  col = NTuple::_Matrix<TYP>::create( ntup, varName, typeid( TYP ), indexName, indexRange, arraySize, min, max,
308  null );
309  }
310  }
311 
312  return col;
313  }
314 
315  INSTANTIATE( float );
316  INSTANTIATE( double );
317  INSTANTIATE( bool );
318  INSTANTIATE( char );
319  INSTANTIATE( int );
320  INSTANTIATE( short );
321  INSTANTIATE( long );
322  INSTANTIATE( long long );
323  INSTANTIATE( unsigned char );
324  INSTANTIATE( unsigned int );
325  INSTANTIATE( unsigned short );
326  INSTANTIATE( unsigned long );
327  INSTANTIATE( unsigned long long );
328 } // namespace RootHistCnv
std::string
STL class.
RootHistCnv::RootObjAddress::tObj
virtual TObject * tObj() const
Retrieve TObject* ptr.
Definition: RootObjAddress.h:91
IOpaqueAddress::par
virtual const std::string * par() const =0
Retrieve String parameters.
RootHistCnv::RNTupleCnv::createRep
StatusCode createRep(DataObject *pObj, IOpaqueAddress *&refpAddr) override
Convert the transient object to the requested representation.
Definition: RNTupleCnv.cpp:130
Gaudi.Configuration.log
log
Definition: Configuration.py:30
RootHistCnv::createNTupleItem
INTupleItem * createNTupleItem(std::string itemName, std::string blockName, std::string indexName, int indexRange, int arraySize, TYP min, TYP max, INTuple *ntup)
Add an item of a given type to the N tuple.
Definition: RNTupleCnv.cpp:271
IDataManagerSvc
Definition: IDataManagerSvc.h:55
StatusCode::isSuccess
bool isSuccess() const
Definition: StatusCode.h:314
IOpaqueAddress
Definition: IOpaqueAddress.h:33
ISvcLocator
Definition: ISvcLocator.h:46
MSG::WARNING
@ WARNING
Definition: IMessageSvc.h:25
RootHistCnv::RNTupleCnv::createObj
StatusCode createObj(IOpaqueAddress *pAddr, DataObject *&refpObj) override
Create the transient representation of an object.
Definition: RNTupleCnv.cpp:95
max
EventIDBase max(const EventIDBase &lhs, const EventIDBase &rhs)
Definition: EventIDBase.h:225
IRegistry
Definition: IRegistry.h:32
DataTypeInfo::USHORT
@ USHORT
Definition: DataTypeInfo.h:36
RootHistCnv::RootObjAddress
Definition: RootObjAddress.h:21
IDataProviderSvc.h
DataTypeInfo::ULONG
@ ULONG
Definition: DataTypeInfo.h:38
compareOutputFiles.sp
sp
Definition: compareOutputFiles.py:515
DataTypeInfo::LONG
@ LONG
Definition: DataTypeInfo.h:42
DataTypeInfo::DOUBLE
@ DOUBLE
Definition: DataTypeInfo.h:45
SmartIF.h
IRegistry::name
virtual const name_type & name() const =0
Name of the directory (or key)
RootHistCnv
Definition: DirectoryCnv.h:27
NTuple::_Item::create
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:36
RootHistCnv::RNTupleCnv::updateObj
StatusCode updateObj(IOpaqueAddress *pAddr, DataObject *refpObj) override
Update the transient object from the other representation.
Definition: RNTupleCnv.cpp:69
StatusCode
Definition: StatusCode.h:65
DataTypeInfo::SHORT
@ SHORT
Definition: DataTypeInfo.h:40
Gaudi::Histos::book
GAUDI_API AIDA::IHistogram1D * book(IHistogramSvc *svc, const std::string &path, const Gaudi::Histo1DDef &hist)
helper function to book 1D-histogram
Definition: HistoDef.cpp:97
RootHistCnv::RNTupleCnv::finalize
StatusCode finalize() override
Finalize the converter.
Definition: RNTupleCnv.cpp:61
IOpaqueAddress.h
RootHistCnv::parseName
bool parseName(const std::string &full, std::string &blk, std::string &var)
Definition: RNTupleCnv.cpp:246
NTuple.h
INTuple
Definition: INTuple.h:91
RootHistCnv::RConverter
Definition: RConverter.h:41
CLID
unsigned int CLID
Class ID definition.
Definition: ClassID.h:18
endmsg
MsgStream & endmsg(MsgStream &s)
MsgStream Modifier: endmsg. Calls the output method of the MsgStream.
Definition: MsgStream.h:203
NTuple::_Matrix::create
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:50
min
EventIDBase min(const EventIDBase &lhs, const EventIDBase &rhs)
Definition: EventIDBase.h:212
INTupleItem
Definition: INTuple.h:37
RootObjAddress.h
IRegistry.h
MsgStream
Definition: MsgStream.h:34
GlobalDirectoryRestore
Definition: RConverter.h:26
IOpaqueAddress::registry
virtual IRegistry * registry() const =0
Update branch name.
NTuple::Range::max
static TYP max()
Maximal number of data.
Definition: NTuple.h:94
RootHistCnv::RNTupleCnv::initialize
StatusCode initialize() override
Initialize the converter.
Definition: RNTupleCnv.cpp:49
DataTypeInfo::FLOAT
@ FLOAT
Definition: DataTypeInfo.h:44
std::string::substr
T substr(T... args)
RootHistCnv::INSTANTIATE
INSTANTIATE(float)
IRegistry::address
virtual IOpaqueAddress * address() const =0
Retrieve opaque storage address.
RNTupleCnv.h
gaudirun.type
type
Definition: gaudirun.py:162
DataTypeInfo::UCHAR
@ UCHAR
Definition: DataTypeInfo.h:35
DataTypeInfo::UINT
@ UINT
Definition: DataTypeInfo.h:37
StatusCode::SUCCESS
constexpr static const auto SUCCESS
Definition: StatusCode.h:100
DataObject.h
DataTypeInfo::BOOL
@ BOOL
Definition: DataTypeInfo.h:43
MSG::ERROR
@ ERROR
Definition: IMessageSvc.h:25
DataTypeInfo::ULONGLONG
@ ULONGLONG
Definition: DataTypeInfo.h:55
IRegistry::identifier
virtual const id_type & identifier() const =0
Full identifier (or key)
DataObject
Definition: DataObject.h:40
NTuple::_Array::create
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:43
GaudiConfig2.semantics.ident
ident
Definition: semantics.py:200
DataTypeInfo::LONGLONG
@ LONGLONG
Definition: DataTypeInfo.h:54
RootHistCnv::RNTupleCnv::updateRep
StatusCode updateRep(IOpaqueAddress *pAddr, DataObject *pObj) override
Update the converted representation of a transient object.
Definition: RNTupleCnv.cpp:182
AsyncIncidents.msgSvc
msgSvc
Definition: AsyncIncidents.py:34
Converter::initialize
StatusCode initialize() override
Initialize the converter.
Definition: Converter.cpp:53
StatusCode::FAILURE
constexpr static const auto FAILURE
Definition: StatusCode.h:101
RootHistCnv::RNTupleCnv::rootVarType
virtual std::string rootVarType(int)
Return ROOT type info:
Definition: RNTupleCnv.cpp:210
RootHistCnv::RNTupleCnv::RNTupleCnv
RNTupleCnv(ISvcLocator *svc, const CLID &clid)
Standard constructor.
Definition: RNTupleCnv.cpp:44
DataObject::registry
IRegistry * registry() const
Get pointer to Registry.
Definition: DataObject.h:82
NTuple::Range::min
static TYP min()
Minimal number of data.
Definition: NTuple.h:92
IDataManagerSvc.h
DataTypeInfo::INT
@ INT
Definition: DataTypeInfo.h:41
INTupleSvc.h
Converter::finalize
StatusCode finalize() override
Initialize the converter.
Definition: Converter.cpp:60
DataTypeInfo::CHAR
@ CHAR
Definition: DataTypeInfo.h:39
MsgStream.h
IOpaqueAddress::ipar
virtual const unsigned long * ipar() const =0
Access to generic link parameters.