Jpp  18.3.0
the software that should make you happy
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
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  */
16 namespace JCOMPAREHISTOGRAMS {}
17 namespace JPP { using namespace JCOMPAREHISTOGRAMS; }
18 
19 namespace 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
Interface to read input and write output for TObject tests.
Definition: JTest_t.hh:40
void test(const TObject *o1, const TObject *o2) override
Applies Chi2 test for two ROOT TH1 histograms.
Definition: JTestChi2.hh:46
std::string getTitle() const
Returns a standard string to be used as title of a graphical root object.
Definition: JResultTitle.hh:57
JTestChi2()
Default constructor.
Definition: JTestChi2.hh:35
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
std::string options
Options for the ROOT chi2 test.
Definition: JTestChi2.hh:119
#define MAKE_STRING(A)
Make string.
Definition: JPrint.hh:127
std::istream & read(std::istream &in) override
Read test parameters from input.
Definition: JTestChi2.hh:103
then awk string
const std::string resultType
test result type
Definition: JTest_t.hh:181
const std::string testName
test name
Definition: JTest_t.hh:180
Implementation of the Chi2 test for ROOT histograms.
Definition: JTestChi2.hh:27
double threshold
Threshold p-value or chi2-value to decide if test is passed.
Definition: JTestChi2.hh:118
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
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