Jpp 20.0.0-rc.2
the software that should make you happy
Loading...
Searching...
No Matches
JHistogram1D.cc File Reference

Program to create TH1D and fill according given formula. More...

#include <string>
#include <iostream>
#include <fstream>
#include <vector>
#include "TROOT.h"
#include "TFile.h"
#include "TH1D.h"
#include "TF1.h"
#include "TRandom3.h"
#include "JLang/JToken.hh"
#include "JTools/JAbstractHistogram.hh"
#include "JGizmo/JGizmoToolkit.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 create TH1D and fill according given formula.

Author
mdejong

Definition in file JGizmo/JHistogram1D.cc.

Function Documentation

◆ main()

int main ( int argc,
char ** argv )

Definition at line 26 of file JGizmo/JHistogram1D.cc.

27{
28 using namespace std;
29 using namespace JPP;
30
31 typedef JLANG::JToken<';'> JToken_t;
33
34 string outputFile;
35 string inputFile;
36 string formula;
37 vector<JToken_t> parameters;
38 size_t numberOfEvents;
39 string title;
41 bool logx;
42 bool sumw2;
43 UInt_t seed;
44 int debug;
45
46 try {
47
48 JParser<> zap("Program to create TH1D and fill according given formula.");
49
50 zap['o'] = make_field(outputFile);
51 zap['f'] = make_field(inputFile) = "";
52 zap['F'] = make_field(formula) = "";
53 zap['@'] = make_field(parameters) = JPARSER::initialised();
54 zap['n'] = make_field(numberOfEvents) = 0;
55 zap['T'] = make_field(title) = "h0";
56 zap['x'] = make_field(X) = JHistogram_t(100, -1.0, +1.0);
57 zap['X'] = make_field(logx);
58 zap['s'] = make_field(sumw2);
59 zap['S'] = make_field(seed) = 0;
60 zap['d'] = make_field(debug) = 1;
61
62 zap(argc, argv);
63 }
64 catch(const exception &error) {
65 FATAL(error.what() << endl);
66 }
67
68
69 if ((formula != "" && inputFile != "") ||
70 (formula == "" && inputFile == "")) {
71 FATAL("Specify input file or formula." << endl);
72 }
73
74 gRandom->SetSeed(seed);
75
76 TFile out(outputFile.c_str(), "recreate");
77
78 const string::size_type pos = title.rfind('/');
79
80 if (pos != string::npos) {
81
82 const string dir = title.substr(0, pos);
83
84 out.mkdir(dir.c_str());
85 out.cd (dir.c_str());
86
87 title = title.substr(pos + 1);
88 }
89
90 TH1D h0(title.c_str(), NULL,
92
93 if (formula != "") {
94
95 TF1 f1("f1", formula.c_str(),
96 logx ? pow(10.0, X.getLowerLimit()) : X.getLowerLimit(),
97 logx ? pow(10.0, X.getUpperLimit()) : X.getUpperLimit());
98
99 for (vector<JToken_t>::const_iterator i = parameters.begin(); i != parameters.end(); ++i) {
100 f1.FixParameter(getParameter(*i), getValue(*i));
101 }
102
103 if (numberOfEvents > 0) {
104
105 h0.Sumw2();
106
107 for (size_t i = 0; i != numberOfEvents; ++i) {
108
109 const double x = f1.GetRandom(gRandom);
110
111 h0.Fill(logx ? log10(x) : x);
112 }
113
114 } else {
115
116 for (Int_t ix = 1; ix <= h0.GetXaxis()->GetNbins(); ++ix) {
117 h0.SetBinContent(ix, f1.Eval(h0.GetXaxis()->GetBinCenter(ix)));
118 }
119 }
120
121 } else if (inputFile != "") {
122
123 if (sumw2) {
124 h0.Sumw2();
125 }
126
127 ifstream in(inputFile.c_str());
128
129 for (double x; in >> x; ) {
130 h0.Fill(x);
131 }
132
133 in.close();
134 }
135
136 out.Write();
137 out.Close();
138}
string outputFile
#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
Wrapper class around string.
Definition JToken.hh:26
Utility class to parse command line options.
Definition JParser.hh:1698
T getLowerLimit() const
Get lower limit.
Definition JRange.hh:202
T getUpperLimit() const
Get upper limit.
Definition JRange.hh:213
double getValue(const JScale_t scale)
Get numerical value corresponding to scale.
Definition JScale.hh:47
T pow(const T &x, const double y)
Power .
Definition JMath.hh:97
This name space includes all other name spaces (except KM3NETDAQ, KM3NET and ANTARES).
Empty structure for specification of parser element that is initialised (i.e. does not require input)...
Definition JParser.hh:68
Simple data structure for histogram binning.
int getNumberOfBins() const
Get number of bins.