Jpp 19.3.0-rc.1
the software that should make you happy
Loading...
Searching...
No Matches
JSum1D.cc
Go to the documentation of this file.
1#include <string>
2#include <iostream>
3#include <iomanip>
4#include <cmath>
5
6#include "TROOT.h"
7#include "TFile.h"
8#include "TObject.h"
9#include "TKey.h"
10#include "TH1.h"
11#include "TString.h"
12#include "TRegexp.h"
13
14#include "JTools/JRange.hh"
17
18#include "Jeep/JPrint.hh"
19#include "Jeep/JParser.hh"
20#include "Jeep/JMessage.hh"
21
22
23/**
24 * \file
25 *
26 * Auxiliary program to make cumulative ROOT histogram.
27 * The option <tt>-f</tt> corresponds to <tt><file name>:<object name></tt>.
28 * \author mdejong
29 */
30int main(int argc, char **argv)
31{
32 using namespace std;
33 using namespace JPP;
34
35 typedef JRange<double> JRange_t;
36
37 vector<JRootObjectID> inputFile;
38 string outputFile;
39 JRange_t X;
40 bool binwidth;
41 bool reverse;
42 bool normalise;
43 int debug;
44
45 try {
46
47 JParser<> zap("Auxiliary program to make cumulative ROOT histogram.");
48
49 zap['f'] = make_field(inputFile, "<input file>:<object name>");
50 zap['o'] = make_field(outputFile, "ROOT file with histogram(s)") = "sum.root";
51 zap['x'] = make_field(X, "accepted x-range values") = JRange_t();
52 zap['B'] = make_field(binwidth, "multiply by bin width");
53 zap['R'] = make_field(reverse, "sum right to left instead of left to right");
54 zap['N'] = make_field(normalise, "normalise histogram contents");
55 zap['d'] = make_field(debug) = 0;
56
57 zap(argc, argv);
58 }
59 catch(const exception &error) {
60 FATAL(error.what() << endl);
61 }
62
63
64 TFile out(outputFile.c_str(), "recreate");
65
66 for (vector<JRootObjectID>::const_iterator input = inputFile.begin(); input != inputFile.end(); ++input) {
67
68 DEBUG("Input: " << *input << endl);
69
70 TDirectory* dir = getDirectory(*input);
71
72 if (dir == NULL) {
73 ERROR("File: " << input->getFullFilename() << " not opened." << endl);
74 continue;
75 }
76
77 const TRegexp regexp(input->getObjectName());
78
79 TIter iter(dir->GetListOfKeys());
80
81 for (TKey* key; (key = (TKey*) iter.Next()) != NULL; ) {
82
83 const TString tag(key->GetName());
84
85 DEBUG("Key: " << tag << " match = " << tag.Contains(regexp) << endl);
86
87 // option match
88
89 if (tag.Contains(regexp) && isTObject(key)) {
90
91 TObject* object = key->ReadObj();
92
93 try {
94
95 TH1& h1 = dynamic_cast<TH1&>(*object);
96
97 double W = 0.0;
98
99 if (!reverse) {
100
101 for (Int_t i = 0; i <= h1.GetXaxis()->GetNbins() + 1; ++i) {
102
103 if (X(h1.GetBinCenter(i))) {
104 W += h1.GetBinContent(i) * (binwidth ? h1.GetBinWidth(i) : 1.0);
105 }
106
107 h1.SetBinContent(i, W);
108
109 if (h1.GetSumw2N()) {
110 h1.SetBinError(i, h1.GetBinError(i) * (binwidth ? h1.GetBinWidth(i) : 1.0));
111 }
112 }
113
114 } else {
115
116 for (Int_t i = h1.GetXaxis()->GetNbins() + 1; i >= 0; --i) {
117
118 if (X(h1.GetBinCenter(i))) {
119 W += h1.GetBinContent(i) * (binwidth ? h1.GetBinWidth(i) : 1.0);
120 }
121
122 h1.SetBinContent(i, W);
123
124 if (h1.GetSumw2N()) {
125 h1.SetBinError(i, h1.GetBinError(i) * (binwidth ? h1.GetBinWidth(i) : 1.0));
126 }
127 }
128 }
129
130 NOTICE(setw(24) << left << h1.GetName() << ' ' << SCIENTIFIC(12,3) << W << endl);
131
132 if (normalise && W != 0.0) {
133 h1.Scale(1.0/W);
134 }
135
136 out.WriteTObject(&h1);
137 }
138 catch(exception&) {}
139 }
140 }
141 }
142
143 out.Write();
144 out.Close();
145}
string outputFile
General purpose messaging.
#define DEBUG(A)
Message macros.
Definition JMessage.hh:62
#define ERROR(A)
Definition JMessage.hh:66
#define NOTICE(A)
Definition JMessage.hh:64
#define FATAL(A)
Definition JMessage.hh:67
int debug
debug level
Definition JSirene.cc:72
Utility class to parse command line options.
#define make_field(A,...)
macro to convert parameter to JParserTemplateElement object
Definition JParser.hh:2142
I/O formatting auxiliaries.
Auxiliary class to define a range between two values.
int main(int argc, char **argv)
Definition JSum1D.cc:30
Utility class to parse command line options.
Definition JParser.hh:1698
Range of values.
Definition JRange.hh:42
This name space includes all other name spaces (except KM3NETDAQ, KM3NET and ANTARES).
Auxiliary data structure for floating point format specification.
Definition JManip.hh:488