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