#include #include #include #include #include #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" /** * Auxiliary class for custom I/O. */ struct JFormula : public TString {}; /** * Read formula from input stream. * * \param in input stream * \param object formula * \return input stream */ inline std::istream& operator>>(std::istream& in, JFormula& object) { return object.ReadLine(in); } /** * Write formula to output stream. * * \param out output stream * \param object formula * \return output stream */ inline std::ostream& operator<<(std::ostream& out, const JFormula& object) { return out << object.Data(); } /** * Replace regular expression in input by given replacement. * * \param target input * \param regexp regular expression * \param replacement replacement * \return result */ template inline TString replace(const TString& target, const TRegexp& regexp, const T& replacement) { Ssiz_t len; Ssiz_t pos; TString buffer = target; if ((pos = buffer.Index(regexp, &len)) != -1) { TSubString substr = buffer(pos, len); TString format(substr.Data(), substr.Length()); format.ReplaceAll("T", "s"); buffer.Replace(pos, len, TString::Format(format.Data(), replacement)); } return buffer; } /** * \file * * Auxiliary program to print result from ROOT objects. * The option -f corresponds to \:\. * * The formula (option -F \) refers to a ROOT TFormula. * The expression may contain member methods of the corresponding object. * \author mdejong */ int main(int argc, char **argv) { using namespace std; using namespace JPP; vector inputFile; vector formula; TString option; int debug; try { JParser<> zap("Auxiliary program to print result from ROOT objects."\ "\nNote that the formula may contain method names of the specified object."); zap['f'] = make_field(inputFile, ":"); zap['F'] = make_field(formula, "ROOT TFormula (may contain method names of object)") = JPARSER::initialised(); zap['O'] = make_field(option, "format, e.g. \"%s %T %f\","\ "\nwhere '%s', '%T' and '%f' will be replaced by name, title and value(s) from formula(s), respectively") = ""; zap['d'] = make_field(debug) = 0; zap(argc, argv); } catch(const exception &error) { FATAL(error.what() << endl); } const TRegexp STRING("%[+-]?[0-9]*s"); const TRegexp TITLE ("%[+-]?[0-9]*T"); const TRegexp DOUBLE("%[+-]?[0-9]*\\.?[0-9]*f"); for (vector::const_iterator input = inputFile.begin(); input != inputFile.end(); ++input) { DEBUG("Input: " << *input << endl); TDirectory* dir = getDirectory(*input); if (dir == NULL) { ERROR("File: " << input->getFullFilename() << " not opened." << endl); continue; } const TRegexp regexp(input->getObjectName()); TIter iter(dir->GetListOfKeys()); for (TKey* key; (key = (TKey*) iter.Next()) != NULL; ) { const TString tag(key->GetName()); DEBUG("Key: " << tag << " match = " << tag.Contains(regexp) << endl); // option match if (tag.Contains(regexp) && isTObject(key)) { TObject* object = key->ReadObj(); try { if (option != "") { TString buffer = option; buffer = replace(buffer, STRING, object->GetName()); buffer = replace(buffer, TITLE, object->GetTitle()); for (vector::const_iterator i = formula.begin(); i != formula.end(); ++i) { const double value = getResult(*i, object); if (buffer.Contains(DOUBLE)){ buffer = replace(buffer, DOUBLE, value); } else { if (i != formula.begin()) { buffer.Append(" "); } buffer.Append(TString::Format("%20.10e", value)); } } cout << buffer << endl; } else { for (vector::const_iterator i = formula.begin(); i != formula.end(); ++i) { cout << ' ' << SCIENTIFIC(20,10) << getResult(*i, object); } } } catch(exception&) {} } } } }