The Gaudi Framework  v30r3 (a5ef0a68)
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 
30  template <typename R, typename A>
31  bool setAxisLabels_( A* aida, const std::string& xAxis, const std::string& yAxis )
32  {
33  if ( !aida ) return false;
35  if ( !root ) return false;
36  root->SetXTitle( xAxis.c_str() );
37  root->SetYTitle( yAxis.c_str() );
38  return true;
39  }
40 
41  //--------------------------------------------------------------------
42 
43  bool setBinLabels_( TAxis* axis, const Gaudi::Utils::Histos::BinLabels& labels )
44  {
45  if ( !axis ) return false;
46  const unsigned nbins = axis->GetNbins();
47  for ( const auto& i : labels ) {
48  if ( 1 + i.first <= 0 || 1 + i.first > nbins ) return false;
49  // Argh... ROOT bins start counting at '1' instead of '0'
50  axis->SetBinLabel( 1 + i.first, i.second.c_str() );
51  }
52  return true;
53  }
54 
55  //--------------------------------------------------------------------
56 
57  template <typename R, typename A>
58  bool setBinLabels_( A* aida, const Gaudi::Utils::Histos::BinLabels& labels )
59  {
60  if ( !aida ) return false;
61  R* root = Gaudi::Utils::Aida2ROOT::aida2root( aida );
62  if ( !root ) return false;
63  return setBinLabels_( root->GetXaxis(), labels );
64  }
65 
66  template <typename Histogram>
67  bool setBinLabels_( Histogram* hist, const Gaudi::Utils::Histos::Labels& labels )
68  {
70  l.reserve( labels.size() );
71  for ( unsigned i = 0; i < labels.size(); ++i ) l.emplace_back( i, labels[i] );
72  return Gaudi::Utils::Histos::setBinLabels( hist, l );
73  }
74 
75  //--------------------------------------------------------------------
76 }
77 
78 namespace Gaudi
79 {
80  namespace Utils
81  {
82  namespace Histos
83  {
84 
85  // --------------------------------------------------------------------------
86 
87  bool setBinLabels( AIDA::IHistogram1D* hist, const BinLabels& labels )
88  {
89  return setBinLabels_<TH1D>( hist, labels );
90  }
91 
92  bool setBinLabels( AIDA::IProfile1D* hist, const BinLabels& labels )
93  {
94  return setBinLabels_<TProfile>( hist, labels );
95  }
96 
97  bool setBinLabels( AIDA::IHistogram1D* hist, const Labels& labels ) { return setBinLabels_( hist, labels ); }
98 
99  bool setBinLabels( AIDA::IProfile1D* hist, const Labels& labels ) { return setBinLabels_( hist, labels ); }
100 
101  bool setBinLabels( AIDA::IHistogram2D* hist, const Labels& xlabels, const Labels& ylabels )
102  {
103  if ( !hist ) return false;
104  TH2D* h2d = Gaudi::Utils::Aida2ROOT::aida2root( hist );
105  if ( !h2d ) return false;
106  BinLabels lx;
107  lx.reserve( xlabels.size() );
108  for ( unsigned int i = 0; i < xlabels.size(); ++i ) {
109  lx.emplace_back( i, xlabels[i] );
110  }
111  BinLabels ly;
112  ly.reserve( ylabels.size() );
113  for ( unsigned int i = 0; i < ylabels.size(); ++i ) {
114  ly.emplace_back( i, ylabels[i] );
115  }
116  return ( setBinLabels_( h2d->GetXaxis(), lx ) && setBinLabels_( h2d->GetYaxis(), ly ) );
117  }
118 
119  bool setBinLabels( AIDA::IHistogram2D* hist, const BinLabels& xlabels, const BinLabels& ylabels )
120  {
121  TH2D* h2d = Gaudi::Utils::Aida2ROOT::aida2root( hist );
122  return ( h2d && setBinLabels_( h2d->GetXaxis(), xlabels ) && setBinLabels_( h2d->GetYaxis(), ylabels ) );
123  }
124 
125  bool setBinLabels( AIDA::IProfile2D* hist, const Labels& xlabels, const Labels& ylabels )
126  {
127  if ( !hist ) return false;
128  TProfile2D* h2d = Gaudi::Utils::Aida2ROOT::aida2root( hist );
129  if ( !h2d ) return false;
130  BinLabels lx;
131  lx.reserve( xlabels.size() );
132  for ( unsigned int i = 0; i < xlabels.size(); ++i ) {
133  lx.emplace_back( i, xlabels[i] );
134  }
135  BinLabels ly;
136  ly.reserve( ylabels.size() );
137  for ( unsigned int i = 0; i < ylabels.size(); ++i ) {
138  ly.emplace_back( i, ylabels[i] );
139  }
140  return ( setBinLabels_( h2d->GetXaxis(), lx ) && setBinLabels_( h2d->GetYaxis(), ly ) );
141  }
142 
143  bool setBinLabels( AIDA::IProfile2D* hist, const BinLabels& xlabels, const BinLabels& ylabels )
144  {
145  TProfile2D* h2d = Gaudi::Utils::Aida2ROOT::aida2root( hist );
146  return ( h2d && setBinLabels_( h2d->GetXaxis(), xlabels ) && setBinLabels_( h2d->GetYaxis(), ylabels ) );
147  }
148 
149  // --------------------------------------------------------------------------
150 
151  bool setAxisLabels( AIDA::IHistogram1D* hist, const std::string& xAxis, const std::string& yAxis )
152  {
153  return setAxisLabels_<TH1D>( hist, xAxis, yAxis );
154  }
155 
156  bool setAxisLabels( AIDA::IProfile1D* hist, const std::string& xAxis, const std::string& yAxis )
157  {
158  return setAxisLabels_<TProfile>( hist, xAxis, yAxis );
159  }
160 
161  bool setAxisLabels( AIDA::IHistogram2D* hist, const std::string& xAxis, const std::string& yAxis )
162  {
163  return setAxisLabels_<TH2D>( hist, xAxis, yAxis );
164  }
165 
166  bool setAxisLabels( AIDA::IProfile2D* hist, const std::string& xAxis, const std::string& yAxis )
167  {
168  return setAxisLabels_<TProfile2D>( hist, xAxis, yAxis );
169  }
170 
171  // --------------------------------------------------------------------------
172  }
173  }
174 }
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:31
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:73
std::vector< std::string > Labels
Typedef for a list of labels.
Definition: HistoLabels.h:27
dictionary l
Definition: gaudirun.py:440
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:97
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)