GamR  0.0.0
GammaROOT
Loading...
Searching...
No Matches
Transform.cc
Go to the documentation of this file.
1/* ROOT */
2#include <TMath.h>
3#include <TRandom3.h>
4
5#include <utils/Utilities.hh>
6#include <toolkit/Gate.hh>
7#include "Transform.hh"
8#include "Display.hh"
9
10namespace GamR {
11 namespace Spect {
12
21 TH1D Scale(const TH1 *h, TFormula f)
22 {
23 TRandom3 dither(0430204771);
24 TH1D newhist;
25 newhist.SetBins(h->GetNbinsX(), f.Eval(h->GetXaxis()->GetXmin()), f.Eval(h->GetXaxis()->GetXmax()));
26 for (int i = 0; i < h->GetNbinsX(); ++i) {
27 for (int c = 0; c < h->GetBinContent(i); ++c) {
28 auto x = h->GetXaxis()->GetBinLowEdge(i) + dither.Rndm() * h->GetXaxis()->GetBinWidth(i);
29 newhist.Fill(f.Eval(x));
30 }
31 }
32 return newhist;
33 }
34
45 TH1D ScalePoly(const TH1 *h, std::vector<Double_t> pars)
46 {
47 std::string funcstr = "pol" + std::to_string(pars.size() - 1);
48 TFormula f("func", funcstr.c_str());
49 for (size_t n = 0; n < pars.size(); ++n) {
50 f.SetParameter(n, pars[n]);
51 }
52 f.Print();
53 return Scale(h, f);
54 }
55
64 void ScaleLabelsLinear(TH1 *h, Double_t m, Double_t c)
65 {
66 auto y = [&](Double_t x) { return m * x + c; };
67 auto xlo = h->GetXaxis()->GetXmin();
68 auto xhi = h->GetXaxis()->GetXmax();
69 h->GetXaxis()->SetLimits(y(xlo), y(xhi));
70 return;
71 }
72
80 void Shift(TH1 &h, Int_t shift)
81 {
82 if (shift == 0) {
83 return;
84 } else if (shift > 0) // go from high to low
85 {
86 for (int i = h.GetNbinsX(); i > shift; --i) {
87 h.SetBinContent(i, h.GetBinContent(i - shift));
88 h.SetBinError(i, h.GetBinError(i - shift));
89 }
90 for (int i = shift; i >= 0; --i) {
91 h.SetBinContent(i, 0);
92 h.SetBinError(i, 0);
93 }
94 } else if (shift < 0) // go from low to high
95 {
96 for (int i = 0; i < h.GetNbinsX() + shift; ++i) {
97 h.SetBinContent(i, h.GetBinContent(i - shift));
98 h.SetBinError(i, h.GetBinError(i - shift));
99 }
100 for (int i = h.GetNbinsX() + shift; i < h.GetNbinsX(); ++i) {
101 h.SetBinContent(i, 0);
102 h.SetBinError(i, 0);
103 }
104 }
105 return;
106 }
107
121 TH1D *Add(TH1 *hist1, TH1 *hist2, const char *name, double c1 /*=1.0*/, double c2 /*=1.0*/)
122 {
123 TH1D *out = (TH1D *)hist1->Clone(name);
124 out->Add(hist1, hist2, c1, c2);
125 for (int i = 0; i < hist1->GetNbinsX(); ++i) {
126 out->SetBinError(i, sqrt(pow(c1 * hist1->GetBinError(i), 2) + pow(c2 * hist2->GetBinError(i), 2)));
127 }
128 return out;
129 }
130
131 TH1D *Add(std::vector<TH1*> hists, const char *name) {
132 if (hists.size() == 0) { return NULL; }
133 TH1D *out = (TH1D *)hists[0]->Clone(name);
134 for (int i=1; i<hists.size(); ++i) {
135 out->Add(hists[i], 1);
136 }
137 return out;
138 }
139
140 TH1D *Add(std::vector<int> hists, const char *name) {
141 auto spectra = List1DSpectra(true);
142 std::vector<TH1*> sum_spectra;
143 for (auto &i : hists) {
144 sum_spectra.push_back(spectra[i]);
145 }
146 return Add(sum_spectra, name);
147 }
148
149 TH1D *Add(int iStart, int iStop, const char *name) {
150 auto spectra = List1DSpectra(true);
151 std::vector<TH1*> sum_spectra;
152 for (int i=iStart; i<=iStop; ++i) {
153 sum_spectra.push_back(spectra[i]);
154 }
155 return Add(sum_spectra, name);
156 }
157
171 TH2D *Add(TH2 *hist1, TH2 *hist2, const char *name, double c1 /*=1.0*/, double c2 /*=1.0*/)
172 {
173 TH2D *out = (TH2D *)hist1->Clone(name);
174 out->Add(hist1, hist2, c1, c2);
175 for (int i = 0; i < hist1->GetNbinsX(); ++i) {
176 for (int j = 0; j < hist1->GetNbinsY(); ++j) {
177 out->SetBinError(i, j, sqrt(pow(c1 * hist1->GetBinError(i, j), 2) + pow(c2 * hist2->GetBinError(i, j), 2)));
178 }
179 }
180 return out;
181 } /*Add*/
182
183 TH2D *Add(std::vector<TH2*> hists, const char *name) {
184 if (hists.size() == 0) { return NULL; }
185 TH2D *out = (TH2D*)hists[0]->Clone(name);
186 for (int i=1; i<hists.size(); ++i) {
187 out->Add(hists[i], 1);
188 }
189 return out;
190 }
191
192 TH2D *Add2(std::vector<int> hists, const char *name) {
193 auto spectra = List2DSpectra(true);
194 std::vector<TH2*> sum_spectra;
195 for (auto &i : hists) {
196 sum_spectra.push_back(spectra[i]);
197 }
198 return Add(sum_spectra, name);
199 }
200
201 TH2D *Add2(int iStart, int iStop, const char *name) {
202 auto spectra = List2DSpectra(true);
203 std::vector<TH2*> sum_spectra;
204 for (int i=iStart; i<=iStop; ++i) {
205 sum_spectra.push_back(spectra[i]);
206 }
207 return Add(sum_spectra, name);
208 }
209
220 TH1D *Multiply(TH1 *hist1, const char *name, double c1 /*= 1.0*/) {
221 TH1D *out = (TH1D*)hist1->Clone(name);
222 out->Scale(c1);
223 for (int i=0; i < hist1->GetNbinsX(); ++i) {
224 out->SetBinError(i, c1*hist1->GetBinError(i));
225 }
226 return out;
227 }
228
236 void Reverse(TH1 *h)
237 {
238 TH1D *clone = (TH1D *)h->Clone();
239 for (int i = 0; i < h->GetNbinsX(); ++i) {
240 h->SetBinContent(i, clone->GetBinContent(h->GetNbinsX() - i));
241 h->SetBinError(i, clone->GetBinError(h->GetNbinsX() - i));
242 }
243 return;
244 }
245
254
255 TH2D *RotateTH2(TH2 *hist, float angle, const char *name, TCutG *bananaGate /*=0*/)
256 {
257 TRandom3 random;
258
259 int iMax = hist->GetNbinsX();
260 int jMax = hist->GetNbinsY();
261
262 float xMin = ((TAxis *)hist->GetXaxis())->GetXmin();
263 float xMax = ((TAxis *)hist->GetXaxis())->GetXmax();
264 float yMin = ((TAxis *)hist->GetYaxis())->GetXmin();
265 float yMax = ((TAxis *)hist->GetYaxis())->GetXmax();
266
267 float cosangle = TMath::Cos(angle * TMath::Pi() / 180.);
268 float sinangle = TMath::Sin(angle * TMath::Pi() / 180.);
269
270 int nOutX = iMax * cosangle + jMax * sinangle;
271 int nOutY = iMax * sinangle + jMax * cosangle;
272
273 TH2D *out_h = new TH2D(name, name, nOutX, xMin * cosangle - yMax * sinangle, xMax * cosangle - yMin * sinangle,
274 nOutY, xMin * sinangle + yMin * cosangle, xMax * sinangle + yMax * cosangle);
275 // TH1D *out_h = new TH1D(name, name, iMax, xMin*cosangle - yMax*sinangle,
276 // xMax*cosangle - yMin*sinangle);
277 float binWidthX = ((TAxis *)hist->GetXaxis())->GetBinCenter(1) - ((TAxis *)hist->GetXaxis())->GetBinCenter(0);
278 float binWidthY = ((TAxis *)hist->GetYaxis())->GetBinCenter(1) - ((TAxis *)hist->GetYaxis())->GetBinCenter(0);
279
280 for (int i = 0; i < iMax; i++) {
281 for (int j = 0; j < jMax; j++) {
282 float x = ((TAxis *)hist->GetXaxis())->GetBinCenter(i);
283 float y = ((TAxis *)hist->GetYaxis())->GetBinCenter(j);
284
285 if (bananaGate) {
286 if (!bananaGate->IsInside(x, y)) {
287 continue;
288 }
289 }
290 /* do rotation + projection */
291
292 for (int w = 1; w <= hist->GetBinContent(i, j); w++) {
293 float xRan = x + (random.Rndm() - 0.5) * binWidthX;
294 float yRan = y + (random.Rndm() - 0.5) * binWidthY;
295 float projX = xRan * cosangle - yRan * sinangle;
296 float projY = xRan * sinangle + yRan * cosangle;
297
298 out_h->Fill(projX, projY);
299 }
300 }
301 }
302
303 return out_h;
304 }
305
306 TH1 *Rebin(TH1 *hist, int rebin) {
307 hist->Rebin(rebin);
308 return hist;
309 }
310
311 TH2 *RebinX(TH2 *hist, int rebin) {
312 hist->RebinX(rebin);
313 return hist;
314 }
315
316 TH2 *RebinY(TH2 *hist, int rebin) {
317 hist->RebinY(rebin);
318 return hist;
319 }
320
321 void RebinX(TVirtualPad *canvas, int rebin) {
322 if (!canvas) { if (!gPad) { return; } canvas = gPad; }
323
324 int nPads = GamR::Utils::GetNPads(canvas);
325
326 if (!nPads) {
327 TH2D* hist = GamR::Utils::GetHist2D(canvas);
328 GamR::Spect::RebinX(hist, rebin);
329 canvas->Modified();
330 }
331
332 for (int i=0; i<nPads; ++i) {
333 canvas->cd(i+1);
334 RebinX(canvas->cd(i+1), rebin);
335 }
336 canvas->Modified();
337 canvas->cd();
338 }
339
340 void RebinY(TVirtualPad *canvas, int rebin) {
341 if (!canvas) { if (!gPad) { return; } canvas = gPad; }
342
343 int nPads = GamR::Utils::GetNPads(canvas);
344
345 if (!nPads) {
346 TH2D* hist = GamR::Utils::GetHist2D(canvas);
347 GamR::Spect::RebinY(hist, rebin);
348 canvas->Modified();
349 }
350
351 for (int i=0; i<nPads; ++i) {
352 canvas->cd(i+1);
353 RebinY(canvas->cd(i+1), rebin);
354 }
355 canvas->Modified();
356 canvas->cd();
357 }
358
359 void Rebin(TVirtualPad *canvas, int rebin) {
360 if (!canvas) { if (!gPad) { return; } canvas = gPad; }
361
362 int nPads = GamR::Utils::GetNPads(canvas);
363
364 if (!nPads) {
365 std::vector<TH1D*> hists = GamR::Utils::GetHists1D(canvas);
366 if (hists.size() > 0) {
367 for (int i=0; i<hists.size(); ++i) {
368 GamR::Spect::Rebin(hists[i], rebin);
369 }
370 canvas->Modified();
371 }
372 }
373
374 for (int i=0; i<nPads; ++i) {
375 canvas->cd(i+1);
376 Rebin(canvas->cd(i+1), rebin);
377 }
378 canvas->Modified();
379 canvas->cd();
380 }
381
382 void Norm(TH1 *hist, TH1 *ref, double low, double high) {
383 GamR::TK::Gate gate(low, high);
384 gate.Norm(hist, ref);
385 }
386
387 void NormBackSub(TH1 *hist, TH1 *ref, double low, double high, double backlow, double backhigh) {
388 GamR::TK::Gate gate(low, high);
389 GamR::TK::Gate back(backlow, backhigh);
390 gate.NormBackSub(hist, ref, back);
391 }
392 } // namespace Spect
393} // namespace GamR
void NormBackSub(TH1 *hist, TH1 *ref, Gate background)
Definition Gate.cc:691
void Norm(TH1 *hist, TH1 *ref)
Definition Gate.cc:686
std::vector< TH2 * > List2DSpectra(bool quiet)
Definition Display.cc:53
void ScaleLabelsLinear(TH1 *h, Double_t m, Double_t c)
Scales the labels of a histogram. Preferable over GamR::Spect::Scale and GamR::Spect::ScalePoly as it...
Definition Transform.cc:64
void Shift(TH1 &h, Int_t shift)
Definition Transform.cc:80
TH2 * RebinY(TH2 *hist, int rebin)
Definition Transform.cc:316
TH1 * Rebin(TH1 *hist, int rebin)
Definition Transform.cc:306
TH2D * Add2(std::vector< int > hists, const char *name)
Definition Transform.cc:192
TH1D * Multiply(TH1 *hist1, const char *name, double c1)
Definition Transform.cc:220
std::vector< TH1 * > List1DSpectra(bool quiet)
Definition Display.cc:16
TH1D Scale(const TH1 *h, TFormula f)
Scales a histogram along the X axis and dithers to avoid quantization errors.
Definition Transform.cc:21
void NormBackSub(TH1 *hist, TH1 *ref, double low, double high, double backlow, double backhigh)
Definition Transform.cc:387
void Reverse(TH1 *h)
Definition Transform.cc:236
TH2D * RotateTH2(TH2 *hist, float angle, const char *name, TCutG *bananaGate)
Definition Transform.cc:255
TH1D ScalePoly(const TH1 *h, std::vector< Double_t > pars)
Scales histogram by a polynomial.
Definition Transform.cc:45
TH2 * RebinX(TH2 *hist, int rebin)
Definition Transform.cc:311
TH1D * Add(TH1 *hist1, TH1 *hist2, const char *name, double c1, double c2)
Definition Transform.cc:121
void Norm(TH1 *hist, TH1 *ref, double low, double high)
Definition Transform.cc:382
int GetNPads(TVirtualPad *pad)
Definition Utilities.cc:411
std::vector< TH1D * > GetHists1D(TVirtualPad *canvas)
Definition Utilities.cc:204
TH2D * GetHist2D(TVirtualPad *canvas)
Definition Utilities.cc:221
Definition Gain.cc:19