Project
Loading...
Searching...
No Matches
FlatHisto2D.cxx
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
17#include <TH1F.h>
18#include <TH2F.h>
19
20namespace o2
21{
22namespace dataformats
23{
24
25using namespace o2::dataformats;
26
27template <typename T>
28FlatHisto2D<T>::FlatHisto2D(uint32_t nbx, T xmin, T xmax, uint32_t nby, T ymin, T ymax)
29{
30 init(nbx, xmin, xmax, nby, ymin, ymax);
31}
32
33template <typename T>
35{
36 mContainer = src.mContainer;
37 init(gsl::span<const T>(mContainer.data(), mContainer.size()));
38}
39
40template <typename T>
42{
43 if (this == &rhs) {
44 return *this;
45 }
46 if (!rhs.canFill() && rhs.getNBins()) { // was initialized
47 throw std::runtime_error("trying to copy read-only histogram");
48 }
49 if (rhs.getNBins()) {
50 mContainer = rhs.mContainer;
51 init(gsl::span<const T>(mContainer.data(), mContainer.size()));
52 }
53 return *this;
54}
55
56template <typename T>
57void FlatHisto2D<T>::adoptExternal(const gsl::span<const T> ext)
58{
59 assert(ext.size() > NServiceSlots);
60 mContainer.clear();
61 mContainerView = ext;
62 init(mContainerView);
63}
64
65template <typename T>
68 if (!(getNBinsX() == other.getNBinsX() && getXMin() == other.getXMin() && getXMax() == other.getXMax() &&
69 getNBinsY() == other.getNBinsY() && getYMin() == other.getYMin() && getYMax() == other.getYMax() &&
70 canFill())) {
71 throw std::runtime_error("adding incompatible histos or destination histo is const");
72 }
73 for (uint32_t i = getNBins(); i--;) {
74 mDataPtr[i] += other.mDataPtr[i];
75 }
77
78template <typename T>
80{
81 if (!(getNBinsX() == other.getNBinsX() && getXMin() == other.getXMin() && getXMax() == other.getXMax() &&
82 getNBinsY() == other.getNBinsY() && getYMin() == other.getYMin() && getYMax() == other.getYMax() &&
83 canFill())) {
84 throw std::runtime_error("subtracting incompatible histos or destination histo is const");
85 }
86 for (uint32_t i = getNBins(); i--;) {
87 mDataPtr[i] -= other.mDataPtr[i];
88 }
89}
90
91template <typename T>
93{
94 T sum = 0;
95 for (uint32_t i = getNBins(); i--;) {
96 sum += getBinContent(i);
97 }
98 return sum;
99}
100
101template <typename T>
102void FlatHisto2D<T>::init(const gsl::span<const T> ext)
103{ // when reading from file, need to call this method to make it operational
104 assert(ext.size() > NServiceSlots);
105 mContainerView = ext;
106 mDataPtr = const_cast<T*>(&ext[NServiceSlots]);
107 mNBinsX = (uint32_t)ext[NBinsX];
108 mNBinsY = (uint32_t)ext[NBinsY];
109 mXMin = ext[XMin];
110 mXMax = ext[XMax];
111 mYMin = ext[YMin];
112 mYMax = ext[YMax];
113 mBinSizeX = ext[BinSizeX];
114 mBinSizeY = ext[BinSizeY];
115 mBinSizeXInv = 1. / mBinSizeX;
116 mBinSizeYInv = 1. / mBinSizeY;
117}
118
119template <typename T>
120void FlatHisto2D<T>::init(uint32_t nbx, T xmin, T xmax, uint32_t nby, T ymin, T ymax)
121{ // when reading from file, need to call this method to make it operational
122 assert(nbx > 0 && xmin < xmax);
123 assert(nby > 0 && ymin < ymax);
124 mContainer.resize(nbx * nby + NServiceSlots, 0.);
125 mContainer[NBinsX] = nbx;
126 mContainer[NBinsY] = nby;
127 mContainer[XMin] = xmin;
128 mContainer[XMax] = xmax;
129 mContainer[YMin] = ymin;
130 mContainer[YMax] = ymax;
131 mContainer[BinSizeX] = (xmax - xmin) / nbx;
132 mContainer[BinSizeY] = (ymax - ymin) / nby;
133 init(gsl::span<const T>(mContainer.data(), mContainer.size()));
134}
135
136template <typename T>
137std::unique_ptr<TH2F> FlatHisto2D<T>::createTH2F(const std::string& name) const
138{
139 auto h = std::make_unique<TH2F>(name.c_str(), name.c_str(), getNBinsX(), getXMin(), getXMax(), getNBinsY(), getYMin(), getYMax());
140 for (uint32_t i = getNBinsX(); i--;) {
141 for (uint32_t j = getNBinsY(); j--;) {
142 auto w = getBinContent(i, j);
143 if (w) {
144 h->SetBinContent(i + 1, j + 1, w);
145 }
146 }
147 }
148 return std::move(h);
149}
150
151template <typename T>
152std::unique_ptr<TH1F> FlatHisto2D<T>::createSliceYTH1F(uint32_t binX, const std::string& name) const
153{
154 auto h = std::make_unique<TH1F>(name.c_str(), name.c_str(), getNBinsY(), getYMin(), getYMax());
155 if (binX < getNBinsX()) {
156 for (uint32_t i = getNBinsY(); i--;) {
157 h->SetBinContent(i + 1, getBinContent(binX, i));
158 }
159 }
160 return h;
161}
162
163template <typename T>
164std::unique_ptr<TH1F> FlatHisto2D<T>::createSliceXTH1F(uint32_t binY, const std::string& name) const
165{
166 auto h = std::make_unique<TH1F>(name.c_str(), name.c_str(), getNBinsX(), getXMin(), getXMax());
167 if (binY < getNBinsY()) {
168 for (uint32_t i = getNBinsX(); i--;) {
169 h->SetBinContent(i + 1, getBinContent(i, binY));
170 }
171 }
172 return h;
173}
174
175template class FlatHisto2D<float>;
176template class FlatHisto2D<double>;
177
178} // namespace dataformats
179} // namespace o2
2D messeageable histo class
int32_t i
uint32_t j
Definition RawData.h:0
Class for time synchronization of RawReader instances.
std::unique_ptr< TH1F > createSliceYTH1F(uint32_t binX, const std::string &name="histo2dsliceY") const
void subtract(const FlatHisto2D &other)
std::unique_ptr< TH1F > createSliceXTH1F(uint32_t binY, const std::string &name="histo2dsliceX") const
std::unique_ptr< TH2F > createTH2F(const std::string &name="histo2d") const
void adoptExternal(const gsl::span< const T > ext)
FlatHisto2D & operator=(const FlatHisto2D &rhs)
void add(const FlatHisto2D &other)
float sum(float s, o2::dcs::DataPointValue v)
Definition dcs-ccdb.cxx:39
GLenum src
Definition glcorearb.h:1767
GLuint const GLchar * name
Definition glcorearb.h:781
GLubyte GLubyte GLubyte GLubyte w
Definition glcorearb.h:852
Definition of a container to keep/associate and arbitrary number of labels associated to an index wit...
a couple of static helper functions to create timestamp values for CCDB queries or override obsolete ...
VectorOfTObjectPtrs other