| The Gaudi Framework v30r0 (c919700c)

- Main Page
- Related Pages
- Modules
- Namespaces
- Classes
- Files

- File List
- File Members

Histo2String.cpp
Go to the documentation of this file.
1 // ============================================================================
2 // Include files
3 // ============================================================================
4 // ROOT
5 // ============================================================================
6 #include "TH1D.h"
7 #include "TH1F.h"
8 #include "TH2D.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"
23 #include "GaudiUtils/Histo2String.h"
24 #include "GaudiUtils/HistoXML.h"
25 // ============================================================================
31 // ============================================================================
32 namespace
33 {
34 // ==========================================================================
35 template <class HISTO>
36 std::ostream& _toStream_1D_( const HISTO& histo, std::ostream& stream, const bool asXML )
37 {
38 if ( asXML ) {
39 return Gaudi::Utils::Histos::toXml( histo, stream );
40 }
41 //
42 stream << "{ ";
43 //
44 stream << "'name' : ";
45 Gaudi::Utils::toStream( std::string( histo.GetName() ), stream ) << " , ";
46 stream << "'title' : ";
47 Gaudi::Utils::toStream( std::string( histo.GetTitle() ), stream ) << " , ";
48 //
49 const TAxis* axis = histo.GetXaxis();
50 const unsigned int nBins = axis->GetNbins();
51 //
52 stream << std::endl << "'X' : { ";
53 if ( axis->IsVariableBinSize() ) {
54 const TArrayD* xbins = axis->GetXbins();
55 const unsigned int xsize = xbins->GetSize();
56 std::vector<double> edges;
57 for ( unsigned int iBin = 0; iBin < xsize; ++iBin ) {
58 edges.push_back( xbins->At( iBin ) );
59 }
60 stream << "'edges' : ";
61 Gaudi::Utils::toStream( edges, stream ) << " }, " << std::endl;
62 } else {
63 stream << "'nbins' : ";
64 Gaudi::Utils::toStream( nBins, stream ) << " , ";
65 stream << "'low' : ";
66 Gaudi::Utils::toStream( axis->GetXmin(), stream ) << " , ";
67 stream << "'high' : ";
68 Gaudi::Utils::toStream( axis->GetXmax(), stream ) << " }, " << std::endl;
69 }
70 // finally: the content
71 std::vector<std::pair<double, double>> bins;
72 for ( unsigned int iBin = 0; iBin <= nBins + 1; ++iBin ) {
73 bins.emplace_back( histo.GetBinContent( iBin ), histo.GetBinError( iBin ) );
74 }
75 stream << "'bins' : ";
76 Gaudi::Utils::toStream( bins, stream );
77 //
78 stream << " }";
79 //
80 return stream;
81 }
82 // ==========================================================================
83 template <class HISTO>
84 std::ostream& _toStream_2D_( const HISTO& histo, std::ostream& stream, const bool asXML )
85 {
86 if ( asXML ) {
87 return Gaudi::Utils::Histos::toXml( histo, stream );
88 }
89 //
90 stream << "{ ";
91 //
92 stream << "'name' : ";
93 Gaudi::Utils::toStream( std::string( histo.GetName() ), stream ) << " , ";
94 stream << "'title' : ";
95 Gaudi::Utils::toStream( std::string( histo.GetTitle() ), stream ) << " , ";
96 //
97 const TAxis* xaxis = histo.GetXaxis();
98 const int xBins = xaxis->GetNbins();
99 //
100 stream << std::endl << "'X' : { ";
101 if ( xaxis->IsVariableBinSize() ) {
102 const TArrayD* xbins = xaxis->GetXbins();
103 const unsigned int xsize = xbins->GetSize();
104 std::vector<double> edges;
105 for ( unsigned int iBin = 0; iBin < xsize; ++iBin ) {
106 edges.push_back( xbins->At( iBin ) );
107 }
108 // the edges
109 stream << "'edges' : ";
110 Gaudi::Utils::toStream( edges, stream ) << " }," << std::endl;
111 } else {
112 stream << "'nbins' : ";
113 Gaudi::Utils::toStream( xBins, stream ) << " , ";
114 stream << "'low' : ";
115 Gaudi::Utils::toStream( xaxis->GetXmin(), stream ) << " , ";
116 stream << "'high' : ";
117 Gaudi::Utils::toStream( xaxis->GetXmax(), stream ) << " }, " << std::endl;
118 }
119 //
120 const TAxis* yaxis = histo.GetYaxis();
121 const int yBins = yaxis->GetNbins();
122 //
123 stream << std::endl << "'Y' : { ";
124 if ( yaxis->IsVariableBinSize() ) {
125 const TArrayD* ybins = yaxis->GetXbins();
126 const unsigned int ysize = ybins->GetSize();
127 std::vector<double> edges;
128 for ( unsigned int iBin = 0; iBin < ysize; ++iBin ) {
129 edges.push_back( ybins->At( iBin ) );
130 }
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 ) {
147 stream << std::endl;
148 }
149 for ( int iBin = 0; iBin <= xBins + 1; ++iBin ) {
150 //
151 Gaudi::Utils::toStream( std::make_pair( histo.GetBinContent( iBin, jBin ), histo.GetBinError( iBin, jBin ) ),
152 stream );
153 //
154 if ( xBins + 1 != iBin || 0 != jBin ) {
155 stream << " , ";
156 }
157 }
158 }
159 stream << " ]";
160 //
161 stream << " }";
162 //
163 return stream;
164 }
165 // ==========================================================================
166 } // end of anonymous namespace
167 // ============================================================================
168 /* stream the ROOT histogram into output stream
169 * @param histo (INPUT) the histogram to be streamed
170 * @param stream (OUTPUT) the stream
171 * @param asXML (INPUT) use XML-format
172 */
173 // ============================================================================
174 std::ostream& Gaudi::Utils::toStream( const TH1D& histo, std::ostream& stream, const bool asXML )
175 {
176 return _toStream_1D_( histo, stream, asXML );
177 }
178 // ============================================================================
179 /* stream the ROOT histogram into output stream
180 * @param histo (INPUT) the histogram to be streamed
181 * @param stream (OUTPUT) the stream
182 * @param asXML (INPUT) use XML-format
183 */
184 // ============================================================================
185 std::ostream& Gaudi::Utils::toStream( const TH1F& histo, std::ostream& stream, const bool asXML )
186 {
187 return _toStream_1D_( histo, stream, asXML );
188 }
189 // ============================================================================
190 /* stream the ROOT histogram into output stream
191 * @param histo (INPUT) the histogram to be streamed
192 * @param stream (OUTPUT) the stream
193 * @param asXML (INPUT) use XML-format
194 */
195 // ============================================================================
196 std::ostream& Gaudi::Utils::toStream( const TH2D& histo, std::ostream& stream, const bool asXML )
197 {
198 return _toStream_2D_( histo, stream, asXML );
199 }
200 // ============================================================================
201 /* stream the ROOT 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 TH2F& histo, std::ostream& stream, const bool asXML )
208 {
209 return _toStream_2D_( histo, stream, asXML );
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::IHistogram1D& histo, std::ostream& stream, const bool asXML )
219 {
220 auto root = Gaudi::Utils::Aida2ROOT::aida2root( &histo );
221 return root ? toStream( *root, stream, asXML ) : stream;
222 }
223 // ============================================================================
224 /* stream the AIDA histogram into output stream
225 * @param histo (INPUT) the histogram to be streamed
226 * @param stream (OUTPUT) the stream
227 * @param asXML (INPUT) use XML-format
228 */
229 // ============================================================================
230 std::ostream& Gaudi::Utils::toStream( const AIDA::IHistogram2D& histo, std::ostream& stream, const bool asXML )
231 {
232 //
233 auto root = Gaudi::Utils::Aida2ROOT::aida2root( &histo );
234 return root ? toStream( *root, stream, asXML ) : stream;
235 }
236 // ============================================================================
237 /* convert the histogram into the string
238 * @param histo (INPUT) the histogram to be streamed
239 * @param asXML (INPUT) use XML-format
240 * @return the string representation of the histogram
241 * @author Vanya BELYAEV Ivan.Belyaev@nikhef.nl
242 * @date 2009-09-26
243 */
244 // ============================================================================
245 std::string Gaudi::Utils::toString( const TH1D& histo, const bool asXML )
246 {
247 std::ostringstream o;
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 TH1F& histo, const bool asXML )
261 {
262 std::ostringstream o;
263 toStream( histo, o, asXML );
264 return o.str();
265 }
266 // ============================================================================
267 /* convert the histogram into the string
268 * @param histo (INPUT) the histogram to be streamed
269 * @param asXML (INPUT) use XML-format
270 * @return the string representation of the histogram
271 * @author Vanya BELYAEV Ivan.Belyaev@nikhef.nl
272 * @date 2009-09-26
273 */
274 // ============================================================================
275 std::string Gaudi::Utils::toString( const TH2D& histo, const bool asXML )
276 {
277 std::ostringstream o;
278 toStream( histo, o, asXML );
279 return o.str();
280 }
281 // ============================================================================
282 /* convert the histogram into the string
283 * @param histo (INPUT) the histogram to be streamed
284 * @param asXML (INPUT) use XML-format
285 * @return the string representation of the histogram
286 * @author Vanya BELYAEV Ivan.Belyaev@nikhef.nl
287 * @date 2009-09-26
288 */
289 // ============================================================================
290 std::string Gaudi::Utils::toString( const TH2F& histo, const bool asXML )
291 {
292 std::ostringstream o;
293 toStream( histo, o, asXML );
294 return o.str();
295 }
296 // ============================================================================
297 /* convert the histogram into the string
298 * @param histo (INPUT) the histogram to be streamed
299 * @param asXML (INPUT) use XML-format
300 * @return the string representation of the histogram
301 * @author Vanya BELYAEV Ivan.Belyaev@nikhef.nl
302 * @date 2009-09-26
303 */
304 // ============================================================================
305 std::string Gaudi::Utils::toString( const AIDA::IHistogram1D& histo, 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( const AIDA::IHistogram2D& histo, const bool asXML )
321 {
322 std::ostringstream o;
323 toStream( histo, o, asXML );
324 return o.str();
325 }
326 // =============================================================================
327 /* convert the histogram into the string
328 * @param histo (INPUT) the histogram to be streamed
329 * @param asXML (INPUT) use XML-format
330 * @return the string representation of the histogram
331 * @author Vanya BELYAEV Ivan.Belyaev@nikhef.nl
332 * @date 2009-09-26
333 */
334 // =============================================================================
335 std::string Gaudi::Utils::toString( const AIDA::IHistogram1D* histo ) { return histo ? toString( *histo ) : "{}"; }
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 // =============================================================================
345 std::string Gaudi::Utils::toString( AIDA::IHistogram1D* histo ) { return histo ? toString( *histo ) : "{}"; }
346 // ============================================================================
347 /* convert the histogram into the string
348 * @param histo (INPUT) the histogram to be streamed
349 * @param asXML (INPUT) use XML-format
350 * @return the string representation of the histogram
351 * @author Vanya BELYAEV Ivan.Belyaev@nikhef.nl
352 * @date 2009-09-26
353 */
354 // =============================================================================
355 std::string Gaudi::Utils::toString( const TH1D* histo ) { return histo ? toString( *histo ) : "{}"; }
356 // ============================================================================
357 /* convert the histogram into the string
358 * @param histo (INPUT) the histogram to be streamed
359 * @param asXML (INPUT) use XML-format
360 * @return the string representation of the histogram
361 * @author Vanya BELYAEV Ivan.Belyaev@nikhef.nl
362 * @date 2009-09-26
363 */
364 // =============================================================================
365 std::string Gaudi::Utils::toString( const TH2D* histo ) { return histo ? toString( *histo ) : "{}"; }
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( TH1D* histo ) { return histo ? toString( *histo ) : "{}"; }
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( TH2D* histo ) { return histo ? toString( *histo ) : "{}"; }
386 // ============================================================================
387 // The END
388 // ============================================================================
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:293
Gaudi::Utils::toString
std::string toString(const TYPE &obj)
the generic implementation of the type conversion to the string
Definition: ToStream.h:346
std::endl
T endl(T...args)
HistoXML.h
std::string
STL class.
Aida2ROOT.h
std::vector::push_back
T push_back(T...args)
GaudiMath::Interpolation::toStream
std::ostream & toStream(const Type &, std::ostream &)
Definition: Interpolation.cpp:47
std::ostringstream
STL class.
Histo2String.h
std::ostringstream::str
T str(T...args)
std::make_pair
T make_pair(T...args)
Gaudi::Utils::Aida2ROOT::aida2root
static TH1D * aida2root(AIDA::IHistogram1D *aida)
get the underlying pointer for 1D-histogram
Definition: Aida2ROOT.cpp:73
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:66
std::vector< double >
gaudiComponentHelp.root
root
Definition: gaudiComponentHelp.py:18
ToStream.h
implementation of various functions for streaming.
std::ostream
STL class.
GaudiMath::Interpolation::toString
std::string toString(const Type &)
Definition: Interpolation.cpp:42
std::vector::emplace_back
T emplace_back(T...args)