Jpp 19.3.0-rc.1
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
11#include "JLang/JToken.hh"
14
15#include "Jeep/JParser.hh"
16#include "Jeep/JMessage.hh"
17
18
19/**
20 * \file
21 *
22 * Program to create TH2D and fill according given formula.
23 * \author mdejong
24 */
25int main(int argc, char **argv)
26{
27 using namespace std;
28 using namespace JPP;
29
30 typedef JToken<';'> JToken_t;
32
33 string outputFile;
34 string inputFile;
35 string formula;
36 vector<JToken_t> parameters;
37 Int_t numberOfEvents;
38 string title;
41 bool sumw2;
42 int debug;
43
44 try {
45
46 JParser<> zap("Program to create TH2D and fill according given formula.");
47
48 zap['o'] = make_field(outputFile);
49 zap['f'] = make_field(inputFile) = "";
50 zap['F'] = make_field(formula) = "";
51 zap['@'] = make_field(parameters) = JPARSER::initialised();
52 zap['n'] = make_field(numberOfEvents) = 0;
53 zap['T'] = make_field(title) = "h0";
54 zap['x'] = make_field(X) = JHistogram_t(100, -1.0, +1.0);
55 zap['y'] = make_field(Y) = JHistogram_t(100, -1.0, +1.0);
56 zap['s'] = make_field(sumw2);
57 zap['d'] = make_field(debug) = 1;
58
59 zap(argc, argv);
60 }
61 catch(const exception &error) {
62 FATAL(error.what() << endl);
63 }
64
65
66 if ((formula != "" && inputFile != "") ||
67 (formula == "" && inputFile == "")) {
68 FATAL("Specify input file or formula." << endl);
69 }
70
71
72 TFile out(outputFile.c_str(), "recreate");
73
74 const string::size_type pos = title.rfind('/');
75
76 if (pos != string::npos) {
77
78 const string dir = title.substr(0, pos);
79
80 out.mkdir(dir.c_str());
81 out.cd (dir.c_str());
82
83 title = title.substr(pos + 1);
84 }
85
86 TH2D h0(title.c_str(), NULL,
89
90 if (formula != "") {
91
92 TF2 f2("f2", formula.c_str());
93
94 for (vector<JToken_t>::const_iterator i = parameters.begin(); i != parameters.end(); ++i) {
95 f2.FixParameter(getParameter(*i), getValue(*i));
96 }
97
98 if (numberOfEvents > 0) {
99
100 h0.Sumw2();
101 h0.FillRandom(f2.GetName(), numberOfEvents);
102
103 } else {
104
105 for (Int_t ix = 1; ix <= h0.GetXaxis()->GetNbins(); ++ix) {
106 for (Int_t iy = 1; iy <= h0.GetYaxis()->GetNbins(); ++iy) {
107 h0.SetBinContent(ix, iy, f2.Eval(h0.GetXaxis()->GetBinCenter(ix),
108 h0.GetYaxis()->GetBinCenter(iy)));
109 }
110 }
111 }
112 } else if (inputFile != "") {
113
114 if (sumw2) {
115 h0.Sumw2();
116 }
117
118 ifstream in(inputFile.c_str());
119
120 for (double x, y; in >> x >> y; ) {
121 h0.Fill(x, y);
122 }
123
124 in.close();
125 }
126
127 out.Write();
128 out.Close();
129}
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.