The Gaudi Framework  v30r3 (a5ef0a68)
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 {
42  // ==========================================================================
44  //
45  template <typename TYPE>
46  std::unique_ptr<TYPE> _Xml( const std::string& input )
47  {
48  // 1) use XML-parser
49  std::unique_ptr<TObject> obj{TBufferXML::ConvertFromXML( input.c_str() )};
50  TYPE* histo = ( obj ? dynamic_cast<TYPE*>( obj.get() ) : nullptr );
51  // slightly tricky: in case the dynamic cast succeeds, transfer owership
52  // by invoking 'release' on obj, and 'at the same time' pass 'histo' into
53  // the constructor of unique_ptr -- but if the cast fails, do NOT transfer
54  // ownership... the , operator is the closest I can think of
55  return std::unique_ptr<TYPE>{histo ? ( obj.release(), histo ) : nullptr};
56  }
57  //
58  // ==========================================================================
59 } // end of anonymous namespace
60 // ============================================================================
61 /* stream the ROOT histogram into output stream as XML
62  * @param histo (INPUT) the histogram to be streamed
63  * @param stream (OUTPUT) the stream
64  */
65 // ============================================================================
67 {
68  return stream << TBufferXML::ConvertToXML( &histo );
69 }
70 // ============================================================================
71 /* stream the ROOT histogram into output stream as XML
72  * @param histo (INPUT) the histogram to be streamed
73  * @param stream (OUTPUT) the stream
74  */
75 // ============================================================================
77 {
78  return stream << TBufferXML::ConvertToXML( &histo );
79 }
80 // ============================================================================
81 /* stream the ROOT histogram into output stream as XML
82  * @param histo (INPUT) the histogram to be streamed
83  * @param stream (OUTPUT) the stream
84  */
85 // ============================================================================
87 {
88  return stream << TBufferXML::ConvertToXML( &histo );
89 }
90 // ============================================================================
91 /* stream the ROOT histogram into output stream as XML
92  * @param histo (INPUT) the histogram to be streamed
93  * @param stream (OUTPUT) the stream
94  */
95 // ============================================================================
97 {
98  return stream << TBufferXML::ConvertToXML( &histo );
99 }
100 // ============================================================================
101 /* stream the ROOT histogram into output stream as XML
102  * @param histo (INPUT) the histogram to be streamed
103  * @param stream (OUTPUT) the stream
104  */
105 // ============================================================================
107 {
108  return stream << TBufferXML::ConvertToXML( &histo );
109 }
110 // ============================================================================
111 /* stream the ROOT histogram into output stream as XML
112  * @param histo (INPUT) the histogram to be streamed
113  * @param stream (OUTPUT) the stream
114  */
115 // ============================================================================
117 {
118  return stream << TBufferXML::ConvertToXML( &histo );
119 }
120 // ============================================================================
121 /* stream the ROOT histogram into output stream as XML
122  * @param histo (INPUT) the histogram to be streamed
123  * @param stream (OUTPUT) the stream
124  */
125 // ============================================================================
126 std::ostream& Gaudi::Utils::Histos::toXml( const TProfile& histo, std::ostream& stream )
127 {
128  return stream << TBufferXML::ConvertToXML( &histo );
129 }
130 // ============================================================================
131 /* stream the ROOT histogram into 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 TProfile2D& histo, std::ostream& stream )
137 {
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 // ============================================================================
146 std::ostream& Gaudi::Utils::Histos::toXml( const AIDA::IHistogram1D& histo, std::ostream& stream )
147 {
148  auto root = Gaudi::Utils::Aida2ROOT::aida2root( &histo );
149  return root ? toXml( *root, stream ) : stream;
150 }
151 // ============================================================================
152 /* stream the ROOT histogram into the output stream as XML
153  * @param histo (INPUT) the histogram to be streamed
154  * @param stream (OUTPUT) the stream
155  */
156 // ============================================================================
157 std::ostream& Gaudi::Utils::Histos::toXml( const AIDA::IHistogram2D& histo, std::ostream& stream )
158 {
159  auto root = Gaudi::Utils::Aida2ROOT::aida2root( &histo );
160  return root ? toXml( *root, stream ) : stream;
161 }
162 // ============================================================================
163 /* stream the ROOT histogram into the output stream as XML
164  * @param histo (INPUT) the histogram to be streamed
165  * @param stream (OUTPUT) the stream
166  */
167 // ============================================================================
168 std::ostream& Gaudi::Utils::Histos::toXml( const AIDA::IHistogram3D& histo, std::ostream& stream )
169 {
170  auto root = Gaudi::Utils::Aida2ROOT::aida2root( &histo );
171  return root ? toXml( *root, stream ) : stream;
172 }
173 // ============================================================================
174 /* stream the ROOT histogram into the output stream as XML
175  * @param histo (INPUT) the histogram to be streamed
176  * @param stream (OUTPUT) the stream
177  */
178 // ============================================================================
179 std::ostream& Gaudi::Utils::Histos::toXml( const AIDA::IProfile1D& histo, std::ostream& stream )
180 {
181  auto root = Gaudi::Utils::Aida2ROOT::aida2root( &histo );
182  return root ? toXml( *root, stream ) : stream;
183 }
184 // ============================================================================
185 /* stream the ROOT histogram into the output stream as XML
186  * @param histo (INPUT) the histogram to be streamed
187  * @param stream (OUTPUT) the stream
188  */
189 // ============================================================================
190 std::ostream& Gaudi::Utils::Histos::toXml( const AIDA::IProfile2D& histo, std::ostream& stream )
191 {
192  auto root = Gaudi::Utils::Aida2ROOT::aida2root( &histo );
193  return root ? toXml( *root, stream ) : stream;
194 }
195 // ============================================================================
196 /* parse the histogram from standard ROOT XML
197  * @param result (OUTPUT) the parsed histogram
198  * @param input (INPUT) the input XML string
199  * @return status code
200  */
201 // ============================================================================
203 {
204  //
205  result.Reset(); // RESET old histogram
206  //
207 
208  auto histo = _Xml<TH1D>( input );
209  if ( !histo ) {
210  return StatusCode::FAILURE;
211  } // 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 // ============================================================================
225 {
226  //
227  result.Reset(); // RESET old histogram
228  //
229  auto histo = _Xml<TH2D>( input );
230  if ( !histo ) {
231  return StatusCode::FAILURE;
232  } // RETURN
233  //
234  histo->Copy( result );
235  //
236  return StatusCode::SUCCESS;
237 }
238 // ============================================================================
239 /* parse the histogram from standard ROOT XML
240  * @param result (OUTPUT) the parsed histogram
241  * @param input (INPUT) the input XML string
242  * @return status code
243  */
244 // ============================================================================
246 {
247  //
248  result.Reset(); // RESET old histogram
249  //
250  auto histo = _Xml<TH3D>( input );
251  if ( !histo ) {
252  return StatusCode::FAILURE;
253  } // RETURN
254  //
255  histo->Copy( result );
256  //
257  return StatusCode::SUCCESS;
258 }
259 // ============================================================================
260 /* parse the histogram from standard ROOT XML
261  * @param result (OUTPUT) the parsed histogram
262  * @param input (INPUT) the input XML string
263  * @return status code
264  */
265 // ============================================================================
267 {
268  //
269  result.Reset(); // RESET old histogram
270  //
271  auto histo = _Xml<TH1F>( input );
272  if ( !histo ) {
273  return StatusCode::FAILURE;
274  } // RETURN
275  //
276  histo->Copy( result );
277  //
278  return StatusCode::SUCCESS;
279 }
280 // ============================================================================
281 /* parse the histogram from standard ROOT XML
282  * @param result (OUTPUT) the parsed histogram
283  * @param input (INPUT) the input XML string
284  * @return status code
285  */
286 // ============================================================================
288 {
289  //
290  result.Reset(); // RESET old histogram
291  //
292  auto histo = _Xml<TH2F>( input );
293  if ( !histo ) {
294  return StatusCode::FAILURE;
295  } // RETURN
296  //
297  histo->Copy( result );
298  //
299  return StatusCode::SUCCESS;
300 }
301 // ============================================================================
302 /* parse the histogram from standard ROOT XML
303  * @param result (OUTPUT) the parsed histogram
304  * @param input (INPUT) the input XML string
305  * @return status code
306  */
307 // ============================================================================
309 {
310  //
311  result.Reset(); // RESET old histogram
312  //
313  auto histo = _Xml<TH3F>( input );
314  if ( !histo ) {
315  return StatusCode::FAILURE;
316  } // RETURN
317  //
318  histo->Copy( result );
319  //
320  return StatusCode::SUCCESS;
321 }
322 // ============================================================================
323 /* parse the histogram from standard ROOT XML
324  * @param result (OUTPUT) the parsed histogram
325  * @param input (INPUT) the input XML string
326  * @return status code
327  */
328 // ============================================================================
329 StatusCode Gaudi::Utils::Histos::fromXml( TProfile& result, const std::string& input )
330 {
331  //
332  result.Reset(); // RESET old histogram
333  //
334  auto histo = _Xml<TProfile>( input );
335  if ( !histo ) {
336  return StatusCode::FAILURE;
337  } // RETURN
338  //
339  histo->Copy( result );
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( TProfile2D& result, const std::string& input )
351 {
352  //
353  result.Reset(); // RESET old histogram
354  //
355  auto histo = _Xml<TProfile2D>( input );
356  if ( !histo ) {
357  return StatusCode::FAILURE;
358  } // RETURN
359  //
360  histo->Copy( result );
361  //
362  return StatusCode::SUCCESS;
363 }
364 
365 // ============================================================================
366 /* parse the histogram from standard ROOT XML
367  * @param result (OUTPUT) the parsed histogram
368  * @param input (INPUT) the input XML string
369  * @return status code
370  */
371 // ============================================================================
373 {
374  if ( result ) {
375  return fromXml( *result, input );
376  }
377  //
378  auto histo = _Xml<TH1D>( input );
379  if ( !histo ) {
380  return StatusCode::FAILURE;
381  } // RETURN
382  //
383  result = histo.release(); // ASSIGN
384  //
385  return StatusCode::SUCCESS;
386 }
387 // ============================================================================
388 /* parse the histogram from standard ROOT XML
389  * @param result (OUTPUT) the parsed histogram
390  * @param input (INPUT) the input XML string
391  * @return status code
392  */
393 // ============================================================================
395 {
396  if ( result ) {
397  return fromXml( *result, input );
398  }
399  //
400  auto histo = _Xml<TH2D>( input );
401  if ( !histo ) {
402  return StatusCode::FAILURE;
403  } // RETURN
404  //
405  result = histo.release(); // ASSIGN
406  //
407  return StatusCode::SUCCESS;
408 }
409 // ============================================================================
410 /* parse the histogram from standard ROOT XML
411  * @param result (OUTPUT) the parsed histogram
412  * @param input (INPUT) the input XML string
413  * @return status code
414  */
415 // ============================================================================
417 {
418  if ( result ) {
419  return fromXml( *result, input );
420  }
421  //
422  auto histo = _Xml<TH3D>( input );
423  if ( !histo ) {
424  return StatusCode::FAILURE;
425  } // RETURN
426  //
427  result = histo.release(); // ASSIGN
428  //
429  return StatusCode::SUCCESS;
430 }
431 
432 // ============================================================================
433 /* parse the histogram from standard ROOT XML
434  * @param result (OUTPUT) the parsed histogram
435  * @param input (INPUT) the input XML string
436  * @return status code
437  */
438 // ============================================================================
439 StatusCode Gaudi::Utils::Histos::fromXml( TProfile*& result, const std::string& input )
440 {
441  if ( result ) {
442  return fromXml( *result, input );
443  }
444  //
445  auto histo = _Xml<TProfile>( input );
446  if ( !histo ) {
447  return StatusCode::FAILURE;
448  } // RETURN
449  //
450  result = histo.release(); // ASSIGN
451  //
452  return StatusCode::SUCCESS;
453 }
454 // ============================================================================
455 /* parse the histogram from standard ROOT XML
456  * @param result (OUTPUT) the parsed histogram
457  * @param input (INPUT) the input XML string
458  * @return status code
459  */
460 // ============================================================================
461 StatusCode Gaudi::Utils::Histos::fromXml( TProfile2D*& result, const std::string& input )
462 {
463  if ( result ) {
464  return fromXml( *result, input );
465  }
466  //
467  auto histo = _Xml<TProfile2D>( input );
468  if ( !histo ) {
469  return StatusCode::FAILURE;
470  } // RETURN
471  //
472  result = histo.release(); // ASSIGN
473  //
474  return StatusCode::SUCCESS;
475 }
476 // ============================================================================
477 /* parse the histogram from standard ROOT XML
478  * @param result (OUTPUT) the parsed histogram
479  * @param input (INPUT) the input XML string
480  * @return status code
481  */
482 // ============================================================================
483 StatusCode Gaudi::Utils::Histos::fromXml( AIDA::IHistogram1D& result, const std::string& input )
484 {
485  auto root = Gaudi::Utils::Aida2ROOT::aida2root( &result );
486  return root ? fromXml( *root, input ) : StatusCode::FAILURE; // RETURN
487 }
488 // ============================================================================
489 /* parse the histogram from standard ROOT XML
490  * @param result (OUTPUT) the parsed histogram
491  * @param input (INPUT) the input XML string
492  * @return status code
493  */
494 // ============================================================================
495 StatusCode Gaudi::Utils::Histos::fromXml( AIDA::IHistogram2D& result, const std::string& input )
496 {
497  auto root = Gaudi::Utils::Aida2ROOT::aida2root( &result );
498  return root ? fromXml( *root, input ) : StatusCode::FAILURE; // RETURN
499 }
500 // ============================================================================
501 /* parse the histogram from standard ROOT XML
502  * @param result (OUTPUT) the parsed histogram
503  * @param input (INPUT) the input XML string
504  * @return status code
505  */
506 // ============================================================================
507 StatusCode Gaudi::Utils::Histos::fromXml( AIDA::IHistogram3D& result, const std::string& input )
508 {
509  auto root = Gaudi::Utils::Aida2ROOT::aida2root( &result );
510  return root ? fromXml( *root, input ) : StatusCode::FAILURE; // RETURN
511 }
512 // ============================================================================
513 /* parse the histogram from standard ROOT XML
514  * @param result (OUTPUT) the parsed histogram
515  * @param input (INPUT) the input XML string
516  * @return status code
517  */
518 // ============================================================================
519 StatusCode Gaudi::Utils::Histos::fromXml( AIDA::IProfile1D& result, const std::string& input )
520 {
521  auto root = Gaudi::Utils::Aida2ROOT::aida2root( &result );
522  return root ? fromXml( *root, input ) : StatusCode::FAILURE; // RETURN
523 }
524 // ============================================================================
525 /* parse the histogram from standard ROOT XML
526  * @param result (OUTPUT) the parsed histogram
527  * @param input (INPUT) the input XML string
528  * @return status code
529  */
530 // ============================================================================
531 StatusCode Gaudi::Utils::Histos::fromXml( AIDA::IProfile2D& result, const std::string& input )
532 {
533  auto root = Gaudi::Utils::Aida2ROOT::aida2root( &result );
534  return root ? fromXml( *root, input ) : StatusCode::FAILURE; // RETURN
535 }
536 // ============================================================================
537 // The END
538 // ============================================================================
constexpr static const auto FAILURE
Definition: StatusCode.h:88
GAUDI_API StatusCode fromXml(TH1D &result, const std::string &input)
parse the histogram from standard ROOT XML
Definition: HistoXML.cpp:202
STL class.
This class is used for returning status codes from appropriate routines.
Definition: StatusCode.h:51
static TH1D * aida2root(AIDA::IHistogram1D *aida)
get the underlying pointer for 1D-histogram
Definition: Aida2ROOT.cpp:73
constexpr static const auto SUCCESS
Definition: StatusCode.h:87
GAUDI_API std::ostream & toXml(const TH1D &histo, std::ostream &stream)
stream the ROOT histogram into output stream as XML
Definition: HistoXML.cpp:66
STL class.
T c_str(T...args)
STL class.