ATLAS Offline Software
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 
10 MuonHoughHisto2D::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),
21  m_distance_to_next_maximum(100) {
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 
34 void MuonHoughHisto2D::init() { m_histBuffer.reset( new unsigned int[m_size]); }
35 
37  resetHisto();
38 
39  m_bins_above_threshold.clear();
40  m_maximumbins.clear(); // obsolete?
41  m_maxima_found = 0;
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 
84  m_maxima_found = 1;
85 }
86 
87 std::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 
111 std::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 
130 std::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 
157 bool 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 
180 int 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 
189 std::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 
210 int 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 
226 std::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 }
MuonHoughHisto2D::getBinContent
double getBinContent(int binnumber) const
returns x axis
Definition: MuonHoughHisto2D.h:205
python.SystemOfUnits.second
int second
Definition: SystemOfUnits.py:120
MuonHoughHisto2D::m_invbinwidthx
double m_invbinwidthx
inv binwidth x axis used for cpu speedup
Definition: MuonHoughHisto2D.h:152
ymin
double ymin
Definition: listroot.cxx:63
MuonHoughHisto2D::m_ymax
double m_ymax
maximum y coordinate
Definition: MuonHoughHisto2D.h:144
MuonHoughHisto2D::m_maximum
unsigned int m_maximum
Definition: MuonHoughHisto2D.h:179
MuonHoughHisto2D::m_binwidthy
double m_binwidthy
binwidth y axis
Definition: MuonHoughHisto2D.h:149
MuonHoughHisto2D::getMax
std::pair< int, double > getMax() const
returns binnumber and maximum of histogram (doesn't use m_bins_above_threshold)
Definition: MuonHoughHisto2D.cxx:87
MuonHoughHisto2D::findMaxima
void findMaxima()
find maxima in histogram
Definition: MuonHoughHisto2D.cxx:47
MuonHoughHisto2D::m_maximumIsValid
bool m_maximumIsValid
Definition: MuonHoughHisto2D.h:180
dqt_zlumi_pandas.hname
string hname
Definition: dqt_zlumi_pandas.py:279
MuonHoughHisto2D::binnumberToXCoord
double binnumberToXCoord(int binnumber) const
gives x-coordinate for certain binnumber
Definition: MuonHoughHisto2D.h:228
MuonHoughHisto2D::distanceBins
int distanceBins(int binnumber1, int binnumber2) const
calculates the distance in binwidths between two binnumbers ("Manhattan metric")
Definition: MuonHoughHisto2D.cxx:180
MuonHoughHisto2D::m_histBuffer
std::unique_ptr< unsigned int[]> m_histBuffer
actual storage of bin values
Definition: MuonHoughHisto2D.h:125
MuonHoughHisto2D::m_size
unsigned int m_size
size of array
Definition: MuonHoughHisto2D.h:127
MuonHoughHisto2D::init
void init()
initialises private members, called in constructor
Definition: MuonHoughHisto2D.cxx:34
MuonHoughHisto2D::m_maxima_found
bool m_maxima_found
check if search for maxima already performed
Definition: MuonHoughHisto2D.h:175
MuonHoughHisto2D::m_maximumbins
std::vector< int > m_maximumbins
binnumbers found as maxima
Definition: MuonHoughHisto2D.h:161
MuonHoughHisto2D::m_threshold
unsigned int m_threshold
Definition: MuonHoughHisto2D.h:169
ATH_MSG_VERBOSE
#define ATH_MSG_VERBOSE(x)
Definition: AthMsgStreamMacros.h:28
MuonHoughHisto2D::m_distance_to_next_maximum
const int m_distance_to_next_maximum
minimum distance for a maximum to be away from another maximum
Definition: MuonHoughHisto2D.h:172
MuonHoughHisto2D::checkIfMaximum
bool checkIfMaximum(int binnumber, double &maximum, int &maxbin) const
check if binnumber is a maximum
Definition: MuonHoughHisto2D.cxx:157
MuonHoughHisto2D::m_nbinsy
unsigned int m_nbinsy
number of y bins
Definition: MuonHoughHisto2D.h:138
MuonHoughHisto2D::m_ymin
double m_ymin
minimum y coordinate
Definition: MuonHoughHisto2D.h:142
lumiFormat.i
int i
Definition: lumiFormat.py:85
xmin
double xmin
Definition: listroot.cxx:60
ATH_MSG_DEBUG
#define ATH_MSG_DEBUG(x)
Definition: AthMsgStreamMacros.h:29
MuonHoughHisto2D::content_Bin_Area
double content_Bin_Area(int binnumber) const
return the total content of binarea (default: content of bin)
Definition: MuonHoughHisto2D.h:206
MuonHoughHisto2D::checkIfMaximumAlreadyUsed
bool checkIfMaximumAlreadyUsed(int binnumber) const
check when searching for several maxima if binnumber is close to an earlier found maximum
Definition: MuonHoughHisto2D.cxx:144
MuonHoughHisto2D::reset
void reset()
clears histogram and bins_above_threshold
Definition: MuonHoughHisto2D.cxx:36
AthMessaging
Class to provide easy MsgStream access and capabilities.
Definition: AthMessaging.h:55
MuonHoughHisto2D::m_nbinsx
unsigned int m_nbinsx
number of x bins
Definition: MuonHoughHisto2D.h:130
MuonHoughHisto2D.h
MuonHoughHisto2D::binnumberToCoords
std::pair< double, double > binnumberToCoords(int binnumber, int printlevel=0) const
gives coordinates for certain binnumber
Definition: MuonHoughHisto2D.cxx:189
MuonHoughHisto2D::resetHisto
void resetHisto()
resets histogram
Definition: MuonHoughHisto2D.h:234
MuonHoughHisto2D::binInHistogram
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 ...
Definition: MuonHoughHisto2D.cxx:210
MuonHoughHisto2D::binnumberToYCoord
double binnumberToYCoord(int binnumber) const
gives y-coordinate for certain binnumber
Definition: MuonHoughHisto2D.h:231
MuonHoughHisto2D::m_xmin
double m_xmin
minimum x coordinate
Definition: MuonHoughHisto2D.h:134
MuonHoughHisto2D::getMaxBin
int getMaxBin(unsigned int maximum_number=0)
return maximum binnumber
Definition: MuonHoughHisto2D.h:218
LArNewCalib_Delay_OFC_Cali.check
check
Definition: LArNewCalib_Delay_OFC_Cali.py:267
MuonHoughHisto2D::m_xmax
double m_xmax
maximum x coordinate
Definition: MuonHoughHisto2D.h:136
MuonHoughHisto2D::getNbinsY
int getNbinsY() const
returns number of bins y coordinate
Definition: MuonHoughHisto2D.h:208
MuonHoughHisto2D::getMaximumBin
std::pair< int, double > getMaximumBin(unsigned int maximum_number=0)
returns binnumber and maximum of maximum number maximum_number
Definition: MuonHoughHisto2D.cxx:111
ATH_MSG_WARNING
#define ATH_MSG_WARNING(x)
Definition: AthMsgStreamMacros.h:32
MuonHoughHisto2D::m_nbinsy_plus2
unsigned int m_nbinsy_plus2
number of y bins + 2 , used for cpu speedup
Definition: MuonHoughHisto2D.h:140
DeMoScan.first
bool first
Definition: DeMoScan.py:536
MuonHoughHisto2D::m_bins_above_threshold
std::set< int > m_bins_above_threshold
set of bins that are above threshold used for speeding up searching for maxima
Definition: MuonHoughHisto2D.h:158
xmax
double xmax
Definition: listroot.cxx:61
MuonHoughHisto2D::m_invbinwidthy
double m_invbinwidthy
inv binwidth y axis used for cpu speedup
Definition: MuonHoughHisto2D.h:154
MuonHoughHisto2D::m_number_of_maxima
const int m_number_of_maxima
number of maxima to be searched for
Definition: MuonHoughHisto2D.h:165
MuonHoughHisto2D::getCoordsMaximum
std::pair< double, double > getCoordsMaximum(unsigned int maximum_number=0)
returns coords of maximum number maximum_number
Definition: MuonHoughHisto2D.cxx:130
Amg::distance
float distance(const Amg::Vector3D &p1, const Amg::Vector3D &p2)
calculates the distance between two point in 3D space
Definition: GeoPrimitivesHelpers.h:54
MuonHoughHisto2D::m_maximumBin
int m_maximumBin
maximum
Definition: MuonHoughHisto2D.h:178
MuonHoughHisto2D::m_scale
unsigned int m_scale
threshold for minimum content for a maximum
Definition: MuonHoughHisto2D.h:168
histogram
std::string histogram
Definition: chains.cxx:52
MuonHoughHisto2D::m_nbinsx_plus2
unsigned int m_nbinsx_plus2
number of x bins + 2 , used for cpu speedup
Definition: MuonHoughHisto2D.h:132
MuonHoughHisto2D::bookAndFillRootHistogram
std::unique_ptr< TH2F > bookAndFillRootHistogram(const std::string &hname) const
intialises a root histogram of MuonHoughHisto2D and fill it, used for debugging purposes
Definition: MuonHoughHisto2D.cxx:226
ymax
double ymax
Definition: listroot.cxx:64
MuonHoughHisto2D::m_binwidthx
double m_binwidthx
binwidth x axis
Definition: MuonHoughHisto2D.h:147
MuonHoughHisto2D::MuonHoughHisto2D
MuonHoughHisto2D(int nbinsx, double xmin, double xmax, int nbinsy, double ymin, double ymax, int number_of_maxima=1)
constructor
Definition: MuonHoughHisto2D.cxx:10