CMS 3D CMS Logo

HistoCompare.cc
Go to the documentation of this file.
1 
13 
14 #include <iostream>
15 
17  result_ = 0;
18  do_nothing_ = false;
19 }
20 
21 HistoCompare::HistoCompare(const TString &refFilename) {
22  result_ = 0;
23  do_nothing_ = false;
24  // open reference file
25  refFile_ = new TFile(refFilename);
26  if (refFile_->IsZombie()) {
27  std::cout << " Error openning file " << refFilename << std::endl;
28  std::cout << " we will not compare histograms. " << std::endl;
29  do_nothing_ = true;
30  } else {
31  std::cout << " open file " << refFilename << std::endl;
32  }
33 
34  setChi2Test_ = true;
35 }
36 
38 
39 TH1 *HistoCompare::Compare(TH1 *h, const TString &hname) {
40  if (do_nothing_)
41  return nullptr;
42 
43  // std::cout << "histo: " << h->GetName() << " entries: "<< h->GetEntries() <<
44  // std::endl; std::cout << "hname: " << hname << std::endl;
45 
46  // create a residual histogram
47  // if (resHisto_) delete resHisto_;
48  resHisto_ = (TH1 *)h->Clone(TString(h->GetName()) + "_residuals");
49  resHisto_->Reset();
50 
51  // clone input histogram
52  TH1 *htemp = (TH1 *)h->Clone(TString(h->GetName()));
53 
54  refFile_->cd();
55  // std::cout << "get reference" << std::endl;
56  refHisto_ = (TH1 *)gDirectory->Get(hname);
57  // std::cout << "historef: " << refHisto_->GetName() << " entries: "<<
58  // refHisto_->GetEntries() << std::endl; std::cout << "name: " <<
59  // refHisto_->GetName() << std::endl;
60 
61  // normalize histograms
62  htemp->Scale(1. / htemp->Integral());
63  refHisto_->Scale(1. / refHisto_->Integral());
64 
65  if (setChi2Test_) {
66  result_ = refHisto_->Chi2Test(htemp, "UFOF");
67  }
68  if (setKGTest_) {
69  result_ = refHisto_->KolmogorovTest(htemp, "UO");
70  }
71 
72  resHisto_->Add(htemp, refHisto_, 1., -1.);
73 
74  // std::cout << " residual done." << std::endl;
75 
76  return resHisto_;
77 }
HistoCompare::refHisto_
TH1 * refHisto_
Definition: HistoCompare.h:57
gather_cfg.cout
cout
Definition: gather_cfg.py:144
HistoCompare::~HistoCompare
~HistoCompare()
Definition: HistoCompare.cc:37
HistoCompare::refFile_
TFile * refFile_
Definition: HistoCompare.h:58
HistoCompare.h
HistoCompare::Compare
TH1 * Compare(TH1 *h, const TString &hname)
Definition: HistoCompare.cc:39
HistoCompare::setChi2Test_
bool setChi2Test_
Definition: HistoCompare.h:48
h
HistoCompare::resHisto_
TH1 * resHisto_
Definition: HistoCompare.h:56
HistoCompare::result_
double result_
Definition: HistoCompare.h:53
HistoCompare::HistoCompare
HistoCompare()
Definition: HistoCompare.cc:16
HistoCompare::setKGTest_
bool setKGTest_
Definition: HistoCompare.h:52
HistoCompare::do_nothing_
bool do_nothing_
Definition: HistoCompare.h:54