ChiZeroRegression.cc
Go to the documentation of this file.
1 #include "ChiZeroRegression.h"
2 
3 #include <utl/PhysicalConstants.h>
4 #include <fevt/Pixel.h>
5 #include <fevt/PixelRecData.h>
6 
7 #include <det/Detector.h>
8 #include <fdet/FDetector.h>
9 #include <fdet/Eye.h>
10 #include <utl/CoordinateSystemPtr.h>
11 #include <fdet/FTimeFitModel.h>
12 #include <TMinuit.h>
13 
14 #include "../../General/RecDataWriterNG/ConversionUtil.h"
15 
16 namespace FdProfileConstrainedGeometryFit {
17 
21 std::vector<double> ChiZeroRegression::times;
22 std::vector<double> ChiZeroRegression::timeErrs;
23 std::vector<double> ChiZeroRegression::chii;
24 
25 void
26 ChiZeroRegression::SetRealAtm(bool realAtm, bool deex) {
27  fRealAtm = realAtm;
28  fDeex = deex;
29  fdet::FTimeFitModel::GetInstance().SetModel((fRealAtm && fDeex) ? (fdet::FTimeFitModel::eRealisticAtm) :
31 }
32 
33 void
35 {
36  using namespace fevt;
37 
38  fEyeId = eyeRec.GetEyeId();
39  fTelId = eyeRec.TimeFitPixelsBegin()->GetTelescopeId();
40  chii.resize(0);
41  times.resize(0);
42  timeErrs.resize(0);
43 
45  end = eyeRec.TimeFitPixelsEnd(); pix != end; ++pix) {
46  const PixelRecData& pixRec = pix->GetRecData();
47  chii.push_back(pixRec.GetChi_i());
48  times.push_back(pixRec.GetT_i());
49  timeErrs.push_back(pixRec.GetT_iError());
50  }
51 
52  utl::CoordinateSystemPtr eyeCS = det::Detector::GetInstance().GetFDetector().GetEye(fEyeId).GetEyeCoordinateSystem();
53  fsdpTheta = eyeRec.GetSDP().GetTheta(eyeCS);
54 }
55 
56 void
58  double& chi2,
59  double& rp,
60  double& t0)
61  const
62 {
63  std::vector<double> xs(chii.size());
64 
65  double rpErr = 0.;
66  double t0Err = 0.;
67  for (unsigned int i = 0, n = chii.size(); i < n; ++i)
68  xs[i] = tan(0.5*(chi0 - chii[i]));
69  if (!fUseLightFlux) {
70  otoa::LinearFit(xs, times, timeErrs, t0, rp, chi2);
71  } else {
72  LinearFitErrors(xs, times, timeErrs, t0, t0Err, rp, rpErr, chi2);
73  }
74 
75  rp *= utl::kSpeedOfLight;
76  rpErr *= utl::kSpeedOfLight;
77 
78  if (fRealAtm)
79  MinuitFitErrors(chi0, t0, t0Err, rp, rpErr, chi2);
80 }
81 
82 void
84  double& chi2,
85  double& rp, double &rpErr,
86  double& t0, double &t0Err)
87  const
88 {
89  std::vector<double> xs(chii.size());
90 
91  for (unsigned int i = 0, n = chii.size(); i < n; ++i)
92  xs[i] = tan(0.5*(chi0 - chii[i]));
93 
94  LinearFitErrors(xs, times, timeErrs, t0, t0Err, rp, rpErr, chi2);
95  rp *= utl::kSpeedOfLight;
96  rpErr *= utl::kSpeedOfLight;
97 
98  if (fRealAtm)
99  MinuitFitErrors(chi0, t0, t0Err, rp, rpErr, chi2);
100 }
101 
102 void
103 ChiZeroRegression::MinuitFitFunc(int& /*npar*/, double* /*gin*/, double& f, double* par, int /*iflag*/)
104 {
105  double& Chi0 = par[0];
106  double& T0 = par[1];
107  double& Rp = par[2];
108  fdet::FTimeFitModel& timeFitModel = fdet::FTimeFitModel::GetInstance();
109 
110  double logLikeSum = 0.;
111  for (int i = 0, n = chii.size(); i < n; ++i) {
112  double ti_exp = timeFitModel.GetTimeAtAperture(T0, Rp, Chi0, chii[i], fsdpTheta, fEyeId, fTelId);
113  logLikeSum += (-0.5*log(timeErrs[i]*timeErrs[i]) - 0.5*pow( (times[i] - ti_exp)/timeErrs[i] , 2));
114  }
115  f = -2.*logLikeSum;
116 }
117 
118 void
120  double& T0, double& T0err,
121  double& Rp, double& Rperr,
122  double& chi2) const
123 {
124  const int npar = 3;
125  TMinuit theMinuit(npar);
126  theMinuit.SetPrintLevel(-1);
127  theMinuit.SetFCN(ChiZeroRegression::MinuitFitFunc);
128  double arglist[npar];
129  int ierflag=0;
130  arglist[0] = 1;
131  arglist[1] = 1;
132  theMinuit.mnexcm("SET ERR", arglist,1,ierflag);
133  if (ierflag) ERROR("Minuit SET ERR failed");
134  static double vstart[npar];
135  static double step[npar];
136  vstart[0] = Chi0;
137  vstart[1] = T0;
138  vstart[2] = Rp;
139  step[0] = 0;
140  step[1] = 10;
141  step[2] = 10;
142  theMinuit.mnparm(0, "Chi0", vstart[0], step[0], 0,0,ierflag);
143  theMinuit.FixParameter(0);
144  theMinuit.mnparm(1, "T0", vstart[1], step[1], vstart[1]-300, vstart[1]+300.,ierflag); //hardcoded boundaries, should be optional in the future?
145  theMinuit.mnparm(2, "Rp", vstart[2], step[2], vstart[2]-300., vstart[2]+300.,ierflag);
146  arglist[0] = 3000; //maxcalls
147  arglist[1] = 1; //tolerance
148  theMinuit.mnexcm("MIGRAD", arglist, 2, ierflag);
149  if (ierflag) {
150  ERROR("Minuit MIGRAD failed");
151  }
152  theMinuit.GetParameter(1, T0, T0err);
153  theMinuit.GetParameter(2, Rp, Rperr);
154  chi2 = theMinuit.fAmin;
155 }
156 
157 void
158 ChiZeroRegression::LinearFitErrors(const std::vector<double>& x,
159  const std::vector<double>& y,
160  const std::vector<double>& ey,
161  double& a0, double& a0err,
162  double& a1, double& a1err,
163  double& chi2) const
164 {
165  double sw = 0;
166  double sx = 0;
167  double sxx = 0;
168  double sy = 0;
169  double sxy = 0;
170  double syy = 0;
171 
172  for (unsigned int i = 0; i<x.size(); ++i) {
173  if (ey[i] > 0) {
174  const double wi = 1.0 / (ey[i]*ey[i]);
175  sw += wi;
176  sx += wi * x[i];
177  sxx += wi * x[i] * x[i];
178  sy += wi * y[i];
179  sxy += wi * x[i] * y[i];
180  syy += wi * y[i] * y[i];
181  }
182  }
183 
184  const double dinv = 1.0/(sw*sxx - sx*sx);
185 
186  a0 = (sxx*sy - sx * sxy) * dinv;
187  a1 = (-sx*sy + sw * sxy) * dinv;
188  a0err = sqrt (sxx * dinv);
189  a1err = sqrt (sw * dinv);
190 
191  if (!fUseLightFlux) {
192  chi2 = syy - a0*sy - a1*sxy;
193  } else {
194  double logLikeSum = 0; //differs only by constant from chi2, but put in
195  for (unsigned int i = 0; i<x.size(); ++i) {
196  logLikeSum += (-0.5*log(ey[i]*ey[i]) - 0.5*pow( (y[i]-(a0+a1*x[i]))/ey[i] , 2));
197  }
198  chi2 = -2.*logLikeSum;
199  }
200 }
201 
202 } //namespace
void MinuitFitErrors(const double Chi0, double &T0, double &T0err, double &Rp, double &Rperr, double &chi2) const
double GetChi_i() const
Definition: PixelRecData.h:117
double GetTheta(const CoordinateSystemPtr &coordinateSystem) const
zenith (theta) angle in spherical coordinates
Definition: BasicVector.h:248
unsigned int GetEyeId() const
Definition: EyeRecData.h:73
const unsigned int npar
Definition: UnivRec.h:75
double pow(const double x, const unsigned int i)
boost::indirect_iterator< ConstInternalPixelIterator, const Pixel & > ConstPixelIterator
Const iterator over pixels used in reconstruction.
Definition: EyeRecData.h:117
boost::shared_ptr< const CoordinateTransformer > CoordinateSystemPtr
Shared pointer for coordinate systems.
void PrepParams(const fevt::EyeRecData &eyeRec)
void LinearFitErrors(const std::vector< double > &x, const std::vector< double > &y, const std::vector< double > &ey, double &a0, double &a0err, double &a1, double &a1err, double &chi2) const
static void MinuitFitFunc(int &npar, double *gin, double &f, double *par, int iflag)
utl::TimeInterval GetT_i() const
Definition: PixelRecData.h:124
Eye-specific shower reconstruction data.
Definition: EyeRecData.h:65
const utl::AxialVector & GetSDP() const
Definition: EyeRecData.h:75
constexpr double kSpeedOfLight
PixelIterator TimeFitPixelsEnd()
Definition: EyeRecData.h:171
utl::TimeInterval GetT_iError() const
Definition: PixelRecData.h:125
double GetTimeAtAperture(const double t0, const double rp, const double chi0, const double chi_i, const double thetaSDP, const int eye, const int tel) const
void LinearFit(const vector< double > &x, const vector< double > &y, const vector< double > &ey, double &a0, double &a1, double &chi2)
Do a linear fit and return coefficients and chi2.
PixelIterator TimeFitPixelsBegin()
Definition: EyeRecData.h:167
#define ERROR(message)
Macro for logging error messages.
Definition: ErrorLogger.h:165
Fluorescence Detector Pixel Reconstructed Data.
Definition: PixelRecData.h:27
void operator()(const double chi0, double &chi2, double &rp, double &t0) const

, generated on Tue Sep 26 2023.