CMS 3D CMS Logo

LocalMaximumSeedFinder.cc
Go to the documentation of this file.
2 
3 #include <algorithm>
4 #include <queue>
5 #include <cfloat>
8 
9 namespace {
10  const reco::PFRecHit::Neighbours _noNeighbours(nullptr, 0);
11 }
12 
14  : SeedFinderBase(conf),
15  _nNeighbours(conf.getParameter<int>("nNeighbours")),
16  _layerMap({{"PS2", (int)PFLayer::PS2},
17  {"PS1", (int)PFLayer::PS1},
18  {"ECAL_ENDCAP", (int)PFLayer::ECAL_ENDCAP},
19  {"ECAL_BARREL", (int)PFLayer::ECAL_BARREL},
20  {"NONE", (int)PFLayer::NONE},
21  {"HCAL_BARREL1", (int)PFLayer::HCAL_BARREL1},
22  {"HCAL_BARREL2_RING0", (int)PFLayer::HCAL_BARREL2},
23  // hack to deal with ring1 in HO
24  {"HCAL_BARREL2_RING1", 19},
25  {"HCAL_ENDCAP", (int)PFLayer::HCAL_ENDCAP},
26  {"HF_EM", (int)PFLayer::HF_EM},
27  {"HF_HAD", (int)PFLayer::HF_HAD}}) {
28  const std::vector<edm::ParameterSet>& thresholds = conf.getParameterSetVector("thresholdsByDetector");
29  for (const auto& pset : thresholds) {
30  const std::string& det = pset.getParameter<std::string>("detector");
31 
32  std::vector<int> depths;
33  std::vector<double> thresh_E;
34  std::vector<double> thresh_pT;
35  std::vector<double> thresh_pT2;
36 
37  if (det == std::string("HCAL_BARREL1") || det == std::string("HCAL_ENDCAP")) {
38  depths = pset.getParameter<std::vector<int> >("depths");
39  thresh_E = pset.getParameter<std::vector<double> >("seedingThreshold");
40  thresh_pT = pset.getParameter<std::vector<double> >("seedingThresholdPt");
41  if (thresh_E.size() != depths.size() || thresh_pT.size() != depths.size()) {
42  throw cms::Exception("InvalidGatheringThreshold") << "gatheringThresholds mismatch with the numbers of depths";
43  }
44  } else {
45  depths.push_back(0);
46  thresh_E.push_back(pset.getParameter<double>("seedingThreshold"));
47  thresh_pT.push_back(pset.getParameter<double>("seedingThresholdPt"));
48  }
49 
50  for (unsigned int i = 0; i < thresh_pT.size(); ++i) {
51  thresh_pT2.push_back(thresh_pT[i] * thresh_pT[i]);
52  }
53 
54  auto entry = _layerMap.find(det);
55  if (entry == _layerMap.end()) {
56  throw cms::Exception("InvalidDetectorLayer") << "Detector layer : " << det << " is not in the list of recognized"
57  << " detector layers!";
58  }
59 
60  _thresholds[entry->second + layerOffset] = std::make_tuple(depths, thresh_E, thresh_pT2);
61  }
62 }
63 
64 // the starting state of seedable is all false!
66  const std::vector<bool>& mask,
67  std::vector<bool>& seedable) {
68  auto nhits = input->size();
69  initDynArray(bool, nhits, usable, true);
70  //need to run over energy sorted rechits
71  declareDynArray(float, nhits, energies);
72  unInitDynArray(int, nhits, qst); // queue storage
73  auto cmp = [&](int i, int j) { return energies[i] < energies[j]; };
74  std::priority_queue<int, DynArray<int>, decltype(cmp)> ordered_hits(cmp, std::move(qst));
75 
76  for (unsigned i = 0; i < nhits; ++i) {
77  if (!mask[i])
78  continue; // cannot seed masked objects
79  auto const& maybeseed = (*input)[i];
80  energies[i] = maybeseed.energy();
81  int seedlayer = (int)maybeseed.layer();
82  if (seedlayer == PFLayer::HCAL_BARREL2 && std::abs(maybeseed.positionREP().eta()) > 0.34) {
83  seedlayer = 19;
84  }
85  auto const& thresholds = _thresholds[seedlayer + layerOffset];
86 
87  double thresholdE = 0.;
88  double thresholdPT2 = 0.;
89 
90  for (unsigned int j = 0; j < (std::get<2>(thresholds)).size(); ++j) {
91  int depth = std::get<0>(thresholds)[j];
92  if ((seedlayer == PFLayer::HCAL_BARREL1 && maybeseed.depth() == depth) ||
93  (seedlayer == PFLayer::HCAL_ENDCAP && maybeseed.depth() == depth) ||
94  (seedlayer != PFLayer::HCAL_BARREL1 && seedlayer != PFLayer::HCAL_ENDCAP)) {
95  thresholdE = std::get<1>(thresholds)[j];
96  thresholdPT2 = std::get<2>(thresholds)[j];
97  }
98  }
99 
100  if (maybeseed.energy() < thresholdE || maybeseed.pt2() < thresholdPT2)
101  usable[i] = false;
102  if (!usable[i])
103  continue;
104  ordered_hits.push(i);
105  }
106 
107  while (!ordered_hits.empty()) {
108  auto idx = ordered_hits.top();
109  ordered_hits.pop();
110  if (!usable[idx])
111  continue;
112  //get the neighbours of this seed
113  auto const& maybeseed = (*input)[idx];
114  reco::PFRecHit::Neighbours myNeighbours;
115  switch (_nNeighbours) {
116  case -1:
117  myNeighbours = maybeseed.neighbours();
118  break;
119  case 0: // for HF clustering
120  myNeighbours = _noNeighbours;
121  break;
122  case 4:
123  myNeighbours = maybeseed.neighbours4();
124  break;
125  case 8:
126  myNeighbours = maybeseed.neighbours8();
127  break;
128  default:
129  throw cms::Exception("InvalidConfiguration") << "LocalMaximumSeedFinder only accepts nNeighbors = {-1,0,4,8}";
130  }
131  seedable[idx] = true;
132  for (auto neighbour : myNeighbours) {
133  if (!mask[neighbour])
134  continue;
135  if (energies[neighbour] > energies[idx]) {
136  // std::cout << "how this can be?" << std::endl;
137  seedable[idx] = false;
138  break;
139  }
140  }
141  if (seedable[idx]) {
142  for (auto neighbour : myNeighbours) {
143  usable[neighbour] = false;
144  }
145  }
146  }
147 
148  LogDebug("LocalMaximumSeedFinder") << " found " << std::count(seedable.begin(), seedable.end(), true) << " seeds";
149 }
#define LogDebug(id)
#define declareDynArray(T, n, x)
Definition: DynArray.h:91
void findSeeds(const edm::Handle< reco::PFRecHitCollection > &input, const std::vector< bool > &mask, std::vector< bool > &seedable) override
#define initDynArray(T, n, x, i)
Definition: DynArray.h:94
LocalMaximumSeedFinder(const edm::ParameterSet &conf)
static std::string const input
Definition: EdmProvDump.cc:48
#define unInitDynArray(T, n, x)
Definition: DynArray.h:88
std::array< I3tuple, 35 > _thresholds
Abs< T >::type abs(const T &t)
Definition: Abs.h:22
const std::unordered_map< std::string, int > _layerMap
def move(src, dest)
Definition: eostools.py:511