The Gaudi Framework  v30r3 (a5ef0a68)
RNTupleCnv.cpp
Go to the documentation of this file.
1 // Include files
2 #include "RNTupleCnv.h"
3 #include "RootObjAddress.h"
4 
10 #include "GaudiKernel/IRegistry.h"
11 #include "GaudiKernel/MsgStream.h"
12 #include "GaudiKernel/NTuple.h"
13 #include "GaudiKernel/SmartIF.h"
14 
15 // Root
16 #include "TTree.h"
17 
18 #ifdef __ICC
19 // disable icc remark #1572: floating-point equality and inequality comparisons are unreliable
20 // they are intended
21 #pragma warning( disable : 1572 )
22 #endif
23 
24 //-----------------------------------------------------------------------------
25 //
26 // Implementation of class : RootHistCnv::RNTupleCnv
27 //
28 // Author : Charles Leggett
29 //
30 //-----------------------------------------------------------------------------
31 
32 //-----------------------------------------------------------------------------
34 RootHistCnv::RNTupleCnv::RNTupleCnv( ISvcLocator* svc, const CLID& clid ) : RConverter( clid, svc ) {}
35 //-----------------------------------------------------------------------------
36 
37 //-----------------------------------------------------------------------------
40 {
41  //-----------------------------------------------------------------------------
43  if ( status.isSuccess() ) {
44  m_ntupleSvc = serviceLocator()->service( "NTupleSvc" );
45  if ( !m_ntupleSvc ) status = StatusCode::FAILURE;
46  }
47  return status;
48 }
49 
50 //-----------------------------------------------------------------------------
53 {
54  //-----------------------------------------------------------------------------
56  return Converter::finalize();
57 }
58 
59 //-----------------------------------------------------------------------------
62 //-----------------------------------------------------------------------------
63 {
64  MsgStream log( msgSvc(), "RNTupleCnv" );
65 
67 
68  RootObjAddress* rAddr = dynamic_cast<RootObjAddress*>( pAddress );
69 
70  if ( !rAddr ) {
71  log << MSG::ERROR << "Could not dynamic cast to RootObjAddress" << endmsg;
72  return StatusCode::FAILURE;
73  }
74 
75  TTree* rtree = (TTree*)rAddr->tObj();
76 
77  try {
78  unsigned long* info = (unsigned long*)pAddress->ipar();
79  setDirectory( pAddress->par()[0] );
80  status = readData( rtree, dynamic_cast<INTuple*>( pObject ), info[1]++ );
81  } catch ( ... ) {
82  }
83  return status;
84 }
85 
86 //-----------------------------------------------------------------------------
89 //-----------------------------------------------------------------------------
90 {
91  MsgStream log( msgSvc(), "RNTupleCnv" );
92 
93  IRegistry* pReg = pAddress->registry();
94 
95  // log << MSG::WARNING << "adr: " << pAddress->par()[0] << " <> "
96  // << pAddress->par()[1] << " <> " << pAddress->ipar()[0] << " <> "
97  // << pAddress->ipar()[1] << " <> " << hex << rAddr->tObj()
98  // << dec << " <> " << pReg->identifier() << endmsg;
99 
100  std::string ident = pReg->identifier();
101 
102  StatusCode status = readObject( pAddress, refpObject ); // Doesn't do anything
103  if ( status.isSuccess() ) {
104  RootObjAddress* rAddr = dynamic_cast<RootObjAddress*>( pAddress );
105  if ( !rAddr ) {
106  log << MSG::ERROR << "Could not cast to RootObjAddress" << endmsg;
107  return StatusCode::FAILURE;
108  }
109  INTuple* nt = nullptr;
110  TTree* tobj = (TTree*)rAddr->tObj();
111  status = load( tobj, nt );
112  if ( status.isSuccess() ) {
113  refpObject = dynamic_cast<DataObject*>( nt );
114  } else {
115  log << MSG::ERROR << "Problems loading ntuple id: " << pReg->identifier() << endmsg;
116  }
117  }
118  return status;
119 }
120 
121 //-----------------------------------------------------------------------------
124 //-----------------------------------------------------------------------------
125 {
126  GlobalDirectoryRestore restore;
127  pAddr = nullptr;
128  try {
129  IRegistry* pReg = pObject->registry();
130  if ( pReg ) {
131  pAddr = pReg->address();
132  if ( !pAddr ) {
133  auto dataMgr = dataProvider().as<IDataManagerSvc>();
134  if ( dataMgr ) {
135  IRegistry* pParentReg = nullptr;
136  StatusCode status = dataMgr->objectParent( pReg, pParentReg );
137  if ( status.isSuccess() ) {
138  IOpaqueAddress* pParAddr = pParentReg->address();
139  if ( pParAddr ) {
140  TDirectory* pParentDir = (TDirectory*)pParAddr->ipar()[0];
141  if ( pParentDir ) {
142  TTree* pTree = nullptr;
143  auto dsc = pReg->name().substr( 1 );
144  gDirectory = pParentDir;
145  status = book( dsc, dynamic_cast<INTuple*>( pObject ), pTree );
146  if ( !status.isSuccess() ) {
147  return status;
148  }
149  status = createAddress( pObject, gDirectory, pTree, pAddr );
150  if ( !status.isSuccess() ) {
151  return status;
152  }
153  return writeData( pTree, dynamic_cast<INTuple*>( pObject ) );
154  }
155  }
156  }
157  }
158  } else {
159  TDirectory* pDir = (TDirectory*)pAddr->ipar()[0];
160  RootObjAddress* rAddr = dynamic_cast<RootObjAddress*>( pAddr );
161  if ( !rAddr ) {
162  MsgStream log( msgSvc(), "RNTupleCnv" );
163  log << MSG::ERROR << "Could not cast to RootObjAddress" << endmsg;
164  return StatusCode::FAILURE;
165  }
166  TTree* pTree = (TTree*)rAddr->tObj();
167  gDirectory = pDir;
168  return writeData( pTree, dynamic_cast<INTuple*>( pObject ) );
169  }
170  }
171  } catch ( ... ) {
172  }
173  MsgStream log( msgSvc(), "RNTupleCnv" );
174  log << MSG::ERROR << "Failed to create persistent N-tuple!" << endmsg;
175  return StatusCode::FAILURE;
176 }
177 
178 //-----------------------------------------------------------------------------
181 //-----------------------------------------------------------------------------
182 {
183  MsgStream log( msgSvc(), "RNTupleCnv" );
184  if ( pAddr ) {
185  GlobalDirectoryRestore restore;
186  TDirectory* pDir = (TDirectory*)pAddr->ipar()[0];
187  RootObjAddress* rAddr = dynamic_cast<RootObjAddress*>( pAddr );
188  if ( !rAddr ) {
189  log << MSG::ERROR << "Could not cast to RootObjAddress" << endmsg;
190  return StatusCode::FAILURE;
191  }
192  TTree* pTree = (TTree*)rAddr->tObj();
193  if ( pDir && pTree ) {
194  gDirectory->cd( pDir->GetPath() );
195  pTree->Write( "", TObject::kOverwrite );
196  return StatusCode::SUCCESS;
197  }
198  } else {
199  log << MSG::WARNING << "empty ntuple: " << pObj->registry()->identifier() << endmsg;
200  return createRep( pObj, pAddr );
201  }
202  return StatusCode::FAILURE;
203 }
204 
206 
207 //-----------------------------------------------------------------------------
209 {
210  //-----------------------------------------------------------------------------
211  switch ( type ) {
212  case DataTypeInfo::BOOL:
213  return "/O"; // bool
214  case DataTypeInfo::SHORT:
215  return "/S"; // short
216  case DataTypeInfo::INT:
217  return "/I"; // int
218  case DataTypeInfo::LONG:
219  return "/I"; // long
221  return "/L"; // longlong
223  return "/s"; // ushort
224  case DataTypeInfo::UINT:
225  return "/i"; // uint
226  case DataTypeInfo::ULONG:
227  return "/i"; // ulong
229  return "/l"; // ulonglong
230  case DataTypeInfo::FLOAT:
231  return "/F"; // float
233  return "/D"; // double
234  case DataTypeInfo::CHAR:
235  return "/B"; // char
236  case DataTypeInfo::UCHAR:
237  return "/b"; // char
238  default:
239  return "";
240  }
241  // cannot reach this point
242 }
243 
244 //-----------------------------------------------------------------------------
246 {
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 
271  template <class TYP>
272  INTupleItem* createNTupleItem( std::string itemName, std::string blockName, std::string indexName, int indexRange,
273  int arraySize, TYP min, TYP max, INTuple* ntup )
274  {
275 
276  std::string varName;
277  if ( blockName != "" ) {
278  varName = blockName + "/" + itemName;
279  } else {
280  varName = itemName;
281  }
282 
283  TYP null = 0;
284  INTupleItem* col = nullptr;
285 
286  if ( min == 0 && max == 0 ) {
287  min = NTuple::Range<TYP>::min();
288  max = NTuple::Range<TYP>::max();
289  }
290 
291  if ( indexName == "" ) {
292 
293  if ( arraySize == 1 ) {
294  // simple items
295  col = NTuple::_Item<TYP>::create( ntup, varName, typeid( TYP ), min, max, null );
296 
297  } else {
298  // Arrays of fixed size
299  col = NTuple::_Array<TYP>::create( ntup, varName, typeid( TYP ), "", arraySize, min, max, null );
300  }
301 
302  } else {
303 
304  if ( arraySize == 1 ) {
305  // Arrays of variable size
306  col = NTuple::_Array<TYP>::create( ntup, varName, typeid( TYP ), indexName, indexRange, min, max, null );
307  } else {
308  // Matrices
309  col = NTuple::_Matrix<TYP>::create( ntup, varName, typeid( TYP ), indexName, indexRange, arraySize, min, max,
310  null );
311  }
312  }
313 
314  return col;
315  }
316 
317  INSTANTIATE( float );
318  INSTANTIATE( double );
319  INSTANTIATE( bool );
320  INSTANTIATE( char );
321  INSTANTIATE( int );
322  INSTANTIATE( short );
323  INSTANTIATE( long );
324  INSTANTIATE( long long );
325  INSTANTIATE( unsigned char );
326  INSTANTIATE( unsigned int );
327  INSTANTIATE( unsigned short );
328  INSTANTIATE( unsigned long );
329  INSTANTIATE( unsigned long long );
330 }
Root Converter.
Definition: RConverter.h:33
constexpr static const auto FAILURE
Definition: StatusCode.h:88
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:25
void setDirectory(const std::string &loc)
Definition: RConverter.cpp:113
RNTupleCnv(ISvcLocator *svc, const CLID &clid)
Standard constructor.
Definition: RNTupleCnv.cpp:34
virtual StatusCode book(const std::string &desc, INTuple *pObj, TTree *&tree)=0
Book a new N tuple.
StatusCode updateRep(IOpaqueAddress *pAddr, DataObject *pObj) override
Update the converted representation of a transient object.
Definition: RNTupleCnv.cpp:180
bool parseName(const std::string &full, std::string &blk, std::string &var)
Definition: RNTupleCnv.cpp:245
bool isSuccess() const
Definition: StatusCode.h:287
StatusCode initialize() override
Initialize the converter.
Definition: Converter.cpp:43
virtual IRegistry * registry() const =0
Update branch name.
static TYP min()
Minimal number of data.
Definition: NTuple.h:85
SmartIF< IMessageSvc > & msgSvc() const
Retrieve pointer to message service.
Definition: Converter.cpp:102
StatusCode initialize() override
Initialize the converter.
Definition: RNTupleCnv.cpp:39
virtual const name_type & name() const =0
Name of the directory (or key)
virtual const std::string * par() const =0
Retrieve String parameters.
StatusCode finalize() override
Finalize the converter.
Definition: RNTupleCnv.cpp:52
IRegistry * registry() const
Get pointer to Registry.
Definition: DataObject.h:73
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
STL class.
NTuple interface class definition.
Definition: INTuple.h:82
StatusCode finalize() override
Initialize the converter.
Definition: Converter.cpp:53
StatusCode service(const Gaudi::Utils::TypeNameString &name, T *&svc, bool createIf=true)
Templated method to access a service by name.
Definition: ISvcLocator.h:79
SmartIF< IFace > as() const
return a new SmartIF instance to another interface
Definition: SmartIF.h:115
NTuple interface class definition.
Definition: INTuple.h:27
virtual const unsigned long * ipar() const =0
Access to generic link parameters.
virtual const id_type & identifier() const =0
Full identifier (or key)
StatusCode updateObj(IOpaqueAddress *pAddr, DataObject *refpObj) override
Update the transient object from the other representation.
Definition: RNTupleCnv.cpp:61
This class is used for returning status codes from appropriate routines.
Definition: StatusCode.h:51
virtual TObject * tObj() const
Retrieve TObject* ptr.
static TYP max()
Maximal number of data.
Definition: NTuple.h:87
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:8
constexpr static const auto SUCCESS
Definition: StatusCode.h:87
SmartIF< ISvcLocator > & serviceLocator() const
Retrieve pointer to service locator.
Definition: Converter.cpp:99
virtual StatusCode load(TTree *tree, INTuple *&refpObj)=0
Create the transient representation of an object.
INSTANTIATE(float)
T find(T...args)
SmartIF< IDataProviderSvc > & dataProvider() const override
Get Data provider service.
Definition: Converter.cpp:73
virtual StatusCode writeData(TTree *rtree, INTuple *pObj)=0
Write N tuple data.
virtual StatusCode readObject(IOpaqueAddress *pAddr, DataObject *&refpObj)
Create the transient representation of an object.
Definition: RConverter.cpp:256
T substr(T...args)
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:272
void reset(TYPE *ptr=nullptr)
Set the internal pointer to the passed one disposing of the old one.
Definition: SmartIF.h:92
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:26
Opaque address interface definition.
SmartIF< INTupleSvc > m_ntupleSvc
Reference to N tuple service.
Definition: RNTupleCnv.h:56
virtual IOpaqueAddress * address() const =0
Retrieve opaque storage address.
A DataObject is the base class of any identifiable object on any data store.
Definition: DataObject.h:30
StatusCode createRep(DataObject *pObj, IOpaqueAddress *&refpAddr) override
Convert the transient object to the requested representation.
Definition: RNTupleCnv.cpp:123
virtual StatusCode readData(TTree *rtree, INTuple *pObj, long ievt)=0
Read N tuple data.
MsgStream & endmsg(MsgStream &s)
MsgStream Modifier: endmsg. Calls the output method of the MsgStream.
Definition: MsgStream.h:209
StatusCode createObj(IOpaqueAddress *pAddr, DataObject *&refpObj) override
Create the transient representation of an object.
Definition: RNTupleCnv.cpp:88
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:42
StatusCode createAddress(DataObject *pObject, TDirectory *pDir, TObject *pTObject, IOpaqueAddress *&refpAddr)
Create address of the transient object according to the requested representation. ...
Definition: RConverter.cpp:160
virtual std::string rootVarType(int)
Return ROOT type info:
Definition: RNTupleCnv.cpp:208