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 /*#include "TrackingTools/TrajectoryState/interface/TrajectoryStateTransform.h"
24 #include "TrackingTools/GeomPropagators/interface/AnalyticalImpactPointExtrapolator.h"
25 #include "MagneticField/Engine/interface/MagneticField.h"
26 #include "MagneticField/Records/interface/IdealMagneticFieldRecord.h"
27 #include "TrackingTools/TrajectoryState/interface/TrajectoryStateOnSurface.h"
28 #include "RecoVertex/VertexPrimitives/interface/ConvertToFromReco.h"
29 */
30 //#define CRAZYSORT
31 
32 namespace pat {
34  public:
37 
38  virtual void produce(edm::Event&, const edm::EventSetup&);
39 
40  private:
48  // for debugging
49  float calcDxy(float dx, float dy, float phi) {
50  return - dx * std::sin(phi) + dy * std::cos(phi);
51  }
53  return p.Z()-v.Z() - ((p.X()-v.X()) * c.px() + (p.Y()-v.Y())*c.py()) * c.pz()/(c.pt()*c.pt());
54  }
55  };
56 }
57 
59  Cands_(consumes<reco::PFCandidateCollection>(iConfig.getParameter<edm::InputTag>("inputCollection"))),
60  CandsFromPVLoose_(consumes<reco::PFCandidateFwdPtrVector>(iConfig.getParameter<edm::InputTag>("inputCollectionFromPVLoose"))),
61  CandsFromPVTight_(consumes<reco::PFCandidateFwdPtrVector>(iConfig.getParameter<edm::InputTag>("inputCollectionFromPVTight"))),
62  PVs_(consumes<reco::VertexCollection>(iConfig.getParameter<edm::InputTag>("inputVertices"))),
63  PVOrigs_(consumes<reco::VertexCollection>(iConfig.getParameter<edm::InputTag>("originalVertices"))),
64  TKOrigs_(consumes<reco::TrackCollection>(iConfig.getParameter<edm::InputTag>("originalTracks"))),
65  minPtForTrackProperties_(iConfig.getParameter<double>("minPtForTrackProperties"))
66 {
67  produces< std::vector<pat::PackedCandidate> > ();
68  produces< edm::Association<pat::PackedCandidateCollection> > ();
69  produces< edm::Association<reco::PFCandidateCollection> > ();
70 }
71 
73 
75 
76 #ifdef CRAZYSORT
78  iEvent.getByLabel("selectedPatJets", jets);
79 #endif
80 
81 /* edm::ESHandle<MagneticField> magneticField;
82  iSetup.get<IdealMagneticFieldRecord>().get(magneticField);
83  AnalyticalImpactPointExtrapolator extrapolator(&*magneticField);
84 */
86  iEvent.getByToken( Cands_, cands );
87  std::vector<reco::Candidate>::const_iterator cand;
88 
90  iEvent.getByToken( CandsFromPVLoose_, candsFromPVLoose );
92  iEvent.getByToken( CandsFromPVTight_, candsFromPVTight );
93 
94  std::vector<pat::PackedCandidate::PVAssoc> fromPV(cands->size(), pat::PackedCandidate::NoPV);
95  for (const reco::PFCandidateFwdPtr &ptr : *candsFromPVLoose) {
96  if (ptr.ptr().id() == cands.id()) {
97  fromPV[ptr.ptr().key()] = pat::PackedCandidate::PVLoose;
98  } else if (ptr.backPtr().id() == cands.id()) {
99  fromPV[ptr.backPtr().key()] = pat::PackedCandidate::PVLoose;
100  } else {
101  throw cms::Exception("Configuration", "The elements from 'inputCollectionFromPVLoose' don't point to 'inputCollection'\n");
102  }
103  }
104  for (const reco::PFCandidateFwdPtr &ptr : *candsFromPVTight) {
105  if (ptr.ptr().id() == cands.id()) {
106  fromPV[ptr.ptr().key()] = pat::PackedCandidate::PVTight;
107  } else if (ptr.backPtr().id() == cands.id()) {
108  fromPV[ptr.backPtr().key()] = pat::PackedCandidate::PVTight;
109  } else {
110  throw cms::Exception("Configuration", "The elements from 'inputCollectionFromPVTight' don't point to 'inputCollection'\n");
111  }
112  }
113 
115  iEvent.getByToken( PVOrigs_, PVOrigs );
116  const reco::Vertex & PVOrig = (*PVOrigs)[0];
118  iEvent.getByToken( PVs_, PVs );
119  reco::VertexRef PV(PVs.id());
120  math::XYZPoint PVpos;
121  if (!PVs->empty()) {
122  PV = reco::VertexRef(PVs, 0);
123  PVpos = PV->position();
124  }
125 
127  iEvent.getByToken( TKOrigs_, TKOrigs );
128 
129  std::auto_ptr< std::vector<pat::PackedCandidate> > outPtrP( new std::vector<pat::PackedCandidate> );
130  std::vector<int> mapping(cands->size());
131  std::vector<int> mappingTk(TKOrigs->size(), -1);
132 #ifdef CRAZYSORT
133  std::vector<int> jetOrder;
134  std::vector<int> jetOrderReverse;
135  for(unsigned int i=0;i<cands->size();i++) jetOrderReverse.push_back(-1);
136  for (edm::View<pat::Jet>::const_iterator it = jets->begin(), ed = jets->end(); it != ed; ++it) {
137  const pat::Jet & jet = *it;
139  for(unsigned int i=0;i<dau.size();i++)
140  {
141  if((*cands)[dau[i].key()].trackRef().isNonnull() && (*cands)[dau[i].key()].pt() > minPtForTrackProperties_){
142  jetOrder.push_back(dau[i].key());
143  jetOrderReverse[jetOrder.back()]=jetOrder.size()-1;
144  }
145  }
146  for(unsigned int i=0;i<dau.size();i++)
147  {
148  if(!((*cands)[dau[i].key()].trackRef().isNonnull() && (*cands)[dau[i].key()].pt() > minPtForTrackProperties_)){
149  jetOrder.push_back(dau[i].key());
150  jetOrderReverse[jetOrder.back()]=jetOrder.size()-1;
151  }
152  }
153 
154  }
155  for(unsigned int ic=0, nc = cands->size(); ic < nc; ++ic) {
156  if(jetOrderReverse[ic]==-1 && (*cands)[ic].trackRef().isNonnull() && (*cands)[ic].pt() > minPtForTrackProperties_)
157  {
158  jetOrder.push_back(ic);
159  jetOrderReverse[jetOrder.back()]=jetOrder.size()-1;
160  }
161 
162  }
163  //all what's left
164  for(unsigned int ic=0, nc = cands->size(); ic < nc; ++ic) {
165  if(jetOrderReverse[ic]==-1)
166  {
167  jetOrder.push_back(ic);
168  jetOrderReverse[jetOrder.back()]=jetOrder.size()-1;
169  }
170 
171  }
172 #endif //CRAZYSORT
173 
174 
175  for(unsigned int ic=0, nc = cands->size(); ic < nc; ++ic) {
176 #ifdef CRAZYSORT
177  const reco::PFCandidate &cand=(*cands)[jetOrder[ic]];
178 #else
179  const reco::PFCandidate &cand=(*cands)[ic];
180 #endif
181  float phiAtVtx = cand.phi();
182  const reco::Track *ctrack = 0;
183  if ((abs(cand.pdgId()) == 11 || cand.pdgId() == 22) && cand.gsfTrackRef().isNonnull()) {
184  ctrack = &*cand.gsfTrackRef();
185  } else if (cand.trackRef().isNonnull()) {
186  ctrack = &*cand.trackRef();
187  }
188  if (ctrack) {
189  math::XYZPoint vtx = cand.vertex();
191 
192 // TrajectoryStateOnSurface tsos = extrapolator.extrapolate(trajectoryStateTransform::initialFreeState(*ctrack,&*magneticField), RecoVertex::convertPos(PV->position()));
193 // vtx = tsos.globalPosition();
194 // phiAtVtx = tsos.globalDirection().phi();
195  vtx = ctrack->referencePoint();
196  phiAtVtx = ctrack->phi();
197  int nlost = ctrack->trackerExpectedHitsInner().numberOfLostHits();
198  if (nlost == 0) {
199  if ( ctrack->hitPattern().hasValidHitInFirstPixelBarrel()) {
201  }
202  } else {
204  }
205 
206 
207  outPtrP->push_back( pat::PackedCandidate(cand.polarP4(), vtx, phiAtVtx, cand.pdgId(), PV));
208 
209  // properties of the best track
210  outPtrP->back().setLostInnerHits( lostHits );
211  if(outPtrP->back().pt() > minPtForTrackProperties_) {
212  outPtrP->back().setTrackProperties(*ctrack);
213  //outPtrP->back().setTrackProperties(*ctrack,tsos.curvilinearError());
214  }
215 
216  // these things are always for the CKF track
217  if(cand.trackRef().isNonnull() && PVOrig.trackWeight(cand.trackRef()) > 0.5) {
218  outPtrP->back().setFromPV(pat::PackedCandidate::PVUsedInFit);
219  } else {
220  outPtrP->back().setFromPV( fromPV[ic] );
221  }
222  outPtrP->back().setTrackHighPurity( cand.trackRef().isNonnull() && cand.trackRef()->quality(reco::Track::highPurity) );
223  if (cand.muonRef().isNonnull()) {
224  outPtrP->back().setMuonID(cand.muonRef()->isStandAloneMuon(), cand.muonRef()->isGlobalMuon());
225  }
226  } else {
227  outPtrP->push_back( pat::PackedCandidate(cand.polarP4(), PVpos, cand.phi(), cand.pdgId(), PV));
228  outPtrP->back().setFromPV( fromPV[ic] );
229  }
230 
231  mapping[ic] = ic; // trivial at the moment!
232  if (cand.trackRef().isNonnull() && cand.trackRef().id() == TKOrigs.id()) {
233  mappingTk[cand.trackRef().key()] = ic;
234  }
235 
236  }
237 
238 
240 
241  // now build the two maps
242  std::auto_ptr<edm::Association<pat::PackedCandidateCollection> > pf2pc(new edm::Association<pat::PackedCandidateCollection>(oh ));
243  std::auto_ptr<edm::Association<reco::PFCandidateCollection > > pc2pf(new edm::Association<reco::PFCandidateCollection >(cands));
246 #ifdef CRAZYSORT
247  pf2pcFiller.insert(cands, jetOrderReverse.begin(), jetOrderReverse.end());
248  pc2pfFiller.insert(oh , jetOrder.begin(), jetOrder.end());
249 #else
250  pf2pcFiller.insert(cands, mapping.begin(), mapping.end());
251  pc2pfFiller.insert(oh , mapping.begin(), mapping.end());
252 #endif
253  // include also the mapping track -> packed PFCand
254  pf2pcFiller.insert(TKOrigs, mappingTk.begin(), mappingTk.end());
255 
256  pf2pcFiller.fill();
257  pc2pfFiller.fill();
258  iEvent.put(pf2pc);
259  iEvent.put(pc2pf);
260 
261 }
262 
263 
int i
Definition: DBlmapReader.cc:9
virtual int pdgId() const
PDG identifier.
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
#define DEFINE_FWK_MODULE(type)
Definition: MakerMacros.h:17
virtual float phi() const
momentum azimuthal angle
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 pt() const =0
transverse momentum
int iEvent
Definition: GenABIO.cc:230
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
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
virtual const PolarLorentzVector & polarP4() const
four-momentum Lorentz vector
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