Jpp
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
JScale1D.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 "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/JParser.hh"
20 #include "Jeep/JMessage.hh"
21 
22 
23 /**
24  * \file
25  *
26  * Auxiliary program to scale ROOT histograms.
27  * The option <tt>-f</tt> corresponds to <tt><file name>:<object name></tt>.
28  *
29  * The scale factor (option <tt>-F <factor></tt>) refers to a ROOT TFormula.
30  * The expression may contain member methods of the corresponding object.
31  * \author mdejong
32  */
33 int main(int argc, char **argv)
34 {
35  using namespace std;
36  using namespace JPP;
37 
38  typedef JRange<double> JRange_t;
39 
40  vector<JRootObjectID> inputFile;
41  string outputFile;
42  TString formula;
43  JRange_t X;
44  JRange_t Y;
45  int debug;
46 
47  try {
48 
49  JParser<> zap("Auxiliary program to scale ROOT histograms."\
50  "\nNote that the formula may contain method names of the specified object.");
51 
52  zap['f'] = make_field(inputFile, "<input file>:<object name>");
53  zap['o'] = make_field(outputFile, "ROOT file with histogram(s)") = "scale.root";
54  zap['F'] = make_field(formula, "ROOT TFormula (may contain method names of object)");
55  zap['x'] = make_field(X, "abscissa range (only for Integral)") = JRange_t();
56  zap['y'] = make_field(Y, "abscissa range (only for Integral)") = JRange_t();
57  zap['d'] = make_field(debug) = 0;
58 
59  zap(argc, argv);
60  }
61  catch(const exception &error) {
62  FATAL(error.what() << endl);
63  }
64 
65 
66  TFile out(outputFile.c_str(), "recreate");
67 
68  for (vector<JRootObjectID>::const_iterator input = inputFile.begin(); input != inputFile.end(); ++input) {
69 
70  DEBUG("Input: " << *input << endl);
71 
72  TDirectory* dir = getDirectory(*input);
73 
74  if (dir == NULL) {
75  ERROR("File: " << input->getFullFilename() << " not opened." << endl);
76  continue;
77  }
78 
79  const TRegexp regexp(input->getObjectName());
80 
81  TIter iter(dir->GetListOfKeys());
82 
83  for (TKey* key; (key = (TKey*) iter.Next()) != NULL; ) {
84 
85  const TString tag(key->GetName());
86 
87  DEBUG("Key: " << tag << " match = " << tag.Contains(regexp) << endl);
88 
89  // option match
90 
91  if (tag.Contains(regexp)) {
92 
93  TObject* object = key->ReadObj();
94 
95  try {
96 
97  TH1& h1 = dynamic_cast<TH1&>(*object);
98 
99  if (X != JRange_t()) { h1.GetXaxis()->SetRangeUser(X.getLowerLimit(), X.getUpperLimit()); }
100  if (Y != JRange_t()) { h1.GetYaxis()->SetRangeUser(Y.getLowerLimit(), Y.getUpperLimit()); }
101 
102  double factor = getResult(formula, object);
103 
104  DEBUG(h1.GetName() << ' ' << formula << ' ' << factor << endl);
105 
106  h1.Scale(factor);
107 
108  out.WriteTObject(&h1);
109  }
110  catch(exception&) {}
111  }
112  }
113  }
114 
115  out.Write();
116  out.Close();
117 }
Utility class to parse command line options.
Definition: JParser.hh:1493
Definition: JRoot.hh:19
then for HISTOGRAM in h0 h1
Definition: JMatrixNZ.sh:69
string outputFile
#define make_field(A,...)
macro to convert parameter to JParserTemplateElement object
Definition: JParser.hh:1954
Double_t getResult(const TString &text, TObject *object=NULL)
Get result of given textual formula.
#define ERROR(A)
Definition: JMessage.hh:66
int debug
debug level
Definition: JSirene.cc:61
General purpose messaging.
#define FATAL(A)
Definition: JMessage.hh:67
JRange< Double_t > JRange_t
Definition: JFitToT.hh:34
Auxiliary class to define a range between two values.
Utility class to parse command line options.
TDirectory * getDirectory(const JRootObjectID &id)
Get TDirectory pointer.
#define DEBUG(A)
Message macros.
Definition: JMessage.hh:62
int main(int argc, char *argv[])
Definition: Main.cpp:15