ATLAS Offline Software
MDTOverview.cxx
Go to the documentation of this file.
1 /*
2  Copyright (C) 2002-2022 CERN for the benefit of the ATLAS collaboration
3 */
4 
6 
7 #include <iostream>
8 #include <utility>
9 #include <vector>
10 #include <list>
11 #include <string>
12 
13 #include "TH2.h"
14 #include "TAxis.h"
15 #include "TClass.h"
16 
17 
18 #include "dqm_core/exceptions.h"
19 #include "dqm_core/AlgorithmConfig.h"
20 #include "dqm_core/AlgorithmManager.h"
21 #include "dqm_core/Result.h"
23 #include "ers/ers.h"
24 #include "boost/format.hpp"
25 
26 static dqm_algorithms::MDTOverview MDTOverview_Global( "MDTOverview_Global" );
27 static dqm_algorithms::MDTOverview MDTOverview_Station( "MDTOverview_Station" );
28 
29 
30 namespace dqm_algorithms {
31 
32 // *********************************************************************
33 // Public Methods
34 // *********************************************************************
35 
36 MDTOverview::MDTOverview(const std::string & name)
37  : m_name ( name )
38 {
39  dqm_core::AlgorithmManager::instance().registerAlgorithm( name, this );
40 }
41 
42 
44 {
45 }
46 
47 
50 {
51  return new MDTOverview(m_name);
52 }
53 
54 
56 MDTOverview::execute( const std::string& name, const TObject& object, const dqm_core::AlgorithmConfig& config)
57 {
58  const TH2 * hist;
59  const TH2 * ref;
60 
61  if( object.IsA()->InheritsFrom( "TH2" ) ) {
62  hist = static_cast<const TH2*>(&object);
63  if (hist->GetDimension() != 2 ){
64  throw dqm_core::BadConfig( ERS_HERE, name, "dimension != 2 " );
65  }
66  } else {
67  throw dqm_core::BadConfig( ERS_HERE, name, "does not inherit from TH2" );
68  }
69 
70  //Get Parameters and Thresholds
71 
72  double greenTh;
73  double redTh;
74  double minstat;
75  double thresh;
76  double using_ref_for_flag;
77  std::string thresholdname;
78  if (m_name == "MDTOverview_Global") {
79  thresholdname="Percentage";
80  } else if (m_name == "MDTOverview_Station") {
81  thresholdname="Limits";
82  } else {
83  throw dqm_core::BadConfig( ERS_HERE, "None", m_name );
84  }
85 
86  try {
87  minstat = dqm_algorithms::tools::GetFirstFromMap("MinStat", config.getParameters(), 5000);
88  thresh = dqm_algorithms::tools::GetFirstFromMap("thresh", config.getParameters(), 10);
89  using_ref_for_flag = dqm_algorithms::tools::GetFirstFromMap("using_ref_for_flag", config.getParameters(), 0);
90  redTh = dqm_algorithms::tools::GetFromMap( thresholdname, config.getRedThresholds());
91  greenTh = dqm_algorithms::tools::GetFromMap( thresholdname, config.getGreenThresholds() );
92  }
93  catch ( dqm_core::Exception & ex ) {
94  throw dqm_core::BadConfig( ERS_HERE, name, ex.what(), ex );
95  }
96 
97  //Get Reference Histo
98  try {
99  ref = static_cast<const TH2 *>( config.getReference() );
100  }
101  catch ( dqm_core::Exception & ex ) {
102  throw dqm_core::BadRefHist(ERS_HERE,name," Could not retreive reference");
103  }
104  if (hist->GetDimension() != ref->GetDimension() ) {
105  throw dqm_core::BadRefHist( ERS_HERE, name, "Reference VS histo: Different dimension!" );
106  }
107 
108 
109  //Check of statistics
110  if (hist->GetEntries() < minstat ) {
111  ERS_INFO("Histogram does not satisfy MinStat requirement " <<hist->GetName());
113  result->tags_["InsufficientEntries"] = hist->GetEntries();
114  return result;
115  }
116  ERS_DEBUG(1,"Statistics: "<< hist->GetEntries()<< " entries ");
117 
118 
119  //Algo
120  int binX = hist->GetNbinsX();
121  int binY = hist->GetNbinsY();
122  double nML=0;
123 
124  std::vector<int> hist_buffer;
125  std::vector<int> ref_buffer;
126 
127  for(int x_index=1;x_index<=binX;x_index++){
128  for(int y_index=1;y_index<=binY;y_index++){
129  if( hist->GetBinContent(x_index,y_index) != 0 ) hist_buffer.push_back((int)hist->GetBinContent(x_index,y_index));
130  if( ref->GetBinContent(x_index,y_index) != 0 ) ref_buffer.push_back((int)ref->GetBinContent(x_index,y_index));
131  };
132  };
133 
134  const char *htitle = hist->GetTitle();
135  ERS_DEBUG(1,"Histogram title: "<< htitle);
136 
137  if (m_name == "MDTOverview_Global") {
138  //GLOBAL htitle="Global HitsPerML Endcap"
139  //if (strstr(htitle, "Global")) {
140  if (strstr(htitle, "BA")) {
141  nML = 594; //4 BOG0 included
142  } else if (strstr(htitle, "BC")) {
143  nML = 590;
144  } else if (strstr(htitle, "EA")) {
145  nML = 502; // 16 BIS7 + 8 BIS8 +16 BEE + 10 EEL included
146  } else if (strstr(htitle, "EC")) {
147  nML = 502; // BIS7/8 + BEE + EEL included
148  }
149 
150  } else if (m_name == "MDTOverview_Station") {
151  // Barrel htitle="HitsPerML_BX_Station"
152  if (strstr(htitle, "B")) {
153  if (strstr(htitle,"Inner")) {
154  ERS_DEBUG(1,"Station Inner ");
155  nML = 212;
156  } else if (strstr(htitle,"Middle")) {
157  ERS_DEBUG(1,"Station Middle ");
158  nML = 178;
159  } else if (strstr(htitle,"Outer")) {
160  ERS_DEBUG(1,"Station Outer ");
161  if (strstr(htitle, "BA")) {
162  nML = 204; // BOG0 included
163  } else if (strstr(htitle, "BC")) {
164  nML = 200;
165  }
166  }
167  // Endcap htitle="HitsPerML_EX_Station"
168  } else if (strstr(htitle, "E")) {
169 
170  if (strstr(htitle,"Inner")) {
171  ERS_DEBUG(1,"Station Inner ");
172  nML = 124;// 16 BIS7 + 8 BIS8 included
173  } else if (strstr(htitle,"Middle")) {
174  ERS_DEBUG(1,"Station Middle ");
175  nML = 160;
176  } else if (strstr(htitle,"Outer")) {
177  ERS_DEBUG(1,"Station Outer ");
178  nML = 192;
179  } else if (strstr(htitle,"extra")) {
180  ERS_DEBUG(1,"Extra chambers (BEE, EEL, BIS7, BIS8)");
181  nML = 26;// 16 BEE + 10 EEL included
182  }
183  }
184  }
185 
186  auto median=[](std::vector<int> & v)->int {
187  const auto midPoint = v.begin()+v.size()/2;
188  std::nth_element(v.begin(), midPoint, v.end());
189  return v[v.size()/2];
190  };
191 
192 
193 
194  int mediana_hist = median(hist_buffer);
195  int mediana_ref = median(ref_buffer);
196  const int size_hist = hist_buffer.size();
197 
198  int count=0;
199  int count_non_0=0;
200  int sec_count=0;
201  int ML_count=0;
202  int sector=1;
203  std::pair<int,int> empty_bin;
204  std::vector< std::pair<int,int> > new_empty_bins;
205 
206  for(int y_index=1;y_index<=binY;y_index++){
207  if(ML_count==2){
208  ML_count=0;
209  sec_count=0;
210  sector++;
211  };
212  ML_count++;
213 
214  for(int x_index=1;x_index<=binX;x_index++){
215  if(using_ref_for_flag) {
216  if( hist->GetBinContent(x_index,y_index)< mediana_hist*thresh/100 && ref->GetBinContent(x_index,y_index) >= mediana_ref*thresh/100){
217  sec_count++;
218  count++;
219  if( hist->GetBinContent(x_index,y_index)>0) count_non_0++;
220  }
221  } else {
222  if( hist->GetBinContent(x_index,y_index)< mediana_hist*thresh/100){
223  sec_count++;
224  count++;
225  };
226  }
227  };
228 
229  if(ML_count==2 && sec_count>0){
230  empty_bin.first=sector;
231  empty_bin.second=sec_count;
232  new_empty_bins.push_back(empty_bin);
233  }
234  };
235 
236  if(nML<=0){
238  result->tags_["Configuration_ERROR_Setted_N_tot_ML"] = nML;
240  return result;
241  };
242 
243  //Result
245  result->tags_["00-%_ML_ON"] = 100*((double)(size_hist-count_non_0)/nML);
246 
247  if(thresholdname == "Limits" ) {
248 
249  if(count>0) {
250  std::string tag;
251  std::string tag1="Sector";
252  std::string tag2="_number_of_ML_off";
253  result->tags_["01-NEW_ML_OFF"] = count;
254  for(int j=0; j<(int)new_empty_bins.size(); j++){
255  boost::format fmt("%02d");
256  fmt % new_empty_bins[j].first;
257  tag="01-"+tag1+fmt.str()+tag2;
258  result->tags_[tag] = new_empty_bins[j].second;
259  };
260  }
261  if (nML-count-size_hist > 0) result->tags_["02-KNOWN_ML_OFF"] = nML-count-size_hist;
262 
263  // Flag assignment
264  if (count > redTh) {
265  result->status_ = dqm_core::Result::Red;
266  ERS_DEBUG(1,"Red");
267  } else if ( count <= redTh && count > greenTh ) {
268  result->status_ = dqm_core::Result::Yellow;
269  ERS_DEBUG(1,"Yellow");
270  } else if (count <= greenTh) {
271  result->status_ = dqm_core::Result::Green;
272  ERS_DEBUG(1,"Green");
273  } else {
275  ERS_DEBUG(1,"Undefined");
276  }
277 
278  } else if (thresholdname == "Percentage") {
279  // Flag assignment
280  if (100*((double)size_hist/nML)<redTh) {
281  result->status_ = dqm_core::Result::Red;
282  ERS_DEBUG(1,"Red");
283  } else if ( 100*((double)size_hist/nML)>=redTh && 100*((double)size_hist/nML)<greenTh ) {
284  result->status_ = dqm_core::Result::Yellow;
285  ERS_DEBUG(1,"Yellow");
286  } else if (100*((double)size_hist/nML)>=greenTh) {
287  result->status_ = dqm_core::Result::Green;
288  ERS_DEBUG(1,"Green");
289  } else {
291  ERS_DEBUG(1,"Undefined");
292  }
293  }
294  return result;
295 }
296 
297 
298 void
300 {
301  std::string message;
302  message += "\n";
303  message += "Algorithm: \"" + m_name + "\"\n";
304  message += "Description: check if there are any bins with less then a thresh% of the entries of the median bin\n";
305  message += " and perform a comparison with reference.\n";
306  message += "MDTOverview_Global: Mandatory Green/Red Threshold: Percentage: of ML OFF\n";
307  message += "MDTOverview_Station: Mandatory Green/Red Threshold: Limits: number of ML OFF\n";
308  message += "Optional Parameters: thresh = min % of hits from the median chamber accepted\n";
309  message += " MinStat = Minimum histogram statistics needed to perform Algorithm\n";
310  message += " using_ref_for_flag = if enabled (1) it compares the OFF MLs with reference, if not enabled (0)\n";
311  message += " the flag is setted checking the absolute % of ML on in the detector\n";
312  message += "\n";
313 
314  out << message;
315 }
316 
317 } // namespace dqm_algorithms
dqm_algorithms::MDTOverview
Definition: MDTOverview.h:20
dqm_algorithms::MDTOverview::MDTOverview
MDTOverview(const std::string &name)
Definition: MDTOverview.cxx:36
Undefined
@ Undefined
Definition: MaterialTypes.h:8
get_generator_info.result
result
Definition: get_generator_info.py:21
vtune_athena.format
format
Definition: vtune_athena.py:14
IsA
#define IsA
Declare the TObject style functions.
Definition: xAODTEventBranch.h:59
CaloCellPos2Ntuple.int
int
Definition: CaloCellPos2Ntuple.py:24
physval_make_web_display.thresh
thresh
Definition: physval_make_web_display.py:35
plotmaker.hist
hist
Definition: plotmaker.py:148
python.FakeAthena.Algorithm
def Algorithm(name)
Definition: FakeAthena.py:41
python.AthDsoLogger.out
out
Definition: AthDsoLogger.py:71
fmt
const char *const fmt
Definition: TripleGaussCollFit.cxx:84
InDet::median
float median(std::vector< float > &Vec)
Definition: BTagVrtSec.cxx:35
ReweightUtils.message
message
Definition: ReweightUtils.py:15
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
Trk::binY
@ binY
Definition: BinningType.h:48
MDTOverview.h
dqm_algorithms::MDTOverview::m_name
std::string m_name
Definition: MDTOverview.h:33
Result
ICscStripFitter::Result Result
Definition: CalibCscStripFitter.cxx:13
plotBeamSpotCompare.tag1
string tag1
Definition: plotBeamSpotCompare.py:75
dqm_algorithms::MDTOverview::~MDTOverview
virtual ~MDTOverview()
Definition: MDTOverview.cxx:43
plotBeamSpotCompare.tag2
string tag2
Definition: plotBeamSpotCompare.py:76
python.handimod.Green
int Green
Definition: handimod.py:524
dqm_algorithms::MDTOverview::printDescription
virtual void printDescription(std::ostream &out)
Definition: MDTOverview.cxx:299
xAOD::double
double
Definition: CompositeParticle_v1.cxx:159
Trk::binX
@ binX
Definition: BinningType.h:47
TH2
Definition: rootspy.cxx:373
python.handimod.Red
Red
Definition: handimod.py:551
name
std::string name
Definition: Control/AthContainers/Root/debug.cxx:195
dqm_algorithms::MDTOverview::clone
virtual dqm_core::Algorithm * clone()
Definition: MDTOverview.cxx:49
python.PyAthena.v
v
Definition: PyAthena.py:157
dqm_algorithms::MDTOverview::execute
virtual dqm_core::Result * execute(const std::string &name, const TObject &object, const dqm_core::AlgorithmConfig &config)
Definition: MDTOverview.cxx:56
fmt
dqm_algorithms
Definition: AddReference.h:17
ref
const boost::regex ref(r_ef)
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
CaloCondBlobAlgs_fillNoiseFromASCII.tag
string tag
Definition: CaloCondBlobAlgs_fillNoiseFromASCII.py:24
dqm_algorithms::tools::GetFirstFromMap
double GetFirstFromMap(const std::string &paramName, const std::map< std::string, double > &params)
Definition: AlgorithmHelper.cxx:339