ATLAS Offline Software
BinPrint.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 <cmath>
8 #include <iostream>
9 #include <map>
10 
11 #include <TH1.h>
12 #include <TClass.h>
13 
14 #include "dqm_core/exceptions.h"
16 #include "dqm_core/AlgorithmManager.h"
17 #include "dqm_core/AlgorithmConfig.h"
18 #include "dqm_core/Result.h"
19 
20 static dqm_algorithms::BinPrint staticInstance;
21 
22 namespace dqm_algorithms {
23 
24  // *********************************************************************
25  // Public Methods
26  // *********************************************************************
27 
28  void
30  printDescription(std::ostream& out)
31  {
32  std::string message;
33  message += "\n";
34  message += "Algorithm: \"" + m_name + "\"\n";
35  message += "Description: Extracts bin contents from a TProfile, TH1, TH2 object without performing\n";
36  message += " any assessment; the status is always \"Undefined\"\n";
37  message += "Optional Parameter: UnMask_All Default = -1\n";
38  message += " <0: Only unmask bins when Mask_## = 0\n";
39  message += " >=0: Unmask all bins and only mask bins when Mask_## = 1\n";
40  message += " # >= 0: Maximum number of bins printed is #\n";
41  message += " This setting is combined so that large bin lists do not occur accidentaly.\n";
42  message += "Optional Parameter: UseValue: Default = 0\n";
43  message += " 0: Print all unmasked bins\n";
44  message += " 1: Print bin contents >= Value\n";
45  message += " >1: Print bin contents > Value\n";
46  message += " -1: Print bin contents <= Value\n";
47  message += " <-1: Print bin contents < Value\n";
48  message += "Optional Parameter: Value:\n";
49  message += "Optional Parameter: UseMaskValue: Default = 0\n";
50  message += " 0: Mask no bins\n";
51  message += " 1: Change Mask for all >= MaskValue\n";
52  message += " >1: Change Mask for all > Mask Value\n";
53  message += " -1: Change Mask for all <= MaskValue\n";
54  message += " <-1: Change Mask for all < Mask Value\n";
55  message += " NOTE: The same boundaries apply to the mask\n";
56  message += "Optional Parameter: MaskValue:\n";
57  message += " Example of Printing values from 0. <= x <= 1.\n";
58  message += " UseValue = 1\n";
59  message += " Value = 0. ## x >= 0.\n";
60  message += " UseMaskValue = 2\n";
61  message += " MaskValue = 1. ## x <= 1.\n";
62  message += " Example of Printing values outside of the range above\n";
63  message += " UseValue = 2\n";
64  message += " Value = 1.\n";
65  message += " ## By Value Only: x > 1.0\n";
66  message += " UseMaskValue = -2\n";
67  message += " MaskValue = 0.\n";
68  message += " ## Including Mask: x < 0.0\n";
69  message += "Optional Parameter: TypeValue: Default = 0\n";
70  message += " 0: Value is compared to total entries in bin\n";
71  message += " 1: Value is compared to fraction of entries in histogram appearing in bin\n";
72  message += " This also determines how the bin contents will be listed if published.\n";
73  message += "1D Histogram Configuration:\n";
74  message += "Optional Parameters: Mask_#\n";
75  message += " 0: UnMask bin\n";
76  message += " 1: Mask bin\n";
77  message += " Example of Printing all but the first bin\n";
78  message += " ***In algorithm declaration\n";
79  message += " UnMask_All = 3\n";
80  message += " Mask_1 = 1\n";
81  message += "2D Histogram Configuration:\n";
82  message += "Optional Parameters: Mask_#_#\n";
83  message += " 0: UnMask bin\n";
84  message += " 1: Mask bin\n";
85  message += " Example of masking the X = 1, Y = 2 bin:\n";
86  message += " ***In algorithm declaration\n";
87  message += " UnMask_All = 3\n";
88  message += " Mask_1_2 = 1\n";
89  message += "\n";
90  message += "Optional Parameters: PrintError\n";
91  message += " 1: Print Corresponding Bin Error\n";
92 
93  out << message;
94  }
95 
97  BinPrint()
98  : m_name("BinPrint")
99  , m_NbinsX(-1)
100  , m_NbinsY(-1)
101  , m_UnMask_All(-1)
102  , m_UseValue(0)
103  , m_TypeValue(0)
104  , m_Value(0.)
105  , m_UseMaskValue(0)
106  , m_MaskValue(0.)
107 
108  {
109  dqm_core::AlgorithmManager::instance().registerAlgorithm( m_name, this );
110  }
111 
112 
114  ~BinPrint()
115  {
116  }
117 
118 
121  clone()
122  {
123  return new BinPrint(*this);
124  }
125 
126 
129  execute( const std::string& name, const TObject& data, const dqm_core::AlgorithmConfig& config)
130  {
131  //No status flags are set
134  //Histogram dimension default = no dimensions
135  m_NbinsX = -1;
136  m_NbinsY = -1;
137 
138  if (!data.IsA()->InheritsFrom("TH1")) {
139  throw dqm_core::BadConfig(ERS_HERE, name, "does not inherit from TH1");
140  }
141  const TH1* h = static_cast<const TH1*>(&data); // h cannot be null
142  if (h->GetDimension() > 2) {
143  throw dqm_core::BadConfig(ERS_HERE, name, "dimension > 2 ");
144  }
145 
146  //Get optional parameters
147  int Npublished = 0;
148  m_UseValue = UseValue_GetFromMap(config.getParameters());
149  m_TypeValue = TypeValue_GetFromMap(config.getParameters());
150  m_Value = Value_GetFromMap(config.getParameters());
151  m_UseMaskValue = UseMaskValue_GetFromMap(config.getParameters());
152  m_MaskValue = MaskValue_GetFromMap(config.getParameters());
153  const int Error = (int) dqm_algorithms::tools::GetFirstFromMap( "PrintError", config.getParameters(), 0);
154  //out<<Error<<"Error"<<std::endl;
155  //**********
156  // 1D Histogram case
157  //**********
158  if (h->GetDimension() == 1) {
159  m_NbinsX = h->GetNbinsX();
160 
161  //Get bin mask
162  std::vector<bool> Mask;
163  Mask = Mask1D_GetFromMap(config.getParameters());
164 
165  //Rescale m_Value if fractional definition is used
166  double h_entries = h->GetEntries();
167  if ( m_TypeValue > 0.5 ) m_Value = m_Value * h_entries;
168 
169  //Check all bins
170  for (int bin = 0; bin < m_NbinsX; bin++) {
171  double bincon = h->GetBinContent(bin + 1);
172  double ebincon = h->GetBinError(bin + 1);
173  //out<<"here"<<Mask[bin]<<std::endl;
174  //if( !Mask[bin] ) {
175  if( Mask[bin] ) {
176  bool do_printbin = false;
177  if(m_UseValue == 0) do_printbin = true;
178  if( (m_UseValue == 1 && bincon >= m_Value) ||
179  (m_UseValue > 1 && bincon > m_Value) ||
180  (m_UseValue == -1 && bincon <= m_Value) ||
181  (m_UseValue < -1 && bincon < m_Value)
182  ) do_printbin = true;
183  if( (m_UseMaskValue == 1 && bincon >= m_MaskValue) ||
184  (m_UseMaskValue > 1 && bincon > m_MaskValue) ||
185  (m_UseMaskValue == -1 && bincon <= m_MaskValue) ||
186  (m_UseMaskValue < -1 && bincon < m_MaskValue)
187  ) do_printbin = ! do_printbin;
188  if(do_printbin) {
189  //out<<"bin with name " << bincon<<std::endl;
190  Npublished++;
191  if ( m_UnMask_All < 0 ||
192  Npublished <= m_UnMask_All ) {
193  double binvalX = h->GetXaxis()->GetBinCenter(bin + 1);
194  std::string binname = Form("%s_Bin(%u | %.*e)", name.c_str(), bin, 2, binvalX);
195  std::string sigbinname = Form("%s_EBin(%u | %.*e)", name.c_str(), bin, 2, binvalX);
196  //ERS_DEBUG(1, "bin with name " << binname <<" and value "<< bincon);
197  //out<<"bin with name " << binname <<" and value "<< bincon<<std::endl;
198  //out<<"bin with name " << sigbinname <<" and value "<< ebincon<<std::endl;
199  //From() ~ printf() (s ~ string, u ~ unsigned int, .*e ~ scientific)
200  if ( m_TypeValue > 0.5 ) {
201  result->tags_[binname.c_str()] = (bincon / h_entries);
202  if (Error ==1 ) result->tags_[sigbinname.c_str()] = ebincon ;
203  } else {
204  result->tags_[binname.c_str()] = bincon;
205  if (Error ==1 ) result->tags_[sigbinname.c_str()] = ebincon ;
206  }
207  }
208  }
209  }
210  }
211  }
212 
213  //**********
214  // 2D Histogram case
215  //**********
216  if (h->GetDimension() == 2) {
217  m_NbinsX = h->GetNbinsX();
218  m_NbinsY = h->GetNbinsY();
219 
220  //Get bin mask
221  std::vector< std::vector<bool> > Mask;
222  Mask = Mask2D_GetFromMap(config.getParameters());
223 
224  //Rescale m_Value if fractional definition is used
225  double h_entries = h->GetEntries();
226  if ( m_TypeValue > 0.5 ) m_Value = m_Value * h_entries;
227 
228  //Check all bins
229  for (int binX = 0; binX < m_NbinsX; binX++) {
230  for (int binY = 0; binY < m_NbinsY; binY++) {
231  double bincon = h->GetBinContent(binX + 1, binY + 1);
232  double ebincon = h->GetBinError(binX + 1, binY + 1);
233  if( !Mask[binX][binY] ) {
234  bool do_printbin = false;
235  if(m_UseValue == 0) do_printbin = true;
236  if( (m_UseValue == 1 && bincon >= m_Value) ||
237  (m_UseValue > 1 && bincon > m_Value) ||
238  (m_UseValue == -1 && bincon <= m_Value) ||
239  (m_UseValue < -1 && bincon < m_Value)
240  ) do_printbin = true;
241  if( (m_UseMaskValue == 1 && bincon >= m_MaskValue) ||
242  (m_UseMaskValue > 1 && bincon > m_MaskValue) ||
243  (m_UseMaskValue == -1 && bincon <= m_MaskValue) ||
244  (m_UseMaskValue < -1 && bincon < m_MaskValue)
245  ) do_printbin = ! do_printbin;
246  if(do_printbin) {
247  Npublished++;
248  if ( m_UnMask_All < 0 ||
249  Npublished <= m_UnMask_All ) {
250  double binvalX = h->GetXaxis()->GetBinCenter(binX + 1);
251  double binvalY = h->GetYaxis()->GetBinCenter(binY + 1);
252  std::string binname = Form("%s_Bin((%u,%u) | %.*e,%.*e)", name.c_str(), binX, binY, 2, binvalX, 2, binvalY);
253  std::string sigbinname = Form("%s_EBin((%u,%u) | %.*e,%.*e)", name.c_str(), binX, binY, 2, binvalX, 2, binvalY);
254  if ( m_TypeValue > 0.5 ) {
255  result->tags_[binname.c_str()] = (bincon / h_entries);
256  result->tags_[sigbinname.c_str()] = ebincon;
257  } else {
258  }
259  result->tags_[binname.c_str()] = bincon;
260  result->tags_[sigbinname.c_str()] = ebincon;
261  }
262  }
263  }
264  }
265  }
266  }
267 
268  //Append statement if all warnings or errors were not printed
269  if ( m_UnMask_All >= 0 &&
270  Npublished > m_UnMask_All ) {
271  std::string pub_excess = Form("%s_PROBLEM_Publishable_UnPrinted",name.c_str());
272  result->tags_[pub_excess.c_str()] = Npublished - m_UnMask_All;
273  }
274 
275  return result;
276  }
277 
278 
279  // *********************************************************************
280  // Protected Methods
281  // *********************************************************************
282 
283  int BinPrint::UseValue_GetFromMap(const std::map<std::string, double> & params)
284  {
285  std::map<std::string, double>::const_iterator it = params.find("UseValue");
286  if ( it != params.end() ){
287  return((int) it->second);
288  } else {
289  return(0);
290  }
291  }
292 
293  int BinPrint::TypeValue_GetFromMap(const std::map<std::string, double> & params)
294  {
295  std::map<std::string, double>::const_iterator it = params.find("TypeValue");
296  if ( it != params.end() ){
297  return((int) it->second);
298  } else {
299  return(0);
300  }
301  }
302 
303  double BinPrint::Value_GetFromMap(const std::map<std::string, double> & params)
304  {
305  std::map<std::string, double>::const_iterator it = params.find("Value");
306  if ( it != params.end() ){
307  return(it->second);
308  } else {
309  return(0.0);
310  }
311  }
312 
313  int BinPrint::UseMaskValue_GetFromMap(const std::map<std::string, double> & params)
314  {
315  std::map<std::string, double>::const_iterator it = params.find("UseMaskValue");
316  if ( it != params.end() ){
317  return((int) it->second);
318  } else {
319  return(0);
320  }
321  }
322 
323  double BinPrint::MaskValue_GetFromMap(const std::map<std::string, double> & params)
324  {
325  std::map<std::string, double>::const_iterator it = params.find("MaskValue");
326  if ( it != params.end() ){
327  return(it->second);
328  } else {
329  return(0.0);
330  }
331  }
332 
333  std::vector<bool> BinPrint::Mask1D_GetFromMap(const std::map<std::string, double> & params)
334  {
335  std::map<std::string, double>::const_iterator mask_map_bin;
336 
337  //Make default mask
338  bool default_Mask = true;
339  m_UnMask_All = -1;
340  //Get m_UnMask_All configuration
341  mask_map_bin = params.find("UnMask_All");
342  if ( mask_map_bin != params.end() ) {
343  if(mask_map_bin->second > 0.5) {
344  m_UnMask_All = (int) mask_map_bin->second;
345  default_Mask = false;
346  }
347  }
348  std::vector<bool> Mask(m_NbinsX, default_Mask);
349 
350  //Get specific mask settings
351  for ( int bin = 0; bin < m_NbinsX; bin++ ) {
352  std::string mask_bin = Form("Mask_%d", bin + 1);
353  mask_map_bin = params.find(mask_bin.c_str());
354  if ( mask_map_bin != params.end() ) {
355  if(mask_map_bin->second > 0.5) Mask[bin] = true;
356  else Mask[bin] = false;
357  }
358  }
359 
360  return(Mask);
361  }
362 
363  std::vector< std::vector<bool> > BinPrint::Mask2D_GetFromMap(const std::map<std::string, double> & params)
364  {
365  std::map<std::string, double>::const_iterator mask_map_bin;
366 
367  //Make default mask
368  bool default_Mask = true;
369  m_UnMask_All = -1;
370  //Get m_UnMask_All configuration
371  mask_map_bin = params.find("UnMask_All");
372  if ( mask_map_bin != params.end() ) {
373  if(mask_map_bin->second > 0.5) {
374  m_UnMask_All = (int) mask_map_bin->second;
375  default_Mask = false;
376  }
377  }
378  std::vector< std::vector<bool> > Mask(m_NbinsX, std::vector<bool>(m_NbinsY, default_Mask));
379 
380  //Get specific mask settings
381  for ( int binX = 0; binX < m_NbinsX; binX++ ) {
382  for ( int binY = 0; binY < m_NbinsY; binY++ ) {
383  std::string mask_bin = Form("Mask_%d_%d", binX + 1, binY + 1);
384  mask_map_bin = params.find(mask_bin.c_str());
385  if ( mask_map_bin != params.end() ) {
386  if(mask_map_bin->second > 0.5) Mask[binX][binY] = true;
387  else Mask[binX][binY] = false;
388  }
389  }
390  }
391 
392  return(Mask);
393  }
394 
395 } // namespace dqm_algorithms
dqm_algorithms::BinPrint::BinPrint
BinPrint()
Definition: BinPrint.cxx:97
BinPrint.h
data
char data[hepevt_bytes_allocation_ATLAS]
Definition: HepEvt.cxx:11
dqm_algorithms::BinPrint::~BinPrint
virtual ~BinPrint()
Definition: BinPrint.cxx:114
dqm_algorithms::BinPrint::UseValue_GetFromMap
int UseValue_GetFromMap(const std::map< std::string, double > &params)
Definition: BinPrint.cxx:283
Undefined
@ Undefined
Definition: MaterialTypes.h:8
get_generator_info.result
result
Definition: get_generator_info.py:21
dqm_algorithms::BinPrint::m_MaskValue
double m_MaskValue
Definition: BinPrint.h:42
dqm_algorithms::BinPrint
Definition: BinPrint.h:18
CaloCellPos2Ntuple.int
int
Definition: CaloCellPos2Ntuple.py:24
dqm_algorithms::BinPrint::printDescription
virtual void printDescription(std::ostream &out)
Definition: BinPrint.cxx:30
python.FakeAthena.Algorithm
def Algorithm(name)
Definition: FakeAthena.py:41
skel.it
it
Definition: skel.GENtoEVGEN.py:423
python.AthDsoLogger.out
out
Definition: AthDsoLogger.py:71
bin
Definition: BinsDiffFromStripMedian.h:43
dqm_algorithms::BinPrint::m_UnMask_All
int m_UnMask_All
Definition: BinPrint.h:37
dqm_algorithms::BinPrint::m_NbinsY
int m_NbinsY
Definition: BinPrint.h:36
ReweightUtils.message
message
Definition: ReweightUtils.py:15
config
Definition: PhysicsAnalysis/AnalysisCommon/AssociationUtils/python/config.py:1
instance
std::map< std::string, double > instance
Definition: Run_To_Get_Tags.h:8
dqm_algorithms::BinPrint::execute
virtual dqm_core::Result * execute(const std::string &name, const TObject &data, const dqm_core::AlgorithmConfig &config)
Definition: BinPrint.cxx:129
Trk::binY
@ binY
Definition: BinningType.h:48
Result
ICscStripFitter::Result Result
Definition: CalibCscStripFitter.cxx:13
dqm_algorithms::BinPrint::Mask2D_GetFromMap
std::vector< std::vector< bool > > Mask2D_GetFromMap(const std::map< std::string, double > &params)
Definition: BinPrint.cxx:363
dqm_algorithms::BinPrint::TypeValue_GetFromMap
int TypeValue_GetFromMap(const std::map< std::string, double > &params)
Definition: BinPrint.cxx:293
dqm_algorithms::BinPrint::m_Value
double m_Value
Definition: BinPrint.h:40
Trk::binX
@ binX
Definition: BinningType.h:47
dqm_algorithms::BinPrint::Value_GetFromMap
double Value_GetFromMap(const std::map< std::string, double > &params)
Definition: BinPrint.cxx:303
name
std::string name
Definition: Control/AthContainers/Root/debug.cxx:195
plotBeamSpotVxVal.bin
int bin
Definition: plotBeamSpotVxVal.py:83
dqm_algorithms::BinPrint::m_NbinsX
int m_NbinsX
Definition: BinPrint.h:35
dqm_algorithms::BinPrint::UseMaskValue_GetFromMap
int UseMaskValue_GetFromMap(const std::map< std::string, double > &params)
Definition: BinPrint.cxx:313
dqm_algorithms::BinPrint::m_name
std::string m_name
Definition: BinPrint.h:33
dqm_algorithms
Definition: AddReference.h:17
h
dqm_algorithms::BinPrint::clone
virtual dqm_core::Algorithm * clone()
Definition: BinPrint.cxx:121
dqm_algorithms::BinPrint::MaskValue_GetFromMap
double MaskValue_GetFromMap(const std::map< std::string, double > &params)
Definition: BinPrint.cxx:323
TH1
Definition: rootspy.cxx:268
AlgorithmHelper.h
dqm_algorithms::BinPrint::Mask1D_GetFromMap
std::vector< bool > Mask1D_GetFromMap(const std::map< std::string, double > &params)
Definition: BinPrint.cxx:333
PowhegControl_ttFCNC_NLO.params
params
Definition: PowhegControl_ttFCNC_NLO.py:226
dqm_algorithms::tools::GetFirstFromMap
double GetFirstFromMap(const std::string &paramName, const std::map< std::string, double > &params)
Definition: AlgorithmHelper.cxx:339
dqm_algorithms::BinPrint::m_UseMaskValue
int m_UseMaskValue
Definition: BinPrint.h:41
dqm_algorithms::BinPrint::m_TypeValue
int m_TypeValue
Definition: BinPrint.h:39
dqm_algorithms::BinPrint::m_UseValue
int m_UseValue
Definition: BinPrint.h:38