Jpp  18.0.0-rc.2
the software that should make you happy
 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 "TGraph.h"
13 #include "TGraphErrors.h"
14 #include "TString.h"
15 #include "TRegexp.h"
16 
17 #include "JTools/JRange.hh"
18 #include "JGizmo/JRootObjectID.hh"
19 #include "JGizmo/JGizmoToolkit.hh"
20 
21 #include "Jeep/JParser.hh"
22 #include "Jeep/JMessage.hh"
23 
24 
25 /**
26  * \file
27  *
28  * Auxiliary program to scale ROOT histograms.
29  * The option <tt>-f</tt> corresponds to <tt><file name>:<object name></tt>.
30  *
31  * The scale factor (option <tt>-F <factor></tt>) refers to a ROOT TFormula.
32  * The expression may contain member methods of the corresponding object.
33  * \author mdejong
34  */
35 int main(int argc, char **argv)
36 {
37  using namespace std;
38  using namespace JPP;
39 
40  typedef JRange<double> JRange_t;
41 
42  vector<JRootObjectID> inputFile;
43  string outputFile;
44  TString formula;
45  JRange_t X;
46  JRange_t Y;
47  string option;
48  int debug;
49 
50  try {
51 
52  JParser<> zap("Auxiliary program to scale ROOT histograms."\
53  "\nNote that the formula may contain method names of the specified object.");
54 
55  zap['f'] = make_field(inputFile, "<input file>:<object name>");
56  zap['o'] = make_field(outputFile, "ROOT file with histogram(s)") = "scale.root";
57  zap['F'] = make_field(formula, "ROOT TFormula (may contain method names of object)");
58  zap['x'] = make_field(X, "abscissa range (only for Integral)") = JRange_t();
59  zap['y'] = make_field(Y, "abscissa range (only for Integral)") = JRange_t();
60  zap['O'] = make_field(option) = "", "nosw2";
61  zap['d'] = make_field(debug) = 0;
62 
63  zap(argc, argv);
64  }
65  catch(const exception &error) {
66  FATAL(error.what() << endl);
67  }
68 
69 
70  TFile out(outputFile.c_str(), "recreate");
71 
72  for (vector<JRootObjectID>::const_iterator input = inputFile.begin(); input != inputFile.end(); ++input) {
73 
74  DEBUG("Input: " << *input << endl);
75 
76  TDirectory* dir = getDirectory(*input);
77 
78  if (dir == NULL) {
79  ERROR("File: " << input->getFullFilename() << " not opened." << endl);
80  continue;
81  }
82 
83  const TRegexp regexp(input->getObjectName());
84 
85  TIter iter(dir->GetListOfKeys());
86 
87  for (TKey* key; (key = (TKey*) iter.Next()) != NULL; ) {
88 
89  const TString tag(key->GetName());
90 
91  DEBUG("Key: " << tag << " match = " << tag.Contains(regexp) << endl);
92 
93  // option match
94 
95  if (tag.Contains(regexp) && isTObject(key)) {
96 
97  TObject* object = key->ReadObj();
98 
99  TH1* h1 = dynamic_cast<TH1*> (object);
100  TGraphErrors* ge = dynamic_cast<TGraphErrors*>(object);
101  TGraph* g1 = dynamic_cast<TGraph*> (object);
102 
103  if (h1 != NULL ||
104  ge != NULL ||
105  g1 != NULL) {
106 
107  const double factor = getResult(formula, object);
108 
109  if (h1 != NULL) {
110 
111  if (X != JRange_t()) { h1->GetXaxis()->SetRangeUser(X.getLowerLimit(), X.getUpperLimit()); }
112  if (Y != JRange_t()) { h1->GetYaxis()->SetRangeUser(Y.getLowerLimit(), Y.getUpperLimit()); }
113 
114  h1->Scale(factor, option.c_str());
115 
116  } else if (ge != NULL) {
117 
118  for (Int_t i = 0; i != ge->GetN(); ++i) {
119  ge->GetY() [i] *= factor;
120  ge->GetEY()[i] *= factor;
121  }
122 
123  } else if (g1 != NULL) {
124 
125  for (Int_t i = 0; i != g1->GetN(); ++i) {
126  g1->GetY() [i] *= factor;
127  }
128  }
129 
130  out.WriteTObject(object);
131  }
132  }
133  }
134  }
135 
136  out.Write();
137  out.Close();
138 }
Utility class to parse command line options.
Definition: JParser.hh:1514
int main(int argc, char *argv[])
Definition: Main.cc:15
Definition: JRoot.hh:19
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
string outputFile
#define make_field(A,...)
macro to convert parameter to JParserTemplateElement object
Definition: JParser.hh:1989
Double_t getResult(const TString &text, TObject *object=NULL)
Get result of given textual formula.
#define ERROR(A)
Definition: JMessage.hh:66
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.
bool isTObject(const TKey *key)
Check if given key corresponds to a TObject.
no fit printf nominal n $STRING awk v X
TDirectory * getDirectory(const JRootObjectID &id)
Get TDirectory pointer.
int debug
debug level
#define DEBUG(A)
Message macros.
Definition: JMessage.hh:62
Double_t g1(const Double_t x)
Function.
Definition: JQuantiles.cc:25