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