CMS 3D CMS Logo

PATLeptonTimeLifeInfoProducer.cc
Go to the documentation of this file.
1 
15 
24 
35 
36 #include <cstring>
37 
38 template <typename T>
40 public:
43 
44  static void fillDescriptions(edm::ConfigurationDescriptions& descriptions);
45  void produce(edm::Event&, const edm::EventSetup&) override;
46 
47 private:
48  //--- private utility methods
49  const reco::Track* getTrack(const T&);
52  static bool fitVertex(const std::vector<reco::TransientTrack>& transTrk, TransientVertex& transVtx) {
53  if (transTrk.size() < 2)
54  return false;
55  KalmanVertexFitter kvf(true);
56  transVtx = kvf.vertex(transTrk);
57  return transVtx.hasRefittedTracks() && transVtx.refittedTracks().size() == transTrk.size();
58  }
59 
60  //--- configuration parameters
65  int pvChoice_;
66 
68  //--- value map for TrackTimeLifeInfo (to be stored into the event)
70 };
71 
72 template <typename T>
74  : leptonsToken_(consumes<std::vector<T>>(cfg.getParameter<edm::InputTag>("src"))),
75  pvToken_(consumes<reco::VertexCollection>(cfg.getParameter<edm::InputTag>("pvSource"))),
76  transTrackBuilderToken_(esConsumes(edm::ESInputTag("", "TransientTrackBuilder"))),
77  selector_(cfg.getParameter<std::string>("selection")),
78  pvChoice_(cfg.getParameter<int>("pvChoice")) {
79  produces<TrackTimeLifeInfoMap>();
80 }
81 
82 template <typename T>
84  // Get leptons
86  evt.getByToken(leptonsToken_, leptons);
87 
88  // Get the vertices
90  evt.getByToken(pvToken_, vertices);
91 
92  // Get transient track builder
93  const TransientTrackBuilder& transTrackBuilder = es.getData(transTrackBuilderToken_);
94 
95  std::vector<TrackTimeLifeInfo> infos;
96  infos.reserve(leptons->size());
97 
98  for (const auto& lepton : *leptons) {
100 
101  // Do nothing for lepton not passing selection
102  if (!selector_(lepton)) {
103  infos.push_back(info);
104  continue;
105  }
106  size_t pv_idx = 0;
107  if (pvChoice_ == useClosestInDz && getTrack(lepton) != nullptr) {
108  float dz_min = 999;
109  size_t vtx_idx = 0;
110  for (const auto& vtx : *vertices) {
111  float dz_tmp = std::abs(getTrack(lepton)->dz(vtx.position()));
112  if (dz_tmp < dz_min) {
113  dz_min = dz_tmp;
114  pv_idx = vtx_idx;
115  }
116  vtx_idx++;
117  }
118  }
119  const reco::Vertex& pv = !vertices->empty() ? (*vertices)[pv_idx] : reco::Vertex();
120 
121  // Obtain IP vector and set related info into lepton
122  produceAndFillIPInfo(lepton, transTrackBuilder, pv, info);
123 
124  // Fit SV and set related info for taus or do nothing for other lepton types
125  produceAndFillSVInfo(lepton, transTrackBuilder, pv, info);
126  infos.push_back(info);
127  } // end of lepton loop
128 
129  // Build the valuemap
130  auto infoMap = std::make_unique<TrackTimeLifeInfoMap>();
132  filler.insert(leptons, infos.begin(), infos.end());
133  filler.fill();
134 
135  // Store output into the event
136  evt.put(std::move(infoMap));
137 }
138 
139 template <>
141  return electron.gsfTrack().isNonnull() ? electron.gsfTrack().get() : nullptr;
142 }
143 
144 template <>
146  return muon.innerTrack().isNonnull() ? muon.innerTrack().get() : nullptr;
147 }
148 
149 template <>
151  const reco::Track* track = nullptr;
152  if (tau.leadChargedHadrCand().isNonnull())
153  track = tau.leadChargedHadrCand()->bestTrack();
154  return track;
155 }
156 
157 template <typename T>
159  const TransientTrackBuilder& transTrackBuilder,
160  const reco::Vertex& pv,
162  const reco::Track* track = getTrack(lepton);
163  if (track != nullptr) {
164  info.setTrack(track);
165  info.setBField_z(transTrackBuilder.field()->inInverseGeV(GlobalPoint(track->vx(), track->vy(), track->vz())).z());
166 
167  // Extrapolate track to the point closest to PV
168  reco::TransientTrack transTrack = transTrackBuilder.build(track);
169  AnalyticalImpactPointExtrapolator extrapolator(transTrack.field());
170  TrajectoryStateOnSurface closestState =
171  extrapolator.extrapolate(transTrack.impactPointState(), RecoVertex::convertPos(pv.position()));
172  GlobalPoint pca = closestState.globalPosition();
173  GlobalError pca_cov = closestState.cartesianError().position();
174  GlobalVector ip_vec = GlobalVector(pca.x() - pv.x(), pca.y() - pv.y(), pca.z() - pv.z());
175  GlobalError ip_cov = pca_cov + GlobalError(pv.covariance());
176  VertexDistance3D pca_dist;
177  Measurement1D ip_mes = pca_dist.distance(pv, VertexState(pca, pca_cov));
178  if (ip_vec.dot(GlobalVector(lepton.px(), lepton.py(), lepton.pz())) < 0)
179  ip_mes = Measurement1D(-1. * ip_mes.value(), ip_mes.error());
180 
181  // Store PCA info
182  info.setPCA(pca, pca_cov);
183  info.setIP(ip_vec, ip_cov);
184  info.setIPLength(ip_mes);
185  }
186 }
187 
188 template <typename T>
190  const TransientTrackBuilder& transTrackBuilder,
191  const reco::Vertex& pv,
193 
194 template <>
196  const TransientTrackBuilder& transTrackBuilder,
197  const reco::Vertex& pv,
199  // Fit SV with tracks of charged tau decay products
200  int fitOK = 0;
201  if (tau.signalChargedHadrCands().size() + tau.signalLostTracks().size() > 1) {
202  // Get tracks from tau signal charged candidates
203  std::vector<reco::TransientTrack> transTrks;
204  TransientVertex transVtx;
205  for (const auto& cand : tau.signalChargedHadrCands()) {
206  if (cand.isNull())
207  continue;
208  const reco::Track* track = cand->bestTrack();
209  if (track != nullptr)
210  transTrks.push_back(transTrackBuilder.build(track));
211  }
212  for (const auto& cand : tau.signalLostTracks()) {
213  if (cand.isNull())
214  continue;
215  const reco::Track* track = cand->bestTrack();
216  if (track != nullptr)
217  transTrks.push_back(transTrackBuilder.build(track));
218  }
219  // Fit SV with KalmanVertexFitter
220  fitOK = fitVertex(transTrks, transVtx) ? 1 : -1;
221  if (fitOK > 0) {
222  reco::Vertex sv = transVtx;
223  // Get flight-length
224  // Full PV->SV flight vector with its covariance
225  GlobalVector flight_vec = GlobalVector(sv.x() - pv.x(), sv.y() - pv.y(), sv.z() - pv.z());
226  GlobalError flight_cov = transVtx.positionError() + GlobalError(pv.covariance());
227  //MB: can be taken from tau itself (but with different fit of PV and SV) as follows:
228  //tau.flightLength().mag2());
229  //tau.flightLengthSig();
230  VertexDistance3D sv_dist;
231  Measurement1D flightLength_mes = sv_dist.signedDistance(pv, sv, GlobalVector(tau.px(), tau.py(), tau.pz()));
232 
233  // Store SV info
234  info.setSV(sv);
235  info.setFlightVector(flight_vec, flight_cov);
236  info.setFlightLength(flightLength_mes);
237  }
238  }
239 }
240 
241 template <typename T>
243  // pat{Electron,Muon,Tau}TimeLifeInfoProducer
245 
246  std::string lepCollName;
247  if (typeid(T) == typeid(pat::Electron))
248  lepCollName = "slimmedElectrons";
249  else if (typeid(T) == typeid(pat::Muon))
250  lepCollName = "slimmedMuons";
251  else if (typeid(T) == typeid(pat::Tau))
252  lepCollName = "slimmedTaus";
253  desc.add<edm::InputTag>("src", edm::InputTag(lepCollName));
254  desc.add<edm::InputTag>("pvSource", edm::InputTag("offlineSlimmedPrimaryVertices"));
255  desc.add<std::string>("selection", "")->setComment("Selection required to produce and store time-life information");
256  desc.add<int>("pvChoice", useFront)
257  ->setComment(
258  "Define PV to compute IP: 0: first PV, 1: PV with the smallest dz of the tau leading track (default: " +
259  std::to_string(useFront) + ")");
260 
261  descriptions.addWithDefaultLabel(desc);
262 }
263 
reco::Vertex::Point convertPos(const GlobalPoint &p)
void addWithDefaultLabel(ParameterSetDescription const &psetDescription)
ESGetTokenH3DDVariant esConsumes(std::string const &Record, edm::ConsumesCollector &)
Definition: DeDxTools.cc:283
static const TGPicture * info(bool iBackgroundIsBlack)
OrphanHandle< PROD > put(std::unique_ptr< PROD > product)
Put a new product.
Definition: Event.h:133
T const & getData(const ESGetToken< T, R > &iToken) const noexcept(false)
Definition: EventSetup.h:119
T z() const
Definition: PV3DBase.h:61
GlobalError positionError() const
void produce(edm::Event &, const edm::EventSetup &) override
PreciseFloatType< T, U >::Type dot(const Vector3DBase< U, FrameTag > &v) const
Definition: Vector3DBase.h:99
PATLeptonTimeLifeInfoProducer(const edm::ParameterSet &)
void produceAndFillSVInfo(const T &, const TransientTrackBuilder &, const reco::Vertex &, TrackTimeLifeInfo &)
Global3DPoint GlobalPoint
Definition: GlobalPoint.h:10
GlobalVector inInverseGeV(const GlobalPoint &gp) const
Field value ad specified global point, in 1/Gev.
Definition: MagneticField.h:36
GlobalErrorBase< double, ErrorMatrixTag > GlobalError
Definition: GlobalError.h:13
bool getByToken(EDGetToken token, Handle< PROD > &result) const
Definition: Event.h:528
edm::EDGetTokenT< reco::VertexCollection > pvToken_
std::vector< Vertex > VertexCollection
Definition: Vertex.h:31
edm::EDGetTokenT< std::vector< T > > leptonsToken_
PATLeptonTimeLifeInfoProducer< pat::Electron > PATElectronTimeLifeInfoProducer
static std::string to_string(const XMLCh *ch)
CachingVertex< 5 > vertex(const std::vector< reco::TransientTrack > &tracks) const override
reco::TransientTrack build(const reco::Track *p) const
PATLeptonTimeLifeInfoProducer< pat::Muon > PATMuonTimeLifeInfoProducer
T x() const
Definition: PV3DBase.h:59
T y() const
Definition: PV3DBase.h:60
static bool fitVertex(const std::vector< reco::TransientTrack > &transTrk, TransientVertex &transVtx)
const MagneticField * field() const
Abs< T >::type abs(const T &t)
Definition: Abs.h:22
#define DEFINE_FWK_MODULE(type)
Definition: MakerMacros.h:16
Analysis-level tau class.
Definition: Tau.h:53
static const TrackGhostTrackState * getTrack(const BasicGhostTrackState *basic)
const MagneticField * field() const
const StringCutObjectSelector< T > selector_
bool hasRefittedTracks() const
void produceAndFillIPInfo(const T &, const TransientTrackBuilder &, const reco::Vertex &, TrackTimeLifeInfo &)
Analysis-level electron class.
Definition: Electron.h:51
double value() const
Definition: Measurement1D.h:25
fixed size matrix
const reco::Track * getTrack(const T &)
HLT enums.
static void fillDescriptions(edm::ConfigurationDescriptions &descriptions)
double error() const
Definition: Measurement1D.h:27
long double T
Analysis-level muon class.
Definition: Muon.h:51
Structure to hold time-life information.
PATLeptonTimeLifeInfoProducer< pat::Tau > PATTauTimeLifeInfoProducer
edm::ESGetToken< TransientTrackBuilder, TransientTrackRecord > transTrackBuilderToken_
def move(src, dest)
Definition: eostools.py:511
TrajectoryStateOnSurface impactPointState() const
Global3DVector GlobalVector
Definition: GlobalVector.h:10
std::vector< reco::TransientTrack > const & refittedTracks() const
Produces lepton life-time information.