ATLAS Offline Software
Loading...
Searching...
No Matches
Muon::Fit2D Class Reference

A 2D linear regression calculator. More...

#include <Fit2D.h>

Collaboration diagram for Muon::Fit2D:

Classes

struct  LinStats
 A structure to hold linear fit statistics. More...
struct  Point
 A 2D point used in statistics and fit. More...
struct  SimpleStats

Public Types

typedef std::vector< Point * > PointArray
 A vector of points.

Public Member Functions

 Fit2D ()
 Constructor.

Static Public Member Functions

static void SimpleStatistics (const PointArray &points, SimpleStats &stats)
 Calculate simple statistics for the Y values of a set of points.
static void fitLine (PointArray &points, double fExclChi2, bool bDump, LinStats &stats)
 Fit a straight line through the given points.
static void fitPoint (PointArray &points, double fExclChi2, bool bDump, SimpleStats &stats)
 Estimate a new point from the given points.

Detailed Description

A 2D linear regression calculator.

The class accepts a list of X/ points and computes the least-squares straight line fit. It reports the intercept, slope and goodness of fit.

Points can be excluded from the calculation, and can also have a weight associated with them.

The class also computes simple statistics (meand, standard deviation) of the Y values.

Definition at line 28 of file Fit2D.h.

Member Typedef Documentation

◆ PointArray

typedef std::vector<Point*> Muon::Fit2D::PointArray

A vector of points.

Definition at line 59 of file Fit2D.h.

Constructor & Destructor Documentation

◆ Fit2D()

Muon::Fit2D::Fit2D ( )
inline

Constructor.

Definition at line 179 of file Fit2D.h.

180 {
181 }

Member Function Documentation

◆ fitLine()

void Muon::Fit2D::fitLine ( Fit2D::PointArray & points,
double fExclChi2,
bool bDump,
LinStats & stats )
static

Fit a straight line through the given points.

Parameters
pointsThe list of data points
fExclChi2CHi2 value for excluding outliers
bDumpWrite details to log
stats[output] The fit results

Definition at line 132 of file Fit2D.cxx.

133{
134 MsgStream log(Athena::getMessageSvc(),"Fit2D::fitLine(");
135 double *x = new double[points.size()];
136 double *y = new double[points.size()];
137 double *w = new double[points.size()];
138 for (;;)
139 {
140 stats.clear();
141 for (unsigned iPt = 0; iPt < points.size(); iPt++)
142 {
143 if (!points[iPt]->bExclude)
144 {
145 x[stats.n] = points[iPt]->fX;
146 y[stats.n] = points[iPt]->fY;
147 w[stats.n] = points[iPt]->fW;
148 stats.n++;
149 }
150 }
151 if (stats.n < 2) goto done;
152
153 gsl_fit_wlinear(x, 1,
154 w, 1,
155 y, 1,
156 stats.n,
157 &stats.fIntercept, &stats.fSlope,
158 &stats.fCov[0][0], &stats.fCov[0][1], &stats.fCov[1][1],
159 &stats.fChi2);
160 stats.fCov[1][0] = stats.fCov[0][1];
161 if (bDump && log.level()<=MSG::DEBUG)
162 log << MSG::DEBUG << stats.toString() << endmsg;
163
164 double fY, fYerr, fMaxChi2 = 0;
165 int i = 0, iPtMax = 0;
166 for (unsigned iPt = 0; iPt < points.size(); iPt++)
167 {
168 if (!points[iPt]->bExclude)
169 {
170 stats.eval(x[i], fY, fYerr);
171 double fDiff = y[i] - fY;
172 points[iPt]->fChi2 = w[i] * fDiff * fDiff;
173 if (fMaxChi2 < points[iPt]->fChi2)
174 {
175 fMaxChi2 = points[iPt]->fChi2;
176 iPtMax = iPt;
177 }
178 if (bDump && log.level()<=MSG::DEBUG)
179 {
180 log << MSG::DEBUG << "idx=" << points[iPt]->nIdx
181 << ", x=" << x[i]
182 << ", y=" << y[i]
183 << ", w=" << w[i]
184 << ", Y=" << fY
185 << ", chi2=" << points[iPt]->fChi2
186 << endmsg;
187 }
188 i++;
189 }
190 }
191 if (fMaxChi2 < fExclChi2 || stats.n <= 2)
192 break;
193 points[iPtMax]->bExclude = true;
194 }
195done:
196 delete [] x;
197 delete [] y;
198 delete [] w;
199}
#define endmsg
#define y
#define x
IMessageSvc * getMessageSvc(bool quiet=false)

◆ fitPoint()

void Muon::Fit2D::fitPoint ( PointArray & points,
double fExclChi2,
bool bDump,
SimpleStats & stats )
static

Estimate a new point from the given points.

Parameters
pointsThe list of data points
fExclChi2CHi2 value for excluding outliers
bDumpWrite details to log
stats[output] The fit results

Definition at line 73 of file Fit2D.cxx.

74{
75 MsgStream log(Athena::getMessageSvc(),"Fit2D::fitPoint(");
76 double *y = new double[points.size()];
77 double *w = new double[points.size()];
78 for (;;)
79 {
80 stats.clear();
81 for (unsigned iPt = 0; iPt < points.size(); iPt++)
82 {
83 // Exclude outliers.
84 if (!points[iPt]->bExclude)
85 {
86 y[stats.n] = points[iPt]->fY;
87 w[stats.n] = points[iPt]->fW;
88 stats.n++;
89 }
90 }
91 // Calculate mean and standard deviation.
92 if (stats.n == 0) goto done;
93 stats.fMean = gsl_stats_wmean(w, 1, y, 1, stats.n);
94 if (stats.n == 1) goto done;
95 stats.fStd = gsl_stats_wsd(w, 1, y, 1, stats.n);
96 double fMaxChi2 = 0;
97 int i = 0, iPtMax = 0;
98 for (unsigned iPt = 0; iPt < points.size(); iPt++)
99 {
100 if (!points[iPt]->bExclude)
101 {
102 double fDiff = y[i] - stats.fMean;
103 points[iPt]->fChi2 = w[i] * fDiff * fDiff;
104 stats.fChi2 += points[iPt]->fChi2;
105 if (fMaxChi2 < points[iPt]->fChi2)
106 {
107 fMaxChi2 = points[iPt]->fChi2;
108 iPtMax = iPt;
109 }
110 if (bDump && log.level()<=MSG::DEBUG)
111 {
112 log << MSG::DEBUG << "idx=" << points[iPt]->nIdx
113 << ", y=" << y[i]
114 << ", w=" << w[i]
115 << ", chi2=" << points[iPt]->fChi2
116 << endmsg;
117 }
118 i++;
119 }
120 }
121 if (bDump && log.level()<=MSG::DEBUG)
122 log << MSG::DEBUG << stats.toString() << endmsg;
123 if (fMaxChi2 < fExclChi2 || stats.n <= 2)
124 break;
125 points[iPtMax]->bExclude = true;
126 }
127done:
128 delete [] y;
129 delete [] w;
130}

◆ SimpleStatistics()

void Muon::Fit2D::SimpleStatistics ( const PointArray & points,
SimpleStats & stats )
static

Calculate simple statistics for the Y values of a set of points.

Parameters
pointsThe list of data points
stats[output] The statistics results

Definition at line 43 of file Fit2D.cxx.

44{
45 stats.clear();
46 double *y = new double[points.size()];
47 double *w = new double[points.size()];
48 for (unsigned iPt = 0; iPt < points.size(); iPt++)
49 {
50 // Exclude outliers.
51 if (!points[iPt]->bExclude)
52 {
53 y[stats.n] = points[iPt]->fY;
54 w[stats.n] = points[iPt]->fW;
55 stats.n++;
56 }
57 }
58 if (stats.n == 0) goto done;
59 // Calculate mean and standard deviation.
60 stats.fMean = gsl_stats_wmean(w, 1, y, 1, stats.n);
61 if (stats.n == 1) goto done;
62 stats.fStd = gsl_stats_wsd (w, 1, y, 1, stats.n);
63 for (int i = 0; i < stats.n; i++)
64 {
65 double fDiff = y[i] - stats.fMean;
66 stats.fChi2 += w[i] * fDiff * fDiff;
67 }
68done:
69 delete [] y;
70 delete [] w;
71}

The documentation for this class was generated from the following files: