Jpp  17.3.1
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 "JPhysics/JK40Rates.hh"
7 
8 #include "JMath/JMath.hh"
10 
11 #include "JFit/JVectorNZ.hh"
12 #include "JFit/JMatrixNZ.hh"
13 
14 
15 /**
16  * \file
17  *
18  * Auxiliary methods to evaluate Poisson probabilities and chi2.
19  * \author mdejong
20  */
21 
22 /**
23  * Auxiliary classes and methods for linear and iterative data regression.
24  */
25 namespace JFIT {}
26 namespace JPP { using namespace JFIT; }
27 
28 namespace JFIT {
29 
30  using JPHYSICS::JK40Rates;
31 
32 
33  /**
34  * Get Poisson probability to observe a hit or not
35  * for given expectation value for the number of hits.
36  *
37  * \param expval expectation value
38  * \param hit hit
39  * \return probability
40  */
41  inline double getP(const double expval, bool hit)
42  {
43  if (hit)
44  return -expm1(-expval); // 1 - P(0)
45  else
46  return exp(-expval); // P(0)
47  }
48 
49 
50  /**
51  * Get chi2 corresponding to given probability.
52  *
53  * \param P probability
54  * \return chi2
55  */
56  inline double getChi2(const double P)
57  {
58  return -log(P);
59  }
60 
61 
62  /**
63  * Get chi2 to observe a hit or not for given expectation value for the number of hits.
64  *
65  * \param expval expectation value
66  * \param hit hit
67  * \return probability
68  */
69  inline double getChi2(const double expval, bool hit)
70  {
71  if (hit)
72  return -log1p(-exp(-expval));
73  else
74  return expval;
75  }
76 
77 
78  /**
79  * Determine chi2 of a hit for a given model and time resolution.
80  *
81  * The template argument <tt>JModel_t</tt> refers to a data structure
82  * which should provide for the following method:
83  * <pre>
84  * double %getT(const JHit_t& hit) const; // get expected time of hit
85  * </pre>
86  *
87  * \param model model
88  * \param hit hit
89  * \param sigma sigma
90  * \return chi2
91  */
92  template<class JModel_t, class JHit_t>
93  inline double getChi2(const JModel_t& model, const JHit_t& hit, const double sigma)
94  {
95  const double ds = (hit.getT() - model.getT(hit)) / sigma;
96 
97  return ds * ds;
98  }
99 
100 
101  /**
102  * Determine chi2 of data for 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 value_type& hit) const; // expected time of hit
108  * </pre>
109  * where <tt>value_type</tt> corresponds to the value type if the input data.
110  *
111  * \param model model
112  * \param __begin begin of data
113  * \param __end end of data
114  * \param sigma time resolution [ns]
115  * \return chi2
116  */
117  template<class JModel_t, class T>
118  inline double getChi2(const JModel_t& model, T __begin, T __end, const double sigma)
119  {
120  double chi2 = 0.0;
121 
122  for (T hit = __begin; hit != __end; ++hit) {
123  chi2 += getChi2(model, *hit, sigma);
124  }
125 
126  return chi2;
127  }
128 
129 
130  /**
131  * Determine chi2 using full covariance matrix.
132  *
133  * \param Y residuals
134  * \param V covariance matrix
135  * \return chi2
136  */
137  inline double getChi2(const JVectorNZ& Y,
138  const JMatrixNZ& V)
139  {
140  return V.getDot(Y);
141  }
142 
143 
144  /**
145  * Determine chi2 of data for given track using full covariance matrix.
146  *
147  * \param track track
148  * \param __begin begin of data
149  * \param __end end of data
150  * \param V covariance matrix
151  * \return chi2
152  */
153  template<class T>
154  inline double getChi2(const JLine1Z& track,
155  T __begin,
156  T __end,
157  const JMatrixNZ& V)
158  {
159  const JVectorNZ Y(track, __begin, __end);
160 
161  return getChi2(Y, V);
162  }
163 
164 
165  /**
166  * Determine chi2 of data for given track and angular and time resolution.
167  *
168  * \param track track
169  * \param __begin begin of data
170  * \param __end end of data
171  * \param alpha angular resolution [deg]
172  * \param sigma time resolution [ns]
173  * \return chi2
174  */
175  template<class T>
176  inline double getChi2(const JLine1Z& track, T __begin, T __end, const double alpha, const double sigma)
177  {
178  JMatrixNZ V(track, __begin, __end, alpha, sigma);
179 
180  V.invert();
181 
182  return getChi2(track, __begin, __end, V);
183  }
184 
185 
186  /**
187  * Determine difference between chi2 with and without hit using full covariance matrix.
188  *
189  * \param Y residuals
190  * \param V covariance matrix
191  * \param i index of excluded hit
192  * \return chi2
193  */
194  inline double getChi2(const JVectorNZ& Y,
195  const JMatrixNZ& V,
196  const int i)
197  {
198  double chi2 = 0.0;
199 
200  for (size_t j = 0; j != V.size(); ++j) {
201  chi2 += V(i,j) * Y[j];
202  }
203 
204  return chi2*chi2 / V(i,i);
205  }
206 
207 
208  /**
209  * Get chi2 of data for given model and fit function.
210  *
211  * The template argument <tt>JFit_t</tt> refers to a data structure
212  * which should provide for the function object operator:
213  * <pre>
214  * double operator()(const JModel_t& model, const value_type&) const; // chi2
215  * </pre>
216  * where
217  * <tt>JModel_t</tt> refers to the given model and
218  * <tt>value_type</tt> to the value type if the input data.
219  * The return value should correspond to the chi2 of the hit.
220  *
221  * \param model model
222  * \param fit fit function
223  * \param __begin begin of data
224  * \param __end end of data
225  */
226  template<class JModel_t, class JFit_t, class T>
227  inline double getChi2(const JModel_t& model, const JFit_t& fit, T __begin, T __end)
228  {
229  double chi2 = 0.0;
230 
231  for (T hit = __begin; hit != __end; ++hit) {
232  chi2 += fit(model, *hit);
233  }
234 
235  return chi2;
236  }
237 
238 
239  /**
240  * Get probability due to random background.
241  *
242  * \param N number of active PMTs
243  * \param M multiplicity of hits
244  * \param R_Hz rates [Hz]
245  * \param T_ns time window [ns]
246  * \return probability
247  */
248  inline double getProbability(const size_t N,
249  const size_t M,
250  const JK40Rates& R_Hz,
251  const double T_ns)
252  {
253  using namespace JPP;
254 
255  if (N == 0) {
256  return (M == 0 ? 1.0 : 0.0);
257  }
258 
259  const double T = T_ns * 1.0e-9;
260  const double p = -expm1(-R_Hz.getSinglesRate() * N * T); // 1 - P(0) due to singles rate
261 
262  double P = 0.0;
263 
264  for (multiplicity_type i = R_Hz.getLowerL1Multiplicity(); i <= R_Hz.getUpperL1Multiplicity(); ++i) {
265  P -= expm1(-R_Hz.getMultiplesRate(i) * T); // 1 - P(0) due to multiples rates
266  }
267 
268  P *= (1.0 + p); // 1 - P(0) due to multiples rates M-1 combined with singles rate
269 
270  return (poisson(M, R_Hz.getSinglesRate() * N * T) - // P(M) due to singles rate
271  expm1(-R_Hz.getMultiplesRate(M) * T) - // P(M) due to multiples rate
272  expm1(-R_Hz.getMultiplesRate(M-1) * T) * p) / (1.0 + P); // P(M-1) * P(1) due to multiples rate combined with singles rate
273  }
274 }
275 
276 #endif
JFit_t
Enumeration for fit algorithms.
Definition: JKatoomba.hh:105
Auxiliary methods for mathematics.
static double getDot(const variance &first, const variance &second)
Get dot product.
Definition: JMatrixNZ.hh:196
then JShowerPostfit f $INPUT_FILE o $OUTPUT_FILE N
multiplicity_type getLowerL1Multiplicity() const
Get lower multiplicty.
Definition: JK40Rates.hh:108
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
V(JDAQEvent-JTriggerReprocessor)*1.0/(JDAQEvent+1.0e-10)
const double sigma[]
Definition: JQuadrature.cc:74
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 getMultiplesRate(const multiplicity_type M) const
Get multiples rate at given multiplicity.
Definition: JK40Rates.hh:94
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:41
void invert()
Invert matrix according LDU decomposition.
Definition: JMatrixNS.hh:80
size_t size() const
Get dimension of matrix.
Definition: JMatrixND.hh:177
double getSinglesRate() const
Get singles rate.
Definition: JK40Rates.hh:71
size_t multiplicity_type
Type definition of multiplicity.
Definition: JK40Rates.hh:33
then if[[!-f $DETECTOR]] then JDetector sh $DETECTOR fi cat $WORKDIR trigger_parameters txt<< EOFtrigger3DMuon.enabled=1;trigger3DMuon.numberOfHits=5;trigger3DMuon.gridAngle_deg=1;ctMin=0.0;TMaxLocal_ns=15.0;EOF set_variable TRIGGEREFFICIENCY_TRIGGERED_EVENTS_ONLY INPUT_FILES=() for((i=1;$i<=$NUMBER_OF_RUNS;++i));do JSirene.sh $DETECTOR $JPP_DATA/genhen.km3net_wpd_V2_0.evt.gz $WORKDIR/sirene_ ${i}.root JTriggerEfficiency.sh $DETECTOR $DETECTOR $WORKDIR/sirene_ ${i}.root $WORKDIR/trigger_efficiency_ ${i}.root $WORKDIR/trigger_parameters.txt $JPP_DATA/PMT_parameters.txt INPUT_FILES+=($WORKDIR/trigger_efficiency_ ${i}.root) done for ANGLE_DEG in $ANGLES_DEG[*];do set_variable SIGMA_NS 3.0 set_variable OUTLIERS 3 set_variable OUTPUT_FILE $WORKDIR/matrix\[${ANGLE_DEG}\deg\].root $JPP_DIR/examples/JReconstruction-f"$INPUT_FILES[*]"-o $OUTPUT_FILE-S ${SIGMA_NS}-A ${ANGLE_DEG}-O ${OUTLIERS}-d ${DEBUG}--!fiif[[$OPTION=="plot"]];then if((0));then for H1 in h0 h1;do JPlot1D-f"$WORKDIR/matrix["${^ANGLES_DEG}" deg].root:${H1}"-y"1 2e3"-Y-L TR-T""-\^"number of events [a.u.]"-> o chi2
Definition: JMatrixNZ.sh:106
double getProbability(const size_t N, const size_t M, const JK40Rates &R_Hz, const double T_ns)
Get probability due to random background.
Definition: JFitToolkit.hh:248
Data structure for fit of straight line paralel to z-axis.
Definition: JLine1Z.hh:27
double poisson(const size_t n, const double mu)
Poisson probability density distribition.
multiplicity_type getUpperL1Multiplicity() const
Get upper multiplicty.
Definition: JK40Rates.hh:119
Base class for data structures with artithmetic capabilities.
int j
Definition: JPolint.hh:703
then cat $TRIPOD_INITIAL<< EOF1 256877.5 4743716.7-2438.42 256815.5 4743395.0-2435.53 257096.2 4743636.0-2439.5EOFfiif[[!-f $DETECTOR]];then JEditDetector-a $DETECTOR_INITIAL-s"-1 addz -6.9"-o $DETECTOR--!eval`JPrintDetector-a $DETECTOR-O SUMMARY`for STRING in ${STRINGS[*]};do set_variable MODULE`getModule-a $DETECTOR-L"$STRING 0"`JEditDetector-a $DETECTOR-M"$MODULE setz -2.9"-o $DETECTOR--!donefiif[[!-f $TRIPOD]];then cp-p $TRIPOD_INITIAL $TRIPODfiJAcoustics.sh $DETECTOR_IDcat > acoustics_trigger_parameters txt<< EOFQ=0.0;TMax_s=0.020;numberOfHits=90;EOFJAcousticsEventBuilder.sh $DETECTOR $RUNS[*]INPUT_FILES=(`ls KM3NeT_ ${(l:8::0::0:) DETECTOR_ID}_0 *${^RUNS}_event.root`) cd $WORKDIRif[!$HOMEDIR-ef $WORKDIR];then cp-p $HOMEDIR/$DETECTOR $WORKDIR cp-p $HOMEDIR/${^ACOUSTICS_KEYS}.txt $WORKDIR cp-p $HOMEDIR/${^INPUT_FILES}$WORKDIRfisource $JPP_DIR/examples/JAcoustics/acoustics-fit-toolkit.shtimer_startinitialise stage_b 1 0 100.0e-6 0.002 0.1 0 > &stage log
double getChi2(const double P)
Get chi2 corresponding to given probability.
Definition: JFitToolkit.hh:56
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"` source JAcousticsToolkit.sh typeset -A TRIPODS get_tripods $WORKDIR/tripod.txt TRIPODS XMEAN
Auxiliary class for K40 rates.
Definition: JK40Rates.hh:41
then $DIR JPlotNPE PDG P
Definition: JPlotNPE-PDG.sh:62