Jpp  debug
the software that should make you happy
Public Member Functions | Protected Attributes | Private Attributes | List of all members
JCOMPAREHISTOGRAMS::JTestChi2_Bin Class Reference

Implementation of a bin-by-bin Chi2 test for 2D histograms. More...

#include <JTestChi2_Bin.hh>

Inheritance diagram for JCOMPAREHISTOGRAMS::JTestChi2_Bin:
JCOMPAREHISTOGRAMS::JTest_t std::vector< JTestResult >

Public Member Functions

 JTestChi2_Bin ()
 Default constructor. More...
 
void test (const TObject *o1, const TObject *o2) override
 Tests the statistical compatibility of two ROOT 2D histograms. More...
 
std::istream & read (std::istream &in) override
 Read test parameters from input. More...
 
virtual std::ostream & write (std::ostream &out, const char delimiter=' ', const bool onlyFailures=false) const
 Write test result to output. More...
 
virtual void save (TFile *f, const std::string &path, const bool onlyFailures=false) const
 Writes the test result to root file. More...
 
const std::string & getTestName () const
 Get test name. More...
 
const std::string & getResultType () const
 Get result type. More...
 

Protected Attributes

const std::string testName
 test name More...
 
const std::string resultType
 test result type More...
 

Private Attributes

double chi2Threshold
 threshold chi2-value to decide if test is passed for a bin. More...
 
double outliersThreshold
 fraction of bins allowed to fail the test. More...
 

Detailed Description

Implementation of a bin-by-bin Chi2 test for 2D histograms.


This class is derived from the abstract class JTest_t(). For a general description of the implementation of this and other tests derived from JTest_t(), see its documentation.

Definition at line 27 of file JTestChi2_Bin.hh.

Constructor & Destructor Documentation

◆ JTestChi2_Bin()

JCOMPAREHISTOGRAMS::JTestChi2_Bin::JTestChi2_Bin ( )
inline

Default constructor.

Definition at line 35 of file JTestChi2_Bin.hh.

35  :
36  JTest_t("Chi2_Bin", "Outliers[%]")
37  {}
JTest_t(const std::string &testName, const std::string &resultType)
Constructor.
Definition: JTest_t.hh:51

Member Function Documentation

◆ test()

void JCOMPAREHISTOGRAMS::JTestChi2_Bin::test ( const TObject o1,
const TObject o2 
)
inlineoverridevirtual

Tests the statistical compatibility of two ROOT 2D histograms.

Parameters
o1First histogram
o2Second histogram

Implements JCOMPAREHISTOGRAMS::JTest_t.

Definition at line 46 of file JTestChi2_Bin.hh.

47  {
48  using namespace std;
49  using namespace JPP;
50 
51  const TH1* h1 = dynamic_cast<const TH1*>(o1);
52  const TH1* h2 = dynamic_cast<const TH1*>(o2);
53 
54  if (h1 == NULL || h2 == NULL) {
55  THROW(JValueOutOfRange, "JTestChi2_Bin::test(): Could not cast given TObjects to TH1.");
56  }
57 
58  const int nx1 = h1->GetNbinsX();
59  const int nx2 = h2->GetNbinsX();
60  const int ny1 = h1->GetNbinsY();
61  const int ny2 = h2->GetNbinsY();
62  const int nz1 = h1->GetNbinsZ();
63  const int nz2 = h2->GetNbinsZ();
64 
65  const double M = h1->GetSumOfWeights();
66  const double N = h2->GetSumOfWeights();
67 
68  if(nx1 != nx2 || ny1 != ny2 || nz1 != nz2 || M == 0 || N == 0) {
69  THROW(JValueOutOfRange, "JTestChi2_t::JChi2TestBin_2D(): Histograms with different binning or with zero entries.");
70  }
71 
72  TH1* h3 = (TH1*) h1->Clone(h1->GetName() == h2->GetName() ?
73  MAKE_CSTRING(h1->GetName() << "_" << testName) :
74  MAKE_CSTRING(h1->GetName() << "_VS_" << h2->GetName() << "_" << testName));
75 
76  h3->Reset();
77 
78  double outliers = 0;
79 
80  for (int i=1 ; i<nx1 ; ++i) {
81  for (int j=1 ; j<ny1 ; ++j) {
82  for (int k=1 ; k<nz1 ; ++k) {
83 
84  const double m = h1->GetBinContent(i,j,k);
85  const double n = h2->GetBinContent(i,j,k);
86 
87  const double em = h1->GetBinError(i,j,k);
88  const double en = h2->GetBinError(i,j,k);
89 
90  if (n > 0.0 && m > 0.0 && em > 0.0 && en > 0.0) {
91 
92  const double delta = N*m - M*n;
93  const double sigma = M*M*en*en + N*N*em*em;
94  const double chi2 = delta*delta / sigma;
95 
96  if (fabs(chi2) > chi2Threshold) {
97  outliers += 1./(nx1*ny1);
98  }
99 
100  h3->SetBinContent(i,j,k,chi2);
101  }
102  }
103  }
104  }
105 
106  const bool passed = (outliers < outliersThreshold);
107 
108  const JResultTitle title(testName, resultType, passed, 100.*outliers);
109 
110  h3->SetTitle(title.getTitle().c_str());
111 
112  const int Ndims = h3->GetDimension();
113 
114  if (Ndims == 1) {
115  h3->GetYaxis()->SetTitle(resultType.c_str());
116  } else if (Ndims == 2) {
117  h3->GetZaxis()->SetTitle(resultType.c_str());
118  }
119 
120  const JTestResult r(testName,
121  JRootObjectID(MAKE_STRING(h1->GetDirectory()->GetPath() << h1->GetName())),
122  JRootObjectID(MAKE_STRING(h2->GetDirectory()->GetPath() << h1->GetName())),
123  resultType, 100.*outliers, 100.*outliersThreshold, h3, passed);
124 
125  this->push_back(r);
126  }
#define THROW(JException_t, A)
Marco for throwing exception with std::ostream compatible message.
Definition: JException.hh:712
#define MAKE_CSTRING(A)
Make C-string.
Definition: JPrint.hh:136
#define MAKE_STRING(A)
Make string.
Definition: JPrint.hh:127
Class dedicated to standardize the title of the graphical objects produced by the JTest_t() derived c...
Definition: JResultTitle.hh:26
double outliersThreshold
fraction of bins allowed to fail the test.
double chi2Threshold
threshold chi2-value to decide if test is passed for a bin.
const std::string resultType
test result type
Definition: JTest_t.hh:181
const std::string testName
test name
Definition: JTest_t.hh:180
Auxiliary class to handle file name, ROOT directory and object name.
Exception for accessing a value in a collection that is outside of its range.
Definition: JException.hh:180
const double sigma[]
Definition: JQuadrature.cc:74
This name space includes all other name spaces (except KM3NETDAQ, KM3NET and ANTARES).
const int n
Definition: JPolint.hh:786
data_type r[M+1]
Definition: JPolint.hh:868
int j
Definition: JPolint.hh:792
Definition: JSTDTypes.hh:14
Structure containing the result of the test.
Definition: JTestResult.hh:30

◆ read()

std::istream& JCOMPAREHISTOGRAMS::JTestChi2_Bin::read ( std::istream &  in)
inlineoverridevirtual

Read test parameters from input.

Parameters
ininput stream
Returns
input stream

Implements JCOMPAREHISTOGRAMS::JTest_t.

Definition at line 135 of file JTestChi2_Bin.hh.

136  {
137  using namespace JPP;
138 
140 
141  if (chi2Threshold < 0.0) {
142  THROW(JValueOutOfRange, "JTestChi2_Bin::read(): Invalid chi2-threshold value " << chi2Threshold);
143  }
144 
145  if (outliersThreshold < 0.0 || outliersThreshold > 1.0) {
146  THROW(JValueOutOfRange, "JTestChi2_Bin::read(): Invalid outliers threshold value " << outliersThreshold);
147  }
148 
149  return in;
150  }

◆ write()

virtual std::ostream& JCOMPAREHISTOGRAMS::JTest_t::write ( std::ostream &  out,
const char  delimiter = ' ',
const bool  onlyFailures = false 
) const
inlinevirtualinherited

Write test result to output.

Parameters
outoutput stream
delimiterfield delimiter
onlyFailuresif true, write only failures.
Returns
output stream

Definition at line 84 of file JTest_t.hh.

87  {
88  using namespace std;
89  using namespace JPP;
90 
91  for (vector<JTestResult>::const_iterator r = this->begin() ; r != this->end() ; ++r) {
92 
93  if (onlyFailures && r->passed) { continue; }
94 
95  print(out, *r, delimiter, true);
96  }
97 
98  return out;
99  }
std::ostream & print(std::ostream &out, const JTestSummary &summary, const char delimiter=' ', const bool useColors=true)
Print test summary.

◆ save()

virtual void JCOMPAREHISTOGRAMS::JTest_t::save ( TFile *  f,
const std::string &  path,
const bool  onlyFailures = false 
) const
inlinevirtualinherited

Writes the test result to root file.

Parameters
fA ROOT file
pathPath in root file.
onlyFailuresIf true, write only failures.

Definition at line 108 of file JTest_t.hh.

111  {
112  using namespace std;
113  using namespace JPP;
114 
115  if (f->GetDirectory(path.c_str())==0) {
116  f->mkdir(path.c_str());
117  }
118 
119  f->cd(path.c_str());
120 
121  for (vector<JTestResult>::const_iterator r = this->begin() ; r != this->end() ; ++r) {
122 
123  if (onlyFailures && r->passed) { continue; }
124 
125  r->obj->Write();
126  }
127  }

◆ getTestName()

const std::string& JCOMPAREHISTOGRAMS::JTest_t::getTestName ( ) const
inlineinherited

Get test name.

Returns
test name

Definition at line 135 of file JTest_t.hh.

136  {
137  return testName;
138  }

◆ getResultType()

const std::string& JCOMPAREHISTOGRAMS::JTest_t::getResultType ( ) const
inlineinherited

Get result type.

Returns
result type

Definition at line 146 of file JTest_t.hh.

147  {
148  return resultType;
149  }

Member Data Documentation

◆ chi2Threshold

double JCOMPAREHISTOGRAMS::JTestChi2_Bin::chi2Threshold
private

threshold chi2-value to decide if test is passed for a bin.

Definition at line 155 of file JTestChi2_Bin.hh.

◆ outliersThreshold

double JCOMPAREHISTOGRAMS::JTestChi2_Bin::outliersThreshold
private

fraction of bins allowed to fail the test.

Definition at line 156 of file JTestChi2_Bin.hh.

◆ testName

const std::string JCOMPAREHISTOGRAMS::JTest_t::testName
protectedinherited

test name

Definition at line 180 of file JTest_t.hh.

◆ resultType

const std::string JCOMPAREHISTOGRAMS::JTest_t::resultType
protectedinherited

test result type

Definition at line 181 of file JTest_t.hh.


The documentation for this class was generated from the following file: