Jpp
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
JShower3EZRegressor.hh
Go to the documentation of this file.
1 #ifndef __JFIT__JSHOWER3EZREGRESSOR__
2 #define __JFIT__JSHOWER3EZREGRESSOR__
3 
4 #include "JPhysics/JPDFTypes.hh"
5 #include "JPhysics/JPDFTable.hh"
7 #include "JPhysics/JNPETable.hh"
8 
11 #include "JTools/JConstants.hh"
12 #include "JTools/JRange.hh"
13 #include "JTools/JResult.hh"
14 #include "JTools/JFunction1D_t.hh"
17 
18 #include "JGeometry3D/JVector3D.hh"
19 #include "JGeometry3D/JVersor3D.hh"
21 
22 #include "JMath/JZero.hh"
23 
24 #include "JFit/JTimeRange.hh"
25 #include "JFit/JPMTW0.hh"
26 #include "JFit/JSimplex.hh"
27 #include "JFit/JGandalf.hh"
28 #include "JFit/JMEstimator.hh"
29 #include "JFit/JRegressor.hh"
30 #include "JFit/JShower3EZ.hh"
31 #include "JFit/JFitToolkit.hh"
32 
33 #include "Jeep/JMessage.hh"
34 
35 /**
36  * \file
37  * Data regression method for JFIT::JShower3Z.
38  */
39 
40 namespace JFIT {
41 
45  using JTOOLS::get_value;
46 
47  /**
48  * Regressor function object for JShower3EZ fit using JSimplex minimiser.
49  */
50  template<>
52  public JAbstractRegressor<JShower3EZ, JSimplex>
53  {
55 
62 
68 
69  /**
70  * Parameterized constructor
71  *
72  * The PDF file descriptor should contain the wild card character JPHYSICS::WILD_CARD which
73  * will be replaced by the corresponding PDF types listed in JRegressor<JShower3Z, JGandalf>::pdf_t.
74  *
75  * \param fileDescriptor PDF file descriptor
76  */
77 
78  JRegressor(const std::string& fileDescriptor):
79  estimator(new JMEstimatorNull())
80  {
81  using namespace std;
82  using namespace JPP;
83 
84  const JPDF_t::JSupervisor supervisor(new JPDF_t::JDefaultResult(JMATH::zero));
85 
86  for (int i = 0; i != NUMBER_OF_PDFS; ++i) {
87 
88  try {
89 
90  JPDF_t pdf;
91 
92  const string file_name = getFilename(fileDescriptor, pdf_t[i]);
93 
94  NOTICE("loading PDF from file " << file_name << "... " << flush);
95 
96  pdf.load(file_name.c_str());
97 
98  NOTICE("OK" << endl);
99 
100  pdf.setExceptionHandler(supervisor);
101 
102  npe[ i ] = JNPE_t(pdf);
103  }
104  catch(const JException& error) {
105  FATAL(error.what() << endl);
106  }
107  }
108 
109  // Add PDFs
110  for (int i = 1; i < NUMBER_OF_PDFS; i += 2) {
111 
112  npe[ i ].add(npe[i-1]);
113 
114  JNPE_t buffer;
115 
116  npe[i-1].swap(buffer);
117  }
118  }
119 
120  /**
121  * Fit function.
122  * This method is used to determine the chi2 of given PMT with respect to shower hypothesis.
123  *
124  * \param shower shower
125  * \param pmt pmt
126  * \return chi2
127  */
128  double operator()(const JShower3EZ& shower, const JPMTW0& pmt) const
129  {
130  using namespace JPP;
131 
132  JPosition3D D(pmt.getPosition());
133  JDirection3D U(pmt.getDirection());
134 
135  D.sub(shower.getPosition());
136 
137  JVersor3D shower_dir(shower.getDirection());
138 
139  const double z = D.getDot(shower_dir);
140  const double x = D.getX() - z * shower.getDX();
141  const double y = D.getY() - z * shower.getDY();
142  const double cosDelta = z/D.getLength(); // Delta = angle between shower direction and PMT position
143 
144  U.rotate(JRotation3Z(-atan2(y,x))); // rotate PMT axis to x-z plane
145 
146  const double theta = U.getTheta();
147  const double phi = fabs(U.getPhi());
148 
149  JNPE_t::result_type H0 = getH0(pmt.getR()); // getH0 = Get background hypothesis value for time integrated PDF.
150  JNPE_t::result_type H1 = getH1(D.getLength(), cosDelta, theta, phi, shower.getE()); // getH1 = Get signal hypothesis value for time integrated PDF.
151 
152  if (get_value(H1) >= Vmax_npe) {
153  H1 *= Vmax_npe / get_value(H1);
154  }
155 
156  H1 += H0; // now H1 is signal + background
157 
158  const bool hit = pmt.getN() != 0;
159  const double u = getChi2(get_value(H1), hit); // -log(lik)
160 
161  return estimator->getRho(u);
162  }
163 
164  /**
165  * Get background hypothesis value for time integrated PDF.
166  *
167  * \param R_Hz rate [Hz]
168  * \return hypothesis value
169  */
170  JNPE_t::result_type getH0(const double R_Hz) const
171  {
172  return JNPE_t::result_type(R_Hz * 1e-9 * T_ns.getLength());
173  }
174 
175  /**
176  * Get signal hypothesis value for time integrated PDF.
177  *
178  * \param D PMT distance from shower [m]
179  * \param cosDelta angle between shower direction and PMT position
180  * \param theta PMT zenith angle [deg]
181  * \param phi PMT azimuth angle [deg]
182  * \param E shower energy [GeV]
183  * \return hypothesis value
184  */
186  const double cosDelta,
187  const double theta,
188  const double phi,
189  const double E) const
190  {
192 
193  for (int i = 0; i != NUMBER_OF_PDFS; ++i) {
194 
195  if (!npe[i].empty() && D <= npe[i].getXmax()) {
196 
197  try {
198 
199  JNPE_t::result_type y1 = E * npe[i](std::max(D, npe[i].getXmin()), cosDelta, theta, phi);
200 
201  if (get_value(y1) > 0.0) {
202  h1 += y1;
203  }
204 
205  }
206  catch(JLANG::JException& error) {
207  ERROR(error << std::endl);
208  }
209  }
210  }
211 
212  return h1;
213  }
214 
215 
216  /**
217  * Get maximal road width of PDF.
218  *
219  * \return road width [m]
220  */
221  inline double getRmax() const
222  {
223  double xmax = 0.0;
224 
225  for (int i = 0; i != NUMBER_OF_PDFS; ++i) {
226 
227  if (!npe[i].empty() && npe[i].getXmax() > xmax) {
228  xmax = npe[i].getXmax();
229  }
230 
231  }
232 
233  return xmax;
234  }
235 
236  static JTimeRange T_ns; //!< Time window with respect to Cherenkov hypothesis [ns]
237  static double Vmax_npe; //!< Maximal integral of PDF [npe]
238 
239  static const int NUMBER_OF_PDFS = 2;
240 
241  static const JPDFType_t pdf_t[NUMBER_OF_PDFS];
242 
243  JNPE_t npe[NUMBER_OF_PDFS]; //!< PDF
244 
246  };
247 
248 
249  /**
250  * Regressor function object for JShower3EZ fit using JGandalf minimiser.
251  */
252  template<>
254  public JAbstractRegressor<JShower3EZ, JGandalf>
255  {
257 
264 
270 
271  /**
272  * Parameterized constructor
273  *
274  * The PDF file descriptor should contain the wild card character JPHYSICS::WILD_CARD which
275  * will be replaced by the corresponding PDF types listed in JRegressor<JShower3Z, JGandalf>::pdf_t.
276  *
277  * \param fileDescriptor PDF file descriptor
278  */
279 
280  JRegressor(const std::string& fileDescriptor):
281  estimator(new JMEstimatorNull())
282  {
283  using namespace std;
284  using namespace JPP;
285 
286  const JPDF_t::JSupervisor supervisor(new JPDF_t::JDefaultResult(JMATH::zero));
287 
288  for (int i = 0; i != NUMBER_OF_PDFS; ++i) {
289 
290  try {
291 
292  JPDF_t pdf;
293 
294  const string file_name = getFilename(fileDescriptor, pdf_t[i]);
295 
296  NOTICE("loading PDF from file " << file_name << "... " << flush);
297 
298  pdf.load(file_name.c_str());
299 
300  NOTICE("OK" << endl);
301 
302  pdf.setExceptionHandler(supervisor);
303 
304  npe[i] = JNPE_t(pdf);
305  }
306  catch(const JException& error) {
307  FATAL(error.what() << endl);
308  }
309  }
310 
311  // Add PDFs
312  for (int i = 1; i < NUMBER_OF_PDFS; i += 2) {
313 
314  npe[ i ].add(npe[i-1]);
315 
316  JNPE_t buffer;
317 
318  npe[i-1].swap(buffer);
319  }
320  }
321 
322  /**
323  * Fit function.
324  * This method is used to determine the chi2 of given PMT with respect to shower hypothesis.
325  *
326  * \param shower shower
327  * \param pmt pmt
328  * \return chi2
329  */
330  result_type operator()(const JShower3EZ& shower, const JPMTW0& pmt) const
331  {
332  using namespace JPP;
333  using namespace std;
334 
335  JPosition3D D(pmt.getPosition());
336  JDirection3D U(pmt.getDirection());
337 
338  D.sub(shower.getPosition());
339 
340  JVersor3D shower_dir(shower.getDirection());
341 
342  const double z = D.getDot(shower_dir);
343  const double x = D.getX() - z * shower.getDX();
344  const double y = D.getY() - z * shower.getDY();
345  const double R2 = D.getLengthSquared() - z*z;
346  const double R = sqrt(R2);
347  const double d = D.getLength();
348  const double cos_th0 = z/d; // theta0 = angle between shower direction and PMT position
349 
350  U.rotate(JRotation3Z(-atan2(y,x))); // rotate PMT axis to x-z plane
351 
352  const double theta = U.getTheta();
353  const double phi = fabs(U.getPhi());
354 
355  JNPE_t::result_type H0 = getH0(pmt.getR()); // getH0 = Get background hypothesis value for time integrated PDF.
356  JNPE_t::result_type H1 = getH1(d, cos_th0, theta, phi, shower.getE()); // getH1 = Get signal hypothesis value for time integrated PDF.
357 
358  if (get_value(H1) >= Vmax_npe) {
359  H1 *= Vmax_npe / get_value(H1);
360  }
361 
362  H1 += H0; // now H1 is signal + background
363 
364  const bool hit = pmt.getN() != 0;
365  const double u = H1.getChi2(hit);
366 
368 
369  result.chi2 = estimator->getRho(u);
370 
371  result.gradient = JShower3EZ(JPoint4D(JVector3D(x*z / (d*d*d), // d(cos_th0)/d(x)
372  y*z / (d*d*d), // d(cos_th0)/d(y)
373  -(R*R) / (d*d*d)), // d(cos_th0)/d(z)
374  0.0), // d(cos_th0)/d(t)
375  JVersor3Z(x*z*z / (d*R), // d(cos_th0)/d(dx)
376  y*z*z / (d*R)), // d(cos_th0)/d(dy)
377  0.0); // d(cos_th0)/d(E)
378 
379  result.gradient.mul(estimator->getPsi(u));
380  result.gradient.mul(H1.getDerivativeOfChi2(hit)); // x d(chi2)/d(cos_th0)
381 
382  return result;
383  }
384 
385  /**
386  * Get background hypothesis value for time integrated PDF.
387  *
388  * \param R_Hz rate [Hz]
389  * \return hypothesis value
390  */
391  JNPE_t::result_type getH0(const double R_Hz) const
392  {
393  return JNPE_t::result_type(R_Hz * 1e-9 * T_ns.getLength(), 0.0);
394  }
395 
396  /**
397  * Get signal hypothesis value for time integrated PDF.
398  *
399  * \param D PMT distance from shower [m]
400  * \param cosDelta angle between shower direction and PMT position
401  * \param theta PMT zenith angle [deg]
402  * \param phi PMT azimuth angle [deg]
403  * \param E shower energy [GeV]
404  * \return hypothesis value
405  */
407  const double cosDelta,
408  const double theta,
409  const double phi,
410  const double E) const
411  {
413 
414  for (int i = 0; i != NUMBER_OF_PDFS; ++i) {
415 
416  if (!npe[i].empty() && D <= npe[i].getXmax()) {
417 
418  try {
419 
420  JNPE_t::result_type y1 = E * npe[i](std::max(D, npe[i].getXmin()), cosDelta, theta, phi);
421 
422  if (get_value(y1) > 0.0) {
423  h1 += y1;
424  }
425 
426  }
427  catch(JLANG::JException& error) {
428  ERROR(error << std::endl);
429  }
430  }
431  }
432 
433  return h1;
434  }
435 
436 
437  /**
438  * Get maximal road width of PDF.
439  *
440  * \return road width [m]
441  */
442  inline double getRmax() const
443  {
444  double xmax = 0.0;
445 
446  for (int i = 0; i != NUMBER_OF_PDFS; ++i) {
447 
448  if (!npe[i].empty() && npe[i].getXmax() > xmax) {
449  xmax = npe[i].getXmax();
450  }
451 
452  }
453 
454  return xmax;
455  }
456 
457  static JTimeRange T_ns; //!< Time window with respect to Cherenkov hypothesis [ns]
458  static double Vmax_npe; //!< Maximal integral of PDF [npe]
459 
460  static const int NUMBER_OF_PDFS = 2;
461 
462  static const JFIT::JPDFType_t pdf_t[NUMBER_OF_PDFS];
463 
464  JNPE_t npe[NUMBER_OF_PDFS]; //!< PDF
465 
467  };
468 
469  /**
470  * PDF types.
471  */
474 
477 
478  /**
479  * Default values.
480  */
482  double JRegressor<JShower3EZ, JSimplex>::Vmax_npe = std::numeric_limits<double>::max();
483 
485  double JRegressor<JShower3EZ, JGandalf>::Vmax_npe = std::numeric_limits<double>::max();
486 }
487 
488 #endif
Auxiliary methods to evaluate Poisson probabilities and chi2.
Template definition of a data regressor of given model.
Definition: JRegressor.hh:68
JPHYSICS::JPDFTable< JFunction1D_t, JPDFMaplist_t > JPDF_t
double getE() const
Get energy.
Definition: JEnergy.hh:166
Type definition of a spline interpolation method based on a JCollection with JResultPDF result type...
General exception.
Definition: JException.hh:23
void load(const char *file_name)
Load from input file.
General purpose data regression method.
do echo Generating $dir eval D
Definition: JDrawLED.sh:50
double getR() const
Get rate.
Definition: JPMTW0.hh:56
Null M-estimator.
Definition: JMEstimator.hh:53
Auxiliary methods for PDF calculations.
This include file containes various data structures that can be used as specific return types for the...
Auxiliary class for handling PMT geometry, rate and response.
Definition: JPMTW0.hh:22
Data structure for vertex fit.
Definition: JPoint4D.hh:22
scattered light from EM shower
Definition: JPDFTypes.hh:41
const JDirection3D & getDirection() const
Get direction.
JTOOLS::JMapList< JTOOLS::JPolint0FunctionalMap, JTOOLS::JMapList< JTOOLS::JPolint0FunctionalMap, JTOOLS::JMapList< JTOOLS::JPolint0FunctionalGridMap, JTOOLS::JMapList< JTOOLS::JPolint0FunctionalGridMap > > > > JPDFMaplist_t
void setExceptionHandler(const supervisor_type &supervisor)
Set the supervisor for handling of exceptions.
double getRmax() const
Get maximal road width of PDF.
then for HISTOGRAM in h0 h1
Definition: JMatrixNZ.sh:69
JNPE_t::result_type getH1(const double D, const double cosDelta, const double theta, const double phi, const double E) const
Get signal hypothesis value for time integrated PDF.
static const JZero zero
Function object to assign zero value.
Definition: JZero.hh:105
Type definition of a 1st degree polynomial interpolation based on a JMap implementation.
JTOOLS::JMapList< JTOOLS::JPolint0FunctionalMap, JTOOLS::JMapList< JTOOLS::JPolint0FunctionalMap, JTOOLS::JMapList< JTOOLS::JPolint0FunctionalGridMap, JTOOLS::JMapList< JTOOLS::JPolint0FunctionalGridMap > > > > JNPEMaplist_t
Definition of zero value for any class.
static const JFIT::JPDFType_t pdf_t[NUMBER_OF_PDFS]
JTOOLS::JMapList< JTOOLS::JPolint1FunctionalMapH, JTOOLS::JMapList< JTOOLS::JPolint1FunctionalMap, JTOOLS::JMapList< JTOOLS::JPolint1FunctionalGridMap, JTOOLS::JMapList< JTOOLS::JPolint1FunctionalGridMap > > > > JNPEMaplist_t
double getDY() const
Get y direction.
Definition: JVersor3Z.hh:156
Various implementations of functional maps.
JRange< double > JTimeRange
Type definition for time range.
esac $JPP_DIR examples JDetector JTransitTime o $OUTPUT_FILE n N $NPE T $TTS_NS d $DEBUG for HISTOGRAM in tts tt2 pmt
Definition: JTransitTime.sh:36
Numbering scheme for PDF types.
double operator()(const JShower3EZ &shower, const JPMTW0 &pmt) const
Fit function.
Type definition of a 1st degree polynomial interpolation based on a JGridMap implementation.
Constants.
Data structure for fit of straight line in positive z-direction with energy.
Definition: JShower3EZ.hh:25
The template JSharedPointer class can be used to share a pointer to an object.
Data structure for vector in three dimensions.
Definition: JVector3D.hh:33
Map list.
Definition: JMapList.hh:24
JPHYSICS::JNPETable< double, double, JNPEMaplist_t > JNPE_t
Type definition of a 1st degree polynomial interpolation based on a JMap implementation.
static double Vmax_npe
Maximal integral of PDF [npe].
return result
Definition: JPolint.hh:695
then print_variable DETECTOR INPUT_FILE INTERMEDIATE_FILE check_input_file $DETECTOR $INPUT_FILE check_output_file $INTERMEDIATE_FILE $OUTPUT_FILE JMCEvt f $INPUT_FILE o $INTERMEDIATE_FILE d
Definition: JPath.sh:52
#define NOTICE(A)
Definition: JMessage.hh:64
direct light from EM shower
Definition: JPDFTypes.hh:40
JTOOLS::JMapList< JTOOLS::JPolint0FunctionalMap, JTOOLS::JMapList< JTOOLS::JPolint0FunctionalMap, JTOOLS::JMapList< JTOOLS::JPolint0FunctionalGridMap, JTOOLS::JMapList< JTOOLS::JPolint0FunctionalGridMap > > > > JPDFMaplist_t
const JPosition3D & getPosition() const
Get position.
Definition: JPosition3D.hh:129
JPDFType_t
PDF types.
Definition: JPDFTypes.hh:27
JLANG::JSharedPointer< JMEstimator > estimator
M-Estimator function.
then usage $script[distance] fi case set_variable R
Definition: JDrawLED.sh:40
General purpose messaging.
const JVersor3Z & getDirection() const
Get direction.
Definition: JVersor3Z.hh:79
#define FATAL(A)
Definition: JMessage.hh:67
Fit method based on the Levenberg-Marquardt method.
Definition: JGandalf.hh:52
JNPE_t::result_type getH1(const double D, const double cosDelta, const double theta, const double phi, const double E) const
Get signal hypothesis value for time integrated PDF.
JRegressor(const std::string &fileDescriptor)
Parameterized constructor.
int getN() const
Get number of hits.
Definition: JPMTW0.hh:67
Auxiliary class to define a range between two values.
double getRmax() const
Get maximal road width of PDF.
Simple fit method based on Powell&#39;s algorithm, see reference: Numerical Recipes in C++...
Definition: JSimplex.hh:42
double getDX() const
Get x direction.
Definition: JVersor3Z.hh:145
Type definition of a zero degree polynomial interpolation based on a JGridMap implementation.
result_type operator()(const JShower3EZ &shower, const JPMTW0 &pmt) const
Fit function.
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:88
static double Vmax_npe
Maximal integral of PDF [npe].
JLANG::JSharedPointer< JMEstimator > estimator
M-Estimator function.
static JTimeRange T_ns
Time window with respect to Cherenkov hypothesis [ns].
JNPE_t::result_type getH0(const double R_Hz) const
Get background hypothesis value for time integrated PDF.
Data structure for position in three dimensions.
Definition: JPosition3D.hh:35
virtual const char * what() const
Get error message.
Definition: JException.hh:48
Data structure for normalised vector in three dimensions.
Definition: JVersor3D.hh:23
double u[N+1]
Definition: JPolint.hh:706
double getChi2(const double P)
Get chi2 corresponding to given probability.
Definition: JFitToolkit.hh:79
Data structure for normalised vector in positive z-direction.
Definition: JVersor3Z.hh:37
JPHYSICS::JPDFTable< JFunction1D_t, JPDFMaplist_t > JPDF_t
Abstract class for global fit method.
Definition: JRegressor.hh:75
JResultEvaluator< JResult_t >::result_type get_value(const JResult_t &value)
Helper method to recursively evaluate a to function value.
Definition: JResult.hh:936
static JTimeRange T_ns
Time window with respect to Cherenkov hypothesis [ns].
JNPE_t::result_type getH0(const double R_Hz) const
Get background hypothesis value for time integrated PDF.
Maximum likelihood estimator (M-estimators).
Type definition of a zero degree polynomial interpolation based on a JMap implementation.
then usage $script[input file[working directory[option]]] nWhere option can be E
Definition: JMuonPostfit.sh:37
JPHYSICS::JNPETable< double, double, JNPEMaplist_t > JNPE_t
JRegressor(const std::string &fileDescriptor)
Parameterized constructor.