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

Program to test ROOT fit. More...

#include <string>
#include <iostream>
#include <iomanip>
#include <vector>
#include <cmath>
#include "TROOT.h"
#include "TFile.h"
#include "TF1.h"
#include "TH1D.h"
#include "JTools/JQuantile.hh"
#include "JROOT/JRootToolkit.hh"
#include "JMath/JGauss.hh"
#include "Jeep/JTimer.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 ROOT fit.

Author
mdejong

Definition in file JRootFitToGauss.cc.

Function Documentation

int main ( int  argc,
char **  argv 
)

Definition at line 28 of file JRootFitToGauss.cc.

29 {
30  using namespace std;
31  using namespace JPP;
32 
33  string outputFile;
34  int numberOfEvents;
35  JGauss gauss;
36  JGauss precision;
37  int debug;
38 
39  try {
40 
41  JParser<> zap("Program to test ROOT fit.");
42 
43  zap['o'] = make_field(outputFile) = "";
44  zap['n'] = make_field(numberOfEvents) = 1000;
45  zap['@'] = make_field(gauss) = JGauss(0.0, 1.0, 1000.0, 100.0);
46  zap['e'] = make_field(precision) = JGauss(0.05, 0.05, 25.0, 25.0);
47  zap['d'] = make_field(debug) = 3;
48 
49  zap(argc, argv);
50  }
51  catch(const exception& error) {
52  FATAL(error.what() << endl);
53  }
54 
55 
56  ASSERT(numberOfEvents > 0);
57 
58  TF1 fs("fs", "exp(-0.5 * (x-[0])*(x-[0]) / ([1]*[1]))");
59  TF1 fb("fb", "1.0");
60 
61  fs.FixParameter(0, gauss.mean);
62  fs.FixParameter(1, gauss.sigma);
63 
64  const Int_t nx = 21;
65  const Double_t xmin = -5.0;
66  const Double_t xmax = +5.0;
67 
68  JQuantile Q[] = { JQuantile("mean "),
69  JQuantile("sigma "),
70  JQuantile("signal "),
71  JQuantile("background") };
72 
73  TH1D H[] = { TH1D("ha", "", 101, -0.1, +0.1),
74  TH1D("hb", "", 101, -0.1, +0.1),
75  TH1D("hc", "", 101, -100.0, +100.0),
76  TH1D("hd", "", 101, -100.0, +100.0) };
77 
78  JTimer timer;
79 
80  for (int i = 0; i != numberOfEvents; ++i) {
81 
82  STATUS("event: " << setw(10) << i << '\r'); DEBUG(endl);
83 
84  TH1D h0("h0", NULL, nx, xmin, xmax);
85 
86  h0.Sumw2();
87 
88  h0.FillRandom("fs", (Int_t) gauss.signal);
89  h0.FillRandom("fb", (Int_t) gauss.background);
90 
91  TF1 f1("f1", "[2]*exp(-0.5 * (x-[0])*(x-[0]) / ([1]*[1])) / (TMath::Sqrt(2.0*TMath::Pi())*[1]) + [3]");
92 
93  f1.SetParameter(0, h0.GetMean());
94  f1.SetParameter(1, h0.GetRMS());
95  f1.SetParameter(2, h0.GetEntries() - h0.GetMinimum() * h0.GetNbinsX());
96  f1.SetParameter(3, h0.GetMinimum());
97 
98  string option = "NWL";
99 
100  if (debug < debug_t && option.find('Q') == string::npos) {
101  option += "Q";
102  }
103 
104  timer.start();
105 
106  h0.Fit(&f1, option.c_str());
107 
108  timer.stop();
109 
110  const double Y[] = { f1.GetParameter(0) - gauss.mean,
111  f1.GetParameter(1) - gauss.sigma,
112  f1.GetParameter(2) * nx / (xmax - xmin) - gauss.signal,
113  f1.GetParameter(3) * nx - gauss.background };
114 
115  for (int i = 0; i != sizeof(Q)/sizeof(Q[0]); ++i) {
116  Q[i].put (Y[i]);
117  H[i].Fill(Y[i]);
118  }
119  }
120 
121  for (int i = 0; i != sizeof(Q)/sizeof(Q[0]); ++i) {
122  NOTICE((i == 0 ? longprint : shortprint) << Q[i]);
123  }
124 
125  if (debug >= notice_t) {
126  timer.print(cout, true, micro_t);
127  }
128 
129  if (outputFile != "") {
130 
131  TFile out(outputFile.c_str(), "recreate");
132 
133  for (int i = 0; i != sizeof(H)/sizeof(H[0]); ++i) {
134  out << H[i];
135  }
136 
137  out.Write();
138  out.Close();
139  }
140 
141  for (int i = 0; i != sizeof(Q)/sizeof(Q[0]); ++i) {
142  ASSERT(fabs(Q[i].getMean()) < Q[i].getSTDev());
143  }
144 
145  ASSERT(Q[0].getSTDev() < precision.mean);
146  ASSERT(Q[1].getSTDev() < precision.sigma);
147  ASSERT(Q[2].getSTDev() < precision.signal);
148  ASSERT(Q[3].getSTDev() < precision.background);
149 
150  return 0;
151 }
Utility class to parse command line options.
Definition: JParser.hh:1500
Q(UTCMax_s-UTCMin_s)-livetime_s
debug
Definition: JMessage.hh:29
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
#define ASSERT(A,...)
Assert macro.
Definition: JMessage.hh:90
#define make_field(A,...)
macro to convert parameter to JParserTemplateElement object
Definition: JParser.hh:1961
#define NOTICE(A)
Definition: JMessage.hh:64
int debug
debug level
Definition: JSirene.cc:63
std::ostream & shortprint(std::ostream &out)
Set short printing.
Definition: JManip.hh:144
std::ostream & longprint(std::ostream &out)
Set long printing.
Definition: JManip.hh:172
#define FATAL(A)
Definition: JMessage.hh:67
then usage $script< input_file >< detector_file > fi set_variable OUTPUT_DIR set_variable SELECTOR JDAQTimesliceL1 set_variable DEBUG case set_variable DEBUG
double gauss(const double x, const double sigma)
Gauss function (normalised to 1 at x = 0).