The Gaudi Framework  master (82fdf313)
Loading...
Searching...
No Matches
HistoXML.cpp
Go to the documentation of this file.
1/***********************************************************************************\
2* (c) Copyright 1998-2024 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// ============================================================================
42#include <GaudiUtils/HistoXML.h>
43// ============================================================================
49// ============================================================================
50namespace {
51 // ==========================================================================
53 //
54 template <typename TYPE>
55 std::unique_ptr<TYPE> _Xml( std::string_view input ) {
56 // recrate a C string from the string_view as ROOT needs one
57 std::string s{ input };
58 // 1) use XML-parser
59 std::unique_ptr<TObject> obj{ TBufferXML::ConvertFromXML( s.c_str() ) };
60 TYPE* histo = ( obj ? dynamic_cast<TYPE*>( obj.get() ) : nullptr );
61 // slightly tricky: in case the dynamic cast succeeds, transfer owership
62 // by invoking 'release' on obj, and 'at the same time' pass 'histo' into
63 // the constructor of unique_ptr -- but if the cast fails, do NOT transfer
64 // ownership... the , operator is the closest I can think of
65 return std::unique_ptr<TYPE>{ histo ? ( obj.release(), histo ) : nullptr };
66 }
67 //
68 // ==========================================================================
69} // end of anonymous namespace
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// ============================================================================
76std::ostream& Gaudi::Utils::Histos::toXml( const TH1D& histo, std::ostream& stream ) {
77 return stream << TBufferXML::ConvertToXML( &histo );
78}
79// ============================================================================
80/* stream the ROOT histogram into output stream as XML
81 * @param histo (INPUT) the histogram to be streamed
82 * @param stream (OUTPUT) the stream
83 */
84// ============================================================================
85std::ostream& Gaudi::Utils::Histos::toXml( const TH2D& histo, std::ostream& stream ) {
86 return stream << TBufferXML::ConvertToXML( &histo );
87}
88// ============================================================================
89/* stream the ROOT histogram into output stream as XML
90 * @param histo (INPUT) the histogram to be streamed
91 * @param stream (OUTPUT) the stream
92 */
93// ============================================================================
94std::ostream& Gaudi::Utils::Histos::toXml( const TH3D& histo, std::ostream& stream ) {
95 return stream << TBufferXML::ConvertToXML( &histo );
96}
97// ============================================================================
98/* stream the ROOT histogram into output stream as XML
99 * @param histo (INPUT) the histogram to be streamed
100 * @param stream (OUTPUT) the stream
101 */
102// ============================================================================
103std::ostream& Gaudi::Utils::Histos::toXml( const TH1F& histo, std::ostream& stream ) {
104 return stream << TBufferXML::ConvertToXML( &histo );
105}
106// ============================================================================
107/* stream the ROOT histogram into output stream as XML
108 * @param histo (INPUT) the histogram to be streamed
109 * @param stream (OUTPUT) the stream
110 */
111// ============================================================================
112std::ostream& Gaudi::Utils::Histos::toXml( const TH2F& histo, std::ostream& stream ) {
113 return stream << TBufferXML::ConvertToXML( &histo );
114}
115// ============================================================================
116/* stream the ROOT histogram into output stream as XML
117 * @param histo (INPUT) the histogram to be streamed
118 * @param stream (OUTPUT) the stream
119 */
120// ============================================================================
121std::ostream& Gaudi::Utils::Histos::toXml( const TH3F& histo, std::ostream& stream ) {
122 return stream << TBufferXML::ConvertToXML( &histo );
123}
124// ============================================================================
125/* stream the ROOT histogram into output stream as XML
126 * @param histo (INPUT) the histogram to be streamed
127 * @param stream (OUTPUT) the stream
128 */
129// ============================================================================
130std::ostream& Gaudi::Utils::Histos::toXml( const TProfile& histo, std::ostream& stream ) {
131 return stream << TBufferXML::ConvertToXML( &histo );
132}
133// ============================================================================
134/* stream the ROOT histogram into output stream as XML
135 * @param histo (INPUT) the histogram to be streamed
136 * @param stream (OUTPUT) the stream
137 */
138// ============================================================================
139std::ostream& Gaudi::Utils::Histos::toXml( const TProfile2D& histo, std::ostream& stream ) {
140 return stream << TBufferXML::ConvertToXML( &histo );
141}
142// ============================================================================
143/* stream the ROOT histogram into the output stream as XML
144 * @param histo (INPUT) the histogram to be streamed
145 * @param stream (OUTPUT) the stream
146 */
147// ============================================================================
148std::ostream& Gaudi::Utils::Histos::toXml( const AIDA::IHistogram1D& histo, std::ostream& stream ) {
149 auto root = Gaudi::Utils::Aida2ROOT::aida2root( &histo );
150 return root ? toXml( *root, stream ) : stream;
151}
152// ============================================================================
153/* stream the ROOT histogram into the output stream as XML
154 * @param histo (INPUT) the histogram to be streamed
155 * @param stream (OUTPUT) the stream
156 */
157// ============================================================================
158std::ostream& Gaudi::Utils::Histos::toXml( const AIDA::IHistogram2D& histo, std::ostream& stream ) {
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// ============================================================================
168std::ostream& Gaudi::Utils::Histos::toXml( const AIDA::IHistogram3D& histo, std::ostream& stream ) {
169 auto root = Gaudi::Utils::Aida2ROOT::aida2root( &histo );
170 return root ? toXml( *root, stream ) : stream;
171}
172// ============================================================================
173/* stream the ROOT histogram into the output stream as XML
174 * @param histo (INPUT) the histogram to be streamed
175 * @param stream (OUTPUT) the stream
176 */
177// ============================================================================
178std::ostream& Gaudi::Utils::Histos::toXml( const AIDA::IProfile1D& histo, std::ostream& stream ) {
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// ============================================================================
188std::ostream& Gaudi::Utils::Histos::toXml( const AIDA::IProfile2D& histo, std::ostream& stream ) {
189 auto root = Gaudi::Utils::Aida2ROOT::aida2root( &histo );
190 return root ? toXml( *root, stream ) : stream;
191}
192// ============================================================================
193/* parse the histogram from standard ROOT XML
194 * @param result (OUTPUT) the parsed histogram
195 * @param input (INPUT) the input XML string
196 * @return status code
197 */
198// ============================================================================
199StatusCode Gaudi::Utils::Histos::fromXml( TH1D& result, std::string_view input ) {
200 //
201 result.Reset(); // RESET old histogram
202 //
203
204 auto histo = _Xml<TH1D>( input );
205 if ( !histo ) { return StatusCode::FAILURE; } // RETURN
206 //
207 histo->Copy( result );
208 //
209 return StatusCode::SUCCESS;
210}
211// ============================================================================
212/* parse the histogram from standard ROOT XML
213 * @param result (OUTPUT) the parsed histogram
214 * @param input (INPUT) the input XML string
215 * @return status code
216 */
217// ============================================================================
218StatusCode Gaudi::Utils::Histos::fromXml( TH2D& result, std::string_view input ) {
219 //
220 result.Reset(); // RESET old histogram
221 //
222 auto histo = _Xml<TH2D>( input );
223 if ( !histo ) { return StatusCode::FAILURE; } // RETURN
224 //
225 histo->Copy( result );
226 //
227 return StatusCode::SUCCESS;
228}
229// ============================================================================
230/* parse the histogram from standard ROOT XML
231 * @param result (OUTPUT) the parsed histogram
232 * @param input (INPUT) the input XML string
233 * @return status code
234 */
235// ============================================================================
236StatusCode Gaudi::Utils::Histos::fromXml( TH3D& result, std::string_view input ) {
237 //
238 result.Reset(); // RESET old histogram
239 //
240 auto histo = _Xml<TH3D>( input );
241 if ( !histo ) { return StatusCode::FAILURE; } // RETURN
242 //
243 histo->Copy( result );
244 //
245 return StatusCode::SUCCESS;
246}
247// ============================================================================
248/* parse the histogram from standard ROOT XML
249 * @param result (OUTPUT) the parsed histogram
250 * @param input (INPUT) the input XML string
251 * @return status code
252 */
253// ============================================================================
254StatusCode Gaudi::Utils::Histos::fromXml( TH1F& result, std::string_view input ) {
255 //
256 result.Reset(); // RESET old histogram
257 //
258 auto histo = _Xml<TH1F>( input );
259 if ( !histo ) { return StatusCode::FAILURE; } // RETURN
260 //
261 histo->Copy( result );
262 //
263 return StatusCode::SUCCESS;
264}
265// ============================================================================
266/* parse the histogram from standard ROOT XML
267 * @param result (OUTPUT) the parsed histogram
268 * @param input (INPUT) the input XML string
269 * @return status code
270 */
271// ============================================================================
272StatusCode Gaudi::Utils::Histos::fromXml( TH2F& result, std::string_view input ) {
273 //
274 result.Reset(); // RESET old histogram
275 //
276 auto histo = _Xml<TH2F>( input );
277 if ( !histo ) { return StatusCode::FAILURE; } // RETURN
278 //
279 histo->Copy( result );
280 //
281 return StatusCode::SUCCESS;
282}
283// ============================================================================
284/* parse the histogram from standard ROOT XML
285 * @param result (OUTPUT) the parsed histogram
286 * @param input (INPUT) the input XML string
287 * @return status code
288 */
289// ============================================================================
290StatusCode Gaudi::Utils::Histos::fromXml( TH3F& result, std::string_view input ) {
291 //
292 result.Reset(); // RESET old histogram
293 //
294 auto histo = _Xml<TH3F>( input );
295 if ( !histo ) { return StatusCode::FAILURE; } // 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// ============================================================================
308StatusCode Gaudi::Utils::Histos::fromXml( TProfile& result, std::string_view input ) {
309 //
310 result.Reset(); // RESET old histogram
311 //
312 auto histo = _Xml<TProfile>( input );
313 if ( !histo ) { return StatusCode::FAILURE; } // RETURN
314 //
315 histo->Copy( result );
316 //
317 return StatusCode::SUCCESS;
318}
319// ============================================================================
320/* parse the histogram from standard ROOT XML
321 * @param result (OUTPUT) the parsed histogram
322 * @param input (INPUT) the input XML string
323 * @return status code
324 */
325// ============================================================================
326StatusCode Gaudi::Utils::Histos::fromXml( TProfile2D& result, std::string_view input ) {
327 //
328 result.Reset(); // RESET old histogram
329 //
330 auto histo = _Xml<TProfile2D>( input );
331 if ( !histo ) { return StatusCode::FAILURE; } // RETURN
332 //
333 histo->Copy( result );
334 //
335 return StatusCode::SUCCESS;
336}
337
338// ============================================================================
339/* parse the histogram from standard ROOT XML
340 * @param result (OUTPUT) the parsed histogram
341 * @param input (INPUT) the input XML string
342 * @return status code
343 */
344// ============================================================================
345StatusCode Gaudi::Utils::Histos::fromXml( TH1D*& result, std::string_view input ) {
346 if ( result ) { return fromXml( *result, input ); }
347 //
348 auto histo = _Xml<TH1D>( input );
349 if ( !histo ) { return StatusCode::FAILURE; } // RETURN
350 //
351 result = histo.release(); // ASSIGN
352 //
353 return StatusCode::SUCCESS;
354}
355// ============================================================================
356/* parse the histogram from standard ROOT XML
357 * @param result (OUTPUT) the parsed histogram
358 * @param input (INPUT) the input XML string
359 * @return status code
360 */
361// ============================================================================
362StatusCode Gaudi::Utils::Histos::fromXml( TH2D*& result, std::string_view input ) {
363 if ( result ) { return fromXml( *result, input ); }
364 //
365 auto histo = _Xml<TH2D>( input );
366 if ( !histo ) { return StatusCode::FAILURE; } // RETURN
367 //
368 result = histo.release(); // ASSIGN
369 //
370 return StatusCode::SUCCESS;
371}
372// ============================================================================
373/* parse the histogram from standard ROOT XML
374 * @param result (OUTPUT) the parsed histogram
375 * @param input (INPUT) the input XML string
376 * @return status code
377 */
378// ============================================================================
379StatusCode Gaudi::Utils::Histos::fromXml( TH3D*& result, std::string_view input ) {
380 if ( result ) { return fromXml( *result, input ); }
381 //
382 auto histo = _Xml<TH3D>( input );
383 if ( !histo ) { return StatusCode::FAILURE; } // RETURN
384 //
385 result = histo.release(); // ASSIGN
386 //
387 return StatusCode::SUCCESS;
388}
389
390// ============================================================================
391/* parse the histogram from standard ROOT XML
392 * @param result (OUTPUT) the parsed histogram
393 * @param input (INPUT) the input XML string
394 * @return status code
395 */
396// ============================================================================
397StatusCode Gaudi::Utils::Histos::fromXml( TProfile*& result, std::string_view input ) {
398 if ( result ) { return fromXml( *result, input ); }
399 //
400 auto histo = _Xml<TProfile>( input );
401 if ( !histo ) { return StatusCode::FAILURE; } // RETURN
402 //
403 result = histo.release(); // ASSIGN
404 //
405 return StatusCode::SUCCESS;
406}
407// ============================================================================
408/* parse the histogram from standard ROOT XML
409 * @param result (OUTPUT) the parsed histogram
410 * @param input (INPUT) the input XML string
411 * @return status code
412 */
413// ============================================================================
414StatusCode Gaudi::Utils::Histos::fromXml( TProfile2D*& result, std::string_view input ) {
415 if ( result ) { return fromXml( *result, input ); }
416 //
417 auto histo = _Xml<TProfile2D>( input );
418 if ( !histo ) { return StatusCode::FAILURE; } // RETURN
419 //
420 result = histo.release(); // ASSIGN
421 //
422 return StatusCode::SUCCESS;
423}
424// ============================================================================
425/* parse the histogram from standard ROOT XML
426 * @param result (OUTPUT) the parsed histogram
427 * @param input (INPUT) the input XML string
428 * @return status code
429 */
430// ============================================================================
431StatusCode Gaudi::Utils::Histos::fromXml( AIDA::IHistogram1D& result, std::string_view input ) {
432 auto root = Gaudi::Utils::Aida2ROOT::aida2root( &result );
433 return root ? fromXml( *root, input ) : StatusCode::FAILURE; // RETURN
434}
435// ============================================================================
436/* parse the histogram from standard ROOT XML
437 * @param result (OUTPUT) the parsed histogram
438 * @param input (INPUT) the input XML string
439 * @return status code
440 */
441// ============================================================================
442StatusCode Gaudi::Utils::Histos::fromXml( AIDA::IHistogram2D& result, std::string_view input ) {
443 auto root = Gaudi::Utils::Aida2ROOT::aida2root( &result );
444 return root ? fromXml( *root, input ) : StatusCode::FAILURE; // RETURN
445}
446// ============================================================================
447/* parse the histogram from standard ROOT XML
448 * @param result (OUTPUT) the parsed histogram
449 * @param input (INPUT) the input XML string
450 * @return status code
451 */
452// ============================================================================
453StatusCode Gaudi::Utils::Histos::fromXml( AIDA::IHistogram3D& result, std::string_view input ) {
454 auto root = Gaudi::Utils::Aida2ROOT::aida2root( &result );
455 return root ? fromXml( *root, input ) : StatusCode::FAILURE; // RETURN
456}
457// ============================================================================
458/* parse the histogram from standard ROOT XML
459 * @param result (OUTPUT) the parsed histogram
460 * @param input (INPUT) the input XML string
461 * @return status code
462 */
463// ============================================================================
464StatusCode Gaudi::Utils::Histos::fromXml( AIDA::IProfile1D& result, std::string_view input ) {
465 auto root = Gaudi::Utils::Aida2ROOT::aida2root( &result );
466 return root ? fromXml( *root, input ) : StatusCode::FAILURE; // RETURN
467}
468// ============================================================================
469/* parse the histogram from standard ROOT XML
470 * @param result (OUTPUT) the parsed histogram
471 * @param input (INPUT) the input XML string
472 * @return status code
473 */
474// ============================================================================
475StatusCode Gaudi::Utils::Histos::fromXml( AIDA::IProfile2D& result, std::string_view input ) {
476 auto root = Gaudi::Utils::Aida2ROOT::aida2root( &result );
477 return root ? fromXml( *root, input ) : StatusCode::FAILURE; // RETURN
478}
479// ============================================================================
480// The END
481// ============================================================================
static TH1D * aida2root(AIDA::IHistogram1D *aida)
get the underlying pointer for 1D-histogram
Definition Aida2ROOT.cpp:60
This class is used for returning status codes from appropriate routines.
Definition StatusCode.h:64
constexpr static const auto SUCCESS
Definition StatusCode.h:99
constexpr static const auto FAILURE
Definition StatusCode.h:100
GAUDI_API StatusCode fromXml(TH1D &result, std::string_view input)
parse the histogram from standard ROOT XML
Definition HistoXML.cpp:199
GAUDI_API std::ostream & toXml(const TH1D &histo, std::ostream &stream)
stream the ROOT histogram into output stream as XML
Definition HistoXML.cpp:76