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
74
75// 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
76using 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>>;
77
78//**************************************************************************************************
82//**************************************************************************************************
83// flag to mark variable bin size in configurable bin edges
84constexpr double VARIABLE_WIDTH = 0.;
85
86struct AxisSpec {
87 AxisSpec(std::vector<double> binEdges_, std::optional<std::string> title_ = std::nullopt, std::optional<std::string> name_ = std::nullopt)
88 : nBins(std::nullopt),
89 binEdges(std::move(binEdges_)),
90 title(std::move(title_)),
91 name(std::move(name_))
92 {
93 }
94
95 AxisSpec(int nBins_, double binMin_, double binMax_, std::optional<std::string> title_ = std::nullopt, std::optional<std::string> name_ = std::nullopt)
96 : nBins(nBins_),
97 binEdges({binMin_, binMax_}),
98 title(std::move(title_)),
99 name(std::move(name_))
100 {
101 if (binMin_ > binMax_) {
102 LOG(fatal) << "Defined ill-defined axis";
103 }
104 }
105
106 // first entry is assumed to be the number of bins; in case of variable size binning it must be set to zero
107 AxisSpec(ConfigurableAxis binEdges_, std::optional<std::string> title_ = std::nullopt, std::optional<std::string> name_ = std::nullopt)
108 : nBins(std::nullopt),
109 binEdges(std::vector<double>(binEdges_)),
110 title(std::move(title_)),
111 name(std::move(name_))
112 {
113 if (binEdges.empty()) {
114 return;
115 }
116 if (binEdges[0] != VARIABLE_WIDTH) {
117 nBins = static_cast<int>(binEdges[0]);
118 binEdges.resize(3); // nBins, lowerBound, upperBound, disregard whatever else is stored in vecotr
119 }
120 binEdges.erase(binEdges.begin()); // remove first entry that we assume to be number of bins
121 }
122
123 [[nodiscard]] long getNbins() const;
124
126 void makeLogarithmic();
127
129 std::optional<int> nBins{};
130 std::vector<double> binEdges{};
131 std::optional<std::string> title{};
132 std::optional<std::string> name{};
133};
134
135//**************************************************************************************************
139//**************************************************************************************************
141 HistogramConfigSpec(HistType type_, std::vector<AxisSpec> axes_, uint8_t nSteps_ = 1)
142 : type(type_),
143 axes(std::move(axes_)),
144 nSteps(nSteps_)
145 {
146 }
150
151 void addAxis(const AxisSpec& axis)
152 {
153 axes.push_back(axis);
154 }
155
156 void addAxis(int nBins_, double binMin_, double binMax_, std::optional<std::string> title_ = std::nullopt, std::optional<std::string> name_ = std::nullopt)
157 {
158 axes.emplace_back(nBins_, binMin_, binMax_, title_, name_);
159 }
160
161 void addAxis(std::vector<double> binEdges_, std::optional<std::string> title_ = std::nullopt, std::optional<std::string> name_ = std::nullopt)
162 {
163 axes.emplace_back(binEdges_, title_, name_);
164 }
165
166 void addAxes(std::vector<AxisSpec> axes_)
167 {
168 axes.insert(axes.end(), axes_.begin(), axes_.end());
169 }
170
171 // add axes defined in other HistogramConfigSpec object
173 {
174 axes.insert(axes.end(), other.axes.begin(), other.axes.end());
175 }
176
178 std::vector<AxisSpec> axes{};
179 uint32_t nSteps{1}; // variable used only in StepTHn
180};
181
182//**************************************************************************************************
186//**************************************************************************************************
188 HistogramSpec(char const* name_, char const* const title_, HistogramConfigSpec config_, bool callSumw2_ = false)
189 : name(name_),
190 hash(runtime_hash(name_)),
191 title(title_),
192 config(std::move(config_)),
193 callSumw2(callSumw2_)
194 {
195 }
196
197 HistogramSpec() = default;
200
201 std::string name{};
202 uint32_t hash = 0;
203 std::string title{};
205 bool callSumw2{}; // wether or not hist needs heavy error structure produced by Sumw2()
206};
207
208//**************************************************************************************************
213//**************************************************************************************************
215
216 // create histogram of type T with the axes defined in HistogramSpec
217 template <typename T>
218 static std::unique_ptr<T> createHist(const HistogramSpec& histSpec);
219
220 // runtime version of the above
221 static HistPtr createHistVariant(const HistogramSpec& histSpec);
222
223 // helper function to get the axis via index for any type of root histogram
224 template <typename T>
225 static TAxis* getAxis(const int i, T* hist);
226};
227
228#define DECLAREEXT(HType) \
229 extern template std::unique_ptr<HType> HistFactory::createHist<HType>(const HistogramSpec& histSpec);
254DECLAREEXT(THnSparseD);
255DECLAREEXT(THnSparseF);
256DECLAREEXT(THnSparseI);
257DECLAREEXT(THnSparseC);
258DECLAREEXT(THnSparseS);
259DECLAREEXT(THnSparseL);
260DECLAREEXT(TProfile);
261DECLAREEXT(TProfile2D);
262DECLAREEXT(TProfile3D);
265#undef DECLAREEXT
266
267} // namespace o2::framework
268#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"