Jpp
JPrintResult.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 "TString.h"
12 #include "TRegexp.h"
13 
14 #include "JGizmo/JRootObjectID.hh"
15 #include "JGizmo/JGizmoToolkit.hh"
16 
17 #include "Jeep/JParser.hh"
18 #include "Jeep/JMessage.hh"
19 #include "Jeep/JPrint.hh"
20 
21 
22 /**
23  * Auxiliary class for custom I/O.
24  */
25 struct JFormula :
26  public TString
27 {};
28 
29 /**
30  * Read formula from input stream.
31  *
32  * \param in input stream
33  * \param object formula
34  * \return input stream
35  */
36 inline std::istream& operator>>(std::istream& in, JFormula& object)
37 {
38  return object.ReadLine(in);
39 }
40 
41 /**
42  * Write formula to output stream.
43  *
44  * \param out output stream
45  * \param object formula
46  * \return output stream
47  */
48 inline std::ostream& operator<<(std::ostream& out, const JFormula& object)
49 {
50  return out << object.Data();
51 }
52 
53 
54 /**
55  * \file
56  *
57  * Auxiliary program to print result from ROOT histograms.
58  * The option <tt>-f</tt> corresponds to <tt><file name>:<object name></tt>.
59  *
60  * The formula (option <tt>-F <formula></tt>) refers to a ROOT TFormula.
61  * The expression may contain member methods of the corresponding object.
62  * \author mdejong
63  */
64 int main(int argc, char **argv)
65 {
66  using namespace std;
67  using namespace JPP;
68 
69  vector<JRootObjectID> inputFile;
70  vector<JFormula> formula;
71  TString option;
72  int debug;
73 
74  try {
75 
76  JParser<> zap("Auxiliary program to print result from ROOT histograms."\
77  "\nNote that the formula may contain method names of the specified object.");
78 
79  zap['f'] = make_field(inputFile, "<input file>:<object name>");
80  zap['F'] = make_field(formula, "ROOT TFormula (may contain method names of object)") = JPARSER::initialised();
81  zap['O'] = make_field(option, "format, e.g. \"%s %d\", where '%s' will be replaced by name and '%d' by value") = "";
82  zap['d'] = make_field(debug) = 0;
83 
84  zap(argc, argv);
85  }
86  catch(const exception &error) {
87  FATAL(error.what() << endl);
88  }
89 
90 
91  const TRegexp STRING("%[+-]?[0-9]*s *");
92  const TRegexp DOUBLE("%[+-]?[0-9]*\\.?[0-9]*f");
93 
94 
95  for (vector<JRootObjectID>::const_iterator input = inputFile.begin(); input != inputFile.end(); ++input) {
96 
97  DEBUG("Input: " << *input << endl);
98 
99  TDirectory* dir = getDirectory(*input);
100 
101  if (dir == NULL) {
102  ERROR("File: " << input->getFullFilename() << " not opened." << endl);
103  continue;
104  }
105 
106  const TRegexp regexp(input->getObjectName());
107 
108  TIter iter(dir->GetListOfKeys());
109 
110  for (TKey* key; (key = (TKey*) iter.Next()) != NULL; ) {
111 
112  const TString tag(key->GetName());
113 
114  DEBUG("Key: " << tag << " match = " << tag.Contains(regexp) << endl);
115 
116  // option match
117 
118  if (tag.Contains(regexp)) {
119 
120  TObject* object = key->ReadObj();
121 
122  try {
123 
124  if (option != "") {
125 
126  TString buffer = option;
127 
128  Ssiz_t len;
129  Ssiz_t pos;
130 
131  pos = buffer.Index(STRING, &len);
132 
133  if (pos != -1) {
134  buffer.Replace(pos, len, TString::Format(TString(buffer(pos, len).Data(), len), object->GetName()));
135  }
136 
137  TString sub("%12.3f");
138 
139  for (vector<JFormula>::const_iterator i = formula.begin(); i != formula.end(); ++i) {
140 
141  const double result = getResult(*i, object);
142 
143  pos = buffer.Index(DOUBLE, &len);
144 
145  if (pos != -1) {
146 
147  sub = TString(buffer(pos, len).Data(), len);
148 
149  buffer.Replace(pos, len, TString::Format(sub, result));
150 
151  } else {
152 
153  if (i != formula.begin()) {
154  buffer.Append(" ");
155  }
156 
157  buffer.Append(TString::Format(sub, result));
158  }
159  }
160  cout << buffer << endl;
161 
162  } else {
163 
164  for (vector<JFormula>::const_iterator i = formula.begin(); i != formula.end(); ++i) {
165  cout << ' ' << FIXED(12,3) << getResult(*i, object);
166  }
167  }
168  }
169  catch(exception&) {}
170  }
171  }
172  }
173 }
TObject
Definition: JRoot.hh:19
FIXED
Auxiliary data structure for floating point format specification.
Definition: JPrint.hh:481
JMessage.hh
JFormula
Auxiliary class for custom I/O.
Definition: JPrintResult.cc:25
JPrint.hh
JPARSER::initialised
Empty structure for specification of parser element that is initialised (i.e.
Definition: JParser.hh:63
JGIZMO::getResult
Double_t getResult(const TString &text, TObject *object=NULL)
Get result of given textual formula.
Definition: JGizmoToolkit.hh:212
std::vector
Definition: JSTDTypes.hh:12
JPARSER::JParser
Utility class to parse command line options.
Definition: JParser.hh:1493
JGizmoToolkit.hh
JPP
This name space includes all other name spaces (except KM3NETDAQ, KM3NET and ANTARES).
Definition: JAAnetToolkit.hh:37
ERROR
#define ERROR(A)
Definition: JMessage.hh:66
debug
int debug
debug level
Definition: JSirene.cc:59
JRootObjectID.hh
JTOOLS::result
return result
Definition: JPolint.hh:695
JGIZMO::getDirectory
TDirectory * getDirectory(const JRootObjectID &id)
Get TDirectory pointer.
Definition: JGizmoToolkit.hh:121
JParser.hh
operator<<
std::ostream & operator<<(std::ostream &out, const JFormula &object)
Write formula to output stream.
Definition: JPrintResult.cc:48
operator>>
std::istream & operator>>(std::istream &in, JFormula &object)
Read formula from input stream.
Definition: JPrintResult.cc:36
make_field
#define make_field(A,...)
macro to convert parameter to JParserTemplateElement object
Definition: JParser.hh:1954
DEBUG
#define DEBUG(A)
Message macros.
Definition: JMessage.hh:62
std
Definition: jaanetDictionary.h:36
main
int main(int argc, char **argv)
Definition: JPrintResult.cc:64
FATAL
#define FATAL(A)
Definition: JMessage.hh:67