Heavy ion Analysis Libriares
Loading...
Searching...
No Matches
FastHist.cxx
1/*
2 * FastHist.cxx
3 *
4 * Created on: 6 cze 2022
5 * Author: Daniel Wielanek
6 * E-mail: daniel.wielanek@gmail.com
7 * Warsaw University of Technology, Faculty of Physics
8 */
9#include "FastHist.h"
10#include <iostream>
11
12namespace Hal {
13 FastHist::FastHist(TString name, TString title) :
14 fAxisNameX(""),
15 fAxisNameY(""),
16 fAxisNameZ(""),
17 fTitle(title),
18 fBinsNoX(0),
19 fBinsNoY(0),
20 fBinsNoZ(0),
21 fTotalBinsNo(0),
22 fValues(nullptr) {
23 SetName(name);
24 }
25
26 FastHist::~FastHist() {
27 if (fValues) delete[] fValues;
28 }
29
30 FastHist1D::FastHist1D(TString name, TString title, Int_t nbins, Double_t min, Double_t max) : FastHist(name, title) {
31 fBinsNoX = nbins + 2;
32 if (nbins != 0) {
33 fValues = new Double_t[fBinsNoX];
34 fMinX = min;
35 fMaxX = max;
36 fStepX = (fMaxX - fMinX) / nbins;
37 fStepX = 1.0 / fStepX;
38 }
39 for (int i = 0; i < fTotalBinsNo; i++)
40 fValues[i] = 0;
41 fTotalBinsNo = fBinsNoX;
42 }
43
44 Int_t FastHist1D::FindBin(Double_t x) const {
45 Int_t bin = (x - fMinX) * fStepX + 1;
46 if (bin < 0) {
47 bin = 0;
48 } else if (bin > fBinsNoX - 1) {
49 bin = fBinsNoX - 1;
50 }
51 return bin;
52 }
53
54 void FastHist1D::Fill(Double_t x, Double_t w) {
55 Int_t bin = (x - fMinX) * fStepX + 1;
56 if (bin < 0) {
57 bin = 0;
58 } else if (bin > fBinsNoX - 1) {
59 bin = fBinsNoX - 1;
60 }
61 fValues[bin] += w;
62 }
63
64 TH1* FastHist1D::GetTH1() const {
65 TH1D* h = new TH1D(GetName(), GetTitle(), fBinsNoX - 2, fMinX, fMaxX);
66 if (fAxisNameX.Length() > 0) h->GetXaxis()->SetTitle(fAxisNameX);
67 if (fAxisNameY.Length() > 0) h->GetYaxis()->SetTitle(fAxisNameY);
68 for (int i = 0; i < fBinsNoX; i++) {
69 h->SetBinContent(i, fValues[i]);
70 }
71 return h;
72 }
73
74 FastHist2D::FastHist2D(TString name,
75 TString title,
76 Int_t nbinsX,
77 Double_t xmin,
78 Double_t xmax,
79 Int_t nbinsY,
80 Double_t ymin,
81 Double_t ymax) :
82 FastHist(name, title) {
83 fBinsNoX = nbinsX + 2;
84 fBinsNoY = nbinsY + 2;
85 fMinX = xmin;
86 fMinY = ymin;
87 fMaxX = xmax;
88 fMaxY = ymax;
89 fTotalBinsNo = fBinsNoX * fBinsNoY;
90 fValues = new Double_t[fTotalBinsNo];
91 fStepX = (fMaxX - fMinX) / (Double_t) nbinsX;
92 fStepY = (fMaxY - fMinY) / (Double_t) nbinsY;
93 fStepX = 1.0 / fStepX;
94 fStepY = 1.0 / fStepY;
95 for (int i = 0; i < fTotalBinsNo; i++)
96 fValues[i] = 0;
97 }
98
99 Int_t FastHist2D::FindBin(Double_t x, Double_t y) const {
100 Int_t binX = (x - fMinX) * fStepX + 1;
101 Int_t binY = (y - fMinY) * fStepY + 1;
102
103 if (binX < 0) {
104 binX = 0;
105 } else if (binX + 1 > fBinsNoX) {
106 binX = fBinsNoX - 1;
107 }
108 if (binY < 0) {
109 binY = 0;
110 } else if (binY + 1 > fBinsNoY) {
111 binY = fBinsNoY - 1;
112 }
113 return binX * fBinsNoY + binY;
114 }
115
116 void FastHist2D::Fill(Double_t x, Double_t y, Double_t w) {
117 Int_t binX = (x - fMinX) * fStepX + 1;
118 Int_t binY = (y - fMinY) * fStepY + 1;
119
120 if (binX < 0) {
121 binX = 0;
122 } else if (binX + 1 > fBinsNoX) {
123 binX = fBinsNoX - 1;
124 }
125 if (binY < 0) {
126 binY = 0;
127 } else if (binY + 1 > fBinsNoY) {
128 binY = fBinsNoY - 1;
129 }
130 fValues[binX * fBinsNoY + binY] += w;
131 }
132
133 Double_t FastHist2D::GetBinContent(Int_t i, Int_t j) const { return fValues[i * fBinsNoY + j]; }
134
135 TH1* FastHist2D::GetTH1() const {
136 TH2D* h = new TH2D(GetName(), GetTitle(), fBinsNoX - 2, fMinX, fMaxX, fBinsNoY - 2, fMinY, fMaxY);
137 if (fAxisNameX.Length() > 0) h->GetXaxis()->SetTitle(fAxisNameX);
138 if (fAxisNameY.Length() > 0) h->GetYaxis()->SetTitle(fAxisNameY);
139 if (fAxisNameZ.Length() > 0) h->GetZaxis()->SetTitle(fAxisNameZ);
140 for (int i = 0; i < fBinsNoX; i++) {
141 for (int j = 0; j < fBinsNoY; j++) {
142 h->SetBinContent(i, j, fValues[i * fBinsNoY + j]);
143 }
144 }
145 return h;
146 }
147
148 FastHist3D::FastHist3D(TString name,
149 TString title,
150 Int_t nbinsX,
151 Double_t xmin,
152 Double_t xmax,
153 Int_t nbinsY,
154 Double_t ymin,
155 Double_t ymax,
156 Int_t nbinsZ,
157 Double_t zmin,
158 Double_t zmax) :
159 FastHist(name, title) {
160 fBinsNoX = nbinsX + 2;
161 fBinsNoY = nbinsY + 2;
162 fBinsNoZ = nbinsZ + 2;
163 fMinX = xmin;
164 fMinY = ymin;
165 fMinZ = zmin;
166 fMaxX = xmax;
167 fMaxY = ymax;
168 fMaxZ = zmax;
169 fTotalBinsNo = fBinsNoX * fBinsNoY * fBinsNoZ;
170 fValues = new Double_t[fTotalBinsNo];
171 fStepX = (fMaxX - fMinX) / nbinsX;
172 fStepY = (fMaxY - fMinY) / nbinsY;
173 fStepZ = (fMaxZ - fMinZ) / nbinsZ;
174 fStepX = 1.0 / fStepX;
175 fStepY = 1.0 / fStepY;
176 fStepZ = 1.0 / fStepZ;
177 fBinsYZ = fBinsNoY * fBinsNoZ;
178 for (int i = 0; i < fTotalBinsNo; i++)
179 fValues[i] = 0;
180 }
181
182 Int_t FastHist3D::FindBin(Double_t x, Double_t y, Double_t z) const {
183 Int_t binX = (x - fMinX) * fStepX + 1;
184 Int_t binY = (y - fMinY) * fStepY + 1;
185 Int_t binZ = (z - fMinZ) * fStepZ + 1;
186 if (binX < 0) {
187 binX = 0;
188 } else if (binX + 1 > fBinsNoX) {
189 binX = fBinsNoX - 1;
190 }
191 if (binY < 0) {
192 binY = 0;
193 } else if (binY + 1 > fBinsNoY) {
194 binY = fBinsNoY - 1;
195 }
196 if (binZ < 0) {
197 binZ = 0;
198 } else if (binZ + 1 > fBinsNoZ) {
199 binZ = fBinsNoZ - 1;
200 }
201 return binX * fBinsYZ + binY * fBinsNoZ + binZ;
202 }
203
204
205 void FastHist3D::Fill(Double_t x, Double_t y, Double_t z, Double_t w) {
206 Int_t binX = (x - fMinX) * fStepX + 1;
207 Int_t binY = (y - fMinY) * fStepY + 1;
208 Int_t binZ = (z - fMinZ) * fStepZ + 1;
209 if (binX < 0) {
210 binX = 0;
211 } else if (binX + 1 > fBinsNoX) {
212 binX = fBinsNoX - 1;
213 }
214 if (binY < 0) {
215 binY = 0;
216 } else if (binY + 1 > fBinsNoY) {
217 binY = fBinsNoY - 1;
218 }
219 if (binZ < 0) {
220 binZ = 0;
221 } else if (binZ + 1 > fBinsNoZ) {
222 binZ = fBinsNoZ - 1;
223 }
224
225 fValues[binX * fBinsYZ + binY * fBinsNoZ + binZ] += w;
226 }
227 Double_t FastHist3D::GetBinContent(Int_t i, Int_t j, Int_t k) const { return fValues[i * fBinsYZ + j * fBinsNoZ + k]; }
228
229 TH1* FastHist3D::GetTH1() const {
230 TH3D* h = new TH3D(GetName(), GetTitle(), fBinsNoX - 2, fMinX, fMaxX, fBinsNoY - 2, fMinY, fMaxY, fBinsNoZ - 2, fMinZ, fMaxZ);
231 if (fAxisNameX.Length() > 0) h->GetXaxis()->SetTitle(fAxisNameX);
232 if (fAxisNameY.Length() > 0) h->GetYaxis()->SetTitle(fAxisNameY);
233 if (fAxisNameZ.Length() > 0) h->GetZaxis()->SetTitle(fAxisNameZ);
234 for (int i = 0; i < fBinsNoX; i++) {
235 for (int j = 0; j < fBinsNoY; j++) {
236 for (int k = 0; k < fBinsNoZ; k++) {
237 h->SetBinContent(i, j, k, fValues[i * fBinsYZ + j * fBinsNoZ + k]);
238 }
239 }
240 }
241 return h;
242 }
243} /* namespace Hal */