CMS 3D CMS Logo

PFTrackProducer.cc
Go to the documentation of this file.
1 
27 
29 public:
31  explicit PFTrackProducer(const edm::ParameterSet&);
32 
33  static void fillDescriptions(edm::ConfigurationDescriptions& descriptions);
34 
35 private:
36  void beginRun(const edm::Run&, const edm::EventSetup&) override;
37  void endRun(const edm::Run&, const edm::EventSetup&) override;
38 
40  void produce(edm::Event&, const edm::EventSetup&) override;
41 
44 
46  std::unique_ptr<PFTrackTransformer> pfTransformer_;
47  std::vector<edm::EDGetTokenT<reco::TrackCollection>> tracksContainers_;
48  std::vector<edm::EDGetTokenT<std::vector<Trajectory>>> trajContainers_;
55  bool trajinev_;
56  bool gsfinev_;
57 };
58 
61 
64  desc.add<std::string>("TrackQuality", "highPurity");
65  desc.add<bool>("UseQuality", true);
66  desc.add<edm::InputTag>("GsfTrackModuleLabel", {"electronGsfTracks"});
67  desc.add<std::vector<edm::InputTag>>("TkColList", {edm::InputTag("generalTracks")});
68  desc.add<edm::InputTag>("PrimaryVertexLabel", {"offlinePrimaryVertices"});
69  desc.add<edm::InputTag>("MuColl", {"muons1stStep"});
70  desc.add<bool>("TrajInEvents", false);
71  desc.add<bool>("GsfTracksInEvents", true);
72  descriptions.add("pfTrack", desc);
73 }
74 
75 using namespace std;
76 using namespace edm;
77 using namespace reco;
79  : transientTrackToken_(esConsumes(edm::ESInputTag("", "TransientTrackBuilder"))),
80  magneticFieldToken_(esConsumes<edm::Transition::BeginRun>()),
81  pfTransformer_() {
82  produces<reco::PFRecTrackCollection>();
83 
84  std::vector<InputTag> tags = iConfig.getParameter<vector<InputTag>>("TkColList");
85  trajinev_ = iConfig.getParameter<bool>("TrajInEvents");
86  tracksContainers_.reserve(tags.size());
87  if (trajinev_) {
88  trajContainers_.reserve(tags.size());
89  }
90  for (auto const& tag : tags) {
91  tracksContainers_.push_back(consumes<reco::TrackCollection>(tag));
92  if (trajinev_) {
93  trajContainers_.push_back(consumes<std::vector<Trajectory>>(tag));
94  }
95  }
96 
97  useQuality_ = iConfig.getParameter<bool>("UseQuality");
98 
99  gsfinev_ = iConfig.getParameter<bool>("GsfTracksInEvents");
100  if (gsfinev_) {
101  gsfTrackLabel_ = consumes<reco::GsfTrackCollection>(iConfig.getParameter<InputTag>("GsfTrackModuleLabel"));
102  }
103 
105 
106  muonColl_ = consumes<reco::MuonCollection>(iConfig.getParameter<InputTag>("MuColl"));
107 
108  vtx_h = consumes<reco::VertexCollection>(iConfig.getParameter<edm::InputTag>("PrimaryVertexLabel"));
109 }
110 
112  //create the empty collections
113  auto PfTrColl = std::make_unique<reco::PFRecTrackCollection>();
114 
115  //read track collection
116  Handle<GsfTrackCollection> gsftrackcoll;
117  bool foundgsf = false;
118  if (gsfinev_) {
119  foundgsf = iEvent.getByToken(gsfTrackLabel_, gsftrackcoll);
120  }
121 
122  //Get PV for STIP calculation, if there is none then take the dummy
124  iEvent.getByToken(vtx_h, vertex);
126  const reco::Vertex* pv = &dummy;
127  if (vertex.isValid()) {
128  pv = &*vertex->begin();
129  } else { // create a dummy PV
131  e(0, 0) = 0.0015 * 0.0015;
132  e(1, 1) = 0.0015 * 0.0015;
133  e(2, 2) = 15. * 15.;
134  reco::Vertex::Point p(0, 0, 0);
135  dummy = reco::Vertex(p, e, 0, 0, 0);
136  }
137 
138  //setup transient track builder
139  TransientTrackBuilder const& thebuilder = iSetup.getData(transientTrackToken_);
140 
141  // read muon collection
143  iEvent.getByToken(muonColl_, recMuons);
144 
145  //default value for when trajinev_ is false
146  const vector<Trajectory> dummyTj(0);
147 
148  for (unsigned int istr = 0; istr < tracksContainers_.size(); istr++) {
149  //Track collection
150  Handle<reco::TrackCollection> tkRefCollection;
151  iEvent.getByToken(tracksContainers_[istr], tkRefCollection);
152  reco::TrackCollection Tk = *(tkRefCollection.product());
153 
154  //Use a pointer to aoid unnecessary copying of the collection
155  const vector<Trajectory>* Tj = &dummyTj;
156  if (trajinev_) {
157  //Trajectory collection
158  Handle<vector<Trajectory>> tjCollection;
159  iEvent.getByToken(trajContainers_[istr], tjCollection);
160 
161  Tj = tjCollection.product();
162  }
163 
164  for (unsigned int i = 0; i < Tk.size(); i++) {
165  reco::TrackRef trackRef(tkRefCollection, i);
166 
167  if (useQuality_ && (!(Tk[i].quality(trackQuality_)))) {
168  bool isMuCandidate = false;
169 
170  //TrackRef trackRef(tkRefCollection, i);
171 
172  if (recMuons.isValid()) {
173  for (unsigned j = 0; j < recMuons->size(); j++) {
174  reco::MuonRef muonref(recMuons, j);
175  if (muonref->track().isNonnull())
176  if (muonref->track() == trackRef && muonref->isGlobalMuon()) {
177  isMuCandidate = true;
178  //cout<<" SAVING TRACK "<<endl;
179  break;
180  }
181  }
182  }
183  if (!isMuCandidate) {
184  continue;
185  }
186  }
187 
188  // find the pre-id kf track
189  bool preId = false;
190  if (foundgsf) {
191  //NOTE: foundgsf is only true if gsftrackcoll is valid
192  for (auto const& gsfTrack : *gsftrackcoll) {
193  if (gsfTrack.seedRef().isNull())
194  continue;
195  auto const& seed = *(gsfTrack.extra()->seedRef());
196  auto const& ElSeed = dynamic_cast<ElectronSeed const&>(seed);
197  if (ElSeed.ctfTrack().isNonnull()) {
198  if (ElSeed.ctfTrack() == trackRef) {
199  preId = true;
200  break;
201  }
202  }
203  }
204  }
205  if (preId) {
206  // Set PFRecTrack of type KF_ElCAND
207  reco::PFRecTrack pftrack(trackRef->charge(), reco::PFRecTrack::KF_ELCAND, i, trackRef);
208 
209  bool valid = false;
210  if (trajinev_) {
211  valid = pfTransformer_->addPoints(pftrack, *trackRef, (*Tj)[i]);
212  } else {
213  Trajectory FakeTraj;
214  valid = pfTransformer_->addPoints(pftrack, *trackRef, FakeTraj);
215  }
216  if (valid) {
217  //calculate STIP
218  double stip = -999;
219  const reco::PFTrajectoryPoint& atECAL = pftrack.extrapolatedPoint(reco::PFTrajectoryPoint::ECALEntrance);
220  if (atECAL.isValid()) //if track extrapolates to ECAL
221  {
222  GlobalVector direction(pftrack.extrapolatedPoint(reco::PFTrajectoryPoint::ECALEntrance).position().x(),
223  pftrack.extrapolatedPoint(reco::PFTrajectoryPoint::ECALEntrance).position().y(),
224  pftrack.extrapolatedPoint(reco::PFTrajectoryPoint::ECALEntrance).position().z());
225  stip = IPTools::signedTransverseImpactParameter(thebuilder.build(*trackRef), direction, *pv)
226  .second.significance();
227  }
228  pftrack.setSTIP(stip);
229  PfTrColl->push_back(pftrack);
230  }
231  } else {
232  reco::PFRecTrack pftrack(trackRef->charge(), reco::PFRecTrack::KF, i, trackRef);
233  bool valid = false;
234  if (trajinev_) {
235  valid = pfTransformer_->addPoints(pftrack, *trackRef, (*Tj)[i]);
236  } else {
237  Trajectory FakeTraj;
238  valid = pfTransformer_->addPoints(pftrack, *trackRef, FakeTraj);
239  }
240 
241  if (valid) {
242  double stip = -999;
243  const reco::PFTrajectoryPoint& atECAL = pftrack.extrapolatedPoint(reco::PFTrajectoryPoint::ECALEntrance);
244  if (atECAL.isValid()) {
245  GlobalVector direction(pftrack.extrapolatedPoint(reco::PFTrajectoryPoint::ECALEntrance).position().x(),
246  pftrack.extrapolatedPoint(reco::PFTrajectoryPoint::ECALEntrance).position().y(),
247  pftrack.extrapolatedPoint(reco::PFTrajectoryPoint::ECALEntrance).position().z());
248  stip = IPTools::signedTransverseImpactParameter(thebuilder.build(*trackRef), direction, *pv)
249  .second.significance();
250  }
251  pftrack.setSTIP(stip);
252  PfTrColl->push_back(pftrack);
253  }
254  }
255  }
256  }
257  iEvent.put(std::move(PfTrColl));
258 }
259 
260 // ------------ method called once each job just before starting event loop ------------
261 void PFTrackProducer::beginRun(const edm::Run& run, const EventSetup& iSetup) {
262  auto const& magneticField = iSetup.getData(magneticFieldToken_);
263  pfTransformer_ = std::make_unique<PFTrackTransformer>(math::XYZVector(magneticField.inTesla(GlobalPoint(0, 0, 0))));
264  if (!trajinev_)
265  pfTransformer_->OnlyProp();
266 }
267 
268 // ------------ method called once each job just after ending the event loop ------------
269 void PFTrackProducer::endRun(const edm::Run& run, const EventSetup& iSetup) { pfTransformer_.reset(); }
reconstructed track used as an input to particle flow
Definition: PFRecTrack.h:20
ESGetTokenH3DDVariant esConsumes(std::string const &Record, edm::ConsumesCollector &)
Definition: DeDxTools.cc:283
T getParameter(std::string const &) const
Definition: ParameterSet.h:307
T const & getData(const ESGetToken< T, R > &iToken) const noexcept(false)
Definition: EventSetup.h:119
TrackQuality
track quality
Definition: TrackBase.h:150
PFTrackProducer(const edm::ParameterSet &)
Constructor.
const edm::ESGetToken< TransientTrackBuilder, TransientTrackRecord > transientTrackToken_
std::pair< bool, Measurement1D > signedTransverseImpactParameter(const reco::TransientTrack &track, const GlobalVector &direction, const reco::Vertex &vertex)
Definition: IPTools.cc:57
T const * product() const
Definition: Handle.h:70
std::vector< Track > TrackCollection
collection of Tracks
Definition: TrackFwd.h:14
math::Error< dimension >::type Error
covariance error matrix (3x3)
Definition: Vertex.h:45
bool isNonnull() const
Checks for non-null.
Definition: Ref.h:238
edm::EDGetTokenT< reco::GsfTrackCollection > gsfTrackLabel_
edm::EDGetTokenT< reco::VertexCollection > vtx_h
std::vector< edm::EDGetTokenT< reco::TrackCollection > > tracksContainers_
reco::TransientTrack build(const reco::Track *p) const
U second(std::pair< T, U > const &p)
string quality
bool isValid() const
is this point valid ?
int iEvent
Definition: GenABIO.cc:224
static void fillDescriptions(edm::ConfigurationDescriptions &descriptions)
void endRun(const edm::Run &, const edm::EventSetup &) override
bool useQuality_
TRACK QUALITY.
def pv(vc)
Definition: MetAnalyzer.py:7
Transition
Definition: Transition.h:12
math::XYZPoint Point
point in the space
Definition: Vertex.h:40
#define DEFINE_FWK_MODULE(type)
Definition: MakerMacros.h:16
edm::EDGetTokenT< reco::MuonCollection > muonColl_
void beginRun(const edm::Run &, const edm::EventSetup &) override
const edm::ESGetToken< MagneticField, IdealMagneticFieldRecord > magneticFieldToken_
static TrackQuality qualityByName(const std::string &name)
Definition: TrackBase.cc:126
XYZVectorD XYZVector
spatial vector with cartesian internal representation
Definition: Vector3D.h:31
std::vector< edm::EDGetTokenT< std::vector< Trajectory > > > trajContainers_
void add(std::string const &label, ParameterSetDescription const &psetDescription)
bool isValid() const
Definition: HandleBase.h:70
void produce(edm::Event &, const edm::EventSetup &) override
Produce the PFRecTrack collection.
fixed size matrix
HLT enums.
reco::TrackBase::TrackQuality trackQuality_
A PFTrack holds several trajectory points, which basically contain the position and momentum of a tra...
def move(src, dest)
Definition: eostools.py:511
std::unique_ptr< PFTrackTransformer > pfTransformer_
PFTrackTransformer.
Definition: Run.h:45