-
Notifications
You must be signed in to change notification settings - Fork 4.2k
/
MuonTrackLoader.cc
828 lines (667 loc) · 33.4 KB
/
MuonTrackLoader.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
/** \class MuonTrackLoader
* Class to load the product in the event
*
* \author R. Bellan - INFN Torino <riccardo.bellan@cern.ch>
*/
#include <memory>
#include "RecoMuon/TrackingTools/interface/MuonTrackLoader.h"
#include "RecoMuon/TrackingTools/interface/MuonPatternRecoDumper.h"
#include "RecoMuon/TrackingTools/interface/MuonServiceProxy.h"
#include "RecoMuon/TrackingTools/interface/MuonUpdatorAtVertex.h"
#include "TrackingTools/PatternTools/interface/Trajectory.h"
#include "TrackingTools/TrackFitters/interface/TrajectoryFitter.h"
#include "TrackingTools/PatternTools/interface/TrajTrackAssociation.h"
#include "TrackingTools/TransientTrack/interface/TransientTrack.h"
#include "TrackingTools/GeomPropagators/interface/TrackerBounds.h"
#include "TrackingTools/PatternTools/interface/TrajectorySmoother.h"
#include "TrackingTools/Records/interface/TrackingComponentsRecord.h"
#include "TrackingTools/DetLayers/interface/BarrelDetLayer.h"
#include "TrackingTools/DetLayers/interface/ForwardDetLayer.h"
#include "Geometry/CommonDetUnit/interface/GeomDet.h"
#include "FWCore/ParameterSet/interface/ParameterSet.h"
#include "FWCore/Framework/interface/Event.h"
#include "FWCore/MessageLogger/interface/MessageLogger.h"
#include "DataFormats/Math/interface/LorentzVector.h"
#include "DataFormats/TrackReco/interface/Track.h"
#include "DataFormats/TrackReco/interface/TrackExtra.h"
#include "DataFormats/TrackReco/interface/TrackToTrackMap.h"
#include "DataFormats/MuonReco/interface/MuonTrackLinks.h"
#include "DataFormats/MuonReco/interface/MuonFwd.h"
#include "TrackingTools/Records/interface/TransientRecHitRecord.h"
#include "RecoTracker/TransientTrackingRecHit/interface/Traj2TrackHits.h"
using namespace edm;
using namespace std;
using namespace reco;
std::vector<const TrackingRecHit*> MuonTrackLoader::unpackHit(const TrackingRecHit& hit) {
// get rec hit det id and rec hit type
DetId id = hit.geographicalId();
uint16_t detid = id.det();
std::vector<const TrackingRecHit*> hits;
if (detid == DetId::Tracker) {
hits.push_back(&hit);
} else if (detid == DetId::Muon) {
uint16_t subdet = id.subdetId();
if (subdet == (uint16_t)MuonSubdetId::DT) {
if (hit.dimension() == 1) { // DT rechit (granularity 2)
hits.push_back(&hit);
} else if (hit.dimension() > 1) { // 4D segment (granularity 0).
// Both 2 and 4 dim cases. MB4s have 2D, but formatted in 4D segment
// 4D --> 2D
std::vector<const TrackingRecHit*> seg2D = hit.recHits();
// load 1D hits (2D --> 1D)
for (std::vector<const TrackingRecHit*>::const_iterator it = seg2D.begin(); it != seg2D.end(); ++it) {
std::vector<const TrackingRecHit*> hits1D = (*it)->recHits();
copy(hits1D.begin(), hits1D.end(), back_inserter(hits));
}
}
} else if (subdet == (uint16_t)MuonSubdetId::CSC) {
if (hit.dimension() == 2) { // CSC rechit (granularity 2)
hits.push_back(&hit);
} else if (hit.dimension() == 4) { // 4D segment (granularity 0)
// load 2D hits (4D --> 1D)
hits = hit.recHits();
}
} else if (subdet == (uint16_t)MuonSubdetId::RPC) {
hits.push_back(&hit);
} else if (subdet == (uint16_t)MuonSubdetId::GEM) {
if (hit.dimension() == 2) { // GEM rechit
hits.push_back(&hit);
} else if (hit.dimension() == 4) { // GEM segment
hits = hit.recHits();
}
} else if (subdet == (uint16_t)MuonSubdetId::ME0) { //segment
hits = hit.recHits();
}
}
return hits;
}
// constructor (obsolete, should use eventSetUp not service..)
MuonTrackLoader::MuonTrackLoader(ParameterSet& parameterSet,
edm::ConsumesCollector& iC,
const MuonServiceProxy* service)
: theService(service) {
// option to do or not the smoothing step.
// the trajectories which are passed to the track loader are supposed to be non-smoothed
theSmoothingStep = parameterSet.getParameter<bool>("DoSmoothing");
if (theSmoothingStep) {
auto smootherName = parameterSet.getParameter<string>("Smoother");
theSmootherToken = iC.esConsumes(edm::ESInputTag("", smootherName));
auto trackerRecHitBuilderName = parameterSet.getParameter<std::string>("TTRHBuilder");
theTrackerRecHitBuilderToken = iC.esConsumes(edm::ESInputTag("", trackerRecHitBuilderName));
}
// update at vertex
theUpdatingAtVtx = parameterSet.getParameter<bool>("VertexConstraint");
// beam spot input tag
theBeamSpotInputTag = parameterSet.getParameter<edm::InputTag>("beamSpot");
theBeamSpotToken = iC.consumes<reco::BeamSpot>(theBeamSpotInputTag);
// Flag to put the trajectory into the event
theTrajectoryFlag = parameterSet.getUntrackedParameter<bool>("PutTrajectoryIntoEvent", true);
theL2SeededTkLabel = parameterSet.getUntrackedParameter<string>("MuonSeededTracksInstance", string());
ParameterSet updatorPar = parameterSet.getParameter<ParameterSet>("MuonUpdatorAtVertexParameters");
theUpdatorAtVtx = std::make_unique<MuonUpdatorAtVertex>(updatorPar, service);
thePutTkTrackFlag = parameterSet.getUntrackedParameter<bool>("PutTkTrackIntoEvent", false);
theSmoothTkTrackFlag = parameterSet.getUntrackedParameter<bool>("SmoothTkTrack", false);
theAllowNoVtxFlag = parameterSet.getUntrackedParameter<bool>("AllowNoVertex", false);
}
MuonTrackLoader::~MuonTrackLoader() {}
OrphanHandle<reco::TrackCollection> MuonTrackLoader::loadTracks(TrajectoryContainer& trajectories,
Event& event,
const TrackerTopology& ttopo,
const string& instance,
bool reallyDoSmoothing) {
std::vector<bool> dummyVecBool;
return loadTracks(trajectories, event, dummyVecBool, ttopo, instance, reallyDoSmoothing);
}
OrphanHandle<reco::TrackCollection> MuonTrackLoader::loadTracks(TrajectoryContainer& trajectories,
Event& event,
std::vector<bool>& tkBoolVec,
const TrackerTopology& ttopo,
const string& instance,
bool reallyDoSmoothing) {
const bool doSmoothing = theSmoothingStep && reallyDoSmoothing;
const string metname = "Muon|RecoMuon|MuonTrackLoader";
// the track collectios; they will be loaded in the event
auto trackCollection = std::make_unique<reco::TrackCollection>();
// ... and its reference into the event
reco::TrackRefProd trackCollectionRefProd = event.getRefBeforePut<reco::TrackCollection>(instance);
// track collection for the tracks updated at vertex
auto updatedAtVtxTrackCollection = std::make_unique<reco::TrackCollection>();
// ... and its (eventually) reference into the event
reco::TrackRefProd trackUpdatedCollectionRefProd;
if (theUpdatingAtVtx)
trackUpdatedCollectionRefProd = event.getRefBeforePut<reco::TrackCollection>(instance + "UpdatedAtVtx");
// Association map between updated and non updated at vtx tracks
auto trackToTrackmap = std::make_unique<reco::TrackToTrackMap>(trackCollectionRefProd, trackUpdatedCollectionRefProd);
// the track extra collection, it will be loaded in the event
auto trackExtraCollection = std::make_unique<reco::TrackExtraCollection>();
// ... and its reference into the event
reco::TrackExtraRefProd trackExtraCollectionRefProd = event.getRefBeforePut<reco::TrackExtraCollection>(instance);
// the rechit collection, it will be loaded in the event
auto recHitCollection = std::make_unique<TrackingRecHitCollection>();
// ... and its reference into the event
TrackingRecHitRefProd recHitCollectionRefProd = event.getRefBeforePut<TrackingRecHitCollection>(instance);
// Collection of Trajectory
auto trajectoryCollection = std::make_unique<vector<Trajectory>>();
// don't waste any time...
if (trajectories.empty()) {
event.put(std::move(recHitCollection), instance);
event.put(std::move(trackExtraCollection), instance);
if (theTrajectoryFlag) {
event.put(std::move(trajectoryCollection), instance);
// Association map between track and trajectory
auto trajTrackMap = std::make_unique<TrajTrackAssociationCollection>();
event.put(std::move(trajTrackMap), instance);
}
if (theUpdatingAtVtx) {
event.put(std::move(trackToTrackmap));
event.put(std::move(updatedAtVtxTrackCollection), instance + "UpdatedAtVtx");
}
return event.put(std::move(trackCollection), instance);
}
edm::Handle<reco::BeamSpot> beamSpot;
event.getByToken(theBeamSpotToken, beamSpot);
LogTrace(metname) << "Create the collection of Tracks";
reco::TrackRef::key_type trackIndex = 0;
reco::TrackRef::key_type trackUpdatedIndex = 0;
reco::TrackExtraRef::key_type trackExtraIndex = 0;
edm::Ref<reco::TrackCollection>::key_type iTkRef = 0;
edm::Ref<std::vector<Trajectory>>::key_type iTjRef = 0;
std::map<unsigned int, unsigned int> tjTkMap;
if (doSmoothing) {
TrajectorySmoother const& aSmoother = theService->eventSetup().getData(theSmootherToken);
theSmoother.reset(aSmoother.clone());
TransientTrackingRecHitBuilder const& theTrackerRecHitBuilder =
theService->eventSetup().getData(theTrackerRecHitBuilderToken);
hitCloner = static_cast<TkTransientTrackingRecHitBuilder const&>(theTrackerRecHitBuilder).cloner();
theSmoother->setHitCloner(&hitCloner);
}
unsigned int tjCnt = 0;
for (TrajectoryContainer::iterator itRawTrajectory = trajectories.begin(); itRawTrajectory != trajectories.end();
++itRawTrajectory, ++tjCnt) {
auto rawTrajectory = std::move(*itRawTrajectory);
Trajectory& trajectory = *rawTrajectory;
if (doSmoothing) {
vector<Trajectory> trajectoriesSM = theSmoother->trajectories(*rawTrajectory);
if (!trajectoriesSM.empty()) {
const edm::RefToBase<TrajectorySeed> tmpSeedRef = (*rawTrajectory).seedRef();
trajectory = trajectoriesSM.front();
trajectory.setSeedRef(tmpSeedRef);
LogDebug(metname) << "theSeedRef.isNonnull " << trajectory.seedRef().isNonnull();
} else
LogInfo(metname) << "The trajectory has not been smoothed!" << endl;
}
if (theTrajectoryFlag) {
trajectoryCollection->push_back(trajectory);
iTjRef++;
}
// build the "bare" track from the trajectory.
// This track has the parameters defined at PCA (no update)
pair<bool, reco::Track> resultOfTrackExtrapAtPCA = buildTrackAtPCA(trajectory, *beamSpot);
// Check if the extrapolation went well
if (!resultOfTrackExtrapAtPCA.first) {
continue;
}
// take the "bare" track at PCA
reco::Track& track = resultOfTrackExtrapAtPCA.second;
// build the "bare" track extra from the trajectory
reco::TrackExtra trackExtra = buildTrackExtra(trajectory);
// get the TrackExtraRef (persitent reference of the track extra)
reco::TrackExtraRef trackExtraRef(trackExtraCollectionRefProd, trackExtraIndex++);
// set the persistent track-extra reference to the Track
track.setExtra(trackExtraRef);
// build the updated-at-vertex track, starting from the previous track
pair<bool, reco::Track> updateResult(false, reco::Track());
if (theUpdatingAtVtx) {
// build the "bare" track UPDATED at vtx
updateResult = buildTrackUpdatedAtPCA(track, *beamSpot);
if (!updateResult.first)
++trackIndex;
else {
// set the persistent track-extra reference to the Track
updateResult.second.setExtra(trackExtraRef);
// Fill the map
trackToTrackmap->insert(reco::TrackRef(trackCollectionRefProd, trackIndex++),
reco::TrackRef(trackUpdatedCollectionRefProd, trackUpdatedIndex++));
}
}
// get the transient rechit and co from the trajectory
reco::TrackExtra::TrajParams trajParams;
reco::TrackExtra::Chi2sFive chi2s;
Traj2TrackHits t2t;
auto ih = recHitCollection->size();
t2t(trajectory, *recHitCollection, trajParams, chi2s);
auto ie = recHitCollection->size();
// set the TrackingRecHitRef (persitent reference of the tracking rec hits)
trackExtra.setHits(recHitCollectionRefProd, ih, ie - ih);
trackExtra.setTrajParams(std::move(trajParams), std::move(chi2s));
assert(trackExtra.trajParams().size() == trackExtra.recHitsSize());
// Fill the hit pattern
for (; ih < ie; ++ih) {
auto const& hit = (*recHitCollection)[ih];
auto hits = MuonTrackLoader::unpackHit(hit);
for (auto hh : hits) {
if UNLIKELY (!track.appendHitPattern(*hh, ttopo))
break;
}
if (theUpdatingAtVtx && updateResult.first) {
for (auto hh : hits) {
if UNLIKELY (!updateResult.second.appendHitPattern(*hh, ttopo))
break;
}
}
}
// fill the TrackExtraCollection
trackExtraCollection->push_back(trackExtra);
// fill the TrackCollection
trackCollection->push_back(track);
iTkRef++;
LogTrace(metname) << "Debug Track being loaded pt " << track.pt();
// fill the TrackCollection updated at vtx
if (theUpdatingAtVtx && updateResult.first)
updatedAtVtxTrackCollection->push_back(updateResult.second);
if (tkBoolVec.size() > tjCnt)
tkBoolVec[tjCnt] = true;
if (theTrajectoryFlag)
tjTkMap[iTjRef - 1] = iTkRef - 1;
}
// Put the Collections in the event
LogTrace(metname) << "put the Collections in the event";
event.put(std::move(recHitCollection), instance);
event.put(std::move(trackExtraCollection), instance);
OrphanHandle<reco::TrackCollection> returnTrackHandle;
OrphanHandle<reco::TrackCollection> nonUpdatedHandle;
if (theUpdatingAtVtx) {
nonUpdatedHandle = event.put(std::move(trackCollection), instance);
event.put(std::move(trackToTrackmap));
returnTrackHandle = event.put(std::move(updatedAtVtxTrackCollection), instance + "UpdatedAtVtx");
} else {
returnTrackHandle = event.put(std::move(trackCollection), instance);
nonUpdatedHandle = returnTrackHandle;
}
if (theTrajectoryFlag) {
OrphanHandle<std::vector<Trajectory>> rTrajs = event.put(std::move(trajectoryCollection), instance);
// Association map between track and trajectory
auto trajTrackMap = std::make_unique<TrajTrackAssociationCollection>(rTrajs, nonUpdatedHandle);
// Now Create traj<->tracks association map
for (std::map<unsigned int, unsigned int>::iterator i = tjTkMap.begin(); i != tjTkMap.end(); i++) {
trajTrackMap->insert(edm::Ref<std::vector<Trajectory>>(rTrajs, (*i).first),
edm::Ref<reco::TrackCollection>(nonUpdatedHandle, (*i).second));
}
event.put(std::move(trajTrackMap), instance);
}
return returnTrackHandle;
}
OrphanHandle<reco::MuonTrackLinksCollection> MuonTrackLoader::loadTracks(CandidateContainer& muonCands,
Event& event,
const TrackerTopology& ttopo) {
const string metname = "Muon|RecoMuon|MuonTrackLoader";
// the muon collection, it will be loaded in the event
auto trackLinksCollection = std::make_unique<reco::MuonTrackLinksCollection>();
// don't waste any time...
if (muonCands.empty()) {
auto trackExtraCollection = std::make_unique<reco::TrackExtraCollection>();
auto recHitCollection = std::make_unique<TrackingRecHitCollection>();
auto trackCollection = std::make_unique<reco::TrackCollection>();
event.put(std::move(recHitCollection));
event.put(std::move(trackExtraCollection));
event.put(std::move(trackCollection));
//need to also put the tracker tracks collection if requested
if (thePutTkTrackFlag) {
//will take care of putting nothing in the event but the empty collection
TrajectoryContainer trackerTrajs;
loadTracks(trackerTrajs, event, ttopo, theL2SeededTkLabel, theSmoothTkTrackFlag);
}
return event.put(std::move(trackLinksCollection));
}
// get combined Trajectories
TrajectoryContainer combinedTrajs;
TrajectoryContainer trackerTrajs;
for (CandidateContainer::iterator it = muonCands.begin(); it != muonCands.end(); ++it) {
LogDebug(metname) << "Loader glbSeedRef " << (*it)->trajectory()->seedRef().isNonnull();
if ((*it)->trackerTrajectory())
LogDebug(metname) << " "
<< "tkSeedRef " << (*it)->trackerTrajectory()->seedRef().isNonnull();
combinedTrajs.push_back((*it)->releaseTrajectory());
{
auto tt = (*it)->releaseTrackerTrajectory();
if (thePutTkTrackFlag)
trackerTrajs.push_back(std::move(tt));
}
// // Create the links between sta and tracker tracks
// reco::MuonTrackLinks links;
// links.setStandAloneTrack((*it)->muonTrack());
// links.setTrackerTrack((*it)->trackerTrack());
// trackLinksCollection->push_back(links);
// delete *it;
}
// create the TrackCollection of combined Trajectories
// FIXME: could this be done one track at a time in the previous loop?
LogTrace(metname) << "Build combinedTracks";
std::vector<bool> combTksVec(combinedTrajs.size(), false);
OrphanHandle<reco::TrackCollection> combinedTracks = loadTracks(combinedTrajs, event, combTksVec, ttopo);
OrphanHandle<reco::TrackCollection> trackerTracks;
std::vector<bool> trackerTksVec(trackerTrajs.size(), false);
if (thePutTkTrackFlag) {
LogTrace(metname) << "Build trackerTracks: " << trackerTrajs.size();
trackerTracks = loadTracks(trackerTrajs, event, trackerTksVec, ttopo, theL2SeededTkLabel, theSmoothTkTrackFlag);
}
trackerTrajs.clear();
LogTrace(metname) << "Set the final links in the MuonTrackLinks collection";
unsigned int candposition(0), position(0), tkposition(0);
//reco::TrackCollection::const_iterator glIt = combinedTracks->begin(),
// glEnd = combinedTracks->end();
for (CandidateContainer::const_iterator it = muonCands.begin(); it != muonCands.end(); ++it, ++candposition) {
// The presence of the global track determines whether to fill the MuonTrackLinks or not
// N.B. We are assuming here that the global tracks in "combinedTracks"
// have the same order as the muon candidates in "muonCands"
// (except for possible missing tracks), which should always be the case...
//if( glIt == glEnd ) break;
if (combTksVec[candposition]) {
reco::TrackRef combinedTR(combinedTracks, position++);
//++glIt;
// Create the links between sta and tracker tracks
reco::MuonTrackLinks links;
links.setStandAloneTrack((*it)->muonTrack());
links.setTrackerTrack((*it)->trackerTrack());
links.setGlobalTrack(combinedTR);
if (thePutTkTrackFlag && trackerTksVec[candposition]) {
reco::TrackRef trackerTR(trackerTracks, tkposition++);
links.setTrackerTrack(trackerTR);
}
trackLinksCollection->push_back(links);
}
else { // if no global track, still increment the tracker-track counter when appropriate
if (thePutTkTrackFlag && trackerTksVec[candposition])
tkposition++;
}
}
if (thePutTkTrackFlag && trackerTracks.isValid() && !(!combinedTracks->empty() && !trackerTracks->empty()))
LogWarning(metname) << "The MuonTrackLinkCollection is incomplete";
// put the MuonCollection in the event
LogTrace(metname) << "put the MuonCollection in the event"
<< "\n";
return event.put(std::move(trackLinksCollection));
}
OrphanHandle<reco::TrackCollection> MuonTrackLoader::loadTracks(
TrajectoryContainer& trajectories,
Event& event,
const std::vector<std::pair<Trajectory*, reco::TrackRef>>& miniMap,
Handle<reco::TrackCollection> const& trackHandle,
const TrackerTopology& ttopo,
const string& instance,
bool reallyDoSmoothing) {
const bool doSmoothing = theSmoothingStep && reallyDoSmoothing;
const string metname = "Muon|RecoMuon|MuonTrackLoader|TevMuonTrackLoader";
LogDebug(metname) << "TeV LoadTracks instance: " << instance;
// the track collectios; they will be loaded in the event
auto trackCollection = std::make_unique<reco::TrackCollection>();
// ... and its reference into the event
reco::TrackRefProd trackCollectionRefProd = event.getRefBeforePut<reco::TrackCollection>(instance);
// Association map between GlobalMuons and TeVMuons
auto trackToTrackmap = std::make_unique<reco::TrackToTrackMap>(trackHandle, trackCollectionRefProd);
// the track extra collection, it will be loaded in the event
auto trackExtraCollection = std::make_unique<reco::TrackExtraCollection>();
// ... and its reference into the event
reco::TrackExtraRefProd trackExtraCollectionRefProd = event.getRefBeforePut<reco::TrackExtraCollection>(instance);
// the rechit collection, it will be loaded in the event
auto recHitCollection = std::make_unique<TrackingRecHitCollection>();
// ... and its reference into the event
TrackingRecHitRefProd recHitCollectionRefProd = event.getRefBeforePut<TrackingRecHitCollection>(instance);
// Collection of Trajectory
auto trajectoryCollection = std::make_unique<std::vector<Trajectory>>();
// don't waste any time...
if (trajectories.empty()) {
event.put(std::move(recHitCollection), instance);
event.put(std::move(trackExtraCollection), instance);
if (theTrajectoryFlag) {
event.put(std::move(trajectoryCollection), instance);
// Association map between track and trajectory
auto trajTrackMap = std::make_unique<TrajTrackAssociationCollection>();
event.put(std::move(trajTrackMap), instance);
}
event.put(std::move(trackToTrackmap), instance);
return event.put(std::move(trackCollection), instance);
}
LogTrace(metname) << "Create the collection of Tracks";
edm::Handle<reco::BeamSpot> beamSpot;
event.getByToken(theBeamSpotToken, beamSpot);
reco::TrackRef::key_type trackIndex = 0;
reco::TrackExtraRef::key_type trackExtraIndex = 0;
edm::Ref<reco::TrackCollection>::key_type iTkRef = 0;
edm::Ref<std::vector<Trajectory>>::key_type iTjRef = 0;
std::map<unsigned int, unsigned int> tjTkMap;
if (doSmoothing) {
TrajectorySmoother const& aSmoother = theService->eventSetup().getData(theSmootherToken);
theSmoother.reset(aSmoother.clone());
TransientTrackingRecHitBuilder const& theTrackerRecHitBuilder =
theService->eventSetup().getData(theTrackerRecHitBuilderToken);
hitCloner = static_cast<TkTransientTrackingRecHitBuilder const&>(theTrackerRecHitBuilder).cloner();
theSmoother->setHitCloner(&hitCloner);
}
for (TrajectoryContainer::iterator itRawTrajectory = trajectories.begin(); itRawTrajectory != trajectories.end();
++itRawTrajectory) {
auto rawTrajectory = std::move(*itRawTrajectory);
reco::TrackRef glbRef;
std::vector<std::pair<Trajectory*, reco::TrackRef>>::const_iterator mmit;
for (mmit = miniMap.begin(); mmit != miniMap.end(); ++mmit) {
if (mmit->first == rawTrajectory.get())
glbRef = mmit->second;
}
Trajectory& trajectory = *rawTrajectory;
if (doSmoothing) {
vector<Trajectory> trajectoriesSM = theSmoother->trajectories(*rawTrajectory);
if (!trajectoriesSM.empty()) {
const edm::RefToBase<TrajectorySeed> tmpSeedRef = (*rawTrajectory).seedRef();
trajectory = trajectoriesSM.front();
trajectory.setSeedRef(tmpSeedRef);
} else
LogInfo(metname) << "The trajectory has not been smoothed!" << endl;
}
if (theTrajectoryFlag) {
trajectoryCollection->push_back(trajectory);
iTjRef++;
}
// build the "bare" track from the trajectory.
// This track has the parameters defined at PCA (no update)
pair<bool, reco::Track> resultOfTrackExtrapAtPCA = buildTrackAtPCA(trajectory, *beamSpot);
// Check if the extrapolation went well
if (!resultOfTrackExtrapAtPCA.first) {
continue;
}
// take the "bare" track at PCA
reco::Track& track = resultOfTrackExtrapAtPCA.second;
// build the "bare" track extra from the trajectory
reco::TrackExtra trackExtra = buildTrackExtra(trajectory);
// get the TrackExtraRef (persitent reference of the track extra)
reco::TrackExtraRef trackExtraRef(trackExtraCollectionRefProd, trackExtraIndex++);
// set the persistent track-extra reference to the Track
track.setExtra(trackExtraRef);
// Fill the map
trackToTrackmap->insert(glbRef, reco::TrackRef(trackCollectionRefProd, trackIndex++));
// build the updated-at-vertex track, starting from the previous track
pair<bool, reco::Track> updateResult(false, reco::Track());
// get the transient rechit and co from the trajectory
reco::TrackExtra::TrajParams trajParams;
reco::TrackExtra::Chi2sFive chi2s;
Traj2TrackHits t2t;
auto ih = recHitCollection->size();
t2t(trajectory, *recHitCollection, trajParams, chi2s);
auto ie = recHitCollection->size();
// set the TrackingRecHitRef (persitent reference of the tracking rec hits)
trackExtra.setHits(recHitCollectionRefProd, ih, ie - ih);
trackExtra.setTrajParams(std::move(trajParams), std::move(chi2s));
assert(trackExtra.trajParams().size() == trackExtra.recHitsSize());
// Fill the hit pattern
for (; ih < ie; ++ih) {
auto const& hit = (*recHitCollection)[ih];
auto hits = MuonTrackLoader::unpackHit(hit);
for (auto hh : hits) {
if UNLIKELY (!track.appendHitPattern(*hh, ttopo))
break;
}
if (theUpdatingAtVtx && updateResult.first) {
for (auto hh : hits) {
if UNLIKELY (!updateResult.second.appendHitPattern(*hh, ttopo))
break;
}
}
}
// fill the TrackExtraCollection
trackExtraCollection->push_back(trackExtra);
// fill the TrackCollection
trackCollection->push_back(track);
iTkRef++;
LogTrace(metname) << "Debug Track being loaded pt " << track.pt();
if (theTrajectoryFlag)
tjTkMap[iTjRef - 1] = iTkRef - 1;
}
// Put the Collections in the event
LogTrace(metname) << "put the Collections in the event";
event.put(std::move(recHitCollection), instance);
event.put(std::move(trackExtraCollection), instance);
OrphanHandle<reco::TrackCollection> returnTrackHandle;
OrphanHandle<reco::TrackCollection> nonUpdatedHandle;
if (theUpdatingAtVtx) {
} else {
event.put(std::move(trackToTrackmap), instance);
returnTrackHandle = event.put(std::move(trackCollection), instance);
nonUpdatedHandle = returnTrackHandle;
}
if (theTrajectoryFlag) {
OrphanHandle<std::vector<Trajectory>> rTrajs = event.put(std::move(trajectoryCollection), instance);
// Association map between track and trajectory
auto trajTrackMap = std::make_unique<TrajTrackAssociationCollection>(rTrajs, nonUpdatedHandle);
// Now Create traj<->tracks association map
for (std::map<unsigned int, unsigned int>::iterator i = tjTkMap.begin(); i != tjTkMap.end(); i++) {
trajTrackMap->insert(edm::Ref<std::vector<Trajectory>>(rTrajs, (*i).first),
edm::Ref<reco::TrackCollection>(nonUpdatedHandle, (*i).second));
}
event.put(std::move(trajTrackMap), instance);
}
return returnTrackHandle;
}
pair<bool, reco::Track> MuonTrackLoader::buildTrackAtPCA(const Trajectory& trajectory,
const reco::BeamSpot& beamSpot) const {
const string metname = "Muon|RecoMuon|MuonTrackLoader";
MuonPatternRecoDumper debug;
// FIXME: check the prop direction
TrajectoryStateOnSurface innerTSOS = trajectory.geometricalInnermostState();
// This is needed to extrapolate the tsos at vertex
LogTrace(metname) << "Propagate to PCA...";
pair<bool, FreeTrajectoryState> extrapolationResult = theUpdatorAtVtx->propagate(innerTSOS, beamSpot);
FreeTrajectoryState ftsAtVtx;
if (extrapolationResult.first)
ftsAtVtx = extrapolationResult.second;
else {
if (TrackerBounds::isInside(innerTSOS.globalPosition())) {
LogInfo(metname) << "Track in the Tracker: taking the innermost state instead of the state at PCA";
ftsAtVtx = *innerTSOS.freeState();
} else {
if (theAllowNoVtxFlag) {
LogInfo(metname) << "Propagation to PCA failed, taking the innermost state instead of the state at PCA";
ftsAtVtx = *innerTSOS.freeState();
} else {
LogInfo(metname) << "Stand Alone track: this track will be rejected";
return pair<bool, reco::Track>(false, reco::Track());
}
}
}
LogTrace(metname) << "TSOS after the extrapolation at vtx";
LogTrace(metname) << debug.dumpFTS(ftsAtVtx);
GlobalPoint pca = ftsAtVtx.position();
math::XYZPoint persistentPCA(pca.x(), pca.y(), pca.z());
GlobalVector p = ftsAtVtx.momentum();
math::XYZVector persistentMomentum(p.x(), p.y(), p.z());
bool bon = true;
if (fabs(theService->magneticField()->inTesla(GlobalPoint(0, 0, 0)).z()) < 0.01)
bon = false;
double ndof = trajectory.ndof(bon);
reco::Track track(
trajectory.chiSquared(), ndof, persistentPCA, persistentMomentum, ftsAtVtx.charge(), ftsAtVtx.curvilinearError());
return pair<bool, reco::Track>(true, track);
}
pair<bool, reco::Track> MuonTrackLoader::buildTrackUpdatedAtPCA(const reco::Track& track,
const reco::BeamSpot& beamSpot) const {
const string metname = "Muon|RecoMuon|MuonTrackLoader";
MuonPatternRecoDumper debug;
// build the transient track
reco::TransientTrack transientTrack(track, &*theService->magneticField(), theService->trackingGeometry());
LogTrace(metname) << "Apply the vertex constraint";
pair<bool, FreeTrajectoryState> updateResult = theUpdatorAtVtx->update(transientTrack, beamSpot);
if (!updateResult.first) {
return pair<bool, reco::Track>(false, reco::Track());
}
LogTrace(metname) << "FTS after the vertex constraint";
FreeTrajectoryState& ftsAtVtx = updateResult.second;
LogTrace(metname) << debug.dumpFTS(ftsAtVtx);
GlobalPoint pca = ftsAtVtx.position();
math::XYZPoint persistentPCA(pca.x(), pca.y(), pca.z());
GlobalVector p = ftsAtVtx.momentum();
math::XYZVector persistentMomentum(p.x(), p.y(), p.z());
reco::Track updatedTrack(
track.chi2(), track.ndof(), persistentPCA, persistentMomentum, ftsAtVtx.charge(), ftsAtVtx.curvilinearError());
return pair<bool, reco::Track>(true, updatedTrack);
}
reco::TrackExtra MuonTrackLoader::buildTrackExtra(const Trajectory& trajectory) const {
const string metname = "Muon|RecoMuon|MuonTrackLoader";
const Trajectory::RecHitContainer transRecHits = trajectory.recHits();
// put the collection of TrackingRecHit in the event
// sets the outermost and innermost TSOSs
// FIXME: check it!
TrajectoryStateOnSurface outerTSOS;
TrajectoryStateOnSurface innerTSOS;
unsigned int innerId = 0, outerId = 0;
TrajectoryMeasurement::ConstRecHitPointer outerRecHit;
DetId outerDetId;
if (trajectory.direction() == alongMomentum) {
LogTrace(metname) << "alongMomentum";
outerTSOS = trajectory.lastMeasurement().updatedState();
innerTSOS = trajectory.firstMeasurement().updatedState();
outerId = trajectory.lastMeasurement().recHit()->geographicalId().rawId();
innerId = trajectory.firstMeasurement().recHit()->geographicalId().rawId();
outerRecHit = trajectory.lastMeasurement().recHit();
outerDetId = trajectory.lastMeasurement().recHit()->geographicalId();
} else if (trajectory.direction() == oppositeToMomentum) {
LogTrace(metname) << "oppositeToMomentum";
outerTSOS = trajectory.firstMeasurement().updatedState();
innerTSOS = trajectory.lastMeasurement().updatedState();
outerId = trajectory.firstMeasurement().recHit()->geographicalId().rawId();
innerId = trajectory.lastMeasurement().recHit()->geographicalId().rawId();
outerRecHit = trajectory.firstMeasurement().recHit();
outerDetId = trajectory.firstMeasurement().recHit()->geographicalId();
} else
LogError(metname) << "Wrong propagation direction!";
const GeomDet* outerDet = theService->trackingGeometry()->idToDet(outerDetId);
GlobalPoint outerTSOSPos = outerTSOS.globalParameters().position();
bool inside = outerDet->surface().bounds().inside(outerDet->toLocal(outerTSOSPos));
GlobalPoint hitPos =
(outerRecHit->isValid()) ? outerRecHit->globalPosition() : outerTSOS.globalParameters().position();
if (!inside) {
LogTrace(metname) << "The Global Muon outerMostMeasurementState is not compatible with the recHit detector! "
"Setting outerMost postition to recHit position if recHit isValid: "
<< outerRecHit->isValid();
LogTrace(metname) << "From " << outerTSOSPos << " to " << hitPos;
}
//build the TrackExtra
GlobalPoint v = (inside) ? outerTSOSPos : hitPos;
GlobalVector p = outerTSOS.globalParameters().momentum();
math::XYZPoint outpos(v.x(), v.y(), v.z());
math::XYZVector outmom(p.x(), p.y(), p.z());
v = innerTSOS.globalParameters().position();
p = innerTSOS.globalParameters().momentum();
math::XYZPoint inpos(v.x(), v.y(), v.z());
math::XYZVector inmom(p.x(), p.y(), p.z());
reco::TrackExtra trackExtra(outpos,
outmom,
true,
inpos,
inmom,
true,
outerTSOS.curvilinearError(),
outerId,
innerTSOS.curvilinearError(),
innerId,
trajectory.direction(),
trajectory.seedRef());
return trackExtra;
}