#include #include #include #include #include #include "TROOT.h" #include "TFile.h" #include "TObject.h" #include "TKey.h" #include "TH1.h" #include "TGraph.h" #include "TGraphErrors.h" #include "TString.h" #include "TRegexp.h" #include "JTools/JRange.hh" #include "JGizmo/JRootObjectID.hh" #include "JGizmo/JGizmoToolkit.hh" #include "Jeep/JParser.hh" #include "Jeep/JMessage.hh" /** * \file * * Auxiliary program to scale ROOT histograms. * The option -f corresponds to \:\. * * The scale factor (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; typedef JRange JRange_t; vector inputFile; string outputFile; TString formula; JRange_t X; JRange_t Y; string option; int debug; try { JParser<> zap("Auxiliary program to scale ROOT histograms."\ "\nNote that the formula may contain method names of the specified object."); zap['f'] = make_field(inputFile, ":"); zap['o'] = make_field(outputFile, "ROOT file with histogram(s)") = "scale.root"; zap['F'] = make_field(formula, "ROOT TFormula (may contain method names of object)"); zap['x'] = make_field(X, "abscissa range (only for Integral)") = JRange_t(); zap['y'] = make_field(Y, "abscissa range (only for Integral)") = JRange_t(); zap['O'] = make_field(option) = "", "nosw2", "width", "nosw2 width"; zap['d'] = make_field(debug) = 0; zap(argc, argv); } catch(const exception &error) { FATAL(error.what() << endl); } TFile out(outputFile.c_str(), "recreate"); 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(); TH1* h1 = dynamic_cast (object); TGraphErrors* ge = dynamic_cast(object); TGraph* g1 = dynamic_cast (object); if (h1 != NULL || ge != NULL || g1 != NULL) { const double factor = getResult(formula, object); if (h1 != NULL) { if (X != JRange_t()) { h1->GetXaxis()->SetRangeUser(X.getLowerLimit(), X.getUpperLimit()); } if (Y != JRange_t()) { h1->GetYaxis()->SetRangeUser(Y.getLowerLimit(), Y.getUpperLimit()); } h1->Scale(factor, option.c_str()); } else if (ge != NULL) { for (Int_t i = 0; i != ge->GetN(); ++i) { ge->GetY() [i] *= factor; ge->GetEY()[i] *= factor; } } else if (g1 != NULL) { for (Int_t i = 0; i != g1->GetN(); ++i) { g1->GetY() [i] *= factor; } } out.WriteTObject(object); } } } } out.Write(); out.Close(); }