Jpp  pmt_effective_area_update
the software that should make you happy
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
JRebin1D.cc
Go to the documentation of this file.
1 #include <string>
2 #include <iostream>
3 #include <iomanip>
4 #include <sstream>
5 #include <cmath>
6 
7 #include "TROOT.h"
8 #include "TFile.h"
9 #include "TObject.h"
10 #include "TKey.h"
11 #include "TH1D.h"
12 #include "TString.h"
13 #include "TRegexp.h"
14 
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 rebin ROOT histograms.
27  * The option <tt>-f</tt> corresponds to <tt><file name>:<object name></tt>.
28  *
29  * \author mdejong
30  */
31 int main(int argc, char **argv)
32 {
33  using namespace std;
34  using namespace JPP;
35 
36  vector<JRootObjectID> inputFile;
37  string outputFile;
39  int debug;
40 
41  try {
42 
43  JParser<> zap("Auxiliary program to rebin ROOT histograms.");
44 
45  zap['f'] = make_field(inputFile, "<input file>:<object name>");
46  zap['o'] = make_field(outputFile, "ROOT file with histogram(s)") = "rebin.root";
47  zap['x'] = make_field(X, "abscissa values");
48  zap['d'] = make_field(debug) = 0;
49 
50  zap(argc, argv);
51  }
52  catch(const exception &error) {
53  FATAL(error.what() << endl);
54  }
55 
56 
57  if (X.empty()) {
58  FATAL("No abscissa values." << endl);
59  }
60 
61  TFile out(outputFile.c_str(), "recreate");
62 
63  for (vector<JRootObjectID>::const_iterator input = inputFile.begin(); input != inputFile.end(); ++input) {
64 
65  DEBUG("Input: " << *input << endl);
66 
67  TDirectory* dir = getDirectory(*input);
68 
69  if (dir == NULL) {
70  ERROR("File: " << input->getFullFilename() << " not opened." << endl);
71  continue;
72  }
73 
74  const TRegexp regexp(input->getObjectName());
75 
76  TIter iter(dir->GetListOfKeys());
77 
78  for (TKey* key; (key = (TKey*) iter.Next()) != NULL; ) {
79 
80  const TString tag(key->GetName());
81 
82  DEBUG("Key: " << tag << " match = " << tag.Contains(regexp) << endl);
83 
84  // option match
85 
86  if (tag.Contains(regexp)) {
87 
88  TObject* object = key->ReadObj();
89 
90  try {
91 
92  TH1D& h0 = dynamic_cast<TH1D&>(*object);
93 
94  TH1D h1(h0.GetName(), NULL, X.size() - 1, X.data());
95 
96  for (int ix = 1; ix <= h1.GetXaxis()->GetNbins(); ++ix) {
97 
98  const Double_t xmin = h0.GetXaxis()->FindBin(h1.GetXaxis()->GetBinLowEdge(ix));
99  const Double_t xmax = h0.GetXaxis()->FindBin(h1.GetXaxis()->GetBinUpEdge (ix));
100 
101  const Int_t imin = xmin;
102  const Int_t imax = xmax;
103 
104  if (imax != imin) {
105 
106  Double_t Y = 0.0;
107  Double_t Z = 0.0;
108  Double_t W = 0.0;
109 
110  for (Int_t i = imin; i != imax; ++i) {
111 
112  const Double_t y = h0.GetBinContent(i);
113  const Double_t z = h0.GetBinWidth (i);
114  const Double_t w = h0.GetBinError (i);
115 
116  Y += y*z;
117  Z += z;
118  W += w*z * w*z;
119  }
120 
121  Y = Y / Z;
122  W = sqrt(W) / Z;
123 
124  h1.SetBinContent(ix, Y);
125  h1.SetBinError (ix, W);
126 
127  } else {
128 
129  ERROR("Invalid bin " << ix << " [" << FIXED(9,3) << xmin << "," << FIXED(9,3) << xmax << "]" << " -> " << imin << ' ' << imax << endl);
130  }
131  }
132 
133  out.WriteTObject(&h1);
134  }
135  catch(exception&) {}
136  }
137  }
138  }
139 
140  out.Write();
141  out.Close();
142 }
Utility class to parse command line options.
Definition: JParser.hh:1500
data_type w[N+1][M+1]
Definition: JPolint.hh:741
int main(int argc, char *argv[])
Definition: Main.cc:15
Definition: JRoot.hh:19
then for HISTOGRAM in h0 h1
Definition: JMatrixNZ.sh:71
then fatal Wrong number of arguments fi set_variable STRING $argv[1] set_variable DETECTORXY_TXT $WORKDIR $DETECTORXY_TXT tail read X Y CHI2 RMS printf optimum n $X $Y $CHI2 $RMS awk v Y
Auxiliary data structure for floating point format specification.
Definition: JManip.hh:446
string outputFile
I/O formatting auxiliaries.
#define make_field(A,...)
macro to convert parameter to JParserTemplateElement object
Definition: JParser.hh:1961
#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
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.