Jpp 19.3.0-rc.1
the software that should make you happy
Loading...
Searching...
No Matches
JHistogram2D.cc File Reference

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

#include <string>
#include <iostream>
#include <fstream>
#include <vector>
#include "TROOT.h"
#include "TFile.h"
#include "TH2D.h"
#include "TF2.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 TH2D and fill according given formula.

Author
mdejong

Definition in file JGizmo/JHistogram2D.cc.

Function Documentation

◆ main()

int main ( int argc,
char ** argv )

Definition at line 25 of file JGizmo/JHistogram2D.cc.

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
#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
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.