Jpp test-rotations-new
the software that should make you happy
Loading...
Searching...
No Matches
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
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 */
25struct 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 */
36inline 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 */
48inline std::ostream& operator<<(std::ostream& out, const JFormula& object)
49{
50 return out << object.Data();
51}
52
53
54/**
55 * Replace regular expression in input by given replacement.
56 *
57 * \param target input
58 * \param regexp regular expression
59 * \param replacement replacement
60 * \return result
61 */
62template<class T>
63inline TString replace(const TString& target, const TRegexp& regexp, const T& replacement)
64{
65 Ssiz_t len;
66 Ssiz_t pos;
67
68 TString buffer = target;
69
70 if ((pos = buffer.Index(regexp, &len)) != -1) {
71
72 TSubString substr = buffer(pos, len);
73
74 TString format(substr.Data(), substr.Length());
75
76 format.ReplaceAll("T", "s");
77
78 buffer.Replace(pos, len, TString::Format(format.Data(), replacement));
79 }
80
81 return buffer;
82}
83
84
85/**
86 * \file
87 *
88 * Auxiliary program to print result from ROOT objects.
89 * The option <tt>-f</tt> corresponds to <tt><file name>:<object name></tt>.
90 *
91 * The formula (option <tt>-F <formula></tt>) refers to a ROOT TFormula.
92 * The expression may contain member methods of the corresponding object.
93 * \author mdejong
94 */
95int main(int argc, char **argv)
96{
97 using namespace std;
98 using namespace JPP;
99
100 vector<JRootObjectID> inputFile;
101 vector<JFormula> formula;
102 TString option;
103 int debug;
104
105 try {
106
107 JParser<> zap("Auxiliary program to print result from ROOT objects."\
108 "\nNote that the formula may contain method names of the specified object.");
109
110 zap['f'] = make_field(inputFile, "<input file>:<object name>");
111 zap['F'] = make_field(formula, "ROOT TFormula (may contain method names of object)") = JPARSER::initialised();
112 zap['O'] = make_field(option, "format, e.g. \"%s %T %f\","\
113 "\nwhere '%s', '%T' and '%f' will be replaced by name, title and value(s) from formula(s), respectively") = "";
114 zap['d'] = make_field(debug) = 0;
115
116 zap(argc, argv);
117 }
118 catch(const exception &error) {
119 FATAL(error.what() << endl);
120 }
121
122
123 const TRegexp STRING("%[+-]?[0-9]*s");
124 const TRegexp TITLE ("%[+-]?[0-9]*T");
125 const TRegexp DOUBLE("%[+-]?[0-9]*\\.?[0-9]*f");
126
127
128 for (vector<JRootObjectID>::const_iterator input = inputFile.begin(); input != inputFile.end(); ++input) {
129
130 DEBUG("Input: " << *input << endl);
131
132 TDirectory* dir = getDirectory(*input);
133
134 if (dir == NULL) {
135 ERROR("File: " << input->getFullFilename() << " not opened." << endl);
136 continue;
137 }
138
139 const TRegexp regexp(input->getObjectName());
140
141 TIter iter(dir->GetListOfKeys());
142
143 for (TKey* key; (key = (TKey*) iter.Next()) != NULL; ) {
144
145 const TString tag(key->GetName());
146
147 DEBUG("Key: " << tag << " match = " << tag.Contains(regexp) << endl);
148
149 // option match
150
151 if (tag.Contains(regexp) && isTObject(key)) {
152
153 TObject* object = key->ReadObj();
154
155 try {
156
157 if (option != "") {
158
159 TString buffer = option;
160
161 buffer = replace(buffer, STRING, object->GetName());
162 buffer = replace(buffer, TITLE, object->GetTitle());
163
164 for (vector<JFormula>::const_iterator i = formula.begin(); i != formula.end(); ++i) {
165
166 const double value = getResult(*i, object);
167
168 if (buffer.Contains(DOUBLE)){
169
170 buffer = replace(buffer, DOUBLE, value);
171
172 } else {
173
174 if (i != formula.begin()) {
175 buffer.Append(" ");
176 }
177
178 buffer.Append(TString::Format("%20.10e", value));
179 }
180 }
181 cout << buffer << endl;
182
183 } else {
184
185 for (vector<JFormula>::const_iterator i = formula.begin(); i != formula.end(); ++i) {
186 cout << ' ' << SCIENTIFIC(20,10) << getResult(*i, object);
187 }
188 }
189 }
190 catch(exception&) {}
191 }
192 }
193 }
194}
General purpose messaging.
#define DEBUG(A)
Message macros.
Definition JMessage.hh:62
#define ERROR(A)
Definition JMessage.hh:66
#define FATAL(A)
Definition JMessage.hh:67
int debug
debug level
Definition JSirene.cc:72
Utility class to parse command line options.
#define make_field(A,...)
macro to convert parameter to JParserTemplateElement object
Definition JParser.hh:2142
int main(int argc, char **argv)
TString replace(const TString &target, const TRegexp &regexp, const T &replacement)
Replace regular expression in input by given replacement.
std::ostream & operator<<(std::ostream &out, const JFormula &object)
Write formula to output stream.
std::istream & operator>>(std::istream &in, JFormula &object)
Read formula from input stream.
I/O formatting auxiliaries.
Utility class to parse command line options.
Definition JParser.hh:1698
This name space includes all other name spaces (except KM3NETDAQ, KM3NET and ANTARES).
Auxiliary class for custom I/O.
Empty structure for specification of parser element that is initialised (i.e. does not require input)...
Definition JParser.hh:68
Auxiliary data structure for floating point format specification.
Definition JManip.hh:488