Project
Loading...
Searching...
No Matches
FlatHisto1D.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
15
16#ifndef ALICEO2_FLATHISTO1D_H
17#define ALICEO2_FLATHISTO1D_H
18
19#include <Rtypes.h>
20#include <vector>
21#include <gsl/span>
22#include <type_traits>
23#include <cassert>
24#include <memory>
25
26class TH1F;
27
28namespace o2
29{
30namespace dataformats
31{
32
33/*
34 Fast 1D histo class which can be messages as
35 FlatHisto1D<float> histo(nbins, xmin, xmax);
36 histo.fill(...);
37 pc.outputs().snapshot(Output{"Origin", "Desc", 0}, histo.getBase());
38
39 and received (read only!) as
40 const auto hdata = pc.inputs().get<gsl::span<float>>("histodata");
41 FlatHisto1D<float> histoView;
42 histoView.adoptExternal(hdata);
43 or directly
44 FlatHisto1D<float> histoView(pc.inputs().get<gsl::span<float>>("histodata"));
45*/
46
47template <typename T = float>
49{
50 static_assert(std::is_same<T, float>::value || std::is_same<T, double>::value, "T must be float or double");
51
52 public:
53 enum { NBins,
58
59 FlatHisto1D() = default;
60 FlatHisto1D(uint32_t nb, T xmin, T xmax);
62 FlatHisto1D(const gsl::span<const T> ext) { adoptExternal(ext); }
64 void adoptExternal(const gsl::span<const T> ext);
65 void init()
66 {
67 // when reading from file, need to call this method to make it operational
68 assert(mContainer.size() > NServiceSlots);
69 init(gsl::span<const T>(mContainer.data(), mContainer.size()));
70 }
71 void init(uint32_t nbx, T xmin, T xmax);
72 uint32_t getNBins() const { return mNBins; }
73 T getXMin() const { return mXMin; }
74 T getXMax() const { return mXMax; }
75 T getBinSize() const { return mBinSize; }
76 T getBinSizeInv() const { return mBinSizeInv; }
77
78 T getBinContent(uint32_t ib) const
79 {
80 assert(ib < getNBins());
81 return mDataPtr[ib];
82 }
83
84 const T* getData() const
85 {
86 return mDataPtr;
87 }
88
90 {
91 auto bin = getBin(x);
92 return isValidBin(bin) ? getBinContent(bin) : 0;
93 }
94
95 bool isValidBin(uint32_t bin) const { return bin < getNBins(); }
96 bool isBinEmpty(uint32_t bin) const { return getBinContent(bin) == 0; }
97
98 T getBinStart(uint32_t i) const
99 {
100 assert(i < getNBins());
101 return getXMin() + i * getBinSize();
102 }
103
104 T getBinCenter(uint32_t i) const
105 {
106 assert(i < getNBins());
107 return getXMin() + (i + 0.5) * getBinSize();
108 }
109
110 T getBinEnd(uint32_t i) const
111 {
112 assert(i < getNBins());
113 return getXMin() + (i + 1) * getBinSize();
114 }
115
116 void add(const FlatHisto1D& other);
117
118 void subtract(const FlatHisto1D& other);
119
120 void setBinContent(uint32_t bin, T w)
121 {
122 assert(canFill() && isValidBin(bin));
123 mDataPtr[bin] = w;
124 }
125
126 void clear()
127 {
128 assert(canFill());
129 memset(mDataPtr, 0, sizeof(T) * getNBins());
130 }
131
132 T getSum() const;
133
134 int fill(T x)
135 {
136 uint32_t bin = getBin(x);
137 if (isValidBin(bin)) {
138 mDataPtr[bin]++;
139 return (int)bin;
140 }
141 return -1;
142 }
143
144 int fill(T x, T w)
145 {
146 uint32_t bin = getBin(x);
147 if (isValidBin(bin)) {
148 mDataPtr[bin] += w;
149 return (int)bin;
150 }
151 return -1;
152 }
153
154 void fillBin(uint32_t bin, T w)
155 {
156 if (isValidBin(bin)) {
157 mDataPtr[bin] += w;
158 }
159 }
160
161 uint32_t getBin(T x) const
162 {
163 auto dx = x - getXMin();
164 return dx < 0 ? 0xffffffff : uint32_t(dx * getBinSizeInv());
165 }
166
167 bool canFill() const
168 {
169 // histo can be filled only if hase its own data, otherwise only query can be done on the view
170 return mContainer.size() > NServiceSlots;
171 }
172
173 std::unique_ptr<TH1F> createTH1F(const std::string& name = "histo1d") const;
174
175 const std::vector<T>& getBase() const { return mContainer; }
176 gsl::span<const T> getView() const { return mContainerView; }
177
178 protected:
179 void init(const gsl::span<const T> ext);
180
181 std::vector<T> mContainer; // global container
182 gsl::span<const T> mContainerView{};
183 T* mDataPtr{};
184 T mXMin{};
185 T mXMax{};
188 uint32_t mNBins{};
189
191};
192
195
196} // namespace dataformats
197} // namespace o2
198
199#endif
int32_t i
uint32_t getBin(T x) const
FlatHisto1D(const gsl::span< const T > ext)
Definition FlatHisto1D.h:62
std::vector< T > mContainer
const std::vector< T > & getBase() const
void adoptExternal(const gsl::span< const T > ext)
bool isBinEmpty(uint32_t bin) const
Definition FlatHisto1D.h:96
std::unique_ptr< TH1F > createTH1F(const std::string &name="histo1d") const
FlatHisto1D & operator=(const FlatHisto1D &rhs)
void add(const FlatHisto1D &other)
T getBinStart(uint32_t i) const
Definition FlatHisto1D.h:98
T getBinEnd(uint32_t i) const
const T * getData() const
Definition FlatHisto1D.h:84
gsl::span< const T > mContainerView
T * mDataPtr
pointer on container
void subtract(const FlatHisto1D &other)
void fillBin(uint32_t bin, T w)
T getBinCenter(uint32_t i) const
T getBinContent(uint32_t ib) const
Definition FlatHisto1D.h:78
uint32_t getNBins() const
Definition FlatHisto1D.h:72
bool isValidBin(uint32_t bin) const
Definition FlatHisto1D.h:95
gsl::span< const T > getView() const
void setBinContent(uint32_t bin, T w)
GLint GLenum GLint x
Definition glcorearb.h:403
GLenum src
Definition glcorearb.h:1767
GLuint const GLchar * name
Definition glcorearb.h:781
GLubyte GLubyte GLubyte GLubyte w
Definition glcorearb.h:852
a couple of static helper functions to create timestamp values for CCDB queries or override obsolete ...
VectorOfTObjectPtrs other