Jpp  16.0.0
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  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) = "";
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  TH1D h0(title.c_str(), NULL,
73  X.getNumberOfBins(), X.getLowerLimit(), X.getUpperLimit());
74 
75  if (formula != "") {
76 
77  TF1 f1("f1", formula.c_str());
78 
79  for (vector<JToken_t>::const_iterator i = parameters.begin(); i != parameters.end(); ++i) {
80  f1.FixParameter(getParameter(*i), getValue(*i));
81  }
82 
83  if (numberOfEvents > 0) {
84 
85  h0.Sumw2();
86  h0.FillRandom(f1.GetName(), numberOfEvents);
87 
88  } else {
89 
90  for (Int_t ix = 1; ix <= h0.GetXaxis()->GetNbins(); ++ix) {
91  h0.SetBinContent(ix, f1.Eval(h0.GetXaxis()->GetBinCenter(ix)));
92  }
93  }
94 
95  } else if (inputFile != "") {
96 
97  if (sumw2) {
98  h0.Sumw2();
99  }
100 
101  ifstream in(inputFile.c_str());
102 
103  for (double x; in >> x; ) {
104  h0.Fill(x);
105  }
106 
107  in.close();
108  }
109 
110  out.Write();
111  out.Close();
112 }
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:42