ATLAS Offline Software
Chi2Test.cxx
Go to the documentation of this file.
1 /*
2  Copyright (C) 2002-2022 CERN for the benefit of the ATLAS collaboration
3 */
4 
9 #include <dqm_core/AlgorithmConfig.h>
12 #include <TH1.h>
13 #include <TEfficiency.h>
14 #include <TF1.h>
15 #include <TClass.h>
16 #include <ers/ers.h>
17 
18 #include <dqm_core/AlgorithmManager.h>
19 static dqm_algorithms::Chi2Test chi2_ndf( "Chi2_per_NDF" );
20 static dqm_algorithms::Chi2Test chi2_p( "Prob" );
21 static dqm_algorithms::Chi2Test chi2_uw("ProbUW" );
22 static dqm_algorithms::Chi2Test chi2_ww("ProbWW" );
23 static dqm_algorithms::Chi2Test chi2_chi2("Chi2");
24 
25 
26 
28  : m_name ( name )
29 {
30  dqm_core::AlgorithmManager::instance().registerAlgorithm("Chi2Test_"+ name, this );
31 }
32 
35 {
36  return new Chi2Test( m_name );
37 }
38 
39 
41 dqm_algorithms::Chi2Test::execute( const std::string & name ,
42  const TObject & object,
43  const dqm_core::AlgorithmConfig & config )
44 {
45  const TH1* histogram = 0;
46  const TEfficiency* efficiency = 0;
47  TH1* passed_histogram = 0;
48  TH1* total_histogram = 0;
49 
50  if(object.IsA()->InheritsFrom( "TH1" )) {
51  histogram = static_cast<const TH1*>( &object );
52  if (histogram->GetDimension() > 2 ){
53  throw dqm_core::BadConfig( ERS_HERE, name, "dimension > 2 " );
54  }
55  } else if(object.IsA()->InheritsFrom( "TEfficiency" )) {
56  // get the histograms from TEfficiency object to perform Chi2Test
57  efficiency = static_cast<const TEfficiency*>( &object);
58  if (efficiency->GetDimension() > 2 ){
59  throw dqm_core::BadConfig( ERS_HERE, name, "dimension > 2 " );
60  }
61 
62  passed_histogram = efficiency->GetCopyPassedHisto();
63  total_histogram = efficiency->GetCopyTotalHisto();
64  passed_histogram->Divide(total_histogram);
65 
66  } else {
67  throw dqm_core::BadConfig( ERS_HERE, name, "does not inherit from TH1 or TEfficiency");
68  }
69 
70  const double minstat = dqm_algorithms::tools::GetFirstFromMap( "MinStat", config.getParameters(), -1);
71  double current_stat = 0;
72 
73  if(object.IsA()->InheritsFrom( "TH1" )) {
74  current_stat = histogram->GetEntries();
75  } else if(object.IsA()->InheritsFrom( "TEfficiency" )){
76  current_stat = total_histogram->GetEntries();
77  }
78 
79  if(current_stat < minstat ) {
81  result->tags_["InsufficientEntries"] = current_stat;
82  return result;
83  }
84 
85  TH1 * refhist = 0;
86  TH1 * ref_total_hist;
87  TEfficiency * refeff;
88  double gthresho;
89  double rthresho;
90  std::string option;
91  std::string thresholdname="P";
92  if (m_name == "Chi2_per_NDF") {
93  option="Chi2/ndfUU";
94  thresholdname="Chi2_per_NDF";
95  }else if (m_name == "Prob" ) {
96  option="UU";
97  }else if (m_name == "ProbUW") {
98  option="UW";
99  }else if (m_name == "ProbWW") {
100  option="WW";
101  } else if (m_name == "Chi2") {
102  option="CHI2UU";
103  thresholdname="Chi2";
104  } else {
105  throw dqm_core::BadConfig( ERS_HERE, "None", m_name );
106  }
107  try {
108  gthresho = dqm_algorithms::tools::GetFromMap( thresholdname, config.getGreenThresholds() );
109  rthresho = dqm_algorithms::tools::GetFromMap( thresholdname, config.getRedThresholds() );
110  }
111  catch ( dqm_core::Exception & ex ) {
112  throw dqm_core::BadConfig( ERS_HERE, name, ex.what(), ex );
113 
114  }
115 
116  if(object.IsA()->InheritsFrom( "TH1" )) {
117  try {
118  refhist = dynamic_cast<TH1 *>( config.getReference() );
119  }
120  catch ( dqm_core::Exception & ex ) {
121  throw dqm_core::BadRefHist(ERS_HERE,name," Could not retreive reference");
122  }
123  } else if(object.IsA()->InheritsFrom( "TEfficiency" )){
124  try {
125  refeff = dynamic_cast<TEfficiency *>( config.getReference() );
126  }
127  catch ( dqm_core::Exception & ex ) {
128  throw dqm_core::BadRefHist(ERS_HERE,name," Could not retreive reference");
129  }
130 
131  refhist = refeff->GetCopyPassedHisto();
132  ref_total_hist = refeff->GetCopyTotalHisto();
133  refhist->Divide(ref_total_hist);
134  }
135 
136  if (!refhist) { throw dqm_core::BadRefHist(ERS_HERE,name,"Bad reference type"); }
137 
138  double value = 0;
139  if(object.IsA()->InheritsFrom( "TH1" )) {
140 
141  if (histogram->GetDimension() != refhist->GetDimension() ) {
142  throw dqm_core::BadRefHist( ERS_HERE, "Dimension", name );
143  }
144 
145  if ((histogram->GetNbinsX() != refhist->GetNbinsX()) || (histogram->GetNbinsY() != refhist->GetNbinsY())) {
146  throw dqm_core::BadRefHist( ERS_HERE, "number of bins", name );
147  }
148 
149  value = histogram->Chi2Test( refhist, option.c_str() );
150 
151  } else if(object.IsA()->InheritsFrom( "TEfficiency" )){
152 
153  if (passed_histogram->GetDimension() != refhist->GetDimension() ) {
154  throw dqm_core::BadRefHist( ERS_HERE, "Dimension", name );
155  }
156 
157  if ((passed_histogram->GetNbinsX() != refhist->GetNbinsX()) || (passed_histogram->GetNbinsY() != refhist->GetNbinsY())) {
158  throw dqm_core::BadRefHist( ERS_HERE, "number of bins", name );
159  }
160 
161  value = passed_histogram->Chi2Test( refhist, option.c_str() );
162  }
163 
164  ERS_DEBUG(1,"Green threshold: "<< gthresho << "; Red threshold: " << rthresho );
165  ERS_DEBUG(1,"Chi2 Test with Option " << option << " is " << value );
166 
168  result->tags_[thresholdname] = value;
169 
170  if (thresholdname == "P") {//Checking against a probability
171  if ( value >= gthresho ) {
172  result->status_ = dqm_core::Result::Green;
173  } else if ( value > rthresho ) {
174  result->status_ = dqm_core::Result::Yellow;
175  } else {
176  result->status_ = dqm_core::Result::Red;
177  }
178  } else {//checking against Chi2 value
179  if ( value <= gthresho ) {
180  result->status_ = dqm_core::Result::Green;
181  } else if ( value < rthresho ) {
182  result->status_ = dqm_core::Result::Yellow;
183  } else {
184  result->status_ = dqm_core::Result::Red;
185  }
186 
187  }
188 
189  ERS_DEBUG(2,"Result: "<<*result);
190  return result;
191 
192 }
193 void
195 {
196  m_option="UU";
197  std::string thresholdname = "P";
198  if (m_name == "Chi2_per_NDF") {
199  m_option="Chi2/ndf";
200  thresholdname="Chi2_per_NDF";
201  }else if (m_name == "Prob") {
202  m_option="UU";
203  }else if (m_name == "ProbUW") {
204  m_option="UW";
205  }else if (m_name == "ProbWW") {
206  m_option="WW";
207  }else if (m_name == "Chi2") {
208  m_option="CHI2";
209  thresholdname="Chi2";
210  }
211 
212  out<<"Chi2Test_"+ m_name+": Gives back "+thresholdname+" after performing Chi2 test on histogram against referece histogram with option: "<<m_option<<" (see TH1::GetChi2Test)"<<std::endl;
213 
214 
215  out<<"Mandatory Green/Red Threshold: "+ thresholdname+" : "+thresholdname+" to give Green/Red result\n"<<std::endl;
216 
217  out<<"Optional Parameter: MinStat: Minimum histogram statistics needed to perform Algorithm\n"<<std::endl;
218 
219 }
220 
dqm_algorithms::Chi2Test::Chi2Test
Chi2Test(const std::string &name)
Definition: Chi2Test.cxx:27
Undefined
@ Undefined
Definition: MaterialTypes.h:8
get_generator_info.result
result
Definition: get_generator_info.py:21
IsA
#define IsA
Declare the TObject style functions.
Definition: xAODTEventBranch.h:59
Chi2Test.h
dqm_algorithms::Chi2Test::printDescription
void printDescription(std::ostream &out)
Definition: Chi2Test.cxx:194
python.AthDsoLogger.out
out
Definition: AthDsoLogger.py:71
dqm_algorithms::Chi2Test::execute
dqm_core::Result * execute(const std::string &, const TObject &, const dqm_core::AlgorithmConfig &)
Definition: Chi2Test.cxx:41
athena.value
value
Definition: athena.py:122
config
Definition: PhysicsAnalysis/AnalysisCommon/AssociationUtils/python/config.py:1
instance
std::map< std::string, double > instance
Definition: Run_To_Get_Tags.h:8
efficiency
void efficiency(std::vector< double > &bins, std::vector< double > &values, const std::vector< std::string > &files, const std::string &histname, const std::string &tplotname, const std::string &label="")
Definition: dependence.cxx:128
Result
ICscStripFitter::Result Result
Definition: CalibCscStripFitter.cxx:13
python.handimod.Green
int Green
Definition: handimod.py:524
python.handimod.Red
Red
Definition: handimod.py:551
name
std::string name
Definition: Control/AthContainers/Root/debug.cxx:195
TH1
Definition: rootspy.cxx:268
dqm_algorithms::Chi2Test::clone
Chi2Test * clone()
Definition: Chi2Test.cxx:34
dqm_algorithms::Chi2Test
Definition: Chi2Test.h:19
AlgorithmHelper.h
dqm_algorithms::tools::GetFromMap
const T & GetFromMap(const std::string &pname, const std::map< std::string, T > &params)
Definition: AlgorithmHelper.h:114
pickleTool.object
object
Definition: pickleTool.py:30
dqm_algorithms::tools::GetFirstFromMap
double GetFirstFromMap(const std::string &paramName, const std::map< std::string, double > &params)
Definition: AlgorithmHelper.cxx:339
histogram
std::string histogram
Definition: chains.cxx:52