All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
HistoLabels.cpp
Go to the documentation of this file.
1 // $Id: $
2 // Include files
3 
4 //-----------------------------------------------------------------------------
5 // Implementation file for class : HistoLabels
6 //-----------------------------------------------------------------------------
7 #ifdef WIN32
8 // Disable warning
9 // warning C4996: 'sprintf': This function or variable may be unsafe.
10 // coming from TString.h
11 #pragma warning(disable:4996)
12 #endif
13 
14 // local
15 #include "GaudiUtils/HistoLabels.h"
16 #include "GaudiUtils/Aida2ROOT.h"
17 
18 // ============================================================================
19 // ROOT
20 // ============================================================================
21 #include "TH1D.h"
22 #include "TH2D.h"
23 #include "TProfile.h"
24 #include "TProfile2D.h"
25 
26 // Private namespace
27 namespace
28 {
29  //--------------------------------------------------------------------
30 
31  template <typename R, typename A>
32  bool setAxisLabels_( A* aida,
33  const std::string& xAxis,
34  const std::string& yAxis )
35  {
36  if (!aida) return false;
38  if (!root) return false;
39  root->SetXTitle(xAxis.c_str());
40  root->SetYTitle(yAxis.c_str());
41  return true;
42  }
43 
44  //--------------------------------------------------------------------
45 
46  bool setBinLabels_( TAxis* axis,
47  const Gaudi::Utils::Histos::BinLabels & labels )
48  {
49  if (!axis) return false;
50  const unsigned nbins = axis->GetNbins();
51  for ( Gaudi::Utils::Histos::BinLabels::const_iterator i = labels.begin();
52  i != labels.end(); ++i )
53  {
54  if ( 1+i->first <= 0 || 1+i->first > nbins ) return false;
55  // Argh... ROOT bins start counting at '1' instead of '0'
56  axis -> SetBinLabel( 1 + i->first, i->second.c_str() );
57  }
58  return true;
59  }
60 
61  //--------------------------------------------------------------------
62 
63  template <typename R, typename A>
64  bool setBinLabels_( A* aida,
65  const Gaudi::Utils::Histos::BinLabels& labels )
66  {
67  if (!aida) return false;
68  R * root = Gaudi::Utils::Aida2ROOT::aida2root( aida );
69  if (!root) return false;
70  return setBinLabels_( root->GetXaxis(), labels );
71  }
72 
73  template <typename Histogram>
74  bool setBinLabels_( Histogram* hist,
75  const Gaudi::Utils::Histos::Labels& labels )
76  {
78  l.reserve(labels.size());
79  for ( unsigned i = 0; i<labels.size(); ++i )
80  {
81  l.push_back( Gaudi::Utils::Histos::BinLabel(i,labels[i]) );
82  }
84  }
85 
86  //--------------------------------------------------------------------
87 
88 }
89 
90 namespace Gaudi
91 {
92  namespace Utils
93  {
94  namespace Histos
95  {
96 
97  // --------------------------------------------------------------------------
98 
99  bool setBinLabels( AIDA::IHistogram1D* hist,
100  const BinLabels& labels )
101  {
102  return setBinLabels_<TH1D>(hist,labels);
103  }
104 
105  bool setBinLabels( AIDA::IProfile1D* hist,
106  const BinLabels& labels )
107  {
108  return setBinLabels_<TProfile>(hist,labels);
109  }
110 
111  bool setBinLabels( AIDA::IHistogram1D* hist,
112  const Labels& labels )
113  {
114  return setBinLabels_(hist,labels);
115  }
116 
117  bool setBinLabels( AIDA::IProfile1D* hist,
118  const Labels& labels )
119  {
120  return setBinLabels_(hist,labels);
121  }
122 
123  bool setBinLabels( AIDA::IHistogram2D* hist,
124  const Labels& xlabels,
125  const Labels& ylabels )
126  {
127  if (!hist) return false;
128  TH2D * 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  {
134  lx.push_back( Gaudi::Utils::Histos::BinLabel( i , xlabels[i] ) );
135  }
136  BinLabels ly;
137  ly.reserve(ylabels.size());
138  for ( unsigned int i = 0; i < ylabels.size(); ++i )
139  {
140  ly.push_back(Gaudi::Utils::Histos::BinLabel( i , ylabels[i] ) );
141  }
142  return ( setBinLabels_( h2d->GetXaxis(), lx ) &&
143  setBinLabels_( h2d->GetYaxis(), ly ) );
144  }
145 
146  bool setBinLabels( AIDA::IHistogram2D* hist,
147  const BinLabels& xlabels,
148  const BinLabels& ylabels)
149  {
150  TH2D * h2d = Gaudi::Utils::Aida2ROOT::aida2root( hist );
151  return ( h2d &&
152  setBinLabels_( h2d->GetXaxis(), xlabels ) &&
153  setBinLabels_( h2d->GetYaxis(), ylabels ) );
154  }
155 
156  bool setBinLabels( AIDA::IProfile2D* hist,
157  const Labels& xlabels,
158  const Labels& ylabels )
159  {
160  if (!hist) return false;
161  TProfile2D * h2d = Gaudi::Utils::Aida2ROOT::aida2root( hist );
162  if (!h2d) return false;
163  BinLabels lx;
164  lx.reserve(xlabels.size());
165  for ( unsigned int i = 0; i < xlabels.size(); ++i )
166  {
167  lx.push_back(Gaudi::Utils::Histos::BinLabel( i , xlabels[i] ) );
168  }
169  BinLabels ly;
170  ly.reserve(ylabels.size());
171  for ( unsigned int i = 0; i < ylabels.size(); ++i )
172  {
173  ly.push_back(Gaudi::Utils::Histos::BinLabel( i , ylabels[i] ) );
174  }
175  return ( setBinLabels_( h2d->GetXaxis(), lx ) &&
176  setBinLabels_( h2d->GetYaxis(), ly ) );
177  }
178 
179  bool setBinLabels( AIDA::IProfile2D* hist,
180  const BinLabels& xlabels,
181  const BinLabels& ylabels )
182  {
183  TProfile2D * h2d = Gaudi::Utils::Aida2ROOT::aida2root( hist );
184  return ( h2d &&
185  setBinLabels_( h2d->GetXaxis(), xlabels ) &&
186  setBinLabels_( h2d->GetYaxis(), ylabels ) );
187  }
188 
189  // --------------------------------------------------------------------------
190 
191  bool setAxisLabels( AIDA::IHistogram1D* hist,
192  const std::string & xAxis,
193  const std::string & yAxis )
194  {
195  return setAxisLabels_<TH1D>( hist, xAxis, yAxis );
196  }
197 
198  bool setAxisLabels( AIDA::IProfile1D* hist,
199  const std::string & xAxis,
200  const std::string & yAxis )
201  {
202  return setAxisLabels_<TProfile>( hist, xAxis, yAxis );
203  }
204 
205  bool setAxisLabels( AIDA::IHistogram2D* hist,
206  const std::string & xAxis,
207  const std::string & yAxis )
208  {
209  return setAxisLabels_<TH2D>( hist, xAxis, yAxis );
210  }
211 
212  bool setAxisLabels( AIDA::IProfile2D* hist,
213  const std::string & xAxis,
214  const std::string & yAxis )
215  {
216  return setAxisLabels_<TProfile2D>( hist, xAxis, yAxis );
217  }
218 
219  // --------------------------------------------------------------------------
220 
221  }
222  }
223 }
std::vector< BinLabel > BinLabels
Typedef for a list of bin numbers and their associated label.
Definition: HistoLabels.h:32
GAUDI_API bool setAxisLabels(AIDA::IHistogram1D *hist, const std::string &xAxis, const std::string &yAxis)
Set the axis labels for the given 1D histogram.
static TH1D * aida2root(AIDA::IHistogram1D *aida)
get the underlying pointer for 1D-histogram
Definition: Aida2ROOT.cpp:55
std::vector< std::string > Labels
Typedef for a list of labels.
Definition: HistoLabels.h:28
dictionary l
Definition: gaudirun.py:365
GAUDI_API bool setBinLabels(AIDA::IHistogram1D *hist, const Labels &labels)
Set the Bin labels for a given 1D histogram.
std::pair< unsigned, std::string > BinLabel
Typedef for a bin number and its associated label.
Definition: HistoLabels.h:30
tuple root
Definition: IOTest.py:42
This is a number of static methods for bootstrapping the Gaudi framework.
Definition: Bootstrap.h:14
list i
Definition: ana.py:128