The Gaudi Framework  master (82fdf313)
Loading...
Searching...
No Matches
Histo2String.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// ROOT
15// ============================================================================
16#include <TH1D.h>
17#include <TH1F.h>
18#include <TH2D.h>
19#include <TH2F.h>
20#include <TH3D.h>
21#include <TH3F.h>
22// ============================================================================
23// AIDA
24// ============================================================================
25#include <AIDA/IHistogram1D.h>
26#include <AIDA/IHistogram2D.h>
27#include <AIDA/IHistogram3D.h>
28// ============================================================================
29// GaudiKernel
30// ============================================================================
32// ============================================================================
33// local
34// ============================================================================
37#include <GaudiUtils/HistoXML.h>
38// ============================================================================
44// ============================================================================
45namespace {
46 // ==========================================================================
47 template <class HISTO>
48 std::ostream& _toStream_1D_( const HISTO& histo, std::ostream& stream, const bool asXML ) {
49 if ( asXML ) { return Gaudi::Utils::Histos::toXml( histo, stream ); }
50 //
51 stream << "{ ";
52 //
53 stream << "'name' : ";
54 Gaudi::Utils::toStream( std::string( histo.GetName() ), stream ) << " , ";
55 stream << "'title' : ";
56 Gaudi::Utils::toStream( std::string( histo.GetTitle() ), stream ) << " , ";
57 //
58 const TAxis* axis = histo.GetXaxis();
59 const unsigned int nBins = axis->GetNbins();
60 //
61 stream << std::endl << "'X' : { ";
62 if ( axis->IsVariableBinSize() ) {
63 const TArrayD* xbins = axis->GetXbins();
64 const unsigned int xsize = xbins->GetSize();
65 std::vector<double> edges;
66 for ( unsigned int iBin = 0; iBin < xsize; ++iBin ) { edges.push_back( xbins->At( iBin ) ); }
67 stream << "'edges' : ";
68 Gaudi::Utils::toStream( edges, stream ) << " }, " << std::endl;
69 } else {
70 stream << "'nbins' : ";
71 Gaudi::Utils::toStream( nBins, stream ) << " , ";
72 stream << "'low' : ";
73 Gaudi::Utils::toStream( axis->GetXmin(), stream ) << " , ";
74 stream << "'high' : ";
75 Gaudi::Utils::toStream( axis->GetXmax(), stream ) << " }, " << std::endl;
76 }
77 // finally: the content
78 std::vector<std::pair<double, double>> bins;
79 for ( unsigned int iBin = 0; iBin <= nBins + 1; ++iBin ) {
80 bins.emplace_back( histo.GetBinContent( iBin ), histo.GetBinError( iBin ) );
81 }
82 stream << "'bins' : ";
83 Gaudi::Utils::toStream( bins, stream );
84 //
85 stream << " }";
86 //
87 return stream;
88 }
89 // ==========================================================================
90 template <class HISTO>
91 std::ostream& _toStream_2D_( const HISTO& histo, std::ostream& stream, const bool asXML ) {
92 if ( asXML ) { return Gaudi::Utils::Histos::toXml( histo, stream ); }
93 //
94 stream << "{ ";
95 //
96 stream << "'name' : ";
97 Gaudi::Utils::toStream( std::string( histo.GetName() ), stream ) << " , ";
98 stream << "'title' : ";
99 Gaudi::Utils::toStream( std::string( histo.GetTitle() ), stream ) << " , ";
100 //
101 const TAxis* xaxis = histo.GetXaxis();
102 const int xBins = xaxis->GetNbins();
103 //
104 stream << std::endl << "'X' : { ";
105 if ( xaxis->IsVariableBinSize() ) {
106 const TArrayD* xbins = xaxis->GetXbins();
107 const unsigned int xsize = xbins->GetSize();
108 std::vector<double> edges;
109 for ( unsigned int iBin = 0; iBin < xsize; ++iBin ) { edges.push_back( xbins->At( iBin ) ); }
110 // the edges
111 stream << "'edges' : ";
112 Gaudi::Utils::toStream( edges, stream ) << " }," << std::endl;
113 } else {
114 stream << "'nbins' : ";
115 Gaudi::Utils::toStream( xBins, stream ) << " , ";
116 stream << "'low' : ";
117 Gaudi::Utils::toStream( xaxis->GetXmin(), stream ) << " , ";
118 stream << "'high' : ";
119 Gaudi::Utils::toStream( xaxis->GetXmax(), stream ) << " }, " << std::endl;
120 }
121 //
122 const TAxis* yaxis = histo.GetYaxis();
123 const int yBins = yaxis->GetNbins();
124 //
125 stream << std::endl << "'Y' : { ";
126 if ( yaxis->IsVariableBinSize() ) {
127 const TArrayD* ybins = yaxis->GetXbins();
128 const unsigned int ysize = ybins->GetSize();
129 std::vector<double> edges;
130 for ( unsigned int iBin = 0; iBin < ysize; ++iBin ) { edges.push_back( ybins->At( iBin ) ); }
131 // the edges
132 stream << " 'edges' : ";
133 Gaudi::Utils::toStream( edges, stream ) << " }," << std::endl;
134 } else {
135 stream << "'nbins' : ";
136 Gaudi::Utils::toStream( yBins, stream ) << " , ";
137 stream << "'low' : ";
138 Gaudi::Utils::toStream( yaxis->GetXmin(), stream ) << " , ";
139 stream << "'high' : ";
140 Gaudi::Utils::toStream( yaxis->GetXmax(), stream ) << " }, " << std::endl;
141 }
142 //
143 // finally: the content
144 stream << "'bins' : " << std::endl << " [ ";
145 for ( int jBin = yBins + 1; jBin >= 0; --jBin ) {
146 if ( yBins + 1 != jBin ) { stream << std::endl; }
147 for ( int iBin = 0; iBin <= xBins + 1; ++iBin ) {
148 //
149 Gaudi::Utils::toStream( std::make_pair( histo.GetBinContent( iBin, jBin ), histo.GetBinError( iBin, jBin ) ),
150 stream );
151 //
152 if ( xBins + 1 != iBin || 0 != jBin ) { stream << " , "; }
153 }
154 }
155 stream << " ]";
156 //
157 stream << " }";
158 //
159 return stream;
160 }
161 // ==========================================================================
162 template <class HISTO>
163 std::ostream& _toStream_3D_( const HISTO& histo, std::ostream& stream, const bool asXML ) {
164 if ( asXML ) { return Gaudi::Utils::Histos::toXml( histo, stream ); }
165 //
166 stream << "{ ";
167 //
168 stream << "'name' : ";
169 Gaudi::Utils::toStream( std::string( histo.GetName() ), stream ) << " , ";
170 stream << "'title' : ";
171 Gaudi::Utils::toStream( std::string( histo.GetTitle() ), stream ) << " , ";
172 //
173 const TAxis* xaxis = histo.GetXaxis();
174 const int xBins = xaxis->GetNbins();
175 //
176 stream << std::endl << "'X' : { ";
177 if ( xaxis->IsVariableBinSize() ) {
178 const TArrayD* xbins = xaxis->GetXbins();
179 const unsigned int xsize = xbins->GetSize();
180 std::vector<double> edges;
181 for ( unsigned int iBin = 0; iBin < xsize; ++iBin ) { edges.push_back( xbins->At( iBin ) ); }
182 // the edges
183 stream << "'edges' : ";
184 Gaudi::Utils::toStream( edges, stream ) << " }," << std::endl;
185 } else {
186 stream << "'nbins' : ";
187 Gaudi::Utils::toStream( xBins, stream ) << " , ";
188 stream << "'low' : ";
189 Gaudi::Utils::toStream( xaxis->GetXmin(), stream ) << " , ";
190 stream << "'high' : ";
191 Gaudi::Utils::toStream( xaxis->GetXmax(), stream ) << " }, " << std::endl;
192 }
193 //
194 const TAxis* yaxis = histo.GetYaxis();
195 const int yBins = yaxis->GetNbins();
196 //
197 stream << std::endl << "'Y' : { ";
198 if ( yaxis->IsVariableBinSize() ) {
199 const TArrayD* ybins = yaxis->GetXbins();
200 const unsigned int ysize = ybins->GetSize();
201 std::vector<double> edges;
202 for ( unsigned int iBin = 0; iBin < ysize; ++iBin ) { edges.push_back( ybins->At( iBin ) ); }
203 // the edges
204 stream << " 'edges' : ";
205 Gaudi::Utils::toStream( edges, stream ) << " }," << std::endl;
206 } else {
207 stream << "'nbins' : ";
208 Gaudi::Utils::toStream( yBins, stream ) << " , ";
209 stream << "'low' : ";
210 Gaudi::Utils::toStream( yaxis->GetXmin(), stream ) << " , ";
211 stream << "'high' : ";
212 Gaudi::Utils::toStream( yaxis->GetXmax(), stream ) << " }, " << std::endl;
213 }
214 //
215 const TAxis* zaxis = histo.GetZaxis();
216 const int zBins = zaxis->GetNbins();
217 //
218 stream << std::endl << "'Z' : { ";
219 if ( zaxis->IsVariableBinSize() ) {
220 const TArrayD* zbins = zaxis->GetXbins();
221 const unsigned int zsize = zbins->GetSize();
222 std::vector<double> edges;
223 for ( unsigned int iBin = 0; iBin < zsize; ++iBin ) { edges.push_back( zbins->At( iBin ) ); }
224 // the edges
225 stream << " 'edges' : ";
226 Gaudi::Utils::toStream( edges, stream ) << " }," << std::endl;
227 } else {
228 stream << "'nbins' : ";
229 Gaudi::Utils::toStream( zBins, stream ) << " , ";
230 stream << "'low' : ";
231 Gaudi::Utils::toStream( zaxis->GetXmin(), stream ) << " , ";
232 stream << "'high' : ";
233 Gaudi::Utils::toStream( zaxis->GetXmax(), stream ) << " }, " << std::endl;
234 }
235 //
236 // finally: the content
237 stream << "'bins' : " << std::endl << " [ ";
238 for ( int kBin = 0; kBin <= zBins + 1; ++kBin ) {
239 for ( int jBin = yBins + 1; jBin >= 0; --jBin ) {
240 if ( yBins + 1 != jBin ) { stream << std::endl; }
241 for ( int iBin = 0; iBin <= xBins + 1; ++iBin ) {
242 //
244 std::make_pair( histo.GetBinContent( iBin, jBin, kBin ), histo.GetBinError( iBin, jBin, kBin ) ),
245 stream );
246 //
247 if ( !( ( ( xBins + 1 ) == iBin ) && ( 0 == jBin ) && ( ( zBins + 1 ) == kBin ) ) ) { stream << " , "; }
248 }
249 }
250 }
251 stream << " ]";
252 //
253 stream << " }";
254 //
255 return stream;
256 }
257 // ==========================================================================
258} // end of anonymous namespace
259// ============================================================================
260/* stream the ROOT histogram into output stream
261 * @param histo (INPUT) the histogram to be streamed
262 * @param stream (OUTPUT) the stream
263 * @param asXML (INPUT) use XML-format
264 */
265// ============================================================================
266std::ostream& Gaudi::Utils::toStream( const TH1D& histo, std::ostream& stream, const bool asXML ) {
267 return _toStream_1D_( histo, stream, asXML );
268}
269// ============================================================================
270/* stream the ROOT histogram into output stream
271 * @param histo (INPUT) the histogram to be streamed
272 * @param stream (OUTPUT) the stream
273 * @param asXML (INPUT) use XML-format
274 */
275// ============================================================================
276std::ostream& Gaudi::Utils::toStream( const TH1F& histo, std::ostream& stream, const bool asXML ) {
277 return _toStream_1D_( histo, stream, asXML );
278}
279// ============================================================================
280/* stream the ROOT histogram into output stream
281 * @param histo (INPUT) the histogram to be streamed
282 * @param stream (OUTPUT) the stream
283 * @param asXML (INPUT) use XML-format
284 */
285// ============================================================================
286std::ostream& Gaudi::Utils::toStream( const TH2D& histo, std::ostream& stream, const bool asXML ) {
287 return _toStream_2D_( histo, stream, asXML );
288}
289// ============================================================================
290/* stream the ROOT histogram into output stream
291 * @param histo (INPUT) the histogram to be streamed
292 * @param stream (OUTPUT) the stream
293 * @param asXML (INPUT) use XML-format
294 */
295// ============================================================================
296std::ostream& Gaudi::Utils::toStream( const TH2F& histo, std::ostream& stream, const bool asXML ) {
297 return _toStream_2D_( histo, stream, asXML );
298}
299// ============================================================================
300/* stream the ROOT histogram into output stream
301 * @param histo (INPUT) the histogram to be streamed
302 * @param stream (OUTPUT) the stream
303 * @param asXML (INPUT) use XML-format
304 */
305// ============================================================================
306std::ostream& Gaudi::Utils::toStream( const TH3D& histo, std::ostream& stream, const bool asXML ) {
307 return _toStream_3D_( histo, stream, asXML );
308}
309// ============================================================================
310/* stream the ROOT histogram into output stream
311 * @param histo (INPUT) the histogram to be streamed
312 * @param stream (OUTPUT) the stream
313 * @param asXML (INPUT) use XML-format
314 */
315// ============================================================================
316std::ostream& Gaudi::Utils::toStream( const TH3F& histo, std::ostream& stream, const bool asXML ) {
317 return _toStream_3D_( histo, stream, asXML );
318}
319// ============================================================================
320/* stream the AIDA histogram into output stream
321 * @param histo (INPUT) the histogram to be streamed
322 * @param stream (OUTPUT) the stream
323 * @param asXML (INPUT) use XML-format
324 */
325// ============================================================================
326std::ostream& Gaudi::Utils::toStream( const AIDA::IHistogram1D& histo, std::ostream& stream, const bool asXML ) {
327 auto root = Gaudi::Utils::Aida2ROOT::aida2root( &histo );
328 return root ? toStream( *root, stream, asXML ) : stream;
329}
330// ============================================================================
331/* stream the AIDA histogram into output stream
332 * @param histo (INPUT) the histogram to be streamed
333 * @param stream (OUTPUT) the stream
334 * @param asXML (INPUT) use XML-format
335 */
336// ============================================================================
337std::ostream& Gaudi::Utils::toStream( const AIDA::IHistogram2D& histo, std::ostream& stream, const bool asXML ) {
338 //
339 auto root = Gaudi::Utils::Aida2ROOT::aida2root( &histo );
340 return root ? toStream( *root, stream, asXML ) : stream;
341}
342// ============================================================================
343/* stream the AIDA histogram into output stream
344 * @param histo (INPUT) the histogram to be streamed
345 * @param stream (OUTPUT) the stream
346 * @param asXML (INPUT) use XML-format
347 */
348// ============================================================================
349std::ostream& Gaudi::Utils::toStream( const AIDA::IHistogram3D& histo, std::ostream& stream, const bool asXML ) {
350 //
351 auto root = Gaudi::Utils::Aida2ROOT::aida2root( &histo );
352 return root ? toStream( *root, stream, asXML ) : stream;
353}
354
355// ============================================================================
356/* convert the histogram into the string
357 * @param histo (INPUT) the histogram to be streamed
358 * @param asXML (INPUT) use XML-format
359 * @return the string representation of the histogram
360 * @author Vanya BELYAEV Ivan.Belyaev@nikhef.nl
361 * @date 2009-09-26
362 */
363// ============================================================================
364std::string Gaudi::Utils::toString( const TH1D& histo, const bool asXML ) {
365 std::ostringstream o;
366 toStream( histo, o, asXML );
367 return o.str();
368}
369// ============================================================================
370/* convert the histogram into the string
371 * @param histo (INPUT) the histogram to be streamed
372 * @param asXML (INPUT) use XML-format
373 * @return the string representation of the histogram
374 * @author Vanya BELYAEV Ivan.Belyaev@nikhef.nl
375 * @date 2009-09-26
376 */
377// ============================================================================
378std::string Gaudi::Utils::toString( const TH1F& histo, const bool asXML ) {
379 std::ostringstream o;
380 toStream( histo, o, asXML );
381 return o.str();
382}
383// ============================================================================
384/* convert the histogram into the string
385 * @param histo (INPUT) the histogram to be streamed
386 * @param asXML (INPUT) use XML-format
387 * @return the string representation of the histogram
388 * @author Vanya BELYAEV Ivan.Belyaev@nikhef.nl
389 * @date 2009-09-26
390 */
391// ============================================================================
392std::string Gaudi::Utils::toString( const TH2D& histo, const bool asXML ) {
393 std::ostringstream o;
394 toStream( histo, o, asXML );
395 return o.str();
396}
397// ============================================================================
398/* convert the histogram into the string
399 * @param histo (INPUT) the histogram to be streamed
400 * @param asXML (INPUT) use XML-format
401 * @return the string representation of the histogram
402 * @author Vanya BELYAEV Ivan.Belyaev@nikhef.nl
403 * @date 2009-09-26
404 */
405// ============================================================================
406std::string Gaudi::Utils::toString( const TH2F& histo, const bool asXML ) {
407 std::ostringstream o;
408 toStream( histo, o, asXML );
409 return o.str();
410}
411// ============================================================================
412/* convert the histogram into the string
413 * @param histo (INPUT) the histogram to be streamed
414 * @param asXML (INPUT) use XML-format
415 * @return the string representation of the histogram
416 * @author Vanya BELYAEV Ivan.Belyaev@nikhef.nl
417 * @date 2009-09-26
418 */
419// ============================================================================
420std::string Gaudi::Utils::toString( const TH3D& histo, const bool asXML ) {
421 std::ostringstream o;
422 toStream( histo, o, asXML );
423 return o.str();
424}
425// ============================================================================
426/* convert the histogram into the string
427 * @param histo (INPUT) the histogram to be streamed
428 * @param asXML (INPUT) use XML-format
429 * @return the string representation of the histogram
430 * @author Vanya BELYAEV Ivan.Belyaev@nikhef.nl
431 * @date 2009-09-26
432 */
433// ============================================================================
434std::string Gaudi::Utils::toString( const TH3F& histo, const bool asXML ) {
435 std::ostringstream o;
436 toStream( histo, o, asXML );
437 return o.str();
438}
439// ============================================================================
440/* convert the histogram into the string
441 * @param histo (INPUT) the histogram to be streamed
442 * @param asXML (INPUT) use XML-format
443 * @return the string representation of the histogram
444 * @author Vanya BELYAEV Ivan.Belyaev@nikhef.nl
445 * @date 2009-09-26
446 */
447// ============================================================================
448std::string Gaudi::Utils::toString( const AIDA::IHistogram1D& histo, const bool asXML ) {
449 std::ostringstream o;
450 toStream( histo, o, asXML );
451 return o.str();
452}
453// =============================================================================
454/* convert the histogram into the string
455 * @param histo (INPUT) the histogram to be streamed
456 * @param asXML (INPUT) use XML-format
457 * @return the string representation of the histogram
458 * @author Vanya BELYAEV Ivan.Belyaev@nikhef.nl
459 * @date 2009-09-26
460 */
461// ============================================================================
462std::string Gaudi::Utils::toString( const AIDA::IHistogram2D& histo, const bool asXML ) {
463 std::ostringstream o;
464 toStream( histo, o, asXML );
465 return o.str();
466}
467// =============================================================================
468/* convert the histogram into the string
469 * @param histo (INPUT) the histogram to be streamed
470 * @param asXML (INPUT) use XML-format
471 * @return the string representation of the histogram
472 * @author Vanya BELYAEV Ivan.Belyaev@nikhef.nl
473 * @date 2009-09-26
474 */
475// ============================================================================
476std::string Gaudi::Utils::toString( const AIDA::IHistogram3D& histo, const bool asXML ) {
477 std::ostringstream o;
478 toStream( histo, o, asXML );
479 return o.str();
480}
481// =============================================================================
482/* convert the histogram into the string
483 * @param histo (INPUT) the histogram to be streamed
484 * @param asXML (INPUT) use XML-format
485 * @return the string representation of the histogram
486 * @author Vanya BELYAEV Ivan.Belyaev@nikhef.nl
487 * @date 2009-09-26
488 */
489// =============================================================================
490std::string Gaudi::Utils::toString( const AIDA::IHistogram1D* histo ) { return histo ? toString( *histo ) : "{}"; }
491// ============================================================================
492/* convert the histogram into the string
493 * @param histo (INPUT) the histogram to be streamed
494 * @param asXML (INPUT) use XML-format
495 * @return the string representation of the histogram
496 * @author Vanya BELYAEV Ivan.Belyaev@nikhef.nl
497 * @date 2009-09-26
498 */
499// =============================================================================
500std::string Gaudi::Utils::toString( AIDA::IHistogram1D* histo ) { return histo ? toString( *histo ) : "{}"; }
501// ============================================================================
502/* convert the histogram into the string
503 * @param histo (INPUT) the histogram to be streamed
504 * @param asXML (INPUT) use XML-format
505 * @return the string representation of the histogram
506 * @author Vanya BELYAEV Ivan.Belyaev@nikhef.nl
507 * @date 2009-09-26
508 */
509// =============================================================================
510std::string Gaudi::Utils::toString( const TH1D* histo ) { return histo ? toString( *histo ) : "{}"; }
511// ============================================================================
512/* convert the histogram into the string
513 * @param histo (INPUT) the histogram to be streamed
514 * @param asXML (INPUT) use XML-format
515 * @return the string representation of the histogram
516 * @author Vanya BELYAEV Ivan.Belyaev@nikhef.nl
517 * @date 2009-09-26
518 */
519// =============================================================================
520std::string Gaudi::Utils::toString( const TH2D* histo ) { return histo ? toString( *histo ) : "{}"; }
521// ============================================================================
522/* convert the histogram into the string
523 * @param histo (INPUT) the histogram to be streamed
524 * @param asXML (INPUT) use XML-format
525 * @return the string representation of the histogram
526 * @author Vanya BELYAEV Ivan.Belyaev@nikhef.nl
527 * @date 2009-09-26
528 */
529// =============================================================================
530std::string Gaudi::Utils::toString( TH1D* histo ) { return histo ? toString( *histo ) : "{}"; }
531// ============================================================================
532/* convert the histogram into the string
533 * @param histo (INPUT) the histogram to be streamed
534 * @param asXML (INPUT) use XML-format
535 * @return the string representation of the histogram
536 * @author Vanya BELYAEV Ivan.Belyaev@nikhef.nl
537 * @date 2009-09-26
538 */
539// =============================================================================
540std::string Gaudi::Utils::toString( TH2D* histo ) { return histo ? toString( *histo ) : "{}"; }
541// ============================================================================
542/* convert the histogram into the string
543 * @param histo (INPUT) the histogram to be streamed
544 * @param asXML (INPUT) use XML-format
545 * @return the string representation of the histogram
546 * @author Vanya BELYAEV Ivan.Belyaev@nikhef.nl
547 * @date 2009-09-26
548 */
549// =============================================================================
550std::string Gaudi::Utils::toString( const TH3D* histo ) { return histo ? toString( *histo ) : "{}"; }
551// ============================================================================
552/* convert the histogram into the string
553 * @param histo (INPUT) the histogram to be streamed
554 * @param asXML (INPUT) use XML-format
555 * @return the string representation of the histogram
556 * @author Vanya BELYAEV Ivan.Belyaev@nikhef.nl
557 * @date 2009-09-26
558 */
559// =============================================================================
560std::string Gaudi::Utils::toString( TH3D* histo ) { return histo ? toString( *histo ) : "{}"; }
561// ============================================================================
562// The END
563// ============================================================================
std::ostream & toStream(const DataObjID &d, std::ostream &os)
Definition DataObjID.cpp:84
implementation of various functions for streaming.
static TH1D * aida2root(AIDA::IHistogram1D *aida)
get the underlying pointer for 1D-histogram
Definition Aida2ROOT.cpp:60
GAUDI_API std::ostream & toXml(const TH1D &histo, std::ostream &stream)
stream the ROOT histogram into output stream as XML
Definition HistoXML.cpp:76
std::string toString(const TYPE &obj)
the generic implementation of the type conversion to the string
Definition ToStream.h:326
std::ostream & toStream(ITERATOR first, ITERATOR last, std::ostream &s, const std::string &open, const std::string &close, const std::string &delim)
the helper function to print the sequence
Definition ToStream.h:304
stream
Definition Write.py:32