Jpp  master_rocky-40-g5f0272dcd
the software that should make you happy
JShowerBrightPointRegressor.hh
Go to the documentation of this file.
1 #ifndef __JFIT__JSHOWERBRIGHTPOINTREGRESSOR__
2 #define __JFIT__JSHOWERBRIGHTPOINTREGRESSOR__
3 
4 #include "JPhysics/JPDFTypes.hh"
5 #include "JPhysics/JPDFTable.hh"
7 #include "JPhysics/JNPETable.hh"
8 #include "JPhysics/JConstants.hh"
9 
10 #include "JTools/JResult.hh"
11 
12 #include "JMath/JZero.hh"
13 
14 #include "JFit/JGandalf.hh"
15 #include "JFit/JPoint4E.hh"
16 #include "JFit/JMEstimator.hh"
17 #include "JFit/JRegressor.hh"
18 #include "JFit/JFitToolkit.hh"
19 #include "JFit/JTimeRange.hh"
20 
21 #include "Jeep/JMessage.hh"
22 
23 /**
24  * \file
25  * Data regression method for JFIT::JPoint4E from a bright point isoptropic emission PDF.
26  *
27  * \author adomi, vcarretero
28  */
29 
30 namespace JFIT {}
31 namespace JPP { using namespace JFIT; }
32 
33 namespace JFIT {
34 
38  using JTOOLS::get_value;
39 
40  /**
41  * Regressor function object for JPoint4E fit using JGandalf minimiser.
42  */
43  template<>
45  public JAbstractRegressor<JPoint4E, JGandalf>
46  {
48 
53 
54 
55  /**
56  * Parameterized constructor
57  *
58  * The PDF file descriptor should contain the wild card character JPHYSICS::WILDCARD which
59  * will be replaced by the corresponding PDF types.
60  *
61  * \param fileDescriptor PDF file descriptor
62  * \param TTS TTS [ns]
63  * \param numberOfPoints number of points for Gauss-Hermite integration of TTS
64  * \param epsilon precision for Gauss-Hermite integration of TTS
65  */
66  JRegressor(const std::string& fileDescriptor,
67  const double TTS,
68  const int numberOfPoints = 25,
69  const double epsilon = 1.0e-10)
70  {
71  using namespace std;
72  using namespace JPP;
73 
74  const JPDF_t::JSupervisor supervisor(new JPDF_t::JDefaultResult(JMATH::zero));
75 
76  for (int i = 0; i != NUMBER_OF_PDFS; ++i) {
77 
78  try {
79 
80  const string file_name = getFilename(fileDescriptor, pdf_t[i]);
81 
82  NOTICE("loading PDF from file " << file_name << "... " << flush);
83 
84  pdf[i].load(file_name.c_str());
85 
86  NOTICE("OK" << endl);
87 
88  pdf[i].setExceptionHandler(supervisor);
89  }
90  catch(const JException& error) {
91  FATAL(error.what() << endl);
92  }
93  }
94 
95  // Add PDFs
96  for (int i = 1; i < NUMBER_OF_PDFS; i += 2) {
97 
98  pdf[ i ].add(pdf[i-1]);
99 
100  JPDF_t buffer;
101 
102  pdf[i-1].swap(buffer);
103 
104  if (TTS > 0.0) {
105  pdf[i].blur(TTS, numberOfPoints, epsilon);
106  } else if (TTS < 0.0) {
107  ERROR("Illegal value of TTS [ns]: " << TTS << endl);
108  }
109  }
110  }
111 
112  /**
113  * Fit function.
114  * This method is used to determine the chi2 and gradient of given hit with respect a bright point emitting isotropically
115  *
116  * JHit_t refers to a data structure which should have the following member methods:
117  * - double getX(); // [m]
118  * - double getY(); // [m]
119  * - double getZ(); // [m]
120  * - double getDX(); // [u]
121  * - double getDY(); // [u]
122  * - double getDZ(); // [u]
123  * - double getT(); // [ns]
124  *
125  * \param vx shower vertex
126  * \param hit hit
127  * \return chi2 and gradient
128  */
129  template<class JHit_t>
130  result_type operator()(const JPoint4E& vx, const JHit_t& hit) const
131  {
132  using namespace JPP;
133 
134  JPosition3D D(hit.getPosition());
135  JDirection3D U(hit.getDirection());
136 
137  D.sub(vx.getPosition());
138  double length = D.getLength();
139  double ct = U.getDot(D) / length;
140 
141  if (ct > +1.0) { ct = +1.0; }
142  if (ct < -1.0) { ct = -1.0; }
143 
144  const double t = vx.getT() + (length * getIndexOfRefraction() * getInverseSpeedOfLight());
145 
146  const double dt = T_ns.constrain(hit.getT() - t);
147 
148  JPDF_t::result_type H0 = getH0(hit.getR(), dt); // getH0 = Get background hypothesis value
149  JPDF_t::result_type H1 = getH1(length, ct, dt); // getH1 = Get signal hypothesis value / 1 GeV
150 
151  if (get_value(H1) >= Vmax_npe) {
152  H1 *= Vmax_npe / get_value(H1);
153  }
154 
155  double H1_value = get_value(H1);
156  double v_H1 = H1.v; //Integral from tmin to t of just H1
157  double V_H1 = H1.V; //Integral from tmin to tmax of just H1
158  H1 *= vx.getE();
159 
160  JPDF_t::result_type HT = H1+H0; //now H1 is signal + background
161  double HT_value = get_value(HT);
163  result.chi2 = HT.getChi2() - H0.getChi2(); // Likelihood ratio
164 
165  double exp_V_HT = exp(-HT.V); //V is the integral from tmin to tmax of EH1+H0
166 
167  double energy_gradient = -1 / HT_value; //dPdE
168  energy_gradient *= (H1_value - HT_value * v_H1) * (1-exp_V_HT) - HT_value * exp_V_HT * V_H1; //Numerator
169  energy_gradient /= (1-exp_V_HT); // Denominator
170 
171  /*
172  * Here it is evaluated: d(chi2)/d(ct) * d(ct)/d(x0,y0,z0,t0) + d(chi2)/dE
173  */
174  result.gradient = JPoint4E(JPoint4D(JVector3D(-getIndexOfRefraction() * D.getX() / length, // d(ct)/d(x0)
175  -getIndexOfRefraction() * D.getY() / length, // d(ct)/d(y0)
176  -getIndexOfRefraction() * D.getZ() / length), // d(ct)/d(z0)
177  getSpeedOfLight()), // d(ct)/d(t0)
178  energy_gradient); // d(chi2)/d(E)
179 
180  static_cast<JPoint4D&>(result.gradient).mul(getInverseSpeedOfLight() * (HT.getDerivativeOfChi2() -
181  H0.getDerivativeOfChi2())); // x d(chi2)/d(ct1)
182 
183  return result;
184 
185  }
186 
187  /**
188  * Get background hypothesis value for time differentiated PDF.
189  *
190  * \param R_Hz rate [Hz]
191  * \param t1 time [ns]
192  * \return hypothesis value
193  */
194  JPDF_t::result_type getH0(const double R_Hz,
195  const double t1) const
196  {
197  using namespace JPP;
198 
199  return JPDF_t::result_type(R_Hz * 1e-9, t1, T_ns);
200  }
201 
202  /**
203  * Get signal hypothesis value per 1 GeV for bright point emission PDF.
204  *
205  * \param D hit distance from shower vertex [m]
206  * \param ct cosine of the HIT angle
207  * \param t arrival time of the light
208  * \return hypothesis value / GeV
209  */
210  JPDF_t::result_type getH1(const double D,
211  const double ct,
212  const double t) const
213  {
214  using namespace JPP;
215 
217 
218  for (int i = 0; i != NUMBER_OF_PDFS; ++i) {
219 
220  if (!pdf[i].empty() && D <= pdf[i].getXmax()) {
221 
222  try {
223 
224  JPDF_t::result_type y1 = pdf[i](std::max(D, pdf[i].getXmin()), ct, t);
225 
226  // safety measures
227 
228  if (y1.f <= 0.0) {
229  y1.f = 0.0;
230  y1.fp = 0.0;
231  }
232 
233  if (y1.v <= 0.0) {
234  y1.v = 0.0;
235  }
236 
237  h1 += y1;
238 
239  }
240  catch(JLANG::JException& error) {
241  ERROR(error << std::endl);
242  }
243  }
244  }
245 
246  return h1;
247  }
248 
249  /**
250  * Get maximal road width of PDF.
251  *
252  * \return road width [m]
253  */
254  inline double getRmax() const
255  {
256  using namespace JPP;
257 
258  double xmax = 0.0;
259 
260  for (int i = 0; i != NUMBER_OF_PDFS; ++i) {
261 
262  if (!pdf[i].empty() && pdf[i].getXmax() > xmax) {
263  xmax = pdf[i].getXmax();
264  }
265 
266  }
267 
268  return xmax;
269  }
270 
271  static JTimeRange T_ns; //!< Time window with respect to Cherenkov hypothesis [ns]
272  static double Vmax_npe; //!< Maximal integral of PDF [npe]
273 
274  static const int NUMBER_OF_PDFS = 2;
275 
276  static const JPDFType_t pdf_t[NUMBER_OF_PDFS];
277 
278  JPDF_t pdf[NUMBER_OF_PDFS]; //!< PDF
279  };
280 
281 
282  /**
283  * PDF types.
284  */
287 
288  /**
289  * Default values.
290  */
292  double JRegressor<JPoint4E, JGandalf>::Vmax_npe = std::numeric_limits<double>::max();
293 
294 }
295 
296 #endif
Auxiliary methods to evaluate Poisson probabilities and chi2.
Maximum likelihood estimator (M-estimators).
General purpose messaging.
#define ERROR(A)
Definition: JMessage.hh:66
#define NOTICE(A)
Definition: JMessage.hh:64
#define FATAL(A)
Definition: JMessage.hh:67
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:86
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.
Definition: JDirection3D.hh:35
double getDot(const JAngle3D &angle) const
Get dot product.
Data structure for position in three dimensions.
Definition: JPosition3D.hh:38
const JPosition3D & getPosition() const
Get position.
Definition: JPosition3D.hh:130
Data structure for vector in three dimensions.
Definition: JVector3D.hh:36
double getY() const
Get y position.
Definition: JVector3D.hh:104
double getLength() const
Get length.
Definition: JVector3D.hh:246
double getZ() const
Get z position.
Definition: JVector3D.hh:115
JVector3D & sub(const JVector3D &vector)
Subtract vector.
Definition: JVector3D.hh:158
double getX() const
Get x position.
Definition: JVector3D.hh:94
double getT(const JVector3D &pos) const
Get arrival time of Cherenkov light at given position.
Definition: JVertex3D.hh:147
General exception.
Definition: JException.hh:24
virtual const char * what() const override
Get error message.
Definition: JException.hh:64
transformablemultifunction_type::result_type result_type
Definition: JPDFTable.hh:50
void blur(const double TTS, const int numberOfPoints=25, const double epsilon=1.0e-10, const double quantile=0.99)
Blur PDF.
Definition: JPDFTable.hh:111
const double xmax
Definition: JQuadrature.cc:24
const double epsilon
Definition: JQuadrature.cc:21
std::string getFilename(const std::string &file_name)
Get file name part, i.e. part after last JEEP::PATHNAME_SEPARATOR if any.
Definition: JeepToolkit.hh:128
Auxiliary classes and methods for linear and iterative data regression.
Definition: JEnergy.hh:15
static const JZero zero
Function object to assign zero value.
Definition: JZero.hh:105
double getIndexOfRefraction()
Get average index of refraction of water corresponding to group velocity.
const double getInverseSpeedOfLight()
Get inverse speed of light.
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
const double getSpeedOfLight()
Get speed of light.
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
Definition: JSTDTypes.hh:14
Abstract class for global fit method.
Definition: JRegressor.hh:79
Data structure for return value of fit function.
Definition: JGandalf.hh:101
JPDF_t::result_type getH0(const double R_Hz, const double t1) const
Get background hypothesis value for time differentiated PDF.
double getRmax() const
Get maximal road width of PDF.
static double Vmax_npe
Maximal integral of PDF [npe].
result_type operator()(const JPoint4E &vx, const JHit_t &hit) const
Fit function.
JPHYSICS::JPDFTable< JFunction1D_t, JPDFMapList_t > JPDF_t
JRegressor(const std::string &fileDescriptor, const double TTS, const int numberOfPoints=25, const double epsilon=1.0e-10)
Parameterized constructor.
JPDF_t::result_type getH1(const double D, const double ct, const double t) const
Get signal hypothesis value per 1 GeV for bright point emission PDF.
static JTimeRange T_ns
Time window with respect to Cherenkov hypothesis [ns].
JTOOLS::JMAPLIST< JTOOLS::JPolint2FunctionalMap, JTOOLS::JPolint1FunctionalGridMap >::maplist JPDFMapList_t
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.