ATLAS Offline Software
Loading...
Searching...
No Matches
MuonHoughHisto2D.cxx
Go to the documentation of this file.
1/*
2 Copyright (C) 2002-2023 CERN for the benefit of the ATLAS collaboration
3*/
4
6
7#include "TFile.h"
8#include "TH2F.h"
9
10MuonHoughHisto2D::MuonHoughHisto2D(int nbinsx, double xmin, double xmax, int nbinsy, double ymin, double ymax, int number_of_maxima) :
11 AthMessaging("MuonHoughHisto2D"),
12 m_nbinsx(nbinsx),
13 m_xmin(xmin),
14 m_xmax(xmax),
15 m_nbinsy(nbinsy),
16 m_ymin(ymin),
17 m_ymax(ymax),
18 m_number_of_maxima(number_of_maxima),
19 m_scale(10000),
20 m_threshold(2.1 * m_scale),
22 m_nbinsx_plus2 = nbinsx + 2;
23 m_nbinsy_plus2 = nbinsy + 2;
25
26 m_binwidthx = (m_xmax - m_xmin) / (m_nbinsx + 0.);
27 m_binwidthy = (m_ymax - m_ymin) / (m_nbinsy + 0.);
28
31 init();
32}
33
34void MuonHoughHisto2D::init() { m_histBuffer.reset( new unsigned int[m_size]); }
35
37 resetHisto();
38
40 m_maximumbins.clear(); // obsolete?
42 m_maximumBin = -1;
43 m_maximum = 0;
44 m_maximumIsValid = true;
45}
46
48 m_maximumbins.clear(); // clear old m_maxima_vector and start searching again
49
50 int maximum_number = 0;
51
52 while (maximum_number < m_number_of_maxima) {
53 double maximum = -1.;
54 //coverity defect 13671: max bin was set to -1 initially, but this is sued as an index into the array
55 int maxbin = 0;
56
57 ATH_MSG_VERBOSE("MuonHoughHisto2D::size bins above threshold: " << m_bins_above_threshold.size());
58
59 for (const int bin_num :m_bins_above_threshold) {
60 if (!checkIfMaximumAlreadyUsed(bin_num)) {
61 checkIfMaximum(bin_num, maximum, maxbin); // call by ref
62 }
63 }
64
65 if (maximum < m_threshold) {
66 ATH_MSG_DEBUG("MuonHoughHisto2D:: no maximum found");
67 break;
68 } else {
69 ATH_MSG_DEBUG("MuonHoughHisto2D:: Maximum found: " << maximum << " binnumber: " << maxbin << " R (z) " << binnumberToCoords(maxbin).first
70 << " angle " << binnumberToCoords(maxbin).second);
71 m_maximumbins.push_back(maxbin);
72 } // maxbin <> m_threshold
73
74 if (maximum == -1.) {
75 ATH_MSG_VERBOSE("MuonHoughHisto2D::No Bins Above Threshold");
76 } else {
77 std::pair<double, double> coords = binnumberToCoords(maxbin);
78 ATH_MSG_VERBOSE("MuonHoughHisto2D::Maximum Number: " << maximum_number << " Maximum: " << maximum
79 << " binnumber: " << maxbin << " x: " << coords.first << " y: " << coords.second);
80 }
81 maximum_number++;
82 } // number_of_maxima
83
85}
86
87std::pair<int, double> MuonHoughHisto2D::getMax() const {
88 std::pair<int, double> maxpair;
89 int maxbin = -1; // convention! for no bin above threshold
90 unsigned int maximum = m_threshold;
91
92 if (m_maximumIsValid) {
93 if (m_maximum > m_threshold) {
94 maxbin = m_maximumBin;
95 maximum = m_maximum;
96 }
97 } else {
98 for (unsigned int i = 0; i < m_size; i++) {
99 if (m_histBuffer[i] > maximum) {
100 maximum = m_histBuffer[i];
101 maxbin = i;
102 }
103 }
104 }
105 maxpair.first = maxbin;
106 maxpair.second = maximum;
107
108 return maxpair;
109}
110
111std::pair<int, double> MuonHoughHisto2D::getMaximumBin(unsigned int maximum_number) {
112 if (m_maxima_found == 0) {
113 findMaxima(); // fills m_maximumbins
114 }
115
116 int maxbin = -1;
117 double maximum = -1.;
118
119 ATH_MSG_VERBOSE("MuonHoughHisto2D:: m_maximumbins.size: " << m_maximumbins.size());
120
121
122 if (m_maximumbins.size() > maximum_number) {
123 maxbin = m_maximumbins[maximum_number];
124 maximum = content_Bin_Area(maxbin);
125 }
126 return std::make_pair(maxbin, maximum);
127
128} // getMaximumBin
129
130std::pair<double, double> MuonHoughHisto2D::getCoordsMaximum(unsigned int maximum_number) {
131 std::pair<double, double> coordsmaximum;
132 int binnumber = getMaxBin(maximum_number);
133
134 if (binnumber != -1) {
135 coordsmaximum = binnumberToCoords(binnumber);
136 } else {
137 ATH_MSG_WARNING("HoughTransform::No Maximum Found");
138 coordsmaximum.first = 99999.;
139 coordsmaximum.second = 99999.;
140 }
141 return coordsmaximum;
142}
143
145 bool check = false;
146
147 for (unsigned int i = 0; i < m_maximumbins.size(); i++) {
149 check = true;
150 return check;
151 }
152 }
153
154 return check;
155}
156
157bool MuonHoughHisto2D::checkIfMaximum(int binnumber, double& maximum, int& maxbin) const {
158 bool check = false;
159
160 double content_bin_area = content_Bin_Area(binnumber); // now no area anymore == getBinContent
161
162 // when using negative weights the following can happen:
163 if (content_bin_area < m_threshold) return check;
164
165 if (content_bin_area == maximum) {
166 if (getBinContent(maxbin) > getBinContent(binnumber)) // give preference to maximum with peak ( _U_ )
167 {
168 check = true;
169 maximum = content_bin_area;
170 maxbin = binnumber;
171 }
172 } else if (content_bin_area > maximum) {
173 check = true;
174 maximum = content_bin_area;
175 maxbin = binnumber;
176 }
177 return check;
178} // checkIfMaximum
179
180int MuonHoughHisto2D::distanceBins(int binnumber1, int binnumber2) const {
181 int binnumberdistance = std::abs(binnumber1 - binnumber2);
182
183 // Manhattan metric:
184 int distance = (binnumberdistance % m_nbinsx_plus2) + (binnumberdistance / m_nbinsx_plus2);
185
186 return distance;
187}
188
189std::pair<double, double> MuonHoughHisto2D::binnumberToCoords(int binnumber, int /*printLevel*/) const {
190 std::pair<double, double> coordsbin;
191 if (binnumber < 0) {
192 ATH_MSG_WARNING("MuonHoughHisto2D::ERROR: negativebinnumber: " << binnumber);
193 coordsbin.first = 99999.;
194 coordsbin.second = 99999.;
195 return coordsbin;
196 }
197
198 double xcoord = binnumberToXCoord(binnumber);
199 double ycoord = binnumberToYCoord(binnumber);
200
201 ATH_MSG_VERBOSE("MuonHoughHisto2D::Maximum: " << getBinContent(binnumber) << " binnumber: " << binnumber << " x: " << xcoord
202 << " y: " << ycoord);
203
204
205 coordsbin.first = xcoord;
206 coordsbin.second = ycoord;
207 return coordsbin;
208}
209
210int MuonHoughHisto2D::binInHistogram(unsigned int binnumber) const {
211 int bininhisto = 0;
212
213 if ((binnumber) % m_nbinsx_plus2 == 0) {
214 bininhisto = 1;
215 } else if ((binnumber + 1) % m_nbinsx_plus2 == 0) {
216 bininhisto = 2;
217 } else if (binnumber <= m_nbinsx_plus2) {
218 bininhisto = 3;
219 } else if (binnumber >= m_nbinsx_plus2 * (getNbinsY() + 1)) {
220 bininhisto = 4;
221 }
222
223 return bininhisto;
224}
225
226std::unique_ptr<TH2F> MuonHoughHisto2D::bookAndFillRootHistogram(const std::string& hname) const {
227 std::unique_ptr<TH2F> histogram = std::make_unique<TH2F>(hname.c_str(), hname.c_str(), m_nbinsx, m_xmin, m_xmax, m_nbinsy, m_ymin, m_ymax);
228 for (unsigned int i = 0; i < m_size; i++) {
229 int ix = i % m_nbinsx_plus2;
230 int iy = i / m_nbinsx_plus2;
231 histogram->SetBinContent(ix, iy, m_histBuffer[i] / (double)m_scale);
232 }
233 return histogram;
234}
#define ATH_MSG_VERBOSE(x)
#define ATH_MSG_WARNING(x)
#define ATH_MSG_DEBUG(x)
std::string histogram
Definition chains.cxx:52
AthMessaging(IMessageSvc *msgSvc, const std::string &name)
Constructor.
bool checkIfMaximumAlreadyUsed(int binnumber) const
check when searching for several maxima if binnumber is close to an earlier found maximum
std::pair< int, double > getMaximumBin(unsigned int maximum_number=0)
returns binnumber and maximum of maximum number maximum_number
void init()
initialises private members, called in constructor
unsigned int m_size
size of array
unsigned int m_nbinsx
number of x bins
double m_invbinwidthx
inv binwidth x axis used for cpu speedup
std::vector< int > m_maximumbins
binnumbers found as maxima
double m_ymax
maximum y coordinate
int getMaxBin(unsigned int maximum_number=0)
return maximum binnumber
double m_binwidthx
binwidth x axis
unsigned int m_nbinsx_plus2
number of x bins + 2 , used for cpu speedup
unsigned int m_nbinsy_plus2
number of y bins + 2 , used for cpu speedup
const int m_number_of_maxima
number of maxima to be searched for
std::pair< double, double > getCoordsMaximum(unsigned int maximum_number=0)
returns coords of maximum number maximum_number
unsigned int m_maximum
bool checkIfMaximum(int binnumber, double &maximum, int &maxbin) const
check if binnumber is a maximum
int getNbinsY() const
returns number of bins y coordinate
unsigned int m_nbinsy
number of y bins
double m_xmax
maximum x coordinate
void resetHisto()
resets histogram
std::unique_ptr< TH2F > bookAndFillRootHistogram(const std::string &hname) const
intialises a root histogram of MuonHoughHisto2D and fill it, used for debugging purposes
unsigned int m_scale
threshold for minimum content for a maximum
bool m_maxima_found
check if search for maxima already performed
double m_xmin
minimum x coordinate
const int m_distance_to_next_maximum
minimum distance for a maximum to be away from another maximum
void reset()
clears histogram and bins_above_threshold
void findMaxima()
find maxima in histogram
std::set< int > m_bins_above_threshold
set of bins that are above threshold used for speeding up searching for maxima
MuonHoughHisto2D(int nbinsx, double xmin, double xmax, int nbinsy, double ymin, double ymax, int number_of_maxima=1)
constructor
int distanceBins(int binnumber1, int binnumber2) const
calculates the distance in binwidths between two binnumbers ("Manhattan metric")
double m_invbinwidthy
inv binwidth y axis used for cpu speedup
std::pair< int, double > getMax() const
returns binnumber and maximum of histogram (doesn't use m_bins_above_threshold)
int binInHistogram(unsigned int bin) const
checks if bin is in histogram or if it is an under/overflow bin (unused) 0 is in histo 1 x underflow ...
unsigned int m_threshold
double m_ymin
minimum y coordinate
double getBinContent(int binnumber) const
returns x axis
std::unique_ptr< unsigned int[]> m_histBuffer
actual storage of bin values
double binnumberToXCoord(int binnumber) const
gives x-coordinate for certain binnumber
double binnumberToYCoord(int binnumber) const
gives y-coordinate for certain binnumber
double content_Bin_Area(int binnumber) const
return the total content of binarea (default: content of bin)
std::pair< double, double > binnumberToCoords(int binnumber, int printlevel=0) const
gives coordinates for certain binnumber
double m_binwidthy
binwidth y axis
double xmax
Definition listroot.cxx:61
double ymin
Definition listroot.cxx:63
double xmin
Definition listroot.cxx:60
double ymax
Definition listroot.cxx:64