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 public:
48  FFTJetPFPileupCleaner() = delete;
51  ~FFTJetPFPileupCleaner() override;
52 
53 protected:
54  // methods
55  void beginJob() override;
56  void produce(edm::Event&, const edm::EventSetup&) override;
57  void endJob() override;
58 
59 private:
62  void buildRemovalMask();
63  bool isAcceptableVtx(reco::VertexCollection::const_iterator iv) const;
64 
66  const edm::Handle<reco::VertexCollection>& fakeVertices,
68  bool* fromFakeSet) const;
69 
73 
77 
78  // The following, if true, will switch to an algorithm
79  // which takes a fake primary vertex into account
81 
82  // The following, if true, will cause association of a candidate
83  // with some vertex no matter what
85 
86  // The following switch will check if the primary vertex
87  // is a neighbor (in Z) of a track and will keep the
88  // track if this is so, even if it is not directly associated
89  // with the primary vertex. This switch is meaningful only if
90  // "checkClosestZVertex" is true.
92 
93  // The following, if true, will cause removal of candidates
94  // associated with the main vertex
96 
97  // The following will tell us to remove candidates not associated
98  // with any vertex
100 
101  // Do we want to reverse the decision?
103 
104  // Flags for removing things. See PFCandidate header
105  // for particle types.
106  bool remove_X; // undefined
107  bool remove_h; // charged hadron
108  bool remove_e; // electron
109  bool remove_mu; // muon
110  bool remove_gamma; // photon
111  bool remove_h0; // neutral hadron
112  bool remove_h_HF; // HF tower identified as a hadron
113  bool remove_egamma_HF; // HF tower identified as an EM particle
114 
115  // Mask for removing things
116  unsigned removalMask;
117 
118  // Min and max eta for keeping things
119  double etaMin;
120  double etaMax;
121 
122  // Cut for the vertex Ndof
124 
125  // Cut for the vertex Z
127 
128  // Cut for the vertex rho
129  double vertexRhoCut;
130 
131  // Vector for associating tracks with Z positions of the vertices
132  mutable std::vector<std::pair<double, unsigned> > zAssoc;
133 };
134 
135 //
136 // constructors and destructor
137 //
156  removalMask(0),
157  init_param(double, etaMin),
158  init_param(double, etaMax),
159  init_param(double, vertexNdofCut),
160  init_param(double, vertexZmaxCut),
161  init_param(double, vertexRhoCut) {
163 
164  PFCandidatesToken = consumes<reco::PFCandidateCollection>(PFCandidates);
165  VerticesToken = consumes<reco::VertexCollection>(Vertices);
167  FakePrimaryVerticesToken = consumes<reco::VertexCollection>(FakePrimaryVertices);
168 
169  produces<reco::PFCandidateCollection>();
170 }
171 
173 
174 bool FFTJetPFPileupCleaner::isAcceptableVtx(reco::VertexCollection::const_iterator iv) const {
175  return !iv->isFake() && static_cast<double>(iv->ndof()) > vertexNdofCut && std::abs(iv->z()) < vertexZmaxCut &&
176  iv->position().rho() < vertexRhoCut;
177 }
178 
179 // ------------ method called to produce the data ------------
181  // get PFCandidates
182  auto pOutput = std::make_unique<reco::PFCandidateCollection>();
183 
186 
187  // get vertices
189  iEvent.getByToken(VerticesToken, vertices);
190 
192  if (useFakePrimaryVertex) {
193  iEvent.getByToken(FakePrimaryVerticesToken, fakeVertices);
194  if (!fakeVertices.isValid())
195  throw cms::Exception("FFTJetBadConfig") << "ERROR in FFTJetPFPileupCleaner:"
196  " could not find fake vertices"
197  << std::endl;
198  }
199 
200  const unsigned ncand = pfCandidates->size();
201  for (unsigned i = 0; i < ncand; ++i) {
203  bool remove = false;
204 
205  if (isRemovable(candptr->particleId())) {
206  bool fromFakeSet = false;
207  reco::VertexRef vertexref(findSomeVertexWFakes(vertices, fakeVertices, *candptr, &fromFakeSet));
208  if (vertexref.isNull()) {
209  // Track is not associated with any vertex
210  // in any of the vertex sets
211  remove = removeUnassociated;
212  } else if (vertexref.key() == 0 && (!useFakePrimaryVertex || fromFakeSet)) {
213  // Track is associated with the main primary vertex
214  // However, if we are using fake vertices, this only
215  // matters if the vertex comes from the fake set. If
216  // it comes from the real set, remove the track anyway
217  // because in the combined set the associated vertex
218  // would not be the main primary vertex.
219  remove = removeMainVertex;
220  } else
221  remove = true;
222  }
223 
224  // Check the eta range
225  if (!remove) {
226  const double eta = candptr->p4().Eta();
227  remove = eta < etaMin || eta > etaMax;
228  }
229 
230  // Should we remember this candidate?
232  remove = !remove;
233  if (!remove) {
234  const reco::PFCandidate& cand = (*pfCandidates)[i];
235  pOutput->push_back(cand);
236  pOutput->back().setSourceCandidatePtr(candptr);
237  }
238  }
239 
240  iEvent.put(std::move(pOutput));
241 }
242 
244  const unsigned shift = static_cast<unsigned>(ptype);
245  assert(shift < 32U);
246  return removalMask & (1U << shift);
247 }
248 
250  const unsigned shift = static_cast<unsigned>(ptype);
251  assert(shift < 32U);
252  const unsigned mask = (1U << shift);
253  if (value)
254  removalMask |= mask;
255  else
256  removalMask &= ~mask;
257 }
258 
259 // The following essentially follows the code in PFPileUp.cc,
260 // with added cut on ndof, vertex Z position, and iteration
261 // over fakes
263  const edm::Handle<reco::VertexCollection>& fakeVertices,
264  const reco::PFCandidate& pfcand,
265  bool* fromFakeSet) const {
266  typedef reco::VertexCollection::const_iterator IV;
268 
269  *fromFakeSet = false;
270  reco::TrackBaseRef trackBaseRef(pfcand.trackRef());
271 
272  size_t iVertex = 0;
273  unsigned nFoundVertex = 0;
274  const IV vertend(vertices->end());
275 
276  {
277  unsigned index = 0;
278  double bestweight = 0.0;
279  for (IV iv = vertices->begin(); iv != vertend; ++iv, ++index)
280  if (isAcceptableVtx(iv)) {
281  const reco::Vertex& vtx = *iv;
282 
283  // loop on tracks in vertices
284  IT trackend(vtx.tracks_end());
285  for (IT iTrack = vtx.tracks_begin(); iTrack != trackend; ++iTrack) {
286  const reco::TrackBaseRef& baseRef = *iTrack;
287 
288  // one of the tracks in the vertex is the same as
289  // the track considered in the function
290  if (baseRef == trackBaseRef) {
291  // select the vertex for which the track has the highest weight
292  const double w = vtx.trackWeight(baseRef);
293  if (w > bestweight) {
294  bestweight = w;
295  iVertex = index;
296  nFoundVertex++;
297  }
298  }
299  }
300  }
301  }
302 
303  if (nFoundVertex > 0) {
304  if (nFoundVertex != 1)
305  edm::LogWarning("TrackOnTwoVertex") << "a track is shared by at least two vertices. "
306  << "Used to be an assert";
307 
308  // Check if we can re-associate this track with one
309  // of the fake vertices
310  if (useFakePrimaryVertex) {
311  const double ztrack = pfcand.vertex().z();
312  double dzmin = std::abs(ztrack - ((*vertices)[iVertex]).z());
313 
314  const IV fakeEnd(fakeVertices->end());
315  unsigned index = 0;
316  for (IV iv = fakeVertices->begin(); iv != fakeEnd; ++iv, ++index)
317  if (isAcceptableVtx(iv)) {
318  const double dz = std::abs(ztrack - iv->z());
319  if (dz < dzmin) {
320  dzmin = dz;
321  iVertex = index;
322  *fromFakeSet = true;
323  }
324  }
325  }
326 
327  return reco::VertexRef(*fromFakeSet ? fakeVertices : vertices, iVertex);
328  }
329 
330  // optional: as a secondary solution, associate the closest vertex in z
331  if (checkClosestZVertex) {
332  const double ztrack = pfcand.vertex().z();
333  bool foundVertex = false;
334 
335  if (keepIfPVneighbor) {
336  // Sort all vertices according to their Z coordinate
337  zAssoc.clear();
338  unsigned index = 0;
339  for (IV iv = vertices->begin(); iv != vertend; ++iv, ++index)
340  if (isAcceptableVtx(iv))
341  zAssoc.push_back(std::pair<double, unsigned>(iv->z(), index));
342  const unsigned numRealVertices = index;
343 
344  // Mix the fake vertex collection into zAssoc.
345  // Note that we do not reset "index" before doing this.
346  if (useFakePrimaryVertex) {
347  const IV fakeEnd(fakeVertices->end());
348  for (IV iv = fakeVertices->begin(); iv != fakeEnd; ++iv, ++index)
349  if (isAcceptableVtx(iv))
350  zAssoc.push_back(std::pair<double, unsigned>(iv->z(), index));
351  }
352 
353  // Check where the track z position fits into this sequence
354  if (!zAssoc.empty()) {
355  std::sort(zAssoc.begin(), zAssoc.end());
356  std::pair<double, unsigned> tPair(ztrack, UINT_MAX);
357  const unsigned iAbove = std::upper_bound(zAssoc.begin(), zAssoc.end(), tPair) - zAssoc.begin();
358 
359  // Check whether one of the vertices with indices
360  // iAbove or (iAbove - 1) is a primary vertex.
361  // If so, return it. Otherwise return the one
362  // with closest distance to the track.
363  unsigned ich[2] = {0U, 0U};
364  unsigned nch = 1;
365  if (iAbove) {
366  ich[0] = iAbove - 1U;
367  ich[1] = iAbove;
368  if (iAbove < zAssoc.size())
369  nch = 2;
370  }
371 
372  double dzmin = 1.0e100;
373  unsigned bestVertexNum = UINT_MAX;
374  for (unsigned icheck = 0; icheck < nch; ++icheck) {
375  const unsigned zAssocIndex = ich[icheck];
376  const unsigned vertexNum = zAssoc[zAssocIndex].second;
377 
378  if (vertexNum == numRealVertices || (!useFakePrimaryVertex && vertexNum == 0U)) {
379  bestVertexNum = vertexNum;
380  break;
381  }
382 
383  const double dz = std::abs(ztrack - zAssoc[zAssocIndex].first);
384  if (dz < dzmin) {
385  dzmin = dz;
386  bestVertexNum = vertexNum;
387  }
388  }
389 
390  foundVertex = bestVertexNum < UINT_MAX;
391  if (foundVertex) {
392  iVertex = bestVertexNum;
393  if (iVertex >= numRealVertices) {
394  *fromFakeSet = true;
395  iVertex -= numRealVertices;
396  }
397  }
398  }
399  } else {
400  // This is a simple association algorithm (from PFPileUp)
401  // extended to take fake vertices into account
402  double dzmin = 1.0e100;
403  unsigned index = 0;
404  for (IV iv = vertices->begin(); iv != vertend; ++iv, ++index)
405  if (isAcceptableVtx(iv)) {
406  const double dz = std::abs(ztrack - iv->z());
407  if (dz < dzmin) {
408  dzmin = dz;
409  iVertex = index;
410  foundVertex = true;
411  }
412  }
413 
414  if (useFakePrimaryVertex) {
415  const IV fakeEnd(fakeVertices->end());
416  index = 0;
417  for (IV iv = fakeVertices->begin(); iv != fakeEnd; ++iv, ++index)
418  if (isAcceptableVtx(iv)) {
419  const double dz = std::abs(ztrack - iv->z());
420  if (dz < dzmin) {
421  dzmin = dz;
422  iVertex = index;
423  *fromFakeSet = true;
424  foundVertex = true;
425  }
426  }
427  }
428  }
429 
430  if (foundVertex)
431  return reco::VertexRef(*fromFakeSet ? fakeVertices : vertices, iVertex);
432  }
433 
434  return reco::VertexRef();
435 }
436 
446 }
447 
448 // ------------ method called once each job just before starting event loop
450 
451 // ------------ method called once each job just after ending the event loop
453 
454 //define this as a plug-in
void setRemovalBit(reco::PFCandidate::ParticleType ptype, bool onOff)
ParticleType
particle types
Definition: PFCandidate.h:44
int32_t *__restrict__ iv
T w() const
#define DEFINE_FWK_MODULE(type)
Definition: MakerMacros.h:16
edm::EDGetTokenT< reco::VertexCollection > FakePrimaryVerticesToken
bool isRemovable(reco::PFCandidate::ParticleType ptype) const
assert(be >=bs)
key_type key() const
Accessor for product key.
Definition: Ref.h:250
constexpr uint32_t mask
Definition: gpuClustering.h:24
FFTJetPFPileupCleaner & operator=(const FFTJetPFPileupCleaner &)=delete
int iEvent
Definition: GenABIO.cc:224
bool isAcceptableVtx(reco::VertexCollection::const_iterator iv) const
FFTJetPFPileupCleaner()=delete
Abs< T >::type abs(const T &t)
Definition: Abs.h:22
edm::EDGetTokenT< reco::VertexCollection > VerticesToken
Definition: value.py:1
reco::VertexRef findSomeVertexWFakes(const edm::Handle< reco::VertexCollection > &vertices, const edm::Handle< reco::VertexCollection > &fakeVertices, const reco::PFCandidate &pfcand, bool *fromFakeSet) const
std::vector< LinkConnSpec >::const_iterator IT
void produce(edm::Event &, const edm::EventSetup &) override
bool isNull() const
Checks for null.
Definition: Ref.h:235
edm::Ref< VertexCollection > VertexRef
persistent reference to a Vertex
Definition: VertexFwd.h:13
std::vector< std::pair< double, unsigned > > zAssoc
edm::EDGetTokenT< reco::PFCandidateCollection > PFCandidatesToken
bool isValid() const
Definition: HandleBase.h:70
#define init_param(type, varname)
Particle reconstructed by the particle flow algorithm.
Definition: PFCandidate.h:41
HLT enums.
static unsigned int const shift
Log< level::Warning, false > LogWarning
def move(src, dest)
Definition: eostools.py:511
std::vector< TrackBaseRef >::const_iterator trackRef_iterator
The iteratator for the vector<TrackRef>
Definition: Vertex.h:38