Jpp  18.2.1-ARCA-DF-PATCH
the software that should make you happy
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
Classes | Functions
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. More...
 
std::ostream & operator<< (std::ostream &out, const JFormula &object)
 Write formula to output stream. More...
 
template<class T >
TString replace (const TString &target, const TRegexp &regexp, const T &replacement)
 Replace regular expression in input by given replacement. More...
 
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

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 }
then fatal Wrong number of arguments fi set_variable DETECTOR $argv[1] set_variable INPUT_FILE $argv[2] eval JPrintDetector a $DETECTOR O IDENTIFIER eval JPrintDetector a $DETECTOR O SUMMARY JAcoustics sh $DETECTOR_ID source JAcousticsToolkit sh CHECK_EXIT_CODE typeset A EMITTERS get_tripods $WORKDIR tripod txt EMITTERS get_transmitters $WORKDIR transmitter txt EMITTERS for EMITTER in
Definition: JCanberra.sh:46
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 }
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 }
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 }
Utility class to parse command line options.
Definition: JParser.hh:1514
TString replace(const TString &target, const TRegexp &regexp, const T &replacement)
Replace regular expression in input by given replacement.
Definition: JPrintResult.cc:63
clean eval JPrintDetector a $DETECTOR O IDENTIFIER eval JPrintDetector a $DETECTOR O SUMMARY set_variable STRING
Definition: JRoot.hh:19
Empty structure for specification of parser element that is initialised (i.e. does not require input)...
Definition: JParser.hh:83
#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
#define FATAL(A)
Definition: JMessage.hh:67
bool isTObject(const TKey *key)
Check if given key corresponds to a TObject.
TDirectory * getDirectory(const JRootObjectID &id)
Get TDirectory pointer.
Auxiliary data structure for floating point format specification.
Definition: JManip.hh:484
int debug
debug level
#define DEBUG(A)
Message macros.
Definition: JMessage.hh:62