Jpp
JFitToolkit.hh
Go to the documentation of this file.
1 #ifndef __JFIT__JFITTOOLKIT__
2 #define __JFIT__JFITTOOLKIT__
3 
4 #include <cmath>
5 
6 #include "TMath.h"
7 
8 #include "JMath/JMath.hh"
9 #include "JFit/JVectorNZ.hh"
10 #include "JFit/JMatrixNZ.hh"
11 #include "JFit/JEvt.hh"
12 
13 
14 /**
15  * \file
16  *
17  * Auxiliary methods to evaluate Poisson probabilities and chi2.
18  * \author mdejong
19  */
20 
21 /**
22  * Auxiliary classes and methods for linear and iterative data regression.
23  */
24 namespace JFIT {}
25 namespace JPP { using namespace JFIT; }
26 
27 namespace JFIT {
28 
29  /**
30  * Get Poisson probability to observe a hit or not
31  * for given expectation value for the number of hits.
32  *
33  * \param expval expectation value
34  * \param hit hit
35  * \return probability
36  */
37  inline double getP(const double expval, bool hit)
38  {
39  if (hit)
40  return 1.0 - exp(-expval); // 1 - P(0)
41  else
42  return exp(-expval); // P(0)
43  }
44 
45 
46  /**
47  * Get Poisson probability to observe given number of hits or more
48  * for the given expectation value for the number of hits.
49  *
50  * \param expval expectation value
51  * \param numberOfHits number of hits
52  * \param precision precision
53  * \return propability
54  */
55  inline double getP(const double expval, const unsigned int numberOfHits, const double precision = 1.0e-5)
56  {
57  double P = 0.0;
58 
59  for (int i = numberOfHits; ; ++i) {
60 
61  const double p = TMath::PoissonI(i, expval);
62 
63  P += p;
64 
65  if ((double) i > expval && p < precision) {
66  break;
67  }
68  }
69 
70  return P;
71  }
72 
73 
74  /**
75  * Get chi2 corresponding to given probability.
76  *
77  * \param P probability
78  * \return chi2
79  */
80  inline double getChi2(const double P)
81  {
82  return -log(P);
83  }
84 
85 
86  /**
87  * Get chi2 to observe a hit or not for given expectation value for the number of hits.
88  *
89  * \param expval expectation value
90  * \param hit hit
91  * \return probability
92  */
93  inline double getChi2(const double expval, bool hit)
94  {
95  if (hit)
96  return -log(1.0 - exp(-expval));
97  else
98  return expval;
99  }
100 
101 
102  /**
103  * Determine chi2 of a hit for a given model and time resolution.
104  *
105  * The template argument <tt>JModel_t</tt> refers to a data structure
106  * which should provide for the following method:
107  * <pre>
108  * double %getT(const JHit_t& hit) const; // get expected time of hit
109  * </pre>
110  *
111  * \param model model
112  * \param hit hit
113  * \param sigma sigma
114  * \return chi2
115  */
116  template<class JModel_t, class JHit_t>
117  inline double getChi2(const JModel_t& model, const JHit_t& hit, const double sigma)
118  {
119  const double ds = (hit.getT() - model.getT(hit)) / sigma;
120 
121  return ds * ds;
122  }
123 
124 
125  /**
126  * Determine chi2 of data for given model and time resolution.
127  *
128  * The template argument <tt>JModel_t</tt> refers to a data structure
129  * which should provide for the following method:
130  * <pre>
131  * double %getT(const value_type& hit) const; // expected time of hit
132  * </pre>
133  * where <tt>value_type</tt> corresponds to the value type if the input data.
134  *
135  * \param model model
136  * \param __begin begin of data
137  * \param __end end of data
138  * \param sigma time resolution [ns]
139  * \return chi2
140  */
141  template<class JModel_t, class T>
142  inline double getChi2(const JModel_t& model, T __begin, T __end, const double sigma)
143  {
144  double chi2 = 0.0;
145 
146  for (T hit = __begin; hit != __end; ++hit) {
147  chi2 += getChi2(model, *hit, sigma);
148  }
149 
150  return chi2;
151  }
152 
153 
154  /**
155  * Determine chi2 using full covariance matrix.
156  *
157  * \param Y residuals
158  * \param V covariance matrix
159  * \return chi2
160  */
161  inline double getChi2(const JVectorNZ& Y,
162  const JMatrixNZ& V)
163  {
164  return V.getDot(Y);
165  }
166 
167 
168  /**
169  * Determine chi2 of data for given track using full covariance matrix.
170  *
171  * \param track track
172  * \param __begin begin of data
173  * \param __end end of data
174  * \param V covariance matrix
175  * \return chi2
176  */
177  template<class T>
178  inline double getChi2(const JLine1Z& track,
179  T __begin,
180  T __end,
181  const JMatrixNZ& V)
182  {
183  const JVectorNZ Y(track, __begin, __end);
184 
185  return getChi2(Y, V);
186  }
187 
188 
189  /**
190  * Determine chi2 of data for given track and angular and time resolution.
191  *
192  * \param track track
193  * \param __begin begin of data
194  * \param __end end of data
195  * \param alpha angular resolution [deg]
196  * \param sigma time resolution [ns]
197  * \return chi2
198  */
199  template<class T>
200  inline double getChi2(const JLine1Z& track, T __begin, T __end, const double alpha, const double sigma)
201  {
202  JMatrixNZ V(track, __begin, __end, alpha, sigma);
203 
204  V.invert();
205 
206  return getChi2(track, __begin, __end, V);
207  }
208 
209 
210  /**
211  * Determine difference between chi2 with and without hit using full covariance matrix.
212  *
213  * \param Y residuals
214  * \param V covariance matrix
215  * \param i index of excluded hit
216  * \return chi2
217  */
218  inline double getChi2(const JVectorNZ& Y,
219  const JMatrixNZ& V,
220  const int i)
221  {
222  double chi2 = 0.0;
223 
224  for (size_t j = 0; j != V.size(); ++j) {
225  chi2 += V(i,j) * Y[j];
226  }
227 
228  return chi2*chi2 / V(i,i);
229  }
230 
231 
232  /**
233  * Get chi2 of data for given model and fit function.
234  *
235  * The template argument <tt>JFit_t</tt> refers to a data structure
236  * which should provide for the function object operator:
237  * <pre>
238  * double operator()(const JModel_t& model, const value_type&) const; // chi2
239  * </pre>
240  * where
241  * <tt>JModel_t</tt> refers to the given model and
242  * <tt>value_type</tt> to the value type if the input data.
243  * The return value should correspond to the chi2 of the hit.
244  *
245  * \param model model
246  * \param fit fit function
247  * \param __begin begin of data
248  * \param __end end of data
249  */
250  template<class JModel_t, class JFit_t, class T>
251  inline double getChi2(const JModel_t& model, const JFit_t& fit, T __begin, T __end)
252  {
253  double chi2 = 0.0;
254 
255  for (T hit = __begin; hit != __end; ++hit) {
256  chi2 += fit(model, *hit);
257  }
258 
259  return chi2;
260  }
261 }
262 
263 #endif
JFIT::JMatrixNZ::getDot
static double getDot(const variance &first, const variance &second)
Get dot product.
Definition: JMatrixNZ.hh:196
JVectorNZ.hh
JAANET::JHit_t
Auxiliary class to set-up Hit.
Definition: JHit_t.hh:25
JFIT
Auxiliary classes and methods for linear and iterative data regression.
Definition: JEnergy.hh:15
JTOOLS::j
int j
Definition: JPolint.hh:634
JMATH::JMatrixND_t::size
size_t size() const
Get dimension of matrix.
Definition: JMatrixND.hh:154
JPP
This name space includes all other name spaces (except KM3NETDAQ, KM3NET and ANTARES).
Definition: JAAnetToolkit.hh:37
JEvt.hh
JFIT::getChi2
double getChi2(const JModel_t &model, const JFit_t &fit, T __begin, T __end)
Get chi2 of data for given model and fit function.
Definition: JFitToolkit.hh:251
JMatrixNZ.hh
JFIT::JLine1Z
Data structure for fit of straight line paralel to z-axis.
Definition: JLine1Z.hh:27
JMath.hh
JMATH::JMatrixNS::invert
void invert()
Invert matrix according LDU decomposition.
Definition: JMatrixNS.hh:80
JFIT::JVectorNZ
Determination of the time residual vector of hits for a track along z-axis (JFIT::JLine1Z).
Definition: JVectorNZ.hh:28
JFIT::JMatrixNZ
Determination of the co-variance matrix of hits for a track along z-axis (JFIT::JLine1Z).
Definition: JMatrixNZ.hh:28
JFIT::getP
double getP(const double expval, const unsigned int numberOfHits, const double precision=1.0e-5)
Get Poisson probability to observe given number of hits or more for the given expectation value for t...
Definition: JFitToolkit.hh:55