Jpp
JGizmo/JHistogram1D.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 "TH1D.h"
9 #include "TF1.h"
10 
11 #include "JLang/JToken.hh"
13 #include "JGizmo/JGizmoToolkit.hh"
14 
15 #include "Jeep/JParser.hh"
16 #include "Jeep/JMessage.hh"
17 
18 
19 /**
20  * \file
21  *
22  * Program to create TH1D and fill according given formula.
23  * \author mdejong
24  */
25 int main(int argc, char **argv)
26 {
27  using namespace std;
28  using namespace JPP;
29 
30  typedef JLANG::JToken<';'> JToken_t;
31  typedef JAbstractHistogram<Double_t> JHistogram_t;
32 
33  string outputFile;
34  string formula;
35  vector<JToken_t> parameterValues;
36  Int_t numberOfEvents;
37  JHistogram_t X;
38  int debug;
39 
40  try {
41 
42  JParser<> zap("Program to create TH1D and fill according given formula.");
43 
44  zap['o'] = make_field(outputFile);
45  zap['F'] = make_field(formula);
46  zap['='] = make_field(parameterValues) = JPARSER::initialised();
47  zap['n'] = make_field(numberOfEvents) = 0;
48  zap['x'] = make_field(X) = JHistogram_t(100, -1.0, +1.0);
49  zap['d'] = make_field(debug) = 1;
50 
51  zap(argc, argv);
52  }
53  catch(const exception &error) {
54  FATAL(error.what() << endl);
55  }
56 
57 
58  TF1 f1("f1", formula.c_str());
59 
60  for (vector<JToken_t>::const_iterator i = parameterValues.begin(); i != parameterValues.end(); ++i) {
61  f1.FixParameter(getParameter(*i), getValue(*i));
62  }
63 
64 
65  TFile out(outputFile.c_str(), "recreate");
66 
67  TH1D h0("h0", NULL,
68  X.getNumberOfBins(), X.getLowerLimit(), X.getUpperLimit());
69 
70  h0.Sumw2();
71 
72  if (numberOfEvents > 0) {
73 
74  h0.FillRandom(f1.GetName(), numberOfEvents);
75 
76  } else {
77 
78  for (Int_t ix = 1; ix <= h0.GetXaxis()->GetNbins(); ++ix) {
79  h0.SetBinContent(ix, f1.Eval(h0.GetXaxis()->GetBinCenter(ix)));
80  }
81  }
82 
83  out.Write();
84  out.Close();
85 }
JAbstractHistogram.hh
JGIZMO::getParameter
int getParameter(const std::string &text)
Get parameter number from text string.
Definition: JGizmoToolkit.hh:288
JToken.hh
JMessage.hh
JPARSER::initialised
Empty structure for specification of parser element that is initialised (i.e.
Definition: JParser.hh:63
std::vector
Definition: JSTDTypes.hh:12
main
int main(int argc, char **argv)
Definition: JGizmo/JHistogram1D.cc:25
JPARSER::JParser
Utility class to parse command line options.
Definition: JParser.hh:1493
JGizmoToolkit.hh
JPP
This name space includes all other name spaces (except KM3NETDAQ, KM3NET and ANTARES).
Definition: JAAnetToolkit.hh:37
debug
int debug
debug level
Definition: JSirene.cc:59
JParser.hh
make_field
#define make_field(A,...)
macro to convert parameter to JParserTemplateElement object
Definition: JParser.hh:1954
JLANG::JToken
Wrapper class around string.
Definition: JToken.hh:23
std
Definition: jaanetDictionary.h:36
JEEP::getValue
double getValue(const JScale_t scale)
Get numerical value corresponding to scale.
Definition: JScale.hh:47
FATAL
#define FATAL(A)
Definition: JMessage.hh:67
outputFile
string outputFile
Definition: JDAQTimesliceSelector.cc:37