-
Notifications
You must be signed in to change notification settings - Fork 499
Expand file tree
/
Copy pathFlatHisto1D.cxx
More file actions
135 lines (121 loc) · 3.78 KB
/
FlatHisto1D.cxx
File metadata and controls
135 lines (121 loc) · 3.78 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
// Copyright 2019-2020 CERN and copyright holders of ALICE O2.
// See https://alice-o2.web.cern.ch/copyright for details of the copyright holders.
// All rights not expressly granted are reserved.
//
// This software is distributed under the terms of the GNU General Public
// License v3 (GPL Version 3), copied verbatim in the file "COPYING".
//
// In applying this license CERN does not waive the privileges and immunities
// granted to it by virtue of its status as an Intergovernmental Organization
// or submit itself to any jurisdiction.
/// @file FlatHisto1D.cxx
/// \brief 1D messeageable histo class
/// \author ruben.shahoyan@cern.ch
#include "CommonDataFormat/FlatHisto1D.h"
#include <TH1F.h>
namespace o2
{
namespace dataformats
{
template <typename T>
FlatHisto1D<T>::FlatHisto1D(uint32_t nb, T xmin, T xmax)
{
init(nb, xmin, xmax);
}
template <typename T>
FlatHisto1D<T>::FlatHisto1D(const FlatHisto1D& src)
{
mContainer = src.mContainer;
init(gsl::span<const T>(mContainer.data(), mContainer.size()));
}
template <typename T>
FlatHisto1D<T>& FlatHisto1D<T>::operator=(const FlatHisto1D<T>& rhs)
{
if (this == &rhs) {
return *this;
}
if (!rhs.canFill() && rhs.getNBins()) { // was initialized
throw std::runtime_error("trying to copy read-only histogram");
}
if (rhs.getNBins()) {
mContainer = rhs.mContainer;
init(gsl::span<const T>(mContainer.data(), mContainer.size()));
}
return *this;
}
template <typename T>
void FlatHisto1D<T>::adoptExternal(const gsl::span<const T> ext)
{
assert(ext.size() > NServiceSlots);
mContainer.clear();
mContainerView = ext;
init(mContainerView);
}
template <typename T>
void FlatHisto1D<T>::add(const FlatHisto1D& other)
{
if (!(getNBins() == other.getNBins() && getXMin() == other.getXMin() && getXMax() == other.getXMax() && canFill())) {
throw std::runtime_error("adding incompatible histos or destination histo is const");
}
for (uint32_t i = getNBins(); i--;) {
mDataPtr[i] += other.mDataPtr[i];
}
}
template <typename T>
void FlatHisto1D<T>::subtract(const FlatHisto1D& other)
{
if (!(getNBins() == other.getNBins() && getXMin() == other.getXMin() && getXMax() == other.getXMax() && canFill())) {
throw std::runtime_error("subtracting incompatible histos or destination histo is const");
}
for (uint32_t i = getNBins(); i--;) {
mDataPtr[i] -= other.mDataPtr[i];
}
}
template <typename T>
T FlatHisto1D<T>::getSum() const
{
T sum = 0;
for (uint32_t i = getNBins(); i--;) {
sum += getBinContent(i);
}
return sum;
}
template <typename T>
void FlatHisto1D<T>::init(const gsl::span<const T> ext)
{ // when reading from file, need to call this method to make it operational
assert(ext.size() > NServiceSlots);
mContainerView = ext;
mDataPtr = const_cast<T*>(&ext[NServiceSlots]);
mNBins = (uint32_t)ext[NBins];
mXMin = ext[XMin];
mXMax = ext[XMax];
mBinSize = ext[BinSize];
mBinSizeInv = 1. / mBinSize;
}
template <typename T>
void FlatHisto1D<T>::init(uint32_t nb, T xmin, T xmax)
{ // when reading from file, need to call this method to make it operational
assert(nb > 0 && xmin < xmax);
mContainer.resize(nb + NServiceSlots, 0.);
mContainer[NBins] = nb;
mContainer[XMin] = xmin;
mContainer[XMax] = xmax;
mContainer[BinSize] = (xmax - xmin) / nb;
init(gsl::span<const T>(mContainer.data(), mContainer.size()));
}
template <typename T>
std::unique_ptr<TH1F> FlatHisto1D<T>::createTH1F(const std::string& name) const
{
auto h = std::make_unique<TH1F>(name.c_str(), name.c_str(), getNBins(), getXMin(), getXMax());
for (uint32_t i = getNBins(); i--;) {
auto w = getBinContent(i);
if (w) {
h->SetBinContent(i + 1, w);
}
}
return std::move(h);
}
template class FlatHisto1D<float>;
template class FlatHisto1D<double>;
} // namespace dataformats
} // namespace o2