CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
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 // $Id: FFTJetPFPileupCleaner.cc,v 1.1 2011/07/15 04:26:34 igv Exp $
17 //
18 //
19 
20 // framework include files
25 
27 
30 
33 
36 
37 #define init_param(type, varname) varname (ps.getParameter< type >( #varname ))
38 
39 //
40 // class declaration
41 //
43 {
44 public:
47 
48 protected:
49  // methods
50  void beginJob();
51  void produce(edm::Event&, const edm::EventSetup&);
52  void endJob();
53 
54 private:
58 
60  void setRemovalBit(reco::PFCandidate::ParticleType ptype, bool onOff);
61  void buildRemovalMask();
62 
65  const reco::PFCandidate& pfcand) const;
66 
69 
70  // The following, if true, will cause association of a candidate
71  // with some vertex no matter what
73 
74  // The following, if true, will cause removal of candidates
75  // associated with the main vertex
77 
78  // The following will tell us to remove candidates not associated
79  // with any vertex
81 
82  // Do we want to reverse the decision?
84 
85  // Flags for removing things. See PFCandidate header
86  // for particle types.
87  bool remove_X; // undefined
88  bool remove_h; // charged hadron
89  bool remove_e; // electron
90  bool remove_mu; // muon
91  bool remove_gamma; // photon
92  bool remove_h0; // neutral hadron
93  bool remove_h_HF; // HF tower identified as a hadron
94  bool remove_egamma_HF; // HF tower identified as an EM particle
95 
96  // Mask for removing things
97  unsigned removalMask;
98 
99  // Min and max eta for keeping things
100  double etaMin;
101  double etaMax;
102 
103  // Cut for the vertex Ndof
105 };
106 
107 //
108 // constructors and destructor
109 //
111  : init_param(edm::InputTag, PFCandidates),
112  init_param(edm::InputTag, Vertices),
113  init_param(bool, checkClosestZVertex),
114  init_param(bool, removeMainVertex),
115  init_param(bool, removeUnassociated),
116  init_param(bool, reverseRemovalDecision),
117  init_param(bool, remove_X ),
118  init_param(bool, remove_h ),
119  init_param(bool, remove_e ),
120  init_param(bool, remove_mu ),
121  init_param(bool, remove_gamma ),
122  init_param(bool, remove_h0 ),
123  init_param(bool, remove_h_HF ),
124  init_param(bool, remove_egamma_HF),
125  removalMask(0),
126  init_param(double, etaMin),
127  init_param(double, etaMax),
128  init_param(double, vertexNdofCut)
129 {
131  produces<reco::PFCandidateCollection>();
132 }
133 
134 
136 {
137 }
138 
139 
140 // ------------ method called to produce the data ------------
142  edm::Event& iEvent, const edm::EventSetup& iSetup)
143 {
144  // get PFCandidates
145  std::auto_ptr<reco::PFCandidateCollection>
146  pOutput(new reco::PFCandidateCollection);
147 
149  iEvent.getByLabel(PFCandidates, pfCandidates);
150 
151  // get vertices
153  iEvent.getByLabel(Vertices, vertices);
154 
155  const unsigned ncand = pfCandidates->size();
156  for (unsigned i=0; i<ncand; ++i)
157  {
158  reco::PFCandidatePtr candptr(pfCandidates, i);
159  bool remove = false;
160 
161  if (isRemovable(candptr->particleId()))
162  {
163  reco::VertexRef vertexref(findSomeVertex(vertices, *candptr));
164  if (vertexref.isNull())
165  remove = removeUnassociated;
166  else if (vertexref.key() == 0)
167  remove = removeMainVertex;
168  else
169  remove = true;
170  }
171 
172  // Check the eta range
173  if (!remove)
174  {
175  const double eta = candptr->p4().Eta();
176  remove = eta < etaMin || eta > etaMax;
177  }
178 
179  // Should we remember this candidate?
181  remove = !remove;
182  if (!remove)
183  {
184  const reco::PFCandidate& cand = (*pfCandidates)[i];
185  pOutput->push_back(cand);
186  pOutput->back().setSourceCandidatePtr(candptr);
187  }
188  }
189 
190  iEvent.put(pOutput);
191 }
192 
193 
195  const reco::PFCandidate::ParticleType ptype) const
196 {
197  const unsigned shift = static_cast<unsigned>(ptype);
198  assert(shift < 32U);
199  return removalMask & (1U << shift);
200 }
201 
202 
204  const reco::PFCandidate::ParticleType ptype, const bool value)
205 {
206  const unsigned shift = static_cast<unsigned>(ptype);
207  assert(shift < 32U);
208  const unsigned mask = (1U << shift);
209  if (value)
210  removalMask |= mask;
211  else
212  removalMask &= ~mask;
213 }
214 
215 
216 // The following essentially duplicates the code in PFPileUp.cc,
217 // with added cut on ndof.
219  const edm::Handle<reco::VertexCollection>& vertices,
220  const reco::PFCandidate& pfcand) const
221 {
222  typedef reco::VertexCollection::const_iterator IV;
224 
225  reco::TrackBaseRef trackBaseRef(pfcand.trackRef());
226 
227  size_t iVertex = 0;
228  unsigned index = 0;
229  unsigned nFoundVertex = 0;
230  double bestweight = 0;
231 
232  const IV vertend(vertices->end());
233  for (IV iv=vertices->begin(); iv!=vertend; ++iv, ++index)
234  {
235  const double ndof = iv->ndof();
236  if (!iv->isFake() && ndof > vertexNdofCut)
237  {
238  const reco::Vertex& vtx = *iv;
239 
240  // loop on tracks in vertices
241  IT trackend(vtx.tracks_end());
242  for (IT iTrack=vtx.tracks_begin(); iTrack!=trackend; ++iTrack)
243  {
244  const reco::TrackBaseRef& baseRef = *iTrack;
245 
246  // one of the tracks in the vertex is the same as
247  // the track considered in the function
248  if (baseRef == trackBaseRef)
249  {
250  // select the vertex for which the track has the highest weight
251  const double w = vtx.trackWeight(baseRef);
252  if (w > bestweight)
253  {
254  bestweight=w;
255  iVertex=index;
256  nFoundVertex++;
257  }
258  }
259  }
260  }
261  }
262 
263  if (nFoundVertex > 0)
264  {
265  if (nFoundVertex != 1)
266  edm::LogWarning("TrackOnTwoVertex")
267  << "a track is shared by at least two vertices. "
268  << "Used to be an assert";
269  return reco::VertexRef(vertices, iVertex);
270  }
271 
272  // optional: as a secondary solution, associate the closest vertex in z
273  if (checkClosestZVertex)
274  {
275  double dzmin = 10000;
276  double ztrack = pfcand.vertex().z();
277  bool foundVertex = false;
278  index = 0;
279  for (IV iv=vertices->begin(); iv!=vertend; ++iv, ++index)
280  {
281  const double ndof = iv->ndof();
282  if (!iv->isFake() && ndof > vertexNdofCut)
283  {
284  const double dz = fabs(ztrack - iv->z());
285  if (dz < dzmin)
286  {
287  dzmin = dz;
288  iVertex = index;
289  foundVertex = true;
290  }
291  }
292  }
293 
294  if (foundVertex)
295  return reco::VertexRef(vertices, iVertex);
296  }
297 
298  return reco::VertexRef();
299 }
300 
301 
303 {
312 }
313 
314 
315 // ------------ method called once each job just before starting event loop
317 {
318 }
319 
320 
321 // ------------ method called once each job just after ending the event loop
323 {
324 }
325 
326 
327 //define this as a plug-in
int i
Definition: DBlmapReader.cc:9
void setRemovalBit(reco::PFCandidate::ParticleType ptype, bool onOff)
ParticleType
particle types
Definition: PFCandidate.h:38
void produce(edm::Event &, const edm::EventSetup &)
trackRef_iterator tracks_end() const
last iterator over tracks
Definition: Vertex.cc:45
#define DEFINE_FWK_MODULE(type)
Definition: MakerMacros.h:17
void setSourceCandidatePtr(const PFCandidatePtr &ptr)
Definition: PFCandidate.h:111
T eta() const
std::vector< PFCandidatePtr > pfCandidates(const PFJet &jet, int particleId, bool sort=true)
reco::TrackRef trackRef() const
Definition: PFCandidate.cc:339
bool isRemovable(reco::PFCandidate::ParticleType ptype) const
int iEvent
Definition: GenABIO.cc:243
bool isNull() const
Checks for null.
Definition: Ref.h:247
OrphanHandle< PROD > put(std::auto_ptr< PROD > product)
Put a new product.
Definition: Event.h:85
float trackWeight(const TrackBaseRef &r) const
returns the weight with which a Track has contributed to the vertex-fit.
virtual const Point & vertex() const
vertex position
Definition: PFCandidate.cc:546
std::vector< LinkConnSpec >::const_iterator IT
edm::Ref< VertexCollection > VertexRef
persistent reference to a Vertex
Definition: VertexFwd.h:13
bool getByLabel(InputTag const &tag, Handle< PROD > &result) const
Definition: Event.h:356
std::vector< reco::PFCandidate > PFCandidateCollection
collection of PFCandidates
key_type key() const
Accessor for product key.
Definition: Ref.h:266
#define init_param(type, varname)
Particle reconstructed by the particle flow algorithm.
Definition: PFCandidate.h:33
FFTJetPFPileupCleaner & operator=(const FFTJetPFPileupCleaner &)
std::vector< VertexType > Vertices
std::vector< TrackBaseRef >::const_iterator trackRef_iterator
The iteratator for the vector&lt;TrackRef&gt;
Definition: Vertex.h:38
static unsigned int const shift
reco::VertexRef findSomeVertex(const edm::Handle< reco::VertexCollection > &vertices, const reco::PFCandidate &pfcand) const
trackRef_iterator tracks_begin() const
first iterator over tracks
Definition: Vertex.cc:40
T w() const
tuple PFCandidates