Jpp - the software that should make you happy
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
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 inputFile;
35  string formula;
37  Int_t numberOfEvents;
38  JHistogram_t X;
39  int debug;
40 
41  try {
42 
43  JParser<> zap("Program to create TH1D and fill according given formula.");
44 
45  zap['o'] = make_field(outputFile);
46  zap['f'] = make_field(inputFile) = "";
47  zap['F'] = make_field(formula) = "";
49  zap['n'] = make_field(numberOfEvents) = 0;
50  zap['x'] = make_field(X) = JHistogram_t(100, -1.0, +1.0);
51  zap['d'] = make_field(debug) = 1;
52 
53  zap(argc, argv);
54  }
55  catch(const exception &error) {
56  FATAL(error.what() << endl);
57  }
58 
59 
60  if ((formula != "" && inputFile != "") ||
61  (formula == "" && inputFile == "")) {
62  FATAL("Specify input file or formula." << endl);
63  }
64 
65 
66  TFile out(outputFile.c_str(), "recreate");
67 
68  TH1D h0("h0", NULL,
69  X.getNumberOfBins(), X.getLowerLimit(), X.getUpperLimit());
70 
71  if (formula != "") {
72 
73  TF1 f1("f1", formula.c_str());
74 
75  for (vector<JToken_t>::const_iterator i = parameters.begin(); i != parameters.end(); ++i) {
76  f1.FixParameter(getParameter(*i), getValue(*i));
77  }
78 
79  if (numberOfEvents > 0) {
80 
81  h0.Sumw2();
82  h0.FillRandom(f1.GetName(), numberOfEvents);
83 
84  } else {
85 
86  for (Int_t ix = 1; ix <= h0.GetXaxis()->GetNbins(); ++ix) {
87  h0.SetBinContent(ix, f1.Eval(h0.GetXaxis()->GetBinCenter(ix)));
88  }
89  }
90 
91  } else if (inputFile != "") {
92 
93  h0.Sumw2();
94 
95  ifstream in(inputFile.c_str());
96 
97  for (double x; in >> x; ) {
98  h0.Fill(x);
99  }
100 
101  in.close();
102  }
103 
104  out.Write();
105  out.Close();
106 }
Utility class to parse command line options.
Definition: JParser.hh:1500
double getValue(const JScale_t scale)
Get numerical value corresponding to scale.
Definition: JScale.hh:47
int main(int argc, char *argv[])
Definition: Main.cc:15
int getParameter(const std::string &text)
Get parameter number from text string.
*fatal Wrong number of arguments esac JCookie sh typeset Z DETECTOR typeset Z SOURCE_RUN typeset Z TARGET_RUN set_variable PARAMETERS_FILE $WORKDIR parameters
Definition: diff-Tuna.sh:38
Empty structure for specification of parser element that is initialised (i.e. does not require input)...
Definition: JParser.hh:66
string outputFile
#define make_field(A,...)
macro to convert parameter to JParserTemplateElement object
Definition: JParser.hh:1961
then break fi done getCenter read X Y Z let X
int debug
debug level
Definition: JSirene.cc:63
General purpose messaging.
#define FATAL(A)
Definition: JMessage.hh:67
Utility class to parse command line options.
Wrapper class around string.
Definition: JToken.hh:23
then fatal Wrong number of arguments fi set_variable DETECTOR $argv[1] set_variable INPUT_FILE $argv[2] eval JPrintDetector a $DETECTOR O IDENTIFIER eval JPrintDetector a $DETECTOR O SUMMARY source JAcoustics sh $DETECTOR_ID CHECK_EXIT_CODE typeset A TRIPODS get_tripods $WORKDIR tripod txt TRIPODS for EMITTER in
Definition: JCanberra.sh:38