Skip to content
Snippets Groups Projects
Commit 5e55f1ac authored by Shaun Roe's avatar Shaun Roe
Browse files

Catch possibility of running offf the end of the eta bounds array

parent a5b04f0f
No related branches found
No related tags found
No related merge requests found
......@@ -10,6 +10,7 @@
#include "TrkTrack/Track.h"
#include <stdexcept>
#include <algorithm>
#include <optional>
template<class EnumType>
class AmbiCounter {
......@@ -24,8 +25,11 @@ public:
};
//
AmbiCounter(const std::vector<float> &eta_bounds): m_etaBounds(eta_bounds){
const std::string errMsg = "eta_bounds size must be " + std::to_string(nRegions);
if (m_etaBounds.size()!=nRegions) throw std::out_of_range(errMsg);
const std::string errMsgPrefix = "In AmbiCounter.icc, eta_bounds size must be ";
if (m_etaBounds.size()!=nRegions) throw std::runtime_error(errMsgPrefix + std::to_string(nRegions) + " elements long.");
if (not std::is_sorted(m_etaBounds.begin(), m_etaBounds.end())){
throw std::runtime_error(errMsgPrefix + "in ascending order.");
}
}
//convert Category to array index
......@@ -52,8 +56,11 @@ public:
}
// increment one bin
void
increment(Categories categoryIdx, unsigned int etaBinIdx) {
++m_counter.at(idx(categoryIdx)).at(etaBinIdx);
increment(Categories category, unsigned int etaBinIdx) {
if ((category>= Categories::kNCounter) or (etaBinIdx >=nRegions)){
throw std::out_of_range("in AmbiCounter.icc::increment()");
}
++m_counter[idx(category)][etaBinIdx];
}
//
AmbiCounter<EnumType> & operator +=(const AmbiCounter<EnumType> &a) {
......@@ -81,7 +88,9 @@ public:
std::array<int, nRegions> &nTracks = m_counter.at(idx(categoryIdx));
// @TODO make sure that list of track parameters is not empty
const double absEta = std::abs(track->trackParameters()->front()->eta());
++nTracks.at(etaBin(absEta));
if (const auto &possibleIdx{etaBin(absEta)}){//i.e. if it's within bounds
++nTracks[possibleIdx.value()];
}
}
}
//
......@@ -94,7 +103,6 @@ public:
const auto allRegionCounts = std::accumulate(displayedArray.begin(), displayedArray.end(),0);
out << std::setiosflags(std::ios::dec) << std::setw(iw) << allRegionCounts;
for (unsigned int etaBinIdx=0; etaBinIdx < nRegions; ++etaBinIdx) {
assert( etaBinIdx < m_counter[idx(categoryIdx)].size() );
out << std::setiosflags(std::ios::dec) << std::setw(iw) << m_counter[idx(categoryIdx)][etaBinIdx];
}
out << "\n";
......@@ -110,14 +118,11 @@ private:
std::array<std::array<int, nRegions>,static_cast<size_t>(Categories::kNCounter)> m_counter{};
std::array<int,nGlobalCounters> m_globalCounter{};
const std::vector<float> &m_etaBounds; //!< eta intervals for internal monitoring
size_t
std::optional<size_t>
etaBin(const double val){
size_t regionIdx{};
//eta *must be* in ascending order in the m_etaBounds vector
for (;regionIdx< nRegions; ++regionIdx){
if (val < m_etaBounds[regionIdx]) break;
}
return regionIdx;
auto pVal = std::lower_bound(m_etaBounds.begin(), m_etaBounds.end(), val);
//if it's in bounds, return the value, otherwise return a nullopt
return (pVal!=m_etaBounds.end()) ? std::optional<size_t>(std::distance(m_etaBounds.begin(), pVal)):std::nullopt;
}
};
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment