Loading [MathJax]/extensions/tex2jax.js
The Gaudi Framework  v31r0 (aeb156f0)
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Modules Pages
HistoLabels.cpp
Go to the documentation of this file.
1 // Include files
2 
3 //-----------------------------------------------------------------------------
4 // Implementation file for class : HistoLabels
5 //-----------------------------------------------------------------------------
6 #ifdef WIN32
7 // Disable warning
8 // warning C4996: 'sprintf': This function or variable may be unsafe.
9 // coming from TString.h
10 # pragma warning( disable : 4996 )
11 #endif
12 
13 // local
14 #include "GaudiUtils/HistoLabels.h"
15 #include "GaudiUtils/Aida2ROOT.h"
16 
17 // ============================================================================
18 // ROOT
19 // ============================================================================
20 #include "TH1D.h"
21 #include "TH2D.h"
22 #include "TProfile.h"
23 #include "TProfile2D.h"
24 
25 // Private namespace
26 namespace {
27  //--------------------------------------------------------------------
28 
29  template <typename R, typename A>
30  bool setAxisLabels_( A* aida, const std::string& xAxis, const std::string& yAxis ) {
31  if ( !aida ) return false;
33  if ( !root ) return false;
34  root->SetXTitle( xAxis.c_str() );
35  root->SetYTitle( yAxis.c_str() );
36  return true;
37  }
38 
39  //--------------------------------------------------------------------
40 
41  bool setBinLabels_( TAxis* axis, const Gaudi::Utils::Histos::BinLabels& labels ) {
42  if ( !axis ) return false;
43  const unsigned nbins = axis->GetNbins();
44  for ( const auto& i : labels ) {
45  if ( 1 + i.first <= 0 || 1 + i.first > nbins ) return false;
46  // Argh... ROOT bins start counting at '1' instead of '0'
47  axis->SetBinLabel( 1 + i.first, i.second.c_str() );
48  }
49  return true;
50  }
51 
52  //--------------------------------------------------------------------
53 
54  template <typename R, typename A>
55  bool setBinLabels_( A* aida, const Gaudi::Utils::Histos::BinLabels& labels ) {
56  if ( !aida ) return false;
57  R* root = Gaudi::Utils::Aida2ROOT::aida2root( aida );
58  if ( !root ) return false;
59  return setBinLabels_( root->GetXaxis(), labels );
60  }
61 
62  template <typename Histogram>
63  bool setBinLabels_( Histogram* hist, const Gaudi::Utils::Histos::Labels& labels ) {
65  l.reserve( labels.size() );
66  for ( unsigned i = 0; i < labels.size(); ++i ) l.emplace_back( i, labels[i] );
67  return Gaudi::Utils::Histos::setBinLabels( hist, l );
68  }
69 
70  //--------------------------------------------------------------------
71 } // namespace
72 
73 namespace Gaudi {
74  namespace Utils {
75  namespace Histos {
76 
77  // --------------------------------------------------------------------------
78 
79  bool setBinLabels( AIDA::IHistogram1D* hist, const BinLabels& labels ) {
80  return setBinLabels_<TH1D>( hist, labels );
81  }
82 
83  bool setBinLabels( AIDA::IProfile1D* hist, const BinLabels& labels ) {
84  return setBinLabels_<TProfile>( hist, labels );
85  }
86 
87  bool setBinLabels( AIDA::IHistogram1D* hist, const Labels& labels ) { return setBinLabels_( hist, labels ); }
88 
89  bool setBinLabels( AIDA::IProfile1D* hist, const Labels& labels ) { return setBinLabels_( hist, labels ); }
90 
91  bool setBinLabels( AIDA::IHistogram2D* hist, const Labels& xlabels, const Labels& ylabels ) {
92  if ( !hist ) return false;
93  TH2D* h2d = Gaudi::Utils::Aida2ROOT::aida2root( hist );
94  if ( !h2d ) return false;
95  BinLabels lx;
96  lx.reserve( xlabels.size() );
97  for ( unsigned int i = 0; i < xlabels.size(); ++i ) { lx.emplace_back( i, xlabels[i] ); }
98  BinLabels ly;
99  ly.reserve( ylabels.size() );
100  for ( unsigned int i = 0; i < ylabels.size(); ++i ) { ly.emplace_back( i, ylabels[i] ); }
101  return ( setBinLabels_( h2d->GetXaxis(), lx ) && setBinLabels_( h2d->GetYaxis(), ly ) );
102  }
103 
104  bool setBinLabels( AIDA::IHistogram2D* hist, const BinLabels& xlabels, const BinLabels& ylabels ) {
105  TH2D* h2d = Gaudi::Utils::Aida2ROOT::aida2root( hist );
106  return ( h2d && setBinLabels_( h2d->GetXaxis(), xlabels ) && setBinLabels_( h2d->GetYaxis(), ylabels ) );
107  }
108 
109  bool setBinLabels( AIDA::IProfile2D* hist, const Labels& xlabels, const Labels& ylabels ) {
110  if ( !hist ) return false;
111  TProfile2D* h2d = Gaudi::Utils::Aida2ROOT::aida2root( hist );
112  if ( !h2d ) return false;
113  BinLabels lx;
114  lx.reserve( xlabels.size() );
115  for ( unsigned int i = 0; i < xlabels.size(); ++i ) { lx.emplace_back( i, xlabels[i] ); }
116  BinLabels ly;
117  ly.reserve( ylabels.size() );
118  for ( unsigned int i = 0; i < ylabels.size(); ++i ) { ly.emplace_back( i, ylabels[i] ); }
119  return ( setBinLabels_( h2d->GetXaxis(), lx ) && setBinLabels_( h2d->GetYaxis(), ly ) );
120  }
121 
122  bool setBinLabels( AIDA::IProfile2D* hist, const BinLabels& xlabels, const BinLabels& ylabels ) {
123  TProfile2D* h2d = Gaudi::Utils::Aida2ROOT::aida2root( hist );
124  return ( h2d && setBinLabels_( h2d->GetXaxis(), xlabels ) && setBinLabels_( h2d->GetYaxis(), ylabels ) );
125  }
126 
127  // --------------------------------------------------------------------------
128 
129  bool setAxisLabels( AIDA::IHistogram1D* hist, const std::string& xAxis, const std::string& yAxis ) {
130  return setAxisLabels_<TH1D>( hist, xAxis, yAxis );
131  }
132 
133  bool setAxisLabels( AIDA::IProfile1D* hist, const std::string& xAxis, const std::string& yAxis ) {
134  return setAxisLabels_<TProfile>( hist, xAxis, yAxis );
135  }
136 
137  bool setAxisLabels( AIDA::IHistogram2D* hist, const std::string& xAxis, const std::string& yAxis ) {
138  return setAxisLabels_<TH2D>( hist, xAxis, yAxis );
139  }
140 
141  bool setAxisLabels( AIDA::IProfile2D* hist, const std::string& xAxis, const std::string& yAxis ) {
142  return setAxisLabels_<TProfile2D>( hist, xAxis, yAxis );
143  }
144 
145  // --------------------------------------------------------------------------
146  } // namespace Histos
147  } // namespace Utils
148 } // namespace Gaudi
helper namespace to collect useful definitions, types, constants and functions, related to manipulati...
std::vector< BinLabel > BinLabels
Typedef for a list of bin numbers and their associated label.
Definition: HistoLabels.h:27
GAUDI_API bool setAxisLabels(AIDA::IHistogram1D *hist, const std::string &xAxis, const std::string &yAxis)
Set the axis labels for the given 1D histogram.
STL class.
static TH1D * aida2root(AIDA::IHistogram1D *aida)
get the underlying pointer for 1D-histogram
Definition: Aida2ROOT.cpp:71
std::vector< std::string > Labels
Typedef for a list of labels.
Definition: HistoLabels.h:23
dictionary l
Definition: gaudirun.py:517
T size(T...args)
STL class.
GAUDI_API bool setBinLabels(AIDA::IHistogram1D *hist, const Labels &labels)
Set the Bin labels for a given 1D histogram.
Definition: HistoLabels.cpp:87
T c_str(T...args)
Helper functions to set/get the application return code.
Definition: __init__.py:1
T reserve(T...args)
T emplace_back(T...args)