Jpp  master_rocky-37-gf0c5bc59d
the software that should make you happy
Functions
JGizmo/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 "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 25 of file JGizmo/JHistogram1D.cc.

26 {
27  using namespace std;
28  using namespace JPP;
29 
30  typedef JLANG::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;
39  JHistogram_t X;
40  bool sumw2;
41  int debug;
42 
43  try {
44 
45  JParser<> zap("Program to create TH1D and fill according given formula.");
46 
47  zap['o'] = make_field(outputFile);
48  zap['f'] = make_field(inputFile) = "";
49  zap['F'] = make_field(formula) = "";
50  zap['@'] = make_field(parameters) = JPARSER::initialised();
51  zap['n'] = make_field(numberOfEvents) = 0;
52  zap['T'] = make_field(title) = "h0";
53  zap['x'] = make_field(X) = JHistogram_t(100, -1.0, +1.0);
54  zap['s'] = make_field(sumw2);
55  zap['d'] = make_field(debug) = 1;
56 
57  zap(argc, argv);
58  }
59  catch(const exception &error) {
60  FATAL(error.what() << endl);
61  }
62 
63 
64  if ((formula != "" && inputFile != "") ||
65  (formula == "" && inputFile == "")) {
66  FATAL("Specify input file or formula." << endl);
67  }
68 
69 
70  TFile out(outputFile.c_str(), "recreate");
71 
72  const string::size_type pos = title.rfind('/');
73 
74  if (pos != string::npos) {
75 
76  const string dir = title.substr(0, pos);
77 
78  out.mkdir(dir.c_str());
79  out.cd (dir.c_str());
80 
81  title = title.substr(pos + 1);
82  }
83 
84  TH1D h0(title.c_str(), NULL,
86 
87  if (formula != "") {
88 
89  TF1 f1("f1", formula.c_str());
90 
91  for (vector<JToken_t>::const_iterator i = parameters.begin(); i != parameters.end(); ++i) {
92  f1.FixParameter(getParameter(*i), getValue(*i));
93  }
94 
95  if (numberOfEvents > 0) {
96 
97  h0.Sumw2();
98  h0.FillRandom(f1.GetName(), numberOfEvents);
99 
100  } else {
101 
102  for (Int_t ix = 1; ix <= h0.GetXaxis()->GetNbins(); ++ix) {
103  h0.SetBinContent(ix, f1.Eval(h0.GetXaxis()->GetBinCenter(ix)));
104  }
105  }
106 
107  } else if (inputFile != "") {
108 
109  if (sumw2) {
110  h0.Sumw2();
111  }
112 
113  ifstream in(inputFile.c_str());
114 
115  for (double x; in >> x; ) {
116  h0.Fill(x);
117  }
118 
119  in.close();
120  }
121 
122  out.Write();
123  out.Close();
124 }
string outputFile
#define FATAL(A)
Definition: JMessage.hh:67
int debug
debug level
Definition: JSirene.cc:69
#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
const JPolynome f1(1.0, 2.0, 3.0)
Function.
JAbstractHistogram< double > JHistogram_t
Type definition for scan along axis.
Definition: JBillabong.cc:61
double getValue(const JScale_t scale)
Get numerical value corresponding to scale.
Definition: JScale.hh:47
int getParameter(const std::string &text)
Get parameter number from text string.
This name space includes all other name spaces (except KM3NETDAQ, KM3NET and ANTARES).
Definition: JSTDTypes.hh:14
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.