Loading [MathJax]/extensions/tex2jax.js
The Gaudi Framework  v31r0 (aeb156f0)
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Modules Pages
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  //-----------------------------------------------------------------------------
42  if ( status.isSuccess() ) {
43  m_ntupleSvc = serviceLocator()->service( "NTupleSvc" );
44  if ( !m_ntupleSvc ) status = StatusCode::FAILURE;
45  }
46  return status;
47 }
48 
49 //-----------------------------------------------------------------------------
52  //-----------------------------------------------------------------------------
54  return Converter::finalize();
55 }
56 
57 //-----------------------------------------------------------------------------
60 //-----------------------------------------------------------------------------
61 {
62  MsgStream log( msgSvc(), "RNTupleCnv" );
63 
65 
66  RootObjAddress* rAddr = dynamic_cast<RootObjAddress*>( pAddress );
67 
68  if ( !rAddr ) {
69  log << MSG::ERROR << "Could not dynamic cast to RootObjAddress" << endmsg;
70  return StatusCode::FAILURE;
71  }
72 
73  TTree* rtree = (TTree*)rAddr->tObj();
74 
75  try {
76  unsigned long* info = (unsigned long*)pAddress->ipar();
77  setDirectory( pAddress->par()[0] );
78  status = readData( rtree, dynamic_cast<INTuple*>( pObject ), info[1]++ );
79  } catch ( ... ) {}
80  return status;
81 }
82 
83 //-----------------------------------------------------------------------------
86 //-----------------------------------------------------------------------------
87 {
88  MsgStream log( msgSvc(), "RNTupleCnv" );
89 
90  IRegistry* pReg = pAddress->registry();
91 
92  // log << MSG::WARNING << "adr: " << pAddress->par()[0] << " <> "
93  // << pAddress->par()[1] << " <> " << pAddress->ipar()[0] << " <> "
94  // << pAddress->ipar()[1] << " <> " << hex << rAddr->tObj()
95  // << dec << " <> " << pReg->identifier() << endmsg;
96 
97  std::string ident = pReg->identifier();
98 
99  StatusCode status = readObject( pAddress, refpObject ); // Doesn't do anything
100  if ( status.isSuccess() ) {
101  RootObjAddress* rAddr = dynamic_cast<RootObjAddress*>( pAddress );
102  if ( !rAddr ) {
103  log << MSG::ERROR << "Could not cast to RootObjAddress" << endmsg;
104  return StatusCode::FAILURE;
105  }
106  INTuple* nt = nullptr;
107  TTree* tobj = (TTree*)rAddr->tObj();
108  status = load( tobj, nt );
109  if ( status.isSuccess() ) {
110  refpObject = dynamic_cast<DataObject*>( nt );
111  } else {
112  log << MSG::ERROR << "Problems loading ntuple id: " << pReg->identifier() << endmsg;
113  }
114  }
115  return status;
116 }
117 
118 //-----------------------------------------------------------------------------
121 //-----------------------------------------------------------------------------
122 {
123  GlobalDirectoryRestore restore;
124  pAddr = nullptr;
125  try {
126  IRegistry* pReg = pObject->registry();
127  if ( pReg ) {
128  pAddr = pReg->address();
129  if ( !pAddr ) {
130  auto dataMgr = dataProvider().as<IDataManagerSvc>();
131  if ( dataMgr ) {
132  IRegistry* pParentReg = nullptr;
133  StatusCode status = dataMgr->objectParent( pReg, pParentReg );
134  if ( status.isSuccess() ) {
135  IOpaqueAddress* pParAddr = pParentReg->address();
136  if ( pParAddr ) {
137  TDirectory* pParentDir = (TDirectory*)pParAddr->ipar()[0];
138  if ( pParentDir ) {
139  TTree* pTree = nullptr;
140  auto dsc = pReg->name().substr( 1 );
141  gDirectory = pParentDir;
142  status = book( dsc, dynamic_cast<INTuple*>( pObject ), pTree );
143  if ( !status.isSuccess() ) { return status; }
144  status = createAddress( pObject, gDirectory, pTree, pAddr );
145  if ( !status.isSuccess() ) { return status; }
146  return writeData( pTree, dynamic_cast<INTuple*>( pObject ) );
147  }
148  }
149  }
150  }
151  } else {
152  TDirectory* pDir = (TDirectory*)pAddr->ipar()[0];
153  RootObjAddress* rAddr = dynamic_cast<RootObjAddress*>( pAddr );
154  if ( !rAddr ) {
155  MsgStream log( msgSvc(), "RNTupleCnv" );
156  log << MSG::ERROR << "Could not cast to RootObjAddress" << endmsg;
157  return StatusCode::FAILURE;
158  }
159  TTree* pTree = (TTree*)rAddr->tObj();
160  gDirectory = pDir;
161  return writeData( pTree, dynamic_cast<INTuple*>( pObject ) );
162  }
163  }
164  } catch ( ... ) {}
165  MsgStream log( msgSvc(), "RNTupleCnv" );
166  log << MSG::ERROR << "Failed to create persistent N-tuple!" << endmsg;
167  return StatusCode::FAILURE;
168 }
169 
170 //-----------------------------------------------------------------------------
173 //-----------------------------------------------------------------------------
174 {
175  MsgStream log( msgSvc(), "RNTupleCnv" );
176  if ( pAddr ) {
177  GlobalDirectoryRestore restore;
178  TDirectory* pDir = (TDirectory*)pAddr->ipar()[0];
179  RootObjAddress* rAddr = dynamic_cast<RootObjAddress*>( pAddr );
180  if ( !rAddr ) {
181  log << MSG::ERROR << "Could not cast to RootObjAddress" << endmsg;
182  return StatusCode::FAILURE;
183  }
184  TTree* pTree = (TTree*)rAddr->tObj();
185  if ( pDir && pTree ) {
186  gDirectory->cd( pDir->GetPath() );
187  pTree->Write( "", TObject::kOverwrite );
188  return StatusCode::SUCCESS;
189  }
190  } else {
191  log << MSG::WARNING << "empty ntuple: " << pObj->registry()->identifier() << endmsg;
192  return createRep( pObj, pAddr );
193  }
194  return StatusCode::FAILURE;
195 }
196 
198 
199 //-----------------------------------------------------------------------------
201  //-----------------------------------------------------------------------------
202  switch ( type ) {
203  case DataTypeInfo::BOOL:
204  return "/O"; // bool
205  case DataTypeInfo::SHORT:
206  return "/S"; // short
207  case DataTypeInfo::INT:
208  return "/I"; // int
209  case DataTypeInfo::LONG:
210  return "/I"; // long
212  return "/L"; // long long
214  return "/s"; // ushort
215  case DataTypeInfo::UINT:
216  return "/i"; // uint
217  case DataTypeInfo::ULONG:
218  return "/i"; // ulong
220  return "/l"; // unsigned long long
221  case DataTypeInfo::FLOAT:
222  return "/F"; // float
224  return "/D"; // double
225  case DataTypeInfo::CHAR:
226  return "/B"; // char
227  case DataTypeInfo::UCHAR:
228  return "/b"; // char
229  default:
230  return "";
231  }
232  // cannot reach this point
233 }
234 
235 //-----------------------------------------------------------------------------
237  //-----------------------------------------------------------------------------
238  int sp;
239  if ( ( sp = full.find( "/" ) ) != -1 ) {
240  blk = full.substr( 0, sp );
241  var = full.substr( sp + 1 );
242  return true;
243  } else {
244  blk = "AUTO_BLK";
245  var = full;
246  return false;
247  }
248 }
249 
250 //-----------------------------------------------------------------------------
252 
253 #define INSTANTIATE( TYP ) \
254  template INTupleItem* createNTupleItem<TYP>( std::string itemName, std::string blockName, std::string index_name, \
255  int indexRange, int arraySize, TYP minimum, TYP maximum, \
256  INTuple* tuple )
257 
258 namespace RootHistCnv {
259 
260  template <class TYP>
261  INTupleItem* createNTupleItem( std::string itemName, std::string blockName, std::string indexName, int indexRange,
262  int arraySize, TYP min, TYP max, INTuple* ntup ) {
263 
264  std::string varName;
265  if ( blockName != "" ) {
266  varName = blockName + "/" + itemName;
267  } else {
268  varName = itemName;
269  }
270 
271  TYP null = 0;
272  INTupleItem* col = nullptr;
273 
274  if ( min == 0 && max == 0 ) {
275  min = NTuple::Range<TYP>::min();
276  max = NTuple::Range<TYP>::max();
277  }
278 
279  if ( indexName == "" ) {
280 
281  if ( arraySize == 1 ) {
282  // simple items
283  col = NTuple::_Item<TYP>::create( ntup, varName, typeid( TYP ), min, max, null );
284 
285  } else {
286  // Arrays of fixed size
287  col = NTuple::_Array<TYP>::create( ntup, varName, typeid( TYP ), "", arraySize, min, max, null );
288  }
289 
290  } else {
291 
292  if ( arraySize == 1 ) {
293  // Arrays of variable size
294  col = NTuple::_Array<TYP>::create( ntup, varName, typeid( TYP ), indexName, indexRange, min, max, null );
295  } else {
296  // Matrices
297  col = NTuple::_Matrix<TYP>::create( ntup, varName, typeid( TYP ), indexName, indexRange, arraySize, min, max,
298  null );
299  }
300  }
301 
302  return col;
303  }
304 
305  INSTANTIATE( float );
306  INSTANTIATE( double );
307  INSTANTIATE( bool );
308  INSTANTIATE( char );
309  INSTANTIATE( int );
310  INSTANTIATE( short );
311  INSTANTIATE( long );
312  INSTANTIATE( long long );
313  INSTANTIATE( unsigned char );
314  INSTANTIATE( unsigned int );
315  INSTANTIATE( unsigned short );
316  INSTANTIATE( unsigned long );
317  INSTANTIATE( unsigned long long );
318 } // namespace RootHistCnv
Root Converter.
Definition: RConverter.h:31
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:108
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:172
bool parseName(const std::string &full, std::string &blk, std::string &var)
Definition: RNTupleCnv.cpp:236
bool isSuccess() const
Definition: StatusCode.h:267
EventIDBase min(const EventIDBase &lhs, const EventIDBase &rhs)
Definition: EventIDBase.h:202
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:82
constexpr static const auto SUCCESS
Definition: StatusCode.h:85
SmartIF< IMessageSvc > & msgSvc() const
Retrieve pointer to message service.
Definition: Converter.cpp:95
StatusCode initialize() override
Initialize the converter.
Definition: RNTupleCnv.cpp:39
virtual const name_type & name() const =0
Name of the directory (or key)
EventIDBase max(const EventIDBase &lhs, const EventIDBase &rhs)
Definition: EventIDBase.h:215
virtual const std::string * par() const =0
Retrieve String parameters.
StatusCode finalize() override
Finalize the converter.
Definition: RNTupleCnv.cpp:51
IRegistry * registry() const
Get pointer to Registry.
Definition: DataObject.h:72
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:33
STL class.
NTuple interface class definition.
Definition: INTuple.h:81
StatusCode finalize() override
Initialize the converter.
Definition: Converter.cpp:50
StatusCode service(const Gaudi::Utils::TypeNameString &name, T *&svc, bool createIf=true)
Templated method to access a service by name.
Definition: ISvcLocator.h:76
SmartIF< IFace > as() const
return a new SmartIF instance to another interface
Definition: SmartIF.h:107
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:59
This class is used for returning status codes from appropriate routines.
Definition: StatusCode.h:50
virtual TObject * tObj() const
Retrieve TObject* ptr.
static TYP max()
Maximal number of data.
Definition: NTuple.h:84
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
SmartIF< ISvcLocator > & serviceLocator() const
Retrieve pointer to service locator.
Definition: Converter.cpp:92
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:68
virtual StatusCode writeData(TTree *rtree, INTuple *pObj)=0
Write N tuple data.
constexpr static const auto FAILURE
Definition: StatusCode.h:86
virtual StatusCode readObject(IOpaqueAddress *pAddr, DataObject *&refpObj)
Create the transient representation of an object.
Definition: RConverter.cpp:250
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:261
void reset(TYPE *ptr=nullptr)
Set the internal pointer to the passed one disposing of the old one.
Definition: SmartIF.h:86
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:54
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:120
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:192
StatusCode createObj(IOpaqueAddress *pAddr, DataObject *&refpObj) override
Create the transient representation of an object.
Definition: RNTupleCnv.cpp:85
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:40
StatusCode createAddress(DataObject *pObject, TDirectory *pDir, TObject *pTObject, IOpaqueAddress *&refpAddr)
Create address of the transient object according to the requested representation. ...
Definition: RConverter.cpp:155
virtual std::string rootVarType(int)
Return ROOT type info:
Definition: RNTupleCnv.cpp:200