Jpp  19.0.0
the software that should make you happy
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
JRootFitToGauss.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 "TROOT.h"
8 #include "TFile.h"
9 #include "TF1.h"
10 #include "TH1D.h"
11 
12 #include "JROOT/JMinimizer.hh"
13 
14 #include "JTools/JQuantile.hh"
15 #include "JROOT/JRootToolkit.hh"
16 #include "JMath/JGauss.hh"
17 
18 #include "Jeep/JTimer.hh"
19 #include "Jeep/JPrint.hh"
20 #include "Jeep/JParser.hh"
21 #include "Jeep/JMessage.hh"
22 
23 
24 /**
25  * \file
26  *
27  * Program to test ROOT fit.
28  * \author mdejong
29  */
30 int main(int argc, char **argv)
31 {
32  using namespace std;
33  using namespace JPP;
34 
35  string outputFile;
36  int numberOfEvents;
37  JGauss gauss;
38  JGauss precision;
39  int debug;
40 
41  try {
42 
43  JParser<> zap("Program to test ROOT fit.");
44 
45  zap['o'] = make_field(outputFile) = "";
46  zap['n'] = make_field(numberOfEvents) = 1000;
47  zap['@'] = make_field(gauss) = JGauss(0.0, 1.0, 1000.0, 100.0);
48  zap['e'] = make_field(precision) = JGauss(0.05, 0.05, 25.0, 25.0);
49  zap['d'] = make_field(debug) = 3;
50 
51  zap(argc, argv);
52  }
53  catch(const exception& error) {
54  FATAL(error.what() << endl);
55  }
56 
57 
58  ASSERT(numberOfEvents > 0);
59 
60  TF1 fs("fs", "exp(-0.5 * (x-[0])*(x-[0]) / ([1]*[1]))");
61  TF1 fb("fb", "1.0");
62 
63  fs.FixParameter(0, gauss.mean);
64  fs.FixParameter(1, gauss.sigma);
65 
66  const Int_t nx = 21;
67  const Double_t xmin = -5.0;
68  const Double_t xmax = +5.0;
69 
70  JQuantile Q[] = { JQuantile("mean "),
71  JQuantile("sigma "),
72  JQuantile("signal "),
73  JQuantile("background") };
74 
75  TH1D H[] = { TH1D("ha", "", 101, -0.1, +0.1),
76  TH1D("hb", "", 101, -0.1, +0.1),
77  TH1D("hc", "", 101, -100.0, +100.0),
78  TH1D("hd", "", 101, -100.0, +100.0) };
79 
80  JTimer timer;
81 
82  for (int i = 0; i != numberOfEvents; ++i) {
83 
84  STATUS("event: " << setw(10) << i << '\r'); DEBUG(endl);
85 
86  TH1D h0("h0", NULL, nx, xmin, xmax);
87 
88  h0.Sumw2();
89 
90  h0.FillRandom("fs", (Int_t) gauss.signal);
91  h0.FillRandom("fb", (Int_t) gauss.background);
92 
93  TF1 f1("f1", "[2]*exp(-0.5 * (x-[0])*(x-[0]) / ([1]*[1])) / (TMath::Sqrt(2.0*TMath::Pi())*[1]) + [3]");
94 
95  f1.SetParameter(0, h0.GetMean());
96  f1.SetParameter(1, h0.GetRMS());
97  f1.SetParameter(2, h0.GetEntries() - h0.GetMinimum() * h0.GetNbinsX());
98  f1.SetParameter(3, h0.GetMinimum());
99 
100  f1.SetParError(0, 1.0e-3);
101  f1.SetParError(1, 1.0e-3);
102  f1.SetParError(2, 0.5);
103  f1.SetParError(3, 0.5);
104 
105  string option = "NWL";
106 
107  if (debug < debug_t && option.find('Q') == string::npos) {
108  option += "Q";
109  }
110 
111  timer.start();
112 
113  h0.Fit(&f1, option.c_str());
114 
115  timer.stop();
116 
117  const double Y[] = { f1.GetParameter(0) - gauss.mean,
118  f1.GetParameter(1) - gauss.sigma,
119  f1.GetParameter(2) * nx / (xmax - xmin) - gauss.signal,
120  f1.GetParameter(3) * nx - gauss.background };
121 
122  for (int i = 0; i != sizeof(Q)/sizeof(Q[0]); ++i) {
123  Q[i].put (Y[i]);
124  H[i].Fill(Y[i]);
125  }
126  }
127 
128  for (int i = 0; i != sizeof(Q)/sizeof(Q[0]); ++i) {
129  NOTICE((i == 0 ? longprint : shortprint) << Q[i]);
130  }
131 
132  if (debug >= notice_t) {
133  timer.print(cout, true, micro_t);
134  }
135 
136  if (outputFile != "") {
137 
138  TFile out(outputFile.c_str(), "recreate");
139 
140  for (int i = 0; i != sizeof(H)/sizeof(H[0]); ++i) {
141  out << H[i];
142  }
143 
144  out.Write();
145  out.Close();
146  }
147 
148  for (int i = 0; i != sizeof(Q)/sizeof(Q[0]); ++i) {
149  ASSERT(fabs(Q[i].getMean()) < Q[i].getSTDev());
150  }
151 
152  ASSERT(Q[0].getSTDev() < precision.mean);
153  ASSERT(Q[1].getSTDev() < precision.sigma);
154  ASSERT(Q[2].getSTDev() < precision.signal);
155  ASSERT(Q[3].getSTDev() < precision.background);
156 
157  return 0;
158 }
const double xmax
Definition: JQuadrature.cc:24
Utility class to parse command line options.
Definition: JParser.hh:1711
Q(UTCMax_s-UTCMin_s)-livetime_s
debug
Definition: JMessage.hh:29
int main(int argc, char *argv[])
Definition: Main.cc:15
micro
Definition: JScale.hh:29
#define STATUS(A)
Definition: JMessage.hh:63
notice
Definition: JMessage.hh:32
static const double H
Planck constant [eV s].
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
string outputFile
double getMean(vector< double > &v)
get mean of vector content
const JPolynome f1(1.0, 2.0, 3.0)
Function.
#define ASSERT(A,...)
Assert macro.
Definition: JMessage.hh:90
I/O formatting auxiliaries.
#define make_field(A,...)
macro to convert parameter to JParserTemplateElement object
Definition: JParser.hh:2158
#define NOTICE(A)
Definition: JMessage.hh:64
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
const double xmin
Definition: JQuadrature.cc:23
Utility class to parse command line options.
then set_variable NUMBER_OF_TESTS else set_variable NUMBER_OF_TESTS fi function gauss()
int debug
debug level
#define DEBUG(A)
Message macros.
Definition: JMessage.hh:62