CMS 3D CMS Logo

TrackAlgoCompareUtil.cc
Go to the documentation of this file.
2 
3 using namespace std;
4 using namespace edm;
5 
6 // constructors and destructor
8  : trackLabel_algoA(consumes<View<reco::Track>>(iConfig.getParameter<edm::InputTag>("trackLabel_algoA"))),
9  trackLabel_algoB(consumes<View<reco::Track>>(iConfig.getParameter<edm::InputTag>("trackLabel_algoB"))),
11  consumes<TrackingParticleCollection>(iConfig.getParameter<edm::InputTag>("trackingParticleLabel_fakes"))),
13  consumes<TrackingParticleCollection>(iConfig.getParameter<edm::InputTag>("trackingParticleLabel_effic"))),
14  beamSpotLabel(consumes<reco::BeamSpot>(iConfig.getParameter<edm::InputTag>("beamSpotLabel"))),
15  magField(esConsumes()),
16  UseAssociators(iConfig.getParameter<bool>("UseAssociators")),
17  UseVertex(iConfig.getParameter<bool>("UseVertex")) {
18  //now do what ever other initialization is needed
19  if (UseVertex) {
20  vertexLabel_algoA = consumes<reco::VertexCollection>(iConfig.getParameter<edm::InputTag>("vertexLabel_algoA"));
21  vertexLabel_algoB = consumes<reco::VertexCollection>(iConfig.getParameter<edm::InputTag>("vertexLabel_algoB"));
22  }
23 
24  if (UseAssociators) {
25  assocLabel_algoA = consumes<reco::TrackToTrackingParticleAssociator>(
26  iConfig.getUntrackedParameter<std::string>("assocLabel_algoA", "trackAssociatorByHits"));
27  assocLabel_algoB = consumes<reco::TrackToTrackingParticleAssociator>(
28  iConfig.getUntrackedParameter<std::string>("assocLabel_algoB", "trackAssociatorByHits"));
29  } else {
30  edm::InputTag algoA = iConfig.getParameter<edm::InputTag>("associatormap_algoA");
31  edm::InputTag algoB = iConfig.getParameter<edm::InputTag>("associatormap_algoB");
32 
33  associatormap_algoA_recoToSim = consumes<reco::RecoToSimCollection>(algoA);
34  associatormap_algoB_recoToSim = consumes<reco::RecoToSimCollection>(algoB);
35  associatormap_algoA_simToReco = consumes<reco::SimToRecoCollection>(algoA);
36  associatormap_algoB_simToReco = consumes<reco::SimToRecoCollection>(algoB);
37  }
38 
39  produces<RecoTracktoTPCollection>("AlgoA");
40  produces<RecoTracktoTPCollection>("AlgoB");
41  produces<TPtoRecoTrackCollection>("TP");
42 }
43 
45 
46 // ------------ method called to produce the data ------------
48  // create output collection instance
49  auto outputAlgoA = std::make_unique<RecoTracktoTPCollection>();
50  auto outputAlgoB = std::make_unique<RecoTracktoTPCollection>();
51  auto outputTP = std::make_unique<TPtoRecoTrackCollection>();
52 
53  // Get Inputs
54  edm::Handle<reco::BeamSpot> recoBeamSpotHandle;
55  iEvent.getByToken(beamSpotLabel, recoBeamSpotHandle);
56  reco::BeamSpot beamSpot = *recoBeamSpotHandle;
57 
58  edm::Handle<View<reco::Track>> trackCollAlgoA;
59  iEvent.getByToken(trackLabel_algoA, trackCollAlgoA);
60 
61  edm::Handle<View<reco::Track>> trackCollAlgoB;
62  iEvent.getByToken(trackLabel_algoB, trackCollAlgoB);
63 
64  edm::Handle<TrackingParticleCollection> trackingParticleCollFakes;
65  iEvent.getByToken(trackingParticleLabel_fakes, trackingParticleCollFakes);
66 
67  edm::Handle<TrackingParticleCollection> trackingParticleCollEffic;
68  iEvent.getByToken(trackingParticleLabel_effic, trackingParticleCollEffic);
69 
72  if (UseVertex) {
73  iEvent.getByToken(vertexLabel_algoA, vertexCollAlgoA);
74  iEvent.getByToken(vertexLabel_algoB, vertexCollAlgoB);
75  }
76 
77  // call the associator functions:
78  reco::RecoToSimCollection recSimColl_AlgoA;
79  reco::RecoToSimCollection recSimColl_AlgoB;
80 
81  reco::SimToRecoCollection simRecColl_AlgoA;
82  reco::SimToRecoCollection simRecColl_AlgoB;
83 
84  if (UseAssociators) {
86  iEvent.getByToken(assocLabel_algoA, theAssociator_algoA);
87 
89  iEvent.getByToken(assocLabel_algoB, theAssociator_algoB);
90 
91  recSimColl_AlgoA = theAssociator_algoA->associateRecoToSim(trackCollAlgoA, trackingParticleCollFakes);
92  recSimColl_AlgoB = theAssociator_algoB->associateRecoToSim(trackCollAlgoB, trackingParticleCollFakes);
93 
94  simRecColl_AlgoA = theAssociator_algoA->associateSimToReco(trackCollAlgoA, trackingParticleCollEffic);
95  simRecColl_AlgoB = theAssociator_algoB->associateSimToReco(trackCollAlgoB, trackingParticleCollEffic);
96  } else {
97  Handle<reco::RecoToSimCollection> recotosimCollectionH_AlgoA;
98  iEvent.getByToken(associatormap_algoA_recoToSim, recotosimCollectionH_AlgoA);
99  recSimColl_AlgoA = *(recotosimCollectionH_AlgoA.product());
100 
101  Handle<reco::RecoToSimCollection> recotosimCollectionH_AlgoB;
102  iEvent.getByToken(associatormap_algoB_recoToSim, recotosimCollectionH_AlgoB);
103  recSimColl_AlgoB = *(recotosimCollectionH_AlgoB.product());
104 
105  Handle<reco::SimToRecoCollection> simtorecoCollectionH_AlgoA;
106  iEvent.getByToken(associatormap_algoA_simToReco, simtorecoCollectionH_AlgoA);
107  simRecColl_AlgoA = *(simtorecoCollectionH_AlgoA.product());
108 
109  Handle<reco::SimToRecoCollection> simtorecoCollectionH_AlgoB;
110  iEvent.getByToken(associatormap_algoB_simToReco, simtorecoCollectionH_AlgoB);
111  simRecColl_AlgoB = *(simtorecoCollectionH_AlgoB.product());
112  }
113 
114  // define the vector of references to trackingParticleColl associated with a given reco::Track
115  std::vector<std::pair<TrackingParticleRef, double>> associatedTrackingParticles;
116 
117  // define the vector of references to trackColl associated with a given TrackingParticle
118  std::vector<std::pair<reco::TrackBaseRef, double>> associatedRecoTracks;
119 
120  // Get the magnetic field data from the event (used to calculate the point of closest TrackingParticle)
121  const MagneticField* magneticField = &iSetup.getData(magField);
122 
123  // fill collection algoA
124  for (View<reco::Track>::size_type i = 0; i < trackCollAlgoA->size(); ++i) {
125  // get recoTrack algo A
126  reco::TrackBaseRef recoTrack(trackCollAlgoA, i);
127  RecoTracktoTP recoTracktoTP;
128  recoTracktoTP.SetRecoTrack(recoTrack);
129  recoTracktoTP.SetBeamSpot(beamSpot.position());
130 
131  // get the associated trackingParticle
132  if (recSimColl_AlgoA.find(recoTrack) != recSimColl_AlgoA.end()) {
133  associatedTrackingParticles = recSimColl_AlgoA[recoTrack];
134  recoTracktoTP.SetTrackingParticle(associatedTrackingParticles.begin()->first);
135  recoTracktoTP.SetShared(associatedTrackingParticles.begin()->second);
136  SetTrackingParticleD0Dz(associatedTrackingParticles.begin()->first, beamSpot, magneticField, recoTracktoTP);
137  } else {
138  recoTracktoTP.SetTrackingParticle(TrackingParticleRef());
139  recoTracktoTP.SetShared(-1.0);
140  }
141 
142  // get the reco primary vertex info
143  if (UseVertex && !vertexCollAlgoA->empty()) {
144  recoTracktoTP.SetRecoVertex(reco::VertexRef(vertexCollAlgoA, 0));
145  } else {
146  recoTracktoTP.SetRecoVertex(reco::VertexRef());
147  }
148 
149  outputAlgoA->push_back(recoTracktoTP);
150  }
151 
152  // fill collection algoB
153  for (reco::TrackCollection::size_type i = 0; i < trackCollAlgoB->size(); ++i) {
154  // get recoTrack algo B
155  reco::TrackBaseRef recoTrack(trackCollAlgoB, i);
156  RecoTracktoTP recoTracktoTP;
157  recoTracktoTP.SetRecoTrack(recoTrack);
158  recoTracktoTP.SetBeamSpot(beamSpot.position());
159 
160  // get the associated trackingParticle
161  if (recSimColl_AlgoB.find(recoTrack) != recSimColl_AlgoB.end()) {
162  associatedTrackingParticles = recSimColl_AlgoB[recoTrack];
163  recoTracktoTP.SetTrackingParticle(associatedTrackingParticles.begin()->first);
164  recoTracktoTP.SetShared(associatedTrackingParticles.begin()->second);
165  SetTrackingParticleD0Dz(associatedTrackingParticles.begin()->first, beamSpot, magneticField, recoTracktoTP);
166  } else {
167  recoTracktoTP.SetTrackingParticle(TrackingParticleRef());
168  recoTracktoTP.SetShared(-1.0);
169  }
170 
171  // get the reco primary vertex info
172  if (UseVertex && !vertexCollAlgoB->empty()) {
173  recoTracktoTP.SetRecoVertex(reco::VertexRef(vertexCollAlgoB, 0));
174  } else {
175  recoTracktoTP.SetRecoVertex(reco::VertexRef());
176  }
177 
178  outputAlgoB->push_back(recoTracktoTP);
179  }
180 
181  for (TrackingParticleCollection::size_type i = 0; i < trackingParticleCollEffic->size(); ++i) {
182  // initialize the trackingParticle (sim) info
183  TrackingParticleRef tparticle(trackingParticleCollEffic, i);
184  TPtoRecoTrack tptoRecoTrack;
185  tptoRecoTrack.SetBeamSpot(beamSpot.position());
186  tptoRecoTrack.SetTrackingParticle(tparticle);
187  SetTrackingParticleD0Dz(tparticle, beamSpot, magneticField, tptoRecoTrack);
188 
189  // get the assocated recoTrack algoA
190  if (simRecColl_AlgoA.find(tparticle) != simRecColl_AlgoA.end()) {
191  associatedRecoTracks = simRecColl_AlgoA[tparticle];
192  tptoRecoTrack.SetRecoTrack_AlgoA(associatedRecoTracks.begin()->first);
193  tptoRecoTrack.SetShared_AlgoA(associatedRecoTracks.begin()->second);
194  } else {
195  tptoRecoTrack.SetRecoTrack_AlgoA(reco::TrackBaseRef());
196  tptoRecoTrack.SetShared_AlgoA(-1.0);
197  }
198 
199  // get the recoVertex algo A
200  if (UseVertex && !vertexCollAlgoA->empty()) {
201  tptoRecoTrack.SetRecoVertex_AlgoA(reco::VertexRef(vertexCollAlgoA, 0));
202  } else {
203  tptoRecoTrack.SetRecoVertex_AlgoA(reco::VertexRef());
204  }
205 
206  // get the assocated recoTrack algoB
207  if (simRecColl_AlgoB.find(tparticle) != simRecColl_AlgoB.end()) {
208  associatedRecoTracks = simRecColl_AlgoB[tparticle];
209  tptoRecoTrack.SetRecoTrack_AlgoB(associatedRecoTracks.begin()->first);
210  tptoRecoTrack.SetShared_AlgoB(associatedRecoTracks.begin()->second);
211  } else {
212  tptoRecoTrack.SetRecoTrack_AlgoB(reco::TrackBaseRef());
213  tptoRecoTrack.SetShared_AlgoB(-1.0);
214  }
215  // get the recoVertex algo B
216  if (UseVertex && !vertexCollAlgoB->empty()) {
217  tptoRecoTrack.SetRecoVertex_AlgoB(reco::VertexRef(vertexCollAlgoB, 0));
218  } else {
219  tptoRecoTrack.SetRecoVertex_AlgoB(reco::VertexRef());
220  }
221 
222  outputTP->push_back(tptoRecoTrack);
223  }
224 
225  // put the collection in the event record
226  iEvent.put(std::move(outputAlgoA), "AlgoA");
227  iEvent.put(std::move(outputAlgoB), "AlgoB");
228  iEvent.put(std::move(outputTP), "TP");
229 }
230 
231 // ------------ Producer Specific Meber Fucntions ----------------------------------------
233  const reco::BeamSpot& bs,
234  const MagneticField* bf,
235  TPtoRecoTrack& TPRT) const {
236  GlobalPoint trackingParticleVertex(tp->vertex().x(), tp->vertex().y(), tp->vertex().z());
237  GlobalVector trackingParticleP3(
238  tp->g4Track_begin()->momentum().x(), tp->g4Track_begin()->momentum().y(), tp->g4Track_begin()->momentum().z());
239  TrackCharge trackingParticleCharge(tp->charge());
240 
241  FreeTrajectoryState ftsAtProduction(trackingParticleVertex, trackingParticleP3, trackingParticleCharge, bf);
242  TSCBLBuilderNoMaterial tscblBuilder;
243  TrajectoryStateClosestToBeamLine tsAtClosestApproach =
244  tscblBuilder(ftsAtProduction, bs); //as in TrackProducerAlgorithm
245 
246  if (tsAtClosestApproach.isValid()) {
247  GlobalPoint v1 = tsAtClosestApproach.trackStateAtPCA().position();
248  GlobalVector p = tsAtClosestApproach.trackStateAtPCA().momentum();
249 
251  TPRT.SetTrackingParticlePCA(v1);
252  } else {
253  TPRT.SetTrackingParticleMomentumPCA(GlobalVector(-9999.0, -9999.0, -9999.0));
254  TPRT.SetTrackingParticlePCA(GlobalPoint(-9999.0, -9999.0, -9999.0));
255  }
256 }
257 
259  const reco::BeamSpot& bs,
260  const MagneticField* bf,
261  RecoTracktoTP& RTTP) const {
262  GlobalPoint trackingParticleVertex(tp->vertex().x(), tp->vertex().y(), tp->vertex().z());
263  GlobalVector trackingParticleP3(
264  tp->g4Track_begin()->momentum().x(), tp->g4Track_begin()->momentum().y(), tp->g4Track_begin()->momentum().z());
265  TrackCharge trackingParticleCharge(tp->charge());
266 
267  FreeTrajectoryState ftsAtProduction(trackingParticleVertex, trackingParticleP3, trackingParticleCharge, bf);
268  TSCBLBuilderNoMaterial tscblBuilder;
269  TrajectoryStateClosestToBeamLine tsAtClosestApproach =
270  tscblBuilder(ftsAtProduction, bs); //as in TrackProducerAlgorithm
271 
272  if (tsAtClosestApproach.isValid()) {
273  GlobalPoint v1 = tsAtClosestApproach.trackStateAtPCA().position();
274  GlobalVector p = tsAtClosestApproach.trackStateAtPCA().momentum();
275 
277  RTTP.SetTrackingParticlePCA(v1);
278  } else {
279  RTTP.SetTrackingParticleMomentumPCA(GlobalVector(-9999.0, -9999.0, -9999.0));
280  RTTP.SetTrackingParticlePCA(GlobalPoint(-9999.0, -9999.0, -9999.0));
281  }
282 }
reco::SimToRecoCollection associateSimToReco(const edm::Handle< edm::View< reco::Track >> &tCH, const edm::Handle< TrackingParticleCollection > &tPCH) const
unsigned int size_type
Definition: View.h:92
void SetTrackingParticle(TrackingParticleRef tp)
Definition: RecoTracktoTP.h:25
ESGetTokenH3DDVariant esConsumes(std::string const &Record, edm::ConsumesCollector &)
Definition: DeDxTools.cc:283
T getParameter(std::string const &) const
Definition: ParameterSet.h:307
const edm::ESGetToken< MagneticField, IdealMagneticFieldRecord > magField
void SetTrackingParticleMomentumPCA(const GlobalVector &p)
Definition: RecoTracktoTP.h:55
T const & getData(const ESGetToken< T, R > &iToken) const noexcept(false)
Definition: EventSetup.h:119
reco::RecoToSimCollection associateRecoToSim(const edm::Handle< edm::View< reco::Track >> &tCH, const edm::Handle< TrackingParticleCollection > &tPCH) const
void SetTrackingParticle(TrackingParticleRef tp)
Definition: TPtoRecoTrack.h:25
void SetShared_AlgoA(const float &mA)
Definition: TPtoRecoTrack.h:30
void SetTrackingParticleMomentumPCA(const GlobalVector &p)
Definition: TPtoRecoTrack.h:82
edm::EDGetTokenT< reco::SimToRecoCollection > associatormap_algoB_simToReco
edm::EDGetTokenT< reco::TrackToTrackingParticleAssociator > assocLabel_algoA
T const * product() const
Definition: Handle.h:70
void SetShared(const float &m)
Definition: RecoTracktoTP.h:29
Global3DPoint GlobalPoint
Definition: GlobalPoint.h:10
edm::EDGetTokenT< reco::VertexCollection > vertexLabel_algoA
void SetTrackingParticlePCA(const GlobalPoint &v)
Definition: RecoTracktoTP.h:56
void SetRecoVertex(reco::VertexRef vertex)
Definition: RecoTracktoTP.h:27
void SetRecoTrack_AlgoA(reco::TrackBaseRef track)
Definition: TPtoRecoTrack.h:27
const edm::EDGetTokenT< TrackingParticleCollection > trackingParticleLabel_effic
void SetTrackingParticlePCA(const GlobalPoint &v)
Definition: TPtoRecoTrack.h:83
uint16_t size_type
const_iterator find(const key_type &k) const
find element with specified reference key
const_iterator end() const
last iterator over the map (read only)
T getUntrackedParameter(std::string const &, T const &) const
GlobalPoint position() const
int TrackCharge
Definition: TrackCharge.h:4
edm::EDGetTokenT< reco::RecoToSimCollection > associatormap_algoA_recoToSim
const edm::EDGetTokenT< edm::View< reco::Track > > trackLabel_algoA
edm::EDGetTokenT< reco::VertexCollection > vertexLabel_algoB
int iEvent
Definition: GenABIO.cc:224
const edm::EDGetTokenT< TrackingParticleCollection > trackingParticleLabel_fakes
void SetRecoVertex_AlgoB(reco::VertexRef vertex)
Definition: TPtoRecoTrack.h:34
const edm::EDGetTokenT< edm::View< reco::Track > > trackLabel_algoB
GlobalVector momentum() const
void SetRecoVertex_AlgoA(reco::VertexRef vertex)
Definition: TPtoRecoTrack.h:33
TrackAlgoCompareUtil(const edm::ParameterSet &)
edm::EDGetTokenT< reco::SimToRecoCollection > associatormap_algoA_simToReco
const edm::EDGetTokenT< reco::BeamSpot > beamSpotLabel
edm::EDGetTokenT< reco::TrackToTrackingParticleAssociator > assocLabel_algoB
void SetShared_AlgoB(const float &mB)
Definition: TPtoRecoTrack.h:31
void SetRecoTrack(reco::TrackBaseRef track)
Definition: RecoTracktoTP.h:26
void SetBeamSpot(const math::XYZPoint &bs)
Definition: TPtoRecoTrack.h:36
fixed size matrix
HLT enums.
void SetBeamSpot(const math::XYZPoint &bs)
Definition: RecoTracktoTP.h:28
void SetTrackingParticleD0Dz(TrackingParticleRef tp, const reco::BeamSpot &bs, const MagneticField *bf, TPtoRecoTrack &TPRT) const
void SetRecoTrack_AlgoB(reco::TrackBaseRef track)
Definition: TPtoRecoTrack.h:28
std::vector< TrackingParticle > TrackingParticleCollection
edm::EDGetTokenT< reco::RecoToSimCollection > associatormap_algoB_recoToSim
edm::Ref< TrackingParticleCollection > TrackingParticleRef
def move(src, dest)
Definition: eostools.py:511
Global3DVector GlobalVector
Definition: GlobalVector.h:10
void produce(edm::StreamID, edm::Event &, const edm::EventSetup &) const override