Jpp  18.3.1
the software that should make you happy
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
JTestChi2_Bin.hh
Go to the documentation of this file.
1 #ifndef __JCOMPAREHISTOGRAMS__JTESTCHI2_BIN__
2 #define __JCOMPAREHISTOGRAMS__JTESTCHI2_BIN__
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, bjung
17  */
18 namespace JCOMPAREHISTOGRAMS {}
19 namespace JPP { using namespace JCOMPAREHISTOGRAMS; }
20 
21 namespace JCOMPAREHISTOGRAMS {
22 
23  /**
24  * Implementation of a bin-by-bin Chi2 test for 2D histograms.\n
25  * 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.
26  */
27  class JTestChi2_Bin:
28  public JTest_t
29  {
30  public:
31 
32  /**
33  * Default constructor.
34  */
36  JTest_t("Chi2_Bin", "Outliers[%]")
37  {}
38 
39 
40  /**
41  * Tests the statistical compatibility of two ROOT 2D 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_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  }
127 
128 
129  /**
130  * Read test parameters from input.
131  *
132  * \param in input stream
133  * \return input stream
134  */
135  std::istream& read(std::istream& in) override
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  }
151 
152 
153  private:
154 
155  double chi2Threshold; //!< threshold chi2-value to decide if test is passed for a bin.
156  double outliersThreshold; //!< fraction of bins allowed to fail the test.
157  };
158 }
159 
160 #endif
Exceptions.
then fatal No hydrophone data file $HYDROPHONE_TXT fi sort gr k
Interface to read input and write output for TObject tests.
Definition: JTest_t.hh:40
std::string getTitle() const
Returns a standard string to be used as title of a graphical root object.
Definition: JResultTitle.hh:57
Class dedicated to standardize the title of the graphical objects produced by the JTest_t() derived c...
Definition: JResultTitle.hh:25
#define THROW(JException_t, A)
Marco for throwing exception with std::ostream compatible message.
Definition: JException.hh:712
Definition: JRoot.hh:19
#define MAKE_CSTRING(A)
Make C-string.
Definition: JPrint.hh:136
Auxiliary class to handle file name, ROOT directory and object name.
data_type r[M+1]
Definition: JPolint.hh:868
JTestChi2_Bin()
Default constructor.
double chi2Threshold
threshold chi2-value to decide if test is passed for a bin.
#define MAKE_STRING(A)
Make string.
Definition: JPrint.hh:127
const double sigma[]
Definition: JQuadrature.cc:74
const int n
Definition: JPolint.hh:786
Implementation of a bin-by-bin Chi2 test for 2D histograms.
const std::string resultType
test result type
Definition: JTest_t.hh:181
const std::string testName
test name
Definition: JTest_t.hh:180
std::istream & read(std::istream &in) override
Read test parameters from input.
then usage $script< input file >[option[primary[working directory]]] nWhere option can be N
Definition: JMuonPostfit.sh:40
then fatal Wrong number of arguments fi set_variable DETECTOR $argv[1] set_variable INPUT_FILE $argv[2] eval JPrintDetector a $DETECTOR O IDENTIFIER eval JPrintDetector a $DETECTOR O SUMMARY JAcoustics sh $DETECTOR_ID source JAcousticsToolkit sh CHECK_EXIT_CODE typeset A EMITTERS get_tripods $WORKDIR tripod txt EMITTERS get_transmitters $WORKDIR transmitter txt EMITTERS for EMITTER in
Definition: JCanberra.sh:48
void test(const TObject *o1, const TObject *o2) override
Tests the statistical compatibility of two ROOT 2D histograms.
then if[[!-f $DETECTOR]] then JDetector sh $DETECTOR fi cat $WORKDIR trigger_parameters txt<< EOFtrigger3DMuon.enabled=1;trigger3DMuon.numberOfHits=5;trigger3DMuon.gridAngle_deg=1;ctMin=0.0;TMaxLocal_ns=15.0;EOF set_variable TRIGGEREFFICIENCY_TRIGGERED_EVENTS_ONLY INPUT_FILES=() for((i=1;$i<=$NUMBER_OF_RUNS;++i));do JSirene.sh $DETECTOR $JPP_DATA/genhen.km3net_wpd_V2_0.evt.gz $WORKDIR/sirene_ ${i}.root JTriggerEfficiency.sh $DETECTOR $DETECTOR $WORKDIR/sirene_ ${i}.root $WORKDIR/trigger_efficiency_ ${i}.root $WORKDIR/trigger_parameters.txt $JPP_DATA/PMT_parameters.txt INPUT_FILES+=($WORKDIR/trigger_efficiency_ ${i}.root) done for ANGLE_DEG in $ANGLES_DEG[*];do set_variable SIGMA_NS 3.0 set_variable OUTLIERS 3 set_variable OUTPUT_FILE $WORKDIR/matrix\[${ANGLE_DEG}\deg\].root $JPP_DIR/examples/JReconstruction-f"$INPUT_FILES[*]"-o $OUTPUT_FILE-S ${SIGMA_NS}-A ${ANGLE_DEG}-O ${OUTLIERS}-d ${DEBUG}--!fiif[[$OPTION=="plot"]];then if((0));then for H1 in h0 h1;do JPlot1D-f"$WORKDIR/matrix["${^ANGLES_DEG}" deg].root:${H1}"-y"1 2e3"-Y-L TR-T""-\^"number of events [a.u.]"-> o chi2
Definition: JMatrixNZ.sh:106
Structure containing the result of the test.
Definition: JTestResult.hh:28
int j
Definition: JPolint.hh:792
double outliersThreshold
fraction of bins allowed to fail the test.