Jpp  15.0.1-rc.1-highqe
the software that should make you happy
 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 "JMath/JMath.hh"
7 #include "JFit/JVectorNZ.hh"
8 #include "JFit/JMatrixNZ.hh"
9 
10 
11 /**
12  * \file
13  *
14  * Auxiliary methods to evaluate Poisson probabilities and chi2.
15  * \author mdejong
16  */
17 
18 /**
19  * Auxiliary classes and methods for linear and iterative data regression.
20  */
21 namespace JFIT {}
22 namespace JPP { using namespace JFIT; }
23 
24 namespace JFIT {
25 
26  /**
27  * Get Poisson probability to observe a hit or not
28  * for given expectation value for the number of hits.
29  *
30  * \param expval expectation value
31  * \param hit hit
32  * \return probability
33  */
34  inline double getP(const double expval, bool hit)
35  {
36  if (hit)
37  return -expm1(-expval); // 1 - P(0)
38  else
39  return exp(-expval); // P(0)
40  }
41 
42 
43  /**
44  * Get Poisson probability to observe given number of hits for the given expectation value for the number of hits.
45  *
46  * \param expval expectation value
47  * \param numberOfHits number of hits
48  * \return propability
49  */
50  inline double getP(const double expval, const size_t numberOfHits)
51  {
52  using namespace JPP;
53 
54  double P = exp(-expval);
55 
56  for (size_t i = numberOfHits; i != 0; --i) {
57  P *= expval / (double) i;
58  }
59 
60  return P;
61  }
62 
63 
64  /**
65  * Get chi2 corresponding to given probability.
66  *
67  * \param P probability
68  * \return chi2
69  */
70  inline double getChi2(const double P)
71  {
72  return -log(P);
73  }
74 
75 
76  /**
77  * Get chi2 to observe a hit or not for given expectation value for the number of hits.
78  *
79  * \param expval expectation value
80  * \param hit hit
81  * \return probability
82  */
83  inline double getChi2(const double expval, bool hit)
84  {
85  if (hit)
86  return -log1p(-exp(-expval));
87  else
88  return expval;
89  }
90 
91 
92  /**
93  * Determine chi2 of a hit for a given model and time resolution.
94  *
95  * The template argument <tt>JModel_t</tt> refers to a data structure
96  * which should provide for the following method:
97  * <pre>
98  * double %getT(const JHit_t& hit) const; // get expected time of hit
99  * </pre>
100  *
101  * \param model model
102  * \param hit hit
103  * \param sigma sigma
104  * \return chi2
105  */
106  template<class JModel_t, class JHit_t>
107  inline double getChi2(const JModel_t& model, const JHit_t& hit, const double sigma)
108  {
109  const double ds = (hit.getT() - model.getT(hit)) / sigma;
110 
111  return ds * ds;
112  }
113 
114 
115  /**
116  * Determine chi2 of data for given model and time resolution.
117  *
118  * The template argument <tt>JModel_t</tt> refers to a data structure
119  * which should provide for the following method:
120  * <pre>
121  * double %getT(const value_type& hit) const; // expected time of hit
122  * </pre>
123  * where <tt>value_type</tt> corresponds to the value type if the input data.
124  *
125  * \param model model
126  * \param __begin begin of data
127  * \param __end end of data
128  * \param sigma time resolution [ns]
129  * \return chi2
130  */
131  template<class JModel_t, class T>
132  inline double getChi2(const JModel_t& model, T __begin, T __end, const double sigma)
133  {
134  double chi2 = 0.0;
135 
136  for (T hit = __begin; hit != __end; ++hit) {
137  chi2 += getChi2(model, *hit, sigma);
138  }
139 
140  return chi2;
141  }
142 
143 
144  /**
145  * Determine chi2 using full covariance matrix.
146  *
147  * \param Y residuals
148  * \param V covariance matrix
149  * \return chi2
150  */
151  inline double getChi2(const JVectorNZ& Y,
152  const JMatrixNZ& V)
153  {
154  return V.getDot(Y);
155  }
156 
157 
158  /**
159  * Determine chi2 of data for given track using full covariance matrix.
160  *
161  * \param track track
162  * \param __begin begin of data
163  * \param __end end of data
164  * \param V covariance matrix
165  * \return chi2
166  */
167  template<class T>
168  inline double getChi2(const JLine1Z& track,
169  T __begin,
170  T __end,
171  const JMatrixNZ& V)
172  {
173  const JVectorNZ Y(track, __begin, __end);
174 
175  return getChi2(Y, V);
176  }
177 
178 
179  /**
180  * Determine chi2 of data for given track and angular and time resolution.
181  *
182  * \param track track
183  * \param __begin begin of data
184  * \param __end end of data
185  * \param alpha angular resolution [deg]
186  * \param sigma time resolution [ns]
187  * \return chi2
188  */
189  template<class T>
190  inline double getChi2(const JLine1Z& track, T __begin, T __end, const double alpha, const double sigma)
191  {
192  JMatrixNZ V(track, __begin, __end, alpha, sigma);
193 
194  V.invert();
195 
196  return getChi2(track, __begin, __end, V);
197  }
198 
199 
200  /**
201  * Determine difference between chi2 with and without hit using full covariance matrix.
202  *
203  * \param Y residuals
204  * \param V covariance matrix
205  * \param i index of excluded hit
206  * \return chi2
207  */
208  inline double getChi2(const JVectorNZ& Y,
209  const JMatrixNZ& V,
210  const int i)
211  {
212  double chi2 = 0.0;
213 
214  for (size_t j = 0; j != V.size(); ++j) {
215  chi2 += V(i,j) * Y[j];
216  }
217 
218  return chi2*chi2 / V(i,i);
219  }
220 
221 
222  /**
223  * Get chi2 of data for given model and fit function.
224  *
225  * The template argument <tt>JFit_t</tt> refers to a data structure
226  * which should provide for the function object operator:
227  * <pre>
228  * double operator()(const JModel_t& model, const value_type&) const; // chi2
229  * </pre>
230  * where
231  * <tt>JModel_t</tt> refers to the given model and
232  * <tt>value_type</tt> to the value type if the input data.
233  * The return value should correspond to the chi2 of the hit.
234  *
235  * \param model model
236  * \param fit fit function
237  * \param __begin begin of data
238  * \param __end end of data
239  */
240  template<class JModel_t, class JFit_t, class T>
241  inline double getChi2(const JModel_t& model, const JFit_t& fit, T __begin, T __end)
242  {
243  double chi2 = 0.0;
244 
245  for (T hit = __begin; hit != __end; ++hit) {
246  chi2 += fit(model, *hit);
247  }
248 
249  return chi2;
250  }
251 }
252 
253 #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 fatal Wrong number of arguments fi set_variable DETECTOR $argv[1] set_variable STRING $argv[2] set_array QUANTILES set_variable FORMULA *[0] exp(-0.5 *(x-[1])*(x-[1])/([2]*[2]))" set_variable MODULE `getModule -a $DETECTOR -L "$STRING 0"` typeset -Z 4 STRING JOpera1D -f hydrophone.root
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:34
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:70
then $DIR JPlotNPE PDG P
Definition: JPlotNPE-PDG.sh:62