CbmRoot
Loading...
Searching...
No Matches
Histo1D.cxx
Go to the documentation of this file.
1/* Copyright (C) 2023 GSI Helmholtzzentrum fuer Schwerionenforschung, Darmstadt
2 SPDX-License-Identifier: GPL-3.0-only
3 Authors: Volker Friese [committer] */
4
5#include "Histo1D.h"
6
7#include <cassert>
8#include <cmath>
9#include <cstdint>
10#include <sstream>
11#include <stdexcept>
12#include <string>
13#include <vector>
14
15
16namespace cbm::algo
17{
18
19 // ----- Standard constructor
20 Histo1D::Histo1D(uint32_t numBins, double minValue, double maxValue, const std::string& name,
21 const std::string& title)
22 : fName(name)
23 , fTitle(title)
24 , fNumBins(numBins)
25 , fMinValue(minValue)
26 , fMaxValue(maxValue)
27 {
28 if (numBins == 0 || maxValue <= minValue)
29 throw std::runtime_error("Histo1D: Invalid specifications " + std::to_string(numBins) + " "
30 + std::to_string(minValue) + " " + std::to_string(maxValue));
31 fContent.resize(numBins);
32 }
33
34 // ----- Add entry
35 void Histo1D::Add(double value, double weight)
36 {
37 if (value < fMinValue)
38 fUnderflow += weight;
39 else if (!(value < fMaxValue))
40 fOverflow += weight;
41 else {
42 uint32_t bin = uint32_t(double(fNumBins) * (value - fMinValue) / (fMaxValue - fMinValue));
43 assert(bin < fNumBins);
44 fContent[bin] += weight;
46 }
47 }
48
49 // ----- Clear content
51 {
52 fContent.assign(fNumBins, 0.);
53 fUnderflow = 0;
54 fOverflow = 0;
55 fNumEntries = 0;
56 }
57
58 // ----- Content access
59 double Histo1D::Content(uint32_t bin) const
60 {
61 if (fNumBins < bin)
62 return 0.;
63 else
64 return fContent[bin];
65 }
66
67
68 // ----- First moment of distribution
69 double Histo1D::Mean() const
70 {
71 double sum1 = 0.;
72 double sum2 = 0.;
73 double binsize = (fMaxValue - fMinValue) / double(fNumBins);
74 for (uint32_t bin = 0; bin < fNumBins; bin++) {
75 double x = fMinValue + (bin + 0.5) * binsize;
76 sum1 += x * fContent[bin];
77 sum2 += fContent[bin];
78 }
79 return sum1 / sum2;
80 }
81
82
83 // ----- Operator +=
84 // TODO: Comparison of floating point numbers; probably fishy.
86 {
87 if (other.fNumBins == fNumBins && other.fMinValue == fMinValue && other.fMaxValue == fMaxValue) {
88 fUnderflow += other.fUnderflow;
89 fOverflow += other.fOverflow;
90 for (uint32_t bin = 0; bin < fNumBins; bin++)
91 fContent[bin] += other.fContent[bin];
92 fNumEntries += other.fNumEntries;
93 }
94 else
95 throw std::runtime_error("Histo1D: Trying to add incompatible histograms");
96 return *this;
97 }
98
99
100 // ----- Second moment of distribution
101 double Histo1D::Stddev() const
102 {
103 double sum1 = 0.;
104 double sum2 = 0.;
105 double binsize = (fMaxValue - fMinValue) / double(fNumBins);
106 for (uint32_t bin = 0; bin < fNumBins; bin++) {
107 double x = fMinValue + (bin + 0.5) * binsize;
108 sum1 += x * x * fContent[bin];
109 sum2 += fContent[bin];
110 }
111 double mean = Mean();
112 return sqrt(sum1 / sum2 - mean * mean);
113 }
114
115
116 // ----- Properties to string
117 std::string Histo1D::ToString() const
118 {
119 std::stringstream ss;
120 ss << fName << ": bins " << fNumBins << " range " << fMinValue << " to " << fMaxValue << " entries " << fNumEntries
121 << " mean " << Mean() << " stddev " << Stddev() << " out of range " << fUnderflow << " , " << fOverflow
122 << " title <" << fTitle << ">";
123 return ss.str();
124 }
125
126} /* namespace cbm::algo */
friend fvec sqrt(const fvec &a)
double Content(uint32_t bin) const
Histogram content in a bin.
Definition Histo1D.cxx:59
std::string fTitle
Definition Histo1D.h:116
void Clear()
Clear histogram contents.
Definition Histo1D.cxx:50
double Stddev() const
Second moment of distribution.
Definition Histo1D.cxx:101
uint32_t fNumBins
Definition Histo1D.h:117
double Mean() const
First moment of distribution.
Definition Histo1D.cxx:69
Histo1D & operator+=(const Histo1D &other)
Add another histogram to an existing one.
Definition Histo1D.cxx:85
std::string ToString() const
Properties to string.
Definition Histo1D.cxx:117
void Add(double value, double weight=1.)
Add an entry to the histogram.
Definition Histo1D.cxx:35
std::vector< double > fContent
Definition Histo1D.h:122
std::string fName
Definition Histo1D.h:115
Histo1D()
Default constructor: needed for boost serialization of vector of Histo1D, need copy ctor call after!
Definition Histo1D.h:143