Jpp 20.0.0-rc.2
the software that should make you happy
Loading...
Searching...
No Matches
JTestSignificance.hh
Go to the documentation of this file.
1#ifndef __JCOMPAREHISTOGRAMS__JTESTSIGNIFICANCE__
2#define __JCOMPAREHISTOGRAMS__JTESTSIGNIFICANCE__
3
4#include <istream>
5#include <ostream>
6
7#include "JLang/JException.hh"
8
11
12#include "TH1.h"
13
14
15/**
16 * \author rgruiz, bofearreigh, bjung
17 */
18namespace JCOMPAREHISTOGRAMS {}
19namespace JPP { using namespace JCOMPAREHISTOGRAMS; }
20
21namespace JCOMPAREHISTOGRAMS {
22
23 /**
24 * Significance test
25 *
26 * Compares two histograms \f$ H_a \f$ and \f$ H_b \f$ by calculating the average bin significance \n
27 *
28 * \f[
29 * S = \frac{1}{N} \cdot \sum_{i=0}^{N} \frac{|a_i - K \cdot b_i|}{\sqrt{\sigma_{a_i}^2 + K^2 \cdot \sigma_{b_i}^2}}
30 * \f]
31 *
32 * where:
33 * - \f$ N \f$ is the number of bins in \f$ H_a \f$ and \f$ H_b \f$.
34 * - \f$ a_i \f$ and \f$ b_i \f$ are the contents of bin \f$ i \f$ in \f$ H_a \f$ and \f$ H_b \f$.
35 * - \f$ K \f$ is a normalisation parameter which can be specified by the user.
36 * - It should be interpreted as the integral of \f$ H_a \f$ divided by the integral of \f$ H_b \f$.
37 * - Note that if the specified value of K is negative, the value will be reset to the integral of \f$ H_a \f$ divided by the integral of \f$ H_b \f$.
38 * - \f$\sigma_{a_i}\f$ and \f$\sigma_{b_i}\f$ are the bin content uncertainties corresponding to respectively \f$ a_i \f$ and \f$ b_i \f$.
39 *
40 * A threshold value for \f$ S \f$ is used to tag the test as passed or failed.
41 */
43 public JTest_t
44 {
45 public:
46
47 /**
48 * Default constructor.
49 */
51 JTest_t("Significance", "Significance")
52 {}
53
54
55 /**
56 * Applies Significance test for two ROOT TH1 histograms.
57 *
58 * \param o1 First histogram
59 * \param o2 Second histogram
60 */
61 void test(const TObject* o1, const TObject* o2) override
62 {
63 using namespace std;
64 using namespace JPP;
65
66 const TH1* h1 = dynamic_cast<const TH1*>(o1);
67 const TH1* h2 = dynamic_cast<const TH1*>(o2);
68
69 if (h1 == NULL || h2 == NULL) {
70 THROW(JValueOutOfRange, "JTestSignificance::test(): Could not cast given TObjects to TH1.");
71 }
72
73 if(h1->GetNbinsX() != h2->GetNbinsX() ||
74 h1->GetNbinsY() != h2->GetNbinsY() ||
75 h1->GetNbinsZ() != h2->GetNbinsZ()) {
76 THROW(JValueOutOfRange, "JTestSignificance::test(): Histograms with different bining. The objects: " <<
77 h1->GetName() << " and " << h2->GetName() << " can not be compared." << endl);
78 }
79
80 if (K < 0) {
81 K = h2->GetEntries() / h1->GetEntries();
82 }
83
84 TH1* h3 = (TH1*) h1->Clone(h1->GetName() == h2->GetName() ?
85 MAKE_CSTRING(h1->GetName() << "_" << testName) :
86 MAKE_CSTRING(h1->GetName() << "_VS_" << h2->GetName() << "_" << testName));
87
88 h3->Add(h2,-1*K);
89
90 double S = 0;
91
92 for (int i=1 ; i <= h1->GetNbinsX() ; ++i){
93 for (int j=1 ; j <= h1->GetNbinsY() ; ++j){
94 for (int k=1 ; k <= h1->GetNbinsZ() ; ++k){
95
96 const double a = h1->GetBinContent(i,j,k);
97 const double b = h2->GetBinContent(i,j,k);
98
99 const double ea = usePoissonStatistics ? sqrt(a) : h1->GetBinError(i,j,k);
100 const double eb = usePoissonStatistics ? sqrt(b) : h2->GetBinError(i,j,k);
101
102 if (a!=0 || b!=0) {
103 S += fabs((a - K*b)/sqrt(ea*ea + K*K*eb*eb));
104 }
105 }
106 }
107 }
108
109 S /= (h1->GetNbinsX()*h1->GetNbinsY()*h1->GetNbinsZ());
110
111 const bool passed = (S < threshold);
112
113 const JResultTitle title(testName, resultType, passed, S);
114
115 h3->SetTitle(title.getTitle().c_str());
116
117 const int Ndims = h3->GetDimension();
118
119 if (Ndims == 1) {
120 h3->GetYaxis()->SetTitle(resultType.c_str());
121 } else if (Ndims == 2) {
122 h3->GetZaxis()->SetTitle(resultType.c_str());
123 }
124
125 const JTestResult r(testName,
126 JRootObjectID(MAKE_STRING(h1->GetDirectory()->GetPath() << h1->GetName())),
127 JRootObjectID(MAKE_STRING(h2->GetDirectory()->GetPath() << h1->GetName())),
128 resultType, S, threshold, h3, passed);
129
130 this->push_back(r);
131 }
132
133
134 /**
135 * Read test parameters from input.
136 *
137 * \param in input stream
138 * \return input stream
139 */
140 std::istream& read(std::istream& in) override
141 {
142 using namespace JPP;
143
144 in >> threshold >> K >> usePoissonStatistics;
145
146 if (threshold < 0.0) {
147 THROW(JValueOutOfRange, "JTestSignificance::read(): Invalid threshold value " << threshold);
148 }
149
150 return in;
151 }
152
153 private:
154
155 double threshold; //!< threshold p-value to decide if test is passed.
156 double K; //!< normalization factor between histograms.
157 bool usePoissonStatistics; //!< toggle use of Poisson statistics
158 };
159}
160
161#endif
Exceptions.
#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.
void test(const TObject *o1, const TObject *o2) override
Applies Significance test for two ROOT TH1 histograms.
double K
normalization factor between histograms.
std::istream & read(std::istream &in) override
Read test parameters from input.
bool usePoissonStatistics
toggle use of Poisson statistics
double threshold
threshold p-value to decide if test is passed.
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.