Jpp  18.2.1
the software that should make you happy
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
JGradientFitToGauss.cc
Go to the documentation of this file.
1 #include <string>
2 #include <iostream>
3 #include <iomanip>
4 #include <vector>
5 #include <cmath>
6 
7 #include "TRandom3.h"
8 
9 #include "JFit/JGradient.hh"
10 #include "JTools/JElement.hh"
11 #include "JTools/JQuantile.hh"
12 #include "JMath/JGauss.hh"
13 #include "JMath/JConstants.hh"
14 
15 #include "Jeep/JPrint.hh"
16 #include "Jeep/JParser.hh"
17 #include "Jeep/JMessage.hh"
18 
19 namespace {
20 
21  using namespace JPP;
22 
23 
24  /**
25  * Fit object.
26  */
27  JGauss fit;
28 
29 
30  /**
31  * Data.
32  */
33  typedef JElement2D<double, double> element_type;
35 
36  buffer_type buffer;
37 
38 
39  /**
40  * Fit function.
41  *
42  * \param gauss gauss
43  * \param point point
44  * \return chi2
45  */
46  inline double g1(const JGauss& gauss, const element_type& point)
47  {
48  const double u = (point.getX() - gauss.mean) / gauss.sigma;
49  const double fs = gauss.signal * exp(-0.5*u*u) / (sqrt(2.0*PI) * gauss.sigma);
50  const double fb = gauss.background;
51  const double f1 = fs + fb;
52 
53  const double y = point.getY();
54  const double dy = (f1 - y) * (f1 - y);
55 
56  if (y > 0.0)
57  return dy / y;
58  else
59  return f1;
60  }
61 
62 
63  /**
64  * Get chi2.
65  *
66  * \param option option
67  * \return chi2
68  */
69  double getChi2(const int option)
70  {
71  double chi2 = 0.0;
72 
73  for (buffer_type::const_iterator i = buffer.begin(); i != buffer.end(); ++i) {
74  chi2 += g1(fit, *i);
75  }
76 
77  return chi2;
78  }
79 
80 
81  /**
82  * Model specific fit parameter.
83  */
84  struct JGaussEditor :
85  public JParameter_t
86  {
87  /**
88  * Constructor.
89  *
90  * \param g1 fit object
91  * \param g2 step
92  */
93  JGaussEditor(JGauss& g1, const JGauss& g2) :
94  g1(g1),
95  g2(g2)
96  {}
97 
98 
99  /**
100  * Apply step.
101  *
102  * \param step step
103  */
104  virtual void apply(const double step) override
105  {
106  g1.add(g2 * step);
107  }
108 
109  JGauss& g1;
110  JGauss g2;
111  };
112 }
113 
114 
115 /**
116  * \file
117  *
118  * Program to test JFIT::JGradient algorithm.
119  * \author mdejong
120  */
121 int main(int argc, char **argv)
122 {
123  using namespace std;
124  using namespace JPP;
125 
126  int numberOfEvents;
127  JGauss gauss;
128  JGauss precision;
129  UInt_t seed;
130  int debug;
131 
132  try {
133 
134  JParser<> zap("Program to test JGradient algorithm.");
135 
136  zap['n'] = make_field(numberOfEvents) = 0;
137  zap['@'] = make_field(gauss) = JGauss(0.0, 1.0, 1000.0, 10.0);
138  zap['e'] = make_field(precision) = JGauss(0.05, 0.05, 25.0, 25.0);
139  zap['S'] = make_field(seed) = 0;
140  zap['d'] = make_field(debug) = 3;
141 
142  zap(argc, argv);
143  }
144  catch(const exception& error) {
145  FATAL(error.what() << endl);
146  }
147 
148  gRandom->SetSeed(seed);
149 
150  if (numberOfEvents == 0) {
151 
152  buffer.push_back(element_type(-3.00000, 16.00000));
153  buffer.push_back(element_type(-2.50000, 22.00000));
154  buffer.push_back(element_type(-2.00000, 70.00000));
155  buffer.push_back(element_type(-1.50000, 152.00000));
156  buffer.push_back(element_type(-1.00000, 261.00000));
157  buffer.push_back(element_type(-0.50000, 337.00000));
158  buffer.push_back(element_type( 0.00000, 378.00000));
159  buffer.push_back(element_type( 0.50000, 360.00000));
160  buffer.push_back(element_type( 1.00000, 253.00000));
161  buffer.push_back(element_type( 1.50000, 129.00000));
162  buffer.push_back(element_type( 2.00000, 72.00000));
163  buffer.push_back(element_type( 2.50000, 22.00000));
164  buffer.push_back(element_type( 3.00000, 11.00000));
165 
166  JGradient gradient(1000000, 0, 1.0e-4, debug);
167 
168  // start values
169 
170  fit = JGauss(0.5, 0.5, 700.0, 0.0);
171 
172  gradient.push_back(JModifier_t("mean", new JGaussEditor(fit, JGauss(1.0, 0.0, 0.0, 0.0)), 1.0e-2));
173  gradient.push_back(JModifier_t("sigma", new JGaussEditor(fit, JGauss(0.0, 1.0, 0.0, 0.0)), 1.0e-2));
174  gradient.push_back(JModifier_t("signal", new JGaussEditor(fit, JGauss(0.0, 0.0, 1.0, 0.0)), 5.0e-0));
175  gradient.push_back(JModifier_t("background", new JGaussEditor(fit, JGauss(0.0, 0.0, 0.0, 1.0)), 5.0e-1));
176 
177  gradient(getChi2);
178 
179  } else {
180 
181  JQuantile Q[] = { JQuantile("mean "),
182  JQuantile("sigma "),
183  JQuantile("signal "),
184  JQuantile("background") };
185 
186  const size_t nx = 21;
187  const double xmin = -5.0;
188  const double xmax = +5.0;
189 
190  for (int i = 0; i != numberOfEvents; ++i) {
191 
192  STATUS("event: " << setw(10) << i << '\r'); DEBUG(endl);
193 
194  buffer.clear();
195 
196  for (double x = xmin, dx = (xmax - xmin) / (nx - 1); x < xmax + 0.5*dx; x += dx) {
197 
198  const double value = gauss(x);
199 
200  buffer.push_back(element_type(x, gRandom->Poisson(value)));
201  }
202 
203  JGradient gradient(1000000, 0, 1.0e-4, debug);
204 
205  // start values
206 
207  fit = JGauss(0.5, 0.5, 700.0, 0.0);
208 
209  gradient.push_back(JModifier_t("mean", new JGaussEditor(fit, JGauss(1.0, 0.0, 0.0, 0.0)), 1.0e-2));
210  gradient.push_back(JModifier_t("sigma", new JGaussEditor(fit, JGauss(0.0, 1.0, 0.0, 0.0)), 1.0e-2));
211  gradient.push_back(JModifier_t("signal", new JGaussEditor(fit, JGauss(0.0, 0.0, 1.0, 0.0)), 5.0e-0));
212  gradient.push_back(JModifier_t("background", new JGaussEditor(fit, JGauss(0.0, 0.0, 0.0, 1.0)), 5.0e-1));
213 
214  const double chi2 = gradient(getChi2);
215 
216  DEBUG("Final value " << fit << endl);
217  DEBUG("Chi2 " << chi2 << endl);
218 
219  Q[0].put(fit.mean - gauss.mean);
220  Q[1].put(fit.sigma - gauss.sigma);
221  Q[2].put(fit.signal - gauss.signal);
222  Q[3].put(fit.background - gauss.background);
223  }
224 
225  for (int i = 0; i != sizeof(Q)/sizeof(Q[0]); ++i) {
226  NOTICE((i == 0 ? longprint : shortprint) << Q[i]);
227  }
228 
229  for (int i = 0; i != sizeof(Q)/sizeof(Q[0]); ++i) {
230  ASSERT(fabs(Q[i].getMean()) < Q[i].getSTDev());
231  }
232 
233  ASSERT(Q[0].getSTDev() < precision.mean);
234  ASSERT(Q[1].getSTDev() < precision.sigma);
235  ASSERT(Q[2].getSTDev() < precision.signal);
236  ASSERT(Q[3].getSTDev() < precision.background);
237  }
238 
239  return 0;
240 }
const double xmax
Definition: JQuadrature.cc:24
Utility class to parse command line options.
Definition: JParser.hh:1514
Q(UTCMax_s-UTCMin_s)-livetime_s
double mean
Definition: JGauss.hh:161
int main(int argc, char *argv[])
Definition: Main.cc:15
The elements in a collection are sorted according to their abscissa values and a given distance opera...
#define STATUS(A)
Definition: JMessage.hh:63
double getMean(vector< double > &v)
get mean of vector content
std::vector< JHitW0 > buffer_type
hits
Definition: JPerth.cc:67
Gauss function object.
Definition: JGauss.hh:173
const JPolynome f1(1.0, 2.0, 3.0)
Function.
#define ASSERT(A,...)
Assert macro.
Definition: JMessage.hh:90
I/O formatting auxiliaries.
Mathematical constants.
#define make_field(A,...)
macro to convert parameter to JParserTemplateElement object
Definition: JParser.hh:1989
#define NOTICE(A)
Definition: JMessage.hh:64
static const double PI
Mathematical constants.
std::ostream & shortprint(std::ostream &out)
Set short printing.
Definition: JManip.hh:144
General purpose messaging.
std::ostream & longprint(std::ostream &out)
Set long printing.
Definition: JManip.hh:172
#define FATAL(A)
Definition: JMessage.hh:67
Auxiliary data structure for editable parameter.
Definition: JGradient.hh:47
double signal
Definition: JGauss.hh:163
const double xmin
Definition: JQuadrature.cc:23
Auxiliary data structure for average.
Definition: JKatoomba_t.hh:76
Utility class to parse command line options.
double background
Definition: JGauss.hh:164
then set_variable NUMBER_OF_TESTS else set_variable NUMBER_OF_TESTS fi function gauss()
double sigma
Definition: JGauss.hh:162
void put(const double x)
Put value.
Definition: JKatoomba_t.hh:101
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
2D Element.
Definition: JElement.hh:46
double u[N+1]
Definition: JPolint.hh:865
double getChi2(const double P)
Get chi2 corresponding to given probability.
Definition: JFitToolkit.hh:56
Conjugate gradient fit.
Definition: JGradient.hh:73
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
int debug
debug level
#define DEBUG(A)
Message macros.
Definition: JMessage.hh:62
Auxiliary class for fit parameter with optional limits.
Definition: JFitK40.hh:107
Double_t g1(const Double_t x)
Function.
Definition: JQuantiles.cc:25