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  string option;
46  int debug;
47 
48  try {
49 
50  JParser<> zap("Auxiliary program to scale ROOT histograms."\
51  "\nNote that the formula may contain method names of the specified object.");
52 
53  zap['f'] = make_field(inputFile, "<input file>:<object name>");
54  zap['o'] = make_field(outputFile, "ROOT file with histogram(s)") = "scale.root";
55  zap['F'] = make_field(formula, "ROOT TFormula (may contain method names of object)");
56  zap['x'] = make_field(X, "abscissa range (only for Integral)") = JRange_t();
57  zap['y'] = make_field(Y, "abscissa range (only for Integral)") = JRange_t();
58  zap['O'] = make_field(option) = "", "nosw2";
59  zap['d'] = make_field(debug) = 0;
60 
61  zap(argc, argv);
62  }
63  catch(const exception &error) {
64  FATAL(error.what() << endl);
65  }
66 
67 
68  TFile out(outputFile.c_str(), "recreate");
69 
70  for (vector<JRootObjectID>::const_iterator input = inputFile.begin(); input != inputFile.end(); ++input) {
71 
72  DEBUG("Input: " << *input << endl);
73 
74  TDirectory* dir = getDirectory(*input);
75 
76  if (dir == NULL) {
77  ERROR("File: " << input->getFullFilename() << " not opened." << endl);
78  continue;
79  }
80 
81  const TRegexp regexp(input->getObjectName());
82 
83  TIter iter(dir->GetListOfKeys());
84 
85  for (TKey* key; (key = (TKey*) iter.Next()) != NULL; ) {
86 
87  const TString tag(key->GetName());
88 
89  DEBUG("Key: " << tag << " match = " << tag.Contains(regexp) << endl);
90 
91  // option match
92 
93  if (tag.Contains(regexp)) {
94 
95  TObject* object = key->ReadObj();
96 
97  try {
98 
99  TH1& h1 = dynamic_cast<TH1&>(*object);
100 
101  if (X != JRange_t()) { h1.GetXaxis()->SetRangeUser(X.getLowerLimit(), X.getUpperLimit()); }
102  if (Y != JRange_t()) { h1.GetYaxis()->SetRangeUser(Y.getLowerLimit(), Y.getUpperLimit()); }
103 
104  double factor = getResult(formula, object);
105 
106  DEBUG(h1.GetName() << ' ' << formula << ' ' << factor << endl);
107 
108  h1.Scale(factor, option.c_str());
109 
110  out.WriteTObject(&h1);
111  }
112  catch(exception&) {}
113  }
114  }
115  }
116 
117  out.Write();
118  out.Close();
119 }
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