CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
PFPileUp.cc
Go to the documentation of this file.
2 
6 
8 
9 // #include "FWCore/MessageLogger/interface/MessageLogger.h"
12 
14 
15 using namespace std;
16 using namespace edm;
17 using namespace reco;
18 
20 
21 
22 
23  inputTagPFCandidates_
24  = iConfig.getParameter<InputTag>("PFCandidates");
25 
26  inputTagVertices_
27  = iConfig.getParameter<InputTag>("Vertices");
28 
29  enable_ = iConfig.getParameter<bool>("Enable");
30 
31  verbose_ =
32  iConfig.getUntrackedParameter<bool>("verbose",false);
33 
34 
35  if ( iConfig.exists("checkClosestZVertex") ) {
36  checkClosestZVertex_ = iConfig.getParameter<bool>("checkClosestZVertex");
37  } else {
38  checkClosestZVertex_ = false;
39  }
40 
41 
42  produces<reco::PileUpPFCandidateCollection>();
43 
44 }
45 
46 
47 
49 
50 
51 
53 
54 
56  const EventSetup& iSetup) {
57 
58 // LogDebug("PFPileUp")<<"START event: "<<iEvent.id().event()
59 // <<" in run "<<iEvent.id().run()<<endl;
60 
61 
62  // get PFCandidates
63 
64  auto_ptr< reco::PileUpPFCandidateCollection >
65  pOutput( new reco::PileUpPFCandidateCollection );
66 
67  if(enable_) {
68 
70  iEvent.getByLabel( inputTagPFCandidates_, pfCandidates);
71 
72 
73  // get vertices
74 
75  Handle<VertexCollection> vertices;
76  iEvent.getByLabel( inputTagVertices_, vertices);
77 
78  for( unsigned i=0; i<pfCandidates->size(); i++ ) {
79 
80  // const reco::PFCandidate& cand = (*pfCandidates)[i];
81  PFCandidatePtr candptr(pfCandidates, i);
82 
83  // PFCandidateRef pfcandref(pfCandidates,i);
84 
85  VertexRef vertexref;
86 
87  switch( candptr->particleId() ) {
88  case PFCandidate::h:
89  vertexref = chargedHadronVertex( vertices, *candptr );
90  break;
91  default:
92  continue;
93  }
94 
95  // no associated vertex, or primary vertex
96  // not pile-up
97  if( vertexref.isNull() ||
98  vertexref.key()==0 ) continue;
99 
100  pOutput->push_back( PileUpPFCandidate( candptr, vertexref ) );
101  pOutput->back().setSourceCandidatePtr( candptr );
102  }
103  }
104  iEvent.put( pOutput );
105 
106 }
107 
108 
109 
110 VertexRef
111 PFPileUp::chargedHadronVertex( const Handle<VertexCollection>& vertices, const PFCandidate& pfcand ) const {
112 
113 
114  reco::TrackBaseRef trackBaseRef( pfcand.trackRef() );
115 
116  size_t iVertex = 0;
117  unsigned index=0;
118  unsigned nFoundVertex = 0;
119  typedef reco::VertexCollection::const_iterator IV;
120  float bestweight=0;
121  for(IV iv=vertices->begin(); iv!=vertices->end(); ++iv, ++index) {
122 
123  const reco::Vertex& vtx = *iv;
124 
126 
127  // loop on tracks in vertices
128  for(IT iTrack=vtx.tracks_begin();
129  iTrack!=vtx.tracks_end(); ++iTrack) {
130 
131  const reco::TrackBaseRef& baseRef = *iTrack;
132 
133  // one of the tracks in the vertex is the same as
134  // the track considered in the function
135  float w = vtx.trackWeight(baseRef);
136  if(baseRef == trackBaseRef ) {
137  //select the vertex for which the track has the highest weight
138  if (w > bestweight){
139  bestweight=w;
140  iVertex=index;
141  nFoundVertex++;
142  }
143  }
144  }
145  }
146 
147  if (nFoundVertex>0){
148  if (nFoundVertex!=1)
149  edm::LogWarning("TrackOnTwoVertex")<<"a track is shared by at least two verteces. Used to be an assert";
150  return VertexRef( vertices, iVertex);
151  }
152  // no vertex found with this track.
153 
154  // optional: as a secondary solution, associate the closest vertex in z
155  if ( checkClosestZVertex_ ) {
156 
157  double dzmin = 10000;
158  double ztrack = pfcand.vertex().z();
159  bool foundVertex = false;
160  index = 0;
161  for(IV iv=vertices->begin(); iv!=vertices->end(); ++iv, ++index) {
162 
163  double dz = fabs(ztrack - iv->z());
164  if(dz<dzmin) {
165  dzmin = dz;
166  iVertex = index;
167  foundVertex = true;
168  }
169  }
170 
171  if( foundVertex )
172  return VertexRef( vertices, iVertex);
173 
174  }
175 
176 
177  return VertexRef();
178 }
179 
180 
T getParameter(std::string const &) const
T getUntrackedParameter(std::string const &, T const &) const
int i
Definition: DBlmapReader.cc:9
virtual void produce(edm::Event &, const edm::EventSetup &)
Definition: PFPileUp.cc:55
trackRef_iterator tracks_end() const
last iterator over tracks
Definition: Vertex.cc:45
bool exists(std::string const &parameterName) const
checks if a parameter exists
std::vector< PFCandidatePtr > pfCandidates(const PFJet &jet, int particleId, bool sort=true)
reco::TrackRef trackRef() const
Definition: PFCandidate.cc:331
int iEvent
Definition: GenABIO.cc:243
bool isNull() const
Checks for null.
Definition: Ref.h:246
OrphanHandle< PROD > put(std::auto_ptr< PROD > product)
Put a new product.
Definition: Event.h:84
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:538
~PFPileUp()
Definition: PFPileUp.cc:48
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:355
std::vector< reco::PileUpPFCandidate > PileUpPFCandidateCollection
collection of PileUpPFCandidates
virtual void beginJob()
Definition: PFPileUp.cc:52
key_type key() const
Accessor for product key.
Definition: Ref.h:265
Particle reconstructed by the particle flow algorithm.
Definition: PFCandidate.h:33
std::vector< TrackBaseRef >::const_iterator trackRef_iterator
The iteratator for the vector&lt;TrackRef&gt;
Definition: Vertex.h:38
PFPileUp(const edm::ParameterSet &)
Definition: PFPileUp.cc:19
trackRef_iterator tracks_begin() const
first iterator over tracks
Definition: Vertex.cc:40
The Signals That Services Can Subscribe To This is based on ActivityRegistry h
Helper function to determine trigger accepts.
Definition: Activities.doc:4
Particle reconstructed by the particle flow algorithm.
reco::VertexRef chargedHadronVertex(const edm::Handle< reco::VertexCollection > &vertices, const reco::PFCandidate &pfcand) const
Definition: PFPileUp.cc:111