#include #include #include #include #include "TROOT.h" #include "TFile.h" #include "TObject.h" #include "TKey.h" #include "TH1.h" #include "TString.h" #include "TRegexp.h" #include "JTools/JRange.hh" #include "JGizmo/JRootObjectID.hh" #include "JGizmo/JGizmoToolkit.hh" #include "Jeep/JPrint.hh" #include "Jeep/JParser.hh" #include "Jeep/JMessage.hh" /** * \file * * Auxiliary program to make cumulative ROOT histogram. * The option -f corresponds to \:\. * \author mdejong */ int main(int argc, char **argv) { using namespace std; using namespace JPP; typedef JRange JRange_t; vector inputFile; string outputFile; JRange_t X; bool binwidth; bool reverse; bool normalise; int debug; try { JParser<> zap("Auxiliary program to make cumulative ROOT histogram."); zap['f'] = make_field(inputFile, ":"); zap['o'] = make_field(outputFile, "ROOT file with histogram(s)") = "sum.root"; zap['x'] = make_field(X, "accepted x-range values") = JRange_t(); zap['B'] = make_field(binwidth, "multiply by bin width"); zap['R'] = make_field(reverse, "sum right to left instead of left to right"); zap['N'] = make_field(normalise, "normalise histogram contents"); 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(); try { TH1& h1 = dynamic_cast(*object); double W = 0.0; if (!reverse) { for (Int_t i = 0; i <= h1.GetXaxis()->GetNbins() + 1; ++i) { if (X(h1.GetBinCenter(i))) { W += h1.GetBinContent(i) * (binwidth ? h1.GetBinWidth(i) : 1.0); } h1.SetBinContent(i, W); if (h1.GetSumw2N()) { h1.SetBinError(i, h1.GetBinError(i) * (binwidth ? h1.GetBinWidth(i) : 1.0)); } } } else { for (Int_t i = h1.GetXaxis()->GetNbins() + 1; i >= 0; --i) { if (X(h1.GetBinCenter(i))) { W += h1.GetBinContent(i) * (binwidth ? h1.GetBinWidth(i) : 1.0); } h1.SetBinContent(i, W); if (h1.GetSumw2N()) { h1.SetBinError(i, h1.GetBinError(i) * (binwidth ? h1.GetBinWidth(i) : 1.0)); } } } NOTICE(setw(24) << left << h1.GetName() << ' ' << SCIENTIFIC(12,3) << W << endl); if (normalise && W != 0.0) { h1.Scale(1.0/W); } out.WriteTObject(&h1); } catch(exception&) {} } } } out.Write(); out.Close(); }