Project
Loading...
Searching...
No Matches
FlatHisto1D.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
19namespace o2
20{
21namespace dataformats
22{
23
24template <typename T>
25FlatHisto1D<T>::FlatHisto1D(uint32_t nb, T xmin, T xmax)
26{
27 init(nb, xmin, xmax);
28}
29
30template <typename T>
32{
33 mContainer = src.mContainer;
34 init(gsl::span<const T>(mContainer.data(), mContainer.size()));
35}
36
37template <typename T>
39{
40 if (this == &rhs) {
41 return *this;
42 }
43 if (!rhs.canFill() && rhs.getNBins()) { // was initialized
44 throw std::runtime_error("trying to copy read-only histogram");
45 }
46 if (rhs.getNBins()) {
47 mContainer = rhs.mContainer;
48 init(gsl::span<const T>(mContainer.data(), mContainer.size()));
49 }
50 return *this;
51}
52
53template <typename T>
54void FlatHisto1D<T>::adoptExternal(const gsl::span<const T> ext)
55{
56 assert(ext.size() > NServiceSlots);
57 mContainer.clear();
58 mContainerView = ext;
59 init(mContainerView);
60}
61
62template <typename T>
64{
65 if (!(getNBins() == other.getNBins() && getXMin() == other.getXMin() && getXMax() == other.getXMax() && canFill())) {
66 throw std::runtime_error("adding incompatible histos or destination histo is const");
67 }
68 for (uint32_t i = getNBins(); i--;) {
69 mDataPtr[i] += other.mDataPtr[i];
70 }
71}
72
73template <typename T>
75{
76 if (!(getNBins() == other.getNBins() && getXMin() == other.getXMin() && getXMax() == other.getXMax() && canFill())) {
77 throw std::runtime_error("subtracting incompatible histos or destination histo is const");
78 }
79 for (uint32_t i = getNBins(); i--;) {
80 mDataPtr[i] -= other.mDataPtr[i];
81 }
82}
83
84template <typename T>
86{
87 T sum = 0;
88 for (uint32_t i = getNBins(); i--;) {
89 sum += getBinContent(i);
90 }
91 return sum;
92}
93
94template <typename T>
95void FlatHisto1D<T>::init(const gsl::span<const T> ext)
96{ // when reading from file, need to call this method to make it operational
97 assert(ext.size() > NServiceSlots);
98 mContainerView = ext;
99 mDataPtr = const_cast<T*>(&ext[NServiceSlots]);
100 mNBins = (uint32_t)ext[NBins];
101 mXMin = ext[XMin];
102 mXMax = ext[XMax];
103 mBinSize = ext[BinSize];
104 mBinSizeInv = 1. / mBinSize;
105}
106template <typename T>
107void FlatHisto1D<T>::init(uint32_t nb, T xmin, T xmax)
108{ // when reading from file, need to call this method to make it operational
109 assert(nb > 0 && xmin < xmax);
110 mContainer.resize(nb + NServiceSlots, 0.);
111 mContainer[NBins] = nb;
112 mContainer[XMin] = xmin;
113 mContainer[XMax] = xmax;
114 mContainer[BinSize] = (xmax - xmin) / nb;
115 init(gsl::span<const T>(mContainer.data(), mContainer.size()));
116}
117
118template <typename T>
119std::unique_ptr<TH1F> FlatHisto1D<T>::createTH1F(const std::string& name) const
120{
121 auto h = std::make_unique<TH1F>(name.c_str(), name.c_str(), getNBins(), getXMin(), getXMax());
122 for (uint32_t i = getNBins(); i--;) {
123 auto w = getBinContent(i);
124 if (w) {
125 h->SetBinContent(i + 1, w);
126 }
127 }
128 return std::move(h);
129}
130
131template class FlatHisto1D<float>;
132template class FlatHisto1D<double>;
133
134} // namespace dataformats
135} // namespace o2
1D messeageable histo class
int32_t i
Class for time synchronization of RawReader instances.
void adoptExternal(const gsl::span< const T > ext)
std::unique_ptr< TH1F > createTH1F(const std::string &name="histo1d") const
FlatHisto1D & operator=(const FlatHisto1D &rhs)
void add(const FlatHisto1D &other)
void subtract(const FlatHisto1D &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
a couple of static helper functions to create timestamp values for CCDB queries or override obsolete ...
VectorOfTObjectPtrs other