Jpp  15.0.3
the software that should make you happy
 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 "JPhysics/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::JShower3EZ.
38  * \author mdejong
39  */
40 
41 namespace JFIT {
42 
46  using JTOOLS::get_value;
47 
48  /**
49  * Regressor function object for JShower3EZ fit using JSimplex minimiser.
50  */
51  template<>
53  public JAbstractRegressor<JShower3EZ, JSimplex>
54  {
56 
63 
69 
70  /**
71  * Parameterized constructor
72  *
73  * The PDF file descriptor should contain the wild card character JPHYSICS::WILD_CARD which
74  * will be replaced by the corresponding PDF types listed in JRegressor<JShower3Z, JGandalf>::pdf_t.
75  *
76  * \param fileDescriptor PDF file descriptor
77  */
78 
79  JRegressor(const std::string& fileDescriptor):
80  estimator(new JMEstimatorNull())
81  {
82  using namespace std;
83  using namespace JPP;
84 
85  const JPDF_t::JSupervisor supervisor(new JPDF_t::JDefaultResult(JMATH::zero));
86 
87  for (int i = 0; i != NUMBER_OF_PDFS; ++i) {
88 
89  try {
90 
91  JPDF_t pdf;
92 
93  const string file_name = getFilename(fileDescriptor, pdf_t[i]);
94 
95  NOTICE("loading PDF from file " << file_name << "... " << flush);
96 
97  pdf.load(file_name.c_str());
98 
99  NOTICE("OK" << endl);
100 
101  pdf.setExceptionHandler(supervisor);
102 
103  npe[ i ] = JNPE_t(pdf);
104  }
105  catch(const JException& error) {
106  FATAL(error.what() << endl);
107  }
108  }
109 
110  // Add PDFs
111  for (int i = 1; i < NUMBER_OF_PDFS; i += 2) {
112 
113  npe[ i ].add(npe[i-1]);
114 
115  JNPE_t buffer;
116 
117  npe[i-1].swap(buffer);
118  }
119  }
120 
121  /**
122  * Fit function.
123  * This method is used to determine the chi2 of given PMT with respect to shower hypothesis.
124  *
125  * \param shower shower
126  * \param pmt pmt
127  * \return chi2
128  */
129  double operator()(const JShower3EZ& shower, const JPMTW0& pmt) const
130  {
131  using namespace JPP;
132 
133  JPosition3D D(pmt.getPosition());
134  JDirection3D U(pmt.getDirection());
135 
136  D.sub(shower.getPosition());
137 
138  JVersor3D shower_dir(shower.getDirection());
139 
140  const double z = D.getDot(shower_dir);
141  const double x = D.getX() - z * shower.getDX();
142  const double y = D.getY() - z * shower.getDY();
143  const double cosDelta = z/D.getLength(); // Delta = angle between shower direction and PMT position
144 
145  U.rotate(JRotation3Z(-atan2(y,x))); // rotate PMT axis to x-z plane
146 
147  const double theta = U.getTheta();
148  const double phi = fabs(U.getPhi());
149 
150  JNPE_t::result_type H0 = getH0(pmt.getR()); // getH0 = Get background hypothesis value for time integrated PDF.
151  JNPE_t::result_type H1 = getH1(D.getLength(), cosDelta, theta, phi, shower.getE()); // getH1 = Get signal hypothesis value for time integrated PDF.
152 
153  if (get_value(H1) >= Vmax_npe) {
154  H1 *= Vmax_npe / get_value(H1);
155  }
156 
157  H1 += H0; // now H1 is signal + background
158 
159  const bool hit = pmt.getN() != 0;
160  const double u = getChi2(get_value(H1), hit); // -log(lik)
161 
162  return estimator->getRho(u);
163  }
164 
165  /**
166  * Get background hypothesis value for time integrated PDF.
167  *
168  * \param R_Hz rate [Hz]
169  * \return hypothesis value
170  */
171  JNPE_t::result_type getH0(const double R_Hz) const
172  {
173  return JNPE_t::result_type(R_Hz * 1e-9 * T_ns.getLength());
174  }
175 
176  /**
177  * Get signal hypothesis value for time integrated PDF.
178  *
179  * \param D PMT distance from shower [m]
180  * \param cosDelta angle between shower direction and PMT position
181  * \param theta PMT zenith angle [deg]
182  * \param phi PMT azimuth angle [deg]
183  * \param E shower energy [GeV]
184  * \return hypothesis value
185  */
187  const double cosDelta,
188  const double theta,
189  const double phi,
190  const double E) const
191  {
193 
194  for (int i = 0; i != NUMBER_OF_PDFS; ++i) {
195 
196  if (!npe[i].empty() && D <= npe[i].getXmax()) {
197 
198  try {
199 
200  JNPE_t::result_type y1 = E * npe[i](std::max(D, npe[i].getXmin()), cosDelta, theta, phi);
201 
202  // safety measure
203 
204  if(y1 < 0){
205  y1 = 0;
206  }
207 
208  h1 += y1;
209 
210  }
211  catch(JLANG::JException& error) {
212  ERROR(error << std::endl);
213  }
214  }
215  }
216 
217  return h1;
218  }
219 
220 
221  static JTimeRange T_ns; //!< Time window with respect to Cherenkov hypothesis [ns]
222  static double Vmax_npe; //!< Maximal integral of PDF [npe]
223 
224  static const int NUMBER_OF_PDFS = 2;
225 
226  static const JPDFType_t pdf_t[NUMBER_OF_PDFS];
227 
228  JNPE_t npe[NUMBER_OF_PDFS]; //!< PDF
229 
231  };
232 
233 
234  /**
235  * Regressor function object for JShower3EZ fit using JGandalf minimiser.
236  */
237  template<>
239  public JAbstractRegressor<JShower3EZ, JGandalf>
240  {
242 
249 
255 
256  /**
257  * Parameterized constructor
258  *
259  * The PDF file descriptor should contain the wild card character JPHYSICS::WILD_CARD which
260  * will be replaced by the corresponding PDF types listed in JRegressor<JShower3Z, JGandalf>::pdf_t.
261  *
262  * \param fileDescriptor PDF file descriptor
263  */
264 
265  JRegressor(const std::string& fileDescriptor):
266  estimator(new JMEstimatorNull())
267  {
268  using namespace std;
269  using namespace JPP;
270 
271  const JPDF_t::JSupervisor supervisor(new JPDF_t::JDefaultResult(JMATH::zero));
272 
273  for (int i = 0; i != NUMBER_OF_PDFS; ++i) {
274 
275  try {
276 
277  JPDF_t pdf;
278 
279  const string file_name = getFilename(fileDescriptor, pdf_t[i]);
280 
281  NOTICE("loading PDF from file " << file_name << "... " << flush);
282 
283  pdf.load(file_name.c_str());
284 
285  NOTICE("OK" << endl);
286 
287  pdf.setExceptionHandler(supervisor);
288 
289  npe[i] = JNPE_t(pdf);
290  }
291  catch(const JException& error) {
292  FATAL(error.what() << endl);
293  }
294  }
295 
296  // Add PDFs
297  for (int i = 1; i < NUMBER_OF_PDFS; i += 2) {
298 
299  npe[ i ].add(npe[i-1]);
300 
301  JNPE_t buffer;
302 
303  npe[i-1].swap(buffer);
304  }
305  }
306 
307  /**
308  * Fit function.
309  * This method is used to determine the chi2 of given PMT with respect to shower hypothesis.
310  *
311  * \param shower shower
312  * \param pmt pmt
313  * \return chi2
314  */
315  result_type operator()(const JShower3EZ& shower, const JPMTW0& pmt) const
316  {
317  using namespace JPP;
318  using namespace std;
319 
320  JPosition3D D(pmt.getPosition());
321  JDirection3D U(pmt.getDirection());
322 
323  D.sub(shower.getPosition());
324 
325  JVersor3D shower_dir(shower.getDirection());
326 
327  const double z = D.getDot(shower_dir);
328  const double x = D.getX() - z * shower.getDX();
329  const double y = D.getY() - z * shower.getDY();
330  const double R2 = D.getLengthSquared() - z*z;
331  const double R = sqrt(R2);
332  const double d = D.getLength();
333  const double cos_th0 = z/d; // theta0 = angle between shower direction and PMT position
334 
335  U.rotate(JRotation3Z(-atan2(y,x))); // rotate PMT axis to x-z plane
336 
337  const double theta = U.getTheta();
338  const double phi = fabs(U.getPhi());
339 
340  JNPE_t::result_type H0 = getH0(pmt.getR()); // getH0 = Get background hypothesis value for time integrated PDF.
341  JNPE_t::result_type H1 = getH1(d, cos_th0, theta, phi, shower.getE()); // getH1 = Get signal hypothesis value for time integrated PDF.
342 
343  if (get_value(H1) >= Vmax_npe) {
344  H1 *= Vmax_npe / get_value(H1);
345  }
346 
347  H1 += H0; // now H1 is signal + background
348 
349  const bool hit = pmt.getN() != 0;
350  const double u = H1.getChi2(hit);
351 
353 
354  result.chi2 = estimator->getRho(u);
355 
356  result.gradient = JShower3EZ(JPoint4D(JVector3D(x*z / (d*d*d), // d(cos_th0)/d(x)
357  y*z / (d*d*d), // d(cos_th0)/d(y)
358  -(R*R) / (d*d*d)), // d(cos_th0)/d(z)
359  0.0), // d(cos_th0)/d(t)
360  JVersor3Z(x*z*z / (d*R), // d(cos_th0)/d(dx)
361  y*z*z / (d*R)), // d(cos_th0)/d(dy)
362  0.0); // d(cos_th0)/d(E)
363 
364  result.gradient.mul(estimator->getPsi(u));
365  result.gradient.mul(H1.getDerivativeOfChi2(hit)); // x d(chi2)/d(cos_th0)
366 
367  return result;
368  }
369 
370  /**
371  * Get background hypothesis value for time integrated PDF.
372  *
373  * \param R_Hz rate [Hz]
374  * \return hypothesis value
375  */
376  JNPE_t::result_type getH0(const double R_Hz) const
377  {
378  return JNPE_t::result_type(R_Hz * 1e-9 * T_ns.getLength(), 0.0);
379  }
380 
381  /**
382  * Get signal hypothesis value for time integrated PDF.
383  *
384  * \param D PMT distance from shower [m]
385  * \param cosDelta angle between shower direction and PMT position
386  * \param theta PMT zenith angle [deg]
387  * \param phi PMT azimuth angle [deg]
388  * \param E shower energy [GeV]
389  * \return hypothesis value
390  */
392  const double cosDelta,
393  const double theta,
394  const double phi,
395  const double E) const
396  {
398 
399  for (int i = 0; i != NUMBER_OF_PDFS; ++i) {
400 
401  if (!npe[i].empty() && D <= npe[i].getXmax()) {
402 
403  try {
404 
405  JNPE_t::result_type y1 = E * npe[i](std::max(D, npe[i].getXmin()), cosDelta, theta, phi);
406 
407  if (get_value(y1) > 0.0) {
408  h1 += y1;
409  }
410 
411  }
412  catch(JLANG::JException& error) {
413  ERROR(error << std::endl);
414  }
415  }
416  }
417 
418  return h1;
419  }
420 
421 
422  static JTimeRange T_ns; //!< Time window with respect to Cherenkov hypothesis [ns]
423  static double Vmax_npe; //!< Maximal integral of PDF [npe]
424 
425  static const int NUMBER_OF_PDFS = 2;
426 
427  static const JFIT::JPDFType_t pdf_t[NUMBER_OF_PDFS];
428 
429  JNPE_t npe[NUMBER_OF_PDFS]; //!< PDF
430 
432  };
433 
434  /**
435  * PDF types.
436  */
439 
442 
443  /**
444  * Default values.
445  */
447  double JRegressor<JShower3EZ, JSimplex>::Vmax_npe = std::numeric_limits<double>::max();
448 
450  double JRegressor<JShower3EZ, JGandalf>::Vmax_npe = std::numeric_limits<double>::max();
451 }
452 
453 #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:170
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.
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::JRange< double > JTimeRange
Type definition for time range (unit [ns]).
JTOOLS::JMapList< JTOOLS::JPolint0FunctionalMap, JTOOLS::JMapList< JTOOLS::JPolint0FunctionalMap, JTOOLS::JMapList< JTOOLS::JPolint0FunctionalGridMap, JTOOLS::JMapList< JTOOLS::JPolint0FunctionalGridMap > > > > JPDFMaplist_t
then for HISTOGRAM in h0 h1
Definition: JMatrixNZ.sh:71
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]
then usage E
Definition: JMuonPostfit.sh:35
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:158
Various implementations of functional maps.
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.
Data structure for fit of straight line in positive z-direction with energy.
Definition: JShower3EZ.hh:28
The template JSharedPointer class can be used to share a pointer to an object.
Data structure for vector in three dimensions.
Definition: JVector3D.hh:34
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:727
#define NOTICE(A)
Definition: JMessage.hh:64
Physics constants.
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:130
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:43
General purpose messaging.
const JVersor3Z & getDirection() const
Get direction.
Definition: JVersor3Z.hh:81
#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.
void setExceptionHandler(const typename function_type::supervisor_type &supervisor)
Set the supervisor for handling of exceptions.
virtual const char * what() const override
Get error message.
Definition: JException.hh:48
then JMuonMCEvt f $INPUT_FILE o $INTERMEDIATE_FILE d
Definition: JMuonPath.sh:47
int getN() const
Get number of hits.
Definition: JPMTW0.hh:67
Auxiliary class to define a range between two values.
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:147
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:36
Data structure for normalised vector in three dimensions.
Definition: JVersor3D.hh:26
double u[N+1]
Definition: JPolint.hh:739
double getChi2(const double P)
Get chi2 corresponding to given probability.
Definition: JFitToolkit.hh:56
Data structure for normalised vector in positive z-direction.
Definition: JVersor3Z.hh:39
JPHYSICS::JPDFTable< JFunction1D_t, JPDFMaplist_t > JPDF_t
Abstract class for global fit method.
Definition: JRegressor.hh:75
do echo Generating $dir eval D
Definition: JDrawLED.sh:53
JResultEvaluator< JResult_t >::result_type get_value(const JResult_t &value)
Helper method to recursively evaluate a to function value.
Definition: JResult.hh:998
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.
JPHYSICS::JNPETable< double, double, JNPEMaplist_t > JNPE_t
JRegressor(const std::string &fileDescriptor)
Parameterized constructor.