CMS 3D CMS Logo

VertexAssociatorByPositionAndTracks.cc
Go to the documentation of this file.
3 #include "CLHEP/Units/GlobalSystemOfUnits.h"
4 
6 
8  double absZ,
9  double sigmaZ,
10  double maxRecoZ,
11  double absT,
12  double sigmaT,
13  double maxRecoT,
14  double sharedTrackFraction,
15  const reco::RecoToSimCollection *trackRecoToSimAssociation,
16  const reco::SimToRecoCollection *trackSimToRecoAssociation):
17  productGetter_(productGetter),
18  absZ_(absZ),
19  sigmaZ_(sigmaZ),
20  maxRecoZ_(maxRecoZ),
21  absT_(absT),
22  sigmaT_(sigmaT),
23  maxRecoT_(maxRecoT),
24  sharedTrackFraction_(sharedTrackFraction),
25  trackRecoToSimAssociation_(trackRecoToSimAssociation),
26  trackSimToRecoAssociation_(trackSimToRecoAssociation)
27 {}
28 
30  double absZ,
31  double sigmaZ,
32  double maxRecoZ,
33  double sharedTrackFraction,
34  const reco::RecoToSimCollection *trackRecoToSimAssociation,
35  const reco::SimToRecoCollection *trackSimToRecoAssociation):
36  productGetter_(productGetter),
37  absZ_(absZ),
38  sigmaZ_(sigmaZ),
39  maxRecoZ_(maxRecoZ),
40  absT_(std::numeric_limits<double>::max()),
41  sigmaT_(std::numeric_limits<double>::max()),
42  maxRecoT_(std::numeric_limits<double>::max()),
43  sharedTrackFraction_(sharedTrackFraction),
44  trackRecoToSimAssociation_(trackRecoToSimAssociation),
45  trackSimToRecoAssociation_(trackSimToRecoAssociation)
46 {}
47 
49 
51  const edm::Handle<TrackingVertexCollection>& tVCH) const {
53 
54  const edm::View<reco::Vertex>& recoVertices = *vCH;
55  const TrackingVertexCollection& simVertices = *tVCH;
56 
57  LogDebug("VertexAssociation") << "VertexAssociatorByPositionAndTracks::associateRecoToSim(): associating "
58  << recoVertices.size() << " reco::Vertices to" << simVertices.size() << " TrackingVertices";
59 
60  // filter sim PVs
61  std::vector<size_t> simPVindices;
62  simPVindices.reserve(recoVertices.size());
63  {
64  int current_event = -1;
65  for(size_t iSim=0; iSim != simVertices.size(); ++iSim) {
66  const TrackingVertex& simVertex = simVertices[iSim];
67 
68  // Associate only to primary vertices of the in-time pileup
69  // events (BX=0, first vertex in each of the events)
70  if(simVertex.eventId().bunchCrossing() != 0) continue;
71  if(simVertex.eventId().event() != current_event) {
72  current_event = simVertex.eventId().event();
73  simPVindices.push_back(iSim);
74  }
75  }
76  }
77 
78  for(size_t iReco=0; iReco != recoVertices.size(); ++iReco) {
79  const reco::Vertex& recoVertex = recoVertices[iReco];
80 
81  // skip fake vertices
82  if(std::abs(recoVertex.z()) > maxRecoZ_ || recoVertex.isFake() || !recoVertex.isValid() || recoVertex.ndof() < 0.)
83  continue;
84 
85  LogTrace("VertexAssociation") << " reco::Vertex at Z " << recoVertex.z();
86 
87  for(const size_t iSim: simPVindices) {
88  const TrackingVertex& simVertex = simVertices[iSim];
89  LogTrace("VertexAssociation") << " Considering TrackingVertex at Z " << simVertex.position().z();
90 
91  // recoVertex.t() == 0. is a special value
92  // need to change this to std::numeric_limits<double>::max() or something more clear
93  const bool useTiming = ( absT_ != std::numeric_limits<double>::max() && recoVertex.t() != 0. );
94  if( useTiming ) {
95  LogTrace("VertexAssociation") << " and T " << recoVertex.t()*CLHEP::second << std::endl;
96  }
97 
98  const double tdiff = std::abs(recoVertex.t() - simVertex.position().t()*CLHEP::second);
99  const double zdiff = std::abs(recoVertex.z() - simVertex.position().z());
100  if( zdiff < absZ_ && zdiff / recoVertex.zError() < sigmaZ_ &&
101  ( !useTiming || ( tdiff < absT_ && tdiff / recoVertex.tError() < sigmaT_ ) ) ) {
102  auto sharedTracks = calculateVertexSharedTracks(recoVertex, simVertex, *trackRecoToSimAssociation_);
103  auto fraction = double(sharedTracks)/recoVertex.tracksSize();
104  if(sharedTrackFraction_ < 0 || fraction > sharedTrackFraction_) {
105  LogTrace("VertexAssociation") << " Matched with significance " << zdiff/recoVertex.zError() << " " << tdiff/recoVertex.tError()
106  << " shared tracks " << sharedTracks << " reco Tracks " << recoVertex.tracksSize() << " TrackingParticles " << simVertex.nDaughterTracks();
107 
108  ret.insert(reco::VertexBaseRef(vCH, iReco), std::make_pair(TrackingVertexRef(tVCH, iSim), sharedTracks));
109  }
110  }
111  }
112  }
113 
114  ret.post_insert();
115 
116  LogDebug("VertexAssociation") << "VertexAssociatorByPositionAndTracks::associateRecoToSim(): finished";
117 
118  return ret;
119 }
120 
122  const edm::Handle<TrackingVertexCollection>& tVCH) const {
124 
125  const edm::View<reco::Vertex>& recoVertices = *vCH;
126  const TrackingVertexCollection& simVertices = *tVCH;
127 
128  LogDebug("VertexAssociation") << "VertexAssociatorByPositionAndTracks::associateSimToReco(): associating "
129  << simVertices.size() << " TrackingVertices to " << recoVertices.size() << " reco::Vertices";
130 
131  int current_event = -1;
132  for(size_t iSim=0; iSim != simVertices.size(); ++iSim) {
133  const TrackingVertex& simVertex = simVertices[iSim];
134 
135  // Associate only primary vertices of the in-time pileup
136  // events (BX=0, first vertex in each of the events)
137  if(simVertex.eventId().bunchCrossing() != 0) continue;
138  if(simVertex.eventId().event() != current_event) {
139  current_event = simVertex.eventId().event();
140  }
141  else {
142  continue;
143  }
144 
145  LogTrace("VertexAssociation") << " TrackingVertex at Z " << simVertex.position().z();
146 
147  for(size_t iReco=0; iReco != recoVertices.size(); ++iReco) {
148  const reco::Vertex& recoVertex = recoVertices[iReco];
149 
150  // skip fake vertices
151  if(std::abs(recoVertex.z()) > maxRecoZ_ || recoVertex.isFake() || !recoVertex.isValid() || recoVertex.ndof() < 0.)
152  continue;
153 
154  LogTrace("VertexAssociation") << " Considering reco::Vertex at Z " << recoVertex.z();
155  const bool useTiming = ( absT_ != std::numeric_limits<double>::max() && recoVertex.t() != 0. );
156  if( useTiming ) {
157  LogTrace("VertexAssociation") << " and T " << recoVertex.t()*CLHEP::second << std::endl;
158  }
159 
160  const double tdiff = std::abs(recoVertex.t() - simVertex.position().t()*CLHEP::second);
161  const double zdiff = std::abs(recoVertex.z() - simVertex.position().z());
162  if( zdiff < absZ_ && zdiff / recoVertex.zError() < sigmaZ_ &&
163  ( !useTiming || ( tdiff < absT_ && tdiff / recoVertex.tError() < sigmaT_ ) ) ) {
164  auto sharedTracks = calculateVertexSharedTracks(simVertex, recoVertex, *trackSimToRecoAssociation_);
165  auto fraction = double(sharedTracks)/recoVertex.tracksSize();
166  if(sharedTrackFraction_ < 0 || fraction > sharedTrackFraction_) {
167  LogTrace("VertexAssociation") << " Matched with significance " << zdiff/recoVertex.zError() << " " << tdiff/recoVertex.tError()
168  << " shared tracks " << sharedTracks << " reco Tracks " << recoVertex.tracksSize() << " TrackingParticles " << simVertex.nDaughterTracks();
169 
170  ret.insert(TrackingVertexRef(tVCH, iSim), std::make_pair(reco::VertexBaseRef(vCH, iReco), sharedTracks));
171  }
172  }
173  }
174  }
175 
176  ret.post_insert();
177 
178  LogDebug("VertexAssociation") << "VertexAssociatorByPositionAndTracks::associateSimToReco(): finished";
179 
180  return ret;
181 }
#define LogDebug(id)
int event() const
get the contents of the subdetector field (should be protected?)
double zError() const
error on z
Definition: Vertex.h:123
bool isValid() const
Tells whether the vertex is valid.
Definition: Vertex.h:68
size_type size() const
const reco::RecoToSimCollection * trackRecoToSimAssociation_
EDProductGetter const * productGetter(std::atomic< void const * > const &iCache)
virtual reco::VertexSimToRecoCollection associateSimToReco(const edm::Handle< edm::View< reco::Vertex > > &vCH, const edm::Handle< TrackingVertexCollection > &tVCH) const
compare reco to sim the handle of reco::Vertex and TrackingVertex collections
U second(std::pair< T, U > const &p)
void post_insert()
post insert action
int bunchCrossing() const
get the detector field from this detid
Abs< T >::type abs(const T &t)
Definition: Abs.h:22
double z() const
z coordinate
Definition: Vertex.h:115
VertexAssociatorByPositionAndTracks(const edm::EDProductGetter *productGetter, double absZ, double sigmaZ, double maxRecoZ, double absT, double sigmaT, double maxRecoT, double sharedTrackFraction, const reco::RecoToSimCollection *trackRecoToSimAssociation, const reco::SimToRecoCollection *trackSimToRecoAssociation)
#define LogTrace(id)
virtual reco::VertexRecoToSimCollection associateRecoToSim(const edm::Handle< edm::View< reco::Vertex > > &vCH, const edm::Handle< TrackingVertexCollection > &tVCH) const
compare reco to sim the handle of reco::Vertex and TrackingVertex collections
double ndof() const
Definition: Vertex.h:105
edm::Ref< TrackingVertexCollection > TrackingVertexRef
bool isFake() const
Definition: Vertex.h:72
unsigned int nDaughterTracks() const
void insert(const key_type &k, const data_type &v)
insert an association
std::vector< TrackingVertex > TrackingVertexCollection
const EncodedEventId & eventId() const
unsigned int calculateVertexSharedTracks(const reco::Vertex &recoV, const TrackingVertex &simV, const reco::RecoToSimCollection &trackRecoToSimAssociation)
const reco::SimToRecoCollection * trackSimToRecoAssociation_
double tError() const
error on t
Definition: Vertex.h:125
size_t tracksSize() const
number of tracks
Definition: Vertex.cc:71
double t() const
t coordinate
Definition: Vertex.h:117
const LorentzVector & position() const