Jpp  master_rocky-43-ge265d140c
the software that should make you happy
JQuantiles2D.cc
Go to the documentation of this file.
1 #include <string>
2 #include <iostream>
3 #include <iomanip>
4 #include <vector>
5 #include <cmath>
6 
7 #include "TROOT.h"
8 #include "TFile.h"
9 #include "TObject.h"
10 #include "TKey.h"
11 #include "TH2.h"
12 #include "TH1D.h"
13 #include "TString.h"
14 #include "TRegexp.h"
15 
16 #include "JGizmo/JRootObjectID.hh"
17 #include "JGizmo/JGizmoToolkit.hh"
18 
19 #include "Jeep/JParser.hh"
20 #include "Jeep/JMessage.hh"
21 
22 
23 /**
24  * \file
25  *
26  * Auxiliary program to extract quantiles from 2D histogram.
27  * The option <tt>-f</tt> corresponds to <tt><file name>:<object name></tt>.
28  * \author mdejong
29  */
30 int main(int argc, char **argv)
31 {
32  using namespace std;
33  using namespace JPP;
34 
35  vector<JRootObjectID> inputFile;
36  string outputFile;
38  bool reverse;
39  int debug;
40 
41  try {
42 
43  JParser<> zap("Auxiliary program to extract quantiles from 2D histogram.");
44 
45  zap['f'] = make_field(inputFile);
46  zap['o'] = make_field(outputFile) = "quantiles.root";
47  zap['Q'] = make_field(Q);
48  zap['R'] = make_field(reverse);
49  zap['d'] = make_field(debug) = 0;
50 
51  zap(argc, argv);
52  }
53  catch(const exception &error) {
54  FATAL(error.what() << endl);
55  }
56 
57 
58  if (Q.empty()) {
59  FATAL("No quantiles." << endl);
60  }
61 
62  TFile out(outputFile.c_str(), "recreate");
63 
64 
65  for (vector<JRootObjectID>::const_iterator input = inputFile.begin(); input != inputFile.end(); ++input) {
66 
67  DEBUG("Input: " << *input << endl);
68 
69  TDirectory* dir = getDirectory(*input);
70 
71  if (dir == NULL) {
72  ERROR("File: " << input->getFullFilename() << " not opened." << endl);
73  continue;
74  }
75 
76  const TRegexp regexp(input->getObjectName());
77 
78  TIter iter(dir->GetListOfKeys());
79 
80  for (TKey* key; (key = (TKey*) iter.Next()) != NULL; ) {
81 
82  const TString tag(key->GetName());
83 
84  DEBUG("Key: " << tag << " match = " << tag.Contains(regexp) << endl);
85 
86  // option match
87 
88  if (tag.Contains(regexp) && isTObject(key)) {
89 
90  TObject* object = key->ReadObj();
91 
92  try {
93 
94  TH2& h2 = dynamic_cast<TH2&>(*object);
95 
96  vector<TH1D*> h1(Q.size(), NULL);
97 
98  for (size_t i = 0; i != Q.size(); ++i) {
99 
100  ostringstream os;
101 
102  os << h2.GetName();
103  os << i << "[" << setw(3) << (int) (100.0 * Q[i]) << "%]";
104 
105  DEBUG("Creating 1D histogram: " << os.str() << endl);
106 
107  TAxis* axis = h2.GetXaxis();
108 
109  if (axis->IsVariableBinSize())
110  h1[i] = new TH1D(os.str().c_str(), NULL, axis->GetNbins(), axis->GetXbins()->GetArray());
111  else
112  h1[i] = new TH1D(os.str().c_str(), NULL, axis->GetNbins(), axis->GetXmin(), axis->GetXmax());
113  }
114 
115 
116  for (Int_t i = 1; i <= h2.GetXaxis()->GetNbins(); ++i) {
117 
118  Double_t W = 0.0;
119 
120  for (Int_t j = 0; j <= h2.GetYaxis()->GetNbins() + 1; ++j) {
121  W += h2.GetBinContent(i,j);
122  }
123 
124  if (W != 0.0) {
125 
126  Double_t w = 0.0;
127 
128  if (!reverse) {
129 
130  for (int j = 0, k = 0; j <= h2.GetYaxis()->GetNbins() + 1; ++j) {
131 
132  w += h2.GetBinContent(i,j);
133 
134  for ( ; k != (int) h1.size() && w >= Q[k]*W; ++k) {
135  h1[k]->SetBinContent(i, h2.GetYaxis()->GetBinCenter(j));
136  }
137  }
138 
139  } else {
140 
141  for (int j = h2.GetYaxis()->GetNbins() + 1, k = 0; j >= 0; --j) {
142 
143  w += h2.GetBinContent(i,j);
144 
145  for ( ; k != (int) h1.size() && w >= Q[k]*W; ++k) {
146  h1[k]->SetBinContent(i, h2.GetYaxis()->GetBinCenter(j));
147  }
148  }
149  }
150  }
151  }
152 
153  out.cd();
154 
155  for (vector<TH1D*>::iterator i = h1.begin(); i != h1.end(); ++i) {
156  (*i)->Write();
157  }
158  }
159  catch(exception&) {
160  ERROR("Not available for other objects than 2D histograms." << endl);
161  }
162  }
163  }
164  }
165 
166  out.Write();
167  out.Close();
168 }
string outputFile
General purpose messaging.
#define DEBUG(A)
Message macros.
Definition: JMessage.hh:62
#define ERROR(A)
Definition: JMessage.hh:66
#define FATAL(A)
Definition: JMessage.hh:67
int debug
debug level
Definition: JSirene.cc:69
Utility class to parse command line options.
#define make_field(A,...)
macro to convert parameter to JParserTemplateElement object
Definition: JParser.hh:2142
int main(int argc, char **argv)
Definition: JQuantiles2D.cc:30
Utility class to parse command line options.
Definition: JParser.hh:1698
TDirectory * getDirectory(const JRootObjectID &id)
Get TDirectory pointer.
bool isTObject(const TKey *key)
Check if given key corresponds to a TObject.
This name space includes all other name spaces (except KM3NETDAQ, KM3NET and ANTARES).
data_type w[N+1][M+1]
Definition: JPolint.hh:867
int j
Definition: JPolint.hh:792
Definition: JSTDTypes.hh:14
Definition: JRoot.hh:19