MUSIC unknown
Analysis for the MUSIC active-target ionization chamber
Loading...
Searching...
No Matches
BeamFit2D.cpp
Go to the documentation of this file.
1#include "BeamFit2D.hpp"
2
3Bool_t BeamFitUtils::InEllipseXY(const BeamFit2D &b, Double_t x, Double_t y,
4 Double_t nx, Double_t ny) {
5 Double_t dx = x - b.mu_x;
6 Double_t dy = y - b.mu_y;
7 Double_t sx = b.sigma_x, sy = b.sigma_y, rho = b.rho;
8 if (sx <= 0 || sy <= 0)
9 return kFALSE;
10 // Correlated 2D Gaussian ellipse (Mahalanobis distance):
11 // χ² = [(dx/σx)² + (dy/σy)² - 2ρ·dx·dy/(σx·σy)] / (1-ρ²)
12 // Inside n-sigma: χ² < n² (nx serves as the n-sigma level).
13 Double_t dx_s = dx / sx, dy_s = dy / sy;
14 Double_t r2 = rho * rho;
15 Double_t chi2 =
16 (dx_s * dx_s + dy_s * dy_s - 2.0 * rho * dx_s * dy_s) / (1.0 - r2);
17 return chi2 < nx * nx;
18}
19
20Moments2D BeamFitUtils::ComputeMoments(TH2F *h, Int_t lo_bx, Int_t hi_bx,
21 Int_t lo_by, Int_t hi_by,
22 Double_t thresh, Double_t bw_x,
23 Double_t bw_y) {
24 Moments2D m;
25 Double_t W = 0, Mx = 0, My = 0, Cxx = 0, Cyy = 0, Cxy = 0;
26 for (Int_t ix = lo_bx; ix <= hi_bx; ix++) {
27 Double_t x = h->GetXaxis()->GetBinCenter(ix);
28 for (Int_t iy = lo_by; iy <= hi_by; iy++) {
29 Double_t w = h->GetBinContent(ix, iy);
30 if (w < thresh)
31 continue;
32 Double_t y = h->GetYaxis()->GetBinCenter(iy);
33 W += w;
34 Mx += w * x;
35 My += w * y;
36 }
37 }
38 if (W <= 0)
39 return m;
40 Mx /= W;
41 My /= W;
42 for (Int_t ix = lo_bx; ix <= hi_bx; ix++) {
43 Double_t x = h->GetXaxis()->GetBinCenter(ix);
44 for (Int_t iy = lo_by; iy <= hi_by; iy++) {
45 Double_t w = h->GetBinContent(ix, iy);
46 if (w < thresh)
47 continue;
48 Double_t y = h->GetYaxis()->GetBinCenter(iy);
49 Double_t dx = x - Mx, dy = y - My;
50 Cxx += w * dx * dx;
51 Cyy += w * dy * dy;
52 Cxy += w * dx * dy;
53 }
54 }
55 Cxx /= W;
56 Cyy /= W;
57 Cxy /= W;
58 m.mu_x = Mx;
59 m.mu_y = My;
60 m.sigma_x = std::max(std::sqrt(std::max(Cxx, 0.0)), 2.0 * bw_x);
61 m.sigma_y = std::max(std::sqrt(std::max(Cyy, 0.0)), 2.0 * bw_y);
62 Double_t r = Cxy / (m.sigma_x * m.sigma_y);
63 if (r > 0.95)
64 r = 0.95;
65 if (r < -0.95)
66 r = -0.95;
67 m.rho = r;
68 m.weight = W;
69 return m;
70}
static Moments2D ComputeMoments(TH2F *h, Int_t lo_bx, Int_t hi_bx, Int_t lo_by, Int_t hi_by, Double_t thresh, Double_t bw_x, Double_t bw_y)
Second moments of a histogram region above a threshold.
Definition BeamFit2D.cpp:20
static Bool_t InEllipseXY(const BeamFit2D &b, Double_t x, Double_t y, Double_t nx, Double_t ny)
Whether a point lies inside the fitted beam ellipse.
Definition BeamFit2D.cpp:3
A fitted 2-D Gaussian beam spot.
Definition BeamFit2D.hpp:16
Double_t rho
Correlation coefficient, in [-1, 1].
Definition BeamFit2D.hpp:20
Double_t sigma_y
Widths along each axis.
Definition BeamFit2D.hpp:19
Double_t mu_x
Definition BeamFit2D.hpp:18
Double_t mu_y
Centroid.
Definition BeamFit2D.hpp:18
Double_t sigma_x
Definition BeamFit2D.hpp:19
Second moments of a 2-D distribution over a bin range.
Definition BeamFit2D.hpp:30
Double_t rho
Correlation coefficient, in [-1, 1].
Definition BeamFit2D.hpp:33
Double_t sigma_y
Weighted RMS widths.
Definition BeamFit2D.hpp:32
Double_t sigma_x
Definition BeamFit2D.hpp:32
Double_t mu_x
Definition BeamFit2D.hpp:31
Double_t mu_y
Weighted centroid.
Definition BeamFit2D.hpp:31
Double_t weight
Total weight included; zero means the range held nothing above threshold.
Definition BeamFit2D.hpp:34