CMS 3D CMS Logo

PATLeptonTimeLifeInfoProducer.cc
Go to the documentation of this file.
1 
16 
25 
36 
37 #include <cstring>
38 
39 template <typename T>
41 public:
44 
45  static void fillDescriptions(edm::ConfigurationDescriptions& descriptions);
46  void produce(edm::Event&, const edm::EventSetup&) override;
47 
48 private:
49  //--- private utility methods
50  const reco::Track* getTrack(const T&);
53  static bool fitVertex(const std::vector<reco::TransientTrack>& transTrk, TransientVertex& transVtx) {
54  if (transTrk.size() < 2)
55  return false;
56  try {
57  KalmanVertexFitter kvf(true);
58  transVtx = kvf.vertex(transTrk);
59  return transVtx.hasRefittedTracks() && transVtx.refittedTracks().size() == transTrk.size();
60  } catch (VertexException& e) {
61  edm::LogWarning("PATLeptonTimeLifeInfoProducer") << " fitVertex failed: " << e.what();
62  return false;
63  }
64  }
65 
66  //--- configuration parameters
71  int pvChoice_;
72 
74  //--- value map for TrackTimeLifeInfo (to be stored into the event)
76 };
77 
78 template <typename T>
80  : leptonsToken_(consumes<std::vector<T>>(cfg.getParameter<edm::InputTag>("src"))),
81  pvToken_(consumes<reco::VertexCollection>(cfg.getParameter<edm::InputTag>("pvSource"))),
82  transTrackBuilderToken_(esConsumes(edm::ESInputTag("", "TransientTrackBuilder"))),
83  selector_(cfg.getParameter<std::string>("selection")),
84  pvChoice_(cfg.getParameter<int>("pvChoice")) {
85  produces<TrackTimeLifeInfoMap>();
86 }
87 
88 template <typename T>
90  // Get leptons
92  evt.getByToken(leptonsToken_, leptons);
93 
94  // Get the vertices
96  evt.getByToken(pvToken_, vertices);
97 
98  // Get transient track builder
99  const TransientTrackBuilder& transTrackBuilder = es.getData(transTrackBuilderToken_);
100 
101  std::vector<TrackTimeLifeInfo> infos;
102  infos.reserve(leptons->size());
103 
104  for (const auto& lepton : *leptons) {
106 
107  // Do nothing for lepton not passing selection
108  if (!selector_(lepton)) {
109  infos.push_back(info);
110  continue;
111  }
112  size_t pv_idx = 0;
113  if (pvChoice_ == useClosestInDz && getTrack(lepton) != nullptr) {
114  float dz_min = 999;
115  size_t vtx_idx = 0;
116  for (const auto& vtx : *vertices) {
117  float dz_tmp = std::abs(getTrack(lepton)->dz(vtx.position()));
118  if (dz_tmp < dz_min) {
119  dz_min = dz_tmp;
120  pv_idx = vtx_idx;
121  }
122  vtx_idx++;
123  }
124  }
125  const reco::Vertex& pv = !vertices->empty() ? (*vertices)[pv_idx] : reco::Vertex();
126 
127  // Obtain IP vector and set related info into lepton
128  produceAndFillIPInfo(lepton, transTrackBuilder, pv, info);
129 
130  // Fit SV and set related info for taus or do nothing for other lepton types
131  produceAndFillSVInfo(lepton, transTrackBuilder, pv, info);
132  infos.push_back(info);
133  } // end of lepton loop
134 
135  // Build the valuemap
136  auto infoMap = std::make_unique<TrackTimeLifeInfoMap>();
138  filler.insert(leptons, infos.begin(), infos.end());
139  filler.fill();
140 
141  // Store output into the event
142  evt.put(std::move(infoMap));
143 }
144 
145 template <>
147  return electron.gsfTrack().isNonnull() ? electron.gsfTrack().get() : nullptr;
148 }
149 
150 template <>
152  return muon.innerTrack().isNonnull() ? muon.innerTrack().get() : nullptr;
153 }
154 
155 template <>
157  const reco::Track* track = nullptr;
158  if (tau.leadChargedHadrCand().isNonnull())
159  track = tau.leadChargedHadrCand()->bestTrack();
160  return track;
161 }
162 
163 template <typename T>
165  const TransientTrackBuilder& transTrackBuilder,
166  const reco::Vertex& pv,
168  const reco::Track* track = getTrack(lepton);
169  if (track != nullptr) {
170  // Extrapolate track to the point closest to PV
171  reco::TransientTrack transTrack = transTrackBuilder.build(track);
172  AnalyticalImpactPointExtrapolator extrapolator(transTrack.field());
173  TrajectoryStateOnSurface closestState =
174  extrapolator.extrapolate(transTrack.impactPointState(), RecoVertex::convertPos(pv.position()));
175  if (!closestState.isValid()) {
176  edm::LogError("PATLeptonTimeLifeInfoProducer")
177  << "closestState not valid! From:\n"
178  << "transTrack.impactPointState():\n"
179  << transTrack.impactPointState() << "RecoVertex::convertPos(pv.position()):\n"
180  << RecoVertex::convertPos(pv.position());
181  return;
182  }
183  GlobalPoint pca = closestState.globalPosition();
184  GlobalError pca_cov = closestState.cartesianError().position();
185  GlobalVector ip_vec = GlobalVector(pca.x() - pv.x(), pca.y() - pv.y(), pca.z() - pv.z());
186  GlobalError ip_cov = pca_cov + GlobalError(pv.covariance());
187  VertexDistance3D pca_dist;
188  Measurement1D ip_mes = pca_dist.distance(pv, VertexState(pca, pca_cov));
189  if (ip_vec.dot(GlobalVector(lepton.px(), lepton.py(), lepton.pz())) < 0)
190  ip_mes = Measurement1D(-1. * ip_mes.value(), ip_mes.error());
191 
192  // Store Track and PCA info
193  info.setTrack(track);
194  info.setBField_z(transTrackBuilder.field()->inInverseGeV(GlobalPoint(track->vx(), track->vy(), track->vz())).z());
195  info.setPCA(pca, pca_cov);
196  info.setIP(ip_vec, ip_cov);
197  info.setIPLength(ip_mes);
198  }
199 }
200 
201 template <typename T>
203  const TransientTrackBuilder& transTrackBuilder,
204  const reco::Vertex& pv,
206 
207 template <>
209  const TransientTrackBuilder& transTrackBuilder,
210  const reco::Vertex& pv,
212  // Fit SV with tracks of charged tau decay products
213  int fitOK = 0;
214  if (tau.signalChargedHadrCands().size() + tau.signalLostTracks().size() > 1) {
215  // Get tracks from tau signal charged candidates
216  std::vector<reco::TransientTrack> transTrks;
217  TransientVertex transVtx;
218  for (const auto& cand : tau.signalChargedHadrCands()) {
219  if (cand.isNull())
220  continue;
221  const reco::Track* track = cand->bestTrack();
222  if (track != nullptr)
223  transTrks.push_back(transTrackBuilder.build(track));
224  }
225  for (const auto& cand : tau.signalLostTracks()) {
226  if (cand.isNull())
227  continue;
228  const reco::Track* track = cand->bestTrack();
229  if (track != nullptr)
230  transTrks.push_back(transTrackBuilder.build(track));
231  }
232  // Fit SV with KalmanVertexFitter
233  fitOK = fitVertex(transTrks, transVtx) ? 1 : -1;
234  if (fitOK > 0) {
235  reco::Vertex sv = transVtx;
236  // Get flight-length
237  // Full PV->SV flight vector with its covariance
238  GlobalVector flight_vec = GlobalVector(sv.x() - pv.x(), sv.y() - pv.y(), sv.z() - pv.z());
239  GlobalError flight_cov = transVtx.positionError() + GlobalError(pv.covariance());
240  //MB: can be taken from tau itself (but with different fit of PV and SV) as follows:
241  //tau.flightLength().mag2());
242  //tau.flightLengthSig();
243  VertexDistance3D sv_dist;
244  Measurement1D flightLength_mes = sv_dist.signedDistance(pv, sv, GlobalVector(tau.px(), tau.py(), tau.pz()));
245 
246  // Store SV info
247  info.setSV(sv);
248  info.setFlightVector(flight_vec, flight_cov);
249  info.setFlightLength(flightLength_mes);
250  }
251  }
252 }
253 
254 template <typename T>
256  // pat{Electron,Muon,Tau}TimeLifeInfoProducer
258 
259  std::string lepCollName;
260  if (typeid(T) == typeid(pat::Electron))
261  lepCollName = "slimmedElectrons";
262  else if (typeid(T) == typeid(pat::Muon))
263  lepCollName = "slimmedMuons";
264  else if (typeid(T) == typeid(pat::Tau))
265  lepCollName = "slimmedTaus";
266  desc.add<edm::InputTag>("src", edm::InputTag(lepCollName));
267  desc.add<edm::InputTag>("pvSource", edm::InputTag("offlineSlimmedPrimaryVertices"));
268  desc.add<std::string>("selection", "")->setComment("Selection required to produce and store time-life information");
269  desc.add<int>("pvChoice", useFront)
270  ->setComment(
271  "Define PV to compute IP: 0: first PV, 1: PV with the smallest dz of the tau leading track (default: " +
272  std::to_string(useFront) + ")");
273 
274  descriptions.addWithDefaultLabel(desc);
275 }
276 
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
Common base class.
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
Log< level::Error, false > LogError
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
Log< level::Warning, false > LogWarning
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.