Jpp test-rotations-new
the software that should make you happy
Loading...
Searching...
No Matches
JTestChi2.hh
Go to the documentation of this file.
1#ifndef __JCOMPAREHISTOGRAMS__JTESTCHI2__
2#define __JCOMPAREHISTOGRAMS__JTESTCHI2__
3
4#include <istream>
5#include <ostream>
6
9
10#include "TH1.h"
11
12
13/**
14 * \author rgruiz, bjung
15 */
16namespace JCOMPAREHISTOGRAMS {}
17namespace JPP { using namespace JCOMPAREHISTOGRAMS; }
18
19namespace JCOMPAREHISTOGRAMS {
20 /**
21 * Implementation of the Chi2 test for ROOT histograms.\n
22 * 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.\n
23 * The input parameter threshold(), is used to evaluate whether the test is passed or failed.\n
24 * The evaluation is done by comparing the threshold() value with the result produced by JChi2Test(). The output of a Chi2 test is a p-value.\n
25 * The parameter threshold() should therefore be a real value between 0 and 1.
26 */
27 class JTestChi2:
28 public JTest_t
29 {
30 public:
31
32 /**
33 * Default constructor.
34 */
36 JTest_t("Chi2", "p-Value(chi2)")
37 {}
38
39
40 /**
41 * Applies Chi2 test for two ROOT TH1 histograms.
42 *
43 * \param o1 First histogram
44 * \param o2 Second histogram
45 */
46 void test(const TObject* o1, const TObject* o2) override
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::test(): Could not cast given TObjects to TH1.");
56 }
57
58 vector<Double_t> residuals(h1->GetNcells());
59
60 const double chi2 = h1->Chi2Test(h2 , options.c_str(), &residuals[0]);
61
62 TH1* h3 = (TH1*) h1->Clone(h1->GetName() == h2->GetName() ?
63 MAKE_CSTRING(h1->GetName() << "_" << testName) :
64 MAKE_CSTRING(h1->GetName() << "_VS_" << h2->GetName() << "_" << testName));
65
66 h3->Reset();
67
68 for (int i = 0; i < h1->GetNcells(); ++i) {
69 h3->SetBinContent(i+1, residuals[i]);
70 }
71
72 const bool passed = ( options.find("CHI2") != string::npos ?
73 (chi2 < threshold) :
74 (chi2 > threshold) );
75
76 const JResultTitle title(testName, resultType, passed, chi2);
77
78 h3->SetTitle(title.getTitle().c_str());
79
80 const int Ndims = h3->GetDimension();
81
82 if (Ndims == 1) {
83 h3->GetYaxis()->SetTitle(resultType.c_str());
84 } else if (Ndims == 2) {
85 h3->GetZaxis()->SetTitle(resultType.c_str());
86 }
87
88 const JTestResult r (testName,
89 JRootObjectID(MAKE_STRING(h1->GetDirectory()->GetPath() << h1->GetName())),
90 JRootObjectID(MAKE_STRING(h2->GetDirectory()->GetPath() << h1->GetName())),
91 resultType, chi2, threshold, h3, passed);
92
93 this->push_back(r);
94 }
95
96
97 /**
98 * Read test parameters from input.
99 *
100 * \param in input stream
101 * \return input stream
102 */
103 std::istream& read(std::istream& in) override
104 {
105 using namespace JPP;
106
107 in >> threshold >> options;
108
109 if (threshold < 0.0) {
110 THROW(JValueOutOfRange, "JTestChi2::read(): Invalid threshold value " << threshold);
111 }
112
113 return in;
114 }
115
116 private:
117
118 double threshold; //!< Threshold p-value or chi2-value to decide if test is passed.
119 std::string options; //!< Options for the ROOT chi2 test.
120 };
121}
122
123#endif
#define THROW(JException_t, A)
Marco for throwing exception with std::ostream compatible message.
#define MAKE_CSTRING(A)
Make C-string.
Definition JPrint.hh:72
#define MAKE_STRING(A)
Make string.
Definition JPrint.hh:63
Class dedicated to standardize the title of the graphical objects produced by the JTest_t() derived c...
std::string getTitle() const
Returns a standard string to be used as title of a graphical root object.
Implementation of the Chi2 test for ROOT histograms.
Definition JTestChi2.hh:29
double threshold
Threshold p-value or chi2-value to decide if test is passed.
Definition JTestChi2.hh:118
void test(const TObject *o1, const TObject *o2) override
Applies Chi2 test for two ROOT TH1 histograms.
Definition JTestChi2.hh:46
std::string options
Options for the ROOT chi2 test.
Definition JTestChi2.hh:119
JTestChi2()
Default constructor.
Definition JTestChi2.hh:35
std::istream & read(std::istream &in) override
Read test parameters from input.
Definition JTestChi2.hh:103
Interface to read input and write output for TObject tests.
Definition JTest_t.hh:42
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.
This name space includes all other name spaces (except KM3NETDAQ, KM3NET and ANTARES).
Structure containing the result of the test.