Jpp
Classes | Functions
JPrintResult.cc File Reference
#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...
 
int main (int argc, char **argv)
 

Detailed Description

Auxiliary program to print result from ROOT histograms. 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 }

◆ main()

int main ( int  argc,
char **  argv 
)

Definition at line 64 of file JPrintResult.cc.

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
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
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
JTOOLS::result
return result
Definition: JPolint.hh:695
JGIZMO::getDirectory
TDirectory * getDirectory(const JRootObjectID &id)
Get TDirectory pointer.
Definition: JGizmoToolkit.hh:121
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
FATAL
#define FATAL(A)
Definition: JMessage.hh:67