Jpp  18.0.0-rc.1
the software that should make you happy
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
JVariance2D.cc
Go to the documentation of this file.
1 
2 #include <string>
3 #include <iostream>
4 #include <iomanip>
5 #include <vector>
6 #include <cmath>
7 
8 #include "TROOT.h"
9 #include "TFile.h"
10 #include "TKey.h"
11 #include "TH1D.h"
12 #include "TH2.h"
13 #include "TString.h"
14 #include "TRegexp.h"
15 
17 #include "JGizmo/JRootObjectID.hh"
18 #include "JGizmo/JGizmoToolkit.hh"
19 
20 #include "Jeep/JParser.hh"
21 #include "Jeep/JMessage.hh"
22 
23 namespace {
24  /**
25  * Additional options for formula.
26  */
27  static const char* const first_t = "first";
28 }
29 
30 
31 /**
32  * \file
33  * Auxiliary program to histogram bin-by-bin deviations of the contents of one (or more) 1D histogram(s).
34  * The option <tt>-f</tt> corresponds to <tt><file name>:<object name></tt>.
35  *
36  * The formula (option <tt>-F <formula></tt>) refers to a ROOT TFormula.
37  * The expression may contain member methods of the corresponding object.
38  *
39  * In case of multiple histograms, the formula is evaluated stand alone (i.e. without ROOT histogram).
40  * If formula equals "first", the bin-by-bin deviations of the contents of the histograms are
41  * evaluated with respect to the first histogram.
42  * \author mdejong
43  */
44 int main(int argc, char **argv)
45 {
46  using namespace std;
47  using namespace JPP;
48 
49  typedef JAbstractHistogram<Double_t> JHistogram_t;
50 
51  vector<JRootObjectID> inputFile;
52  string outputFile;
53  bool reuse;
54  JHistogram_t X;
55  TString formula;
56  bool reverse;
57  int debug;
58 
59  try {
60 
61  JParser<> zap("Auxiliary program to histogram bin-by-bin deviations of a set of 2D histograms.");
62 
63  zap['f'] = make_field(inputFile, "<input file>:<object name>");
64  zap['o'] = make_field(outputFile, "ROOT file with histogram (possibly I/O, option -r)") = "variance.root";
65  zap['r'] = make_field(reuse, "reuse histogram from existing output file");
66  zap['x'] = make_field(X, "histogram binning");
67  zap['F'] = make_field(formula, "ROOT TFormula (may contain method names of object)") = "0";
68  zap['R'] = make_field(reverse, "reverse sign");
69  zap['d'] = make_field(debug) = 1;
70 
71  zap(argc, argv);
72  }
73  catch(const exception &error) {
74  FATAL(error.what() << endl);
75  }
76 
77 
78  vector<TH2*> listOfHistograms;
79 
80  for (vector<JRootObjectID>::const_iterator input = inputFile.begin(); input != inputFile.end(); ++input) {
81 
82  TH2* h2 = dynamic_cast<TH2*>(getObject(*input));
83 
84  if (h2 != NULL) {
85  listOfHistograms.push_back(h2);
86  }
87  }
88 
89  if (listOfHistograms.empty()) {
90  FATAL("No histograms." << endl);
91  }
92 
93  if (formula == "") {
94  FATAL("Empty formula." << endl);
95  }
96 
97  TH1* h0 = NULL;
98 
99  if (reuse) {
100  h0 = dynamic_cast<TH1*>(getObject(JRootObjectID(outputFile, "h0")));
101  }
102 
103  if (h0 == NULL) {
104  h0 = new TH1D("h0", NULL,
105  X.getNumberOfBins(), X.getLowerLimit(), X.getUpperLimit());
106  }
107 
108 
109  TH2* h2 = listOfHistograms[0];
110 
111  if (listOfHistograms.size() == 1) {
112 
113  const double y1 = getResult(formula, h2);
114 
115  DEBUG(h2->GetName() << ' ' << formula << ' ' << y1 << endl);
116 
117  for (Int_t ix = 1; ix <= h2->GetNbinsX(); ++ix) {
118  for (Int_t iy = 1; iy <= h2->GetNbinsY(); ++iy) {
119 
120  const Double_t yp = h2->GetBinContent(ix,iy);
121 
122  h0->Fill(reverse ? y1 - yp : yp - y1);
123  }
124  }
125 
126  } else {
127 
128  if (formula == first_t) {
129 
130  for (Int_t ix = 1; ix <= h2->GetNbinsX(); ++ix) {
131  for (Int_t iy = 1; iy <= h2->GetNbinsY(); ++iy) {
132 
133  const double y1 = h2->GetBinContent(ix,iy);
134 
135  for (vector<TH2*>::const_iterator p = listOfHistograms.begin(); ++p != listOfHistograms.end(); ) {
136 
137  const Double_t yp = (*p)->GetBinContent(ix,iy);
138 
139  h0->Fill(reverse ? y1 - yp : yp - y1);
140  }
141  }
142  }
143 
144  } else {
145 
146  const double y1 = getResult(formula);
147 
148  for (Int_t ix = 1; ix <= h2->GetNbinsX(); ++ix) {
149  for (Int_t iy = 1; iy <= h2->GetNbinsY(); ++iy) {
150 
151  for (vector<TH2*>::const_iterator p = listOfHistograms.begin(); p != listOfHistograms.end(); ++p) {
152 
153  const Double_t yp = (*p)->GetBinContent(ix,iy);
154 
155  h0->Fill(reverse ? y1 - yp : yp - y1);
156  }
157  }
158  }
159  }
160  }
161 
162 
163  TFile out(outputFile.c_str(), "recreate");
164 
165  h0->Write();
166 
167  out.Write();
168  out.Close();
169 }
Utility class to parse command line options.
Definition: JParser.hh:1514
TObject * getObject(const JRootObjectID &id)
Get first TObject with given identifier.
int main(int argc, char *argv[])
Definition: Main.cc:15
string outputFile
#define make_field(A,...)
macro to convert parameter to JParserTemplateElement object
Definition: JParser.hh:1989
Double_t getResult(const TString &text, TObject *object=NULL)
Get result of given textual formula.
General purpose messaging.
#define FATAL(A)
Definition: JMessage.hh:67
Utility class to parse command line options.
no fit printf nominal n $STRING awk v X
int debug
debug level
#define DEBUG(A)
Message macros.
Definition: JMessage.hh:62