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

Program to test JRootfit algorithm. More...

#include <string>
#include <iostream>
#include <iomanip>
#include <vector>
#include <type_traits>
#include <chrono>
#include "TROOT.h"
#include "TFile.h"
#include "TH2D.h"
#include "JROOT/JRootToolkit.hh"
#include "JROOT/JRootTestkit.hh"
#include "JROOT/JRootfit.hh"
#include "JMath/JMathlib2D.hh"
#include "JLang/JManip.hh"
#include "Jeep/JProperties.hh"
#include "Jeep/JParser.hh"

Go to the source code of this file.

Functions

int main (int argc, char **argv)
 

Detailed Description

Program to test JRootfit algorithm.

Author
mdejong

Definition in file JRootfitToGauss2D.cc.

Function Documentation

int main ( int  argc,
char **  argv 
)

Definition at line 30 of file JRootfitToGauss2D.cc.

31 {
32  using namespace std;
33  using namespace JPP;
34 
35  const char* const count_t = "count";
36  const char* const value_t = "value";
37 
38  struct {
39  double signal = 100;
40  double background = 5;
41  double center = 0.0;
42  double sigma = 1.0;
43  } parameters;
44 
45  string inputFile;
46  string outputFile;
47  range_type X;
48  range_type Y;
49  size_t N;
50  string option;
51  bool writeFits;
52  UInt_t seed;
53  int debug;
54 
55  try {
56 
57  JProperties properties;
58 
59  properties.insert(gmake_property(parameters.signal));
60  properties.insert(gmake_property(parameters.background));
61  properties.insert(gmake_property(parameters.center));
62  properties.insert(gmake_property(parameters.sigma));
63 
64  JParser<> zap("Program to test JRootfit algorithm.");
65 
66  zap['f'] = make_field(inputFile) = "";
67  zap['o'] = make_field(outputFile);
68  zap['@'] = make_field(properties) = JPARSER::initialised();
69  zap['x'] = make_field(X) = range_type();
70  zap['y'] = make_field(Y) = range_type();
71  zap['n'] = make_field(N) = 0;
72  zap['O'] = make_field(option) = count_t, value_t;
73  zap['w'] = make_field(writeFits);
74  zap['S'] = make_field(seed) = 0;
75  zap['d'] = make_field(debug) = 1;
76 
77  zap(argc, argv);
78  }
79  catch(const exception& error) {
80  FATAL(error.what() << endl);
81  }
82 
83 
84  gRandom->SetSeed(seed);
85 
86  const size_t ls = 2;
87 
88  const size_t nx = 20 * ls;
89  const double xmin = -5.0 * ls;
90  const double xmax = +5.0 * ls;
91 
92  const size_t ny = 20 * ls;
93  const double ymin = -5.0 * ls;
94  const double ymax = +5.0 * ls;
95 
96  TH2D* h2 = NULL;
97 
98  if (inputFile == "") {
99 
100  h2 = new TH2D("h2", NULL, nx, xmin, xmax, ny, ymin, ymax);
101 
102  h2->Sumw2();
103 
104  auto f2 = (JGauss2X<1>(parameters.center, parameters.sigma) *
105  JGauss2Y<2>(parameters.center, parameters.sigma) *
106  JP0<1>(parameters.signal) +
107  JP0<2>(parameters.background));
108 
109  if (N == 0)
110  FillRandom(h2, f2);
111  else
112  FillRandom(h2, f2, N);
113 
114  } else {
115 
116  TFile* in = TFile::Open(inputFile.c_str(), "exist");
117 
118  in->GetObject("h2", h2);
119  }
120 
121 
122  // determine start values from data
123 
124  const double x0 = h2->GetMean(1);
125  const double y0 = h2->GetMean(2);
126  const double xs = h2->GetStdDev(1) * 0.33;
127  const double ys = h2->GetStdDev(2) * 0.33;
128  const double signal = h2->GetMaximum();
129  const double background = h2->GetMinimum() + 0.10;
130 
131  // fit
132 
133  auto f2 = (JGauss2X<1>(x0, xs) *
134  JGauss2Y<2>(y0, ys) *
135  JP0<1>(signal)) + Exp(JP0<2>(log(background)));
136 
137  //auto f2 = JGauss2D<1>(sqrt(x0*y0), sqrt(xs*ys)) * JP0<1>(signal) + JP0<2>(background);
138 
139  typedef decltype(f2) function_type;
140 
141  JRootfit<function_type>::debug = debug;
142 
143  for (size_t i = 0; i != getNumberOfParameters<function_type>(); ++i) {
144  cout << setw(2) << i << ' '
145  << FIXED(15,9) << getParameter(f2, i) << endl;
146  }
147 
148  {
149  const chrono::steady_clock::time_point t0 = chrono::steady_clock::now();
150 
151  const auto result = (option == count_t ?
152  (writeFits ? Fit<m_count>(h2, f2, {}, X, Y) : Fit<m_count>(*h2, f2, {}, X, Y)) :
153  (writeFits ? Fit<m_value>(h2, f2, {}, X, Y) : Fit<m_value>(*h2, f2, {}, X, Y)));
154 
155  const chrono::steady_clock::time_point t1 = chrono::steady_clock::now();
156 
157  cout << "chi2/NDF " << FIXED(7,3) << result.getChi2() << "/" << result.getNDF() << endl;
158  cout << "Number of iterations " << result.numberOfIterations << endl;
159  cout << "Elapsed time [us] " << setw(8) << chrono::duration_cast<chrono::microseconds>(t1 - t0).count() << endl;
160 
161  for (size_t i = 0; i != result.getNumberOfParameters(); ++i) {
162  cout << setw(2) << i << ' '
163  << FIXED(15,9) << result.getValue(i) << " +/- "
164  << FIXED(15,9) << result.getError(i) << endl;
165  }
166  }
167 
168  TFile out(outputFile.c_str(), "recreate");
169 
170  out << *h2;
171 
172  out.Write();
173  out.Close();
174 }
const double xmax
Definition: JQuadrature.cc:24
Utility class to parse command line options.
Definition: JParser.hh:1711
int getParameter(const std::string &text)
Get parameter number from text string.
#define gmake_property(A)
macros to convert (template) parameter to JPropertiesElement object
Utility class to parse parameter values.
Definition: JProperties.hh:497
then usage $script< input file >[option] nPossible options count
Definition: JVolume1D.sh:31
*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
Empty structure for specification of parser element that is initialised (i.e. does not require input)...
Definition: JParser.hh:84
JExp< JF1_t > Exp(const JF1_t &f1)
Exponent of function.
Definition: JMathlib.hh:2545
then fatal Wrong number of arguments fi set_variable STRING $argv[1] set_variable DETECTORXY_TXT $WORKDIR $DETECTORXY_TXT tail read X Y CHI2 RMS printf optimum n $X $Y $CHI2 $RMS awk v Y
Auxiliary data structure for floating point format specification.
Definition: JManip.hh:446
string outputFile
JRange< int > range_type
const double sigma[]
Definition: JQuadrature.cc:74
void FillRandom(TH1 *h1, const T &f1)
Fill 1D histogram according Poisson statistics with expectation values from given 1D function...
Definition: JRootTestkit.hh:33
#define make_field(A,...)
macro to convert parameter to JParserTemplateElement object
Definition: JParser.hh:2158
JRootfit_t< JF1_t > Fit(const TH1 &h1, const JF1_t &f1, const index_list &ls=index_list(), const range_type &X=range_type())
Global fit fuction.
Definition: JRootfit.hh:1247
#define FATAL(A)
Definition: JMessage.hh:67
const double xmin
Definition: JQuadrature.cc:23
then usage $script< input file >[option[primary[working directory]]] nWhere option can be N
Definition: JMuonPostfit.sh:40
then fatal The output file must have the wildcard in the e g root fi eval JPrintDetector a $DETECTOR O IDENTIFIER eval JPrintDetector a $DETECTOR O SUMMARY JAcoustics sh $DETECTOR_ID source JAcousticsToolkit sh CHECK_EXIT_CODE typeset A EMITTERS get_tripods $WORKDIR tripod txt EMITTERS get_transmitters $WORKDIR transmitter txt EMITTERS for EMITTER in
Definition: JCanberra.sh:48
no fit printf nominal n $STRING awk v X
int debug
debug level
do echo n Creating graphics for string $STRING for((FLOOR=$FIRST_FLOOR;$FLOOR<=$LAST_FLOOR;FLOOR+=1))
size_t getNumberOfParameters()
Get number of parameters.
Definition: JMathlib.hh:155