CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
SecondaryVertexProducer.cc
Go to the documentation of this file.
1 #include <functional>
2 #include <algorithm>
3 #include <iterator>
4 #include <cstddef>
5 #include <string>
6 #include <vector>
7 #include <map>
8 #include <set>
9 
10 #include <boost/iterator/transform_iterator.hpp>
11 
20 
24 
28 
36 
40 
46 
48 
49 using namespace reco;
50 
51 namespace {
52  template<typename T>
53  struct RefToBaseLess : public std::binary_function<edm::RefToBase<T>,
54  edm::RefToBase<T>,
55  bool> {
56  inline bool operator()(const edm::RefToBase<T> &r1,
57  const edm::RefToBase<T> &r2) const
58  {
59  return r1.id() < r2.id() ||
60  (r1.id() == r2.id() && r1.key() < r2.key());
61  }
62  };
63 }
64 
66  public:
67  explicit SecondaryVertexProducer(const edm::ParameterSet &params);
69 
70  virtual void produce(edm::Event &event, const edm::EventSetup &es) override;
71 
72  private:
74  CONSTRAINT_NONE = 0,
79  CONSTRAINT_PV_PRIMARIES_IN_FIT
80  };
81 
82  static ConstraintType getConstraintType(const std::string &name);
83 
99 };
100 
103 {
104  if (name == "None")
105  return CONSTRAINT_NONE;
106  else if (name == "BeamSpot")
107  return CONSTRAINT_BEAMSPOT;
108  else if (name == "BeamSpot+PVPosition")
109  return CONSTRAINT_PV_BEAMSPOT_SIZE;
110  else if (name == "BeamSpotZ+PVErrorScaledXY")
111  return CONSTRAINT_PV_BS_Z_ERRORS_SCALED;
112  else if (name == "PVErrorScaled")
113  return CONSTRAINT_PV_ERROR_SCALED;
114  else if (name == "BeamSpot+PVTracksInFit")
115  return CONSTRAINT_PV_PRIMARIES_IN_FIT;
116  else
117  throw cms::Exception("InvalidArgument")
118  << "SecondaryVertexProducer: ``constraint'' parameter "
119  "value \"" << name << "\" not understood."
120  << std::endl;
121 }
122 
125 {
126  if (name == "AlwaysWithGhostTrack")
128  else if (name == "SingleTracksWithGhostTrack")
130  else if (name == "RefitGhostTrackWithVertices")
132  else
133  throw cms::Exception("InvalidArgument")
134  << "SecondaryVertexProducer: ``fitType'' "
135  "parameter value \"" << name << "\" for "
136  "GhostTrackVertexFinder settings not "
137  "understood." << std::endl;
138 }
139 
141  const edm::ParameterSet &params) :
142  sortCriterium(TrackSorting::getCriterium(params.getParameter<std::string>("trackSort"))),
143  trackSelector(params.getParameter<edm::ParameterSet>("trackSelection")),
144  constraint(getConstraintType(params.getParameter<std::string>("constraint"))),
145  constraintScaling(1.0),
146  vtxRecoPSet(params.getParameter<edm::ParameterSet>("vertexReco")),
147  useGhostTrack(vtxRecoPSet.getParameter<std::string>("finder") == "gtvr"),
148  withPVError(params.getParameter<bool>("usePVError")),
149  minTrackWeight(params.getParameter<double>("minimumTrackWeight")),
150  vertexFilter(params.getParameter<edm::ParameterSet>("vertexCuts")),
151  vertexSorting(params.getParameter<edm::ParameterSet>("vertexSelection"))
152 {
153  token_trackIPTagInfo = consumes<reco::TrackIPTagInfoCollection>(params.getParameter<edm::InputTag>("trackIPTagInfos"));
156  constraintScaling = params.getParameter<double>("pvErrorScaling");
157 
162  token_BeamSpot = consumes<reco::BeamSpot>(params.getParameter<edm::InputTag>("beamSpotTag"));
163  useExternalSV = false;
164  if(params.existsAs<bool>("useExternalSV")) useExternalSV = params.getParameter<bool> ("useExternalSV");
165  if(useExternalSV) {
166  token_extSVCollection = consumes<reco::VertexCollection>(params.getParameter<edm::InputTag>("extSVCollection"));
167  extSVDeltaRToJet = params.getParameter<double>("extSVDeltaRToJet");
168  }
169  produces<SecondaryVertexTagInfoCollection>();
170 }
171 
173 {
174 }
175 
176 namespace {
177  struct SVBuilder :
178  public std::unary_function<const Vertex&, SecondaryVertex> {
179 
180  SVBuilder(const reco::Vertex &pv,
181  const GlobalVector &direction,
182  bool withPVError) :
183  pv(pv), direction(direction),
184  withPVError(withPVError) {}
185 
186  SecondaryVertex operator () (const reco::Vertex &sv) const
187  { return SecondaryVertex(pv, sv, direction, withPVError); }
188 
189  const Vertex &pv;
190  const GlobalVector &direction;
191  bool withPVError;
192  };
193 
194  struct SVFilter :
195  public std::unary_function<const SecondaryVertex&, bool> {
196 
197  SVFilter(const VertexFilter &filter, const Vertex &pv,
198  const GlobalVector &direction) :
199  filter(filter), pv(pv), direction(direction) {}
200 
201  inline bool operator () (const SecondaryVertex &sv) const
202  { return !filter(pv, sv, direction); }
203 
204  const VertexFilter &filter;
205  const Vertex &pv;
206  const GlobalVector &direction;
207  };
208 
209 } // anonynmous namespace
210 
212  const edm::EventSetup &es)
213 {
215  RefToBaseLess<Track> > TransientTrackMap;
216 
218  es.get<TransientTrackRecord>().get("TransientTrackBuilder",
219  trackBuilder);
220 
222  event.getByToken(token_trackIPTagInfo, trackIPTagInfos);
223 
224  // External Sec Vertex collection (e.g. for IVF usage)
226  if(useExternalSV) event.getByToken(token_extSVCollection,extSecVertex);
227 
228 
230  unsigned int bsCovSrc[7] = { 0, };
231  double sigmaZ = 0.0, beamWidth = 0.0;
232  switch(constraint) {
234  event.getByToken(token_BeamSpot,beamSpot);
235  bsCovSrc[3] = bsCovSrc[4] = bsCovSrc[5] = bsCovSrc[6] = 1;
236  sigmaZ = beamSpot->sigmaZ();
237  beamWidth = beamSpot->BeamWidthX();
238  break;
239 
241  event.getByToken(token_BeamSpot,beamSpot);
242  bsCovSrc[0] = bsCovSrc[1] = 2;
243  bsCovSrc[3] = bsCovSrc[4] = bsCovSrc[5] = 1;
244  sigmaZ = beamSpot->sigmaZ();
245  break;
246 
248  bsCovSrc[0] = bsCovSrc[1] = bsCovSrc[2] = 2;
249  break;
250 
251  case CONSTRAINT_BEAMSPOT:
253  event.getByToken(token_BeamSpot,beamSpot);
254  break;
255 
256  default:
257  /* nothing */;
258  }
259 
260  std::auto_ptr<ConfigurableVertexReconstructor> vertexReco;
261  std::auto_ptr<GhostTrackVertexFinder> vertexRecoGT;
262  if (useGhostTrack)
263  vertexRecoGT.reset(new GhostTrackVertexFinder(
264  vtxRecoPSet.getParameter<double>("maxFitChi2"),
265  vtxRecoPSet.getParameter<double>("mergeThreshold"),
266  vtxRecoPSet.getParameter<double>("primcut"),
267  vtxRecoPSet.getParameter<double>("seccut"),
269  else
270  vertexReco.reset(
272 
273  TransientTrackMap primariesMap;
274 
275  // result secondary vertices
276 
277  std::auto_ptr<SecondaryVertexTagInfoCollection>
278  tagInfos(new SecondaryVertexTagInfoCollection);
279 
280  for(TrackIPTagInfoCollection::const_iterator iterJets =
281  trackIPTagInfos->begin(); iterJets != trackIPTagInfos->end();
282  ++iterJets) {
283  std::vector<SecondaryVertexTagInfo::IndexedTrackData> trackData;
284 // std::cout << "Jet " << iterJets-trackIPTagInfos->begin() << std::endl;
285 
286  const Vertex &pv = *iterJets->primaryVertex();
287 
288  std::set<TransientTrack> primaries;
291  iter != pv.tracks_end(); ++iter) {
292  TransientTrackMap::iterator pos =
293  primariesMap.lower_bound(*iter);
294 
295  if (pos != primariesMap.end() &&
296  pos->first == *iter)
297  primaries.insert(pos->second);
298  else {
299  TransientTrack track =
300  trackBuilder->build(
301  iter->castTo<TrackRef>());
302  primariesMap.insert(pos,
303  std::make_pair(*iter, track));
304  primaries.insert(track);
305  }
306  }
307  }
308 
309  edm::RefToBase<Jet> jetRef = iterJets->jet();
310 
311  GlobalVector jetDir(jetRef->momentum().x(),
312  jetRef->momentum().y(),
313  jetRef->momentum().z());
314 
315  std::vector<std::size_t> indices =
316  iterJets->sortedIndexes(sortCriterium);
317 
318  TrackRefVector trackRefs = iterJets->sortedTracks(indices);
319 
320  const std::vector<TrackIPTagInfo::TrackIPData> &ipData =
321  iterJets->impactParameterData();
322 
323  // build transient tracks used for vertex reconstruction
324 
325  std::vector<TransientTrack> fitTracks;
326  std::vector<GhostTrackState> gtStates;
327  std::auto_ptr<GhostTrackPrediction> gtPred;
328  if (useGhostTrack)
329  gtPred.reset(new GhostTrackPrediction(
330  *iterJets->ghostTrack()));
331 
332  for(unsigned int i = 0; i < indices.size(); i++) {
333  typedef SecondaryVertexTagInfo::IndexedTrackData IndexedTrackData;
334 
335  const TrackRef &trackRef = trackRefs[i];
336 
337  trackData.push_back(IndexedTrackData());
338  trackData.back().first = indices[i];
339 
340  // select tracks for SV finder
341 
342  if (!trackSelector(*trackRef, ipData[indices[i]], *jetRef,
344  pv.position()))) {
345  trackData.back().second.svStatus =
346  SecondaryVertexTagInfo::TrackData::trackSelected;
347  continue;
348  }
349 
350  TransientTrackMap::const_iterator pos =
351  primariesMap.find(
352  TrackBaseRef(trackRef));
353  TransientTrack fitTrack;
354  if (pos != primariesMap.end()) {
355  primaries.erase(pos->second);
356  fitTrack = pos->second;
357  } else
358  fitTrack = trackBuilder->build(trackRef);
359  fitTracks.push_back(fitTrack);
360 
361  trackData.back().second.svStatus =
362  SecondaryVertexTagInfo::TrackData::trackUsedForVertexFit;
363 
364  if (useGhostTrack) {
365  GhostTrackState gtState(fitTrack);
366  GlobalPoint pos =
367  ipData[indices[i]].closestToGhostTrack;
368  gtState.linearize(*gtPred, true,
369  gtPred->lambda(pos));
370  gtState.setWeight(ipData[indices[i]].ghostTrackWeight);
371  gtStates.push_back(gtState);
372  }
373  }
374 
375  std::auto_ptr<GhostTrack> ghostTrack;
376  if (useGhostTrack)
377  ghostTrack.reset(new GhostTrack(
381  GlobalVector(
382  iterJets->ghostTrack()->px(),
383  iterJets->ghostTrack()->py(),
384  iterJets->ghostTrack()->pz()),
385  0.05),
386  *gtPred, gtStates,
387  iterJets->ghostTrack()->chi2(),
388  iterJets->ghostTrack()->ndof()));
389 
390  // perform actual vertex finding
391 
392 
393  std::vector<reco::Vertex> extAssoCollection;
394  std::vector<TransientVertex> fittedSVs;
395  std::vector<SecondaryVertex> SVs;
396  if(!useExternalSV){
397  switch(constraint) {
398  case CONSTRAINT_NONE:
399  if (useGhostTrack)
400  fittedSVs = vertexRecoGT->vertices(
401  pv, *ghostTrack);
402  else
403  fittedSVs = vertexReco->vertices(fitTracks);
404  break;
405 
406  case CONSTRAINT_BEAMSPOT:
407  if (useGhostTrack)
408  fittedSVs = vertexRecoGT->vertices(
409  pv, *beamSpot, *ghostTrack);
410  else
411  fittedSVs = vertexReco->vertices(fitTracks,
412  *beamSpot);
413  break;
414 
419  for(unsigned int i = 0; i < 7; i++) {
420  unsigned int covSrc = bsCovSrc[i];
421  for(unsigned int j = 0; j < 7; j++) {
422  double v=0.0;
423  if (!covSrc || bsCovSrc[j] != covSrc)
424  v = 0.0;
425  else if (covSrc == 1)
426  v = beamSpot->covariance(i, j);
427  else if (j<3 && i<3)
428  v = pv.covariance(i, j) *
430  cov(i, j) = v;
431  }
432  }
433 
434  BeamSpot bs(pv.position(), sigmaZ,
435  beamSpot.isValid() ? beamSpot->dxdz() : 0.,
436  beamSpot.isValid() ? beamSpot->dydz() : 0.,
437  beamWidth, cov, BeamSpot::Unknown);
438 
439  if (useGhostTrack)
440  fittedSVs = vertexRecoGT->vertices(
441  pv, bs, *ghostTrack);
442  else
443  fittedSVs = vertexReco->vertices(fitTracks, bs);
444  } break;
445 
447  std::vector<TransientTrack> primaries_(
448  primaries.begin(), primaries.end());
449  if (useGhostTrack)
450  fittedSVs = vertexRecoGT->vertices(
451  pv, *beamSpot, primaries_,
452  *ghostTrack);
453  else
454  fittedSVs = vertexReco->vertices(
455  primaries_, fitTracks,
456  *beamSpot);
457  } break;
458  }
459 
460  // build combined SV information and filter
461 
462  SVBuilder svBuilder(pv, jetDir, withPVError);
463  std::remove_copy_if(boost::make_transform_iterator(
464  fittedSVs.begin(), svBuilder),
465  boost::make_transform_iterator(
466  fittedSVs.end(), svBuilder),
467  std::back_inserter(SVs),
468  SVFilter(vertexFilter, pv, jetDir));
469 
470  // clean up now unneeded collections
471  }else{
472  for(size_t iExtSv = 0; iExtSv < extSecVertex->size(); iExtSv++){
473  const reco::Vertex & extVertex = (*extSecVertex)[iExtSv];
474 // GlobalVector vtxDir = GlobalVector(extVertex.p4().X(),extVertex.p4().Y(),extVertex.p4().Z());
475 // if(Geom::deltaR(extVertex.position() - pv.position(), vtxDir)>0.2) continue; //pointing angle
476 // std::cout << " dR " << iExtSv << " " << Geom::deltaR( ( extVertex.position() - pv.position() ), jetDir ) << "eta: " << ( extVertex.position() - pv.position()).eta() << " vs " << jetDir.eta() << " phi: " << ( extVertex.position() - pv.position()).phi() << " vs " << jetDir.phi() << std::endl;
477  if( Geom::deltaR( ( extVertex.position() - pv.position() ), jetDir ) > extSVDeltaRToJet || extVertex.p4().M() < 0.3)
478  continue;
479 // std::cout << " SV added " << iExtSv << std::endl;
480  extAssoCollection.push_back( extVertex );
481  }
482  SVBuilder svBuilder(pv, jetDir, withPVError);
483  std::remove_copy_if(boost::make_transform_iterator( extAssoCollection.begin(), svBuilder),
484  boost::make_transform_iterator(extAssoCollection.end(), svBuilder),
485  std::back_inserter(SVs),
486  SVFilter(vertexFilter, pv, jetDir));
487 
488 
489  }
490 // std::cout << "size: " << SVs.size() << std::endl;
491  gtPred.reset();
492  ghostTrack.reset();
493  gtStates.clear();
494  fitTracks.clear();
495  fittedSVs.clear();
496  extAssoCollection.clear();
497 
498  // sort SVs by importance
499 
500  std::vector<unsigned int> vtxIndices = vertexSorting(SVs);
501 
502  std::vector<SecondaryVertexTagInfo::VertexData> svData;
503 
504  svData.resize(vtxIndices.size());
505  for(unsigned int idx = 0; idx < vtxIndices.size(); idx++) {
506  const SecondaryVertex &sv = SVs[vtxIndices[idx]];
507 
508  svData[idx].vertex = sv;
509  svData[idx].dist2d = sv.dist2d();
510  svData[idx].dist3d = sv.dist3d();
511  svData[idx].direction =
512  GlobalVector(sv.x() - pv.x(),
513  sv.y() - pv.y(),
514  sv.z() - pv.z());
515 
516  // mark tracks successfully used in vertex fit
517 
519  iter != sv.tracks_end(); iter++) {
520  if (sv.trackWeight(*iter) < minTrackWeight)
521  continue;
522 
524  std::find(trackRefs.begin(), trackRefs.end(),
525  iter->castTo<TrackRef>());
526 
527  if (pos == trackRefs.end() ) {
528  if(!useExternalSV)
529  throw cms::Exception("TrackNotFound")
530  << "Could not find track from secondary "
531  "vertex in original tracks."
532  << std::endl;
533  } else {
534  unsigned int index = pos - trackRefs.begin();
535  trackData[index].second.svStatus =
537  ((unsigned int)SecondaryVertexTagInfo::TrackData::trackAssociatedToVertex + idx);
538  }
539  }
540  }
541 
542  // fill result into tag infos
543 
544  tagInfos->push_back(
546  trackData, svData, SVs.size(),
547  TrackIPTagInfoRef(trackIPTagInfos,
548  iterJets - trackIPTagInfos->begin())));
549  }
550 
551  event.put(tagInfos);
552 }
553 
554 //define this as a plug-in
math::Error< dimension >::type CovarianceMatrix
Definition: BeamSpot.h:31
reco::Vertex::Point convertPos(const GlobalPoint &p)
T getParameter(std::string const &) const
int i
Definition: DBlmapReader.cc:9
bool existsAs(std::string const &parameterName, bool trackiness=true) const
checks if a parameter exists as a given type
Definition: ParameterSet.h:184
reco::TrackIPTagInfo::SortCriteria getCriterium(const std::string &name)
Definition: TrackSorting.cc:11
trackRef_iterator tracks_end() const
last iterator over tracks
Definition: Vertex.cc:44
static ConstraintType getConstraintType(const std::string &name)
Measurement1D dist3d() const
#define DEFINE_FWK_MODULE(type)
Definition: MakerMacros.h:17
double y() const
y coordinate
Definition: Vertex.h:110
SecondaryVertexProducer(const edm::ParameterSet &params)
reco::Vertex::Error convertError(const GlobalError &ge)
Definition: ConvertError.h:8
edm::EDGetTokenT< reco::VertexCollection > token_extSVCollection
void setWeight(double weight)
double covariance(int i, int j) const
(i, j)-th element of error matrix, i, j = 0, ... 2
Definition: Vertex.h:123
ProductID id() const
Definition: RefToBase.h:220
const_iterator end() const
Termination of iteration.
Definition: RefVector.h:249
void find(edm::Handle< EcalRecHitCollection > &hits, DetId thisDet, std::vector< EcalRecHitCollection::const_iterator > &hit, bool debug=false)
Definition: FindCaloHit.cc:7
const Point & position() const
position
Definition: Vertex.h:106
TrackIPTagInfo::SortCriteria sortCriterium
const_iterator begin() const
Initialize an iterator over the RefVector.
Definition: RefVector.h:244
edm::EDGetTokenT< reco::BeamSpot > token_BeamSpot
edm::RefToBase< reco::Track > TrackBaseRef
persistent reference to a Track, using views
Definition: TrackFwd.h:22
int j
Definition: DBlmapReader.cc:9
double z() const
y coordinate
Definition: Vertex.h:112
float trackWeight(const TREF &r) const
returns the weight with which a Track has contributed to the vertex-fit.
Definition: Vertex.h:74
How EventSelector::AcceptEvent() decides whether to accept an event for output otherwise it is excluding the probing of A single or multiple positive and the trigger will pass if any such matching triggers are PASS or EXCEPTION[A criterion thatmatches no triggers at all is detected and causes a throw.] A single negative with an expectation of appropriate bit checking in the decision and the trigger will pass if any such matching triggers are FAIL or EXCEPTION A wildcarded negative criterion that matches more than one trigger in the trigger but the state exists so we define the behavior If all triggers are the negative crieriion will lead to accepting the event(this again matches the behavior of"!*"before the partial wildcard feature was incorporated).The per-event"cost"of each negative criterion with multiple relevant triggers is about the same as!*was in the past
bool isValid() const
Definition: HandleBase.h:76
size_t key() const
Definition: RefToBase.h:228
Measurement1D dist2d() const
double deltaR(double eta1, double eta2, double phi1, double phi2)
Definition: TreeUtility.cc:17
edm::EDGetTokenT< reco::TrackIPTagInfoCollection > token_trackIPTagInfo
double x() const
x coordinate
Definition: Vertex.h:108
std::pair< unsigned int, TrackData > IndexedTrackData
bool linearize(const GhostTrackPrediction &pred, bool initial=false, double lambda=0.)
tuple idx
DEBUGGING if hasattr(process,&quot;trackMonIterativeTracking2012&quot;): print &quot;trackMonIterativeTracking2012 D...
math::XYZTLorentzVectorD p4(float mass=0.13957018, float minWeight=0.5) const
Returns the four momentum of the sum of the tracks, assuming the given mass for the decay products...
Definition: Vertex.cc:113
const T & get() const
Definition: EventSetup.h:55
virtual Vector momentum() const GCC11_FINAL
spatial momentum vector
Error error() const
return SMatrix
Definition: Vertex.h:129
std::vector< TrackBaseRef >::const_iterator trackRef_iterator
The iteratator for the vector&lt;TrackRef&gt;
Definition: Vertex.h:37
void push_back(value_type const &ref)
Add a Ref&lt;C, T&gt; to the RefVector.
Definition: RefVector.h:64
trackRef_iterator tracks_begin() const
first iterator over tracks
Definition: Vertex.cc:39
static GhostTrackVertexFinder::FitType getGhostTrackFitType(const std::string &name)
virtual void produce(edm::Event &event, const edm::EventSetup &es) override
Global3DVector GlobalVector
Definition: GlobalVector.h:10
tuple trackSelector
Tracks selection.
Definition: valSkim_cff.py:4