All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Modules Pages
Histo2String.cpp
Go to the documentation of this file.
1 // ============================================================================
2 // Include files
3 // ============================================================================
4 // ROOT
5 // ============================================================================
6 #include "TH1D.h"
7 #include "TH2D.h"
8 #include "TH1F.h"
9 #include "TH2F.h"
10 // ============================================================================
11 // AIDA
12 // ============================================================================
13 #include "AIDA/IHistogram1D.h"
14 #include "AIDA/IHistogram2D.h"
15 // ============================================================================
16 // GaudiKernel
17 // ============================================================================
18 #include "GaudiKernel/ToStream.h"
19 // ============================================================================
20 // local
21 // ============================================================================
22 #include "GaudiUtils/Aida2ROOT.h"
24 #include "GaudiUtils/HistoXML.h"
25 // ============================================================================
31 // ============================================================================
32 namespace
33 {
34  // ==========================================================================
35  template <class HISTO>
36  std::ostream& _toStream_1D_
37  ( const HISTO& histo ,
38  std::ostream& stream ,
39  const bool asXML )
40  {
41  if ( asXML ) { return Gaudi::Utils::Histos::toXml ( histo , stream ) ; }
42  //
43  stream << "{ " ;
44  //
45  stream << "'name' : " ;
46  Gaudi::Utils::toStream ( std::string ( histo.GetName () ) , stream ) << " , " ;
47  stream << "'title' : " ;
48  Gaudi::Utils::toStream ( std::string ( histo.GetTitle() ) , stream ) << " , " ;
49  //
50  const TAxis* axis = histo.GetXaxis() ;
51  const unsigned int nBins = axis->GetNbins() ;
52  //
53  stream << std::endl << "'X' : { " ;
54  if ( axis->IsVariableBinSize() )
55  {
56  const TArrayD* xbins = axis->GetXbins() ;
57  const unsigned int xsize = xbins->GetSize() ;
58  std::vector<double> edges ;
59  for ( unsigned int iBin = 0 ; iBin < xsize ; ++iBin )
60  { edges.push_back ( xbins->At( iBin ) ) ; }
61  stream << "'edges' : " ;
62  Gaudi::Utils::toStream ( edges , stream ) << " }, " << std::endl;
63  }
64  else
65  {
66  stream << "'nbins' : " ;
67  Gaudi::Utils::toStream ( nBins , stream ) << " , " ;
68  stream << "'low' : " ;
69  Gaudi::Utils::toStream ( axis->GetXmin () , stream ) << " , " ;
70  stream << "'high' : " ;
71  Gaudi::Utils::toStream ( axis->GetXmax () , stream ) << " }, " << std::endl ;
72  }
73  // finally: the content
75  for ( unsigned int iBin = 0 ; iBin <= nBins + 1 ; ++iBin )
76  {
77  bins.emplace_back( histo.GetBinContent( iBin ) ,
78  histo.GetBinError ( iBin ) ) ;
79  }
80  stream << "'bins' : " ;
81  Gaudi::Utils::toStream ( bins , stream ) ;
82  //
83  stream << " }" ;
84  //
85  return stream ;
86  }
87  // ==========================================================================
88  template <class HISTO>
89  std::ostream& _toStream_2D_
90  ( const HISTO& histo ,
91  std::ostream& stream ,
92  const bool asXML )
93  {
94  if ( asXML ) { return Gaudi::Utils::Histos::toXml ( histo , stream ) ; }
95  //
96  stream << "{ " ;
97  //
98  stream << "'name' : " ;
99  Gaudi::Utils::toStream ( std::string( histo.GetName () ) , stream ) << " , " ;
100  stream << "'title' : " ;
101  Gaudi::Utils::toStream ( std::string( histo.GetTitle() ) , stream ) << " , " ;
102  //
103  const TAxis* xaxis = histo.GetXaxis() ;
104  const int xBins = xaxis->GetNbins() ;
105  //
106  stream << std::endl << "'X' : { " ;
107  if ( xaxis->IsVariableBinSize() )
108  {
109  const TArrayD* xbins = xaxis->GetXbins() ;
110  const unsigned int xsize = xbins->GetSize() ;
111  std::vector<double> edges ;
112  for ( unsigned int iBin = 0 ; iBin < xsize ; ++iBin )
113  { edges.push_back ( xbins->At( iBin ) ) ; }
114  // the edges
115  stream << "'edges' : " ;
116  Gaudi::Utils::toStream ( edges , stream ) << " }," << std::endl;
117  }
118  else
119  {
120  stream << "'nbins' : " ;
121  Gaudi::Utils::toStream ( xBins , stream ) << " , " ;
122  stream << "'low' : " ;
123  Gaudi::Utils::toStream ( xaxis->GetXmin () , stream ) << " , " ;
124  stream << "'high' : " ;
125  Gaudi::Utils::toStream ( xaxis->GetXmax () , stream ) << " }, " << std::endl ;
126  }
127  //
128  const TAxis* yaxis = histo.GetYaxis() ;
129  const int yBins = yaxis->GetNbins() ;
130  //
131  stream << std::endl << "'Y' : { " ;
132  if ( yaxis->IsVariableBinSize() )
133  {
134  const TArrayD* ybins = yaxis->GetXbins() ;
135  const unsigned int ysize = ybins->GetSize() ;
136  std::vector<double> edges ;
137  for ( unsigned int iBin = 0 ; iBin < ysize ; ++iBin )
138  { edges.push_back ( ybins->At( iBin ) ) ; }
139  // the edges
140  stream << " 'edges' : " ;
141  Gaudi::Utils::toStream ( edges , stream ) << " }," << std::endl;
142  }
143  else
144  {
145  stream << "'nbins' : " ;
146  Gaudi::Utils::toStream ( yBins , stream ) << " , " ;
147  stream << "'low' : " ;
148  Gaudi::Utils::toStream ( yaxis->GetXmin () , stream ) << " , " ;
149  stream << "'high' : " ;
150  Gaudi::Utils::toStream ( yaxis->GetXmax () , stream ) << " }, " << std::endl ;
151  }
152  //
153  // finally: the content
154  stream << "'bins' : " << std::endl << " [ " ;
155  for ( int jBin = yBins + 1 ; jBin >= 0 ; --jBin )
156  {
157  if ( yBins + 1 != jBin ) { stream << std::endl ; }
158  for ( int iBin = 0 ; iBin <= xBins + 1 ; ++iBin )
159  {
160  //
162  ( histo.GetBinContent( iBin , jBin ) ,
163  histo.GetBinError ( iBin , jBin ) ) , stream ) ;
164  //
165  if ( xBins + 1 != iBin || 0 != jBin ) { stream << " , " ; }
166  }
167  }
168  stream << " ]" ;
169  //
170  stream << " }" ;
171  //
172  return stream ;
173  }
174  // ==========================================================================
175 } // end of anonymous namespace
176 // ============================================================================
177 /* stream the ROOT histogram into output stream
178  * @param histo (INPUT) the histogram to be streamed
179  * @param stream (OUTPUT) the stream
180  * @param asXML (INPUT) use XML-format
181  */
182 // ============================================================================
184 ( const TH1D& histo ,
185  std::ostream& stream ,
186  const bool asXML ) { return _toStream_1D_ ( histo , stream , asXML ) ; }
187 // ============================================================================
188 /* stream the ROOT histogram into output stream
189  * @param histo (INPUT) the histogram to be streamed
190  * @param stream (OUTPUT) the stream
191  * @param asXML (INPUT) use XML-format
192  */
193 // ============================================================================
195 ( const TH1F& histo ,
196  std::ostream& stream ,
197  const bool asXML ) { return _toStream_1D_ ( histo , stream , asXML ) ; }
198 // ============================================================================
199 /* stream the ROOT histogram into output stream
200  * @param histo (INPUT) the histogram to be streamed
201  * @param stream (OUTPUT) the stream
202  * @param asXML (INPUT) use XML-format
203  */
204 // ============================================================================
206 ( const TH2D& histo ,
207  std::ostream& stream ,
208  const bool asXML ) { return _toStream_2D_ ( histo , stream , asXML ) ; }
209 // ============================================================================
210 /* stream the ROOT histogram into output stream
211  * @param histo (INPUT) the histogram to be streamed
212  * @param stream (OUTPUT) the stream
213  * @param asXML (INPUT) use XML-format
214  */
215 // ============================================================================
217 ( const TH2F& histo ,
218  std::ostream& stream ,
219  const bool asXML ) { return _toStream_2D_ ( histo , stream , asXML ) ; }
220 // ============================================================================
221 /* stream the AIDA histogram into output stream
222  * @param histo (INPUT) the histogram to be streamed
223  * @param stream (OUTPUT) the stream
224  * @param asXML (INPUT) use XML-format
225  */
226 // ============================================================================
228 ( const AIDA::IHistogram1D& histo ,
229  std::ostream& stream ,
230  const bool asXML )
231 {
232  auto root = Gaudi::Utils::Aida2ROOT::aida2root ( &histo ) ;
233  return root ? toStream ( *root , stream , asXML ) : stream ;
234 }
235 // ============================================================================
236 /* stream the AIDA histogram into output stream
237  * @param histo (INPUT) the histogram to be streamed
238  * @param stream (OUTPUT) the stream
239  * @param asXML (INPUT) use XML-format
240  */
241 // ============================================================================
243 ( const AIDA::IHistogram2D& histo ,
244  std::ostream& stream ,
245  const bool asXML )
246 {
247  //
248  auto root = Gaudi::Utils::Aida2ROOT::aida2root ( &histo ) ;
249  return root ? toStream ( *root , stream , asXML ) : stream;
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 // ============================================================================
261 ( const TH1D& histo ,
262  const bool asXML )
263 {
265  toStream ( histo , o , asXML ) ;
266  return o.str() ;
267 }
268 // ============================================================================
269 /* convert the histogram into the string
270  * @param histo (INPUT) the histogram to be streamed
271  * @param asXML (INPUT) use XML-format
272  * @return the string representation of the histogram
273  * @author Vanya BELYAEV Ivan.Belyaev@nikhef.nl
274  * @date 2009-09-26
275  */
276 // ============================================================================
278 ( const TH1F& histo ,
279  const bool asXML )
280 {
282  toStream ( histo , o , asXML ) ;
283  return o.str() ;
284 }
285 // ============================================================================
286 /* convert the histogram into the string
287  * @param histo (INPUT) the histogram to be streamed
288  * @param asXML (INPUT) use XML-format
289  * @return the string representation of the histogram
290  * @author Vanya BELYAEV Ivan.Belyaev@nikhef.nl
291  * @date 2009-09-26
292  */
293 // ============================================================================
295 ( const TH2D& histo ,
296  const bool asXML )
297 {
299  toStream ( histo , o , asXML ) ;
300  return o.str() ;
301 }
302 // ============================================================================
303 /* convert the histogram into the string
304  * @param histo (INPUT) the histogram to be streamed
305  * @param asXML (INPUT) use XML-format
306  * @return the string representation of the histogram
307  * @author Vanya BELYAEV Ivan.Belyaev@nikhef.nl
308  * @date 2009-09-26
309  */
310 // ============================================================================
312 ( const TH2F& histo ,
313  const bool asXML )
314 {
316  toStream ( histo , o , asXML ) ;
317  return o.str() ;
318 }
319 // ============================================================================
320 /* convert the histogram into the string
321  * @param histo (INPUT) the histogram to be streamed
322  * @param asXML (INPUT) use XML-format
323  * @return the string representation of the histogram
324  * @author Vanya BELYAEV Ivan.Belyaev@nikhef.nl
325  * @date 2009-09-26
326  */
327 // ============================================================================
329 ( const AIDA::IHistogram1D& histo ,
330  const bool asXML )
331 {
333  toStream ( histo , o , asXML ) ;
334  return o.str() ;
335 }
336 // =============================================================================
337 /* convert the histogram into the string
338  * @param histo (INPUT) the histogram to be streamed
339  * @param asXML (INPUT) use XML-format
340  * @return the string representation of the histogram
341  * @author Vanya BELYAEV Ivan.Belyaev@nikhef.nl
342  * @date 2009-09-26
343  */
344 // ============================================================================
346 ( const AIDA::IHistogram2D& histo ,
347  const bool asXML )
348 {
350  toStream ( histo , o , asXML ) ;
351  return o.str() ;
352 }
353 // =============================================================================
354 /* convert the histogram into the string
355  * @param histo (INPUT) the histogram to be streamed
356  * @param asXML (INPUT) use XML-format
357  * @return the string representation of the histogram
358  * @author Vanya BELYAEV Ivan.Belyaev@nikhef.nl
359  * @date 2009-09-26
360  */
361 // =============================================================================
362 std::string Gaudi::Utils::toString ( const AIDA::IHistogram1D* histo )
363 {
364  return histo ? toString ( *histo ) : "{}";
365 }
366 // ============================================================================
367 /* convert the histogram into the string
368  * @param histo (INPUT) the histogram to be streamed
369  * @param asXML (INPUT) use XML-format
370  * @return the string representation of the histogram
371  * @author Vanya BELYAEV Ivan.Belyaev@nikhef.nl
372  * @date 2009-09-26
373  */
374 // =============================================================================
375 std::string Gaudi::Utils::toString ( AIDA::IHistogram1D* histo )
376 {
377  return histo ? toString ( *histo ) : "{}";
378 }
379 // ============================================================================
380 /* convert the histogram into the string
381  * @param histo (INPUT) the histogram to be streamed
382  * @param asXML (INPUT) use XML-format
383  * @return the string representation of the histogram
384  * @author Vanya BELYAEV Ivan.Belyaev@nikhef.nl
385  * @date 2009-09-26
386  */
387 // =============================================================================
388 std::string Gaudi::Utils::toString ( const TH1D* histo )
389 {
390  return histo ? toString ( *histo ) : "{}";
391 }
392 // ============================================================================
393 /* convert the histogram into the string
394  * @param histo (INPUT) the histogram to be streamed
395  * @param asXML (INPUT) use XML-format
396  * @return the string representation of the histogram
397  * @author Vanya BELYAEV Ivan.Belyaev@nikhef.nl
398  * @date 2009-09-26
399  */
400 // =============================================================================
401 std::string Gaudi::Utils::toString ( const TH2D* histo )
402 {
403  return histo ? toString ( *histo ) : "{}";
404 }
405 // ============================================================================
406 /* convert the histogram into the string
407  * @param histo (INPUT) the histogram to be streamed
408  * @param asXML (INPUT) use XML-format
409  * @return the string representation of the histogram
410  * @author Vanya BELYAEV Ivan.Belyaev@nikhef.nl
411  * @date 2009-09-26
412  */
413 // =============================================================================
415 {
416  return histo ? toString ( *histo ) : "{}";
417 }
418 // ============================================================================
419 /* convert the histogram into the string
420  * @param histo (INPUT) the histogram to be streamed
421  * @param asXML (INPUT) use XML-format
422  * @return the string representation of the histogram
423  * @author Vanya BELYAEV Ivan.Belyaev@nikhef.nl
424  * @date 2009-09-26
425  */
426 // =============================================================================
428 {
429  return histo ? toString ( *histo ) : "{}";
430 }
431 // ============================================================================
432 // The END
433 // ============================================================================
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:315
std::string toString(const TYPE &obj)
the generic implementation of the type conversion to the string
Definition: ToStream.h:367
T endl(T...args)
STL class.
T push_back(T...args)
T make_pair(T...args)
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
implementation of various functions for streaming.
STL class.
T emplace_back(T...args)