Jpp  15.0.5
the software that should make you happy
 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 <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"
15 #include "JGizmo/JRootObjectID.hh"
16 #include "JGizmo/JGizmoToolkit.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  */
30 int 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 reverse;
41  bool normalise;
42  int debug;
43 
44  try {
45 
46  JParser<> zap("Auxiliary program to make cumulative ROOT histogram.");
47 
48  zap['f'] = make_field(inputFile, "<input file>:<object name>");
49  zap['o'] = make_field(outputFile, "ROOT file with histogram(s)") = "sum.root";
50  zap['x'] = make_field(X, "accepted x-range values") = JRange_t();
51  zap['R'] = make_field(reverse, "sum right to left instead of left to right");
52  zap['N'] = make_field(normalise, "normalise histogram contents");
53  zap['d'] = make_field(debug) = 0;
54 
55  zap(argc, argv);
56  }
57  catch(const exception &error) {
58  FATAL(error.what() << endl);
59  }
60 
61 
62  TFile out(outputFile.c_str(), "recreate");
63 
64  for (vector<JRootObjectID>::const_iterator input = inputFile.begin(); input != inputFile.end(); ++input) {
65 
66  DEBUG("Input: " << *input << endl);
67 
68  TDirectory* dir = getDirectory(*input);
69 
70  if (dir == NULL) {
71  ERROR("File: " << input->getFullFilename() << " not opened." << endl);
72  continue;
73  }
74 
75  const TRegexp regexp(input->getObjectName());
76 
77  TIter iter(dir->GetListOfKeys());
78 
79  for (TKey* key; (key = (TKey*) iter.Next()) != NULL; ) {
80 
81  const TString tag(key->GetName());
82 
83  DEBUG("Key: " << tag << " match = " << tag.Contains(regexp) << endl);
84 
85  // option match
86 
87  if (tag.Contains(regexp)) {
88 
89  TObject* object = key->ReadObj();
90 
91  try {
92 
93  TH1& h1 = dynamic_cast<TH1&>(*object);
94 
95  double W = 0.0;
96 
97  if (!reverse) {
98 
99  for (Int_t i = 0; i <= h1.GetXaxis()->GetNbins() + 1; ++i) {
100 
101  if (X(h1.GetBinCenter(i))) {
102  W += h1.GetBinContent(i);
103  }
104 
105  h1.SetBinContent(i, W);
106  }
107 
108  } else {
109 
110  for (Int_t i = h1.GetXaxis()->GetNbins() + 1; i >= 0; --i) {
111 
112  if (X(h1.GetBinCenter(i))) {
113  W += h1.GetBinContent(i);
114  }
115 
116  h1.SetBinContent(i, W);
117  }
118  }
119 
120  NOTICE(setw(24) << left << h1.GetName() << ' ' << SCIENTIFIC(12,3) << W << endl);
121 
122  if (normalise && W != 0.0) {
123  h1.Scale(1.0/W);
124  }
125 
126  out.WriteTObject(&h1);
127  }
128  catch(exception&) {}
129  }
130  }
131  }
132 
133  out.Write();
134  out.Close();
135 }
Utility class to parse command line options.
Definition: JParser.hh:1500
int main(int argc, char *argv[])
Definition: Main.cc:15
Definition: JRoot.hh:19
then for HISTOGRAM in h0 h1
Definition: JMatrixNZ.sh:71
string outputFile
I/O formatting auxiliaries.
#define make_field(A,...)
macro to convert parameter to JParserTemplateElement object
Definition: JParser.hh:1961
#define NOTICE(A)
Definition: JMessage.hh:64
#define ERROR(A)
Definition: JMessage.hh:66
then break fi done getCenter read X Y Z let X
int debug
debug level
Definition: JSirene.cc:63
General purpose messaging.
#define FATAL(A)
Definition: JMessage.hh:67
Auxiliary class to define a range between two values.
Utility class to parse command line options.
then usage $script< input_file >< detector_file > fi set_variable OUTPUT_DIR set_variable SELECTOR JDAQTimesliceL1 set_variable DEBUG case set_variable DEBUG
TDirectory * getDirectory(const JRootObjectID &id)
Get TDirectory pointer.
Auxiliary data structure for floating point format specification.
Definition: JManip.hh:484