CbmRoot
CbmRichRingFitterEllipseMinuit.cxx
Go to the documentation of this file.
1 
9 
10 #include "Minuit2/Minuit2Minimizer.h"
11 
12 #include "FairLogger.h"
13 
14 using std::cout;
15 using std::endl;
16 
18 
20 
22  int nofHits = ring->GetNofHits();
23 
24  if (nofHits <= 5) {
25  ring->SetXYABP(-1., -1., -1., -1., -1.);
26  ring->SetRadius(-1.);
27  return;
28  }
29 
30  vector<double> hitX;
31  vector<double> hitY;
32  hitX.clear();
33  hitY.clear();
34  for (int i = 0; i < nofHits; i++) {
35  hitX.push_back(ring->GetHit(i).fX);
36  hitY.push_back(ring->GetHit(i).fY);
37  }
38 
39  vector<double> fpar = DoFit(hitX, hitY);
40 
41  TransformToRichRing(ring, fpar);
42 
43  CalcChi2(ring);
44 }
45 
47  CbmRichRingLight* ring,
48  const vector<double>& fpar) {
49  // calculate center of the ellipse
50  double xc = (fpar[0] + fpar[2]) / 2.;
51  double yc = (fpar[1] + fpar[3]) / 2.;
52  // calculate length of b axes
53  double p1 = (fpar[0] - fpar[2]) * (fpar[0] - fpar[2])
54  + (fpar[1] - fpar[3]) * (fpar[1] - fpar[3]);
55  if (p1 < 0.) {
56  ring->SetXYABP(-1., -1., -1., -1., -1.);
57  ring->SetRadius(-1.);
58  return;
59  }
60 
61  double c = sqrt(p1) / 2.;
62  double p2 = fpar[4] * fpar[4] - c * c;
63  if (p2 < 0.) {
64  ring->SetXYABP(-1., -1., -1., -1., -1.);
65  ring->SetRadius(-1.);
66  return;
67  }
68  double b = sqrt(p2);
69  // calculate angle
70  double p3 = fpar[2] - fpar[0];
71  if (p3 == 0.) {
72  ring->SetXYABP(-1., -1., -1., -1., -1.);
73  ring->SetRadius(-1.);
74  return;
75  }
76  double k = (fpar[3] - fpar[1]) / (fpar[2] - fpar[0]);
77  double ang = atan(k);
78 
79  ring->SetXYABP(xc, yc, fpar[4], b, ang);
80  ring->SetRadius((b + fpar[4]) / 2.);
81 }
82 
83 vector<double> CbmRichRingFitterEllipseMinuit::DoFit(const vector<double>& x,
84  const vector<double>& y) {
85 
86  // Create initial starting values for parameters
87  double xf1 = 0.;
88  double yf1 = 0.;
89  for (unsigned int i = 0; i < x.size(); i++) {
90  xf1 += x[i];
91  yf1 += y[i];
92  }
93  double a = 5.;
94  xf1 = xf1 / x.size() - a;
95  yf1 = yf1 / x.size();
96  double xf2 = xf1 + a;
97  double yf2 = yf1;
98 
99 
100  ROOT::Minuit2::Minuit2Minimizer min;
101 
102 
103  FCNEllipse2* theFCN = new FCNEllipse2(x, y);
104 
105  min.SetMaxFunctionCalls(1000000);
106  min.SetMaxIterations(100000);
107  min.SetTolerance(0.001);
108 
109  min.SetFunction(*theFCN);
110 
111  // Set the free variables to be minimized!
112  min.SetVariable(0, "xf1", xf1, 0.1);
113  min.SetVariable(1, "yf1", yf1, 0.1);
114  min.SetVariable(2, "xf2", xf2, 0.1);
115  min.SetVariable(3, "yf2", yf2, 0.1);
116  min.SetVariable(4, "a", a, 0.1);
117 
118  min.Minimize();
119 
120  const double* xs = min.X();
121 
122  vector<double> fpar;
123  fpar.clear();
124  for (int i = 0; i < 5; i++) {
125  fpar.push_back(xs[i]);
126  }
127  return fpar;
128 }
CbmRichRingFitterEllipseBase::CalcChi2
virtual void CalcChi2(CbmRichRingLight *ring)
Calculate chi2 of the ellipse fit.
Definition: CbmRichRingFitterEllipseBase.h:42
CbmRichRingFitterEllipseMinuit::TransformToRichRing
void TransformToRichRing(CbmRichRingLight *ring, const vector< double > &par)
Transform obtained parameters from MINUIT to CbmRichRingLight.
Definition: CbmRichRingFitterEllipseMinuit.cxx:46
CbmRichRingFitterEllipseMinuit::~CbmRichRingFitterEllipseMinuit
virtual ~CbmRichRingFitterEllipseMinuit()
Standard destructor.
Definition: CbmRichRingFitterEllipseMinuit.cxx:19
sqrt
friend F32vec4 sqrt(const F32vec4 &a)
Definition: L1/vectors/P4_F32vec4.h:41
i
int i
Definition: L1/vectors/P4_F32vec4.h:25
CbmRichHitLight::fX
float fX
Definition: CbmRichRingLight.h:34
CbmRichRingLight::SetRadius
void SetRadius(float r)
Definition: CbmRichRingLight.h:124
min
friend F32vec4 min(const F32vec4 &a, const F32vec4 &b)
Definition: L1/vectors/P4_F32vec4.h:33
CbmRichRingFitterEllipseMinuit.h
This is the implementation of ellipse fitting using MINUIT.
CbmRichRingFitterEllipseMinuit::DoFit
virtual void DoFit(CbmRichRingLight *ring)
Inherited from CbmRichRingFitterBase.
Definition: CbmRichRingFitterEllipseMinuit.cxx:21
CbmRichRingLight::GetNofHits
int GetNofHits() const
Return number of hits in ring.
Definition: CbmRichRingLight.h:108
CbmRichRingLight::SetXYABP
void SetXYABP(float x, float y, float a, float b, float p)
Set all 5 ellipse parameters.
Definition: CbmRichRingLight.h:146
CbmRichRingFitterEllipseMinuit::CbmRichRingFitterEllipseMinuit
CbmRichRingFitterEllipseMinuit()
Default constructor.
Definition: CbmRichRingFitterEllipseMinuit.cxx:17
CbmRichHitLight::fY
float fY
Definition: CbmRichRingLight.h:35
x
Double_t x
Definition: CbmMvdSensorDigiToHitTask.cxx:68
y
Double_t y
Definition: CbmMvdSensorDigiToHitTask.cxx:68
CbmRichRingLight::GetHit
CbmRichHitLight GetHit(int ind)
Return hit by the index.
Definition: CbmRichRingLight.h:114
FCNEllipse2
Definition: CbmRichRingFitterEllipseMinuit.h:21
CbmRichRingLight
Definition: CbmRichRingLight.h:39