CMS 3D CMS Logo

FFTJetPFPileupCleaner.cc
Go to the documentation of this file.
1 // -*- C++ -*-
2 //
3 // Package: FFTJetProducers
4 // Class: FFTJetPFPileupCleaner
5 //
13 //
14 // Original Author: Igor Volobouev
15 // Created: Thu Jul 14 17:50:33 CDT 2011
16 //
17 //
18 #include <cmath>
19 #include <climits>
20 #include <utility>
21 #include <algorithm>
22 
23 // framework include files
30 
33 
36 
39 
40 #define init_param(type, varname) varname (ps.getParameter< type >( #varname ))
41 
42 //
43 // class declaration
44 //
46 {
47 public:
49  ~FFTJetPFPileupCleaner() override;
50 
51 protected:
52  // methods
53  void beginJob() override;
54  void produce(edm::Event&, const edm::EventSetup&) override;
55  void endJob() override;
56 
57 private:
58  FFTJetPFPileupCleaner() = delete;
61 
63  void setRemovalBit(reco::PFCandidate::ParticleType ptype, bool onOff);
64  void buildRemovalMask();
65  bool isAcceptableVtx(reco::VertexCollection::const_iterator iv) const;
66 
69  const edm::Handle<reco::VertexCollection>& fakeVertices,
70  const reco::PFCandidate& pfcand, bool* fromFakeSet) const;
71 
75 
79 
80  // The following, if true, will switch to an algorithm
81  // which takes a fake primary vertex into account
83 
84  // The following, if true, will cause association of a candidate
85  // with some vertex no matter what
87 
88  // The following switch will check if the primary vertex
89  // is a neighbor (in Z) of a track and will keep the
90  // track if this is so, even if it is not directly associated
91  // with the primary vertex. This switch is meaningful only if
92  // "checkClosestZVertex" is true.
94 
95  // The following, if true, will cause removal of candidates
96  // associated with the main vertex
98 
99  // The following will tell us to remove candidates not associated
100  // with any vertex
102 
103  // Do we want to reverse the decision?
105 
106  // Flags for removing things. See PFCandidate header
107  // for particle types.
108  bool remove_X; // undefined
109  bool remove_h; // charged hadron
110  bool remove_e; // electron
111  bool remove_mu; // muon
112  bool remove_gamma; // photon
113  bool remove_h0; // neutral hadron
114  bool remove_h_HF; // HF tower identified as a hadron
115  bool remove_egamma_HF; // HF tower identified as an EM particle
116 
117  // Mask for removing things
118  unsigned removalMask;
119 
120  // Min and max eta for keeping things
121  double etaMin;
122  double etaMax;
123 
124  // Cut for the vertex Ndof
126 
127  // Cut for the vertex Z
129 
130  // Cut for the vertex rho
131  double vertexRhoCut;
132 
133  // Vector for associating tracks with Z positions of the vertices
134  mutable std::vector<std::pair<double, unsigned> > zAssoc;
135 };
136 
137 //
138 // constructors and destructor
139 //
141  : init_param(edm::InputTag, PFCandidates),
142  init_param(edm::InputTag, Vertices),
143  init_param(edm::InputTag, FakePrimaryVertices),
158  removalMask(0),
159  init_param(double, etaMin),
160  init_param(double, etaMax),
161  init_param(double, vertexNdofCut),
162  init_param(double, vertexZmaxCut),
163  init_param(double, vertexRhoCut)
164 {
166 
167  PFCandidatesToken = consumes<reco::PFCandidateCollection>(PFCandidates);
168  VerticesToken = consumes<reco::VertexCollection>(Vertices);
170  FakePrimaryVerticesToken = consumes<reco::VertexCollection>(FakePrimaryVertices);
171 
172  produces<reco::PFCandidateCollection>();
173 }
174 
175 
177 {
178 }
179 
180 
182  reco::VertexCollection::const_iterator iv) const
183 {
184  return !iv->isFake() &&
185  static_cast<double>(iv->ndof()) > vertexNdofCut &&
186  std::abs(iv->z()) < vertexZmaxCut &&
187  iv->position().rho() < vertexRhoCut;
188 }
189 
190 
191 // ------------ method called to produce the data ------------
193  edm::Event& iEvent, const edm::EventSetup& iSetup)
194 {
195  // get PFCandidates
196  auto pOutput = std::make_unique<reco::PFCandidateCollection>();
197 
199  iEvent.getByToken(PFCandidatesToken, pfCandidates);
200 
201  // get vertices
203  iEvent.getByToken(VerticesToken, vertices);
204 
207  {
208  iEvent.getByToken(FakePrimaryVerticesToken, fakeVertices);
209  if (!fakeVertices.isValid())
210  throw cms::Exception("FFTJetBadConfig")
211  << "ERROR in FFTJetPFPileupCleaner:"
212  " could not find fake vertices"
213  << std::endl;
214  }
215 
216  const unsigned ncand = pfCandidates->size();
217  for (unsigned i=0; i<ncand; ++i)
218  {
219  reco::PFCandidatePtr candptr(pfCandidates, i);
220  bool remove = false;
221 
222  if (isRemovable(candptr->particleId()))
223  {
224  bool fromFakeSet = false;
225  reco::VertexRef vertexref(findSomeVertexWFakes(vertices, fakeVertices,
226  *candptr, &fromFakeSet));
227  if (vertexref.isNull())
228  {
229  // Track is not associated with any vertex
230  // in any of the vertex sets
231  remove = removeUnassociated;
232  }
233  else if (vertexref.key() == 0 &&
234  (!useFakePrimaryVertex || fromFakeSet))
235  {
236  // Track is associated with the main primary vertex
237  // However, if we are using fake vertices, this only
238  // matters if the vertex comes from the fake set. If
239  // it comes from the real set, remove the track anyway
240  // because in the combined set the associated vertex
241  // would not be the main primary vertex.
242  remove = removeMainVertex;
243  }
244  else
245  remove = true;
246  }
247 
248  // Check the eta range
249  if (!remove)
250  {
251  const double eta = candptr->p4().Eta();
252  remove = eta < etaMin || eta > etaMax;
253  }
254 
255  // Should we remember this candidate?
257  remove = !remove;
258  if (!remove)
259  {
260  const reco::PFCandidate& cand = (*pfCandidates)[i];
261  pOutput->push_back(cand);
262  pOutput->back().setSourceCandidatePtr(candptr);
263  }
264  }
265 
266  iEvent.put(std::move(pOutput));
267 }
268 
269 
271  const reco::PFCandidate::ParticleType ptype) const
272 {
273  const unsigned shift = static_cast<unsigned>(ptype);
274  assert(shift < 32U);
275  return removalMask & (1U << shift);
276 }
277 
278 
280  const reco::PFCandidate::ParticleType ptype, const bool value)
281 {
282  const unsigned shift = static_cast<unsigned>(ptype);
283  assert(shift < 32U);
284  const unsigned mask = (1U << shift);
285  if (value)
286  removalMask |= mask;
287  else
288  removalMask &= ~mask;
289 }
290 
291 
292 // The following essentially follows the code in PFPileUp.cc,
293 // with added cut on ndof, vertex Z position, and iteration
294 // over fakes
297  const edm::Handle<reco::VertexCollection>& fakeVertices,
298  const reco::PFCandidate& pfcand, bool* fromFakeSet) const
299 {
300  typedef reco::VertexCollection::const_iterator IV;
302 
303  *fromFakeSet = false;
304  reco::TrackBaseRef trackBaseRef(pfcand.trackRef());
305 
306  size_t iVertex = 0;
307  unsigned nFoundVertex = 0;
308  const IV vertend(vertices->end());
309 
310  {
311  unsigned index = 0;
312  double bestweight = 0.0;
313  for (IV iv=vertices->begin(); iv!=vertend; ++iv, ++index)
314  if (isAcceptableVtx(iv))
315  {
316  const reco::Vertex& vtx = *iv;
317 
318  // loop on tracks in vertices
319  IT trackend(vtx.tracks_end());
320  for (IT iTrack=vtx.tracks_begin(); iTrack!=trackend; ++iTrack)
321  {
322  const reco::TrackBaseRef& baseRef = *iTrack;
323 
324  // one of the tracks in the vertex is the same as
325  // the track considered in the function
326  if (baseRef == trackBaseRef)
327  {
328  // select the vertex for which the track has the highest weight
329  const double w = vtx.trackWeight(baseRef);
330  if (w > bestweight)
331  {
332  bestweight=w;
333  iVertex=index;
334  nFoundVertex++;
335  }
336  }
337  }
338  }
339  }
340 
341  if (nFoundVertex > 0)
342  {
343  if (nFoundVertex != 1)
344  edm::LogWarning("TrackOnTwoVertex")
345  << "a track is shared by at least two vertices. "
346  << "Used to be an assert";
347 
348  // Check if we can re-associate this track with one
349  // of the fake vertices
351  {
352  const double ztrack = pfcand.vertex().z();
353  double dzmin = std::abs(ztrack - ((*vertices)[iVertex]).z());
354 
355  const IV fakeEnd(fakeVertices->end());
356  unsigned index = 0;
357  for (IV iv=fakeVertices->begin(); iv!=fakeEnd; ++iv, ++index)
358  if (isAcceptableVtx(iv))
359  {
360  const double dz = std::abs(ztrack - iv->z());
361  if (dz < dzmin)
362  {
363  dzmin = dz;
364  iVertex = index;
365  *fromFakeSet = true;
366  }
367  }
368  }
369 
370  return reco::VertexRef(*fromFakeSet ? fakeVertices : vertices, iVertex);
371  }
372 
373  // optional: as a secondary solution, associate the closest vertex in z
374  if (checkClosestZVertex)
375  {
376  const double ztrack = pfcand.vertex().z();
377  bool foundVertex = false;
378 
379  if (keepIfPVneighbor)
380  {
381  // Sort all vertices according to their Z coordinate
382  zAssoc.clear();
383  unsigned index = 0;
384  for (IV iv=vertices->begin(); iv!=vertend; ++iv, ++index)
385  if (isAcceptableVtx(iv))
386  zAssoc.push_back(std::pair<double,unsigned>(iv->z(), index));
387  const unsigned numRealVertices = index;
388 
389  // Mix the fake vertex collection into zAssoc.
390  // Note that we do not reset "index" before doing this.
392  {
393  const IV fakeEnd(fakeVertices->end());
394  for (IV iv=fakeVertices->begin(); iv!=fakeEnd; ++iv, ++index)
395  if (isAcceptableVtx(iv))
396  zAssoc.push_back(std::pair<double,unsigned>(iv->z(), index));
397  }
398 
399  // Check where the track z position fits into this sequence
400  if (!zAssoc.empty())
401  {
402  std::sort(zAssoc.begin(), zAssoc.end());
403  std::pair<double,unsigned> tPair(ztrack, UINT_MAX);
404  const unsigned iAbove = std::upper_bound(
405  zAssoc.begin(), zAssoc.end(), tPair) - zAssoc.begin();
406 
407  // Check whether one of the vertices with indices
408  // iAbove or (iAbove - 1) is a primary vertex.
409  // If so, return it. Otherwise return the one
410  // with closest distance to the track.
411  unsigned ich[2] = {0U, 0U};
412  unsigned nch = 1;
413  if (iAbove)
414  {
415  ich[0] = iAbove - 1U;
416  ich[1] = iAbove;
417  if (iAbove < zAssoc.size())
418  nch = 2;
419  }
420 
421  double dzmin = 1.0e100;
422  unsigned bestVertexNum = UINT_MAX;
423  for (unsigned icheck=0; icheck<nch; ++icheck)
424  {
425  const unsigned zAssocIndex = ich[icheck];
426  const unsigned vertexNum = zAssoc[zAssocIndex].second;
427 
428  if (vertexNum == numRealVertices ||
429  (!useFakePrimaryVertex && vertexNum == 0U))
430  {
431  bestVertexNum = vertexNum;
432  break;
433  }
434 
435  const double dz = std::abs(ztrack - zAssoc[zAssocIndex].first);
436  if (dz < dzmin)
437  {
438  dzmin = dz;
439  bestVertexNum = vertexNum;
440  }
441  }
442 
443  foundVertex = bestVertexNum < UINT_MAX;
444  if (foundVertex)
445  {
446  iVertex = bestVertexNum;
447  if (iVertex >= numRealVertices)
448  {
449  *fromFakeSet = true;
450  iVertex -= numRealVertices;
451  }
452  }
453  }
454  }
455  else
456  {
457  // This is a simple association algorithm (from PFPileUp)
458  // extended to take fake vertices into account
459  double dzmin = 1.0e100;
460  unsigned index = 0;
461  for (IV iv=vertices->begin(); iv!=vertend; ++iv, ++index)
462  if (isAcceptableVtx(iv))
463  {
464  const double dz = std::abs(ztrack - iv->z());
465  if (dz < dzmin)
466  {
467  dzmin = dz;
468  iVertex = index;
469  foundVertex = true;
470  }
471  }
472 
474  {
475  const IV fakeEnd(fakeVertices->end());
476  index = 0;
477  for (IV iv=fakeVertices->begin(); iv!=fakeEnd; ++iv, ++index)
478  if (isAcceptableVtx(iv))
479  {
480  const double dz = std::abs(ztrack - iv->z());
481  if (dz < dzmin)
482  {
483  dzmin = dz;
484  iVertex = index;
485  *fromFakeSet = true;
486  foundVertex = true;
487  }
488  }
489  }
490  }
491 
492  if (foundVertex)
493  return reco::VertexRef(*fromFakeSet ? fakeVertices : vertices, iVertex);
494  }
495 
496  return reco::VertexRef();
497 }
498 
499 
501 {
510 }
511 
512 
513 // ------------ method called once each job just before starting event loop
515 {
516 }
517 
518 
519 // ------------ method called once each job just after ending the event loop
521 {
522 }
523 
524 
525 //define this as a plug-in
void setRemovalBit(reco::PFCandidate::ParticleType ptype, bool onOff)
OrphanHandle< PROD > put(std::unique_ptr< PROD > product)
Put a new product.
Definition: Event.h:125
ParticleType
particle types
Definition: PFCandidate.h:45
const double w
Definition: UKUtility.cc:23
trackRef_iterator tracks_end() const
last iterator over tracks
Definition: Vertex.cc:81
bool getByToken(EDGetToken token, Handle< PROD > &result) const
Definition: Event.h:517
edm::EDGetTokenT< reco::VertexCollection > FakePrimaryVerticesToken
key_type key() const
Accessor for product key.
Definition: Ref.h:263
reco::TrackRef trackRef() const
Definition: PFCandidate.cc:442
bool isRemovable(reco::PFCandidate::ParticleType ptype) const
const Point & vertex() const override
vertex position (overwritten by PF...)
Definition: PFCandidate.cc:656
FFTJetPFPileupCleaner & operator=(const FFTJetPFPileupCleaner &)=delete
int iEvent
Definition: GenABIO.cc:224
#define DEFINE_FWK_MODULE(type)
Definition: MakerMacros.h:16
FFTJetPFPileupCleaner()=delete
Abs< T >::type abs(const T &t)
Definition: Abs.h:22
edm::EDGetTokenT< reco::VertexCollection > VerticesToken
float trackWeight(const TREF &r) const
returns the weight with which a Track has contributed to the vertex-fit.
Definition: Vertex.h:81
Definition: value.py:1
std::vector< LinkConnSpec >::const_iterator IT
bool isValid() const
Definition: HandleBase.h:74
void produce(edm::Event &, const edm::EventSetup &) override
edm::Ref< VertexCollection > VertexRef
persistent reference to a Vertex
Definition: VertexFwd.h:13
bool isNull() const
Checks for null.
Definition: Ref.h:248
std::vector< std::pair< double, unsigned > > zAssoc
edm::EDGetTokenT< reco::PFCandidateCollection > PFCandidatesToken
#define init_param(type, varname)
Particle reconstructed by the particle flow algorithm.
Definition: PFCandidate.h:40
HLT enums.
reco::VertexRef findSomeVertexWFakes(const edm::Handle< reco::VertexCollection > &vertices, const edm::Handle< reco::VertexCollection > &fakeVertices, const reco::PFCandidate &pfcand, bool *fromFakeSet) const
std::vector< TrackBaseRef >::const_iterator trackRef_iterator
The iteratator for the vector<TrackRef>
Definition: Vertex.h:37
static unsigned int const shift
trackRef_iterator tracks_begin() const
first iterator over tracks
Definition: Vertex.cc:76
def move(src, dest)
Definition: eostools.py:511
bool isAcceptableVtx(reco::VertexCollection::const_iterator iv) const