CMS 3D CMS Logo

PATLostTracks.cc
Go to the documentation of this file.
1 #include <string>
2 
3 
27 
28 namespace {
29  bool passesQuality(const reco::Track& trk,const std::vector<reco::TrackBase::TrackQuality>& allowedQuals){
30  for(const auto& qual : allowedQuals){
31  if(trk.quality(qual)) return true;
32  }
33  return false;
34  }
35 }
36 
37 namespace pat {
39  public:
40  explicit PATLostTracks(const edm::ParameterSet&);
41  ~PATLostTracks() override;
42 
43  void produce(edm::StreamID, edm::Event&, const edm::EventSetup&) const override;
44 
45  private:
46  enum class TrkStatus {
47  NOTUSED=0,
48  PFCAND,
50  PFELECTRON,
51  PFPOSITRON,
52  VTX
53  };
54  bool passTrkCuts(const reco::Track& tr)const;
55  void addPackedCandidate(std::vector<pat::PackedCandidate>& cands,
56  const reco::TrackRef& trk,
57  const reco::VertexRef& pvSlimmed,
58  const reco::VertexRefProd& pvSlimmedColl,
59  const reco::Vertex& pvOrig,
60  const TrkStatus trkStatus)const;
61 
62  private:
71  const double minPt_;
72  const double minHits_;
73  const double minPixelHits_;
74  const double minPtToStoreProps_;
75  const int covarianceVersion_;
76  const int covarianceSchema_;
77  std::vector<reco::TrackBase::TrackQuality> qualsToAutoAccept_;
78  };
79 }
80 
82  cands_(consumes<reco::PFCandidateCollection>(iConfig.getParameter<edm::InputTag>("inputCandidates"))),
83  map_(consumes<edm::Association<pat::PackedCandidateCollection> >(iConfig.getParameter<edm::InputTag>("packedPFCandidates"))),
84  tracks_(consumes<reco::TrackCollection>(iConfig.getParameter<edm::InputTag>("inputTracks"))),
85  vertices_(consumes<reco::VertexCollection>(iConfig.getParameter<edm::InputTag>("secondaryVertices"))),
86  kshorts_(consumes<reco::VertexCompositeCandidateCollection>(iConfig.getParameter<edm::InputTag>("kshorts"))),
87  lambdas_(consumes<reco::VertexCompositeCandidateCollection>(iConfig.getParameter<edm::InputTag>("lambdas"))),
88  pv_(consumes<reco::VertexCollection>(iConfig.getParameter<edm::InputTag>("primaryVertices"))),
89  pvOrigs_(consumes<reco::VertexCollection>(iConfig.getParameter<edm::InputTag>("originalVertices"))),
90  minPt_(iConfig.getParameter<double>("minPt")),
91  minHits_(iConfig.getParameter<uint32_t>("minHits")),
92  minPixelHits_(iConfig.getParameter<uint32_t>("minPixelHits")) ,
93  minPtToStoreProps_(iConfig.getParameter<double>("minPtToStoreProps")),
94  covarianceVersion_(iConfig.getParameter<int >("covarianceVersion")),
95  covarianceSchema_(iConfig.getParameter<int >("covarianceSchema"))
96 
97 {
98  std::vector<std::string> trkQuals(iConfig.getParameter<std::vector<std::string> >("qualsToAutoAccept"));
99  std::transform(trkQuals.begin(),trkQuals.end(),std::back_inserter(qualsToAutoAccept_),reco::TrackBase::qualityByName);
100 
102  std::ostringstream msg;
103  msg<<" PATLostTracks has a quality requirement which resolves to undefQuality. This usually means a typo and is therefore treated a config error\nquality requirements:\n ";
104  for(const auto& trkQual : trkQuals) msg <<trkQual<<" ";
105  throw cms::Exception("Configuration") << msg.str();
106  }
107 
108  produces< std::vector<reco::Track> > ();
109  produces< std::vector<pat::PackedCandidate> > (); produces< std::vector<pat::PackedCandidate> > ("eleTracks");
110  produces< edm::Association<pat::PackedCandidateCollection> > ();
111 }
112 
114 
116 
118  iEvent.getByToken( cands_, cands );
119 
121  iEvent.getByToken(map_,pf2pc);
122 
124  iEvent.getByToken( tracks_, tracks );
125 
127  iEvent.getByToken( vertices_, vertices );
128 
130  iEvent.getByToken( kshorts_, kshorts );
132  iEvent.getByToken( lambdas_, lambdas );
133 
135  iEvent.getByToken( pv_, pvs );
136  reco::VertexRef pv(pvs.id());
137  reco::VertexRefProd pvRefProd(pvs);
138  if (!pvs->empty()) {
139  pv = reco::VertexRef(pvs, 0);
140  }
142  iEvent.getByToken( pvOrigs_, pvOrigs );
143  const reco::Vertex & pvOrig = (*pvOrigs)[0];
144 
145  auto outPtrTrks = std::make_unique<std::vector<reco::Track>>();
146  auto outPtrTrksAsCands = std::make_unique<std::vector<pat::PackedCandidate>>();
147  auto outPtrEleTrksAsCands = std::make_unique<std::vector<pat::PackedCandidate>>();
148 
149  std::vector<TrkStatus> trkStatus(tracks->size(),TrkStatus::NOTUSED);
150  //Mark all tracks used in candidates
151  //check if packed candidates are storing the tracks by seeing if number of hits >0
152  //currently we dont use that information though
153  //electrons will never store their track (they store the GSF track)
154  for(unsigned int ic=0, nc = cands->size(); ic < nc; ++ic) {
156  const reco::PFCandidate &cand=(*cands)[ic];
157  if(cand.charge() && cand.trackRef().isNonnull() && cand.trackRef().id() == tracks.id() ) {
158 
159  if(cand.pdgId()==11) trkStatus[cand.trackRef().key()]=TrkStatus::PFELECTRON;
160  else if(cand.pdgId()==-11) trkStatus[cand.trackRef().key()]=TrkStatus::PFPOSITRON;
161  else if((*pf2pc)[r]->numberOfHits() > 0) trkStatus[cand.trackRef().key()]=TrkStatus::PFCAND;
162  else trkStatus[cand.trackRef().key()]=TrkStatus::PFCANDNOTRKPROPS;
163  }
164  }
165 
166  //Mark all tracks used in secondary vertices
167  for(const auto& secVert : *vertices){
168  for(auto trkIt = secVert.tracks_begin();trkIt!=secVert.tracks_end();trkIt++){
169  if(trkStatus[trkIt->key()]==TrkStatus::NOTUSED) trkStatus[trkIt->key()]=TrkStatus::VTX;
170  }
171  }
172  for(const auto& v0 : *kshorts){
173  for(size_t dIdx=0;dIdx<v0.numberOfDaughters(); dIdx++){
174  size_t key= (dynamic_cast<const reco::RecoChargedCandidate*>(v0.daughter(dIdx)))->track().key();
175  if(trkStatus[key]==TrkStatus::NOTUSED) trkStatus[key]=TrkStatus::VTX;
176  }
177  }
178  for(const auto& v0 : *lambdas){
179  for(size_t dIdx=0;dIdx<v0.numberOfDaughters(); dIdx++){
180  size_t key= (dynamic_cast<const reco::RecoChargedCandidate*>(v0.daughter(dIdx)))->track().key();
181  if(trkStatus[key]==TrkStatus::NOTUSED) trkStatus[key]=TrkStatus::VTX;
182  }
183  }
184  std::vector<int> mapping(tracks->size(),-1);
185  int lostTrkIndx=0;
186  for(unsigned int trkIndx=0; trkIndx < tracks->size(); trkIndx++){
187  reco::TrackRef trk(tracks,trkIndx);
188  if( trkStatus[trkIndx] == TrkStatus::VTX ||
189  (trkStatus[trkIndx]==TrkStatus::NOTUSED && passTrkCuts(*trk)) ) {
190 
191  outPtrTrks->emplace_back(*trk);
192  addPackedCandidate(*outPtrTrksAsCands,trk,pv,pvRefProd,pvOrig,trkStatus[trkIndx]);
193 
194  //for creating the reco::Track -> pat::PackedCandidate map
195  //not done for the lostTrack:eleTracks collection
196  mapping[trkIndx]=lostTrkIndx;
197  lostTrkIndx++;
198  }else if( (trkStatus[trkIndx]==TrkStatus::PFELECTRON || trkStatus[trkIndx]==TrkStatus::PFPOSITRON )
199  && passTrkCuts(*trk) ) {
200  addPackedCandidate(*outPtrEleTrksAsCands,trk,pv,pvRefProd,pvOrig,trkStatus[trkIndx]);
201 
202 
203  }
204  }
205 
206  iEvent.put(std::move(outPtrTrks));
207  iEvent.put(std::move(outPtrEleTrksAsCands),"eleTracks");
208  edm::OrphanHandle<pat::PackedCandidateCollection> oh = iEvent.put(std::move(outPtrTrksAsCands));
209  auto tk2pc = std::make_unique<edm::Association<pat::PackedCandidateCollection>>(oh);
211  tk2pcFiller.insert(tracks, mapping.begin(), mapping.end());
212  tk2pcFiller.fill() ;
213  iEvent.put(std::move(tk2pc));
214 }
215 
217 {
218  const bool passTrkHits = tr.pt() > minPt_ &&
219  tr.numberOfValidHits() >= minHits_ &&
221  const bool passTrkQual = passesQuality(tr,qualsToAutoAccept_);
222 
223  return passTrkHits || passTrkQual;
224 }
225 
226 void pat::PATLostTracks::addPackedCandidate(std::vector<pat::PackedCandidate>& cands,
227  const reco::TrackRef& trk,
228  const reco::VertexRef& pvSlimmed,
229  const reco::VertexRefProd& pvSlimmedColl,
230  const reco::Vertex& pvOrig,
231  const pat::PATLostTracks::TrkStatus trkStatus)const
232 {
233  const float mass = 0.13957018;
234 
235  int id=211*trk->charge();
236  if(trkStatus==TrkStatus::PFELECTRON) id=11;
237  else if(trkStatus==TrkStatus::PFPOSITRON) id=-11;
238 
239  reco::Candidate::PolarLorentzVector p4(trk->pt(),trk->eta(),trk->phi(),mass);
240  cands.emplace_back(pat::PackedCandidate(p4,trk->vertex(),
241  trk->pt(),trk->eta(),trk->phi(),
242  id,pvSlimmedColl,pvSlimmed.key()));
243 
244  if(trk->pt()>minPtToStoreProps_ || trkStatus==TrkStatus::VTX) cands.back().setTrackProperties(*trk,covarianceSchema_,covarianceVersion_);
245  if(pvOrig.trackWeight(trk) > 0.5) {
246  cands.back().setAssociationQuality(pat::PackedCandidate::UsedInFitTight);
247  }
248 }
249 
250 
251 
252 using pat::PATLostTracks;
T getParameter(std::string const &) const
int pdgId() const final
PDG identifier.
OrphanHandle< PROD > put(std::unique_ptr< PROD > product)
Put a new product.
Definition: Event.h:137
std::vector< VertexCompositeCandidate > VertexCompositeCandidateCollection
collection of Candidate objects
bool isNonnull() const
Checks for non-null.
Definition: Ref.h:253
const double minPt_
const double minPtToStoreProps_
ProductID id() const
Definition: HandleBase.cc:15
bool getByToken(EDGetToken token, Handle< PROD > &result) const
Definition: Event.h:579
~PATLostTracks() override
#define DEFINE_FWK_MODULE(type)
Definition: MakerMacros.h:17
std::vector< pat::PackedCandidate > PackedCandidateCollection
std::vector< Track > TrackCollection
collection of Tracks
Definition: TrackFwd.h:14
const int covarianceSchema_
std::vector< Vertex > VertexCollection
collection of Vertex objects
Definition: VertexFwd.h:9
int charge() const final
electric charge
Definition: LeafCandidate.h:91
key_type key() const
Accessor for product key.
Definition: Ref.h:265
PATLostTracks(const edm::ParameterSet &)
void find(edm::Handle< EcalRecHitCollection > &hits, DetId thisDet, std::vector< EcalRecHitCollection::const_iterator > &hit, bool debug=false)
Definition: FindCaloHit.cc:20
const int covarianceVersion_
ProductID id() const
Accessor for product ID.
Definition: Ref.h:259
EDGetTokenT< ProductType > consumes(edm::InputTag const &tag)
Definition: HeavyIon.h:7
reco::TrackRef trackRef() const
Definition: PFCandidate.cc:442
const double minHits_
const edm::EDGetTokenT< reco::PFCandidateCollection > cands_
int iEvent
Definition: GenABIO.cc:230
double p4[4]
Definition: TauolaWrapper.h:92
double pt() const
track transverse momentum
Definition: TrackBase.h:654
const edm::EDGetTokenT< reco::VertexCompositeCandidateCollection > lambdas_
def pv(vc)
Definition: MetAnalyzer.py:7
void produce(edm::StreamID, edm::Event &, const edm::EventSetup &) const override
unsigned short numberOfValidHits() const
number of valid hits found
Definition: TrackBase.h:889
float trackWeight(const TREF &r) const
returns the weight with which a Track has contributed to the vertex-fit.
Definition: Vertex.h:81
edm::Ref< VertexCollection > VertexRef
persistent reference to a Vertex
Definition: VertexFwd.h:13
std::vector< reco::PFCandidate > PFCandidateCollection
collection of PFCandidates
void addPackedCandidate(std::vector< pat::PackedCandidate > &cands, const reco::TrackRef &trk, const reco::VertexRef &pvSlimmed, const reco::VertexRefProd &pvSlimmedColl, const reco::Vertex &pvOrig, const TrkStatus trkStatus) const
static TrackQuality qualityByName(const std::string &name)
Definition: TrackBase.cc:134
const edm::EDGetTokenT< edm::Association< pat::PackedCandidateCollection > > map_
bool passTrkCuts(const reco::Track &tr) const
const edm::EDGetTokenT< reco::TrackCollection > tracks_
const HitPattern & hitPattern() const
Access the hit pattern, indicating in which Tracker layers the track has hits.
Definition: TrackBase.h:479
const edm::EDGetTokenT< reco::VertexCollection > vertices_
tuple msg
Definition: mps_check.py:278
bool quality(const TrackQuality) const
Track quality.
Definition: TrackBase.h:543
const edm::EDGetTokenT< reco::VertexCompositeCandidateCollection > kshorts_
std::vector< reco::TrackBase::TrackQuality > qualsToAutoAccept_
Particle reconstructed by the particle flow algorithm.
Definition: PFCandidate.h:40
fixed size matrix
HLT enums.
int numberOfValidPixelHits() const
Definition: HitPattern.h:908
const double minPixelHits_
const edm::EDGetTokenT< reco::VertexCollection > pv_
def move(src, dest)
Definition: eostools.py:511
const edm::EDGetTokenT< reco::VertexCollection > pvOrigs_
math::PtEtaPhiMLorentzVector PolarLorentzVector
Lorentz vector.
Definition: Candidate.h:39