Jpp 19.3.0-rc.3
the software that should make you happy
Loading...
Searching...
No Matches
JPrintResult.cc File Reference

Auxiliary program to print result from ROOT objects. More...

#include <string>
#include <iostream>
#include <iomanip>
#include <sstream>
#include <cmath>
#include "TROOT.h"
#include "TFile.h"
#include "TObject.h"
#include "TKey.h"
#include "TString.h"
#include "TRegexp.h"
#include "JGizmo/JRootObjectID.hh"
#include "JGizmo/JGizmoToolkit.hh"
#include "Jeep/JParser.hh"
#include "Jeep/JMessage.hh"
#include "Jeep/JPrint.hh"

Go to the source code of this file.

Classes

struct  JFormula
 Auxiliary class for custom I/O. More...
 

Functions

std::istream & operator>> (std::istream &in, JFormula &object)
 Read formula from input stream.
 
std::ostream & operator<< (std::ostream &out, const JFormula &object)
 Write formula to output stream.
 
template<class T >
TString replace (const TString &target, const TRegexp &regexp, const T &replacement)
 Replace regular expression in input by given replacement.
 
int main (int argc, char **argv)
 

Detailed Description

Auxiliary program to print result from ROOT objects.

The option -f corresponds to <file name>:<object name>.

The formula (option -F <formula>) refers to a ROOT TFormula. The expression may contain member methods of the corresponding object.

Author
mdejong

Definition in file JPrintResult.cc.

Function Documentation

◆ operator>>()

std::istream & operator>> ( std::istream & in,
JFormula & object )
inline

Read formula from input stream.

Parameters
ininput stream
objectformula
Returns
input stream

Definition at line 36 of file JPrintResult.cc.

37{
38 return object.ReadLine(in);
39}

◆ operator<<()

std::ostream & operator<< ( std::ostream & out,
const JFormula & object )
inline

Write formula to output stream.

Parameters
outoutput stream
objectformula
Returns
output stream

Definition at line 48 of file JPrintResult.cc.

49{
50 return out << object.Data();
51}

◆ replace()

template<class T >
TString replace ( const TString & target,
const TRegexp & regexp,
const T & replacement )
inline

Replace regular expression in input by given replacement.

Parameters
targetinput
regexpregular expression
replacementreplacement
Returns
result

Definition at line 63 of file JPrintResult.cc.

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}

◆ main()

int main ( int argc,
char ** argv )

Definition at line 95 of file JPrintResult.cc.

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}
#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
#define make_field(A,...)
macro to convert parameter to JParserTemplateElement object
Definition JParser.hh:2142
TString replace(const TString &target, const TRegexp &regexp, const T &replacement)
Replace regular expression in input by given replacement.
Utility class to parse command line options.
Definition JParser.hh:1698
TDirectory * getDirectory(const JRootObjectID &id)
Get TDirectory pointer.
bool isTObject(const TKey *key)
Check if given key corresponds to a TObject.
Double_t getResult(const TString &text, TObject *object=NULL)
Get result of given textual formula.
This name space includes all other name spaces (except KM3NETDAQ, KM3NET and ANTARES).
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