#include #include #include #include #include #include "TROOT.h" #include "TFile.h" #include "TObject.h" #include "TKey.h" #include "TH1D.h" #include "TString.h" #include "TRegexp.h" #include "JGizmo/JRootObjectID.hh" #include "JGizmo/JGizmoToolkit.hh" #include "Jeep/JPrint.hh" #include "Jeep/JParser.hh" #include "Jeep/JMessage.hh" /** * \file * * Auxiliary program to rebin ROOT histograms. * The option -f corresponds to \:\. * * \author mdejong */ int main(int argc, char **argv) { using namespace std; using namespace JPP; vector inputFile; string outputFile; vector X; int debug; try { JParser<> zap("Auxiliary program to rebin ROOT histograms."); zap['f'] = make_field(inputFile, ":"); zap['o'] = make_field(outputFile, "ROOT file with histogram(s)") = "rebin.root"; zap['x'] = make_field(X, "abscissa values"); zap['d'] = make_field(debug) = 0; zap(argc, argv); } catch(const exception &error) { FATAL(error.what() << endl); } if (X.empty()) { FATAL("No abscissa values." << 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(); try { TH1D& h0 = dynamic_cast(*object); TH1D h1(h0.GetName(), NULL, X.size() - 1, X.data()); for (int ix = 1; ix <= h1.GetXaxis()->GetNbins(); ++ix) { const Double_t xmin = h0.GetXaxis()->FindBin(h1.GetXaxis()->GetBinLowEdge(ix)); const Double_t xmax = h0.GetXaxis()->FindBin(h1.GetXaxis()->GetBinUpEdge (ix)); const Int_t imin = xmin; const Int_t imax = xmax; if (imax != imin) { Double_t Y = 0.0; Double_t Z = 0.0; Double_t W = 0.0; for (Int_t i = imin; i != imax; ++i) { const Double_t y = h0.GetBinContent(i); const Double_t z = h0.GetBinWidth (i); const Double_t w = h0.GetBinError (i); Y += y*z; Z += z; W += w*z * w*z; } Y = Y / Z; W = sqrt(W) / Z; h1.SetBinContent(ix, Y); h1.SetBinError (ix, W); } else { ERROR("Invalid bin " << ix << " [" << FIXED(9,3) << xmin << "," << FIXED(9,3) << xmax << "]" << " -> " << imin << ' ' << imax << endl); } } out.WriteTObject(&h1); } catch(exception&) {} } } } out.Write(); out.Close(); }