ATLAS Offline Software
BinsDiffFromPreviousLBs.cxx
Go to the documentation of this file.
1 /*
2  Copyright (C) 2002-2024 CERN for the benefit of the ATLAS collaboration
3 */
4 
9 #include <dqm_core/AlgorithmConfig.h>
12 #include <dqm_core/AlgorithmManager.h>
13 
14 #include <TH1.h>
15 #include <TF1.h>
16 #include <TClass.h>
17 #include <cmath>
18 #include <algorithm>
19 
20 #include <iostream>
21 
22 
24 
26 {
27  dqm_core::AlgorithmManager::instance().registerAlgorithm("BinsDiffFromPreviousLBs", this);
28 }
29 
31 {
32 }
33 
36 {
37 
38  return new BinsDiffFromPreviousLBs();
39 }
40 
42 {
43  if (lbs.empty()) return false;
44  if (lbs.size() == 1) return true;
45  const bool monotonic = std::is_sorted(lbs.begin(), lbs.end());
46  const bool withinRange = (lbs.back() - lbs.front() + 1) == std::ssize(lbs);
47  return monotonic and withinRange;
48 }
49 
50 
53  const TObject& object,
54  const dqm_core::AlgorithmConfig& config )
55 {
56  const TH1* histogram = nullptr;
57 
58  if( object.IsA()->InheritsFrom( "TH1" ) ) {
59  histogram = static_cast<const TH1*>(&object);
60  if (histogram->GetDimension() > 2 ){
61  throw dqm_core::BadConfig( ERS_HERE, name, "dimension > 2 " );
62  }
63  } else {
64  throw dqm_core::BadConfig( ERS_HERE, name, "does not inherit from TH1" );
65  }
66 
67  const double minstat = dqm_algorithms::tools::GetFirstFromMap( "MinStat", config.getParameters(), -1);
68  const double ignoreval = dqm_algorithms::tools::GetFirstFromMap( "ignoreval", config.getParameters(), -99999);
69  bool greaterthan = (bool) dqm_algorithms::tools::GetFirstFromMap( "GreaterThan", config.getParameters(), 0);
70  bool lessthan = (bool) dqm_algorithms::tools::GetFirstFromMap( "LessThan", config.getParameters(), 0);
71  const bool publish = (bool) dqm_algorithms::tools::GetFirstFromMap( "PublishBins", config.getParameters(), 0);
72  const int maxpublish = (int) dqm_algorithms::tools::GetFirstFromMap( "MaxPublish", config.getParameters(), 20);
73  const double maxdiffabs = dqm_algorithms::tools::GetFirstFromMap( "MaxDiffAbs", config.getParameters(), -1);
74 
75  if (greaterthan && lessthan) {
76  ERS_INFO("Both GreaterThan and LessThan parameters set: Will check for for both");
77  greaterthan = false;
78  lessthan = false;
79  }
80 
81  if ( histogram->GetEntries() < minstat ) {
83  result->tags_["InsufficientEntries"] = histogram->GetEntries();
84  return result;
85  }
86 
87  double bin_threshold;
88  double nsigmas;
89  double gthreshold;
90  double rthreshold;
91  try {
92  nsigmas = dqm_algorithms::tools::GetFirstFromMap( "NSigma", config.getParameters() );
93  bin_threshold = dqm_algorithms::tools::GetFirstFromMap( "BinThreshold", config.getParameters() );
94  rthreshold = dqm_algorithms::tools::GetFromMap( "NBins", config.getRedThresholds() );
95  gthreshold = dqm_algorithms::tools::GetFromMap( "NBins", config.getGreenThresholds() );
96  }
97  catch( dqm_core::Exception & ex ) {
98  throw dqm_core::BadConfig( ERS_HERE, name, ex.what(), ex );
99  }
100 
101  TH1* resulthisto = nullptr;
102  if (histogram->InheritsFrom("TH2")) {
103  resulthisto=(TH1*)(histogram->Clone());
104  } else if (histogram->InheritsFrom("TH1")) {
105  resulthisto=(TH1*)(histogram->Clone());
106  } else {
107  throw dqm_core::BadConfig( ERS_HERE, name, "does not inherit from TH1" );
108  }
109 
110  resulthisto->Reset();
111 
112  std::vector<int> range=dqm_algorithms::tools::GetBinRange(histogram, config.getParameters());
113 
114  std::vector<int> LBs;
116 
117  for ( int k = range[0]; k <= range[1]; ++k ) {
118  for ( int l = range[2]; l <= range[3]; ++l ) {
119  double inputcont = histogram->GetBinContent(k,l);
120  double inputerr = histogram->GetBinError(k,l);
121  double diff=inputcont - bin_threshold;
122  if (inputcont == ignoreval) continue;
123  if (inputerr != 0){
124  double sigma=diff/inputerr;
125  if (greaterthan && diff < 0. ) continue;
126  if (lessthan && diff > 0. ) continue;
127  if ( (std::abs(sigma) > nsigmas) && (std::abs(diff) > maxdiffabs) ) {
128  resulthisto->SetBinContent(k,l,inputcont);
129  LBs.push_back(k);
130  if (publish && (int)LBs.size() < maxpublish){
132  }
133  }
134  }
135 
136  }
137  }
138 
139  int count = LBs.size();
140 
141  result->tags_["NBins"] = count;
142  result->object_ = (boost::shared_ptr<TObject>)(TObject*)(resulthisto);
143 
144  ERS_DEBUG(1,"Number of bins " << nsigmas << " Sigma away from average of "<< bin_threshold << " is " << count);
145  ERS_DEBUG(1,"Green threshold: "<< gthreshold << " bin(s); Red threshold : " << rthreshold << " bin(s) ");
146 
147  if ( count <= gthreshold ) {
148  result->status_ = dqm_core::Result::Green;
149  } else if ( count < rthreshold ) {
150  result->status_ = dqm_core::Result::Yellow;
151  } else {
152  result->status_ = dqm_core::Result::Red;
153  }
154  return result;
155 
156 }
157 void
159 {
160 
161  out<<"BinsDiffFromPreviousLBs: Checks for number of bins (subsequent LBs) NSigma within threshold \n"<<std::endl;
162 
163  out<<"Mandatory Parameter: BinThreshold: Look for bins NSigna away from threshold \n"<<std::endl;
164  out<<"Mandatory Parameter: NSigma: Number of sigma each bin must be within average bin value\n"<<std::endl;
165  out<<"Mandatory Green/Red Threshold: NBins: number of bins N sigma away from average to give Green/Red result\n"<<std::endl;
166 
167  out<<"Optional Parameter: MinStat: Minimum histogram statistics needed to perform Algorithm"<<std::endl;
168  out<<"Optional Parameter: doCheckOnSingleLB: check done not necessarly on subsequent bins (set to 0)"<<std::endl;
169  out<<"Optional Parameter: ignoreval: valued to be ignored for calculating average"<<std::endl;
170  out<<"Optional Parameter: GreaterThan: check only for bins which are GreaterThan average (set to 1)"<<std::endl;
171  out<<"Optional Parameter: LessThan: check only for bins which are LessThan average (set to 1)"<<std::endl;
172  out<<"Optional Parameter: PublishBins: Save bins which are different from average in Result (set to 1)"<<std::endl;
173  out<<"Optional Parameter: MaxPublish: Max number of bins to save (default 20)"<<std::endl;
174  out<<"Optional Parameter: MaxDiffAbs: test fails if NBins more than NSigma away and NBins more than MaxDiffAbs (absolut difference) away from average"<<std::endl;
175 
176 }
177 
dqm_algorithms::tools::GetBinRange
std::vector< int > GetBinRange(const TH1 *histogram, const std::map< std::string, double > &params)
Definition: AlgorithmHelper.cxx:380
beamspotman.lbs
lbs
Definition: beamspotman.py:1154
Undefined
@ Undefined
Definition: MaterialTypes.h:8
pdg_comparison.sigma
sigma
Definition: pdg_comparison.py:324
dqm_algorithms::BinsDiffFromPreviousLBs
Definition: BinsDiffFromPreviousLBs.h:21
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
python.AthDsoLogger.out
out
Definition: AthDsoLogger.py:71
dqm_algorithms::BinsDiffFromPreviousLBs::printDescription
void printDescription(std::ostream &out)
Definition: BinsDiffFromPreviousLBs.cxx:158
mc.diff
diff
Definition: mc.SFGenPy8_MuMu_DD.py:14
UploadAMITag.l
list l
Definition: UploadAMITag.larcaf.py:158
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::BinsDiffFromPreviousLBs::areConsecutive
bool areConsecutive(const std::vector< int > &lbVec)
Definition: BinsDiffFromPreviousLBs.cxx:41
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
Result
ICscStripFitter::Result Result
Definition: CalibCscStripFitter.cxx:13
dqm_algorithms::BinsDiffFromPreviousLBs::~BinsDiffFromPreviousLBs
~BinsDiffFromPreviousLBs()
Definition: BinsDiffFromPreviousLBs.cxx:30
plotBeamSpotVxVal.range
range
Definition: plotBeamSpotVxVal.py:195
python.handimod.Green
int Green
Definition: handimod.py:524
python.handimod.Red
Red
Definition: handimod.py:551
dqm_algorithms::BinsDiffFromPreviousLBs::BinsDiffFromPreviousLBs
BinsDiffFromPreviousLBs()
Definition: BinsDiffFromPreviousLBs.cxx:25
name
std::string name
Definition: Control/AthContainers/Root/debug.cxx:228
BinsDiffFromPreviousLBs.h
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::BinsDiffFromPreviousLBs::execute
dqm_core::Result * execute(const std::string &, const TObject &, const dqm_core::AlgorithmConfig &)
Definition: BinsDiffFromPreviousLBs.cxx:52
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
dqm_algorithms::BinsDiffFromPreviousLBs::clone
BinsDiffFromPreviousLBs * clone()
Definition: BinsDiffFromPreviousLBs.cxx:35
histogram
std::string histogram
Definition: chains.cxx:52
fitman.k
k
Definition: fitman.py:528