| The Gaudi Framework v32r1 (f65d50dc)

RNTupleCnv.cpp
Go to the documentation of this file.
1 // Include files
2 #include "RNTupleCnv.h"
3 #include "RootObjAddress.h"
4
5 #include "GaudiKernel/DataObject.h"
6 #include "GaudiKernel/IDataManagerSvc.h"
7 #include "GaudiKernel/IDataProviderSvc.h"
8 #include "GaudiKernel/INTupleSvc.h"
9 #include "GaudiKernel/IOpaqueAddress.h"
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 //-----------------------------------------------------------------------------
39 StatusCode RootHistCnv::RNTupleCnv::initialize() {
40 //-----------------------------------------------------------------------------
41 StatusCode status = Converter::initialize();
42 if ( status.isSuccess() ) {
43 m_ntupleSvc = serviceLocator()->service( "NTupleSvc" );
44 if ( !m_ntupleSvc ) status = StatusCode::FAILURE;
45 }
46 return status;
47 }
48
49 //-----------------------------------------------------------------------------
51 StatusCode RootHistCnv::RNTupleCnv::finalize() {
52 //-----------------------------------------------------------------------------
53 m_ntupleSvc.reset();
54 return Converter::finalize();
55 }
56
57 //-----------------------------------------------------------------------------
59 StatusCode RootHistCnv::RNTupleCnv::updateObj( IOpaqueAddress* pAddress, DataObject* pObject )
60 //-----------------------------------------------------------------------------
61 {
62 MsgStream log( msgSvc(), "RNTupleCnv" );
63
64 StatusCode status = StatusCode::FAILURE;
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 //-----------------------------------------------------------------------------
85 StatusCode RootHistCnv::RNTupleCnv::createObj( IOpaqueAddress* pAddress, DataObject*& refpObject )
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 //-----------------------------------------------------------------------------
120 StatusCode RootHistCnv::RNTupleCnv::createRep( DataObject* pObject, IOpaqueAddress*& pAddr )
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 //-----------------------------------------------------------------------------
172 StatusCode RootHistCnv::RNTupleCnv::updateRep( IOpaqueAddress* pAddr, DataObject* pObj )
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 //-----------------------------------------------------------------------------
200 std::string RootHistCnv::RNTupleCnv::rootVarType( int type ) {
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
211 case DataTypeInfo::LONGLONG:
212 return "/L"; // long long
213 case DataTypeInfo::USHORT:
214 return "/s"; // ushort
215 case DataTypeInfo::UINT:
216 return "/i"; // uint
217 case DataTypeInfo::ULONG:
218 return "/i"; // ulong
219 case DataTypeInfo::ULONGLONG:
220 return "/l"; // unsigned long long
221 case DataTypeInfo::FLOAT:
222 return "/F"; // float
223 case DataTypeInfo::DOUBLE:
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 //-----------------------------------------------------------------------------
236 bool RootHistCnv::parseName( const std::string& full, std::string& blk, std::string& var ) {
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
RootHistCnv::RConverter
Root Converter.
Definition: RConverter.h:31
IOpaqueAddress::par
virtual const std::string * par() const =0
Retrieve String parameters.
MsgStream
Definition of the MsgStream class used to transmit messages.
Definition: MsgStream.h:24
ISvcLocator
The ISvcLocator is the interface implemented by the Service Factory in the Application Manager to loc...
Definition: ISvcLocator.h:25
DataObject::registry
IRegistry * registry() const
Get pointer to Registry.
Definition: DataObject.h:72
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:87
DataTypeInfo::INT
Definition: DataTypeInfo.h:31
RootHistCnv::RootObjAddress::tObj
virtual TObject * tObj() const
Retrieve TObject* ptr.
Definition: RootObjAddress.h:81
MsgStream.h
RootHistCnv::RNTupleCnv::RNTupleCnv
RNTupleCnv(ISvcLocator *svc, const CLID &clid)
Standard constructor.
Definition: RNTupleCnv.cpp:34
RootHistCnv::RNTupleCnv::updateRep
StatusCode updateRep(IOpaqueAddress *pAddr, DataObject *pObj) override
Update the converted representation of a transient object.
Definition: RNTupleCnv.cpp:172
IDataManagerSvc
Definition: IDataManagerSvc.h:45
compareOutputFiles.sp
sp
Definition: compareOutputFiles.py:494
DataTypeInfo::CHAR
Definition: DataTypeInfo.h:29
NTuple.h
RootHistCnv::parseName
bool parseName(const std::string &full, std::string &blk, std::string &var)
Definition: RNTupleCnv.cpp:236
min
EventIDBase min(const EventIDBase &lhs, const EventIDBase &rhs)
Definition: EventIDBase.h:202
Converter::initialize
StatusCode initialize() override
Initialize the converter.
Definition: Converter.cpp:43
NTuple::Range::min
static TYP min()
Minimal number of data.
Definition: NTuple.h:82
StatusCode::SUCCESS
constexpr static const auto SUCCESS
Definition: StatusCode.h:85
RootHistCnv::RNTupleCnv::initialize
StatusCode initialize() override
Initialize the converter.
Definition: RNTupleCnv.cpp:39
IRegistry::name
virtual const name_type & name() const =0
Name of the directory (or key)
RootHistCnv::RootObjAddress
Definition: RootObjAddress.h:11
DataTypeInfo::SHORT
Definition: DataTypeInfo.h:30
max
EventIDBase max(const EventIDBase &lhs, const EventIDBase &rhs)
Definition: EventIDBase.h:215
boost::spirit
Definition: iter_pos.hpp:14
RootHistCnv::RNTupleCnv::finalize
StatusCode finalize() override
Finalize the converter.
Definition: RNTupleCnv.cpp:51
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:33
AvalancheSchedulerErrorTest.msgSvc
msgSvc
Definition: AvalancheSchedulerErrorTest.py:67
std::string
STL class.
INTuple
NTuple interface class definition.
Definition: INTuple.h:81
Converter::finalize
StatusCode finalize() override
Initialize the converter.
Definition: Converter.cpp:50
INTupleItem
NTuple interface class definition.
Definition: INTuple.h:27
IDataManagerSvc.h
IDataProviderSvc.h
IOpaqueAddress::registry
virtual IRegistry * registry() const =0
Update branch name.
RootHistCnv::RNTupleCnv::updateObj
StatusCode updateObj(IOpaqueAddress *pAddr, DataObject *refpObj) override
Update the transient object from the other representation.
Definition: RNTupleCnv.cpp:59
DataTypeInfo::UINT
Definition: DataTypeInfo.h:27
DataTypeInfo::USHORT
Definition: DataTypeInfo.h:26
StatusCode
This class is used for returning status codes from appropriate routines.
Definition: StatusCode.h:50
gaudirun.type
type
Definition: gaudirun.py:142
DataTypeInfo::BOOL
Definition: DataTypeInfo.h:33
SmartIF.h
DataTypeInfo::UCHAR
Definition: DataTypeInfo.h:25
NTuple::Range::max
static TYP max()
Maximal number of data.
Definition: NTuple.h:84
RootHistCnv
Definition: DirectoryCnv.h:17
IRegistry.h
IRegistry
The IRegistry represents the entry door to the environment any data object residing in a transient da...
Definition: IRegistry.h:22
DataTypeInfo::ULONG
Definition: DataTypeInfo.h:28
DataTypeInfo::LONG
Definition: DataTypeInfo.h:32
CLID
unsigned int CLID
Class ID definition.
Definition: ClassID.h:8
StatusCode::isSuccess
bool isSuccess() const
Definition: StatusCode.h:267
DataTypeInfo::DOUBLE
Definition: DataTypeInfo.h:35
IRegistry::address
virtual IOpaqueAddress * address() const =0
Retrieve opaque storage address.
DataTypeInfo::ULONGLONG
Definition: DataTypeInfo.h:45
DataTypeInfo::LONGLONG
Definition: DataTypeInfo.h:44
IOpaqueAddress.h
RootHistCnv::INSTANTIATE
INSTANTIATE(float)
RootObjAddress.h
RNTupleCnv.h
GlobalDirectoryRestore
Definition: RConverter.h:16
StatusCode::FAILURE
constexpr static const auto FAILURE
Definition: StatusCode.h:86
MSG::ERROR
Definition: IMessageSvc.h:15
std::string::substr
T substr(T... args)
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:261
MSG::WARNING
Definition: IMessageSvc.h:15
IRegistry::identifier
virtual const id_type & identifier() const =0
Full identifier (or key)
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:26
DataTypeInfo::FLOAT
Definition: DataTypeInfo.h:34
IOpaqueAddress
Opaque address interface definition.
Definition: IOpaqueAddress.h:23
DataObject
A DataObject is the base class of any identifiable object on any data store.
Definition: DataObject.h:30
IOpaqueAddress::ipar
virtual const unsigned long * ipar() const =0
Access to generic link parameters.
RootHistCnv::RNTupleCnv::createRep
StatusCode createRep(DataObject *pObj, IOpaqueAddress *&refpAddr) override
Convert the transient object to the requested representation.
Definition: RNTupleCnv.cpp:120
endmsg
MsgStream & endmsg(MsgStream &s)
MsgStream Modifier: endmsg. Calls the output method of the MsgStream.
Definition: MsgStream.h:192
RootHistCnv::RNTupleCnv::createObj
StatusCode createObj(IOpaqueAddress *pAddr, DataObject *&refpObj) override
Create the transient representation of an object.
Definition: RNTupleCnv.cpp:85
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:40
DataObject.h
RootHistCnv::RNTupleCnv::rootVarType
virtual std::string rootVarType(int)
Return ROOT type info:
Definition: RNTupleCnv.cpp:200
Gaudi.Configuration.log
log
Definition: Configuration.py:13
INTupleSvc.h