Jpp 20.0.0-rc.2
the software that should make you happy
Loading...
Searching...
No Matches
JGizmo/JHistogram2D.cc
Go to the documentation of this file.
1#include <string>
2#include <iostream>
3#include <fstream>
4#include <vector>
5
6#include "TROOT.h"
7#include "TFile.h"
8#include "TH2D.h"
9#include "TF2.h"
10#include "TRandom3.h"
11
12#include "JLang/JToken.hh"
15
16#include "Jeep/JParser.hh"
17#include "Jeep/JMessage.hh"
18
19
20/**
21 * \file
22 *
23 * Program to create TH2D and fill according given formula.
24 * \author mdejong
25 */
26int main(int argc, char **argv)
27{
28 using namespace std;
29 using namespace JPP;
30
31 typedef 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;
42 bool logx;
43 bool logy;
44 bool sumw2;
45 UInt_t seed;
46 int debug;
47
48 try {
49
50 JParser<> zap("Program to create TH2D and fill according given formula.");
51
52 zap['o'] = make_field(outputFile);
53 zap['f'] = make_field(inputFile) = "";
54 zap['F'] = make_field(formula) = "";
55 zap['@'] = make_field(parameters) = JPARSER::initialised();
56 zap['n'] = make_field(numberOfEvents) = 0;
57 zap['T'] = make_field(title) = "h0";
58 zap['x'] = make_field(X) = JHistogram_t(100, -1.0, +1.0);
59 zap['y'] = make_field(Y) = JHistogram_t(100, -1.0, +1.0);
60 zap['X'] = make_field(logx);
61 zap['Y'] = make_field(logy);
62 zap['s'] = make_field(sumw2);
63 zap['S'] = make_field(seed) = 0;
64 zap['d'] = make_field(debug) = 1;
65
66 zap(argc, argv);
67 }
68 catch(const exception &error) {
69 FATAL(error.what() << endl);
70 }
71
72
73 if ((formula != "" && inputFile != "") ||
74 (formula == "" && inputFile == "")) {
75 FATAL("Specify input file or formula." << endl);
76 }
77
78 gRandom->SetSeed(seed);
79
80 TFile out(outputFile.c_str(), "recreate");
81
82 const string::size_type pos = title.rfind('/');
83
84 if (pos != string::npos) {
85
86 const string dir = title.substr(0, pos);
87
88 out.mkdir(dir.c_str());
89 out.cd (dir.c_str());
90
91 title = title.substr(pos + 1);
92 }
93
94 TH2D h0(title.c_str(), NULL,
97
98 if (formula != "") {
99
100 TF2 f2("f2", formula.c_str(),
101 logx ? pow(10.0, X.getLowerLimit()) : X.getLowerLimit(),
102 logx ? pow(10.0, X.getUpperLimit()) : X.getUpperLimit(),
103 logy ? pow(10.0, Y.getLowerLimit()) : Y.getLowerLimit(),
104 logy ? pow(10.0, Y.getUpperLimit()) : Y.getUpperLimit());
105
106 for (vector<JToken_t>::const_iterator i = parameters.begin(); i != parameters.end(); ++i) {
107 f2.FixParameter(getParameter(*i), getValue(*i));
108 }
109
110 if (numberOfEvents > 0) {
111
112 h0.Sumw2();
113
114 Double_t x, y;
115
116 for (size_t i = 0; i != numberOfEvents; ++i) {
117
118 f2.GetRandom2(x, y, gRandom);
119
120 h0.Fill(logx ? log10(x) : x,
121 logy ? log10(y) : y);
122 }
123
124 } else {
125
126 for (Int_t ix = 1; ix <= h0.GetXaxis()->GetNbins(); ++ix) {
127 for (Int_t iy = 1; iy <= h0.GetYaxis()->GetNbins(); ++iy) {
128 h0.SetBinContent(ix, iy, f2.Eval(h0.GetXaxis()->GetBinCenter(ix),
129 h0.GetYaxis()->GetBinCenter(iy)));
130 }
131 }
132 }
133 } else if (inputFile != "") {
134
135 if (sumw2) {
136 h0.Sumw2();
137 }
138
139 ifstream in(inputFile.c_str());
140
141 for (double x, y; in >> x >> y; ) {
142 h0.Fill(x, y);
143 }
144
145 in.close();
146 }
147
148 out.Write();
149 out.Close();
150}
string outputFile
int main(int argc, char **argv)
General purpose messaging.
#define FATAL(A)
Definition JMessage.hh:67
int debug
debug level
Definition JSirene.cc:72
Utility class to parse command line options.
#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
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.