Jpp test-rotations-old
the software that should make you happy
Loading...
Searching...
No Matches
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

◆ 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;
170 ULong_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
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
235
237
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}
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
Simple fit method based on Powell's algorithm, see reference: Numerical Recipes in C++,...
Definition JSimplex.hh:44
JModel_t value
Definition JSimplex.hh:240
std::vector< JModel_t > step
Definition JSimplex.hh:241
Utility class to parse command line options.
Definition JParser.hh:1698
One dimensional array of template objects with fixed length.
Definition JArray.hh:43
const double sigma[]
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
ND array iterator.
Simple data structure for an abstract collection of equidistant abscissa values.
Definition JGrid.hh:40
Auxiliary data structure for running average, standard deviation and quantiles.
Definition JQuantile.hh:46
double getSTDev() const
Get standard deviation.
Definition JQuantile.hh:281
void put(const double x, const double w=1.0)
Put value.
Definition JQuantile.hh:133