#include #include #include #include #include #include "TROOT.h" #include "TFile.h" #include "TObject.h" #include "TKey.h" #include "TH2.h" #include "TH1D.h" #include "TString.h" #include "TRegexp.h" #include "JGizmo/JRootObjectID.hh" #include "JGizmo/JGizmoToolkit.hh" #include "Jeep/JParser.hh" #include "Jeep/JMessage.hh" /** * \file * * Auxiliary program to extract quantiles from 2D histogram. * The option -f corresponds to \:\. * \author mdejong */ int main(int argc, char **argv) { using namespace std; using namespace JPP; vector inputFile; string outputFile; vector Q; bool reverse; int debug; try { JParser<> zap("Auxiliary program to extract quantiles from 2D histogram."); zap['f'] = make_field(inputFile); zap['o'] = make_field(outputFile) = "quantiles.root"; zap['Q'] = make_field(Q); zap['R'] = make_field(reverse); zap['d'] = make_field(debug) = 0; zap(argc, argv); } catch(const exception &error) { FATAL(error.what() << endl); } if (Q.empty()) { FATAL("No quantiles." << 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 { TH2& h2 = dynamic_cast(*object); vector h1(Q.size(), NULL); for (size_t i = 0; i != Q.size(); ++i) { ostringstream os; os << h2.GetName(); os << i << "[" << setw(3) << (int) (100.0 * Q[i]) << "%]"; DEBUG("Creating 1D histogram: " << os.str() << endl); TAxis* axis = h2.GetXaxis(); if (axis->IsVariableBinSize()) h1[i] = new TH1D(os.str().c_str(), NULL, axis->GetNbins(), axis->GetXbins()->GetArray()); else h1[i] = new TH1D(os.str().c_str(), NULL, axis->GetNbins(), axis->GetXmin(), axis->GetXmax()); } for (Int_t i = 1; i <= h2.GetXaxis()->GetNbins(); ++i) { Double_t W = 0.0; for (Int_t j = 0; j <= h2.GetYaxis()->GetNbins() + 1; ++j) { W += h2.GetBinContent(i,j); } if (W != 0.0) { Double_t w = 0.0; if (!reverse) { for (int j = 0, k = 0; j <= h2.GetYaxis()->GetNbins() + 1; ++j) { w += h2.GetBinContent(i,j); for ( ; k != (int) h1.size() && w >= Q[k]*W; ++k) { h1[k]->SetBinContent(i, h2.GetYaxis()->GetBinCenter(j)); } } } else { for (int j = h2.GetYaxis()->GetNbins() + 1, k = 0; j >= 0; --j) { w += h2.GetBinContent(i,j); for ( ; k != (int) h1.size() && w >= Q[k]*W; ++k) { h1[k]->SetBinContent(i, h2.GetYaxis()->GetBinCenter(j)); } } } } } out.cd(); for (vector::iterator i = h1.begin(); i != h1.end(); ++i) { (*i)->Write(); } } catch(exception&) { ERROR("Not available for other objects than 2D histograms." << endl); } } } } out.Write(); out.Close(); }