Loading [MathJax]/extensions/tex2jax.js
The Gaudi Framework  v36r16 (ea80daf8)
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Modules Pages
HistoXML.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 // ============================================================================
12 // Include files
13 // ============================================================================
14 // STD&STL
15 // ============================================================================
16 #include <memory>
17 // ============================================================================
18 // ROOT
19 // ============================================================================
20 #include "TBufferXML.h"
21 #include "TH1.h"
22 #include "TH1D.h"
23 #include "TH1F.h"
24 #include "TH2D.h"
25 #include "TH2F.h"
26 #include "TH3D.h"
27 #include "TH3F.h"
28 #include "TProfile.h"
29 #include "TProfile2D.h"
30 // ============================================================================
31 // AIDA
32 // ============================================================================
33 #include "AIDA/IHistogram1D.h"
34 #include "AIDA/IHistogram2D.h"
35 #include "AIDA/IHistogram3D.h"
36 #include "AIDA/IProfile1D.h"
37 #include "AIDA/IProfile2D.h"
38 // ============================================================================
39 // local
40 // ============================================================================
41 #include "GaudiUtils/Aida2ROOT.h"
42 #include "GaudiUtils/HistoXML.h"
43 // ============================================================================
49 // ============================================================================
50 namespace {
51  // ==========================================================================
53  //
54  template <typename TYPE>
55  std::unique_ptr<TYPE> _Xml( const std::string& input ) {
56  // 1) use XML-parser
57  std::unique_ptr<TObject> obj{ TBufferXML::ConvertFromXML( input.c_str() ) };
58  TYPE* histo = ( obj ? dynamic_cast<TYPE*>( obj.get() ) : nullptr );
59  // slightly tricky: in case the dynamic cast succeeds, transfer owership
60  // by invoking 'release' on obj, and 'at the same time' pass 'histo' into
61  // the constructor of unique_ptr -- but if the cast fails, do NOT transfer
62  // ownership... the , operator is the closest I can think of
63  return std::unique_ptr<TYPE>{ histo ? ( obj.release(), histo ) : nullptr };
64  }
65  //
66  // ==========================================================================
67 } // end of anonymous namespace
68 // ============================================================================
69 /* stream the ROOT histogram into output stream as XML
70  * @param histo (INPUT) the histogram to be streamed
71  * @param stream (OUTPUT) the stream
72  */
73 // ============================================================================
75  return stream << TBufferXML::ConvertToXML( &histo );
76 }
77 // ============================================================================
78 /* stream the ROOT histogram into output stream as XML
79  * @param histo (INPUT) the histogram to be streamed
80  * @param stream (OUTPUT) the stream
81  */
82 // ============================================================================
84  return stream << TBufferXML::ConvertToXML( &histo );
85 }
86 // ============================================================================
87 /* stream the ROOT histogram into output stream as XML
88  * @param histo (INPUT) the histogram to be streamed
89  * @param stream (OUTPUT) the stream
90  */
91 // ============================================================================
93  return stream << TBufferXML::ConvertToXML( &histo );
94 }
95 // ============================================================================
96 /* stream the ROOT histogram into output stream as XML
97  * @param histo (INPUT) the histogram to be streamed
98  * @param stream (OUTPUT) the stream
99  */
100 // ============================================================================
102  return stream << TBufferXML::ConvertToXML( &histo );
103 }
104 // ============================================================================
105 /* stream the ROOT histogram into output stream as XML
106  * @param histo (INPUT) the histogram to be streamed
107  * @param stream (OUTPUT) the stream
108  */
109 // ============================================================================
111  return stream << TBufferXML::ConvertToXML( &histo );
112 }
113 // ============================================================================
114 /* stream the ROOT histogram into output stream as XML
115  * @param histo (INPUT) the histogram to be streamed
116  * @param stream (OUTPUT) the stream
117  */
118 // ============================================================================
120  return stream << TBufferXML::ConvertToXML( &histo );
121 }
122 // ============================================================================
123 /* stream the ROOT histogram into output stream as XML
124  * @param histo (INPUT) the histogram to be streamed
125  * @param stream (OUTPUT) the stream
126  */
127 // ============================================================================
129  return stream << TBufferXML::ConvertToXML( &histo );
130 }
131 // ============================================================================
132 /* stream the ROOT histogram into output stream as XML
133  * @param histo (INPUT) the histogram to be streamed
134  * @param stream (OUTPUT) the stream
135  */
136 // ============================================================================
138  return stream << TBufferXML::ConvertToXML( &histo );
139 }
140 // ============================================================================
141 /* stream the ROOT histogram into the output stream as XML
142  * @param histo (INPUT) the histogram to be streamed
143  * @param stream (OUTPUT) the stream
144  */
145 // ============================================================================
148  return root ? toXml( *root, stream ) : stream;
149 }
150 // ============================================================================
151 /* stream the ROOT histogram into the output stream as XML
152  * @param histo (INPUT) the histogram to be streamed
153  * @param stream (OUTPUT) the stream
154  */
155 // ============================================================================
158  return root ? toXml( *root, stream ) : stream;
159 }
160 // ============================================================================
161 /* stream the ROOT histogram into the output stream as XML
162  * @param histo (INPUT) the histogram to be streamed
163  * @param stream (OUTPUT) the stream
164  */
165 // ============================================================================
168  return root ? toXml( *root, stream ) : stream;
169 }
170 // ============================================================================
171 /* stream the ROOT histogram into the output stream as XML
172  * @param histo (INPUT) the histogram to be streamed
173  * @param stream (OUTPUT) the stream
174  */
175 // ============================================================================
178  return root ? toXml( *root, stream ) : stream;
179 }
180 // ============================================================================
181 /* stream the ROOT histogram into the output stream as XML
182  * @param histo (INPUT) the histogram to be streamed
183  * @param stream (OUTPUT) the stream
184  */
185 // ============================================================================
188  return root ? toXml( *root, stream ) : stream;
189 }
190 // ============================================================================
191 /* parse the histogram from standard ROOT XML
192  * @param result (OUTPUT) the parsed histogram
193  * @param input (INPUT) the input XML string
194  * @return status code
195  */
196 // ============================================================================
197 StatusCode Gaudi::Utils::Histos::fromXml( TH1D& result, const std::string& input ) {
198  //
199  result.Reset(); // RESET old histogram
200  //
201 
202  auto histo = _Xml<TH1D>( input );
203  if ( !histo ) { return StatusCode::FAILURE; } // RETURN
204  //
205  histo->Copy( result );
206  //
207  return StatusCode::SUCCESS;
208 }
209 // ============================================================================
210 /* parse the histogram from standard ROOT XML
211  * @param result (OUTPUT) the parsed histogram
212  * @param input (INPUT) the input XML string
213  * @return status code
214  */
215 // ============================================================================
216 StatusCode Gaudi::Utils::Histos::fromXml( TH2D& result, const std::string& input ) {
217  //
218  result.Reset(); // RESET old histogram
219  //
220  auto histo = _Xml<TH2D>( input );
221  if ( !histo ) { return StatusCode::FAILURE; } // RETURN
222  //
223  histo->Copy( result );
224  //
225  return StatusCode::SUCCESS;
226 }
227 // ============================================================================
228 /* parse the histogram from standard ROOT XML
229  * @param result (OUTPUT) the parsed histogram
230  * @param input (INPUT) the input XML string
231  * @return status code
232  */
233 // ============================================================================
234 StatusCode Gaudi::Utils::Histos::fromXml( TH3D& result, const std::string& input ) {
235  //
236  result.Reset(); // RESET old histogram
237  //
238  auto histo = _Xml<TH3D>( input );
239  if ( !histo ) { return StatusCode::FAILURE; } // RETURN
240  //
241  histo->Copy( result );
242  //
243  return StatusCode::SUCCESS;
244 }
245 // ============================================================================
246 /* parse the histogram from standard ROOT XML
247  * @param result (OUTPUT) the parsed histogram
248  * @param input (INPUT) the input XML string
249  * @return status code
250  */
251 // ============================================================================
252 StatusCode Gaudi::Utils::Histos::fromXml( TH1F& result, const std::string& input ) {
253  //
254  result.Reset(); // RESET old histogram
255  //
256  auto histo = _Xml<TH1F>( input );
257  if ( !histo ) { return StatusCode::FAILURE; } // RETURN
258  //
259  histo->Copy( result );
260  //
261  return StatusCode::SUCCESS;
262 }
263 // ============================================================================
264 /* parse the histogram from standard ROOT XML
265  * @param result (OUTPUT) the parsed histogram
266  * @param input (INPUT) the input XML string
267  * @return status code
268  */
269 // ============================================================================
270 StatusCode Gaudi::Utils::Histos::fromXml( TH2F& result, const std::string& input ) {
271  //
272  result.Reset(); // RESET old histogram
273  //
274  auto histo = _Xml<TH2F>( input );
275  if ( !histo ) { return StatusCode::FAILURE; } // RETURN
276  //
277  histo->Copy( result );
278  //
279  return StatusCode::SUCCESS;
280 }
281 // ============================================================================
282 /* parse the histogram from standard ROOT XML
283  * @param result (OUTPUT) the parsed histogram
284  * @param input (INPUT) the input XML string
285  * @return status code
286  */
287 // ============================================================================
288 StatusCode Gaudi::Utils::Histos::fromXml( TH3F& result, const std::string& input ) {
289  //
290  result.Reset(); // RESET old histogram
291  //
292  auto histo = _Xml<TH3F>( input );
293  if ( !histo ) { return StatusCode::FAILURE; } // RETURN
294  //
295  histo->Copy( result );
296  //
297  return StatusCode::SUCCESS;
298 }
299 // ============================================================================
300 /* parse the histogram from standard ROOT XML
301  * @param result (OUTPUT) the parsed histogram
302  * @param input (INPUT) the input XML string
303  * @return status code
304  */
305 // ============================================================================
306 StatusCode Gaudi::Utils::Histos::fromXml( TProfile& result, const std::string& input ) {
307  //
308  result.Reset(); // RESET old histogram
309  //
310  auto histo = _Xml<TProfile>( input );
311  if ( !histo ) { return StatusCode::FAILURE; } // RETURN
312  //
313  histo->Copy( result );
314  //
315  return StatusCode::SUCCESS;
316 }
317 // ============================================================================
318 /* parse the histogram from standard ROOT XML
319  * @param result (OUTPUT) the parsed histogram
320  * @param input (INPUT) the input XML string
321  * @return status code
322  */
323 // ============================================================================
324 StatusCode Gaudi::Utils::Histos::fromXml( TProfile2D& result, const std::string& input ) {
325  //
326  result.Reset(); // RESET old histogram
327  //
328  auto histo = _Xml<TProfile2D>( input );
329  if ( !histo ) { return StatusCode::FAILURE; } // RETURN
330  //
331  histo->Copy( result );
332  //
333  return StatusCode::SUCCESS;
334 }
335 
336 // ============================================================================
337 /* parse the histogram from standard ROOT XML
338  * @param result (OUTPUT) the parsed histogram
339  * @param input (INPUT) the input XML string
340  * @return status code
341  */
342 // ============================================================================
343 StatusCode Gaudi::Utils::Histos::fromXml( TH1D*& result, const std::string& input ) {
344  if ( result ) { return fromXml( *result, input ); }
345  //
346  auto histo = _Xml<TH1D>( input );
347  if ( !histo ) { return StatusCode::FAILURE; } // RETURN
348  //
349  result = histo.release(); // ASSIGN
350  //
351  return StatusCode::SUCCESS;
352 }
353 // ============================================================================
354 /* parse the histogram from standard ROOT XML
355  * @param result (OUTPUT) the parsed histogram
356  * @param input (INPUT) the input XML string
357  * @return status code
358  */
359 // ============================================================================
360 StatusCode Gaudi::Utils::Histos::fromXml( TH2D*& result, const std::string& input ) {
361  if ( result ) { return fromXml( *result, input ); }
362  //
363  auto histo = _Xml<TH2D>( input );
364  if ( !histo ) { return StatusCode::FAILURE; } // RETURN
365  //
366  result = histo.release(); // ASSIGN
367  //
368  return StatusCode::SUCCESS;
369 }
370 // ============================================================================
371 /* parse the histogram from standard ROOT XML
372  * @param result (OUTPUT) the parsed histogram
373  * @param input (INPUT) the input XML string
374  * @return status code
375  */
376 // ============================================================================
377 StatusCode Gaudi::Utils::Histos::fromXml( TH3D*& result, const std::string& input ) {
378  if ( result ) { return fromXml( *result, input ); }
379  //
380  auto histo = _Xml<TH3D>( input );
381  if ( !histo ) { return StatusCode::FAILURE; } // RETURN
382  //
383  result = histo.release(); // ASSIGN
384  //
385  return StatusCode::SUCCESS;
386 }
387 
388 // ============================================================================
389 /* parse the histogram from standard ROOT XML
390  * @param result (OUTPUT) the parsed histogram
391  * @param input (INPUT) the input XML string
392  * @return status code
393  */
394 // ============================================================================
395 StatusCode Gaudi::Utils::Histos::fromXml( TProfile*& result, const std::string& input ) {
396  if ( result ) { return fromXml( *result, input ); }
397  //
398  auto histo = _Xml<TProfile>( input );
399  if ( !histo ) { return StatusCode::FAILURE; } // RETURN
400  //
401  result = histo.release(); // ASSIGN
402  //
403  return StatusCode::SUCCESS;
404 }
405 // ============================================================================
406 /* parse the histogram from standard ROOT XML
407  * @param result (OUTPUT) the parsed histogram
408  * @param input (INPUT) the input XML string
409  * @return status code
410  */
411 // ============================================================================
412 StatusCode Gaudi::Utils::Histos::fromXml( TProfile2D*& result, const std::string& input ) {
413  if ( result ) { return fromXml( *result, input ); }
414  //
415  auto histo = _Xml<TProfile2D>( input );
416  if ( !histo ) { return StatusCode::FAILURE; } // RETURN
417  //
418  result = histo.release(); // ASSIGN
419  //
420  return StatusCode::SUCCESS;
421 }
422 // ============================================================================
423 /* parse the histogram from standard ROOT XML
424  * @param result (OUTPUT) the parsed histogram
425  * @param input (INPUT) the input XML string
426  * @return status code
427  */
428 // ============================================================================
429 StatusCode Gaudi::Utils::Histos::fromXml( AIDA::IHistogram1D& result, const std::string& input ) {
430  auto root = Gaudi::Utils::Aida2ROOT::aida2root( &result );
431  return root ? fromXml( *root, input ) : StatusCode::FAILURE; // RETURN
432 }
433 // ============================================================================
434 /* parse the histogram from standard ROOT XML
435  * @param result (OUTPUT) the parsed histogram
436  * @param input (INPUT) the input XML string
437  * @return status code
438  */
439 // ============================================================================
440 StatusCode Gaudi::Utils::Histos::fromXml( AIDA::IHistogram2D& result, const std::string& input ) {
441  auto root = Gaudi::Utils::Aida2ROOT::aida2root( &result );
442  return root ? fromXml( *root, input ) : StatusCode::FAILURE; // RETURN
443 }
444 // ============================================================================
445 /* parse the histogram from standard ROOT XML
446  * @param result (OUTPUT) the parsed histogram
447  * @param input (INPUT) the input XML string
448  * @return status code
449  */
450 // ============================================================================
451 StatusCode Gaudi::Utils::Histos::fromXml( AIDA::IHistogram3D& result, const std::string& input ) {
452  auto root = Gaudi::Utils::Aida2ROOT::aida2root( &result );
453  return root ? fromXml( *root, input ) : StatusCode::FAILURE; // RETURN
454 }
455 // ============================================================================
456 /* parse the histogram from standard ROOT XML
457  * @param result (OUTPUT) the parsed histogram
458  * @param input (INPUT) the input XML string
459  * @return status code
460  */
461 // ============================================================================
462 StatusCode Gaudi::Utils::Histos::fromXml( AIDA::IProfile1D& result, const std::string& input ) {
463  auto root = Gaudi::Utils::Aida2ROOT::aida2root( &result );
464  return root ? fromXml( *root, input ) : StatusCode::FAILURE; // RETURN
465 }
466 // ============================================================================
467 /* parse the histogram from standard ROOT XML
468  * @param result (OUTPUT) the parsed histogram
469  * @param input (INPUT) the input XML string
470  * @return status code
471  */
472 // ============================================================================
473 StatusCode Gaudi::Utils::Histos::fromXml( AIDA::IProfile2D& result, const std::string& input ) {
474  auto root = Gaudi::Utils::Aida2ROOT::aida2root( &result );
475  return root ? fromXml( *root, input ) : StatusCode::FAILURE; // RETURN
476 }
477 // ============================================================================
478 // The END
479 // ============================================================================
Write.stream
stream
Definition: Write.py:32
std::string
STL class.
Aida2ROOT.h
HistoEx.histo
histo
Definition: HistoEx.py:103
gaudiComponentHelp.root
root
Definition: gaudiComponentHelp.py:43
Gaudi::Utils::Aida2ROOT::aida2root
static TH1D * aida2root(AIDA::IHistogram1D *aida)
get the underlying pointer for 1D-histogram
Definition: Aida2ROOT.cpp:68
StatusCode
Definition: StatusCode.h:65
std::ostream
STL class.
std::string::c_str
T c_str(T... args)
StatusCode::SUCCESS
constexpr static const auto SUCCESS
Definition: StatusCode.h:100
StatusCode::FAILURE
constexpr static const auto FAILURE
Definition: StatusCode.h:101
std::unique_ptr
STL class.
HistoXML.h
Gaudi::Utils::Histos::fromXml
GAUDI_API StatusCode fromXml(TH1D &result, const std::string &input)
parse the histogram from standard ROOT XML
Definition: HistoXML.cpp:197
Gaudi::Utils::Histos::toXml
GAUDI_API std::ostream & toXml(const TH1D &histo, std::ostream &stream)
stream the ROOT histogram into output stream as XML
Definition: HistoXML.cpp:74