Jpp  17.1.1
the software that should make you happy
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
Functions
JSimplexFitArray.cc File Reference

Program to test JFIT::JSimplex algorithm. More...

#include <string>
#include <iostream>
#include <iomanip>
#include <vector>
#include <cmath>
#include "TROOT.h"
#include "TFile.h"
#include "TH1D.h"
#include "TRandom3.h"
#include "TMath.h"
#include "JFit/JSimplex.hh"
#include "JTools/JArray.hh"
#include "JTools/JArrayIterator.hh"
#include "JTools/JGrid.hh"
#include "JTools/JQuantile.hh"
#include "JROOT/JRootToolkit.hh"
#include "Jeep/JPrint.hh"
#include "Jeep/JParser.hh"
#include "Jeep/JMessage.hh"

Go to the source code of this file.

Functions

int main (int argc, char **argv)
 

Detailed Description

Program to test JFIT::JSimplex algorithm.

Author
mdejong

Definition in file JSimplexFitArray.cc.

Function Documentation

int main ( int  argc,
char **  argv 
)

Definition at line 160 of file JSimplexFitArray.cc.

161 {
162  using namespace std;
163  using namespace JPP;
164 
165  typedef JArray<2, double> array_type;
166 
167  string outputFile;
168  int numberOfEvents;
169  array_type parameters;
170  UInt_t seed;
171  int debug;
172 
173  try {
174 
175  JParser<> zap("Program to test JSimplex algorithm.");
176 
177  zap['o'] = make_field(outputFile) = "simplex.root";
178  zap['n'] = make_field(numberOfEvents);
179  zap['M'] = make_field(parameters, "model parameters") = array_type(100.0, 1.0);
180  zap['S'] = make_field(seed) = 0;
181  zap['d'] = make_field(debug) = 2;
182 
183  zap(argc, argv);
184  }
185  catch(const exception& error) {
186  FATAL(error.what() << endl);
187  }
188 
189 
190  gRandom->SetSeed(seed);
191 
192 
193  const double top = parameters[0];
194  const double sigma = parameters[1];
195 
196  model_type model;
197 
198  model[0] = top;
199 
200  for (int i = 0; i != NUMBER_OF_DIMENSIONS; ++i) {
201  model[i*2 + 1] = gRandom->Uniform(-sigma, +sigma);
202  model[i*2 + 2] = gRandom->Gaus(sigma, 0.2*sigma);
203  }
204 
205  const JGrid<double> grid(11, -3.0*sigma, +3.0*sigma);
206 
207 
208  JSimplex<model_type> simplex;
209 
211  JSimplex<model_type>::MAXIMUM_ITERATIONS = 10000;
212 
213  double (*fit)(const model_type&, const hit_type&) = likelihood; // fit function
214 
215 
216  vector<TH1D*> H1;
217 
218  TH1D h0("chi2/NDF", NULL, 200, 0.0, 10.0);
219 
220  H1.push_back(new TH1D(MAKE_CSTRING("p" << 0), NULL, 101, -20.0, +20.0));
221 
222  for (size_t i =1; i != model.size(); ++i) {
223  H1.push_back(new TH1D(MAKE_CSTRING("p" << i), NULL, 101, -0.1, +0.1));
224  }
225 
226 
227  for (int counter = 0; counter != numberOfEvents; ++counter) {
228 
229  STATUS("event: " << setw(10) << counter << '\r'); DEBUG(endl);
230 
231 
232  // generate data
233 
234  vector<hit_type> data;
235 
236  for (JArrayIterator<NUMBER_OF_DIMENSIONS, double> g1(grid); g1; ++g1) {
237 
238  const JArray<NUMBER_OF_DIMENSIONS, double> p1 = *g1;
239 
240  const double value = gRandom->Poisson(model(p1));
241  const double sigma = (value > 1.0 ? sqrt(value) : 0.7);
242 
243  data.push_back(hit_type(p1, value, sigma));
244  }
245 
246 
247  // start values
248 
249  JQuantile Q[NUMBER_OF_DIMENSIONS];
250 
251  double value = 0.0;
252 
253  for (const auto& hit : data) {
254 
255  for (int i = 0; i != NUMBER_OF_DIMENSIONS; ++i) {
256  Q[i].put(hit[i], hit.value);
257  }
258 
259  if (hit.value > value) {
260 
261  simplex.value[0] = hit.value;
262 
263  for (int i = 0; i != NUMBER_OF_DIMENSIONS; ++i) {
264  simplex.value[2*i + 1] = hit[i];
265  }
266 
267  value = hit.value;
268  }
269  }
270 
271  for (int i = 0; i != NUMBER_OF_DIMENSIONS; ++i) {
272  simplex.value[2*i + 2] = 1.0 * Q[i].getSTDev();
273  }
274 
275 
276  // steps
277 
278  simplex.step.resize(2*NUMBER_OF_DIMENSIONS + 1);
279 
280  model_type step;
281 
282  simplex.step[0] = model_type();
283  simplex.step[0][0] = 1.0;
284 
285  for (int i = 0; i != NUMBER_OF_DIMENSIONS; ++i) {
286 
287  simplex.step[2*i + 1] = model_type();
288  simplex.step[2*i + 1][2*i + 1] = 0.1;
289 
290  simplex.step[2*i + 2] = model_type();
291  simplex.step[2*i + 2][2*i + 2] = 0.1;
292  }
293 
294  DEBUG("start values " << simplex.value << endl);
295 
296  for (size_t i = 0; i != simplex.step.size(); ++i) {
297  DEBUG("step: " << setw(2) << i << ' ' << simplex.step[i] << endl);
298  }
299 
300 
301  // fit
302 
303  const double chi2 = simplex(fit, data.begin(), data.end());
304  const int ndf = data.size() - simplex.step.size();
305 
306 
307  h0.Fill(chi2 / ndf);
308 
309  for (size_t i = 0; i != model.size(); ++i) {
310  H1[i]->Fill(model[i] - simplex.value[i]);
311  }
312 
313  DEBUG("chi2 / NDF" << FIXED(12,3) << chi2 << " / " << ndf << endl);
314 
315  DEBUG("final values " << simplex.value << endl);
316  DEBUG("model values " << model << endl);
317 
318  if (debug >= debug_t) {
319 
320  for (const auto& hit : data) {
321 
322  cout << "hit: ";
323 
324  for (int i = 0; i != NUMBER_OF_DIMENSIONS; ++i) {
325  cout << FIXED(7,3) << hit[i] << ' ';
326  }
327 
328  const double value = simplex.value(hit);
329 
330  cout << FIXED(7,3) << hit.value << ' '
331  << FIXED(7,3) << value << ' '
332  << FIXED(7,3) << (hit.value - value) / hit.sigma << endl;
333  }
334  }
335 
336  }
337  STATUS(endl);
338 
339 
340  if (outputFile != "") {
341 
342  TFile out(outputFile.c_str(), "recreate");
343 
344  out << h0;
345 
346  for (size_t i = 0; i != sizeof(H1)/sizeof(H1[0]); ++i) {
347  out << *(H1[i]);
348  }
349 
350  out.Write();
351  out.Close();
352  }
353 }
Utility class to parse command line options.
Definition: JParser.hh:1517
Q(UTCMax_s-UTCMin_s)-livetime_s
debug
Definition: JMessage.hh:29
TPaveText * p1
#define STATUS(A)
Definition: JMessage.hh:63
*fatal Wrong number of arguments esac JCookie sh typeset Z DETECTOR typeset Z SOURCE_RUN typeset Z TARGET_RUN set_variable PARAMETERS_FILE $WORKDIR parameters
Definition: diff-Tuna.sh:38
#define MAKE_CSTRING(A)
Make C-string.
Definition: JPrint.hh:136
Auxiliary data structure for floating point format specification.
Definition: JManip.hh:446
string outputFile
const double sigma[]
Definition: JQuadrature.cc:74
#define make_field(A,...)
macro to convert parameter to JParserTemplateElement object
Definition: JParser.hh:1993
#define FATAL(A)
Definition: JMessage.hh:67
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
int debug
debug level
#define DEBUG(A)
Message macros.
Definition: JMessage.hh:62
Double_t g1(const Double_t x)
Function.
Definition: JQuantiles.cc:25