| # Gaudi Framework, version v25r0 |

Home |  | Generated: Mon Feb 17 2014

- Main Page
- Related Pages
- Modules
- Namespaces
- Classes
- Files

- File List
- File Members

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