Jpp 21.0.0-rc.3
the software that should make you happy
Loading...
Searching...
No Matches
JRootTestkit.hh
Go to the documentation of this file.
1#ifndef __JROOT__JROOTTESTKIT__
2#define __JROOT__JROOTTESTKIT__
3
4#include <cmath>
5#include <cstddef>
6#include <vector>
7#include <algorithm>
8
9#pragma GCC diagnostic push
10#pragma GCC diagnostic ignored "-Wall"
11#include "TH1.h"
12#include "TH2.h"
13#include "TH3.h"
14#include "TRandom3.h"
15#pragma GCC diagnostic pop
16
17
18/**
19 * \author mdejong
20 */
21
22namespace JROOT {}
23namespace JPP { using namespace JROOT; }
24
25namespace JROOT {
26
27 /**
28 * Fill 1D histogram according Poisson statistics with expectation values from given 1D function.
29 *
30 * \param h1 histogram
31 * \param f1 function
32 */
33 template<class T>
34 inline void FillRandom(TH1* h1, const T& f1)
35 {
36 for (Int_t ix = 1; ix <= h1->GetXaxis()->GetNbins(); ++ix){
37
38 const double x = h1->GetXaxis()->GetBinCenter(ix);
39
40 h1->SetBinContent(ix, gRandom->Poisson(f1(x)));
41
42 if (h1->GetSumw2N() != 0) {
43 h1->SetBinError(ix, sqrt(h1->GetBinContent(ix)));
44 }
45 }
46 }
47
48
49 /**
50 * Fill 2D histogram according Poisson statistics with expectation values from given 2D function.
51 *
52 * \param h2 histogram
53 * \param f2 function
54 */
55 template<class T>
56 inline void FillRandom(TH2* h2, const T& f2)
57 {
58 for (Int_t ix = 1; ix <= h2->GetXaxis()->GetNbins(); ++ix){
59 for (Int_t iy = 1; iy <= h2->GetYaxis()->GetNbins(); ++iy){
60
61 const double x = h2->GetXaxis()->GetBinCenter(ix);
62 const double y = h2->GetYaxis()->GetBinCenter(iy);
63
64 h2->SetBinContent(ix, iy, gRandom->Poisson(f2(x,y)));
65
66 if (h2->GetSumw2N() != 0) {
67 h2->SetBinError(ix, iy, sqrt(h2->GetBinContent(ix,iy)));
68 }
69 }
70 }
71 }
72
73
74 /**
75 * Fill 3D histogram according Poisson statistics with expectation values from given 3D function.
76 *
77 * \param h3 histogram
78 * \param f3 function
79 */
80 template<class T>
81 inline void FillRandom(TH3* h3, const T& f3)
82 {
83 for (Int_t ix = 1; ix <= h3->GetXaxis()->GetNbins(); ++ix){
84 for (Int_t iy = 1; iy <= h3->GetYaxis()->GetNbins(); ++iy){
85 for (Int_t iz = 1; iz <= h3->GetZaxis()->GetNbins(); ++iz){
86
87 const double x = h3->GetXaxis()->GetBinCenter(ix);
88 const double y = h3->GetYaxis()->GetBinCenter(iy);
89 const double z = h3->GetZaxis()->GetBinCenter(iz);
90
91 h3->SetBinContent(ix, iy, iz, gRandom->Poisson(f3(x,y,z)));
92
93 if (h3->GetSumw2N() != 0) {
94 h3->SetBinError(ix, iy, iy, sqrt(h3->GetBinContent(ix,iy,iz)));
95 }
96 }
97 }
98 }
99 }
100
101
102 /**
103 * Fill 1D histogram according PDF as given 1D function.
104 *
105 * \param h1 histogram
106 * \param f1 function
107 * \param ns number of entries
108 */
109 template<class T>
110 inline void FillRandom(TH1* h1, const T& f1, const size_t ns)
111 {
112 using namespace std;
113
114 struct tuple_t {
115
116 Double_t x;
117 double W;
118
119 inline bool operator<(const double W) const { return this->W < W; }
120 };
121
122 vector<tuple_t> buffer;
123
124 double W = 0.0;
125
126 for (Int_t ix = 1; ix <= h1->GetXaxis()->GetNbins(); ++ix){
127
128 const Double_t x = h1->GetXaxis()->GetBinCenter(ix);
129
130 W += f1(x);
131
132 buffer.push_back({x, W});
133 }
134
135 for (size_t i = 0; i != ns; ++i) {
136
137 typename vector<tuple_t>::const_iterator p = lower_bound(buffer.begin(), buffer.end(), W * gRandom->Rndm());
138
139 h1->Fill(p->x);
140 }
141 }
142
143
144 /**
145 * Fill 2D histogram according PDF as given 2D function.
146 *
147 * \param h2 histogram
148 * \param f2 function
149 * \param ns number of entries
150 */
151 template<class T>
152 inline void FillRandom(TH2* h2, const T& f2, const size_t ns)
153 {
154 using namespace std;
155
156 struct tuple_t {
157
158 Double_t x;
159 Double_t y;
160 double W;
161
162 inline bool operator<(const double W) const { return this->W < W; }
163 };
164
165 vector<tuple_t> buffer;
166
167 double W = 0.0;
168
169 for (Int_t ix = 1; ix <= h2->GetXaxis()->GetNbins(); ++ix){
170 for (Int_t iy = 1; iy <= h2->GetYaxis()->GetNbins(); ++iy){
171
172 const Double_t x = h2->GetXaxis()->GetBinCenter(ix);
173 const Double_t y = h2->GetYaxis()->GetBinCenter(iy);
174
175 W += f2(x,y);
176
177 buffer.push_back({x, y, W});
178 }
179 }
180
181 for (size_t i = 0; i != ns; ++i) {
182
183 typename vector<tuple_t>::const_iterator p = lower_bound(buffer.begin(), buffer.end(), W * gRandom->Rndm());
184
185 h2->Fill(p->x, p->y);
186 }
187 }
188
189
190 /**
191 * Fill 3D histogram according PDF as given 3D function.
192 *
193 * \param h3 histogram
194 * \param f3 function
195 * \param ns number of entries
196 */
197 template<class T>
198 inline void FillRandom(TH3* h3, const T& f3, const size_t ns)
199 {
200 using namespace std;
201
202 struct tuple_t {
203
204 Double_t x;
205 Double_t y;
206 Double_t z;
207 double W;
208
209 inline bool operator<(const double W) const { return this->W < W; }
210 };
211
212 vector<tuple_t> buffer;
213
214 double W = 0.0;
215
216 for (Int_t ix = 1; ix <= h3->GetXaxis()->GetNbins(); ++ix){
217 for (Int_t iy = 1; iy <= h3->GetYaxis()->GetNbins(); ++iy){
218 for (Int_t iz = 1; iz <= h3->GetYaxis()->GetNbins(); ++iz){
219
220 const Double_t x = h3->GetXaxis()->GetBinCenter(ix);
221 const Double_t y = h3->GetYaxis()->GetBinCenter(iy);
222 const Double_t z = h3->GetZaxis()->GetBinCenter(iz);
223
224 W += f3(x,y,z);
225
226 buffer.push_back({x, y, z, W});
227 }
228 }
229 }
230
231 for (size_t i = 0; i != ns; ++i) {
232
233 typename vector<tuple_t>::const_iterator p = lower_bound(buffer.begin(), buffer.end(), W * gRandom->Rndm());
234
235 h3->Fill(p->x, p->y, p->z);
236 }
237 }
238}
239
240#endif
241
bool operator<(const Status_Item &first, const Status_Item &second)
comparator for Status item; earliest hit first
Definition Ars.hh:413
double f3(const double x, const double y, const double z)
3D function.
This name space includes all other name spaces (except KM3NETDAQ, KM3NET and ANTARES).
Auxiliary classes and methods for ROOT I/O.
void FillRandom(TH1 *h1, const T &f1)
Fill 1D histogram according Poisson statistics with expectation values from given 1D function.