CMS 3D CMS Logo

PATLostTracks.cc
Go to the documentation of this file.
1 #include <string>
2 
3 
28 
29 namespace {
30  bool passesQuality(const reco::Track& trk,const std::vector<reco::TrackBase::TrackQuality>& allowedQuals){
31  for(const auto& qual : allowedQuals){
32  if(trk.quality(qual)) return true;
33  }
34  return false;
35  }
36 }
37 
38 namespace pat {
40  public:
41  explicit PATLostTracks(const edm::ParameterSet&);
42  ~PATLostTracks() override;
43 
44  void produce(edm::StreamID, edm::Event&, const edm::EventSetup&) const override;
45 
46  private:
47  enum class TrkStatus {
48  NOTUSED=0,
49  PFCAND,
51  PFELECTRON,
52  PFPOSITRON,
53  VTX
54  };
55  bool passTrkCuts(const reco::Track& tr)const;
56  void addPackedCandidate(std::vector<pat::PackedCandidate>& cands,
57  const reco::TrackRef& trk,
58  const reco::VertexRef& pvSlimmed,
59  const reco::VertexRefProd& pvSlimmedColl,
60  const reco::Vertex& pvOrig,
61  const TrkStatus trkStatus,
63 
64  private:
73  const double minPt_;
74  const double minHits_;
75  const double minPixelHits_;
76  const double minPtToStoreProps_;
77  const int covarianceVersion_;
78  const int covarianceSchema_;
79  std::vector<reco::TrackBase::TrackQuality> qualsToAutoAccept_;
83  };
84 }
85 
87  cands_(consumes<reco::PFCandidateCollection>(iConfig.getParameter<edm::InputTag>("inputCandidates"))),
88  map_(consumes<edm::Association<pat::PackedCandidateCollection> >(iConfig.getParameter<edm::InputTag>("packedPFCandidates"))),
89  tracks_(consumes<reco::TrackCollection>(iConfig.getParameter<edm::InputTag>("inputTracks"))),
90  vertices_(consumes<reco::VertexCollection>(iConfig.getParameter<edm::InputTag>("secondaryVertices"))),
91  kshorts_(consumes<reco::VertexCompositeCandidateCollection>(iConfig.getParameter<edm::InputTag>("kshorts"))),
92  lambdas_(consumes<reco::VertexCompositeCandidateCollection>(iConfig.getParameter<edm::InputTag>("lambdas"))),
93  pv_(consumes<reco::VertexCollection>(iConfig.getParameter<edm::InputTag>("primaryVertices"))),
94  pvOrigs_(consumes<reco::VertexCollection>(iConfig.getParameter<edm::InputTag>("originalVertices"))),
95  minPt_(iConfig.getParameter<double>("minPt")),
96  minHits_(iConfig.getParameter<uint32_t>("minHits")),
97  minPixelHits_(iConfig.getParameter<uint32_t>("minPixelHits")) ,
98  minPtToStoreProps_(iConfig.getParameter<double>("minPtToStoreProps")),
99  covarianceVersion_(iConfig.getParameter<int >("covarianceVersion")),
100  covarianceSchema_(iConfig.getParameter<int >("covarianceSchema")),
101  muons_(consumes<reco::MuonCollection>(iConfig.getParameter<edm::InputTag>("muons"))),
102  passThroughCut_(iConfig.getParameter<std::string>("passThroughCut")),
103  allowMuonId_(iConfig.getParameter<bool>("allowMuonId")){
104  std::vector<std::string> trkQuals(iConfig.getParameter<std::vector<std::string> >("qualsToAutoAccept"));
105  std::transform(trkQuals.begin(),trkQuals.end(),std::back_inserter(qualsToAutoAccept_),reco::TrackBase::qualityByName);
106 
108  std::ostringstream msg;
109  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 ";
110  for(const auto& trkQual : trkQuals) msg <<trkQual<<" ";
111  throw cms::Exception("Configuration") << msg.str();
112  }
113 
114  produces< std::vector<reco::Track> > ();
115  produces< std::vector<pat::PackedCandidate> > (); produces< std::vector<pat::PackedCandidate> > ("eleTracks");
116  produces< edm::Association<pat::PackedCandidateCollection> > ();
117 }
118 
120 
122 
124  iEvent.getByToken( cands_, cands );
125 
127  iEvent.getByToken(map_,pf2pc);
128 
130  iEvent.getByToken( tracks_, tracks );
131 
133  iEvent.getByToken( vertices_, vertices );
134 
136  iEvent.getByToken(muons_, muons);
137 
139  iEvent.getByToken( kshorts_, kshorts );
141  iEvent.getByToken( lambdas_, lambdas );
142 
144  iEvent.getByToken( pv_, pvs );
145  reco::VertexRef pv(pvs.id());
146  reco::VertexRefProd pvRefProd(pvs);
147  if (!pvs->empty()) {
148  pv = reco::VertexRef(pvs, 0);
149  }
151  iEvent.getByToken( pvOrigs_, pvOrigs );
152  const reco::Vertex & pvOrig = (*pvOrigs)[0];
153 
154  auto outPtrTrks = std::make_unique<std::vector<reco::Track>>();
155  auto outPtrTrksAsCands = std::make_unique<std::vector<pat::PackedCandidate>>();
156  auto outPtrEleTrksAsCands = std::make_unique<std::vector<pat::PackedCandidate>>();
157 
158  std::vector<TrkStatus> trkStatus(tracks->size(),TrkStatus::NOTUSED);
159  //Mark all tracks used in candidates
160  //check if packed candidates are storing the tracks by seeing if number of hits >0
161  //currently we dont use that information though
162  //electrons will never store their track (they store the GSF track)
163  for(unsigned int ic=0, nc = cands->size(); ic < nc; ++ic) {
165  const reco::PFCandidate &cand=(*cands)[ic];
166  if(cand.charge() && cand.trackRef().isNonnull() && cand.trackRef().id() == tracks.id() ) {
167 
168  if(cand.pdgId()==11) trkStatus[cand.trackRef().key()]=TrkStatus::PFELECTRON;
169  else if(cand.pdgId()==-11) trkStatus[cand.trackRef().key()]=TrkStatus::PFPOSITRON;
170  else if((*pf2pc)[r]->numberOfHits() > 0) trkStatus[cand.trackRef().key()]=TrkStatus::PFCAND;
171  else trkStatus[cand.trackRef().key()]=TrkStatus::PFCANDNOTRKPROPS;
172  }
173  }
174 
175  //Mark all tracks used in secondary vertices
176  for(const auto& secVert : *vertices){
177  for(auto trkIt = secVert.tracks_begin();trkIt!=secVert.tracks_end();trkIt++){
178  if(trkStatus[trkIt->key()]==TrkStatus::NOTUSED) trkStatus[trkIt->key()]=TrkStatus::VTX;
179  }
180  }
181  for(const auto& v0 : *kshorts){
182  for(size_t dIdx=0;dIdx<v0.numberOfDaughters(); dIdx++){
183  size_t key= (dynamic_cast<const reco::RecoChargedCandidate*>(v0.daughter(dIdx)))->track().key();
184  if(trkStatus[key]==TrkStatus::NOTUSED) trkStatus[key]=TrkStatus::VTX;
185  }
186  }
187  for(const auto& v0 : *lambdas){
188  for(size_t dIdx=0;dIdx<v0.numberOfDaughters(); dIdx++){
189  size_t key= (dynamic_cast<const reco::RecoChargedCandidate*>(v0.daughter(dIdx)))->track().key();
190  if(trkStatus[key]==TrkStatus::NOTUSED) trkStatus[key]=TrkStatus::VTX;
191  }
192  }
193  std::vector<int> mapping(tracks->size(),-1);
194  int lostTrkIndx=0;
195  for(unsigned int trkIndx=0; trkIndx < tracks->size(); trkIndx++){
196  reco::TrackRef trk(tracks,trkIndx);
197  if( trkStatus[trkIndx] == TrkStatus::VTX ||
198  (trkStatus[trkIndx]==TrkStatus::NOTUSED && passTrkCuts(*trk)) ) {
199 
200  outPtrTrks->emplace_back(*trk);
201  addPackedCandidate(*outPtrTrksAsCands, trk, pv, pvRefProd, pvOrig, trkStatus[trkIndx], muons);
202 
203  //for creating the reco::Track -> pat::PackedCandidate map
204  //not done for the lostTrack:eleTracks collection
205  mapping[trkIndx]=lostTrkIndx;
206  lostTrkIndx++;
207  }else if( (trkStatus[trkIndx]==TrkStatus::PFELECTRON || trkStatus[trkIndx]==TrkStatus::PFPOSITRON )
208  && passTrkCuts(*trk) ) {
209  addPackedCandidate(*outPtrEleTrksAsCands, trk, pv, pvRefProd, pvOrig, trkStatus[trkIndx], muons);
210 
211 
212  }
213  }
214 
215  iEvent.put(std::move(outPtrTrks));
216  iEvent.put(std::move(outPtrEleTrksAsCands),"eleTracks");
217  edm::OrphanHandle<pat::PackedCandidateCollection> oh = iEvent.put(std::move(outPtrTrksAsCands));
218  auto tk2pc = std::make_unique<edm::Association<pat::PackedCandidateCollection>>(oh);
220  tk2pcFiller.insert(tracks, mapping.begin(), mapping.end());
221  tk2pcFiller.fill() ;
222  iEvent.put(std::move(tk2pc));
223 }
224 
226 {
227  const bool passTrkHits = tr.pt() > minPt_ &&
228  tr.numberOfValidHits() >= minHits_ &&
230  const bool passTrkQual = passesQuality(tr,qualsToAutoAccept_);
231 
232  return passTrkHits || passTrkQual || passThroughCut_(tr) ;
233 }
234 
235 void pat::PATLostTracks::addPackedCandidate(std::vector<pat::PackedCandidate>& cands,
236  const reco::TrackRef& trk,
237  const reco::VertexRef& pvSlimmed,
238  const reco::VertexRefProd& pvSlimmedColl,
239  const reco::Vertex& pvOrig,
240  const pat::PATLostTracks::TrkStatus trkStatus,
242 {
243  const float mass = 0.13957018;
244 
245  int id=211*trk->charge();
246  if(trkStatus==TrkStatus::PFELECTRON) id=11;
247  else if(trkStatus==TrkStatus::PFPOSITRON) id=-11;
248 
249  // assign the proper pdgId for tracks that are reconstructed as a muon
250  if (allowMuonId_){
251  for (auto& mu : *muons) {
252  if (reco::TrackRef(mu.innerTrack()) == trk) {
253  id = -13 * trk->charge();
254  break;
255  }
256  }
257  }
258 
259  reco::Candidate::PolarLorentzVector p4(trk->pt(),trk->eta(),trk->phi(),mass);
260  cands.emplace_back(pat::PackedCandidate(p4,trk->vertex(),
261  trk->pt(),trk->eta(),trk->phi(),
262  id,pvSlimmedColl,pvSlimmed.key()));
263 
264  if (trk->quality(reco::TrackBase::highPurity))
265  cands.back().setTrackHighPurity(true);
266  else
267  cands.back().setTrackHighPurity(false);
268 
269  if(trk->pt()>minPtToStoreProps_ || trkStatus==TrkStatus::VTX) cands.back().setTrackProperties(*trk,covarianceSchema_,covarianceVersion_);
270  if(pvOrig.trackWeight(trk) > 0.5) {
271  cands.back().setAssociationQuality(pat::PackedCandidate::UsedInFitTight);
272  }
273 }
274 
275 
276 
277 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:125
std::vector< VertexCompositeCandidate > VertexCompositeCandidateCollection
collection of Candidate objects
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, edm::Handle< reco::MuonCollection > muons) const
bool isNonnull() const
Checks for non-null.
Definition: Ref.h:251
const double minPt_
const double minPtToStoreProps_
ProductID id() const
Definition: HandleBase.cc:15
bool getByToken(EDGetToken token, Handle< PROD > &result) const
Definition: Event.h:517
~PATLostTracks() override
std::vector< pat::PackedCandidate > PackedCandidateCollection
std::vector< Track > TrackCollection
collection of Tracks
Definition: TrackFwd.h:15
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:263
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:257
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:224
#define DEFINE_FWK_MODULE(type)
Definition: MakerMacros.h:16
const edm::EDGetTokenT< reco::MuonCollection > muons_
double p4[4]
Definition: TauolaWrapper.h:92
double pt() const
track transverse momentum
Definition: TrackBase.h:660
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:901
float trackWeight(const TREF &r) const
returns the weight with which a Track has contributed to the vertex-fit.
Definition: Vertex.h:81
const int mu
Definition: Constants.h:22
edm::Ref< VertexCollection > VertexRef
persistent reference to a Vertex
Definition: VertexFwd.h:13
std::vector< reco::PFCandidate > PFCandidateCollection
collection of PFCandidates
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:485
const edm::EDGetTokenT< reco::VertexCollection > vertices_
tuple msg
Definition: mps_check.py:285
bool quality(const TrackQuality) const
Track quality.
Definition: TrackBase.h:549
const edm::EDGetTokenT< reco::VertexCompositeCandidateCollection > kshorts_
std::vector< Muon > MuonCollection
Definition: Muon.h:35
std::vector< reco::TrackBase::TrackQuality > qualsToAutoAccept_
StringCutObjectSelector< reco::Track, false > passThroughCut_
Particle reconstructed by the particle flow algorithm.
Definition: PFCandidate.h:40
fixed size matrix
HLT enums.
int numberOfValidPixelHits() const
Definition: HitPattern.h:916
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