The Gaudi Framework  master (37c0b60a)
HistoLabels.cpp
Go to the documentation of this file.
1 /***********************************************************************************\
2 * (c) Copyright 1998-2024 CERN for the benefit of the LHCb and ATLAS collaborations *
3 * *
4 * This software is distributed under the terms of the Apache version 2 licence, *
5 * copied verbatim in the file "LICENSE". *
6 * *
7 * In applying this licence, CERN does not waive the privileges and immunities *
8 * granted to it by virtue of its status as an Intergovernmental Organization *
9 * or submit itself to any jurisdiction. *
10 \***********************************************************************************/
11 // Include files
12 
13 //-----------------------------------------------------------------------------
14 // Implementation file for class : HistoLabels
15 //-----------------------------------------------------------------------------
16 #ifdef WIN32
17 // Disable warning
18 // warning C4996: 'sprintf': This function or variable may be unsafe.
19 // coming from TString.h
20 # pragma warning( disable : 4996 )
21 #endif
22 
23 // local
24 #include <GaudiUtils/Aida2ROOT.h>
25 #include <GaudiUtils/HistoLabels.h>
26 
27 // ============================================================================
28 // ROOT
29 // ============================================================================
30 #include <TH1D.h>
31 #include <TH2D.h>
32 #include <TProfile.h>
33 #include <TProfile2D.h>
34 
35 // Private namespace
36 namespace {
37  //--------------------------------------------------------------------
38 
39  template <typename R, typename A>
40  bool setAxisLabels_( A* aida, const std::string& xAxis, const std::string& yAxis ) {
41  if ( !aida ) return false;
43  if ( !root ) return false;
44  root->SetXTitle( xAxis.c_str() );
45  root->SetYTitle( yAxis.c_str() );
46  return true;
47  }
48 
49  //--------------------------------------------------------------------
50 
51  bool setBinLabels_( TAxis* axis, const Gaudi::Utils::Histos::BinLabels& labels ) {
52  if ( !axis ) return false;
53  const unsigned nbins = axis->GetNbins();
54  for ( const auto& i : labels ) {
55  if ( 1 + i.first <= 0 || 1 + i.first > nbins ) return false;
56  // Argh... ROOT bins start counting at '1' instead of '0'
57  axis->SetBinLabel( 1 + i.first, i.second.c_str() );
58  }
59  return true;
60  }
61 
62  //--------------------------------------------------------------------
63 
64  template <typename R, typename A>
65  bool setBinLabels_( A* aida, const Gaudi::Utils::Histos::BinLabels& labels ) {
66  if ( !aida ) return false;
68  if ( !root ) return false;
69  return setBinLabels_( root->GetXaxis(), labels );
70  }
71 
72  template <typename Histogram>
73  bool setBinLabels_( Histogram* hist, const Gaudi::Utils::Histos::Labels& labels ) {
75  l.reserve( labels.size() );
76  for ( unsigned i = 0; i < labels.size(); ++i ) l.emplace_back( i, labels[i] );
77  return Gaudi::Utils::Histos::setBinLabels( hist, l );
78  }
79 
80  //--------------------------------------------------------------------
81 } // namespace
82 
83 namespace Gaudi {
84  namespace Utils {
85  namespace Histos {
86 
87  // --------------------------------------------------------------------------
88 
89  bool setBinLabels( AIDA::IHistogram1D* hist, const BinLabels& labels ) {
90  return setBinLabels_<TH1D>( hist, labels );
91  }
92 
93  bool setBinLabels( AIDA::IProfile1D* hist, const BinLabels& labels ) {
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  if ( !hist ) return false;
103  TH2D* h2d = Gaudi::Utils::Aida2ROOT::aida2root( hist );
104  if ( !h2d ) return false;
105  BinLabels lx;
106  lx.reserve( xlabels.size() );
107  for ( unsigned int i = 0; i < xlabels.size(); ++i ) { lx.emplace_back( i, xlabels[i] ); }
108  BinLabels ly;
109  ly.reserve( ylabels.size() );
110  for ( unsigned int i = 0; i < ylabels.size(); ++i ) { ly.emplace_back( i, ylabels[i] ); }
111  return ( setBinLabels_( h2d->GetXaxis(), lx ) && setBinLabels_( h2d->GetYaxis(), ly ) );
112  }
113 
114  bool setBinLabels( AIDA::IHistogram2D* hist, const BinLabels& xlabels, const BinLabels& ylabels ) {
115  TH2D* h2d = Gaudi::Utils::Aida2ROOT::aida2root( hist );
116  return ( h2d && setBinLabels_( h2d->GetXaxis(), xlabels ) && setBinLabels_( h2d->GetYaxis(), ylabels ) );
117  }
118 
119  bool setBinLabels( AIDA::IProfile2D* hist, const Labels& xlabels, const Labels& ylabels ) {
120  if ( !hist ) return false;
121  TProfile2D* h2d = Gaudi::Utils::Aida2ROOT::aida2root( hist );
122  if ( !h2d ) return false;
123  BinLabels lx;
124  lx.reserve( xlabels.size() );
125  for ( unsigned int i = 0; i < xlabels.size(); ++i ) { lx.emplace_back( i, xlabels[i] ); }
126  BinLabels ly;
127  ly.reserve( ylabels.size() );
128  for ( unsigned int i = 0; i < ylabels.size(); ++i ) { ly.emplace_back( i, ylabels[i] ); }
129  return ( setBinLabels_( h2d->GetXaxis(), lx ) && setBinLabels_( h2d->GetYaxis(), ly ) );
130  }
131 
132  bool setBinLabels( AIDA::IProfile2D* hist, const BinLabels& xlabels, const BinLabels& ylabels ) {
133  TProfile2D* h2d = Gaudi::Utils::Aida2ROOT::aida2root( hist );
134  return ( h2d && setBinLabels_( h2d->GetXaxis(), xlabels ) && setBinLabels_( h2d->GetYaxis(), ylabels ) );
135  }
136 
137  // --------------------------------------------------------------------------
138 
139  bool setAxisLabels( AIDA::IHistogram1D* hist, const std::string& xAxis, const std::string& yAxis ) {
140  return setAxisLabels_<TH1D>( hist, xAxis, yAxis );
141  }
142 
143  bool setAxisLabels( AIDA::IProfile1D* hist, const std::string& xAxis, const std::string& yAxis ) {
144  return setAxisLabels_<TProfile>( hist, xAxis, yAxis );
145  }
146 
147  bool setAxisLabels( AIDA::IHistogram2D* hist, const std::string& xAxis, const std::string& yAxis ) {
148  return setAxisLabels_<TH2D>( hist, xAxis, yAxis );
149  }
150 
151  bool setAxisLabels( AIDA::IProfile2D* hist, const std::string& xAxis, const std::string& yAxis ) {
152  return setAxisLabels_<TProfile2D>( hist, xAxis, yAxis );
153  }
154 
155  // --------------------------------------------------------------------------
156  } // namespace Histos
157  } // namespace Utils
158 } // namespace Gaudi
Gaudi::Accumulators::Histogram
HistogramWrapper< StaticHistogram< ND, Atomicity, Arithmetic, AxisTupleType > > Histogram
standard custom histogram. See HistogramWrapper and StaticHistogram for details
Definition: Histogram.h:22
std::string
STL class.
Aida2ROOT.h
std::vector::reserve
T reserve(T... args)
std::vector
STL class.
std::vector::size
T size(T... args)
gaudiComponentHelp.root
root
Definition: gaudiComponentHelp.py:42
Gaudi::Utils::Aida2ROOT::aida2root
static TH1D * aida2root(AIDA::IHistogram1D *aida)
get the underlying pointer for 1D-histogram
Definition: Aida2ROOT.cpp:66
Gaudi::Utils::Histos::setBinLabels
GAUDI_API bool setBinLabels(AIDA::IHistogram1D *hist, const Labels &labels)
Set the Bin labels for a given 1D histogram.
Definition: HistoLabels.cpp:97
std::string::c_str
T c_str(T... args)
Gaudi
This file provides a Grammar for the type Gaudi::Accumulators::Axis It allows to use that type from p...
Definition: __init__.py:1
std::vector::emplace_back
T emplace_back(T... args)
gaudirun.l
dictionary l
Definition: gaudirun.py:583
HistoLabels.h
Gaudi::Utils::Histos::setAxisLabels
GAUDI_API bool setAxisLabels(AIDA::IHistogram1D *hist, const std::string &xAxis, const std::string &yAxis)
Set the axis labels for the given 1D histogram.
Definition: HistoLabels.cpp:139