ATLAS Offline Software
Loading...
Searching...
No Matches
TFCS1DFunctionTemplateHistogram.h
Go to the documentation of this file.
1/*
2 Copyright (C) 2002-2022 CERN for the benefit of the ATLAS collaboration
3*/
4
5#ifndef ISF_FASTCALOSIMEVENT_TFCS1DFunctionTemplateHistogram_h
6#define ISF_FASTCALOSIMEVENT_TFCS1DFunctionTemplateHistogram_h
7
10
11#include "TH1.h"
12#include <iostream>
13
14template <typename Txvec, typename Ty, typename Trandom = float>
16public:
18 typedef Trandom random_type;
19 typedef Txvec xvec_type;
20 typedef Ty y_value_type;
21
22 TFCS1DFunctionTemplateHistogram(TH1 *hist = nullptr, bool doprint = true) {
23 if (hist)
24 Initialize(hist, doprint);
25 };
27
28 std::size_t MemorySizeArray() const {
29 return m_HistoBorders.MemorySizeArray() + m_HistoContents.MemorySizeArray();
30 };
31 std::size_t MemorySize() const { return sizeof(*this) + MemorySizeArray(); };
32
34 void set_nbins(size_t nbins) {
35 m_HistoBorders.set_nbins(nbins);
36 m_HistoContents.set_nbins(nbins);
37 };
38
40 inline size_t get_nbins() const { return m_HistoContents.get_nbins(); };
41
44 void Initialize(const TH1 *hist, bool doprint = true) {
45 Int_t nbins = hist->GetNbinsX();
46
47 std::vector<double> temp_HistoContents;
48 int first, last;
49 double integral =
50 CheckAndIntegrate1DHistogram(hist, temp_HistoContents, first, last);
51 if (integral <= 0) {
52 set_nbins(0);
53 } else {
54 set_nbins(nbins);
55
56 m_HistoBorders.SetMinMax(hist->GetXaxis()->GetBinLowEdge(first + 1),
57 hist->GetXaxis()->GetBinUpEdge(last + 1));
58 Int_t ihist = 0;
59 for (Int_t ibin = first; ibin <= last; ++ibin) {
60 m_HistoBorders.SetBinLowEdge(ihist,
61 hist->GetXaxis()->GetBinLowEdge(ibin + 1));
62 if (ihist > 0)
63 if (m_HistoBorders.GetBinLowEdge(ihist - 1) ==
64 m_HistoBorders.GetBinLowEdge(ihist)) {
65 if (doprint)
66 ATH_MSG_INFO("Skip bin="
67 << ibin + 1
68 << " x=" << hist->GetXaxis()->GetBinLowEdge(ibin + 1)
69 << " fx=" << m_HistoBorders.GetBinLowEdge(ihist));
70 --ihist;
71 if (doprint)
72 ATH_MSG_INFO(" bin="
73 << ibin
74 << " x=" << hist->GetXaxis()->GetBinLowEdge(ibin)
75 << " fx=" << m_HistoBorders.GetBinLowEdge(ihist));
76 }
77 m_HistoContents.set_fraction(ihist,
78 temp_HistoContents[ibin] / integral);
79 if (ihist > 0)
80 if (m_HistoContents.get_fraction(ihist - 1) ==
81 m_HistoContents.get_fraction(ihist)) {
82 if (doprint)
83 ATH_MSG_INFO("Skip fbin="
84 << ihist
85 << " fx=" << m_HistoBorders.GetBinLowEdge(ihist)
86 << " frac=" << m_HistoContents.get_fraction(ihist));
87 --ihist;
88 if (doprint)
89 ATH_MSG_INFO(" fbin="
90 << ihist
91 << " fx=" << m_HistoBorders.GetBinLowEdge(ihist)
92 << " frac=" << m_HistoContents.get_fraction(ihist));
93 }
94
95 if (doprint)
96 ATH_MSG_INFO("bin=" << ibin + 1 << " fbin=" << ihist << "/"
97 << m_HistoBorders.get_nbins() << " x=["
98 << hist->GetXaxis()->GetBinLowEdge(ibin + 1)
99 << "," << hist->GetXaxis()->GetBinUpEdge(ibin + 1)
100 << "] fx=" << m_HistoBorders.GetBinLowEdge(ihist)
101 << " int=" << temp_HistoContents[ibin] / integral
102 << " frac="
103 << m_HistoContents.get_fraction(ihist));
104
105 ++ihist;
106 }
107 if (ihist != nbins) {
108 set_nbins(ihist);
109 m_HistoBorders.SetMinMax(hist->GetXaxis()->GetBinLowEdge(first + 1),
110 hist->GetXaxis()->GetBinUpEdge(last + 1));
111 }
112 }
113 }
114
116
119 virtual double rnd_to_fct(double rnd) const {
120 if (m_HistoContents.get_nbins() == 0)
121 return 0;
122 Trandom residual_rnd;
123 size_t ibin = m_HistoContents.get_bin(rnd, residual_rnd);
124 // ATH_MSG_INFO( fx="<<m_HistoBorders.GetBinLowEdge(ibin)<<"
125 // frac="<<m_HistoContents.get_fraction(ibin)<<"
126 // residual_rnd="<<residual_rnd);
127 return m_HistoBorders.position(ibin, residual_rnd);
128 }
129
130 inline const Txvec &get_HistoBordersx() const { return m_HistoBorders; };
131 inline Txvec &get_HistoBordersx() { return m_HistoBorders; };
132
135 return m_HistoContents;
136 };
140
141protected:
144
145private:
147 1) // TFCS1DFunctionTemplateHistogram
148};
149
152 TFCS1DFunction_HistogramInt8BinEdges, uint8_t, float> {
153public:
157
159 1) // TFCS1DFunctionInt8Int8Histogram
160};
161
164 TFCS1DFunction_HistogramInt8BinEdges, uint16_t, float> {
165public:
169
171 1) // TFCS1DFunctionInt8Int16Histogram
172};
173
176 TFCS1DFunction_HistogramInt8BinEdges, uint32_t, float> {
177public:
181
183 1) // TFCS1DFunctionInt8Int32Histogram
184};
185
188 TFCS1DFunction_HistogramInt16BinEdges, uint16_t, float> {
189public:
193
195 1) // TFCS1DFunctionInt16Int16Histogram
196};
197
200 TFCS1DFunction_HistogramInt16BinEdges, uint32_t, float> {
201public:
205
207 1) // TFCS1DFunctionInt16Int32Histogram
208};
209
212 TFCS1DFunction_HistogramInt32BinEdges, uint32_t, float> {
213public:
217
219 1) // TFCS1DFunctionInt32Int32Histogram
220};
221
222#endif
#define ATH_MSG_INFO(x)
static TRandom * rnd
uint32_t TFCS1DFunction_size_t
Header file for AthHistogramAlgorithm.
void Initialize(const TH1 *hist, bool doprint=true)
Initialize from root histogram.
const TFCS1DFunction_HistogramContent< Ty, Trandom > & get_HistoContents() const
size_t get_nbins() const
return number of bins
std::size_t MemorySize() const
Gives the total memory size, including the size of additional memory allocated inside the class.
TFCS1DFunction_HistogramContent< Ty, Trandom > m_HistoContents
TFCS1DFunction_HistogramContent< Ty, Trandom > & get_HistoContents()
TFCS1DFunctionTemplateHistogram(TH1 *hist=nullptr, bool doprint=true)
virtual double rnd_to_fct(double rnd) const
Function gets random number rnd in the range [0,1) as argument and returns function value according t...
void set_nbins(size_t nbins)
set number of bins
static double CheckAndIntegrate1DHistogram(const TH1 *hist, std::vector< double > &integral_vec, int &first, int &last)
virtual void rnd_to_fct(float value[], const float rnd[]) const
Function gets array of random numbers rnd[] in the range [0,1) as arguments and returns function valu...
double integral(TH1 *h)
Definition computils.cxx:59