Jpp
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
JSum1D.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 "TH1.h"
12 #include "TString.h"
13 #include "TRegexp.h"
14 
15 #include "JTools/JRange.hh"
16 #include "JGizmo/JRootObjectID.hh"
17 #include "JGizmo/JGizmoToolkit.hh"
18 
19 #include "Jeep/JPrint.hh"
20 #include "Jeep/JParser.hh"
21 #include "Jeep/JMessage.hh"
22 
23 
24 /**
25  * \file
26  *
27  * Auxiliary program to make cumulative ROOT histogram.
28  * The option <tt>-f</tt> corresponds to <tt><file name>:<object name></tt>.
29  * \author mdejong
30  */
31 int main(int argc, char **argv)
32 {
33  using namespace std;
34  using namespace JPP;
35 
36  typedef JRange<double> JRange_t;
37 
38  vector<JRootObjectID> inputFile;
39  string outputFile;
40  JRange_t X;
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['R'] = make_field(reverse, "sum right to left instead of left to right");
53  zap['N'] = make_field(normalise, "normalise histogram contents");
54  zap['d'] = make_field(debug) = 0;
55 
56  zap(argc, argv);
57  }
58  catch(const exception &error) {
59  FATAL(error.what() << endl);
60  }
61 
62 
63  TFile out(outputFile.c_str(), "recreate");
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.Index(regexp) << " (-1 /= OK)" << endl);
85 
86  // option match
87 
88  if (tag.Index(regexp) != -1) {
89 
90  TObject* object = key->ReadObj();
91 
92  try {
93 
94  TH1& h1 = dynamic_cast<TH1&>(*object);
95 
96  double W = 0.0;
97 
98  if (!reverse) {
99 
100  for (Int_t i = 0; i <= h1.GetXaxis()->GetNbins() + 1; ++i) {
101 
102  if (X(h1.GetBinCenter(i))) {
103  W += h1.GetBinContent(i);
104  }
105 
106  h1.SetBinContent(i, W);
107  }
108 
109  } else {
110 
111  for (Int_t i = h1.GetXaxis()->GetNbins() + 1; i >= 0; --i) {
112 
113  if (X(h1.GetBinCenter(i))) {
114  W += h1.GetBinContent(i);
115  }
116 
117  h1.SetBinContent(i, W);
118  }
119  }
120 
121  NOTICE(setw(24) << left << h1.GetName() << ' ' << SCIENTIFIC(12,3) << W << endl);
122 
123  if (normalise && W != 0.0) {
124  h1.Scale(1.0/W);
125  }
126 
127  out.WriteTObject(&h1);
128  }
129  catch(exception&) {}
130  }
131  }
132  }
133 
134  out.Write();
135  out.Close();
136 }
Utility class to parse command line options.
Definition: JParser.hh:1410
Definition: JRoot.hh:19
string outputFile
I/O formatting auxiliaries.
#define make_field(A,...)
macro to convert parameter to JParserTemplateElement object
Definition: JParser.hh:1836
#define NOTICE(A)
Definition: JMessage.hh:62
#define ERROR(A)
Definition: JMessage.hh:64
int debug
debug level
Definition: JSirene.cc:59
General purpose messaging.
#define FATAL(A)
Definition: JMessage.hh:65
Auxiliary class to define a range between two values.
Utility class to parse command line options.
TDirectory * getDirectory(const JRootObjectID &id)
Get TDirectory pointer.
Auxiliary data structure for floating point format specification.
Definition: JPrint.hh:498
#define DEBUG(A)
Message macros.
Definition: JMessage.hh:60
int main(int argc, char *argv[])
Definition: Main.cpp:15