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