CMS 3D CMS Logo

LocalMaximumSeedFinder.cc
Go to the documentation of this file.
7 
8 #include <algorithm>
9 #include <cfloat>
10 #include <tuple>
11 #include <unordered_map>
12 #include <queue>
13 
14 class LocalMaximumSeedFinder final : public SeedFinderBase {
15 public:
19 
21  const std::vector<bool>& mask,
22  std::vector<bool>& seedable,
23  const HcalPFCuts*) override;
24 
25 private:
26  const int _nNeighbours;
27 
28  const std::unordered_map<std::string, int> _layerMap;
29 
30  typedef std::tuple<std::vector<int>, std::vector<double>, std::vector<double> > I3tuple;
31 
32  std::array<I3tuple, 35> _thresholds;
33  static constexpr int layerOffset = 15;
34 
35  static constexpr double detacut = 0.01;
36  static constexpr double dphicut = 0.01;
37 };
38 
40 
41 namespace {
42  const reco::PFRecHit::Neighbours _noNeighbours(nullptr, 0);
43 }
44 
46  : SeedFinderBase(conf),
47  _nNeighbours(conf.getParameter<int>("nNeighbours")),
48  _layerMap({{"PS2", (int)PFLayer::PS2},
49  {"PS1", (int)PFLayer::PS1},
50  {"ECAL_ENDCAP", (int)PFLayer::ECAL_ENDCAP},
51  {"ECAL_BARREL", (int)PFLayer::ECAL_BARREL},
52  {"NONE", (int)PFLayer::NONE},
53  {"HCAL_BARREL1", (int)PFLayer::HCAL_BARREL1},
54  {"HCAL_BARREL2_RING0", (int)PFLayer::HCAL_BARREL2},
55  // hack to deal with ring1 in HO
56  {"HCAL_BARREL2_RING1", 19},
57  {"HCAL_ENDCAP", (int)PFLayer::HCAL_ENDCAP},
58  {"HF_EM", (int)PFLayer::HF_EM},
59  {"HF_HAD", (int)PFLayer::HF_HAD}}) {
60  const std::vector<edm::ParameterSet>& thresholds = conf.getParameterSetVector("thresholdsByDetector");
61  for (const auto& pset : thresholds) {
62  const std::string& det = pset.getParameter<std::string>("detector");
63  std::vector<int> depths;
64  std::vector<double> thresh_E;
65  std::vector<double> thresh_pT;
66  std::vector<double> thresh_pT2;
67 
68  if (det == std::string("HCAL_BARREL1") || det == std::string("HCAL_ENDCAP")) {
69  depths = pset.getParameter<std::vector<int> >("depths");
70  thresh_E = pset.getParameter<std::vector<double> >("seedingThreshold");
71  thresh_pT = pset.getParameter<std::vector<double> >("seedingThresholdPt");
72  if (thresh_E.size() != depths.size() || thresh_pT.size() != depths.size()) {
73  throw cms::Exception("InvalidGatheringThreshold") << "gatheringThresholds mismatch with the numbers of depths";
74  }
75  } else {
76  depths.push_back(0);
77  thresh_E.push_back(pset.getParameter<double>("seedingThreshold"));
78  thresh_pT.push_back(pset.getParameter<double>("seedingThresholdPt"));
79  }
80 
81  for (unsigned int i = 0; i < thresh_pT.size(); ++i) {
82  thresh_pT2.push_back(thresh_pT[i] * thresh_pT[i]);
83  }
84 
85  auto entry = _layerMap.find(det);
86  if (entry == _layerMap.end()) {
87  throw cms::Exception("InvalidDetectorLayer") << "Detector layer : " << det << " is not in the list of recognized"
88  << " detector layers!";
89  }
90 
91  _thresholds[entry->second + layerOffset] = std::make_tuple(depths, thresh_E, thresh_pT2);
92  }
93 }
94 
95 // the starting state of seedable is all false!
97  const std::vector<bool>& mask,
98  std::vector<bool>& seedable,
99  const HcalPFCuts* hcalCuts) {
100  auto nhits = input->size();
101  initDynArray(bool, nhits, usable, true);
102  //need to run over energy sorted rechits
103  declareDynArray(float, nhits, energies);
104  unInitDynArray(int, nhits, qst); // queue storage
105  auto cmp = [&](int i, int j) { return energies[i] < energies[j]; };
106  std::priority_queue<int, DynArray<int>, decltype(cmp)> ordered_hits(cmp, std::move(qst));
107 
108  for (unsigned i = 0; i < nhits; ++i) {
109  if (!mask[i])
110  continue; // cannot seed masked objects
111  auto const& maybeseed = (*input)[i];
112  energies[i] = maybeseed.energy();
113  int seedlayer = (int)maybeseed.layer();
114  if (seedlayer == PFLayer::HCAL_BARREL2 && std::abs(maybeseed.positionREP().eta()) > 0.34) {
115  seedlayer = 19;
116  }
117  auto const& thresholds = _thresholds[seedlayer + layerOffset];
118 
119  double thresholdE = 0.;
120  double thresholdPT2 = 0.;
121 
122  for (unsigned int j = 0; j < (std::get<2>(thresholds)).size(); ++j) {
123  int depth = std::get<0>(thresholds)[j];
124  if ((seedlayer == PFLayer::HCAL_BARREL1 && maybeseed.depth() == depth) ||
125  (seedlayer == PFLayer::HCAL_ENDCAP && maybeseed.depth() == depth) ||
126  (seedlayer != PFLayer::HCAL_BARREL1 && seedlayer != PFLayer::HCAL_ENDCAP)) {
127  thresholdE = std::get<1>(thresholds)[j];
128  thresholdPT2 = std::get<2>(thresholds)[j];
129  }
130  }
131 
132  if (hcalCuts != nullptr) { // this means, cutsFromDB is set to True in PFClusterProducer.cc
133  if ((seedlayer == PFLayer::HCAL_BARREL1) || (seedlayer == PFLayer::HCAL_ENDCAP)) {
134  HcalDetId thisId = maybeseed.detId();
135  const HcalPFCut* item = hcalCuts->getValues(thisId.rawId());
136  thresholdE = item->seedThreshold();
137  }
138  }
139 
140  if (maybeseed.energy() < thresholdE || maybeseed.pt2() < thresholdPT2)
141  usable[i] = false;
142  if (!usable[i])
143  continue;
144  ordered_hits.push(i);
145  }
146 
147  while (!ordered_hits.empty()) {
148  auto idx = ordered_hits.top();
149  ordered_hits.pop();
150  if (!usable[idx])
151  continue;
152  //get the neighbours of this seed
153  auto const& maybeseed = (*input)[idx];
154  reco::PFRecHit::Neighbours myNeighbours;
155  switch (_nNeighbours) {
156  case -1:
157  myNeighbours = maybeseed.neighbours();
158  break;
159  case 0: // for HF clustering
160  myNeighbours = _noNeighbours;
161  break;
162  case 4:
163  myNeighbours = maybeseed.neighbours4();
164  break;
165  case 8:
166  myNeighbours = maybeseed.neighbours8();
167  break;
168  default:
169  throw cms::Exception("InvalidConfiguration") << "LocalMaximumSeedFinder only accepts nNeighbors = {-1,0,4,8}";
170  }
171  seedable[idx] = true;
172  for (auto neighbour : myNeighbours) {
173  if (!mask[neighbour])
174  continue;
175  if (energies[neighbour] > energies[idx]) {
176  // std::cout << "how this can be?" << std::endl;
177  seedable[idx] = false;
178  break;
179  }
180  }
181  if (seedable[idx]) {
182  for (auto neighbour : myNeighbours) {
183  //
184  // For HCAL,
185  // even if channel a is a neighbor of channel b, channel b may not be a neighbor of channel a.
186  // So, perform additional checks to ensure making a hit unusable for seeding is safe.
187  int seedlayer = (int)maybeseed.layer();
188  switch (seedlayer) {
191  case PFLayer::HF_EM: // with the current HF setting, we won't see this case
192  // but this can come in if we change _nNeighbours for HF.
193  case PFLayer::HF_HAD: // same as above
194  // HO has only one depth and eta-phi segmentation is regular, so no need to make this check
195  auto const& nei = (*input)[neighbour];
196  if (maybeseed.depth() != nei.depth())
197  continue; // masking is done only if the neighbor is on the same depth layer as the seed
198  if (std::abs(deltaPhi(maybeseed.positionREP().phi(), nei.positionREP().phi())) > dphicut &&
199  std::abs(maybeseed.positionREP().eta() - nei.positionREP().eta()) > detacut)
200  continue; // masking is done only if the neighbor is on the swiss-cross w.r.t. the seed
201  break;
202  }
203 
204  usable[neighbour] = false;
205 
206  } // for-loop
207  }
208  }
209 
210  LogDebug("LocalMaximumSeedFinder") << " found " << std::count(seedable.begin(), seedable.end(), true) << " seeds";
211 }
LocalMaximumSeedFinder & operator=(const LocalMaximumSeedFinder &)=delete
#define initDynArray(T, n, x, i)
Definition: DynArray.h:94
void findSeeds(const edm::Handle< reco::PFRecHitCollection > &input, const std::vector< bool > &mask, std::vector< bool > &seedable, const HcalPFCuts *) override
const Item * getValues(DetId fId, bool throwOnFail=true) const
LocalMaximumSeedFinder(const edm::ParameterSet &conf)
static std::string const input
Definition: EdmProvDump.cc:50
std::tuple< std::vector< int >, std::vector< double >, std::vector< double > > I3tuple
std::array< I3tuple, 35 > _thresholds
#define unInitDynArray(T, n, x)
Definition: DynArray.h:88
Abs< T >::type abs(const T &t)
Definition: Abs.h:22
static constexpr int layerOffset
static constexpr double detacut
static constexpr double dphicut
constexpr uint32_t rawId() const
get the raw id
Definition: DetId.h:57
#define DEFINE_EDM_PLUGIN(factory, type, name)
#define declareDynArray(T, n, x)
Definition: DynArray.h:91
const std::unordered_map< std::string, int > _layerMap
def move(src, dest)
Definition: eostools.py:511
#define LogDebug(id)