Project
Loading...
Searching...
No Matches
HistogramSpec.h
Go to the documentation of this file.
1// Copyright 2019-2020 CERN and copyright holders of ALICE O2.
2// See https://alice-o2.web.cern.ch/copyright for details of the copyright holders.
3// All rights not expressly granted are reserved.
4//
5// This software is distributed under the terms of the GNU General Public
6// License v3 (GPL Version 3), copied verbatim in the file "COPYING".
7//
8// In applying this license CERN does not waive the privileges and immunities
9// granted to it by virtue of its status as an Intergovernmental Organization
10// or submit itself to any jurisdiction.
11
12#ifndef FRAMEWORK_HISTOGRAMSPEC_H_
13#define FRAMEWORK_HISTOGRAMSPEC_H_
14
15#include <string>
16#include <utility>
17#include <variant>
18#include <optional>
19
22
23#include "Framework/StepTHn.h"
24#include <TH1.h>
25#include <TH2.h>
26#include <TH3.h>
27#include <THn.h>
28#include <THnSparse.h>
29#include <TProfile.h>
30#include <TProfile2D.h>
31#include <TProfile3D.h>
32
33namespace o2::framework
34{
35// Available root histogram types
71
72// variant of all possible root pointers; here we use only the interface types since the underlying data representation (int,float,double,long,char) is irrelevant
73using HistPtr = std::variant<std::shared_ptr<THn>, std::shared_ptr<THnSparse>, std::shared_ptr<TH3>, std::shared_ptr<TH2>, std::shared_ptr<TH1>, std::shared_ptr<TProfile3D>, std::shared_ptr<TProfile2D>, std::shared_ptr<TProfile>, std::shared_ptr<StepTHn>>;
74
75//**************************************************************************************************
79//**************************************************************************************************
80// flag to mark variable bin size in configurable bin edges
81constexpr double VARIABLE_WIDTH = 0.;
82
83struct AxisSpec {
84 AxisSpec(std::vector<double> binEdges_, std::optional<std::string> title_ = std::nullopt, std::optional<std::string> name_ = std::nullopt)
85 : nBins(std::nullopt),
86 binEdges(std::move(binEdges_)),
87 title(std::move(title_)),
88 name(std::move(name_))
89 {
90 }
91
92 AxisSpec(int nBins_, double binMin_, double binMax_, std::optional<std::string> title_ = std::nullopt, std::optional<std::string> name_ = std::nullopt)
93 : nBins(nBins_),
94 binEdges({binMin_, binMax_}),
95 title(std::move(title_)),
96 name(std::move(name_))
97 {
98 if (binMin_ > binMax_) {
99 LOG(fatal) << "Defined ill-defined axis";
100 }
101 }
102
103 // first entry is assumed to be the number of bins; in case of variable size binning it must be set to zero
104 AxisSpec(ConfigurableAxis binEdges_, std::optional<std::string> title_ = std::nullopt, std::optional<std::string> name_ = std::nullopt)
105 : nBins(std::nullopt),
106 binEdges(std::vector<double>(binEdges_)),
107 title(std::move(title_)),
108 name(std::move(name_))
109 {
110 if (binEdges.empty()) {
111 return;
112 }
113 if (binEdges[0] != VARIABLE_WIDTH) {
114 nBins = static_cast<int>(binEdges[0]);
115 binEdges.resize(3); // nBins, lowerBound, upperBound, disregard whatever else is stored in vecotr
116 }
117 binEdges.erase(binEdges.begin()); // remove first entry that we assume to be number of bins
118 }
119
120 [[nodiscard]] long getNbins() const;
121
123 void makeLogarithmic();
124
126 std::optional<int> nBins{};
127 std::vector<double> binEdges{};
128 std::optional<std::string> title{};
129 std::optional<std::string> name{};
130};
131
132//**************************************************************************************************
136//**************************************************************************************************
138 HistogramConfigSpec(HistType type_, std::vector<AxisSpec> axes_, uint8_t nSteps_ = 1)
139 : type(type_),
140 axes(std::move(axes_)),
141 nSteps(nSteps_)
142 {
143 }
147
148 void addAxis(const AxisSpec& axis)
149 {
150 axes.push_back(axis);
151 }
152
153 void addAxis(int nBins_, double binMin_, double binMax_, std::optional<std::string> title_ = std::nullopt, std::optional<std::string> name_ = std::nullopt)
154 {
155 axes.emplace_back(nBins_, binMin_, binMax_, title_, name_);
156 }
157
158 void addAxis(std::vector<double> binEdges_, std::optional<std::string> title_ = std::nullopt, std::optional<std::string> name_ = std::nullopt)
159 {
160 axes.emplace_back(binEdges_, title_, name_);
161 }
162
163 void addAxes(std::vector<AxisSpec> axes_)
164 {
165 axes.insert(axes.end(), axes_.begin(), axes_.end());
166 }
167
168 // add axes defined in other HistogramConfigSpec object
170 {
171 axes.insert(axes.end(), other.axes.begin(), other.axes.end());
172 }
173
175 std::vector<AxisSpec> axes{};
176 uint32_t nSteps{1}; // variable used only in StepTHn
177};
178
179//**************************************************************************************************
183//**************************************************************************************************
185 HistogramSpec(char const* name_, char const* const title_, HistogramConfigSpec config_, bool callSumw2_ = false)
186 : name(name_),
187 hash(runtime_hash(name_)),
188 title(title_),
189 config(std::move(config_)),
190 callSumw2(callSumw2_)
191 {
192 }
193
194 HistogramSpec() = default;
197
198 std::string name{};
199 uint32_t hash = 0;
200 std::string title{};
202 bool callSumw2{}; // wether or not hist needs heavy error structure produced by Sumw2()
203};
204
205//**************************************************************************************************
210//**************************************************************************************************
212
213 // create histogram of type T with the axes defined in HistogramSpec
214 template <typename T>
215 static std::unique_ptr<T> createHist(const HistogramSpec& histSpec);
216
217 // runtime version of the above
218 static HistPtr createHistVariant(const HistogramSpec& histSpec);
219
220 // helper function to get the axis via index for any type of root histogram
221 template <typename T>
222 static TAxis* getAxis(const int i, T* hist);
223};
224
225#define DECLAREEXT(HType) \
226 extern template std::unique_ptr<HType> HistFactory::createHist<HType>(const HistogramSpec& histSpec);
248DECLAREEXT(THnSparseD);
249DECLAREEXT(THnSparseF);
250DECLAREEXT(THnSparseI);
251DECLAREEXT(THnSparseC);
252DECLAREEXT(THnSparseS);
253DECLAREEXT(THnSparseL);
254DECLAREEXT(TProfile);
255DECLAREEXT(TProfile2D);
256DECLAREEXT(TProfile3D);
259#undef DECLAREEXT
260
261} // namespace o2::framework
262#endif // FRAMEWORK_HISTOGRAMSPEC_H_
int32_t i
#define DECLAREEXT(HType)
constexpr uint32_t runtime_hash(char const *str)
GLuint const GLchar * name
Definition glcorearb.h:781
GLint GLint GLsizei GLint GLenum GLenum type
Definition glcorearb.h:275
Defining PrimaryVertex explicitly as messageable.
Definition TFIDInfo.h:20
std::variant< std::shared_ptr< THn >, std::shared_ptr< THnSparse >, std::shared_ptr< TH3 >, std::shared_ptr< TH2 >, std::shared_ptr< TH1 >, std::shared_ptr< TProfile3D >, std::shared_ptr< TProfile2D >, std::shared_ptr< TProfile >, std::shared_ptr< StepTHn > > HistPtr
constexpr double VARIABLE_WIDTH
Defining DataPointCompositeObject explicitly as copiable.
void makeLogarithmic()
Function to make the axis logarithmic.
std::optional< int > nBins
Data members.
AxisSpec(std::vector< double > binEdges_, std::optional< std::string > title_=std::nullopt, std::optional< std::string > name_=std::nullopt)
std::optional< std::string > title
Edges of the bin. For fixed bin width these are the limits of the binning.
AxisSpec(int nBins_, double binMin_, double binMax_, std::optional< std::string > title_=std::nullopt, std::optional< std::string > name_=std::nullopt)
std::optional< std::string > name
Optional title of the axis.
std::vector< double > binEdges
Number of bins (only used for fixed bin width axis)
AxisSpec(ConfigurableAxis binEdges_, std::optional< std::string > title_=std::nullopt, std::optional< std::string > name_=std::nullopt)
static HistPtr createHistVariant(const HistogramSpec &histSpec)
static TAxis * getAxis(const int i, T *hist)
static std::unique_ptr< T > createHist(const HistogramSpec &histSpec)
void addAxes(const HistogramConfigSpec &other)
HistogramConfigSpec(HistogramConfigSpec const &other)=default
void addAxis(int nBins_, double binMin_, double binMax_, std::optional< std::string > title_=std::nullopt, std::optional< std::string > name_=std::nullopt)
HistogramConfigSpec(HistogramConfigSpec &&other)=default
void addAxis(const AxisSpec &axis)
void addAxes(std::vector< AxisSpec > axes_)
HistogramConfigSpec(HistType type_, std::vector< AxisSpec > axes_, uint8_t nSteps_=1)
void addAxis(std::vector< double > binEdges_, std::optional< std::string > title_=std::nullopt, std::optional< std::string > name_=std::nullopt)
HistogramSpec(HistogramSpec const &other)=default
HistogramSpec(HistogramSpec &&other)=default
HistogramSpec(char const *name_, char const *const title_, HistogramConfigSpec config_, bool callSumw2_=false)
HistogramConfigSpec config
VectorOfTObjectPtrs other
LOG(info)<< "Compressed in "<< sw.CpuTime()<< " s"