ATLAS Offline Software
BinsSymmetric.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 <TF1.h>
14 #include <cstdio>
15 #include <TClass.h>
16 #include <ers/ers.h>
17 #include <iostream>
18 #include <string>
19 #include <cmath>
20 
21 #include <dqm_core/AlgorithmManager.h>
22 
23 static dqm_algorithms::BinsSymmetric myInstance;
24 
26 {
27  dqm_core::AlgorithmManager::instance().registerAlgorithm("BinsSymmetric",this);
28 }
29 
31 {
32 }
35 {
36  return new BinsSymmetric();
37 }
38 
41  const TObject & object,
42  const dqm_core::AlgorithmConfig & config)
43 {
44 
45  const TH1 * histogram;
46 
47  if (object.IsA()->InheritsFrom("TH1")){
48 
49  histogram = static_cast<const TH1*>(&object);
50 
51  if (histogram->GetDimension() > 3 ){
52  throw dqm_core::BadConfig( ERS_HERE, name, "dimension > 3 " );
53  }
54  } else {
55  throw dqm_core::BadConfig( ERS_HERE, name, "does not inherit from TH1" );
56  }
57 
58  if (histogram->GetEntries() == 0) {
59  ERS_DEBUG(1, "Histogram " <<histogram->GetName()<<" is Empty");
61  }
62 
63  const bool publish = (bool) dqm_algorithms::tools::GetFirstFromMap( "PublishBins", config.getParameters(), 0);
64  const int maxpublish = (int) dqm_algorithms::tools::GetFirstFromMap( "MaxPublish", config.getParameters(), 20);
65  const double mindiffabs = (double) dqm_algorithms::tools::GetFirstFromMap( "MaxDiffAbs", config.getParameters(), 0);
66  const double bin_threshold = (double) dqm_algorithms::tools::GetFirstFromMap( "NSigmaBin", config.getParameters(), 3);
67  const bool ignorezero = (bool) dqm_algorithms::tools::GetFirstFromMap("IgnoreZero",config.getParameters(), 0);
68 
69  double gthreshold;
70  double rthreshold;
71 
72  try {
73  rthreshold = dqm_algorithms::tools::GetFromMap( "NSigma", config.getRedThresholds() );
74  gthreshold = dqm_algorithms::tools::GetFromMap( "NSigma", config.getGreenThresholds() );
75  }
76  catch( dqm_core::Exception & ex ) {
77  throw dqm_core::BadConfig( ERS_HERE, name, ex.what(), ex );
78  }
79 
80  int count = 0;
81 
82  std::vector<int> range=dqm_algorithms::tools::GetBinRange(histogram, config.getParameters());
83 
85 
86  // vectors for global chisq calculation
87  std::vector<double> binvals,binerrs;
88  std::vector<double> refvals,referrs;
89  double small_num = 1.e-05;
90 
91  // logic to see if there is a bin at the central value. If so compare central to average of two bins on either side
92  bool even_nbins = ((range[1] - range[0] + 1) % 2 == 0) ? true : false;
93  int range_comp = (range[1] - range[0] + 1) / 2;
94 
95  int start_bin_low = range[1] - range_comp;
96  int start_bin_high = range[0] + range_comp;
97 
98  // check central bin if there is one
99  if (!even_nbins){
100 
101  double xbin0 = histogram->GetBinCenter(start_bin_low);
102 
103  double bin0 = histogram->GetBinContent(start_bin_low);
104  double bin1 = histogram->GetBinContent(start_bin_low + 1);
105  double bin2 = histogram->GetBinContent(start_bin_low - 1);
106 
107  double binerr0 = histogram->GetBinError(start_bin_low);
108  double binerr1 = histogram->GetBinError(start_bin_low + 1);
109  double binerr2 = histogram->GetBinError(start_bin_low - 1);
110 
111  double mean_bins = (bin1+bin2)/2.;
112  double errmean_bins = std::sqrt(std::pow(binerr1,2.)+std::pow(binerr2,2.)/2.);
113 
114  double diff = std::abs(bin0 - mean_bins);
115  double differr = std::sqrt(std::pow(binerr0,2.)+std::pow(errmean_bins,2));
116  double sigma = 1.;
117 
118  if ((!ignorezero) || (bin0 != 0 && mean_bins != 0)){
119 
120  binvals.push_back(bin0);
121  binerrs.push_back(binerr0);
122  // use reference bin for chisq calculation as the combined bin from neighbours
123  refvals.push_back(mean_bins);
124  referrs.push_back(errmean_bins);
125 
126  if (differr/diff > small_num) sigma = diff/differr;
127 
128  // sigma threshold used to highlight potentially problematic bins
129  if (sigma > bin_threshold && (std::abs(diff) > mindiffabs)) {
130 
131  ++count;
132 
133  if (publish && count <= maxpublish) {
134 
135  std::ostringstream os;
136  os << "Sigma(" << xbin0 << ")(" << bin0 << "," << mean_bins << ")";
137  std::string badbins = os.str();
138  result->tags_[badbins.c_str()] = sigma;
139  ERS_DEBUG(1,"x bin" << start_bin_low << " value " << bin0 << " sigma difference " << sigma);
140 
141  }
142  }
143  }
144  }
145 
146  // now loop over all the other bins
147  for (int i = 0; i < range_comp; ++i){
148 
149  double binhigh = histogram->GetBinContent(start_bin_high+i);
150  double binlow = histogram->GetBinContent(start_bin_low-i);
151 
152  double xbinhigh = histogram->GetBinCenter(start_bin_high+i);
153 
154  double binerrhigh = histogram->GetBinError(start_bin_high+i);
155  double binerrlow = histogram->GetBinError(start_bin_low-i);
156 
157  double diff = std::abs(binlow - binhigh);
158  double differr = std::sqrt(std::pow(binerrlow,2.)+std::pow(binerrhigh,2));
159 
160  if ((!ignorezero) || (binlow != 0 && binhigh != 0)){
161 
162  binvals.push_back(binhigh);
163  binerrs.push_back(binerrlow);
164  refvals.push_back(binlow);
165  referrs.push_back(binerrlow);
166 
167  double sigma = 1.;
168  if (differr > small_num) sigma = diff/differr;
169 
170  if (sigma > bin_threshold && (std::abs(diff) > mindiffabs)) {
171 
172  ++count;
173  if (publish && count <= maxpublish){
174 
175  std::ostringstream os;
176  os << "Sigma(" << xbinhigh << ")" << "(" << binlow << "," << binhigh << ")";
177  std::string badbins = os.str();
178  result->tags_[badbins.c_str()] = sigma;
179  ERS_DEBUG(1,"x bin " << start_bin_high+i << " value " << binhigh);
180  ERS_DEBUG(1,"x bin " << start_bin_low-i << " value " << binlow << " sigma difference " << sigma);
181  }
182  }
183  }
184  }
185 
186  std::pair<double,double> chisq_prob = dqm_algorithms::tools::CalcBinsProbChisq(binvals,binerrs,refvals,referrs);
187 
188  ERS_DEBUG(1, "Number of bins " << bin_threshold << " Sigma away from reference bin is " << count);
189  ERS_DEBUG(1, "Green threshold: "<< gthreshold << " bin(s); Red threshold : " << rthreshold << " bin(s) ");
190  result->tags_["NBins"] = count;
191  result->tags_["SigmaChisq"] = chisq_prob.second;
192 
193  // cut on sigma, this can be eventually modified to cut on prob as well
194 
195  double sigma_check = std::abs(chisq_prob.second);
196  if ( sigma_check <= gthreshold ) {
197  result->status_ = dqm_core::Result::Green;
198  } else if ( sigma_check < rthreshold ) {
199  result->status_ = dqm_core::Result::Yellow;
200  } else {
201  result->status_ = dqm_core::Result::Red;
202  }
203  return result;
204 
205 }
206 
208 
209  out<<"BinsSymmetric: Checks if histogram is symmetric around mid-point of given range. works only for 1-D histograms \n"<<std::endl;
210 out<<"Mandatory Green/Red Threshold: NSigma: N sigma for chisq global fit probability to be away from 0 from unit normal distribution. Thresholds are for Green/Red results\n"<<std::endl;
211  out<<"Optional Parameter: NSigmaBin: Number of sigma bins must be over opposite bin to be flagged and reported (default 3) \n"<<std::endl;
212  out<<"Optional Parameter: MinStat: Minimum histogram statistics needed to perform Algorithm"<<std::endl;
213  out<<"Optional Parameter: xmin: minimum x range"<<std::endl;
214  out<<"Optional Parameter: xmax: maximum x range"<<std::endl;
215  out<<"Optional Parameter: PublishBins: Save bins which are different from average in Result (set to 1)"<<std::endl;
216  out<<"Optional Parameter: MaxDiffAbs: test fails if NBins more than NSigma away and NBins more than MaxDiffAbs (absolut difference) away"<<std::endl;
217  out<<"Optional Parameter: MaxDiffRel: test fails if NBins more than NSigma away and NBins more than MaxDiffRel (relative difference) away\n"<<std::endl;
218 
219 }
dqm_algorithms::tools::GetBinRange
std::vector< int > GetBinRange(const TH1 *histogram, const std::map< std::string, double > &params)
Definition: AlgorithmHelper.cxx:380
BinsSymmetric.h
pdg_comparison.sigma
sigma
Definition: pdg_comparison.py:324
get_generator_info.result
result
Definition: get_generator_info.py:21
IsA
#define IsA
Declare the TObject style functions.
Definition: xAODTEventBranch.h:59
CaloCellPos2Ntuple.int
int
Definition: CaloCellPos2Ntuple.py:24
conifer::pow
constexpr int pow(int x)
Definition: conifer.h:20
dqm_algorithms::tools::CalcBinsProbChisq
std::pair< double, double > CalcBinsProbChisq(const std::vector< double > &inputval, const std::vector< double > &inputerr, double x0, double x0_err)
Definition: AlgorithmHelper.cxx:1411
python.AthDsoLogger.out
out
Definition: AthDsoLogger.py:71
mc.diff
diff
Definition: mc.SFGenPy8_MuMu_DD.py:14
dqt_zlumi_pandas.bin2
bin2
Definition: dqt_zlumi_pandas.py:330
XMLtoHeader.count
count
Definition: XMLtoHeader.py:85
config
Definition: PhysicsAnalysis/AnalysisCommon/AssociationUtils/python/config.py:1
instance
std::map< std::string, double > instance
Definition: Run_To_Get_Tags.h:8
lumiFormat.i
int i
Definition: lumiFormat.py:92
dqm_algorithms::BinsSymmetric::BinsSymmetric
BinsSymmetric()
Definition: BinsSymmetric.cxx:25
Result
ICscStripFitter::Result Result
Definition: CalibCscStripFitter.cxx:13
plotBeamSpotVxVal.range
range
Definition: plotBeamSpotVxVal.py:195
python.handimod.Green
int Green
Definition: handimod.py:524
xAOD::double
double
Definition: CompositeParticle_v1.cxx:159
dqm_algorithms::BinsSymmetric::clone
BinsSymmetric * clone()
Definition: BinsSymmetric.cxx:34
bin2
Definition: KillBinsByStrip.h:34
ReadFromCoolCompare.os
os
Definition: ReadFromCoolCompare.py:231
dqm_algorithms::BinsSymmetric
Definition: BinsSymmetric.h:19
dqm_algorithms::BinsSymmetric::~BinsSymmetric
~BinsSymmetric()
Definition: BinsSymmetric.cxx:30
python.handimod.Red
Red
Definition: handimod.py:551
name
std::string name
Definition: Control/AthContainers/Root/debug.cxx:195
dqm_algorithms::BinsSymmetric::printDescription
void printDescription(std::ostream &out)
Definition: BinsSymmetric.cxx:207
dqt_zlumi_pandas.bin1
bin1
Definition: dqt_zlumi_pandas.py:329
TH1
Definition: rootspy.cxx:268
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
dqm_algorithms::BinsSymmetric::execute
dqm_core::Result * execute(const std::string &, const TObject &, const dqm_core::AlgorithmConfig &)
Definition: BinsSymmetric.cxx:40
xAOD::bool
setBGCode setTAP setLVL2ErrorBits bool
Definition: TrigDecision_v1.cxx:60
histogram
std::string histogram
Definition: chains.cxx:52