#include #include #include #include #include #include "TROOT.h" #include "TFile.h" #include "TKey.h" #include "TH2.h" #include "TProfile2D.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 profile 2D histograms. * \author mdejong, adomi */ int main(int argc, char **argv) { using namespace std; using namespace JPP; typedef JRange JRange_t; vector inputFile; string outputFile; vector X; vector Y; char profile; bool overflow; string format; string option; int debug; try { JParser<> zap("Auxiliary program to profile 2D histograms."); zap['f'] = make_field(inputFile, ":"); zap['P'] = make_field(profile, "profiling") = ' ', 'x', 'X', 'y', 'Y'; zap['o'] = make_field(outputFile, "ROOT file with histogram(s)") = "profile.root"; zap['x'] = make_field(X, "x-abscissa ranges") = JPARSER::initialised(); zap['y'] = make_field(Y, "y-abscissa ranges") = JPARSER::initialised(); zap['+'] = make_field(overflow); zap['F'] = make_field(format, "format, e.g. \"%s %i\" or \"%s %f %f\"") = ""; zap['O'] = make_field(option, "option, see TH2::Profile(X|Y)") = "", "s", "i", "g"; zap['d'] = make_field(debug) = 1; zap(argc, argv); } catch(const exception &error) { FATAL(error.what() << endl); } const bool px = (profile == 'x' || profile == 'X' || !Y.empty()); // profiling on x-axis const bool py = (profile == 'y' || profile == 'Y' || !X.empty()); // profiling on y-axis if (px == py) { FATAL("Invalid operation: " << (px ? "" : "no") << " X profiling " << " and " << (py ? "" : "no") << " Y profiling " << endl); } if (format == "") { format = "%s_"; if (px) { format += "px"; } if (py) { format += "py"; } if (X.empty() && Y.empty()) format += "[%i]"; else format += "[%f,%f]"; } vector listOfObjects; 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)) { TH2* h2 = dynamic_cast(key->ReadObj()); if (h2 != NULL) { if (px) { if (Y.empty()) { listOfObjects.push_back(h2->ProfileX(TString::Format(format.c_str(), h2->GetName()), (overflow ? 0 : 1), h2->GetYaxis()->GetNbins() + (overflow ? 1 : 0), option.c_str())); } else { for (Int_t i = 0; i != (Int_t) Y.size(); ++i) { listOfObjects.push_back(h2->ProfileX(TString::Format(format.c_str(), h2->GetName(), Y[i].getLowerLimit(), Y[i].getUpperLimit()), h2->GetYaxis()->FindBin(Y[i].getLowerLimit()), h2->GetYaxis()->FindBin(Y[i].getUpperLimit()) - 1, option.c_str())); } } } else if (py) { if (X.empty()) { listOfObjects.push_back(h2->ProfileY(TString::Format(format.c_str(), h2->GetName()), (overflow ? 0 : 1), h2->GetXaxis()->GetNbins() + (overflow ? 1 : 0), option.c_str())); } } else { for (Int_t i = 0; i != (Int_t) Y.size(); ++i) { listOfObjects.push_back(h2->ProfileY(TString::Format(format.c_str(), h2->GetName(), X[i].getLowerLimit(), X[i].getUpperLimit()), h2->GetXaxis()->FindBin(X[i].getLowerLimit()), h2->GetXaxis()->FindBin(X[i].getUpperLimit()) - 1, option.c_str())); } } } } } } if (!listOfObjects.empty()) { TFile out(outputFile.c_str(), "recreate"); for (vector::const_iterator i = listOfObjects.begin(); i != listOfObjects.end(); ++i) { (*i)->Write(); } out.Write(); out.Close(); } }