The Gaudi Framework  v36r0 (4abb4d13)
Histo2String.cpp
Go to the documentation of this file.
1 /***********************************************************************************\
2 * (c) Copyright 1998-2019 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 // ============================================================================
21 // AIDA
22 // ============================================================================
23 #include "AIDA/IHistogram1D.h"
24 #include "AIDA/IHistogram2D.h"
25 // ============================================================================
26 // GaudiKernel
27 // ============================================================================
28 #include "GaudiKernel/ToStream.h"
29 // ============================================================================
30 // local
31 // ============================================================================
32 #include "GaudiUtils/Aida2ROOT.h"
34 #include "GaudiUtils/HistoXML.h"
35 // ============================================================================
41 // ============================================================================
42 namespace {
43  // ==========================================================================
44  template <class HISTO>
45  std::ostream& _toStream_1D_( const HISTO& histo, std::ostream& stream, const bool asXML ) {
46  if ( asXML ) { return Gaudi::Utils::Histos::toXml( histo, stream ); }
47  //
48  stream << "{ ";
49  //
50  stream << "'name' : ";
51  Gaudi::Utils::toStream( std::string( histo.GetName() ), stream ) << " , ";
52  stream << "'title' : ";
53  Gaudi::Utils::toStream( std::string( histo.GetTitle() ), stream ) << " , ";
54  //
55  const TAxis* axis = histo.GetXaxis();
56  const unsigned int nBins = axis->GetNbins();
57  //
58  stream << std::endl << "'X' : { ";
59  if ( axis->IsVariableBinSize() ) {
60  const TArrayD* xbins = axis->GetXbins();
61  const unsigned int xsize = xbins->GetSize();
62  std::vector<double> edges;
63  for ( unsigned int iBin = 0; iBin < xsize; ++iBin ) { edges.push_back( xbins->At( iBin ) ); }
64  stream << "'edges' : ";
65  Gaudi::Utils::toStream( edges, stream ) << " }, " << std::endl;
66  } else {
67  stream << "'nbins' : ";
68  Gaudi::Utils::toStream( nBins, stream ) << " , ";
69  stream << "'low' : ";
70  Gaudi::Utils::toStream( axis->GetXmin(), stream ) << " , ";
71  stream << "'high' : ";
72  Gaudi::Utils::toStream( axis->GetXmax(), stream ) << " }, " << std::endl;
73  }
74  // finally: the content
76  for ( unsigned int iBin = 0; iBin <= nBins + 1; ++iBin ) {
77  bins.emplace_back( histo.GetBinContent( iBin ), histo.GetBinError( iBin ) );
78  }
79  stream << "'bins' : ";
81  //
82  stream << " }";
83  //
84  return stream;
85  }
86  // ==========================================================================
87  template <class HISTO>
88  std::ostream& _toStream_2D_( const HISTO& histo, std::ostream& stream, const bool asXML ) {
89  if ( asXML ) { return Gaudi::Utils::Histos::toXml( histo, stream ); }
90  //
91  stream << "{ ";
92  //
93  stream << "'name' : ";
94  Gaudi::Utils::toStream( std::string( histo.GetName() ), stream ) << " , ";
95  stream << "'title' : ";
96  Gaudi::Utils::toStream( std::string( histo.GetTitle() ), stream ) << " , ";
97  //
98  const TAxis* xaxis = histo.GetXaxis();
99  const int xBins = xaxis->GetNbins();
100  //
101  stream << std::endl << "'X' : { ";
102  if ( xaxis->IsVariableBinSize() ) {
103  const TArrayD* xbins = xaxis->GetXbins();
104  const unsigned int xsize = xbins->GetSize();
105  std::vector<double> edges;
106  for ( unsigned int iBin = 0; iBin < xsize; ++iBin ) { edges.push_back( xbins->At( iBin ) ); }
107  // the edges
108  stream << "'edges' : ";
109  Gaudi::Utils::toStream( edges, stream ) << " }," << std::endl;
110  } else {
111  stream << "'nbins' : ";
112  Gaudi::Utils::toStream( xBins, stream ) << " , ";
113  stream << "'low' : ";
114  Gaudi::Utils::toStream( xaxis->GetXmin(), stream ) << " , ";
115  stream << "'high' : ";
116  Gaudi::Utils::toStream( xaxis->GetXmax(), stream ) << " }, " << std::endl;
117  }
118  //
119  const TAxis* yaxis = histo.GetYaxis();
120  const int yBins = yaxis->GetNbins();
121  //
122  stream << std::endl << "'Y' : { ";
123  if ( yaxis->IsVariableBinSize() ) {
124  const TArrayD* ybins = yaxis->GetXbins();
125  const unsigned int ysize = ybins->GetSize();
126  std::vector<double> edges;
127  for ( unsigned int iBin = 0; iBin < ysize; ++iBin ) { edges.push_back( ybins->At( iBin ) ); }
128  // the edges
129  stream << " 'edges' : ";
130  Gaudi::Utils::toStream( edges, stream ) << " }," << std::endl;
131  } else {
132  stream << "'nbins' : ";
133  Gaudi::Utils::toStream( yBins, stream ) << " , ";
134  stream << "'low' : ";
135  Gaudi::Utils::toStream( yaxis->GetXmin(), stream ) << " , ";
136  stream << "'high' : ";
137  Gaudi::Utils::toStream( yaxis->GetXmax(), stream ) << " }, " << std::endl;
138  }
139  //
140  // finally: the content
141  stream << "'bins' : " << std::endl << " [ ";
142  for ( int jBin = yBins + 1; jBin >= 0; --jBin ) {
143  if ( yBins + 1 != jBin ) { stream << std::endl; }
144  for ( int iBin = 0; iBin <= xBins + 1; ++iBin ) {
145  //
146  Gaudi::Utils::toStream( std::make_pair( histo.GetBinContent( iBin, jBin ), histo.GetBinError( iBin, jBin ) ),
147  stream );
148  //
149  if ( xBins + 1 != iBin || 0 != jBin ) { stream << " , "; }
150  }
151  }
152  stream << " ]";
153  //
154  stream << " }";
155  //
156  return stream;
157  }
158  // ==========================================================================
159 } // end of anonymous namespace
160 // ============================================================================
161 /* stream the ROOT histogram into output stream
162  * @param histo (INPUT) the histogram to be streamed
163  * @param stream (OUTPUT) the stream
164  * @param asXML (INPUT) use XML-format
165  */
166 // ============================================================================
167 std::ostream& Gaudi::Utils::toStream( const TH1D& histo, std::ostream& stream, const bool asXML ) {
168  return _toStream_1D_( histo, stream, asXML );
169 }
170 // ============================================================================
171 /* stream the ROOT histogram into output stream
172  * @param histo (INPUT) the histogram to be streamed
173  * @param stream (OUTPUT) the stream
174  * @param asXML (INPUT) use XML-format
175  */
176 // ============================================================================
177 std::ostream& Gaudi::Utils::toStream( const TH1F& histo, std::ostream& stream, const bool asXML ) {
178  return _toStream_1D_( histo, stream, asXML );
179 }
180 // ============================================================================
181 /* stream the ROOT histogram into output stream
182  * @param histo (INPUT) the histogram to be streamed
183  * @param stream (OUTPUT) the stream
184  * @param asXML (INPUT) use XML-format
185  */
186 // ============================================================================
187 std::ostream& Gaudi::Utils::toStream( const TH2D& histo, std::ostream& stream, const bool asXML ) {
188  return _toStream_2D_( histo, stream, asXML );
189 }
190 // ============================================================================
191 /* stream the ROOT histogram into output stream
192  * @param histo (INPUT) the histogram to be streamed
193  * @param stream (OUTPUT) the stream
194  * @param asXML (INPUT) use XML-format
195  */
196 // ============================================================================
197 std::ostream& Gaudi::Utils::toStream( const TH2F& histo, std::ostream& stream, const bool asXML ) {
198  return _toStream_2D_( histo, stream, asXML );
199 }
200 // ============================================================================
201 /* stream the AIDA histogram into output stream
202  * @param histo (INPUT) the histogram to be streamed
203  * @param stream (OUTPUT) the stream
204  * @param asXML (INPUT) use XML-format
205  */
206 // ============================================================================
207 std::ostream& Gaudi::Utils::toStream( const AIDA::IHistogram1D& histo, std::ostream& stream, const bool asXML ) {
209  return root ? toStream( *root, stream, asXML ) : stream;
210 }
211 // ============================================================================
212 /* stream the AIDA histogram into output stream
213  * @param histo (INPUT) the histogram to be streamed
214  * @param stream (OUTPUT) the stream
215  * @param asXML (INPUT) use XML-format
216  */
217 // ============================================================================
218 std::ostream& Gaudi::Utils::toStream( const AIDA::IHistogram2D& histo, std::ostream& stream, const bool asXML ) {
219  //
221  return root ? toStream( *root, stream, asXML ) : stream;
222 }
223 // ============================================================================
224 /* convert the histogram into the string
225  * @param histo (INPUT) the histogram to be streamed
226  * @param asXML (INPUT) use XML-format
227  * @return the string representation of the histogram
228  * @author Vanya BELYAEV Ivan.Belyaev@nikhef.nl
229  * @date 2009-09-26
230  */
231 // ============================================================================
232 std::string Gaudi::Utils::toString( const TH1D& histo, const bool asXML ) {
234  toStream( histo, o, asXML );
235  return o.str();
236 }
237 // ============================================================================
238 /* convert the histogram into the string
239  * @param histo (INPUT) the histogram to be streamed
240  * @param asXML (INPUT) use XML-format
241  * @return the string representation of the histogram
242  * @author Vanya BELYAEV Ivan.Belyaev@nikhef.nl
243  * @date 2009-09-26
244  */
245 // ============================================================================
246 std::string Gaudi::Utils::toString( const TH1F& histo, const bool asXML ) {
248  toStream( histo, o, asXML );
249  return o.str();
250 }
251 // ============================================================================
252 /* convert the histogram into the string
253  * @param histo (INPUT) the histogram to be streamed
254  * @param asXML (INPUT) use XML-format
255  * @return the string representation of the histogram
256  * @author Vanya BELYAEV Ivan.Belyaev@nikhef.nl
257  * @date 2009-09-26
258  */
259 // ============================================================================
260 std::string Gaudi::Utils::toString( const TH2D& histo, const bool asXML ) {
262  toStream( histo, o, asXML );
263  return o.str();
264 }
265 // ============================================================================
266 /* convert the histogram into the string
267  * @param histo (INPUT) the histogram to be streamed
268  * @param asXML (INPUT) use XML-format
269  * @return the string representation of the histogram
270  * @author Vanya BELYAEV Ivan.Belyaev@nikhef.nl
271  * @date 2009-09-26
272  */
273 // ============================================================================
274 std::string Gaudi::Utils::toString( const TH2F& histo, const bool asXML ) {
276  toStream( histo, o, asXML );
277  return o.str();
278 }
279 // ============================================================================
280 /* convert the histogram into the string
281  * @param histo (INPUT) the histogram to be streamed
282  * @param asXML (INPUT) use XML-format
283  * @return the string representation of the histogram
284  * @author Vanya BELYAEV Ivan.Belyaev@nikhef.nl
285  * @date 2009-09-26
286  */
287 // ============================================================================
288 std::string Gaudi::Utils::toString( const AIDA::IHistogram1D& histo, const bool asXML ) {
290  toStream( histo, o, asXML );
291  return o.str();
292 }
293 // =============================================================================
294 /* convert the histogram into the string
295  * @param histo (INPUT) the histogram to be streamed
296  * @param asXML (INPUT) use XML-format
297  * @return the string representation of the histogram
298  * @author Vanya BELYAEV Ivan.Belyaev@nikhef.nl
299  * @date 2009-09-26
300  */
301 // ============================================================================
302 std::string Gaudi::Utils::toString( const AIDA::IHistogram2D& histo, const bool asXML ) {
304  toStream( histo, o, asXML );
305  return o.str();
306 }
307 // =============================================================================
308 /* convert the histogram into the string
309  * @param histo (INPUT) the histogram to be streamed
310  * @param asXML (INPUT) use XML-format
311  * @return the string representation of the histogram
312  * @author Vanya BELYAEV Ivan.Belyaev@nikhef.nl
313  * @date 2009-09-26
314  */
315 // =============================================================================
316 std::string Gaudi::Utils::toString( const AIDA::IHistogram1D* histo ) { return histo ? toString( *histo ) : "{}"; }
317 // ============================================================================
318 /* convert the histogram into the string
319  * @param histo (INPUT) the histogram to be streamed
320  * @param asXML (INPUT) use XML-format
321  * @return the string representation of the histogram
322  * @author Vanya BELYAEV Ivan.Belyaev@nikhef.nl
323  * @date 2009-09-26
324  */
325 // =============================================================================
326 std::string Gaudi::Utils::toString( AIDA::IHistogram1D* histo ) { return histo ? toString( *histo ) : "{}"; }
327 // ============================================================================
328 /* convert the histogram into the string
329  * @param histo (INPUT) the histogram to be streamed
330  * @param asXML (INPUT) use XML-format
331  * @return the string representation of the histogram
332  * @author Vanya BELYAEV Ivan.Belyaev@nikhef.nl
333  * @date 2009-09-26
334  */
335 // =============================================================================
336 std::string Gaudi::Utils::toString( const TH1D* histo ) { return histo ? toString( *histo ) : "{}"; }
337 // ============================================================================
338 /* convert the histogram into the string
339  * @param histo (INPUT) the histogram to be streamed
340  * @param asXML (INPUT) use XML-format
341  * @return the string representation of the histogram
342  * @author Vanya BELYAEV Ivan.Belyaev@nikhef.nl
343  * @date 2009-09-26
344  */
345 // =============================================================================
346 std::string Gaudi::Utils::toString( const TH2D* histo ) { return histo ? toString( *histo ) : "{}"; }
347 // ============================================================================
348 /* convert the histogram into the string
349  * @param histo (INPUT) the histogram to be streamed
350  * @param asXML (INPUT) use XML-format
351  * @return the string representation of the histogram
352  * @author Vanya BELYAEV Ivan.Belyaev@nikhef.nl
353  * @date 2009-09-26
354  */
355 // =============================================================================
356 std::string Gaudi::Utils::toString( TH1D* histo ) { return histo ? toString( *histo ) : "{}"; }
357 // ============================================================================
358 /* convert the histogram into the string
359  * @param histo (INPUT) the histogram to be streamed
360  * @param asXML (INPUT) use XML-format
361  * @return the string representation of the histogram
362  * @author Vanya BELYAEV Ivan.Belyaev@nikhef.nl
363  * @date 2009-09-26
364  */
365 // =============================================================================
366 std::string Gaudi::Utils::toString( TH2D* histo ) { return histo ? toString( *histo ) : "{}"; }
367 // ============================================================================
368 // The END
369 // ============================================================================
Write.stream
stream
Definition: Write.py:31
std::string
STL class.
Aida2ROOT.h
HistoEx.histo
histo
Definition: HistoEx.py:103
std::vector< double >
gaudiComponentHelp.root
root
Definition: gaudiComponentHelp.py:38
Gaudi::Utils::Aida2ROOT::aida2root
static TH1D * aida2root(AIDA::IHistogram1D *aida)
get the underlying pointer for 1D-histogram
Definition: Aida2ROOT.cpp:68
ToStream.h
std::vector::push_back
T push_back(T... args)
std::ostream
STL class.
Histo2String.h
std::ostringstream
STL class.
Gaudi::Utils::toString
std::string toString(const TYPE &obj)
the generic implementation of the type conversion to the string
Definition: ToStream.h:353
toStream
std::ostream & toStream(const DataObjID &d, std::ostream &os)
Definition: DataObjID.cpp:93
std::vector::emplace_back
T emplace_back(T... args)
std::endl
T endl(T... args)
Gaudi::Utils::toStream
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:299
std::ostringstream::str
T str(T... args)
std::make_pair
T make_pair(T... args)
HistoXML.h
Gaudi::Utils::Histos::toXml
GAUDI_API std::ostream & toXml(const TH1D &histo, std::ostream &stream)
stream the ROOT histogram into output stream as XML
Definition: HistoXML.cpp:74