| The Gaudi Framework v32r1 (f65d50dc)

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 template <class HISTO>
35 std::ostream& _toStream_1D_( const HISTO& histo, std::ostream& stream, const bool asXML ) {
36 if ( asXML ) { return Gaudi::Utils::Histos::toXml( histo, stream ); }
37 //
38 stream << "{ ";
39 //
40 stream << "'name' : ";
41 Gaudi::Utils::toStream( std::string( histo.GetName() ), stream ) << " , ";
42 stream << "'title' : ";
43 Gaudi::Utils::toStream( std::string( histo.GetTitle() ), stream ) << " , ";
44 //
45 const TAxis* axis = histo.GetXaxis();
46 const unsigned int nBins = axis->GetNbins();
47 //
48 stream << std::endl << "'X' : { ";
49 if ( axis->IsVariableBinSize() ) {
50 const TArrayD* xbins = axis->GetXbins();
51 const unsigned int xsize = xbins->GetSize();
52 std::vector<double> edges;
53 for ( unsigned int iBin = 0; iBin < xsize; ++iBin ) { edges.push_back( xbins->At( iBin ) ); }
54 stream << "'edges' : ";
55 Gaudi::Utils::toStream( edges, stream ) << " }, " << std::endl;
56 } else {
57 stream << "'nbins' : ";
58 Gaudi::Utils::toStream( nBins, stream ) << " , ";
59 stream << "'low' : ";
60 Gaudi::Utils::toStream( axis->GetXmin(), stream ) << " , ";
61 stream << "'high' : ";
62 Gaudi::Utils::toStream( axis->GetXmax(), stream ) << " }, " << std::endl;
63 }
64 // finally: the content
65 std::vector<std::pair<double, double>> bins;
66 for ( unsigned int iBin = 0; iBin <= nBins + 1; ++iBin ) {
67 bins.emplace_back( histo.GetBinContent( iBin ), histo.GetBinError( iBin ) );
68 }
69 stream << "'bins' : ";
70 Gaudi::Utils::toStream( bins, stream );
71 //
72 stream << " }";
73 //
74 return stream;
75 }
76 // ==========================================================================
77 template <class HISTO>
78 std::ostream& _toStream_2D_( const HISTO& histo, std::ostream& stream, const bool asXML ) {
79 if ( asXML ) { return Gaudi::Utils::Histos::toXml( histo, stream ); }
80 //
81 stream << "{ ";
82 //
83 stream << "'name' : ";
84 Gaudi::Utils::toStream( std::string( histo.GetName() ), stream ) << " , ";
85 stream << "'title' : ";
86 Gaudi::Utils::toStream( std::string( histo.GetTitle() ), stream ) << " , ";
87 //
88 const TAxis* xaxis = histo.GetXaxis();
89 const int xBins = xaxis->GetNbins();
90 //
91 stream << std::endl << "'X' : { ";
92 if ( xaxis->IsVariableBinSize() ) {
93 const TArrayD* xbins = xaxis->GetXbins();
94 const unsigned int xsize = xbins->GetSize();
95 std::vector<double> edges;
96 for ( unsigned int iBin = 0; iBin < xsize; ++iBin ) { edges.push_back( xbins->At( iBin ) ); }
97 // the edges
98 stream << "'edges' : ";
99 Gaudi::Utils::toStream( edges, stream ) << " }," << std::endl;
100 } else {
101 stream << "'nbins' : ";
102 Gaudi::Utils::toStream( xBins, stream ) << " , ";
103 stream << "'low' : ";
104 Gaudi::Utils::toStream( xaxis->GetXmin(), stream ) << " , ";
105 stream << "'high' : ";
106 Gaudi::Utils::toStream( xaxis->GetXmax(), stream ) << " }, " << std::endl;
107 }
108 //
109 const TAxis* yaxis = histo.GetYaxis();
110 const int yBins = yaxis->GetNbins();
111 //
112 stream << std::endl << "'Y' : { ";
113 if ( yaxis->IsVariableBinSize() ) {
114 const TArrayD* ybins = yaxis->GetXbins();
115 const unsigned int ysize = ybins->GetSize();
116 std::vector<double> edges;
117 for ( unsigned int iBin = 0; iBin < ysize; ++iBin ) { edges.push_back( ybins->At( iBin ) ); }
118 // the edges
119 stream << " 'edges' : ";
120 Gaudi::Utils::toStream( edges, stream ) << " }," << std::endl;
121 } else {
122 stream << "'nbins' : ";
123 Gaudi::Utils::toStream( yBins, stream ) << " , ";
124 stream << "'low' : ";
125 Gaudi::Utils::toStream( yaxis->GetXmin(), stream ) << " , ";
126 stream << "'high' : ";
127 Gaudi::Utils::toStream( yaxis->GetXmax(), stream ) << " }, " << std::endl;
128 }
129 //
130 // finally: the content
131 stream << "'bins' : " << std::endl << " [ ";
132 for ( int jBin = yBins + 1; jBin >= 0; --jBin ) {
133 if ( yBins + 1 != jBin ) { stream << std::endl; }
134 for ( int iBin = 0; iBin <= xBins + 1; ++iBin ) {
135 //
136 Gaudi::Utils::toStream( std::make_pair( histo.GetBinContent( iBin, jBin ), histo.GetBinError( iBin, jBin ) ),
137 stream );
138 //
139 if ( xBins + 1 != iBin || 0 != jBin ) { stream << " , "; }
140 }
141 }
142 stream << " ]";
143 //
144 stream << " }";
145 //
146 return stream;
147 }
148 // ==========================================================================
149 } // end of anonymous namespace
150 // ============================================================================
151 /* stream the ROOT histogram into output stream
152 * @param histo (INPUT) the histogram to be streamed
153 * @param stream (OUTPUT) the stream
154 * @param asXML (INPUT) use XML-format
155 */
156 // ============================================================================
157 std::ostream& Gaudi::Utils::toStream( const TH1D& histo, std::ostream& stream, const bool asXML ) {
158 return _toStream_1D_( histo, stream, asXML );
159 }
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 TH1F& 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 TH2D& histo, std::ostream& stream, const bool asXML ) {
178 return _toStream_2D_( 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 TH2F& histo, std::ostream& stream, const bool asXML ) {
188 return _toStream_2D_( histo, stream, asXML );
189 }
190 // ============================================================================
191 /* stream the AIDA 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 AIDA::IHistogram1D& histo, std::ostream& stream, const bool asXML ) {
198 auto root = Gaudi::Utils::Aida2ROOT::aida2root( &histo );
199 return root ? toStream( *root, stream, asXML ) : stream;
200 }
201 // ============================================================================
202 /* stream the AIDA histogram into output stream
203 * @param histo (INPUT) the histogram to be streamed
204 * @param stream (OUTPUT) the stream
205 * @param asXML (INPUT) use XML-format
206 */
207 // ============================================================================
208 std::ostream& Gaudi::Utils::toStream( const AIDA::IHistogram2D& histo, std::ostream& stream, const bool asXML ) {
209 //
210 auto root = Gaudi::Utils::Aida2ROOT::aida2root( &histo );
211 return root ? toStream( *root, stream, asXML ) : stream;
212 }
213 // ============================================================================
214 /* convert the histogram into the string
215 * @param histo (INPUT) the histogram to be streamed
216 * @param asXML (INPUT) use XML-format
217 * @return the string representation of the histogram
218 * @author Vanya BELYAEV Ivan.Belyaev@nikhef.nl
219 * @date 2009-09-26
220 */
221 // ============================================================================
222 std::string Gaudi::Utils::toString( const TH1D& histo, const bool asXML ) {
223 std::ostringstream o;
224 toStream( histo, o, asXML );
225 return o.str();
226 }
227 // ============================================================================
228 /* convert the histogram into the string
229 * @param histo (INPUT) the histogram to be streamed
230 * @param asXML (INPUT) use XML-format
231 * @return the string representation of the histogram
232 * @author Vanya BELYAEV Ivan.Belyaev@nikhef.nl
233 * @date 2009-09-26
234 */
235 // ============================================================================
236 std::string Gaudi::Utils::toString( const TH1F& histo, const bool asXML ) {
237 std::ostringstream o;
238 toStream( histo, o, asXML );
239 return o.str();
240 }
241 // ============================================================================
242 /* convert the histogram into the string
243 * @param histo (INPUT) the histogram to be streamed
244 * @param asXML (INPUT) use XML-format
245 * @return the string representation of the histogram
246 * @author Vanya BELYAEV Ivan.Belyaev@nikhef.nl
247 * @date 2009-09-26
248 */
249 // ============================================================================
250 std::string Gaudi::Utils::toString( const TH2D& histo, const bool asXML ) {
251 std::ostringstream o;
252 toStream( histo, o, asXML );
253 return o.str();
254 }
255 // ============================================================================
256 /* convert the histogram into the string
257 * @param histo (INPUT) the histogram to be streamed
258 * @param asXML (INPUT) use XML-format
259 * @return the string representation of the histogram
260 * @author Vanya BELYAEV Ivan.Belyaev@nikhef.nl
261 * @date 2009-09-26
262 */
263 // ============================================================================
264 std::string Gaudi::Utils::toString( const TH2F& histo, const bool asXML ) {
265 std::ostringstream o;
266 toStream( histo, o, asXML );
267 return o.str();
268 }
269 // ============================================================================
270 /* convert the histogram into the string
271 * @param histo (INPUT) the histogram to be streamed
272 * @param asXML (INPUT) use XML-format
273 * @return the string representation of the histogram
274 * @author Vanya BELYAEV Ivan.Belyaev@nikhef.nl
275 * @date 2009-09-26
276 */
277 // ============================================================================
278 std::string Gaudi::Utils::toString( const AIDA::IHistogram1D& histo, const bool asXML ) {
279 std::ostringstream o;
280 toStream( histo, o, asXML );
281 return o.str();
282 }
283 // =============================================================================
284 /* convert the histogram into the string
285 * @param histo (INPUT) the histogram to be streamed
286 * @param asXML (INPUT) use XML-format
287 * @return the string representation of the histogram
288 * @author Vanya BELYAEV Ivan.Belyaev@nikhef.nl
289 * @date 2009-09-26
290 */
291 // ============================================================================
292 std::string Gaudi::Utils::toString( const AIDA::IHistogram2D& histo, const bool asXML ) {
293 std::ostringstream o;
294 toStream( histo, o, asXML );
295 return o.str();
296 }
297 // =============================================================================
298 /* convert the histogram into the string
299 * @param histo (INPUT) the histogram to be streamed
300 * @param asXML (INPUT) use XML-format
301 * @return the string representation of the histogram
302 * @author Vanya BELYAEV Ivan.Belyaev@nikhef.nl
303 * @date 2009-09-26
304 */
305 // =============================================================================
306 std::string Gaudi::Utils::toString( const AIDA::IHistogram1D* histo ) { return histo ? toString( *histo ) : "{}"; }
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( 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( const TH1D* 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 TH2D* 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( TH1D* 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( TH2D* histo ) { return histo ? toString( *histo ) : "{}"; }
357 // ============================================================================
358 // The END
359 // ============================================================================
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:284
Gaudi::Utils::toString
std::string toString(const TYPE &obj)
the generic implementation of the type conversion to the string
Definition: ToStream.h:334
std::endl
T endl(T... args)
HistoXML.h
std::string
STL class.
Aida2ROOT.h
std::vector::push_back
T push_back(T... args)
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:70
toStream
std::ostream & toStream(const DataObjID &d, std::ostream &os)
Definition: DataObjID.cpp:82
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:64
std::vector< double >
gaudiComponentHelp.root
root
Definition: gaudiComponentHelp.py:27
ToStream.h
implementation of various functions for streaming.
std::ostream
STL class.
std::vector::emplace_back
T emplace_back(T... args)