ATLAS Offline Software
Loading...
Searching...
No Matches
BinDump.cxx
Go to the documentation of this file.
1/*
2 Copyright (C) 2002-2022 CERN for the benefit of the ATLAS collaboration
3*/
4
5// **********************************************************************
6// $Id: BinDump.cxx,v 1.2 2009/02/07 09:37:17 Remi Lafaye
7// **********************************************************************
8
10
11#include <cmath>
12#include <iostream>
13#include <map>
14#include <algorithm>
15
16#include <TClass.h>
17#include <TH1.h>
18#include <TAxis.h>
19
20#include "dqm_core/exceptions.h"
21#include "dqm_core/AlgorithmConfig.h"
23#include "dqm_core/AlgorithmManager.h"
24#include "dqm_core/Result.h"
25#include "ers/ers.h"
26
27
29
30
31namespace dqm_algorithms {
32
33// *********************************************************************
34// Public Methods
35// *********************************************************************
36
38BinDump()
39 : m_name("BinDump")
40{
41 dqm_core::AlgorithmManager::instance().registerAlgorithm( m_name, this );
42}
43
44
47{
48}
49
50
51dqm_core::Algorithm*
53clone()
54{
55 return new BinDump(*this);
56}
57
58
59dqm_core::Result*
61execute( const std::string& name, const TObject& object, const dqm_core::AlgorithmConfig& config)
62{
63 const TH1 * histogram;
64
65 if( object.IsA()->InheritsFrom( "TH1" ) ) {
66 histogram = static_cast<const TH1*>(&object);
67 if (histogram->GetDimension() > 2 ){
68 throw dqm_core::BadConfig( ERS_HERE, name, "dimension > 2 " );
69 }
70 } else {
71 throw dqm_core::BadConfig( ERS_HERE, name, "does not inherit from TH1" );
72 }
73
74 int binStart, binEnd;
75 int method;
76 double gmin;
77 double rmin;
78 try {
79 binStart = (int) dqm_algorithms::tools::GetFirstFromMap("BinStart", config.getParameters() );
80 binEnd = (int) dqm_algorithms::tools::GetFirstFromMap("BinEnd", config.getParameters() );
81 method = (int) dqm_algorithms::tools::GetFirstFromMap("Method", config.getParameters() );
82 rmin = dqm_algorithms::tools::GetFromMap( "NEntries", config.getRedThresholds());
83 gmin = dqm_algorithms::tools::GetFromMap( "NEntries", config.getGreenThresholds() );
84 }
85 catch ( dqm_core::Exception & ex ) {
86 throw dqm_core::BadConfig( ERS_HERE, name, ex.what(), ex );
87 }
88
89 if (rmin < gmin) {
90 std::swap(rmin, gmin);
91 }
92
93 double count = 0;
94 int nbinx = histogram -> GetNbinsX();
95 int nbiny = histogram -> GetNbinsY();
96 if(binStart>binEnd) {
97 binStart= 1;
98 binEnd = histogram -> GetNbinsX();
99 }
100 int binSize = binEnd-binStart+1;
101
102 std::vector<double> contents(binSize);
103 //contents.resize(binSize);
104 for(int i=0;i<binSize;i++) {
105 int binx = binStart+i;
106 contents[i]=0;
107 for ( int j = 1; j <= nbiny; ++j ) {
108 double content= histogram -> GetBinContent(binx,j);
109 contents[i]+=content;
110 count+=content;
111 }
112 }
113
114 ERS_DEBUG(1,"Number of entries for bins is "
115<< count );
116 ERS_DEBUG(1,"Green: "<< gmin << " entries; Red: " << rmin << " entries ");
117
118
119 const TAxis *axis = histogram->GetXaxis();
120 dqm_core::Result* result = new dqm_core::Result();
121 if(axis) {
122 for(int i=0;i<binSize;i++) {
123 char tag[256];
124 const char *label;
125 if(binStart+i==0) sprintf(tag,"Underflows");
126 else if(binStart+i==nbinx+1) sprintf(tag,"Overflows");
127 else {
128 label=axis->GetBinLabel(binStart+i);
129 // remove space in names to help handy.py
130 if(label) {
131 int l=strlen(label);
132 int j=0;
133
134 char *mylabel=new char[l+1];
135 if(mylabel) {
136 for(int k=0;k<l;k++)
137 if(label[k]!=' ') mylabel[j++]=label[k];
138 mylabel[j]='\0';
139 if (mylabel[0]!=0) sprintf(tag,"%s",mylabel);
140 else sprintf(tag,"Bin%d",binStart+i);
141 delete[] mylabel;
142 }
143 else sprintf(tag,"Bin%d",binStart+i);
144 }
145 else sprintf(tag,"Bin%d",binStart+i);
146 }
147 result->tags_[tag] = contents[i];
148 }
149 } else {
150 for(int i=0;i<binSize;i++) {
151 char tag[256];
152 if(binStart+i==0) sprintf(tag,"Underflows");
153 else if(binStart+i==nbinx+1) sprintf(tag,"Overflows");
154 else sprintf(tag,"Bin%d",binStart+i);
155 result->tags_[tag] = contents[i];
156 }
157 }
158
159 if(method==0) { // Check on bin sums
160 if ( count <= gmin ) {
161 result->status_ = dqm_core::Result::Green;
162 } else if ( count > rmin ) {
163 result->status_ = dqm_core::Result::Red;
164 } else {
165 result->status_ = dqm_core::Result::Yellow;
166 }
167 result->tags_["Total"] = count;
168 }
169 else if(method>0) { // Check on maximum bin
170 count = 0;
171 for(int i=0;i<binSize;i++) {
172 if(contents[i]>count) count = contents[i];
173 }
174 if ( count <= gmin ) {
175 result->status_ = dqm_core::Result::Green;
176 } else if ( count > rmin ) {
177 result->status_ = dqm_core::Result::Red;
178 } else {
179 result->status_ = dqm_core::Result::Yellow;
180 }
181 result->tags_["Highest"] = count;
182 }
183 else if(method<0) { // Check on minimum bin
184 count = 1e30;
185 for(int i=0;i<binSize;i++) {
186 if(contents[i]<count) count = contents[i];
187 }
188 if ( count <= gmin ) {
189 result->status_ = dqm_core::Result::Green;
190 } else if ( count > rmin ) {
191 result->status_ = dqm_core::Result::Red;
192 } else {
193 result->status_ = dqm_core::Result::Yellow;
194 }
195 result->tags_["Lowest"] = count;
196 }
197
198 return result;
199}
200
201
202void
204printDescription(std::ostream& out)
205{
206 std::string message;
207 message += "\n";
208 message += "Algorithm: \"" + m_name + "\"\n";
209 message += "Description: Check the number of entries in listed bins\n";
210 message += "Parameters: BinStart first bin to be checked (0=underflows)\n";
211 message += " BinEnd last bin to be checked (nbin+1=overflows)\n";
212 message += " Method=0 check is performed on bin sum\n";
213 message += " Method<0 check is performed on lowest bin\n";
214 message += " Method>0 check is performed on highest bin\n";
215 message += " Nentries Green/Red minimums";
216 message += "\n";
217
218 out << message;
219}
220
221} // namespace dqm_algorithms
static dqm_algorithms::AveragePrint staticInstance
std::string histogram
Definition chains.cxx:52
std::string m_name
Definition BinDump.h:29
virtual dqm_core::Algorithm * clone()
Definition BinDump.cxx:53
virtual dqm_core::Result * execute(const std::string &name, const TObject &object, const dqm_core::AlgorithmConfig &config)
Definition BinDump.cxx:61
virtual void printDescription(std::ostream &out)
Definition BinDump.cxx:204
void contents(std::vector< std::string > &keys, TDirectory *td, const std::string &directory, const std::string &pattern, const std::string &path)
int count(std::string s, const std::string &regx)
count how many occurances of a regx are in a string
Definition hcg.cxx:146
std::string label(const std::string &format, int i)
Definition label.h:19
double GetFirstFromMap(const std::string &paramName, const std::map< std::string, double > &params)
const T & GetFromMap(const std::string &pname, const std::map< std::string, T > &params)
void swap(ElementLinkVector< DOBJ > &lhs, ElementLinkVector< DOBJ > &rhs)
#define IsA
Declare the TObject style functions.