CMS 3D CMS Logo

KDTreeLinkerTrackHcal.cc
Go to the documentation of this file.
7 
8 // This class is used to find all links between Tracks and HCAL clusters
9 // using a KDTree algorithm.
10 // It is used in PFBlockAlgo.cc in the function links().
12 public:
14  ~KDTreeLinkerTrackHcal() override;
15 
16  // With this method, we create the list of track that we want to link.
18 
19  // Here, we create the list of hcalCluster that we want to link. From hcalCluster
20  // and fraction, we will create a second list of rechits that will be used to
21  // build the KDTree.
22  void insertFieldClusterElt(reco::PFBlockElement* hcalCluster) override;
23 
24  // The KDTree building from rechits list.
25  void buildTree() override;
26 
27  // Here we will iterate over all tracks. For each track intersection point with HCAL,
28  // we will search the closest rechits in the KDTree, from rechits we will find the
29  // hcalClusters and after that we will check the links between the track and
30  // all closest hcalClusters.
31  void searchLinks() override;
32 
33  // Here, we will store all Track/HCAL founded links in the PFBlockElement class
34  // of each psCluster in the PFmultilinks field.
35  void updatePFBlockEltWithLinks() override;
36 
37  // Here we free all allocated structures.
38  void clear() override;
39 
40 private:
41  // Data used by the KDTree algorithm : sets of Tracks and HCAL clusters.
44 
45  // Sets of rechits that compose the HCAL clusters.
47 
48  // Map of linked Track/HCAL clusters.
50 
51  // Map of the HCAL clusters associated to a rechit.
53 
54  // KD trees
56 
57  // TrajectoryPoints
62  bool checkExit_;
63 
64  // Hcal-track links
66 };
67 
68 // the text name is different so that we can easily
69 // construct it when calling the factory
70 DEFINE_EDM_PLUGIN(KDTreeLinkerFactory, KDTreeLinkerTrackHcal, "KDTreeTrackAndHCALLinker");
71 
73  : KDTreeLinkerBase(conf),
74  trajectoryLayerEntranceString_(conf.getParameter<std::string>("trajectoryLayerEntrance")),
75  trajectoryLayerExitString_(conf.getParameter<std::string>("trajectoryLayerExit")),
76  nMaxHcalLinksPerTrack_(conf.getParameter<int>("nMaxHcalLinksPerTrack")) {
77  // Initialization
79  phiOffset_ = 0.32;
80  // convert TrajectoryLayers info from string to enum
83  // make sure the requested setting is supported
90  // flag if exit layer should be checked or not
92 }
93 
95 
97  if (track->trackRefPF()->extrapolatedPoint(trajectoryLayerEntrance_).isValid()) {
98  targetSet_.insert(track);
99  }
100 }
101 
103  const reco::PFClusterRef& clusterref = hcalCluster->clusterRef();
104 
105  // This test is more or less done in PFBlockAlgo.h. In others cases, it should be switch on.
106  // if (!((clusterref->layer() == PFLayer::HCAL_ENDCAP) ||
107  // (clusterref->layer() == PFLayer::HCAL_BARREL1)))
108  // return;
109 
110  const std::vector<reco::PFRecHitFraction>& fraction = clusterref->recHitFractions();
111 
112  // We create a list of hcalCluster
113  fieldClusterSet_.insert(hcalCluster);
114  for (size_t rhit = 0; rhit < fraction.size(); ++rhit) {
115  const reco::PFRecHitRef& rh = fraction[rhit].recHitRef();
116  double fract = fraction[rhit].fraction();
117 
118  if ((rh.isNull()) || (fract < cutOffFrac))
119  continue;
120 
121  const reco::PFRecHit& rechit = *rh;
122 
123  // We save the links rechit to HcalClusters
124  rechit2ClusterLinks_[&rechit].insert(hcalCluster);
125 
126  // We create a liste of rechits
127  rechitsSet_.insert(&rechit);
128  }
129 }
130 
132  // List of pseudo-rechits that will be used to create the KDTree
133  std::vector<KDTreeNodeInfo<reco::PFRecHit const*, 2>> eltList;
134 
135  // Filling of this list
136  for (RecHitSet::const_iterator it = rechitsSet_.begin(); it != rechitsSet_.end(); it++) {
137  const reco::PFRecHit::REPPoint& posrep = (*it)->positionREP();
138 
139  KDTreeNodeInfo<reco::PFRecHit const*, 2> rh1(*it, posrep.eta(), posrep.phi());
140  eltList.push_back(rh1);
141 
142  // Here we solve the problem of phi circular set by duplicating some rechits
143  // too close to -Pi (or to Pi) and adding (substracting) to them 2 * Pi.
144  if (rh1.dims[1] > (M_PI - phiOffset_)) {
145  float phi = rh1.dims[1] - 2 * M_PI;
146  KDTreeNodeInfo<reco::PFRecHit const*, 2> rh2(*it, float(posrep.eta()), phi);
147  eltList.push_back(rh2);
148  }
149 
150  if (rh1.dims[1] < (M_PI * -1.0 + phiOffset_)) {
151  float phi = rh1.dims[1] + 2 * M_PI;
152  KDTreeNodeInfo<reco::PFRecHit const*, 2> rh3(*it, float(posrep.eta()), phi);
153  eltList.push_back(rh3);
154  }
155  }
156 
157  // Here we define the upper/lower bounds of the 2D space (eta/phi).
158  float phimin = -1.0 * M_PI - phiOffset_;
159  float phimax = M_PI + phiOffset_;
160 
161  // etamin-etamax, phimin-phimax
162  KDTreeBox region(-3.0f, 3.0f, phimin, phimax);
163 
164  // We may now build the KDTree
165  tree_.build(eltList, region);
166 }
167 
169  // Most of the code has been taken from LinkByRecHit.cc
170 
171  // We iterate over the tracks.
172  for (BlockEltSet::iterator it = targetSet_.begin(); it != targetSet_.end(); it++) {
173  reco::PFRecTrackRef trackref = (*it)->trackRefPF();
174 
175  const reco::PFTrajectoryPoint& atHCAL = trackref->extrapolatedPoint(trajectoryLayerEntrance_);
176 
177  // The track didn't reach hcal
178  if (!atHCAL.isValid())
179  continue;
180 
181  // In case the exit point check is requested, check eta and phi differences between entrance and exit
182  double dHeta = 0.0;
183  float dHphi = 0.0;
184  if (checkExit_) {
185  const reco::PFTrajectoryPoint& atHCALExit = trackref->extrapolatedPoint(trajectoryLayerExit_);
186  dHeta = atHCALExit.positionREP().eta() - atHCAL.positionREP().eta();
187  dHphi = atHCALExit.positionREP().phi() - atHCAL.positionREP().phi();
188  if (dHphi > M_PI)
189  dHphi = dHphi - 2. * M_PI;
190  else if (dHphi < -M_PI)
191  dHphi = dHphi + 2. * M_PI;
192  } // checkExit_
193 
194  float tracketa = atHCAL.positionREP().eta() + 0.1 * dHeta;
195  float trackphi = atHCAL.positionREP().phi() + 0.1 * dHphi;
196 
197  if (trackphi > M_PI)
198  trackphi -= 2 * M_PI;
199  else if (trackphi < -M_PI)
200  trackphi += 2 * M_PI;
201 
202  // Estimate the maximal envelope in phi/eta that will be used to find rechit candidates.
203  // Same envelope for cap et barrel rechits.
204  double inflation = 1.;
205  float rangeeta = (cristalPhiEtaMaxSize_ * (1.5 + 0.5) + 0.2 * fabs(dHeta)) * inflation;
206  float rangephi = (cristalPhiEtaMaxSize_ * (1.5 + 0.5) + 0.2 * fabs(dHphi)) * inflation;
207 
208  // We search for all candidate recHits, ie all recHits contained in the maximal size envelope.
209  std::vector<reco::PFRecHit const*> recHits;
210  KDTreeBox trackBox(tracketa - rangeeta, tracketa + rangeeta, trackphi - rangephi, trackphi + rangephi);
211  tree_.search(trackBox, recHits);
212 
213  // Here we check all rechit candidates using the non-approximated method.
214  for (auto const& recHit : recHits) {
215  const auto& rhrep = recHit->positionREP();
216  const auto& corners = recHit->getCornersREP();
217 
218  double rhsizeeta = fabs(corners[3].eta() - corners[1].eta());
219  double rhsizephi = fabs(corners[3].phi() - corners[1].phi());
220  if (rhsizephi > M_PI)
221  rhsizephi = 2. * M_PI - rhsizephi;
222 
223  double deta = fabs(rhrep.eta() - tracketa);
224  double dphi = fabs(rhrep.phi() - trackphi);
225  if (dphi > M_PI)
226  dphi = 2. * M_PI - dphi;
227 
228  // Find all clusters associated to given rechit
229  RecHit2BlockEltMap::iterator ret = rechit2ClusterLinks_.find(recHit);
230 
231  for (BlockEltSet::iterator clusterIt = ret->second.begin(); clusterIt != ret->second.end(); clusterIt++) {
232  const reco::PFClusterRef clusterref = (*clusterIt)->clusterRef();
233  int fracsNbr = clusterref->recHitFractions().size();
234 
235  double _rhsizeeta = rhsizeeta * (1.5 + 0.5 / fracsNbr) + 0.2 * fabs(dHeta);
236  double _rhsizephi = rhsizephi * (1.5 + 0.5 / fracsNbr) + 0.2 * fabs(dHphi);
237 
238  // Check if the track and the cluster are linked
239  if (deta < (_rhsizeeta / 2.) && dphi < (_rhsizephi / 2.))
240  target2ClusterLinks_[*it].insert(*clusterIt);
241  }
242  }
243  }
244 }
245 
247  //TODO YG : Check if cluster positionREP() is valid ?
248 
249  // Here we save in each track the list of phi/eta values of linked clusters.
250  for (BlockElt2BlockEltMap::iterator it = target2ClusterLinks_.begin(); it != target2ClusterLinks_.end(); ++it) {
251  const auto& trackElt = it->first;
252  const auto& hcalEltSet = it->second;
253  reco::PFMultiLinksTC multitracks(true);
254 
255  //
256  // No restriction on the number of HCAL links per track or isLinkedToDisplacedVertex
257  if (nMaxHcalLinksPerTrack_ < 0. || trackElt->isLinkedToDisplacedVertex()) {
258  for (const auto& hcalElt : hcalEltSet) {
259  reco::PFMultilink multiLink(hcalElt->clusterRef());
260  multitracks.linkedPFObjects.push_back(multiLink);
261 
262  // We set the multilinks flag of the track (for links to ECAL) to true. It will allow us to
263  // use it in an optimized way in prefilter
264  hcalElt->setIsValidMultilinks(true, _targetType);
265  }
266 
267  }
268  //
269  // Store only the N closest HCAL links per track.
270  else {
271  const reco::PFRecTrackRef& trackref = trackElt->trackRefPF();
272  const reco::PFTrajectoryPoint& tkAtHCALEnt = trackref->extrapolatedPoint(trajectoryLayerEntrance_);
273  const reco::PFCluster::REPPoint& tkreppos = tkAtHCALEnt.positionREP();
274  // Check exit point
275  double dHEta = 0.;
276  double dHPhi = 0.;
277  double dRHCALEx = 0.;
278  if (checkExit_) {
279  const reco::PFTrajectoryPoint& tkAtHCALEx = trackref->extrapolatedPoint(trajectoryLayerExit_);
280  dHEta = (tkAtHCALEx.positionREP().Eta() - tkAtHCALEnt.positionREP().Eta());
281  dHPhi = reco::deltaPhi(tkAtHCALEx.positionREP().Phi(), tkAtHCALEnt.positionREP().Phi());
282  dRHCALEx = tkAtHCALEx.position().R();
283  }
284 
285  std::vector<double> vDist;
286  double dist(-1.0);
287 
288  // Fill the vector of distances between HCAL clusters and the track
289  for (const auto& hcalElt : hcalEltSet) {
290  double clusterphi = hcalElt->clusterRef()->positionREP().phi();
291  double clustereta = hcalElt->clusterRef()->positionREP().eta();
292 
293  // when checkExit_ is false
294  if (!checkExit_) {
295  dist = LinkByRecHit::computeDist(clustereta, clusterphi, tkreppos.Eta(), tkreppos.Phi());
296  }
297  // when checkExit_ is true
298  else {
299  //special case ! A looper can exit the barrel inwards and hit the endcap
300  //In this case calculate the distance based on the first crossing since
301  //the looper will probably never make it to the endcap
302  if (dRHCALEx < tkAtHCALEnt.position().R()) {
303  dist = LinkByRecHit::computeDist(clustereta, clusterphi, tkreppos.Eta(), tkreppos.Phi());
304  edm::LogWarning("KDTreeLinkerTrackHcal ")
305  << "Special case of linking with track hitting HCAL and looping back in the tracker ";
306  } else {
308  clustereta, clusterphi, tkreppos.Eta() + 0.1 * dHEta, tkreppos.Phi() + 0.1 * dHPhi);
309  }
310  } // checkExit_
311 
312  vDist.push_back(dist);
313  } // loop over hcalEltSet
314 
315  // Fill multitracks
316  for (auto i : sort_indexes(vDist)) {
317  const BlockEltSet::iterator hcalEltIt = std::next(hcalEltSet.begin(), i);
318  reco::PFMultilink multiLink((*hcalEltIt)->clusterRef());
319  multitracks.linkedPFObjects.push_back(multiLink);
320  // We set the multilinks flag of the track (for links to ECAL) to true. It will allow us to
321  // use it in an optimized way in prefilter
322  (*hcalEltIt)->setIsValidMultilinks(true, _targetType);
323 
324  if (multitracks.linkedPFObjects.size() >= (unsigned)nMaxHcalLinksPerTrack_)
325  break;
326  }
327  }
328 
329  // Store multitracks
330  trackElt->setMultilinks(multitracks, _fieldType);
331  } // loop over target2ClusterLinks_
332 }
333 
335  targetSet_.clear();
336  fieldClusterSet_.clear();
337 
338  rechitsSet_.clear();
339 
340  rechit2ClusterLinks_.clear();
341  target2ClusterLinks_.clear();
342 
343  tree_.clear();
344 }
constexpr double deltaPhi(double phi1, double phi2)
Definition: deltaPhi.h:26
std::set< reco::PFBlockElement * > BlockEltSet
Abstract base class for a PFBlock element (track, cluster...)
reco::PFTrajectoryPoint::LayerType trajectoryLayerEntrance_
const REPPoint & positionREP() const
trajectory position in (rho, eta, phi) base
static double computeDist(double eta1, double phi1, double eta2, double phi2, bool etaPhi=true)
computes a chisquare
void search(const KDTreeBox< DIM > &searchBox, std::vector< DATA > &resRecHitList)
reco::PFBlockElement::Type _fieldType
ret
prodAgent to be discontinued
BlockElt2BlockEltMap target2ClusterLinks_
std::map< const reco::PFRecHit *, BlockEltSet > RecHit2BlockEltMap
static std::vector< size_t > sort_indexes(const std::vector< T > &v)
std::map< reco::PFBlockElement *, BlockEltSet > BlockElt2BlockEltMap
KDTreeLinkerTrackHcal(const edm::ParameterSet &conf)
KDTreeLinkerAlgo< reco::PFRecHit const * > tree_
assert(be >=bs)
bool isValid() const
is this point valid ?
std::string trajectoryLayerEntranceString_
Particle flow rechit (rechit + geometry and topology information). See clustering algorithm in PFClus...
Definition: PFRecHit.h:31
RecHit2BlockEltMap rechit2ClusterLinks_
float phi() const
momentum azimuthal angle
Definition: PtEtaPhiMass.h:54
void updatePFBlockEltWithLinks() override
virtual const PFClusterRef & clusterRef() const
double f[11][100]
std::set< const reco::PFRecHit * > RecHitSet
const Fraction< n, m >::type & fract()
Definition: Fraction.h:36
bool isNull() const
Checks for null.
Definition: Ref.h:235
static LayerType layerTypeByName(const std::string &name)
void build(std::vector< KDTreeNodeInfo< DATA, DIM > > &eltList, const KDTreeBox< DIM > &region)
#define M_PI
const float cutOffFrac
const math::XYZPoint & position() const
cartesian position (x, y, z)
void insertTargetElt(reco::PFBlockElement *track) override
PFMultilinksType linkedPFObjects
reco::PFTrajectoryPoint::LayerType trajectoryLayerExit_
void insertFieldClusterElt(reco::PFBlockElement *hcalCluster) override
float eta() const
momentum pseudorapidity
Definition: PtEtaPhiMass.h:52
reco::PFBlockElement::Type _targetType
ROOT::Math::PositionVector3D< ROOT::Math::CylindricalEta3D< double > > REPPoint
Definition: PFCluster.h:48
#define DEFINE_EDM_PLUGIN(factory, type, name)
A PFTrack holds several trajectory points, which basically contain the position and momentum of a tra...
Log< level::Warning, false > LogWarning
LayerType
Define the different layers where the track can be propagated.