Jpp 20.0.0-27-g39925593c-D
the software that should make you happy
Loading...
Searching...
No Matches
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/JStats.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

◆ main()

int main ( int argc,
char ** argv )

Definition at line 160 of file JSimplexFitArray.cc.

161{
162 using namespace std;
163 using namespace JPP;
164
166
167 string outputFile;
168 int numberOfEvents;
169 array_type parameters;
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 DEBUG("model values " << model << endl);
206
207 const JGrid<double> grid(11, -3.0*sigma, +3.0*sigma);
208
209
210 JSimplex<model_type> simplex;
211
212 JSimplex<model_type>::debug = debug;
213 JSimplex<model_type>::MAXIMUM_ITERATIONS = 10000;
214
215 double (*fit)(const model_type&, const hit_type&) = likelihood; // fit function
216
217
219
220 TH1D h0("chi2/NDF", NULL, 200, 0.0, 10.0);
221
222 H1.push_back(new TH1D(MAKE_CSTRING("p" << 0), NULL, 101, -20.0, +20.0));
223
224 for (size_t i =1; i != model.size(); ++i) {
225 H1.push_back(new TH1D(MAKE_CSTRING("p" << i), NULL, 101, -0.1, +0.1));
226 }
227
228
229 for (int counter = 0; counter != numberOfEvents; ++counter) {
230
231 STATUS("event: " << setw(10) << counter << '\r'); DEBUG(endl);
232
233
234 // generate data
235
237
239
241
242 const double value = gRandom->Poisson(model(p1));
243 const double sigma = (value > 1.0 ? sqrt(value) : 0.7);
244
245 data.push_back(hit_type(p1, value, sigma));
246 }
247
248 // start values
249
250 JStats Q[NUMBER_OF_DIMENSIONS];
251
252 double value = 0.0;
253
254 for (const auto& hit : data) {
255
256 for (int i = 0; i != NUMBER_OF_DIMENSIONS; ++i) {
257 if (hit.value > 0.0) {
258 Q[i].put(hit[i], hit.value);
259 }
260 }
261
262 if (hit.value > value) {
263
264 simplex.value[0] = hit.value;
265
266 for (int i = 0; i != NUMBER_OF_DIMENSIONS; ++i) {
267 simplex.value[2*i + 1] = hit[i];
268 }
269
270 value = hit.value;
271 }
272 }
273
274 for (int i = 0; i != NUMBER_OF_DIMENSIONS; ++i) {
275 simplex.value[2*i + 2] = 1.0 * Q[i].getSTDev();
276 }
277
278
279 // steps
280
281 simplex.step.resize(2*NUMBER_OF_DIMENSIONS + 1);
282
283 model_type step;
284
285 simplex.step[0] = model_type();
286 simplex.step[0][0] = 1.0;
287
288 for (int i = 0; i != NUMBER_OF_DIMENSIONS; ++i) {
289
290 simplex.step[2*i + 1] = model_type();
291 simplex.step[2*i + 1][2*i + 1] = 0.1;
292
293 simplex.step[2*i + 2] = model_type();
294 simplex.step[2*i + 2][2*i + 2] = 0.1;
295 }
296
297 DEBUG("start values " << simplex.value << endl);
298
299 for (size_t i = 0; i != simplex.step.size(); ++i) {
300 DEBUG("step: " << setw(2) << i << ' ' << simplex.step[i] << endl);
301 }
302
303
304 // fit
305
306 const double chi2 = simplex(fit, data.begin(), data.end());
307 const int ndf = data.size() - simplex.step.size();
308
309
310 h0.Fill(chi2 / ndf);
311
312 for (size_t i = 0; i != model.size(); ++i) {
313 H1[i]->Fill(model[i] - simplex.value[i]);
314 }
315
316 DEBUG("chi2 / NDF" << FIXED(12,3) << chi2 << " / " << ndf << endl);
317
318 DEBUG("final values " << simplex.value << endl);
319 DEBUG("model values " << model << endl);
320
321 if (debug >= debug_t) {
322
323 for (const auto& hit : data) {
324
325 cout << "hit: ";
326
327 for (int i = 0; i != NUMBER_OF_DIMENSIONS; ++i) {
328 cout << FIXED(7,3) << hit[i] << ' ';
329 }
330
331 const double value = simplex.value(hit);
332
333 cout << FIXED(7,3) << hit.value << ' '
334 << FIXED(7,3) << value << ' '
335 << FIXED(7,3) << (hit.value - value) / hit.sigma << endl;
336 }
337 }
338
339 }
340 STATUS(endl);
341
342
343 if (outputFile != "") {
344
345 TFile out(outputFile.c_str(), "recreate");
346
347 out << h0;
348
349 for (size_t i = 0; i != sizeof(H1)/sizeof(H1[0]); ++i) {
350 out << *(H1[i]);
351 }
352
353 out.Write();
354 out.Close();
355 }
356}
string outputFile
TPaveText * p1
#define DEBUG(A)
Message macros.
Definition JMessage.hh:62
#define STATUS(A)
Definition JMessage.hh:63
#define FATAL(A)
Definition JMessage.hh:67
int debug
debug level
Definition JSirene.cc:72
#define make_field(A,...)
macro to convert parameter to JParserTemplateElement object
Definition JParser.hh:2142
#define MAKE_CSTRING(A)
Make C-string.
Definition JPrint.hh:72
Double_t g1(const Double_t x)
Function.
Definition JQuantiles.cc:25
Template definition of a multi-dimensional oscillation probability interpolation table.
Utility class to parse command line options.
Definition JParser.hh:1698
This name space includes all other name spaces (except KM3NETDAQ, KM3NET and ANTARES).
Auxiliary data structure for floating point format specification.
Definition JManip.hh:448
Auxiliary data structure for return type of make methods.
Definition JVectorize.hh:28
Auxiliary data structure for running average and standard deviation.
Definition JStats.hh:44
void put(const double x, const double w=1.0)
Put value.
Definition JStats.hh:119
double getSTDev() const
Get standard deviation.
Definition JStats.hh:263