All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups 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
74  std::vector<std::pair<double,double> > bins ;
75  for ( unsigned int iBin = 0 ; iBin <= nBins + 1 ; ++iBin )
76  {
77  bins.push_back ( std::make_pair( 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  //
161  Gaudi::Utils::toStream ( std::make_pair
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 // ============================================================================
183 std::ostream& Gaudi::Utils::toStream
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 // ============================================================================
194 std::ostream& Gaudi::Utils::toStream
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 // ============================================================================
205 std::ostream& Gaudi::Utils::toStream
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 // ============================================================================
216 std::ostream& Gaudi::Utils::toStream
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 // ============================================================================
227 std::ostream& Gaudi::Utils::toStream
228 ( const AIDA::IHistogram1D& histo ,
229  std::ostream& stream ,
230  const bool asXML )
231 {
232  //
233  AIDA::IHistogram1D* aida = const_cast<AIDA::IHistogram1D*> ( &histo ) ;
234  //
235  const TH1D* root = Gaudi::Utils::Aida2ROOT::aida2root ( aida ) ;
236  if ( 0 == root ) { return stream ; } // RETURN
237  //
238  return toStream ( *root , stream , asXML ) ;
239 }
240 // ============================================================================
241 /* stream the AIDA histogram into output stream
242  * @param histo (INPUT) the histogram to be streamed
243  * @param stream (OUTPUT) the stream
244  * @param asXML (INPUT) use XML-format
245  */
246 // ============================================================================
247 std::ostream& Gaudi::Utils::toStream
248 ( const AIDA::IHistogram2D& histo ,
249  std::ostream& stream ,
250  const bool asXML )
251 {
252  //
253  AIDA::IHistogram2D* aida = const_cast<AIDA::IHistogram2D*> ( &histo ) ;
254  //
255  const TH2D* root = Gaudi::Utils::Aida2ROOT::aida2root ( aida ) ;
256  if ( 0 == root ) { return stream ; } // RETURN
257  //
258  return toStream ( *root , stream , asXML ) ;
259 }
260 // ============================================================================
261 /* convert the histogram into the string
262  * @param histo (INPUT) the histogram to be streamed
263  * @param asXML (INPUT) use XML-format
264  * @return the string representation of the histogram
265  * @author Vanya BELYAEV Ivan.Belyaev@nikhef.nl
266  * @date 2009-09-26
267  */
268 // ============================================================================
269 std::string Gaudi::Utils::toString
270 ( const TH1D& histo ,
271  const bool asXML )
272 {
273  std::ostringstream o ;
274  toStream ( histo , o , asXML ) ;
275  return o.str() ;
276 }
277 // ============================================================================
278 /* convert the histogram into the string
279  * @param histo (INPUT) the histogram to be streamed
280  * @param asXML (INPUT) use XML-format
281  * @return the string representation of the histogram
282  * @author Vanya BELYAEV Ivan.Belyaev@nikhef.nl
283  * @date 2009-09-26
284  */
285 // ============================================================================
286 std::string Gaudi::Utils::toString
287 ( const TH1F& histo ,
288  const bool asXML )
289 {
290  std::ostringstream o ;
291  toStream ( histo , o , asXML ) ;
292  return o.str() ;
293 }
294 // ============================================================================
295 /* convert the histogram into the string
296  * @param histo (INPUT) the histogram to be streamed
297  * @param asXML (INPUT) use XML-format
298  * @return the string representation of the histogram
299  * @author Vanya BELYAEV Ivan.Belyaev@nikhef.nl
300  * @date 2009-09-26
301  */
302 // ============================================================================
303 std::string Gaudi::Utils::toString
304 ( const TH2D& histo ,
305  const bool asXML )
306 {
307  std::ostringstream o ;
308  toStream ( histo , o , asXML ) ;
309  return o.str() ;
310 }
311 // ============================================================================
312 /* convert the histogram into the string
313  * @param histo (INPUT) the histogram to be streamed
314  * @param asXML (INPUT) use XML-format
315  * @return the string representation of the histogram
316  * @author Vanya BELYAEV Ivan.Belyaev@nikhef.nl
317  * @date 2009-09-26
318  */
319 // ============================================================================
320 std::string Gaudi::Utils::toString
321 ( const TH2F& histo ,
322  const bool asXML )
323 {
324  std::ostringstream o ;
325  toStream ( histo , o , asXML ) ;
326  return o.str() ;
327 }
328 // ============================================================================
329 /* convert the histogram into the string
330  * @param histo (INPUT) the histogram to be streamed
331  * @param asXML (INPUT) use XML-format
332  * @return the string representation of the histogram
333  * @author Vanya BELYAEV Ivan.Belyaev@nikhef.nl
334  * @date 2009-09-26
335  */
336 // ============================================================================
337 std::string Gaudi::Utils::toString
338 ( const AIDA::IHistogram1D& histo ,
339  const bool asXML )
340 {
341  std::ostringstream o ;
342  toStream ( histo , o , asXML ) ;
343  return o.str() ;
344 }
345 // =============================================================================
346 /* convert the histogram into the string
347  * @param histo (INPUT) the histogram to be streamed
348  * @param asXML (INPUT) use XML-format
349  * @return the string representation of the histogram
350  * @author Vanya BELYAEV Ivan.Belyaev@nikhef.nl
351  * @date 2009-09-26
352  */
353 // ============================================================================
354 std::string Gaudi::Utils::toString
355 ( const AIDA::IHistogram2D& histo ,
356  const bool asXML )
357 {
358  std::ostringstream o ;
359  toStream ( histo , o , asXML ) ;
360  return o.str() ;
361 }
362 // =============================================================================
363 /* convert the histogram into the string
364  * @param histo (INPUT) the histogram to be streamed
365  * @param asXML (INPUT) use XML-format
366  * @return the string representation of the histogram
367  * @author Vanya BELYAEV Ivan.Belyaev@nikhef.nl
368  * @date 2009-09-26
369  */
370 // =============================================================================
371 std::string Gaudi::Utils::toString ( const AIDA::IHistogram1D* histo )
372 {
373  if ( 0 == histo ) { return std::string ("{}") ; }
374  return toString ( *histo ) ;
375 }
376 // ============================================================================
377 /* convert the histogram into the string
378  * @param histo (INPUT) the histogram to be streamed
379  * @param asXML (INPUT) use XML-format
380  * @return the string representation of the histogram
381  * @author Vanya BELYAEV Ivan.Belyaev@nikhef.nl
382  * @date 2009-09-26
383  */
384 // =============================================================================
385 std::string Gaudi::Utils::toString ( AIDA::IHistogram1D* histo )
386 {
387  if ( 0 == histo ) { return std::string ("{}") ; }
388  return toString ( *histo ) ;
389 }
390 // ============================================================================
391 /* convert the histogram into the string
392  * @param histo (INPUT) the histogram to be streamed
393  * @param asXML (INPUT) use XML-format
394  * @return the string representation of the histogram
395  * @author Vanya BELYAEV Ivan.Belyaev@nikhef.nl
396  * @date 2009-09-26
397  */
398 // =============================================================================
399 std::string Gaudi::Utils::toString ( const TH1D* histo )
400 {
401  if ( 0 == histo ) { return std::string ("{}") ; }
402  return toString ( *histo ) ;
403 }
404 // ============================================================================
405 /* convert the histogram into the string
406  * @param histo (INPUT) the histogram to be streamed
407  * @param asXML (INPUT) use XML-format
408  * @return the string representation of the histogram
409  * @author Vanya BELYAEV Ivan.Belyaev@nikhef.nl
410  * @date 2009-09-26
411  */
412 // =============================================================================
413 std::string Gaudi::Utils::toString ( const TH2D* histo )
414 {
415  if ( 0 == histo ) { return std::string ("{}") ; }
416  return 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 // =============================================================================
427 std::string Gaudi::Utils::toString ( TH1D* histo )
428 {
429  if ( 0 == histo ) { return std::string ("{}") ; }
430  return toString ( *histo ) ;
431 }
432 // ============================================================================
433 /* convert the histogram into the string
434  * @param histo (INPUT) the histogram to be streamed
435  * @param asXML (INPUT) use XML-format
436  * @return the string representation of the histogram
437  * @author Vanya BELYAEV Ivan.Belyaev@nikhef.nl
438  * @date 2009-09-26
439  */
440 // =============================================================================
441 std::string Gaudi::Utils::toString ( TH2D* histo )
442 {
443  if ( 0 == histo ) { return std::string ("{}") ; }
444  return toString ( *histo ) ;
445 }
446 // ============================================================================
447 // The END
448 // ============================================================================
std::string toString(const TYPE &obj)
the generic implementation of the type conversion to the string
Definition: ToStream.h:367
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:341
static TH1D * aida2root(AIDA::IHistogram1D *aida)
get the underlying pointer for 1D-histogram
Definition: Aida2ROOT.cpp:55
GAUDI_API std::ostream & toXml(const TH1D &histo, std::ostream &stream)
stream the ROOT histogram into output stream as XML
Definition: HistoXML.cpp:70
tuple root
Definition: IOTest.py:42
implemenattiono fvarioud functions for streaming.