ATLAS Offline Software
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Modules Pages
BinThreshold.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 <TClass.h>
15 #include <ers/ers.h>
16 
17 #include <iostream>
18 #include <cmath>
19 #include <dqm_core/AlgorithmManager.h>
20 
21 namespace
22 {
23  dqm_algorithms::BinThreshold GreaterThan( "GreaterThan" );
24  dqm_algorithms::BinThreshold GreaterThanAbs( "GreaterThanAbs" );
25  dqm_algorithms::BinThreshold GreaterThanNonZeroMedian( "GreaterThanNonZeroMedian" );
26  dqm_algorithms::BinThreshold GreaterThanEqual( "GreaterThanEqual" );
27  dqm_algorithms::BinThreshold LessThan( "LessThan" );
28  dqm_algorithms::BinThreshold LessThanAbs( "LessThanAbs" );
29  dqm_algorithms::BinThreshold LessThanNonZeroMedian( "LessThanNonZeroMedian" );
30  dqm_algorithms::BinThreshold LessThanEqual( "LessThanEqual" );
32  dqm_algorithms::BinThreshold NotEqual( "NotEqual" );
33 }
34 
35 
37  : m_name( name )
38 {
39  dqm_core::AlgorithmManager::instance().registerAlgorithm("Bins_"+name+"_Threshold", this);
40 }
41 
44 {
45 
46  return new BinThreshold( m_name );
47 }
48 
49 
52  const TObject & object,
53  const dqm_core::AlgorithmConfig & config )
54 {
55  const TH1 * histogram;
56 
57  if( object.IsA()->InheritsFrom( "TH1" ) ) {
58  histogram = static_cast<const TH1*>(&object);
59  if (histogram->GetDimension() > 2 ){
60  throw dqm_core::BadConfig( ERS_HERE, name, "dimension > 2 " );
61  }
62  } else {
63  throw dqm_core::BadConfig( ERS_HERE, name, "does not inherit from TH1" );
64  }
65 
66  const double minstat = dqm_algorithms::tools::GetFirstFromMap( "MinStat", config.getParameters(), -1);
67  const bool publish = (bool) dqm_algorithms::tools::GetFirstFromMap( "PublishBins", config.getParameters(), 0);
68  const int maxpublish = (int) dqm_algorithms::tools::GetFirstFromMap( "MaxPublish", config.getParameters(), 20);
69  const int xmax = (int) dqm_algorithms::tools::GetFirstFromMap( "xMax", config.getParameters(), -1);
70  const int xmin = (int) dqm_algorithms::tools::GetFirstFromMap( "xMin", config.getParameters(), -1);
71  const int ymax = (int) dqm_algorithms::tools::GetFirstFromMap( "yMax", config.getParameters(), -1);
72  const int ymin = (int) dqm_algorithms::tools::GetFirstFromMap( "yMin", config.getParameters(), -1);
73 
74  std::string ignoreBins = dqm_algorithms::tools::GetFirstFromMap("IgnoreBins", config.getGenericParameters(), "-1"); //The format is a list of int pairs, separated with : and split by comas. The symbol * can be used to indicate rows or columns. Ex: "2:3,5:*,*:7"
75 
76  std::vector<int> hotColumns, hotRows;
77  std::vector<std::vector<int>> hotBins;
78 
79  parseVetoList(ignoreBins, hotRows, hotColumns, hotBins);
80 
81  bool vetoHotRows = false; if (hotRows.size() > 0) vetoHotRows = true;
82  bool vetoHotBins = false; if (hotBins.size() > 0) vetoHotBins = true;
83  bool vetoHotColumns = false; if (hotColumns.size() > 0) vetoHotColumns = true;
84 
85  if (histogram->GetEntries() < minstat ) {
87  result->tags_["InsufficientEntries"] = histogram->GetEntries();
88  return result;
89  }
90 
91  double bin_threshold;
92  double gthreshold;
93  double rthreshold;
94  try {
95  bin_threshold = dqm_algorithms::tools::GetFirstFromMap( "BinThreshold", config.getParameters() );
96  rthreshold = dqm_algorithms::tools::GetFromMap( "NBins", config.getRedThresholds() );
97  gthreshold = dqm_algorithms::tools::GetFromMap( "NBins", config.getGreenThresholds() );
98  }
99  catch ( dqm_core::Exception & ex ) {
100  throw dqm_core::BadConfig( ERS_HERE, name, ex.what(), ex );
101  }
102 
103 
104  int count = 0;
105  std::vector<int> range=dqm_algorithms::tools::GetBinRange(histogram, config.getParameters());
107  TH1* resulthisto;
108  if (histogram->InheritsFrom("TH2")) {
109  resulthisto=(TH1*)(histogram->Clone());
110  } else if (histogram->InheritsFrom("TH1")) {
111  resulthisto=(TH1*)(histogram->Clone());
112  } else {
113  throw dqm_core::BadConfig( ERS_HERE, name, "does not inherit from TH1" );
114  }
115 
116  resulthisto->Reset();
117  if(m_name.find("Median")!=std::string::npos){
118  std::vector<double> bin_vals;
119  for ( int i = range[0]; i <= range[1]; ++i ) {
120  for ( int j = range[2]; j <= range[3]; ++j ) {
121  if(histogram -> GetBinContent(i,j) > 0) bin_vals . push_back( histogram -> GetBinContent(i,j) );
122  }
123  }
124  std::sort(bin_vals.begin(), bin_vals.end());
125  unsigned bin_vals_size = bin_vals.size();
126  double median = 0;
127  if(bin_vals_size%2==0 && bin_vals_size>1) median = (bin_vals.at(bin_vals_size/2-1)+bin_vals.at(bin_vals_size/2))/2;
128  else if(bin_vals_size%2==1 && bin_vals_size>1) median = bin_vals.at(bin_vals_size/2-1);
129  else if(bin_vals_size==1) median = bin_vals.at(0);
130 
131  double min_bin_threshold = dqm_algorithms::tools::GetFirstFromMap( "MinBinThreshold", config.getParameters() , -99999);
132  double max_bin_threshold = dqm_algorithms::tools::GetFirstFromMap( "MaxBinTrheshold", config.getParameters() , -99999);
133  bin_threshold=median*bin_threshold;
134 
135  bin_threshold = std::max(bin_threshold, min_bin_threshold);
136  if(max_bin_threshold > -1 ) bin_threshold = std::min(bin_threshold, max_bin_threshold);
137 
138  result->tags_["Effective_BinThreshold"] = bin_threshold;
139  }
140 
141  //bools for skiping threshold comparison of vetoed rows, columns and bins
142  bool skipColumn = false;
143  bool skipRow = false;
144  bool skipBin = false;
145 
146  for ( int i = range[0]; i <= range[1]; ++i ) {
147  skipColumn = false;
148  for (int column : hotColumns){
149  if (vetoHotColumns && ( column == i || skipColumn == true)) skipColumn = true;
150  }
151  //Skip column if it is in list of vetoed columns
152  if (skipColumn) continue;
153  //Skip bin threshold comparison if xmax!=-1 and i>xmax
154  if (xmax!=-1 && i>xmax) continue;
155  //Skip bin threshold comparison if xmin!=-1 and i<xmin
156  if (xmin!=-1 && i<xmin) continue;
157 
158  for ( int j = range[2]; j <= range[3]; ++j ) {
159  skipRow = false; skipBin = false;
160  for (int row : hotRows){
161  if (vetoHotRows && ( row == j || skipRow == true)) skipRow = true;
162  }
163  for (const auto& pair : hotBins){
164  if (vetoHotBins && ( (pair[0] == i && pair[1] == j) || skipBin == true)) skipBin = true;
165  }
166  //Skip Bin if it is in list of vetoed bins
167  if (skipBin) continue;
168  //Skip Row if it is in list of vetoed rows
169  if (skipRow) continue;
170  //Skip bin threshold comparison if ymax!=-1 and j>ymax
171  if (ymax!=-1 && j>ymax) continue;
172  //Skip bin threshold comparison if ymin!=-1 and j<ymin
173  if (ymin!=-1 && j<ymin) continue;
174 
175  double content= histogram -> GetBinContent(i,j);
176  if ( CompareBinThreshold(m_name, content, bin_threshold )) {
177  ++count;
178  resulthisto->SetBinContent(i,j,content);
179  if (publish && count< maxpublish){
181  }
182  }
183  }
184  }
185 
186  ERS_DEBUG(1,"Number of bins " << m_name << " treshold of " << bin_threshold << " is " << count );
187  ERS_DEBUG(1,"Green threshold: "<< gthreshold << " bin(s); Red threshold : " << rthreshold << " bin(s) ");
188 
189  int TotalBins = (int) dqm_algorithms::tools::GetFirstFromMap( "TotalBins", config.getParameters() , -99999);
190  if(TotalBins > -10){
191  if(TotalBins < 0) TotalBins = histogram->GetNbinsX()*histogram->GetNbinsY()*histogram->GetNbinsZ();
192  int effectiveCount = count - (histogram->GetNbinsX()*histogram->GetNbinsY()*histogram->GetNbinsZ() - TotalBins);
193  if(m_name.find("LessThan")!=std::string::npos) result->tags_["NBins_%"] = 100.*effectiveCount/TotalBins;
194  else result->tags_["NBins_%"] = 100.*count/TotalBins;
195  }
196 
197  result->tags_["NBins"] = count;
198  result->object_ = (boost::shared_ptr<TObject>)(TObject*)(resulthisto);
199  if (gthreshold > rthreshold) {
200  if ( count >= gthreshold ) {
201  result->status_ = dqm_core::Result::Green;
202  } else if ( count > rthreshold ) {
203  result->status_ = dqm_core::Result::Yellow;
204  } else {
205  result->status_ = dqm_core::Result::Red;
206  }
207  } else {
208  if ( count <= gthreshold ) {
209  result->status_ = dqm_core::Result::Green;
210  } else if ( count < rthreshold ) {
211  result->status_ = dqm_core::Result::Yellow;
212  } else {
213  result->status_ = dqm_core::Result::Red;
214  }
215  }
216 
217  return result;
218 
219 }
220 
221 bool
222 dqm_algorithms::BinThreshold::CompareBinThreshold(const std::string & type, double bincontent, double threshold) {
223 
224  if (type == "GreaterThan") return (bincontent > threshold);
225  if (type == "GreaterThanAbs") return (std::abs(bincontent) > threshold);
226  if (type == "GreaterThanNonZeroMedian") return (bincontent > threshold);
227  if (type == "LessThan") return (bincontent < threshold);
228  if (type == "LessThanAbs") return (std::abs(bincontent) < threshold);
229  if (type == "LessThanNonZeroMedian") return (bincontent < threshold);
230  if (type == "LessThanEqual") return (bincontent <= threshold);
231  if (type == "GreaterThanEqual") return (bincontent >= threshold);
232  if (type == "Equal") return (bincontent == threshold);
233  if (type == "NotEqual") return (bincontent != threshold);
234 
235  return 0;
236 }
237 
238 void dqm_algorithms::BinThreshold::parseVetoList(const std::string& input, std::vector<int>& rows, std::vector<int>& columns, std::vector<std::vector<int>>& bins) {
239 
240  std::string inputNoQuotes;
241  for (char c : input) if (c != '"') inputNoQuotes += c;
242 
243  std::stringstream ss(inputNoQuotes);
244  std::string token;
245 
246  while (std::getline(ss, token, ',')) {
247  std::stringstream pairStream(token);
248  std::string first, second;
249 
250  if (std::getline(pairStream, first, ':') && std::getline(pairStream, second, ':')) {
251  if (first == "*") {
252  rows.push_back(std::stoi(second));
253  } else if (second == "*") {
254  columns.push_back(std::stoi(first));
255  } else {
256  bins.push_back({std::stoi(first), std::stoi(second)});
257  }
258  }
259  }
260 }
261 
262 void
264 {
265 
266  out<<"Bins_"+m_name+"_Threshold: Checks for number of bins "+m_name+" threshold value\n"<<std::endl;
267 
268  out<<"Mandatory Parameter: BinThreshold: Look for bins "+m_name+" BinTreshold; Count number of bins satifying requirement \n"<<std::endl;
269 
270  out<<"Mandatory Green/Red Threshold: NBins: Number of bins satifying "+m_name+" BinThreshold constraint to give Green/Red result\n"<<std::endl;
271 
272  out<<"Optional Parameter: PublishBins: Save bins which are different from average in Result (set to 1)\n"<<std::endl;
273  out<<"Optional Parameter: MaxPublish: Max number of bins to save (default 20)"<<std::endl;
274  out<<"Optional Parameter: MinStat: Minimum histogram statistics needed to perform Algorithm"<<std::endl;
275  out<<"Optional Parameter: xmin: minimum x range"<<std::endl;
276  out<<"Optional Parameter: xmax: maximum x range"<<std::endl;
277  out<<"Optional Parameter: ymin: minimum y range"<<std::endl;
278  out<<"Optional Parameter: ymax: maximum y range\n"<<std::endl;
279  out<<"Optional Parameter: ignoreBins: list of bins to ignore in the threshold comparison (x,y) in a string, separated by comas and semicolons. Ex: 1:3,6:7. It also allows to pass rows and columns like *:4 and *:4 respectively \n"<<std::endl;
280 
281 }
282 
query_example.row
row
Definition: query_example.py:24
dqm_algorithms::tools::GetBinRange
std::vector< int > GetBinRange(const TH1 *histogram, const std::map< std::string, double > &params)
Definition: AlgorithmHelper.cxx:380
python.SystemOfUnits.second
int second
Definition: SystemOfUnits.py:120
ymin
double ymin
Definition: listroot.cxx:63
Undefined
@ Undefined
Definition: MaterialTypes.h:8
get_generator_info.result
result
Definition: get_generator_info.py:21
PowhegControl_ttHplus_NLO.ss
ss
Definition: PowhegControl_ttHplus_NLO.py:83
IsA
#define IsA
Declare the TObject style functions.
Definition: xAODTEventBranch.h:59
max
constexpr double max()
Definition: ap_fixedTest.cxx:33
min
constexpr double min()
Definition: ap_fixedTest.cxx:26
dqm_algorithms::BinThreshold::parseVetoList
void parseVetoList(const std::string &input, std::vector< int > &rows, std::vector< int > &columns, std::vector< std::vector< int >> &bins)
Definition: BinThreshold.cxx:238
python.AthDsoLogger.out
out
Definition: AthDsoLogger.py:71
DeMoUpdate.column
dictionary column
Definition: DeMoUpdate.py:1110
dqm_algorithms::BinThreshold::CompareBinThreshold
bool CompareBinThreshold(const std::string &objname, double bincontent, double threshold)
Definition: BinThreshold.cxx:222
InDet::median
float median(std::vector< float > &Vec)
Definition: BTagVrtSec.cxx:35
dqm_algorithms::tools::PublishBin
void PublishBin(const TH1 *histogram, int xbin, int ybin, double content, dqm_core::Result *result)
Definition: AlgorithmHelper.cxx:426
dqm_algorithms::BinThreshold::execute
dqm_core::Result * execute(const std::string &, const TObject &, const dqm_core::AlgorithmConfig &)
Definition: BinThreshold.cxx:51
m_name
std::string m_name
Definition: ColumnarPhysliteTest.cxx:53
python.CaloAddPedShiftConfig.type
type
Definition: CaloAddPedShiftConfig.py:42
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
grepfile.content
string content
Definition: grepfile.py:56
lumiFormat.i
int i
Definition: lumiFormat.py:85
xmin
double xmin
Definition: listroot.cxx:60
dqm_algorithms::BinThreshold::BinThreshold
BinThreshold(const std::string &name)
Definition: BinThreshold.cxx:36
Result
ICscStripFitter::Result Result
Definition: CalibCscStripFitter.cxx:13
python.LArMinBiasAlgConfig.int
int
Definition: LArMinBiasAlgConfig.py:59
plotBeamSpotVxVal.range
range
Definition: plotBeamSpotVxVal.py:195
python.handimod.Green
int Green
Definition: handimod.py:524
plotting.yearwise_luminosity_vs_mu.bins
bins
Definition: yearwise_luminosity_vs_mu.py:30
beamspotnt.rows
list rows
Definition: bin/beamspotnt.py:1112
python.handimod.Red
Red
Definition: handimod.py:551
dqm_algorithms::BinThreshold::printDescription
void printDescription(std::ostream &out)
Definition: BinThreshold.cxx:263
name
std::string name
Definition: Control/AthContainers/Root/debug.cxx:240
threshold
Definition: chainparser.cxx:74
dqm_algorithms::BinThreshold::clone
BinThreshold * clone()
Definition: BinThreshold.cxx:43
python.TriggerAPI.TriggerAPISession.columns
columns
Definition: TriggerAPISession.py:432
Equal
bool Equal(const TrigMonTE &lhs, const TrigMonTE &rhs)
Definition: TrigMonTE.cxx:152
DeMoScan.first
bool first
Definition: DeMoScan.py:536
dqm_algorithms::BinThreshold
Definition: BinThreshold.h:19
xmax
double xmax
Definition: listroot.cxx:61
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
xAOD::bool
setBGCode setTAP setLVL2ErrorBits bool
Definition: TrigDecision_v1.cxx:60
python.compressB64.c
def c
Definition: compressB64.py:93
histogram
std::string histogram
Definition: chains.cxx:52
ymax
double ymax
Definition: listroot.cxx:64
BinThreshold.h