Loading [MathJax]/extensions/tex2jax.js
The Gaudi Framework  v31r0 (aeb156f0)
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Modules Pages
HistoTableFormat.cpp
Go to the documentation of this file.
1 // ============================================================================
2 // Include files
3 // ============================================================================
4 // STD & STL
5 // ============================================================================
6 #include <cmath>
7 #include <string>
8 // ============================================================================
9 // AIDA
10 // ============================================================================
12 #include "GaudiKernel/Kernel.h"
13 #include <vector>
14 #ifdef __clang__
15 # pragma clang diagnostic push
16 # pragma clang diagnostic ignored "-Wkeyword-macro"
17 #endif
18 #define class class GAUDI_API
19 #ifdef __clang__
20 # pragma clang diagnostic pop
21 #endif
22 #include "AIDA/IAxis.h"
23 #include "AIDA/IHistogram1D.h"
24 #include "AIDA/IProfile1D.h"
25 #undef class
26 // ============================================================================
27 // GaudiKernel
28 // ============================================================================
29 #include "GaudiKernel/DataObject.h"
30 #include "GaudiKernel/IRegistry.h"
31 // ============================================================================
32 // Local
33 // ============================================================================
34 #include "GaudiUtils/HistoStats.h"
36 // ============================================================================
37 // Boost
38 // ============================================================================
39 #include "boost/algorithm/string/erase.hpp"
40 #include "boost/format.hpp"
41 // ============================================================================
47 // ============================================================================
48 namespace {
49  // ==========================================================================
50  // Examples of formats
51  // ==========================================================================
53  const std::string s_histoFormatStat = "| %2$-45.45s | %3$=7d |%8$11.5g | %10$-11.5g|%12$11.5g |%14$11.5g |";
54  // ==========================================================================
56  const std::string s_histoFormatStatOnly = "| %3$=7d |%8$11.5g | %10$-11.5g|%12$11.5g |%14$11.5g |";
57  // ==========================================================================
59  const std::string s_histoFormatStat1 =
60  "| %3$=7d |%8$9.3g+-%9$-9.3g|%10$9.3g+-%11$-9.3g|%12$9.3g+-%13$-9.3g|%14$9.3g+-%15$-9.3g|";
61  // ==========================================================================
65  const std::string s_histoFormatShapeOnly = "|%8$11.5g |%10$11.5g |%12$11.5g |%14$11.5g |%19$11.5g/%21$-11.5g|";
66  // ==========================================================================
71  const std::string s_histoFormatShape = "|%8$9.3g+-%9$-9.3g|%10$9.3g+-%11$-9.3g|%12$9.3g+-%13$-9.3g|%14$9.3g+-%15$-9."
72  "3g|%19$9.3g+-%20$-9.3g|%21$9.3g+-%22$-9.3g|";
73  // ==========================================================================
75  const std::string s_histoFormatLineTitle =
76  " %2$-45.45s mean/sigma/skew/kurtosis = %8$10.4g/%10$10.4g/%12$10.4g/%14$10.4g";
77  // ==========================================================================
79  const std::string s_histoFormatLineOnly = " mean/sigma/skew/kurtosis = %8$10.4g/%10$10.4g/%12$10.4g/%14$10.4g";
80  // ==========================================================================
82  const std::string s_histoFormatOld = " %2$-45.45s Ents/All=%7$5s/%3$5s<X>/sX=%8$.5g/%10$-.5g";
83  // ==========================================================================
85  const std::string s_histoFormatPathTitle = "| %1$-45.45s | %2$-45.45s | ";
86  // ==========================================================================
88  const std::string s_histoFormatFull = "| %1$-45.45s | %2$-45.45s | %3$=7d |%4$5d/%5$-5d|%6$9.3g | "
89  "%7$-9.3g|%8$9.3g+-%9$-9.3g|%10$9.3g+-%11$-9.3g|%12$9.3g+-%13$-9.3g|%14$9.3g+-%"
90  "15$-9.3g|";
92  const std::string s_histoFormatFullStat = "| %3$=7d |%4$5d/%5$-5d|%6$9.3g | "
93  "%7$-9.3g|%8$9.3g+-%9$-9.3g|%10$9.3g+-%11$-9.3g|%12$9.3g+-%13$-9.3g|%14$9."
94  "3g+-%15$-9.3g|";
95  // ==========================================================================
96  // headers:
97  // ==========================================================================
99  const std::string s_histoFormatStatHeader =
100  "| Title | # | Mean | RMS | Skewness | Kurtosis |";
101  // ==========================================================================
103  const std::string s_histoFormatStatOnlyHeader = "| # | Mean | RMS | Skewness | Kurtosis |";
104  // ==========================================================================
106  const std::string s_histoFormatStat1Header =
107  "| # | Mean+-Error | RMS+-Error | Skewness+-Error | Kurtosis+-Error |";
108  // ==========================================================================
110  const std::string s_histoFormatShapeOnlyHeader =
111  "| Mean | RMS | Skewness | Kurtosis | Underflow%/Overflow% |";
112  // ==========================================================================
114  const std::string s_histoFormatShapeHeader = "| Mean | RMS | Skewness | "
115  "Kurtosis | Underflow [%] | Overflow [%] |";
116  // ==========================================================================
118  const std::string s_histoFormatLineTitleHeader = " Title ";
119  // ==========================================================================
121  const std::string s_histoFormatLineOnlyHeader = "";
122  // ==========================================================================
124  const std::string s_histoFormatOldHeader = "";
125  // ==========================================================================
127  const std::string s_histoFormatPathTitleHeader =
128  "| Path in Histogram Data Store | Title | ";
129  // ==========================================================================
131  const std::string s_histoFormatFullHeader = "| Path in Histogram Data Store | Title | "
132  "# |Udflw/Ovflw| nEff | Sum | Mean+-Error | "
133  "RMS+-Error | Skewness+-Error | Kurtosis+-Error |";
134  // ==========================================================================
136  const std::string s_histoFormatFullStatHeader = "| # |Udflw/Ovflw| nEff | Sum | Mean+-Error | "
137  " RMS+-Error | Skewness+-Error | Kurtosis+-Error |";
138  // ==========================================================================
139  // Get cleaned up histogram title for table printout
140  template <typename HISTO>
141  decltype( auto ) _title( const HISTO* h ) {
142  // strip '|' from title as this is used as printout table delimiter and
143  // instances in the title can cause problems with automatic parsing of the
144  // table (e.g. LHCb QM tests) later on.
145  auto htitle = h->title();
146  boost::erase_all( htitle, "|" );
147  return htitle;
148  }
149  // ==========================================================================
150 } // namespace
151 // ============================================================================
152 // get the format by enum
153 // ============================================================================
155  switch ( ID ) {
156  case Old:
157  return s_histoFormatOld;
158  case Full:
159  return s_histoFormatFull;
160  case FullStat:
161  return s_histoFormatFullStat;
162  case Stat:
163  return s_histoFormatStat;
164  case StatOnly:
165  return s_histoFormatStatOnly;
166  case Stat1:
167  return s_histoFormatStat1;
168  case ShapeOnly:
169  return s_histoFormatShapeOnly;
170  case Shape:
171  return s_histoFormatShape;
172  case LineTitle:
173  return s_histoFormatLineTitle;
174  case LineOnly:
175  return s_histoFormatLineOnly;
176  case PathTitle:
177  return s_histoFormatPathTitle;
178  default:
179  break;
180  }
182  return s_histoFormatStat;
183 }
184 // ============================================================================
185 // get the recommended header by enum
186 // ============================================================================
188  switch ( ID ) {
189  case Old:
190  return s_histoFormatOldHeader;
191  case Full:
192  return s_histoFormatFullHeader;
193  case FullStat:
194  return s_histoFormatFullStatHeader;
195  case Stat:
196  return s_histoFormatStatHeader;
197  case Stat1:
198  return s_histoFormatStat1Header;
199  case StatOnly:
200  return s_histoFormatStatOnlyHeader;
201  case ShapeOnly:
202  return s_histoFormatShapeOnlyHeader;
203  case Shape:
204  return s_histoFormatShapeHeader;
205  case LineTitle:
206  return s_histoFormatLineTitleHeader;
207  case LineOnly:
208  return s_histoFormatLineOnlyHeader;
209  case PathTitle:
210  return s_histoFormatPathTitleHeader;
211  default:;
212  }
214  return s_histoFormatStatHeader;
215 }
216 // ============================================================================
217 // get the path in TES for AIDA histogram
218 // ============================================================================
219 std::string Gaudi::Utils::Histos::path( const AIDA::IBaseHistogram* aida ) {
220  if ( !aida ) { return ""; } // RETURN
221  const auto object = dynamic_cast<const DataObject*>( aida );
222  if ( !object ) { return ""; } // RETURN
223  const auto registry = object->registry();
224  if ( !registry ) { return ""; } // RETURN
225  const auto _path = registry->identifier();
226  const auto n = _path.find( "/stat/" );
227  return ( 0 == n ? std::string( _path, 6 ) : _path ); // RETURN
228 }
229 // ============================================================================
230 /* Make the string representation of the histogram
231  * according to the specified format.
232  */
233 // ============================================================================
234 std::string Gaudi::Utils::Histos::format( const AIDA::IHistogram1D* histo, const std::string& fmt ) {
235  if ( !histo ) { return "<NULL>"; }
236  using namespace Gaudi::Utils;
237  using namespace boost::io;
238  boost::format _fmt( fmt );
239  // allow various number of arguments
240  _fmt.exceptions( all_error_bits ^ ( too_many_args_bit | too_few_args_bit ) );
241 
242  _fmt % ( "\"" + path( histo ) + "\"" ) // 1) histogram path
243  % ( "\"" + _title( histo ) + "\"" ) // 2) title
244  % histo->allEntries() // 3) # entries
245  % histo->binEntries( AIDA::IAxis::UNDERFLOW_BIN ) // 4) # underflow
246  % histo->binEntries( AIDA::IAxis::OVERFLOW_BIN ) // 5) # overflow
247  % HistoStats::nEff( histo ) // 6) equivalent entries
248  % histo->sumBinHeights() // 7) integral
249  % HistoStats::mean( histo ) // 8) mean value
250  % HistoStats::meanErr( histo ) // 9) error in mean
251  % HistoStats::rms( histo ) // 10) rms
252  % HistoStats::rmsErr( histo ) // 11) error in rms
253  % HistoStats::skewness( histo ) // 12) skewness
254  % HistoStats::skewnessErr( histo ) // 13) error in skewness
255  % HistoStats::kurtosis( histo ) // 14) kurtosis
256  % HistoStats::kurtosisErr( histo ) // 15) error in kurtosis
257  //
258  % histo->sumAllBinHeights() // 16) full integral (in and out range)
259  % HistoStats::sumAllBinHeightErr( histo ) // 17) error on 16
260  % HistoStats::sumBinHeightErr( histo ) // 18) error on 7
261  //
262  % ( 100 * HistoStats::underflowEntriesFrac( histo ) ) // 19) fraction of underflow entries
263  % ( 100 * HistoStats::underflowEntriesFracErr( histo ) ) // 20) error on 19
264  % ( 100 * HistoStats::overflowEntriesFrac( histo ) ) // 21) fraction of overflow entries
265  % ( 100 * HistoStats::overflowEntriesFracErr( histo ) ) // 22) error on 21
266  //
267  % HistoStats::underflowIntegralFrac( histo ) // 23) fraction of underflow integral
268  % HistoStats::underflowIntegralFracErr( histo ) // 24) error on 23
269  % HistoStats::overflowIntegralFrac( histo ) // 25) fraction of overflow intergal
270  % HistoStats::overflowIntegralFracErr( histo ) // 26) error on 25
271  ;
272 
273  return _fmt.str();
274 }
275 // ============================================================================
276 /* Make the string representation of the histogram
277  * according to the specified format.
278  */
279 // ============================================================================
280 std::string Gaudi::Utils::Histos::format( const AIDA::IProfile1D* histo, const std::string& fmt ) {
281  if ( !histo ) { return "<NULL>"; }
282  using namespace Gaudi::Utils;
283  using namespace boost::io;
284  boost::format _fmt( fmt );
285  // allow various number of arguments
286  _fmt.exceptions( all_error_bits ^ ( too_many_args_bit | too_few_args_bit ) );
287 
288  _fmt % ( "\"" + path( histo ) + "\"" ) // 1) histogram path
289  % ( "\"" + _title( histo ) + "\"" ) // 2) title
290  % histo->allEntries() // 3) # entries
291  % histo->binEntries( AIDA::IAxis::UNDERFLOW_BIN ) // 4) # underflow
292  % histo->binEntries( AIDA::IAxis::OVERFLOW_BIN ) // 5) # overflow
293  % HistoStats::nEff( histo ) // 6) equivalent entries
294  % histo->sumBinHeights() // 7) integral
295  % HistoStats::mean( histo ) // 8) mean value
296  % HistoStats::meanErr( histo ) // 9) error in mean
297  % HistoStats::rms( histo ) // 10) rms
298  % HistoStats::rmsErr( histo ) // 11) error in rms
299  % HistoStats::skewness( histo ) // 12) skewness
300  % HistoStats::skewnessErr( histo ) // 13) error in skewness
301  % HistoStats::kurtosis( histo ) // 14) kurtosis
302  % HistoStats::kurtosisErr( histo ) // 15) error in kurtosis
303  //
304  % histo->sumAllBinHeights() // 16) full integral (in and out range)
305  % HistoStats::sumAllBinHeightErr( histo ) // 17) error on 16
306  % HistoStats::sumBinHeightErr( histo ) // 18) error on 7
307  //
308  % ( 100 * HistoStats::underflowEntriesFrac( histo ) ) // 19) fraction of underflow entries
309  % ( 100 * HistoStats::underflowEntriesFracErr( histo ) ) // 20) error on 19
310  % ( 100 * HistoStats::overflowEntriesFrac( histo ) ) // 21) fraction of overflow entries
311  % ( 100 * HistoStats::overflowEntriesFracErr( histo ) ) // 22) error on 21
312  //
313  % HistoStats::underflowIntegralFrac( histo ) // 23) fraction of underflow integral
314  % HistoStats::underflowIntegralFracErr( histo ) // 24) error on 23
315  % HistoStats::overflowIntegralFrac( histo ) // 25) fraction of overflow intergal
316  % HistoStats::overflowIntegralFracErr( histo ) // 26) error on 25
317  ;
318 
319  return _fmt.str();
320 }
321 // ============================================================================
322 /* format a full row in table, including ID, label, path or any other
323  * "extra" identifier in string form
324  */
325 // ============================================================================
326 std::string Gaudi::Utils::Histos::format( const AIDA::IHistogram1D* histo, const std::string& ID,
327  const std::string& fmt1, const std::string& fmt2 ) {
328  using namespace boost::io;
329  boost::format _fmt( fmt1 );
330  // allow various number of arguments
331  _fmt.exceptions( all_error_bits ^ ( too_many_args_bit | too_few_args_bit ) );
332  //
333  _fmt % ID // format ID
334  % format( histo, fmt2 ); // format the histogram
335  //
336  return _fmt.str();
337 }
338 // ============================================================================
339 /* format a full row in table, including ID, label, path or any other
340  * "extra" identifier in string form
341  */
342 // ============================================================================
343 std::string Gaudi::Utils::Histos::format( const AIDA::IProfile1D* histo, const std::string& ID, const std::string& fmt1,
344  const std::string& fmt2 ) {
345  using namespace boost::io;
346  boost::format _fmt( fmt1 );
347  // allow various number of arguments
348  _fmt.exceptions( all_error_bits ^ ( too_many_args_bit | too_few_args_bit ) );
349  //
350  _fmt % ID // format ID
351  % format( histo, fmt2 ); // format the histogram
352  //
353  return _fmt.str();
354 }
355 // ============================================================================
356 // helper method to merge the headers for short format table
357 // ============================================================================
359  using namespace boost::io;
360  boost::format _fmt( fmt );
361  // allow various number of arguments
362  _fmt.exceptions( all_error_bits ^ ( too_many_args_bit | too_few_args_bit ) );
363  //
364  _fmt % val1 // format ID
365  % val2; // format the histogram
366  //
367  return _fmt.str();
368 }
369 // ============================================================================
370 // default constructor
371 // ============================================================================
373  : m_header( Gaudi::Utils::Histos::Formats::header( ID ) )
374  , m_footer()
375  , m_format( Gaudi::Utils::Histos::Formats::format( ID ) ) {}
376 // ============================================================================
378  : m_header( header ), m_footer( footer ), m_format( format ) {}
379 // ============================================================================
380 // format the table row using the default format
381 // ============================================================================
382 std::string Gaudi::Utils::Histos::Table::toString( const AIDA::IHistogram1D* histo ) const {
383  return Gaudi::Utils::Histos::format( histo, format() );
384 }
385 // ============================================================================
389 // ============================================================================
390 std::string Gaudi::Utils::Histos::Table::toString( const AIDA::IHistogram1D* histo, const std::string& ID,
391  const std::string& fmt ) const {
392  return Gaudi::Utils::Histos::format( histo, ID, fmt, format() );
393 }
394 // ============================================================================
395 
396 // ============================================================================
397 // format the table row using the default format
398 // ============================================================================
399 std::string Gaudi::Utils::Histos::Table::toString( const AIDA::IProfile1D* histo ) const {
400  return Gaudi::Utils::Histos::format( histo, format() );
401 }
402 // ============================================================================
406 // ============================================================================
407 std::string Gaudi::Utils::Histos::Table::toString( const AIDA::IProfile1D* histo, const std::string& ID,
408  const std::string& fmt ) const {
409  return Gaudi::Utils::Histos::format( histo, ID, fmt, format() );
410 }
411 // ============================================================================
412 
413 // ============================================================================
414 // The END
415 // ============================================================================
helper namespace to collect useful definitions, types, constants and functions, related to manipulati...
static double underflowIntegralFracErr(const AIDA::IHistogram1D *histo)
the error on fraction of underflow integral
Definition: HistoStats.cpp:765
static double overflowIntegralFracErr(const AIDA::IHistogram1D *histo)
the error on fraction of overflow intergal
Definition: HistoStats.cpp:753
GAUDI_API std::string htitle(const AIDA::IBaseHistogram *histo, const std::string &title="")
get the title
Definition: Fill.cpp:109
GAUDI_API std::string format(const char *,...)
MsgStream format utility "a la sprintf(...)".
Definition: MsgStream.cpp:109
static double underflowEntriesFrac(const AIDA::IHistogram1D *histo)
the fraction of underflow entries (useful for shape comparison)
Definition: HistoStats.cpp:693
std::string m_header
the table header
static double rmsErr(const AIDA::IHistogram1D *histo)
get an error in the rms value
Definition: HistoStats.cpp:651
Table(const int ID=0)
constructor from enum
Small wrapper class for easy manipulation with generic counters and IStatSvc interface.
Definition: Stat.h:46
static double underflowIntegralFrac(const AIDA::IHistogram1D *histo)
the fraction of underflow integral (useful for shape comparison)
Definition: HistoStats.cpp:717
GAUDI_API std::string format(const AIDA::IHistogram1D *histo, const std::string &fmt)
Make the string representation of the histogram according to the specified format.
static double meanErr(const AIDA::IHistogram1D *histo)
get an error in the mean value
Definition: HistoStats.cpp:635
static double overflowIntegralFrac(const AIDA::IHistogram1D *histo)
the fraction of overflow intergal (useful for shape comparison)
Definition: HistoStats.cpp:705
GAUDI_API std::string header(const int ID=Default)
get the recommended header by enum
const std::string & header() const
the table header
STL class.
static double rms(const AIDA::IHistogram1D *histo)
get the rms value for the histogram (just for completeness)
Definition: HistoStats.cpp:643
GAUDI_API std::string format(const int ID=Default)
get the format by enum
const std::string & format() const
the format
static double overflowEntriesFrac(const AIDA::IHistogram1D *histo)
the fraction of overflow entries (useful for shape comparison)
Definition: HistoStats.cpp:681
std::string m_footer
the table footer
static double sumBinHeightErr(const AIDA::IHistogram1D *histo)
get an error in the sum bin height ("in-range integral")
Definition: HistoStats.cpp:659
static double overflowEntriesFracErr(const AIDA::IHistogram1D *histo)
error on fraction of overflow entries (useful for shape comparison)
Definition: HistoStats.cpp:729
GAUDI_API std::string path(const AIDA::IBaseHistogram *aida)
get the path in THS for AIDA histogram
static double sumAllBinHeightErr(const AIDA::IHistogram1D *histo)
get an error in the sum of all bin height ("integral")
Definition: HistoStats.cpp:669
static double kurtosisErr(const AIDA::IHistogram1D *histo)
get the error in kurtosis for the histogram
Definition: HistoStats.cpp:611
static double mean(const AIDA::IHistogram1D *histo)
get the mean value for the histogram (just for completeness)
Definition: HistoStats.cpp:627
static double skewnessErr(const AIDA::IHistogram1D *histo)
get the error in skewness for the histogram
Definition: HistoStats.cpp:595
static double underflowEntriesFracErr(const AIDA::IHistogram1D *histo)
the error on fraction of underflow entries (useful for shape comparison)
Definition: HistoStats.cpp:741
static double kurtosis(const AIDA::IHistogram1D *histo)
get the kurtosis for the histogram
Definition: HistoStats.cpp:603
A DataObject is the base class of any identifiable object on any data store.
Definition: DataObject.h:30
static double nEff(const AIDA::IHistogram1D *histo)
get the effective entries (just for completeness)
Definition: HistoStats.cpp:619
Helper functions to set/get the application return code.
Definition: __init__.py:1
std::string m_format
the defautl format
static double skewness(const AIDA::IHistogram1D *histo)
get the skewness for the histogram
Definition: HistoStats.cpp:587
std::string toString(const AIDA::IHistogram1D *histo) const
make the string representation according to the default format
const std::string & footer() const
the table footer