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