Jpp 20.0.0-rc.2
the software that should make you happy
Loading...
Searching...
No Matches
JShowerBrightPointRegressor.hh
Go to the documentation of this file.
1#ifndef __JFIT__JSHOWERBRIGHTPOINTREGRESSOR__
2#define __JFIT__JSHOWERBRIGHTPOINTREGRESSOR__
3
4#include <array>
5
11
12#include "JTools/JResult.hh"
13
14#include "JMath/JZero.hh"
15
16#include "JFit/JGandalf.hh"
17#include "JFit/JPoint4E.hh"
18#include "JFit/JMEstimator.hh"
19#include "JFit/JRegressor.hh"
20#include "JFit/JFitToolkit.hh"
21#include "JFit/JTimeRange.hh"
22
23#include "Jeep/JMessage.hh"
24
25/**
26 * \file
27 * Data regression method for JFIT::JPoint4E from a bright point isoptropic emission PDF.
28 *
29 * \author adomi, vcarretero
30 */
31
32namespace JFIT {}
33namespace JPP { using namespace JFIT; }
34
35namespace JFIT {
36
41
42 /**
43 * Function to constrain the energy during the fit, to prevent unphysical values.
44 *
45 * \param value model (I/O)
46 */
47 void model(JPoint4E& value)
48 {
49 using namespace std;
50
51 double E = max(0.0,value.getE());
52 value = JPoint4E(static_cast<const JPoint4D&>(value), E);
53 }
54
55 /**
56 * Template specialisation for storage of PDF tables.
57 */
58 template<>
60 {
65
66 static const int NUMBER_OF_PDFS = 2;
67
69
70 /**
71 * Default constructor.
72 */
75
76 /**
77 * Parameterized constructor
78 *
79 * The PDF file descriptor should contain the wild card character JPHYSICS::WILDCARD which
80 * will be replaced by the corresponding PDF types listed in JRegressorStorage<JPoint4E, JGandalf>::pdf_t.
81 *
82 * The <tt>TTS</tt> corresponds to the additional time smearing applied to the PDFs.
83 *
84 * \param fileDescriptor PDF file descriptor
85 * \param T_ns time range [ns]
86 * \param TTS TTS [ns]
87 * \param numberOfPoints number of points for Gauss-Hermite integration of TTS
88 * \param epsilon precision for Gauss-Hermite integration of TTS
89 */
90 JRegressorStorage(const std::string& fileDescriptor,
91 const JTimeRange& T_ns,
92 const double TTS,
93 const int numberOfPoints = 25,
94 const double epsilon = 1.0e-10) :
95 T_ns(T_ns)
96
97 {
98 using namespace std;
99 using namespace JPP;
100
101 const JPDF_t::JSupervisor supervisor(new JPDF_t::JDefaultResult(JMATH::zero));
102
103 for (int i = 0; i != NUMBER_OF_PDFS; ++i) {
104
105 const string file_name = getFilename(fileDescriptor, pdf_t[i]);
106
107 _pdf[i].load(file_name.c_str());
108
109 _pdf[i].setExceptionHandler(supervisor);
110 }
111
112 // Add PDFs
113 for (int i = 1; i < NUMBER_OF_PDFS; i += 2) {
114
115 _pdf[ i ].add(_pdf[i-1]);
116
117 JPDF_t buffer;
118
119 _pdf[i-1].swap(buffer);
120
121 if (TTS > 0.0) {
122 _pdf[i].blur(TTS, numberOfPoints, epsilon);
123 }
124 }
125 }
126
127 /**
128 * Get PDFs.
129 *
130 * \return PDFs
131 */
132 const JPDFs_t& getPDF() const
133 {
134 return _pdf;
135 }
136
137
138 /**
139 * PDF types.
140 */
141 static const JPDFType_t pdf_t[NUMBER_OF_PDFS];
142 JTimeRange T_ns; //!< Time window with respect to Cherenkov hypothesis [ns]
143
144 private:
145 JPDFs_t _pdf; //!< PDFs
146 };
147
148 /**
149 * PDF types.
150 */
152 DIRECT_LIGHT_FROM_BRIGHT_POINT,
153 SCATTERED_LIGHT_FROM_BRIGHT_POINT
154 };
155
156
157 /**
158 * Regressor function object for JLine3Z fit using JGandalf minimiser.
159 */
160 template<>
161 struct JRegressor<JPoint4E, JGandalf> :
162 public JAbstractRegressor<JPoint4E, JGandalf>,
163 public JRegressorStorage <JPoint4E, JGandalf>
164 {
165 using JAbstractRegressor<JPoint4E, JGandalf>::operator();
166
167 typedef JRegressorStorage<JPoint4E, JGandalf> storage_type;
168
169 /**
170 * Default constructor
171 */
172 JRegressor() :
173 storage_type(),
174 pdf(getPDF())
175 {}
176
177 /**
178 * Constructor.
179 *
180 * The PDF file descriptor should contain the wild card character JPHYSICS::WILDCARD which
181 * will be replaced by the PDF types listed in JRegressorStorage<JPoint4E, JGandalf>::pdf_t.
182 *
183 * The <tt>TTS</tt> corresponds to the additional time smearing applied to the PDFs.
184 *
185 * \param fileDescriptor PDF file descriptor
186 * \param T_ns time range [ns]
187 * \param TTS TTS [ns]
188 * \param numberOfPoints number of points for Gauss-Hermite integration of TTS
189 * \param epsilon precision for Gauss-Hermite integration of TTS
190 */
191 JRegressor(const std::string& fileDescriptor,
192 const JTimeRange& T_ns,
193 const double TTS,
194 const int numberOfPoints = 25,
195 const double epsilon = 1.0e-10) :
196 storage_type(fileDescriptor, T_ns, TTS, numberOfPoints, epsilon),
197 pdf(getPDF())
198 {}
199
200 /**
201 * Constructor.
202 *
203 * \param storage PDF storage
204 */
205 JRegressor(const storage_type& storage) :
206 pdf(storage.getPDF())
207 {
208 T_ns = storage.T_ns;
209 }
210
211 /**
212 * Fit function.
213 * This method is used to determine the chi2 and gradient of given hit with respect a bright point emitting isotropically
214 *
215 * JHit_t refers to a data structure which should have the following member methods:
216 * - double getX(); // [m]
217 * - double getY(); // [m]
218 * - double getZ(); // [m]
219 * - double getDX(); // [u]
220 * - double getDY(); // [u]
221 * - double getDZ(); // [u]
222 * - double getT(); // [ns]
223 *
224 * \param vx shower vertex
225 * \param hit hit
226 * \return chi2 and gradient
227 */
228 template<class JHit_t>
229 result_type operator()(const JPoint4E& vx, const JHit_t& hit) const
230 {
231 using namespace JPP;
232
233 JPosition3D D(hit.getPosition());
234 JDirection3D U(hit.getDirection());
235
236 D.sub(vx.getPosition());
237 double length = D.getLength();
238 double ct = U.getDot(D) / length;
239
240 if (ct > +1.0) { ct = +1.0; }
241 if (ct < -1.0) { ct = -1.0; }
242
243 const double t = vx.getT() + (length * getIndexOfRefraction() * getInverseSpeedOfLight());
244
245 const double dt = T_ns.constrain(hit.getT() - t);
246
247 JPDF_t::result_type H0 = getH0(hit.getR(), dt); // getH0 = Get background hypothesis value
248 JPDF_t::result_type H1 = getH1(length, ct, dt); // getH1 = Get signal hypothesis value / 1 GeV
249
250 if (get_value(H1) >= Vmax_npe) {
251 H1 *= Vmax_npe / get_value(H1);
252 }
253
254 double H1_value = get_value(H1);
255 double v_H1 = H1.v; //Integral from tmin to t of just H1
256 double V_H1 = H1.V; //Integral from tmin to tmax of just H1
257 H1 *= vx.getE();
258
259 JPDF_t::result_type HT = H1+H0; //now H1 is signal + background
260 double HT_value = get_value(HT);
261 result_type result;
262 result.chi2 = HT.getChi2() - H0.getChi2(); // Likelihood ratio
263
264 double exp_V_HT = exp(-HT.V); //V is the integral from tmin to tmax of EH1+H0
265
266 double energy_gradient = -1 / HT_value; //dPdE
267 energy_gradient *= (H1_value - HT_value * v_H1) * (1-exp_V_HT) - HT_value * exp_V_HT * V_H1; //Numerator
268 energy_gradient /= (1-exp_V_HT); // Denominator
269
270 /*
271 * Here it is evaluated: d(chi2)/d(ct) * d(ct)/d(x0,y0,z0,t0) + d(chi2)/dE
272 */
273 result.gradient = JPoint4E(JPoint4D(JVector3D(-getIndexOfRefraction() * D.getX() / length, // d(ct)/d(x0)
274 -getIndexOfRefraction() * D.getY() / length, // d(ct)/d(y0)
275 -getIndexOfRefraction() * D.getZ() / length), // d(ct)/d(z0)
276 getSpeedOfLight()), // d(ct)/d(t0)
277 energy_gradient); // d(chi2)/d(E)
278
279 static_cast<JPoint4D&>(result.gradient).mul(getInverseSpeedOfLight() * (HT.getDerivativeOfChi2() -
280 H0.getDerivativeOfChi2())); // x d(chi2)/d(ct1)
281
282 return result;
283
284 }
285
286 /**
287 * Get background hypothesis value for time differentiated PDF.
288 *
289 * \param R_Hz rate [Hz]
290 * \param t1 time [ns]
291 * \return hypothesis value
292 */
293 JPDF_t::result_type getH0(const double R_Hz,
294 const double t1) const
295 {
296 using namespace JPP;
297
298 return JPDF_t::result_type(R_Hz * 1e-9, t1, T_ns);
299 }
300
301 /**
302 * Get signal hypothesis value per 1 GeV for bright point emission PDF.
303 *
304 * \param D hit distance from shower vertex [m]
305 * \param ct cosine of the HIT angle
306 * \param t arrival time of the light
307 * \return hypothesis value / GeV
308 */
309 JPDF_t::result_type getH1(const double D,
310 const double ct,
311 const double t) const
312 {
313 using namespace JPP;
314
315 JPDF_t::result_type h1 = JMATH::zero;
316
317 for (int i = 0; i != NUMBER_OF_PDFS; ++i) {
318
319 if (!pdf[i].empty() && D <= pdf[i].getXmax()) {
320
321 try {
322
323 JPDF_t::result_type y1 = pdf[i](std::max(D, pdf[i].getXmin()), ct, t);
324
325 // safety measures
326
327 if (y1.f <= 0.0) {
328 y1.f = 0.0;
329 y1.fp = 0.0;
330 }
331
332 if (y1.v <= 0.0) {
333 y1.v = 0.0;
334 }
335
336 h1 += y1;
337 }
338 catch(JLANG::JException& error) {
339 ERROR(error << std::endl);
340 }
341 }
342 }
343
344 return h1;
345 }
346
347 /**
348 * Get maximal road width of PDF.
349 *
350 * \return road width [m]
351 */
352 inline double getRmax() const
353 {
354 using namespace JPP;
355
356 double xmax = 0.0;
357
358 for (int i = 0; i != NUMBER_OF_PDFS; ++i) {
359
360 if (!pdf[i].empty() && pdf[i].getXmax() > xmax) {
361 xmax = pdf[i].getXmax();
362 }
363
364 }
365
366 return xmax;
367 }
368
369 static double Vmax_npe; //!< Maximal integral of PDF [npe]
370
371 const JPDFs_t& pdf; //!< PDF
372 };
373
374
375 /**
376 * Default values.
377 */
378 double JRegressor<JPoint4E, JGandalf>::Vmax_npe = std::numeric_limits<double>::max();
379
380}
381
382#endif
Auxiliary methods to evaluate Poisson probabilities and chi2.
Maximum likelihood estimator (M-estimators).
General purpose messaging.
#define ERROR(A)
Definition JMessage.hh:66
Auxiliary methods for PDF calculations.
Numbering scheme for PDF types.
Physics constants.
General purpose data regression method.
int numberOfPoints
Definition JResultPDF.cc:22
This include file containes various data structures that can be used as specific return types for the...
Definition of zero value for any class.
Fit method based on the Levenberg-Marquardt method.
Definition JGandalf.hh:87
Data structure for vertex fit.
Definition JPoint4D.hh:24
Data structure for vertex fit.
Definition JPoint4E.hh:24
double getE() const
Get energy.
Definition JPoint4E.hh:52
Data structure for direction in three dimensions.
Data structure for position in three dimensions.
const JPosition3D & getPosition() const
Get position.
Data structure for vector in three dimensions.
Definition JVector3D.hh:36
double getT(const JVector3D &pos) const
Get arrival time of Cherenkov light at given position.
Definition JVertex3D.hh:160
General exception.
Definition JException.hh:24
Multi-dimensional PDF table for arrival time of Cherenkov light.
Definition JPDFTable.hh:44
Auxiliary classes and methods for linear and iterative data regression.
void model(JModel_t &value)
Auxiliary function to constrain model during fit.
Definition JGandalf.hh:57
static const JZero zero
Function object to assign zero value.
Definition JZero.hh:105
JPDFType_t
PDF types.
Definition JPDFTypes.hh:24
@ SCATTERED_LIGHT_FROM_BRIGHT_POINT
scattered light from bright point
Definition JPDFTypes.hh:43
@ DIRECT_LIGHT_FROM_BRIGHT_POINT
direct light from bright point
Definition JPDFTypes.hh:42
This name space includes all other name spaces (except KM3NETDAQ, KM3NET and ANTARES).
JResultEvaluator< JResult_t >::result_type get_value(const JResult_t &value)
Helper method to recursively evaluate a to function value.
Definition JResult.hh:998
Abstract class for global fit method.
Definition JRegressor.hh:79
JRegressorStorage(const std::string &fileDescriptor, const JTimeRange &T_ns, const double TTS, const int numberOfPoints=25, const double epsilon=1.0e-10)
Parameterized constructor.
JPHYSICS::JPDFTable< JFunction1D_t, JPDFMapList_t > JPDF_t
std::array< JPDF_t, NUMBER_OF_PDFS > JPDFs_t
PDFs.
JTOOLS::JMAPLIST< JTOOLS::JPolint2FunctionalMap, JTOOLS::JPolint1FunctionalGridMap >::maplist JPDFMapList_t
JTimeRange T_ns
Time window with respect to Cherenkov hypothesis [ns].
Template data structure for storage of internal data.
Template definition of a data regressor of given model.
Definition JRegressor.hh:70
Auxiliary class to set-up Hit.
Definition JSirene.hh:58
Auxiliary class for recursive map list generation.
Definition JMapList.hh:109
Type definition of a 1st degree polynomial interpolation based on a JGridMap implementation.
Type definition of a 2nd degree polynomial interpolation based on a JMap implementation.
Type definition of a spline interpolation method based on a JCollection with JResultPDF result type.