The Gaudi Framework  master (1304469f)
Loading...
Searching...
No Matches
HistoLabels.cpp
Go to the documentation of this file.
1/***********************************************************************************\
2* (c) Copyright 1998-2025 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
17// local
20
21// ============================================================================
22// ROOT
23// ============================================================================
24#include <TH1D.h>
25#include <TH2D.h>
26#include <TProfile.h>
27#include <TProfile2D.h>
28
29// Private namespace
30namespace {
31 //--------------------------------------------------------------------
32
33 template <typename R, typename A>
34 bool setAxisLabels_( A* aida, const std::string& xAxis, const std::string& yAxis ) {
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, const Gaudi::Utils::Histos::BinLabels& labels ) {
46 if ( !axis ) return false;
47 const unsigned nbins = axis->GetNbins();
48 for ( const auto& i : labels ) {
49 if ( 1 + i.first <= 0 || 1 + i.first > nbins ) return false;
50 // Argh... ROOT bins start counting at '1' instead of '0'
51 axis->SetBinLabel( 1 + i.first, i.second.c_str() );
52 }
53 return true;
54 }
55
56 //--------------------------------------------------------------------
57
58 template <typename R, typename A>
59 bool setBinLabels_( A* aida, const Gaudi::Utils::Histos::BinLabels& labels ) {
60 if ( !aida ) return false;
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 ) {
69 l.reserve( labels.size() );
70 for ( unsigned i = 0; i < labels.size(); ++i ) l.emplace_back( i, labels[i] );
71 return Gaudi::Utils::Histos::setBinLabels( hist, l );
72 }
73
74 //--------------------------------------------------------------------
75} // namespace
76
77namespace Gaudi {
78 namespace Utils {
79 namespace Histos {
80
81 // --------------------------------------------------------------------------
82
83 bool setBinLabels( AIDA::IHistogram1D* hist, const BinLabels& labels ) {
84 return setBinLabels_<TH1D>( hist, labels );
85 }
86
87 bool setBinLabels( AIDA::IProfile1D* hist, const BinLabels& labels ) {
88 return setBinLabels_<TProfile>( hist, labels );
89 }
90
91 bool setBinLabels( AIDA::IHistogram1D* hist, const Labels& labels ) { return setBinLabels_( hist, labels ); }
92
93 bool setBinLabels( AIDA::IProfile1D* hist, const Labels& labels ) { return setBinLabels_( hist, labels ); }
94
95 bool setBinLabels( AIDA::IHistogram2D* hist, const Labels& xlabels, const Labels& ylabels ) {
96 if ( !hist ) return false;
97 TH2D* h2d = Gaudi::Utils::Aida2ROOT::aida2root( hist );
98 if ( !h2d ) return false;
99 BinLabels lx;
100 lx.reserve( xlabels.size() );
101 for ( unsigned int i = 0; i < xlabels.size(); ++i ) { lx.emplace_back( i, xlabels[i] ); }
102 BinLabels ly;
103 ly.reserve( ylabels.size() );
104 for ( unsigned int i = 0; i < ylabels.size(); ++i ) { ly.emplace_back( i, ylabels[i] ); }
105 return ( setBinLabels_( h2d->GetXaxis(), lx ) && setBinLabels_( h2d->GetYaxis(), ly ) );
106 }
107
108 bool setBinLabels( AIDA::IHistogram2D* hist, const BinLabels& xlabels, const BinLabels& ylabels ) {
109 TH2D* h2d = Gaudi::Utils::Aida2ROOT::aida2root( hist );
110 return ( h2d && setBinLabels_( h2d->GetXaxis(), xlabels ) && setBinLabels_( h2d->GetYaxis(), ylabels ) );
111 }
112
113 bool setBinLabels( AIDA::IProfile2D* hist, const Labels& xlabels, const Labels& ylabels ) {
114 if ( !hist ) return false;
115 TProfile2D* h2d = Gaudi::Utils::Aida2ROOT::aida2root( hist );
116 if ( !h2d ) return false;
117 BinLabels lx;
118 lx.reserve( xlabels.size() );
119 for ( unsigned int i = 0; i < xlabels.size(); ++i ) { lx.emplace_back( i, xlabels[i] ); }
120 BinLabels ly;
121 ly.reserve( ylabels.size() );
122 for ( unsigned int i = 0; i < ylabels.size(); ++i ) { ly.emplace_back( i, ylabels[i] ); }
123 return ( setBinLabels_( h2d->GetXaxis(), lx ) && setBinLabels_( h2d->GetYaxis(), ly ) );
124 }
125
126 bool setBinLabels( AIDA::IProfile2D* hist, const BinLabels& xlabels, const BinLabels& ylabels ) {
127 TProfile2D* h2d = Gaudi::Utils::Aida2ROOT::aida2root( hist );
128 return ( h2d && setBinLabels_( h2d->GetXaxis(), xlabels ) && setBinLabels_( h2d->GetYaxis(), ylabels ) );
129 }
130
131 // --------------------------------------------------------------------------
132
133 bool setAxisLabels( AIDA::IHistogram1D* hist, const std::string& xAxis, const std::string& yAxis ) {
134 return setAxisLabels_<TH1D>( hist, xAxis, yAxis );
135 }
136
137 bool setAxisLabels( AIDA::IProfile1D* hist, const std::string& xAxis, const std::string& yAxis ) {
138 return setAxisLabels_<TProfile>( hist, xAxis, yAxis );
139 }
140
141 bool setAxisLabels( AIDA::IHistogram2D* hist, const std::string& xAxis, const std::string& yAxis ) {
142 return setAxisLabels_<TH2D>( hist, xAxis, yAxis );
143 }
144
145 bool setAxisLabels( AIDA::IProfile2D* hist, const std::string& xAxis, const std::string& yAxis ) {
146 return setAxisLabels_<TProfile2D>( hist, xAxis, yAxis );
147 }
148
149 // --------------------------------------------------------------------------
150 } // namespace Histos
151 } // namespace Utils
152} // namespace Gaudi
static TH1D * aida2root(AIDA::IHistogram1D *aida)
get the underlying pointer for 1D-histogram
Definition Aida2ROOT.cpp:60
Collection of useful utilities for manipulations with AIDA hisgograms.
Definition HistoDump.h:27
std::vector< BinLabel > BinLabels
Typedef for a list of bin numbers and their associated label.
Definition HistoLabels.h:36
std::vector< std::string > Labels
Typedef for a list of labels.
Definition HistoLabels.h:32
GAUDI_API bool setBinLabels(AIDA::IHistogram1D *hist, const Labels &labels)
Set the Bin labels for a given 1D histogram.
GAUDI_API bool setAxisLabels(AIDA::IHistogram1D *hist, const std::string &xAxis, const std::string &yAxis)
Set the axis labels for the given 1D histogram.
This file provides a Grammar for the type Gaudi::Accumulators::Axis It allows to use that type from p...
Definition __init__.py:1
dict l
Definition gaudirun.py:583