#include #include #include #include #include #include "TROOT.h" #include "TFile.h" #include "TKey.h" #include "TH1.h" #include "TProfile.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 determine standard deviation of a set of 1D histograms. * The option -f corresponds to \:\. * \author mdejong */ int main(int argc, char **argv) { using namespace std; using namespace JPP; vector inputFile; string outputFile; int debug; try { JParser<> zap("Auxiliary program to determine standard deviation of a set of 1D histograms."); zap['f'] = make_field(inputFile, ":"); zap['o'] = make_field(outputFile, "ROOT file with histogram") = "stdev.root"; zap['d'] = make_field(debug) = 1; zap(argc, argv); } catch(const exception &error) { FATAL(error.what() << endl); } TH1* h3 = NULL; vector listOfHistograms; 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* p = key->ReadObj(); if (!p->InheritsFrom("TH1")) { FATAL("Object " << p->GetName() << " not compatible with histogram operations." << endl); } if (h3 == NULL) { h3 = (TH1*) p->Clone("stdev"); } else { TH1* h1 = (TH1*) p; if (h3->GetXaxis()->GetNbins() != h1->GetXaxis()->GetNbins() || h3->GetXaxis()->GetXmin () != h1->GetXaxis()->GetXmin () || h3->GetXaxis()->GetXmax () != h1->GetXaxis()->GetXmax ()) { FATAL("Objects " << h3->GetName() << " and " << p->GetName() << " have different binning." << endl); } } listOfHistograms.push_back((TH1*) p); } } } if (listOfHistograms.size() <= 1) { FATAL("Not enough histograms " << listOfHistograms.size() << endl); } for (Int_t i = 1; i <= h3->GetNbinsX(); ++i) { Double_t Y = 0.0; Double_t Z = 0.0; for (vector::const_iterator p = listOfHistograms.begin(); p != listOfHistograms.end(); ++p) { Y += (*p)->GetBinContent(i); Z += (*p)->GetBinError(i) * (*p)->GetBinError(i); } Z = sqrt(Z); Y /= listOfHistograms.size(); Z /= listOfHistograms.size(); Double_t y = 0.0; for (vector::const_iterator p = listOfHistograms.begin(); p != listOfHistograms.end(); ++p) { y += ((*p)->GetBinContent(i) - Y)*((*p)->GetBinContent(i) - Y) / ((*p)->GetBinError(i)*(*p)->GetBinError(i) + Z*Z); } y = sqrt(y / listOfHistograms.size()); h3->SetBinContent(i, y); h3->SetBinError (i, 0.0); } if (h3 != NULL) { TFile out(outputFile.c_str(), "recreate"); h3->Write(); out.Write(); out.Close(); } }