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 "TH1.h"
11 #include "TH1D.h"
12 #include "TH2D.h"
13 #include "TH3D.h"
14 #include "TH1F.h"
15 #include "TH2F.h"
16 #include "TH3F.h"
17 #include "TProfile.h"
18 #include "TProfile2D.h"
19 #include "TBufferXML.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 ( const TH1D& histo ,
68  std::ostream& stream )
69 {
70  return stream << TBufferXML::ConvertToXML( &histo ) ;
71 }
72 // ============================================================================
73 /* stream the ROOT histogram into output stream as XML
74  * @param histo (INPUT) the histogram to be streamed
75  * @param stream (OUTPUT) the stream
76  */
77 // ============================================================================
79 ( const TH2D& histo ,
80  std::ostream& stream )
81 {
82  return stream << TBufferXML::ConvertToXML( &histo ) ;
83 }
84 // ============================================================================
85 /* stream the ROOT histogram into output stream as XML
86  * @param histo (INPUT) the histogram to be streamed
87  * @param stream (OUTPUT) the stream
88  */
89 // ============================================================================
91 ( const TH3D& histo ,
92  std::ostream& stream )
93 {
94  return stream << TBufferXML::ConvertToXML( &histo ) ;
95 }
96 // ============================================================================
97 /* stream the ROOT histogram into output stream as XML
98  * @param histo (INPUT) the histogram to be streamed
99  * @param stream (OUTPUT) the stream
100  */
101 // ============================================================================
103 ( const TH1F& histo ,
104  std::ostream& stream )
105 {
106  return stream << TBufferXML::ConvertToXML( &histo ) ;
107 }
108 // ============================================================================
109 /* stream the ROOT histogram into output stream as XML
110  * @param histo (INPUT) the histogram to be streamed
111  * @param stream (OUTPUT) the stream
112  */
113 // ============================================================================
115 ( const TH2F& histo ,
116  std::ostream& stream )
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 // ============================================================================
127 ( const TH3F& histo ,
128  std::ostream& stream )
129 {
130  return stream << TBufferXML::ConvertToXML( &histo ) ;
131 }
132 // ============================================================================
133 /* stream the ROOT histogram into output stream as XML
134  * @param histo (INPUT) the histogram to be streamed
135  * @param stream (OUTPUT) the stream
136  */
137 // ============================================================================
139 ( const TProfile& histo ,
140  std::ostream& stream )
141 {
142  return stream << TBufferXML::ConvertToXML( &histo ) ;
143 }
144 // ============================================================================
145 /* stream the ROOT histogram into output stream as XML
146  * @param histo (INPUT) the histogram to be streamed
147  * @param stream (OUTPUT) the stream
148  */
149 // ============================================================================
151 ( const TProfile2D& histo ,
152  std::ostream& stream )
153 {
154  return stream << TBufferXML::ConvertToXML ( &histo ) ;
155 }
156 // ============================================================================
157 /* stream the ROOT histogram into the output stream as XML
158  * @param histo (INPUT) the histogram to be streamed
159  * @param stream (OUTPUT) the stream
160  */
161 // ============================================================================
163 ( const AIDA::IHistogram1D& histo ,
164  std::ostream& stream )
165 {
166  auto root = Gaudi::Utils::Aida2ROOT::aida2root ( &histo ) ;
167  return root ? toXml( *root , stream ) : stream;
168 }
169 // ============================================================================
170 /* stream the ROOT histogram into the output stream as XML
171  * @param histo (INPUT) the histogram to be streamed
172  * @param stream (OUTPUT) the stream
173  */
174 // ============================================================================
176 ( const AIDA::IHistogram2D& histo ,
177  std::ostream& stream )
178 {
179  auto root = Gaudi::Utils::Aida2ROOT::aida2root ( &histo ) ;
180  return root ? toXml( *root , stream ) : stream;
181 }
182 // ============================================================================
183 /* stream the ROOT histogram into the output stream as XML
184  * @param histo (INPUT) the histogram to be streamed
185  * @param stream (OUTPUT) the stream
186  */
187 // ============================================================================
189 ( const AIDA::IHistogram3D& histo ,
190  std::ostream& stream )
191 {
192  auto root = Gaudi::Utils::Aida2ROOT::aida2root ( &histo ) ;
193  return root ? toXml( *root , stream ) : stream;
194 }
195 // ============================================================================
196 /* stream the ROOT histogram into the output stream as XML
197  * @param histo (INPUT) the histogram to be streamed
198  * @param stream (OUTPUT) the stream
199  */
200 // ============================================================================
202 ( const AIDA::IProfile1D& histo ,
203  std::ostream& stream )
204 {
205  auto root = Gaudi::Utils::Aida2ROOT::aida2root ( &histo ) ;
206  return root ? toXml( *root , stream ) : stream;
207 }
208 // ============================================================================
209 /* stream the ROOT histogram into the output stream as XML
210  * @param histo (INPUT) the histogram to be streamed
211  * @param stream (OUTPUT) the stream
212  */
213 // ============================================================================
215 ( const AIDA::IProfile2D& histo ,
216  std::ostream& stream )
217 {
218  auto root = Gaudi::Utils::Aida2ROOT::aida2root ( &histo ) ;
219  return root ? toXml( *root , stream ) : stream;
220 }
221 // ============================================================================
222 /* parse the histogram from standard ROOT XML
223  * @param result (OUTPUT) the parsed histogram
224  * @param input (INPUT) the input XML string
225  * @return status code
226  */
227 // ============================================================================
229 ( TH1D& result , const std::string& input )
230 {
231  //
232  result.Reset() ; // RESET old histogram
233  //
234 
235  auto histo = _Xml<TH1D>( input ) ;
236  if ( !histo ) { return StatusCode::FAILURE ; } // RETURN
237  //
238  histo->Copy ( result ) ;
239  //
240  return StatusCode::SUCCESS ;
241 }
242 // ============================================================================
243 /* parse the histogram from standard ROOT XML
244  * @param result (OUTPUT) the parsed histogram
245  * @param input (INPUT) the input XML string
246  * @return status code
247  */
248 // ============================================================================
250 ( TH2D& result , const std::string& input )
251 {
252  //
253  result.Reset() ; // RESET old histogram
254  //
255  auto histo = _Xml<TH2D>( input ) ;
256  if ( !histo ) { return StatusCode::FAILURE ; } // RETURN
257  //
258  histo->Copy ( result ) ;
259  //
260  return StatusCode::SUCCESS ;
261 }
262 // ============================================================================
263 /* parse the histogram from standard ROOT XML
264  * @param result (OUTPUT) the parsed histogram
265  * @param input (INPUT) the input XML string
266  * @return status code
267  */
268 // ============================================================================
270 ( TH3D& result , const std::string& input )
271 {
272  //
273  result.Reset() ; // RESET old histogram
274  //
275  auto histo = _Xml<TH3D>( input ) ;
276  if ( !histo ) { return StatusCode::FAILURE ; } // RETURN
277  //
278  histo->Copy ( result ) ;
279  //
280  return StatusCode::SUCCESS ;
281 }
282 // ============================================================================
283 /* parse the histogram from standard ROOT XML
284  * @param result (OUTPUT) the parsed histogram
285  * @param input (INPUT) the input XML string
286  * @return status code
287  */
288 // ============================================================================
290 ( TH1F& result , const std::string& input )
291 {
292  //
293  result.Reset() ; // RESET old histogram
294  //
295  auto histo = _Xml<TH1F>( input ) ;
296  if ( !histo ) { return StatusCode::FAILURE ; } // RETURN
297  //
298  histo->Copy ( result ) ;
299  //
300  return StatusCode::SUCCESS ;
301 }
302 // ============================================================================
303 /* parse the histogram from standard ROOT XML
304  * @param result (OUTPUT) the parsed histogram
305  * @param input (INPUT) the input XML string
306  * @return status code
307  */
308 // ============================================================================
310 ( TH2F& result , const std::string& input )
311 {
312  //
313  result.Reset() ; // RESET old histogram
314  //
315  auto histo = _Xml<TH2F>( input ) ;
316  if ( !histo ) { return StatusCode::FAILURE ; } // 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 // ============================================================================
330 ( TH3F& result , const std::string& input )
331 {
332  //
333  result.Reset() ; // RESET old histogram
334  //
335  auto histo =_Xml<TH3F>( input ) ;
336  if ( !histo ) { return StatusCode::FAILURE ; } // RETURN
337  //
338  histo->Copy ( result ) ;
339  //
340  return StatusCode::SUCCESS ;
341 }
342 // ============================================================================
343 /* parse the histogram from standard ROOT XML
344  * @param result (OUTPUT) the parsed histogram
345  * @param input (INPUT) the input XML string
346  * @return status code
347  */
348 // ============================================================================
350 ( TProfile& result , const std::string& input )
351 {
352  //
353  result.Reset() ; // RESET old histogram
354  //
355  auto histo = _Xml<TProfile>( input ) ;
356  if ( !histo ) { return StatusCode::FAILURE ; } // RETURN
357  //
358  histo->Copy ( result ) ;
359  //
360  return StatusCode::SUCCESS ;
361 }
362 // ============================================================================
363 /* parse the histogram from standard ROOT XML
364  * @param result (OUTPUT) the parsed histogram
365  * @param input (INPUT) the input XML string
366  * @return status code
367  */
368 // ============================================================================
370 ( TProfile2D& result , const std::string& input )
371 {
372  //
373  result.Reset() ; // RESET old histogram
374  //
375  auto histo = _Xml<TProfile2D>( input ) ;
376  if ( !histo ) { return StatusCode::FAILURE ; } // RETURN
377  //
378  histo->Copy ( result ) ;
379  //
380  return StatusCode::SUCCESS ;
381 }
382 
383 
384 // ============================================================================
385 /* parse the histogram from standard ROOT XML
386  * @param result (OUTPUT) the parsed histogram
387  * @param input (INPUT) the input XML string
388  * @return status code
389  */
390 // ============================================================================
392 ( TH1D*& result , const std::string& input )
393 {
394  if ( result ) { return fromXml ( *result , input ) ; }
395  //
396  auto histo = _Xml<TH1D>( input ) ;
397  if ( !histo ) { return StatusCode::FAILURE ; } // RETURN
398  //
399  result = histo.release() ; // ASSIGN
400  //
401  return StatusCode::SUCCESS ;
402 }
403 // ============================================================================
404 /* parse the histogram from standard ROOT XML
405  * @param result (OUTPUT) the parsed histogram
406  * @param input (INPUT) the input XML string
407  * @return status code
408  */
409 // ============================================================================
411 ( TH2D*& result , const std::string& input )
412 {
413  if ( result ) { return fromXml ( *result , input ) ; }
414  //
415  auto histo = _Xml<TH2D>( 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 // ============================================================================
430 ( TH3D*& result , const std::string& input )
431 {
432  if ( result ) { return fromXml ( *result , input ) ; }
433  //
434  auto histo = _Xml<TH3D>( input ) ;
435  if ( !histo ) { return StatusCode::FAILURE ; } // RETURN
436  //
437  result = histo.release() ; // ASSIGN
438  //
439  return StatusCode::SUCCESS ;
440 }
441 
442 // ============================================================================
443 /* parse the histogram from standard ROOT XML
444  * @param result (OUTPUT) the parsed histogram
445  * @param input (INPUT) the input XML string
446  * @return status code
447  */
448 // ============================================================================
450 ( TProfile*& result , const std::string& input )
451 {
452  if ( result ) { return fromXml ( *result , input ) ; }
453  //
454  auto histo = _Xml<TProfile>( input ) ;
455  if ( !histo ) { return StatusCode::FAILURE ; } // RETURN
456  //
457  result = histo.release() ; // ASSIGN
458  //
459  return StatusCode::SUCCESS ;
460 }
461 // ============================================================================
462 /* parse the histogram from standard ROOT XML
463  * @param result (OUTPUT) the parsed histogram
464  * @param input (INPUT) the input XML string
465  * @return status code
466  */
467 // ============================================================================
469 ( TProfile2D*& result , const std::string& input )
470 {
471  if ( result ) { return fromXml ( *result , input ) ; }
472  //
473  auto histo = _Xml<TProfile2D>( input ) ;
474  if ( !histo ) { return StatusCode::FAILURE ; } // RETURN
475  //
476  result = histo.release() ; // ASSIGN
477  //
478  return StatusCode::SUCCESS ;
479 }
480 // ============================================================================
481 /* parse the histogram from standard ROOT XML
482  * @param result (OUTPUT) the parsed histogram
483  * @param input (INPUT) the input XML string
484  * @return status code
485  */
486 // ============================================================================
488 ( AIDA::IHistogram1D& result , const std::string& input )
489 {
490  auto root = Gaudi::Utils::Aida2ROOT::aida2root ( &result ) ;
491  return root ? fromXml ( *root , input ) : StatusCode::FAILURE ; // RETURN
492 }
493 // ============================================================================
494 /* parse the histogram from standard ROOT XML
495  * @param result (OUTPUT) the parsed histogram
496  * @param input (INPUT) the input XML string
497  * @return status code
498  */
499 // ============================================================================
501 ( AIDA::IHistogram2D& result , const std::string& input )
502 {
503  auto root = Gaudi::Utils::Aida2ROOT::aida2root ( &result ) ;
504  return root ? fromXml ( *root , input ) : StatusCode::FAILURE; // RETURN
505 }
506 // ============================================================================
507 /* parse the histogram from standard ROOT XML
508  * @param result (OUTPUT) the parsed histogram
509  * @param input (INPUT) the input XML string
510  * @return status code
511  */
512 // ============================================================================
514 ( AIDA::IHistogram3D& result , const std::string& input )
515 {
516  auto root = Gaudi::Utils::Aida2ROOT::aida2root ( &result ) ;
517  return root ? fromXml ( *root , input ) : StatusCode::FAILURE; // RETURN
518 }
519 // ============================================================================
520 /* parse the histogram from standard ROOT XML
521  * @param result (OUTPUT) the parsed histogram
522  * @param input (INPUT) the input XML string
523  * @return status code
524  */
525 // ============================================================================
527 ( AIDA::IProfile1D& result , const std::string& input )
528 {
529  auto root = Gaudi::Utils::Aida2ROOT::aida2root ( &result ) ;
530  return root ? fromXml ( *root , input ) : StatusCode::FAILURE; // RETURN
531 }
532 // ============================================================================
533 /* parse the histogram from standard ROOT XML
534  * @param result (OUTPUT) the parsed histogram
535  * @param input (INPUT) the input XML string
536  * @return status code
537  */
538 // ============================================================================
540 ( AIDA::IProfile2D& result , const std::string& input )
541 {
542  auto root = Gaudi::Utils::Aida2ROOT::aida2root ( &result ) ;
543  return root ? fromXml ( *root , input ) : StatusCode::FAILURE; // RETURN
544 }
545 // ============================================================================
546 // The END
547 // ============================================================================
GAUDI_API StatusCode fromXml(TH1D &result, const std::string &input)
parse the histogram from standard ROOT XML
Definition: HistoXML.cpp:229
STL class.
This class is used for returning status codes from appropriate routines.
Definition: StatusCode.h:26
static TH1D * aida2root(AIDA::IHistogram1D *aida)
get the underlying pointer for 1D-histogram
Definition: Aida2ROOT.cpp:72
GAUDI_API std::ostream & toXml(const TH1D &histo, std::ostream &stream)
stream the ROOT histogram into output stream as XML
Definition: HistoXML.cpp:67
STL class.
T c_str(T...args)
STL class.