CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
PATTracksToPackedCandidates.cc
Go to the documentation of this file.
1 //Based on: PhysicsTools/PatAlgos/plugins/PATLostTracks.cc
2 
3 // system include files
4 #include <memory>
5 
6 // user include files
9 
12 
15 
21 
25 
26 #include <vector>
27 
28 //
29 // class declaration
30 //
31 
33 public:
35 
36  static void fillDescriptions(edm::ConfigurationDescriptions& descriptions);
37 
38  void addPackedCandidate(std::vector<pat::PackedCandidate>& cands,
39  const reco::Track trk,
40  const reco::VertexRef& pvSlimmed,
41  const reco::VertexRefProd& pvSlimmedColl,
42  bool passPixelTrackSel) const;
43 
44 private:
45  void produce(edm::StreamID, edm::Event&, const edm::EventSetup&) const override;
46 
47  // ----------member data ---------------------------
51  const double dzSigCut_;
52  const double dxySigCut_;
53  const double dzSigHP_;
54  const double dxySigHP_;
55  const double ptMax_;
56  const double ptMin_;
57  const bool resetHP_;
58  const int covarianceVersion_;
59  const int covarianceSchema_;
60 };
61 
62 //
63 // constants, enums and typedefs
64 //
65 
66 //
67 // static data member definitions
68 //
69 
70 //
71 // constructors and destructor
72 //
74  : srcTracks_(consumes<reco::TrackCollection>(iConfig.getParameter<edm::InputTag>("srcTracks"))),
75  srcPrimaryVertices_(consumes<reco::VertexCollection>(iConfig.getParameter<edm::InputTag>("srcPrimaryVertices"))),
76  srcOfflineBeamSpot_(consumes<reco::BeamSpot>(iConfig.getParameter<edm::InputTag>("srcOfflineBeamSpot"))),
77  dzSigCut_(iConfig.getParameter<double>("dzSigCut")),
78  dxySigCut_(iConfig.getParameter<double>("dxySigCut")),
79  dzSigHP_(iConfig.getParameter<double>("dzSigHP")),
80  dxySigHP_(iConfig.getParameter<double>("dxySigHP")),
81  ptMax_(iConfig.getParameter<double>("ptMax")),
82  ptMin_(iConfig.getParameter<double>("ptMin")),
83  resetHP_(iConfig.getParameter<bool>("resetHP")),
84  covarianceVersion_(iConfig.getParameter<int>("covarianceVersion")),
85  covarianceSchema_(iConfig.getParameter<int>("covarianceSchema")) {
86  produces<std::vector<pat::PackedCandidate>>();
87  produces<edm::Association<pat::PackedCandidateCollection>>();
88 }
89 
90 //
91 // member functions
92 //
93 
94 // ------------ method called to produce the data ------------
96  using namespace edm;
97  using namespace std;
98 
99  //track collection
100  auto tracks = iEvent.getHandle(srcTracks_);
101 
102  auto outPtrTrksAsCands = std::make_unique<std::vector<pat::PackedCandidate>>();
103 
104  //vtx collection
105  auto pvs = iEvent.getHandle(srcPrimaryVertices_);
106  reco::VertexRef pv(pvs.id());
107  reco::VertexRefProd pvRefProd(pvs);
108 
109  //best vertex
110  double bestvzError;
111  math::XYZPoint bestvtx;
112  math::Error<3>::type vtx_cov;
113  if (!pvs->empty()) {
114  pv = reco::VertexRef(pvs, 0);
115  const reco::Vertex& vtx = (*pvs)[0];
116  bestvzError = vtx.zError();
117  bestvtx = vtx.position();
118  vtx_cov = vtx.covariance();
119  } else {
120  const auto& bs = iEvent.get(srcOfflineBeamSpot_);
121  bestvzError = bs.z0Error();
122  bestvtx = bs.position();
123  vtx_cov = bs.covariance3D();
124  }
125 
126  std::vector<int> mapping(tracks->size(), -1);
127  int savedCandIndx = 0;
128  int trkIndx = -1;
129  for (auto const& trk : *tracks) {
130  trkIndx++;
131  double dzvtx = std::abs(trk.dz(bestvtx));
132  double dxyvtx = std::abs(trk.dxy(bestvtx));
133  double dzerror = std::hypot(trk.dzError(), bestvzError);
134  double dxyerror = trk.dxyError(bestvtx, vtx_cov);
135 
136  if (dzvtx >= dzSigCut_ * dzerror)
137  continue;
138  if (dxyvtx >= dxySigCut_ * dxyerror)
139  continue;
140  if (trk.pt() >= ptMax_ || trk.pt() <= ptMin_)
141  continue;
142 
143  bool passSelection = (dzvtx < dzSigHP_ * dzerror && dxyvtx < dxySigHP_ * dxyerror);
144 
145  addPackedCandidate(*outPtrTrksAsCands, trk, pv, pvRefProd, passSelection);
146 
147  //for creating the reco::Track -> pat::PackedCandidate map
148  mapping[trkIndx] = savedCandIndx;
149  savedCandIndx++;
150  }
151  edm::OrphanHandle<pat::PackedCandidateCollection> oh = iEvent.put(std::move(outPtrTrksAsCands));
152  auto tk2pc = std::make_unique<edm::Association<pat::PackedCandidateCollection>>(oh);
154  tk2pcFiller.insert(tracks, mapping.begin(), mapping.end());
155  tk2pcFiller.fill();
156  iEvent.put(std::move(tk2pc));
157 }
158 
159 void PATTracksToPackedCandidates::addPackedCandidate(std::vector<pat::PackedCandidate>& cands,
160  const reco::Track trk,
161  const reco::VertexRef& pvSlimmed,
162  const reco::VertexRefProd& pvSlimmedColl,
163  bool passPixelTrackSel) const {
164  const float mass = 0.13957018;
165 
166  int id = 211 * trk.charge();
167 
168  reco::Candidate::PolarLorentzVector p4(trk.pt(), trk.eta(), trk.phi(), mass);
169  cands.emplace_back(p4, trk.vertex(), trk.pt(), trk.eta(), trk.phi(), id, pvSlimmedColl, pvSlimmed.key());
170 
171  if (resetHP_) {
172  if (passPixelTrackSel)
173  cands.back().setTrackHighPurity(true);
174  else
175  cands.back().setTrackHighPurity(false);
176  } else {
178  cands.back().setTrackHighPurity(true);
179  else
180  cands.back().setTrackHighPurity(false);
181  }
182 
183  cands.back().setTrackProperties(trk, covarianceSchema_, covarianceVersion_);
184 }
185 
186 // ------------ method fills 'descriptions' with the allowed parameters for the module ------------
188  //The following says we do not know what parameters are allowed so do no validation
189  // Please change this to state exactly what you do use, even if it is no parameters
191  desc.add<edm::InputTag>("srcTracks", {"hiConformalPixelTracks"});
192  desc.add<edm::InputTag>("srcPrimaryVertices", {"offlineSlimmedPrimaryVertices"});
193  desc.add<edm::InputTag>("srcOfflineBeamSpot", {"offlineBeamSpot"})
194  ->setComment("use BeamSpot if empty vtx collection");
195  desc.add<double>("dzSigCut", 10.0);
196  desc.add<double>("dxySigCut", 25.0);
197  desc.add<double>("dzSigHP", 7.0)->setComment("to set HighPurity flag for pixel tracks");
198  desc.add<double>("dxySigHP", 20.0)->setComment("to set HighPurity flag for pixel tracks");
199  desc.add<double>("ptMax", 1.0)->setComment("max pT for pixel tracks - above this will use general tracks");
200  desc.add<double>("ptMin", 0.3)->setComment("min pT for pixel tracks");
201  desc.add<bool>("resetHP", true)
202  ->setComment("pixel tracks do not have HP flag set. Use False if does not want to reset HP flag");
203  desc.add<int>("covarianceVersion", 0)->setComment("so far: 0 is Phase0, 1 is Phase1");
204  desc.add<int>("covarianceSchema", 520)->setComment("use less accurate schema - reduce size of collection");
205  descriptions.addWithDefaultLabel(desc);
206 }
207 
208 //define this as a plug-in
void setComment(std::string const &value)
void addWithDefaultLabel(ParameterSetDescription const &psetDescription)
const edm::EDGetTokenT< reco::VertexCollection > srcPrimaryVertices_
OrphanHandle< PROD > put(std::unique_ptr< PROD > product)
Put a new product.
Definition: Event.h:133
static void fillDescriptions(edm::ConfigurationDescriptions &descriptions)
uint16_t *__restrict__ id
const edm::EDGetTokenT< reco::TrackCollection > srcTracks_
double zError() const
error on z
Definition: Vertex.h:141
#define DEFINE_FWK_MODULE(type)
Definition: MakerMacros.h:16
std::vector< Track > TrackCollection
collection of Tracks
Definition: TrackFwd.h:14
double phi() const
azimuthal angle of momentum vector
Definition: TrackBase.h:649
ErrorD< N >::type type
Definition: Error.h:32
auto const & tracks
cannot be loose
double covariance(int i, int j) const
(i, j)-th element of error matrix, i, j = 0, ... 2
Definition: Vertex.h:148
key_type key() const
Accessor for product key.
Definition: Ref.h:250
std::vector< Vertex > VertexCollection
Definition: Vertex.h:12
const Point & position() const
position
Definition: Vertex.h:127
void produce(edm::StreamID, edm::Event &, const edm::EventSetup &) const override
Handle< PROD > getHandle(EDGetTokenT< PROD > token) const
Definition: Event.h:563
ptMin_(conf.getParameter< double >("ptMin"))
const Point & vertex() const
reference point on the track. This method is DEPRECATED, please use referencePoint() instead ...
Definition: TrackBase.h:676
int iEvent
Definition: GenABIO.cc:224
double eta() const
pseudorapidity of momentum vector
Definition: TrackBase.h:652
double pt() const
track transverse momentum
Definition: TrackBase.h:637
def move
Definition: eostools.py:511
Abs< T >::type abs(const T &t)
Definition: Abs.h:22
bool get(ProductID const &oid, Handle< PROD > &result) const
Definition: Event.h:346
ParameterDescriptionBase * add(U const &iLabel, T const &value)
edm::Ref< VertexCollection > VertexRef
persistent reference to a Vertex
Definition: VertexFwd.h:13
void addPackedCandidate(std::vector< pat::PackedCandidate > &cands, const reco::Track trk, const reco::VertexRef &pvSlimmed, const reco::VertexRefProd &pvSlimmedColl, bool passPixelTrackSel) const
XYZPointD XYZPoint
point in space with cartesian internal representation
Definition: Point3D.h:12
bool quality(const TrackQuality) const
Track quality.
Definition: TrackBase.h:552
PATTracksToPackedCandidates(const edm::ParameterSet &)
int charge() const
track electric charge
Definition: TrackBase.h:596
const edm::EDGetTokenT< reco::BeamSpot > srcOfflineBeamSpot_
math::PtEtaPhiMLorentzVector PolarLorentzVector
Lorentz vector.
Definition: Candidate.h:38