Jpp 20.0.0-195-g190c9e876
the software that should make you happy
Loading...
Searching...
No Matches
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
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.\n
27 * The option <tt>-f</tt> corresponds to <tt><file name>:<object name></tt>.\n
28 * The quantiles are computed for the y-axis.\n
29 * The computation of the quantiles includes under and overflows.
30 * \author mdejong
31 */
32int main(int argc, char **argv)
33{
34 using namespace std;
35 using namespace JPP;
36
37 vector<JRootObjectID> inputFile;
38 string outputFile;
40 bool reverse;
41 int debug;
42
43 try {
44
45 JParser<> zap("Auxiliary program to extract quantiles from 2D histogram.");
46
47 zap['f'] = make_field(inputFile);
48 zap['o'] = make_field(outputFile) = "quantiles.root";
49 zap['Q'] = make_field(Q);
50 zap['R'] = make_field(reverse);
51 zap['d'] = make_field(debug) = 0;
52
53 zap(argc, argv);
54 }
55 catch(const exception &error) {
56 FATAL(error.what() << endl);
57 }
58
59
60 if (Q.empty()) {
61 FATAL("No quantiles." << endl);
62 }
63
64 TFile out(outputFile.c_str(), "recreate");
65
66
67 for (vector<JRootObjectID>::const_iterator input = inputFile.begin(); input != inputFile.end(); ++input) {
68
69 DEBUG("Input: " << *input << endl);
70
71 TDirectory* dir = getDirectory(*input);
72
73 if (dir == NULL) {
74 ERROR("File: " << input->getFullFilename() << " not opened." << endl);
75 continue;
76 }
77
78 const TRegexp regexp(input->getObjectName());
79
80 TIter iter(dir->GetListOfKeys());
81
82 for (TKey* key; (key = (TKey*) iter.Next()) != NULL; ) {
83
84 const TString tag(key->GetName());
85
86 DEBUG("Key: " << tag << " match = " << tag.Contains(regexp) << endl);
87
88 // option match
89
90 if (tag.Contains(regexp) && isTObject(key)) {
91
92 TObject* object = key->ReadObj();
93
94 try {
95
96 TH2& h2 = dynamic_cast<TH2&>(*object);
97
98 vector<TH1D*> h1(Q.size(), NULL);
99
100 for (size_t i = 0; i != Q.size(); ++i) {
101
102 ostringstream os;
103
104 os << h2.GetName();
105 os << i << "[" << setw(3) << (int) (100.0 * Q[i]) << "%]";
106
107 DEBUG("Creating 1D histogram: " << os.str() << endl);
108
109 TAxis* axis = h2.GetXaxis();
110
111 if (axis->IsVariableBinSize())
112 h1[i] = new TH1D(os.str().c_str(), NULL, axis->GetNbins(), axis->GetXbins()->GetArray());
113 else
114 h1[i] = new TH1D(os.str().c_str(), NULL, axis->GetNbins(), axis->GetXmin(), axis->GetXmax());
115 }
116
117
118 for (Int_t ix = 1; ix <= h2.GetXaxis()->GetNbins(); ++ix) {
119
120 Double_t W = 0.0;
121
122 for (Int_t iy = 0; iy <= h2.GetYaxis()->GetNbins() + 1; ++iy) {
123 W += h2.GetBinContent(ix,iy);
124 }
125
126 if (W != 0.0) {
127
128 Double_t w = 0.0;
129
130 if (!reverse) {
131
132 for (int iy = 0, k = 0; iy <= h2.GetYaxis()->GetNbins() + 1; ++iy) {
133
134 w += h2.GetBinContent(ix,iy);
135
136 for ( ; k != (int) h1.size() && w >= Q[k]*W; ++k) {
137 h1[k]->SetBinContent(ix, h2.GetYaxis()->GetBinCenter(iy));
138 }
139 }
140
141 } else {
142
143 for (int iy = h2.GetYaxis()->GetNbins() + 1, k = 0; iy >= 0; --iy) {
144
145 w += h2.GetBinContent(ix,iy);
146
147 for ( ; k != (int) h1.size() && w >= Q[k]*W; ++k) {
148 h1[k]->SetBinContent(ix, h2.GetYaxis()->GetBinCenter(iy));
149 }
150 }
151 }
152 }
153 }
154
155 out.cd();
156
157 for (vector<TH1D*>::iterator i = h1.begin(); i != h1.end(); ++i) {
158 (*i)->Write();
159 }
160 }
161 catch(exception&) {
162 ERROR("Not available for other objects than 2D histograms." << endl);
163 }
164 }
165 }
166 }
167
168 out.Write();
169 out.Close();
170}
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:74
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)
Utility class to parse command line options.
Definition JParser.hh:1698
This name space includes all other name spaces (except KM3NETDAQ, KM3NET and ANTARES).