CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
PATPackedCandidateProducer.cc
Go to the documentation of this file.
1 #include <string>
2 
3 
22 
23 //#define CRAZYSORT
24 
25 namespace pat {
27  public:
30 
31  virtual void produce(edm::Event&, const edm::EventSetup&);
32 
33  private:
41  // for debugging
42  float calcDxy(float dx, float dy, float phi) {
43  return - dx * std::sin(phi) + dy * std::cos(phi);
44  }
46  return p.Z()-v.Z() - ((p.X()-v.X()) * c.px() + (p.Y()-v.Y())*c.py()) * c.pz()/(c.pt()*c.pt());
47  }
48  };
49 }
50 
52  Cands_(consumes<reco::PFCandidateCollection>(iConfig.getParameter<edm::InputTag>("inputCollection"))),
53  CandsFromPVLoose_(consumes<reco::PFCandidateFwdPtrVector>(iConfig.getParameter<edm::InputTag>("inputCollectionFromPVLoose"))),
54  CandsFromPVTight_(consumes<reco::PFCandidateFwdPtrVector>(iConfig.getParameter<edm::InputTag>("inputCollectionFromPVTight"))),
55  PVs_(consumes<reco::VertexCollection>(iConfig.getParameter<edm::InputTag>("inputVertices"))),
56  PVOrigs_(consumes<reco::VertexCollection>(iConfig.getParameter<edm::InputTag>("originalVertices"))),
57  TKOrigs_(consumes<reco::TrackCollection>(iConfig.getParameter<edm::InputTag>("originalTracks"))),
58  minPtForTrackProperties_(iConfig.getParameter<double>("minPtForTrackProperties"))
59 {
60  produces< std::vector<pat::PackedCandidate> > ();
61  produces< edm::Association<pat::PackedCandidateCollection> > ();
62  produces< edm::Association<reco::PFCandidateCollection> > ();
63 }
64 
66 
68 
69 #ifdef CRAZYSORT
71  iEvent.getByLabel("selectedPatJets", jets);
72 #endif
73 
74 
76  iEvent.getByToken( Cands_, cands );
77  std::vector<reco::Candidate>::const_iterator cand;
78 
80  iEvent.getByToken( CandsFromPVLoose_, candsFromPVLoose );
82  iEvent.getByToken( CandsFromPVTight_, candsFromPVTight );
83 
84  std::vector<pat::PackedCandidate::PVAssoc> fromPV(cands->size(), pat::PackedCandidate::NoPV);
85  for (const reco::PFCandidateFwdPtr &ptr : *candsFromPVLoose) {
86  if (ptr.ptr().id() == cands.id()) {
87  fromPV[ptr.ptr().key()] = pat::PackedCandidate::PVLoose;
88  } else if (ptr.backPtr().id() == cands.id()) {
89  fromPV[ptr.backPtr().key()] = pat::PackedCandidate::PVLoose;
90  } else {
91  throw cms::Exception("Configuration", "The elements from 'inputCollectionFromPVLoose' don't point to 'inputCollection'\n");
92  }
93  }
94  for (const reco::PFCandidateFwdPtr &ptr : *candsFromPVTight) {
95  if (ptr.ptr().id() == cands.id()) {
96  fromPV[ptr.ptr().key()] = pat::PackedCandidate::PVTight;
97  } else if (ptr.backPtr().id() == cands.id()) {
98  fromPV[ptr.backPtr().key()] = pat::PackedCandidate::PVTight;
99  } else {
100  throw cms::Exception("Configuration", "The elements from 'inputCollectionFromPVTight' don't point to 'inputCollection'\n");
101  }
102  }
103 
105  iEvent.getByToken( PVOrigs_, PVOrigs );
106  const reco::Vertex & PVOrig = (*PVOrigs)[0];
108  iEvent.getByToken( PVs_, PVs );
109  reco::VertexRef PV(PVs.id());
110  math::XYZPoint PVpos;
111  if (!PVs->empty()) {
112  PV = reco::VertexRef(PVs, 0);
113  PVpos = PV->position();
114  }
115 
117  iEvent.getByToken( TKOrigs_, TKOrigs );
118 
119  std::auto_ptr< std::vector<pat::PackedCandidate> > outPtrP( new std::vector<pat::PackedCandidate> );
120  std::vector<int> mapping(cands->size());
121  std::vector<int> mappingTk(TKOrigs->size(), -1);
122 #ifdef CRAZYSORT
123  std::vector<int> jetOrder;
124  std::vector<int> jetOrderReverse;
125  for(unsigned int i=0;i<cands->size();i++) jetOrderReverse.push_back(-1);
126  for (edm::View<pat::Jet>::const_iterator it = jets->begin(), ed = jets->end(); it != ed; ++it) {
127  const pat::Jet & jet = *it;
129  for(unsigned int i=0;i<dau.size();i++)
130  {
131  if((*cands)[dau[i].key()].trackRef().isNonnull() && (*cands)[dau[i].key()].pt() > minPtForTrackProperties_){
132  jetOrder.push_back(dau[i].key());
133  jetOrderReverse[jetOrder.back()]=jetOrder.size()-1;
134  }
135  }
136  for(unsigned int i=0;i<dau.size();i++)
137  {
138  if(!((*cands)[dau[i].key()].trackRef().isNonnull() && (*cands)[dau[i].key()].pt() > minPtForTrackProperties_)){
139  jetOrder.push_back(dau[i].key());
140  jetOrderReverse[jetOrder.back()]=jetOrder.size()-1;
141  }
142  }
143 
144  }
145  for(unsigned int ic=0, nc = cands->size(); ic < nc; ++ic) {
146  if(jetOrderReverse[ic]==-1 && (*cands)[ic].trackRef().isNonnull() && (*cands)[ic].pt() > minPtForTrackProperties_)
147  {
148  jetOrder.push_back(ic);
149  jetOrderReverse[jetOrder.back()]=jetOrder.size()-1;
150  }
151 
152  }
153  //all what's left
154  for(unsigned int ic=0, nc = cands->size(); ic < nc; ++ic) {
155  if(jetOrderReverse[ic]==-1)
156  {
157  jetOrder.push_back(ic);
158  jetOrderReverse[jetOrder.back()]=jetOrder.size()-1;
159  }
160 
161  }
162 #endif //CRAZYSORT
163 
164 
165  for(unsigned int ic=0, nc = cands->size(); ic < nc; ++ic) {
166 #ifdef CRAZYSORT
167  const reco::PFCandidate &cand=(*cands)[jetOrder[ic]];
168 #else
169  const reco::PFCandidate &cand=(*cands)[ic];
170 #endif
171  float phiAtVtx = cand.phi();
172  const reco::Track *ctrack = 0;
173  if ((abs(cand.pdgId()) == 11 || cand.pdgId() == 22) && cand.gsfTrackRef().isNonnull()) {
174  ctrack = &*cand.gsfTrackRef();
175  } else if (cand.trackRef().isNonnull()) {
176  ctrack = &*cand.trackRef();
177  }
178  if (ctrack) {
179  math::XYZPoint vtx = cand.vertex();
181  vtx = ctrack->referencePoint();
182  phiAtVtx = ctrack->phi();
183  int nlost = ctrack->trackerExpectedHitsInner().numberOfLostHits();
184  if (nlost == 0) {
185  if ( ctrack->hitPattern().hasValidHitInFirstPixelBarrel()) {
187  }
188  } else {
190  }
191  outPtrP->push_back( pat::PackedCandidate(cand.polarP4(), vtx, phiAtVtx, cand.pdgId(), PV));
192 
193  // properties of the best track
194  outPtrP->back().setLostInnerHits( lostHits );
195  if(outPtrP->back().pt() > minPtForTrackProperties_) {
196  outPtrP->back().setTrackProperties(*ctrack);
197  }
198 
199  // these things are always for the CKF track
200  if(cand.trackRef().isNonnull() && PVOrig.trackWeight(cand.trackRef()) > 0.5) {
201  outPtrP->back().setFromPV(pat::PackedCandidate::PVUsedInFit);
202  } else {
203  outPtrP->back().setFromPV( fromPV[ic] );
204  }
205  outPtrP->back().setTrackHighPurity( cand.trackRef().isNonnull() && cand.trackRef()->quality(reco::Track::highPurity) );
206  if (cand.muonRef().isNonnull()) {
207  outPtrP->back().setMuonID(cand.muonRef()->isStandAloneMuon(), cand.muonRef()->isGlobalMuon());
208  }
209  } else {
210  outPtrP->push_back( pat::PackedCandidate(cand.polarP4(), PVpos, cand.phi(), cand.pdgId(), PV));
211  outPtrP->back().setFromPV( fromPV[ic] );
212  }
213 
214  mapping[ic] = ic; // trivial at the moment!
215  if (cand.trackRef().isNonnull() && cand.trackRef().id() == TKOrigs.id()) {
216  mappingTk[cand.trackRef().key()] = ic;
217  }
218 
219  }
220 
221 
223 
224  // now build the two maps
225  std::auto_ptr<edm::Association<pat::PackedCandidateCollection> > pf2pc(new edm::Association<pat::PackedCandidateCollection>(oh ));
226  std::auto_ptr<edm::Association<reco::PFCandidateCollection > > pc2pf(new edm::Association<reco::PFCandidateCollection >(cands));
229 #ifdef CRAZYSORT
230  pf2pcFiller.insert(cands, jetOrderReverse.begin(), jetOrderReverse.end());
231  pc2pfFiller.insert(oh , jetOrder.begin(), jetOrder.end());
232 #else
233  pf2pcFiller.insert(cands, mapping.begin(), mapping.end());
234  pc2pfFiller.insert(oh , mapping.begin(), mapping.end());
235 #endif
236  // include also the mapping track -> packed PFCand
237  pf2pcFiller.insert(TKOrigs, mappingTk.begin(), mappingTk.end());
238 
239  pf2pcFiller.fill();
240  pc2pfFiller.fill();
241  iEvent.put(pf2pc);
242  iEvent.put(pc2pf);
243 
244 }
245 
246 
int i
Definition: DBlmapReader.cc:9
edm::EDGetTokenT< reco::PFCandidateFwdPtrVector > CandsFromPVLoose_
float calcDz(reco::Candidate::Point p, reco::Candidate::Point v, const reco::Candidate &c)
boost::indirect_iterator< typename seq_t::const_iterator > const_iterator
Definition: View.h:81
ProductID id() const
Definition: HandleBase.cc:15
bool getByToken(EDGetToken token, Handle< PROD > &result) const
Definition: Event.h:434
virtual int pdgId() const GCC11_FINAL
PDG identifier.
#define DEFINE_FWK_MODULE(type)
Definition: MakerMacros.h:17
int numberOfLostHits() const
Definition: HitPattern.h:646
Sin< T >::type sin(const T &t)
Definition: Sin.h:22
virtual double pz() const =0
z coordinate of momentum vector
void insert(const H &h, I begin, I end)
Definition: ValueMap.h:52
std::vector< Track > TrackCollection
collection of Tracks
Definition: TrackFwd.h:10
double phi() const
azimuthal angle of momentum vector
Definition: TrackBase.h:137
std::vector< Vertex > VertexCollection
collection of Vertex objects
Definition: VertexFwd.h:9
const Point & referencePoint() const
Reference point on the track.
Definition: TrackBase.h:151
edm::EDGetTokenT< reco::TrackCollection > TKOrigs_
reco::TrackRef trackRef() const
Definition: PFCandidate.cc:429
bool isNonnull() const
Checks for non-null.
Definition: Ref.h:250
virtual float phi() const GCC11_FINAL
momentum azimuthal angle
virtual float pt() const =0
transverse momentum
int iEvent
Definition: GenABIO.cc:243
PATPackedCandidateProducer(const edm::ParameterSet &)
edm::EDGetTokenT< reco::PFCandidateCollection > Cands_
OrphanHandle< PROD > put(std::auto_ptr< PROD > product)
Put a new product.
Definition: Event.h:116
float trackWeight(const TrackBaseRef &r) const
returns the weight with which a Track has contributed to the vertex-fit.
vector< PseudoJet > jets
Cos< T >::type cos(const T &t)
Definition: Cos.h:22
Abs< T >::type abs(const T &t)
Definition: Abs.h:22
virtual const Point & vertex() const
vertex position (overwritten by PF...)
Definition: PFCandidate.cc:643
const HitPattern & trackerExpectedHitsInner() const
Access the hit pattern counting (in the Tracker) the number of expected crossed layers before the fir...
Definition: TrackBase.h:223
virtual double py() const =0
y coordinate of momentum vector
edm::EDGetTokenT< reco::VertexCollection > PVs_
edm::EDGetTokenT< reco::PFCandidateFwdPtrVector > CandsFromPVTight_
const HitPattern & hitPattern() const
Access the hit pattern, indicating in which Tracker layers the track has hits.
Definition: TrackBase.h:221
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:390
reco::MuonRef muonRef() const
Definition: PFCandidate.cc:446
virtual double px() const =0
x coordinate of momentum vector
LostInnerHits
Enumerator specifying the.
std::vector< CandidatePtr > daughters
collection of references to daughters
std::vector< reco::PFCandidate > PFCandidateCollection
collection of PFCandidates
virtual const PolarLorentzVector & polarP4() const GCC11_FINAL
four-momentum Lorentz vector
std::vector< PFCandidateFwdPtr > PFCandidateFwdPtrVector
vector of &quot;forward&quot; reference
XYZPointD XYZPoint
point in space with cartesian internal representation
Definition: Point3D.h:12
key_type key() const
Accessor for product key.
Definition: Ref.h:266
Analysis-level calorimeter jet class.
Definition: Jet.h:73
float calcDxy(float dx, float dy, float phi)
Particle reconstructed by the particle flow algorithm.
Definition: PFCandidate.h:38
list key
Definition: combine.py:13
virtual void produce(edm::Event &, const edm::EventSetup &)
reco::GsfTrackRef gsfTrackRef() const
Definition: PFCandidate.cc:467
const daughters & daughterPtrVector() const
references to daughtes
math::XYZPoint Point
point in the space
Definition: Candidate.h:45
ProductID id() const
Accessor for product ID.
Definition: Ref.h:256
bool hasValidHitInFirstPixelBarrel() const
Definition: HitPattern.cc:181
edm::EDGetTokenT< reco::VertexCollection > PVOrigs_
Definition: DDAxes.h:10