BmnRoot 26.10.0-210-gc52d34b0
Software framework of the BM@N experiment
Loading...
Searching...
No Matches
BmnWeightedLine.h
Go to the documentation of this file.
1/*
2 BM@N alignment routine
3 BM@N experiment at NICA complex, JINR, 2026
4
5 Department: Math & Soft Group of HEP lab
6 Author: Claude (Opus 5)
7 Supervisor: Igor Polev, polev@jinr.int
8
9 BmnWeightedLine header-only implementation
10 Weighted least squares straight line in one projection
11*/
12
13#ifndef BMNWEIGHTEDLINE_H
14#define BMNWEIGHTEDLINE_H
15
16#include <RtypesCore.h>
17#include <TMath.h>
18#include <algorithm>
19#include <cmath>
20
21// v = At0() + Slope() * z. Raw sums lose digits when the points lie far from z = 0 compared with
22// their z spread; for the BM@N detectors they keep 12 digits or more.
23// A point of zero weight adds nothing, but still counts in n.
25{
26 private:
27 Double_t s{0.0}, sz{0.0}, szz{0.0}, sv{0.0}, szv{0.0}, svv{0.0};
28 Int_t n{0};
29 // det value is heavily reused -- it needs to be cached
31
32 public:
33 void Add(Double_t z, Double_t v, Double_t w = 1.0) noexcept
34 {
35 s += w;
36 sz += w * z;
37 szz += w * z * z;
38 sv += w * v;
39 szv += w * z * v;
40 svv += w * v * v;
41 n++;
43 }
44 // the inverse of Add(): the line through the other points
45 void Remove(Double_t z, Double_t v, Double_t w = 1.0) noexcept
46 {
47 s -= w;
48 sz -= w * z;
49 szz -= w * z * z;
50 sv -= w * v;
51 szv -= w * z * v;
52 svv -= w * v * v;
53 n--;
55 }
56 Int_t N() const noexcept { return n; }
57 Double_t Mean() const noexcept { return sv / s; }
58 Double_t At(Double_t z) const noexcept { return At0() + Slope() * z; }
59 Double_t At0() const noexcept { return (szz * sv - sz * szv) / Det(); }
60 Double_t Slope() const noexcept { return (s * szv - sz * sv) / Det(); }
61 Double_t Chi2() const noexcept { return svv - At0() * sv - Slope() * szv; }
62 Double_t Rms() const noexcept { return std::sqrt(std::max(svv / s - Mean() * Mean(), 0.0)); }
63 // Var(At0) + 2 z Cov(At0, Slope) + z^2 Var(Slope), from the weights alone
64 Double_t Var(Double_t z) const noexcept { return (szz - 2.0 * z * sz + z * z * s) / Det(); }
65 Double_t SlopeVar() const noexcept { return s / Det(); }
66 Double_t Cov() const noexcept { return -sz / Det(); } // Cov(At0, Slope)
67 // cached functions used in hot code or heavily reused
68 Double_t Det() const noexcept
69 {
70 if (TMath::IsNaN(det))
71 det = s * szz - sz * sz;
72 return det;
73 }
74};
75
76#endif // BMNWEIGHTEDLINE_H
__m128 v
Definition P4_F32vec4.h:1
int Int_t
double Double_t
Double_t Det() const noexcept
Double_t At0() const noexcept
Double_t Chi2() const noexcept
Double_t Cov() const noexcept
Double_t Var(Double_t z) const noexcept
Int_t N() const noexcept
Double_t At(Double_t z) const noexcept
Double_t Mean() const noexcept
Double_t Rms() const noexcept
void Add(Double_t z, Double_t v, Double_t w=1.0) noexcept
Double_t Slope() const noexcept
Double_t SlopeVar() const noexcept
void Remove(Double_t z, Double_t v, Double_t w=1.0) noexcept
Bool_t IsNaN(Double_t x)
Double_t QuietNaN()