-
Notifications
You must be signed in to change notification settings - Fork 4.2k
/
TrackingNtuple.cc
4680 lines (4310 loc) · 197 KB
/
TrackingNtuple.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
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// -*- C++ -*-
//
// Package: NtupleDump/TrackingNtuple
// Class: TrackingNtuple
//
/**\class TrackingNtuple TrackingNtuple.cc NtupleDump/TrackingNtuple/plugins/TrackingNtuple.cc
Description: [one line class summary]
Implementation:
[Notes on implementation]
*/
//
// Original Author: Giuseppe Cerati
// Created: Tue, 25 Aug 2015 13:22:49 GMT
//
//
// system include files
#include <memory>
// user include files
#include "FWCore/Framework/interface/Frameworkfwd.h"
#include "FWCore/Framework/interface/one/EDAnalyzer.h"
#include "FWCore/Framework/interface/Event.h"
#include "FWCore/Framework/interface/MakerMacros.h"
#include "FWCore/Framework/interface/ESHandle.h"
#include "FWCore/MessageLogger/interface/MessageLogger.h"
#include "FWCore/Utilities/interface/isFinite.h"
#include "FWCore/ServiceRegistry/interface/Service.h"
#include "CommonTools/UtilAlgos/interface/TFileService.h"
#include "CommonTools/Utils/interface/DynArray.h"
#include "DataFormats/Provenance/interface/ProductID.h"
#include "DataFormats/Common/interface/ContainerMask.h"
#include "FWCore/ParameterSet/interface/ParameterSet.h"
#include "FWCore/Utilities/interface/transform.h"
#include "DataFormats/TrackReco/interface/Track.h"
#include "DataFormats/TrackReco/interface/TrackFwd.h"
#include "DataFormats/TrackReco/interface/SeedStopInfo.h"
#include "TrackingTools/Records/interface/TransientRecHitRecord.h"
#include "TrackingTools/TransientTrackingRecHit/interface/TransientTrackingRecHitBuilder.h"
#include "RecoTracker/TransientTrackingRecHit/interface/TkTransientTrackingRecHitBuilder.h"
#include "DataFormats/TrajectorySeed/interface/TrajectorySeed.h"
#include "DataFormats/TrajectorySeed/interface/TrajectorySeedCollection.h"
#include "TrackingTools/TrajectoryState/interface/TrajectoryStateTransform.h"
#include "RecoTracker/PixelTrackFitting/interface/RZLine.h"
#include "TrackingTools/PatternTools/interface/TSCBLBuilderNoMaterial.h"
#include "TrackingTools/TrajectoryState/interface/PerigeeConversions.h"
#include "MagneticField/Engine/interface/MagneticField.h"
#include "MagneticField/Records/interface/IdealMagneticFieldRecord.h"
#include "DataFormats/Phase2TrackerCluster/interface/Phase2TrackerCluster1D.h"
#include "DataFormats/SiPixelDetId/interface/PixelChannelIdentifier.h"
#include "DataFormats/SiStripCluster/interface/SiStripClusterTools.h"
#include "DataFormats/TrackerRecHit2D/interface/SiPixelRecHitCollection.h"
#include "DataFormats/TrackerRecHit2D/interface/SiStripRecHit1D.h"
#include "DataFormats/TrackerRecHit2D/interface/SiStripRecHit2DCollection.h"
#include "DataFormats/TrackerRecHit2D/interface/SiStripMatchedRecHit2DCollection.h"
#include "DataFormats/TrackerRecHit2D/interface/Phase2TrackerRecHit1D.h"
#include "DataFormats/TrackerCommon/interface/TrackerTopology.h"
#include "Geometry/Records/interface/TrackerTopologyRcd.h"
#include "Geometry/TrackerGeometryBuilder/interface/TrackerGeometry.h"
#include "Geometry/Records/interface/TrackerDigiGeometryRecord.h"
#include "DataFormats/VertexReco/interface/Vertex.h"
#include "DataFormats/VertexReco/interface/VertexFwd.h"
#include "SimDataFormats/Associations/interface/TrackToTrackingParticleAssociator.h"
#include "SimGeneral/TrackingAnalysis/interface/SimHitTPAssociationProducer.h"
#include "SimTracker/TrackerHitAssociation/interface/ClusterTPAssociation.h"
#include "SimTracker/TrackAssociation/interface/ParametersDefinerForTP.h"
#include "SimTracker/TrackAssociation/interface/TrackingParticleIP.h"
#include "SimTracker/TrackAssociation/interface/trackAssociationChi2.h"
#include "SimTracker/TrackAssociation/interface/trackHitsToClusterRefs.h"
#include "SimDataFormats/TrackerDigiSimLink/interface/PixelDigiSimLink.h"
#include "SimDataFormats/TrackerDigiSimLink/interface/StripDigiSimLink.h"
#include "SimDataFormats/TrackingAnalysis/interface/TrackingVertex.h"
#include "SimDataFormats/TrackingAnalysis/interface/TrackingVertexContainer.h"
#include "SimTracker/TrackHistory/interface/HistoryBase.h"
#include "HepPDT/ParticleID.hh"
#include "Validation/RecoTrack/interface/trackFromSeedFitFailed.h"
#include "RecoTracker/FinalTrackSelectors/interface/getBestVertex.h"
#include <set>
#include <map>
#include <unordered_set>
#include <unordered_map>
#include <tuple>
#include <utility>
#include "TTree.h"
/*
todo:
add refitted hit position after track/seed fit
add local angle, path length!
*/
namespace {
// This pattern is copied from QuickTrackAssociatorByHitsImpl. If
// further needs arises, it wouldn't hurt to abstract it somehow.
using TrackingParticleRefKeyToIndex = std::unordered_map<reco::RecoToSimCollection::index_type, size_t>;
using TrackingVertexRefKeyToIndex = TrackingParticleRefKeyToIndex;
using SimHitFullKey = std::pair<TrackPSimHitRef::key_type, edm::ProductID>;
using SimHitRefKeyToIndex = std::map<SimHitFullKey, size_t>;
using TrackingParticleRefKeyToCount = TrackingParticleRefKeyToIndex;
std::string subdetstring(int subdet) {
switch (subdet) {
case StripSubdetector::TIB:
return "- TIB";
case StripSubdetector::TOB:
return "- TOB";
case StripSubdetector::TEC:
return "- TEC";
case StripSubdetector::TID:
return "- TID";
case PixelSubdetector::PixelBarrel:
return "- PixBar";
case PixelSubdetector::PixelEndcap:
return "- PixFwd";
default:
return "UNKNOWN TRACKER HIT TYPE";
}
}
struct ProductIDSetPrinter {
ProductIDSetPrinter(const std::set<edm::ProductID>& set) : set_(set) {}
void print(std::ostream& os) const {
for (const auto& item : set_) {
os << item << " ";
}
}
const std::set<edm::ProductID>& set_;
};
std::ostream& operator<<(std::ostream& os, const ProductIDSetPrinter& o) {
o.print(os);
return os;
}
template <typename T>
struct ProductIDMapPrinter {
ProductIDMapPrinter(const std::map<edm::ProductID, T>& map) : map_(map) {}
void print(std::ostream& os) const {
for (const auto& item : map_) {
os << item.first << " ";
}
}
const std::map<edm::ProductID, T>& map_;
};
template <typename T>
auto make_ProductIDMapPrinter(const std::map<edm::ProductID, T>& map) {
return ProductIDMapPrinter<T>(map);
}
template <typename T>
std::ostream& operator<<(std::ostream& os, const ProductIDMapPrinter<T>& o) {
o.print(os);
return os;
}
template <typename T>
struct VectorPrinter {
VectorPrinter(const std::vector<T>& vec) : vec_(vec) {}
void print(std::ostream& os) const {
for (const auto& item : vec_) {
os << item << " ";
}
}
const std::vector<T>& vec_;
};
template <typename T>
auto make_VectorPrinter(const std::vector<T>& vec) {
return VectorPrinter<T>(vec);
}
template <typename T>
std::ostream& operator<<(std::ostream& os, const VectorPrinter<T>& o) {
o.print(os);
return os;
}
void checkProductID(const std::set<edm::ProductID>& set, const edm::ProductID& id, const char* name) {
if (set.find(id) == set.end())
throw cms::Exception("Configuration")
<< "Got " << name << " with a hit with ProductID " << id
<< " which does not match to the set of ProductID's for the hits: " << ProductIDSetPrinter(set)
<< ". Usually this is caused by a wrong hit collection in the configuration.";
}
template <typename SimLink, typename Func>
void forEachMatchedSimLink(const edm::DetSet<SimLink>& digiSimLinks, uint32_t channel, Func func) {
for (const auto& link : digiSimLinks) {
if (link.channel() == channel) {
func(link);
}
}
}
/// No-op function used in the trick of CombineDetId::impl2()
template <typename... Args>
void call_nop(Args&&... args) {}
template <typename... Types>
class CombineDetId {
public:
CombineDetId() {}
/// Return the raw DetId, assumes that the first type is
/// DetIdCommon that has operator[]()
unsigned int operator[](size_t i) const { return std::get<0>(content_)[i]; }
template <typename... Args>
void book(Args&&... args) {
impl([&](auto& vec) { vec.book(std::forward<Args>(args)...); });
}
template <typename... Args>
void push_back(Args&&... args) {
impl([&](auto& vec) { vec.push_back(std::forward<Args>(args)...); });
}
template <typename... Args>
void resize(Args&&... args) {
impl([&](auto& vec) { vec.resize(std::forward<Args>(args)...); });
}
template <typename... Args>
void set(Args&&... args) {
impl([&](auto& vec) { vec.set(std::forward<Args>(args)...); });
}
void clear() {
impl([&](auto& vec) { vec.clear(); });
}
private:
// Trick to not repeate std::index_sequence_for in each of the methods above
template <typename F>
void impl(F&& func) {
impl2(std::index_sequence_for<Types...>{}, std::forward<F>(func));
}
// Trick to exploit parameter pack expansion in function call
// arguments to call a member function for each tuple element
// (with the same signature). The comma operator is needed to
// return a value from the expression as an argument for the
// call_nop.
template <std::size_t... Is, typename F>
void impl2(std::index_sequence<Is...>, F&& func) {
call_nop((func(std::get<Is>(content_)), 0)...);
}
std::tuple<Types...> content_;
};
std::map<unsigned int, double> chargeFraction(const SiPixelCluster& cluster,
const DetId& detId,
const edm::DetSetVector<PixelDigiSimLink>& digiSimLink) {
std::map<unsigned int, double> simTrackIdToAdc;
auto idetset = digiSimLink.find(detId);
if (idetset == digiSimLink.end())
return simTrackIdToAdc;
double adcSum = 0;
PixelDigiSimLink found;
for (int iPix = 0; iPix != cluster.size(); ++iPix) {
const SiPixelCluster::Pixel& pixel = cluster.pixel(iPix);
adcSum += pixel.adc;
uint32_t channel = PixelChannelIdentifier::pixelToChannel(pixel.x, pixel.y);
forEachMatchedSimLink(*idetset, channel, [&](const PixelDigiSimLink& simLink) {
double& adc = simTrackIdToAdc[simLink.SimTrackId()];
adc += pixel.adc * simLink.fraction();
});
}
for (auto& pair : simTrackIdToAdc) {
if (adcSum == 0.)
pair.second = 0.;
else
pair.second /= adcSum;
}
return simTrackIdToAdc;
}
std::map<unsigned int, double> chargeFraction(const SiStripCluster& cluster,
const DetId& detId,
const edm::DetSetVector<StripDigiSimLink>& digiSimLink) {
std::map<unsigned int, double> simTrackIdToAdc;
auto idetset = digiSimLink.find(detId);
if (idetset == digiSimLink.end())
return simTrackIdToAdc;
double adcSum = 0;
StripDigiSimLink found;
int first = cluster.firstStrip();
for (size_t i = 0; i < cluster.amplitudes().size(); ++i) {
adcSum += cluster.amplitudes()[i];
forEachMatchedSimLink(*idetset, first + i, [&](const StripDigiSimLink& simLink) {
double& adc = simTrackIdToAdc[simLink.SimTrackId()];
adc += cluster.amplitudes()[i] * simLink.fraction();
});
for (const auto& pair : simTrackIdToAdc) {
simTrackIdToAdc[pair.first] = (adcSum != 0. ? pair.second / adcSum : 0.);
}
}
return simTrackIdToAdc;
}
std::map<unsigned int, double> chargeFraction(const Phase2TrackerCluster1D& cluster,
const DetId& detId,
const edm::DetSetVector<StripDigiSimLink>& digiSimLink) {
std::map<unsigned int, double> simTrackIdToAdc;
throw cms::Exception("LogicError") << "Not possible to use StripDigiSimLink with Phase2TrackerCluster1D! ";
return simTrackIdToAdc;
}
//In the OT, there is no measurement of the charge, so no ADC value.
//Only in the SSA chip (so in PSs) you have one "threshold" flag that tells you if the charge of at least one strip in the cluster exceeded 1.2 MIPs.
std::map<unsigned int, double> chargeFraction(const Phase2TrackerCluster1D& cluster,
const DetId& detId,
const edm::DetSetVector<PixelDigiSimLink>& digiSimLink) {
std::map<unsigned int, double> simTrackIdToAdc;
return simTrackIdToAdc;
}
struct TrackTPMatch {
int key = -1;
int countClusters = 0;
};
template <typename HRange>
TrackTPMatch findBestMatchingTrackingParticle(const HRange& hits,
const ClusterTPAssociation& clusterToTPMap,
const TrackingParticleRefKeyToIndex& tpKeyToIndex) {
struct Count {
int clusters = 0;
size_t innermostHit = std::numeric_limits<size_t>::max();
};
std::vector<OmniClusterRef> clusters = track_associator::hitsToClusterRefs(hits.begin(), hits.end());
std::unordered_map<int, Count> count;
for (size_t iCluster = 0, end = clusters.size(); iCluster < end; ++iCluster) {
const auto& clusterRef = clusters[iCluster];
auto range = clusterToTPMap.equal_range(clusterRef);
for (auto ip = range.first; ip != range.second; ++ip) {
const auto tpKey = ip->second.key();
if (tpKeyToIndex.find(tpKey) == tpKeyToIndex.end()) // filter out TPs not given as an input
continue;
auto& elem = count[tpKey];
++elem.clusters;
elem.innermostHit = std::min(elem.innermostHit, iCluster);
}
}
// In case there are many matches with the same number of clusters,
// select the one with innermost hit
TrackTPMatch best;
int bestCount = 2; // require >= 3 cluster for the best match
size_t bestInnermostHit = std::numeric_limits<size_t>::max();
for (auto& keyCount : count) {
if (keyCount.second.clusters > bestCount ||
(keyCount.second.clusters == bestCount && keyCount.second.innermostHit < bestInnermostHit)) {
best.key = keyCount.first;
best.countClusters = bestCount = keyCount.second.clusters;
bestInnermostHit = keyCount.second.innermostHit;
}
}
LogTrace("TrackingNtuple") << "findBestMatchingTrackingParticle key " << best.key;
return best;
}
template <typename HRange>
TrackTPMatch findMatchingTrackingParticleFromFirstHit(const HRange& hits,
const ClusterTPAssociation& clusterToTPMap,
const TrackingParticleRefKeyToIndex& tpKeyToIndex) {
TrackTPMatch best;
std::vector<OmniClusterRef> clusters = track_associator::hitsToClusterRefs(hits.begin(), hits.end());
if (clusters.empty()) {
return best;
}
auto operateCluster = [&](const auto& clusterRef, const auto& func) {
auto range = clusterToTPMap.equal_range(clusterRef);
for (auto ip = range.first; ip != range.second; ++ip) {
const auto tpKey = ip->second.key();
if (tpKeyToIndex.find(tpKey) == tpKeyToIndex.end()) // filter out TPs not given as an input
continue;
func(tpKey);
}
};
std::vector<unsigned int>
validTPs; // first cluster can be associated to multiple TPs, use vector as set as this should be small
auto iCluster = clusters.begin();
operateCluster(*iCluster, [&](unsigned int tpKey) { validTPs.push_back(tpKey); });
if (validTPs.empty()) {
return best;
}
++iCluster;
++best.countClusters;
std::vector<bool> foundTPs(validTPs.size(), false);
for (auto iEnd = clusters.end(); iCluster != iEnd; ++iCluster) {
const auto& clusterRef = *iCluster;
// find out to which first-cluster TPs this cluster is matched to
operateCluster(clusterRef, [&](unsigned int tpKey) {
auto found = std::find(cbegin(validTPs), cend(validTPs), tpKey);
if (found != cend(validTPs)) {
foundTPs[std::distance(cbegin(validTPs), found)] = true;
}
});
// remove the non-found TPs
auto iTP = validTPs.size();
do {
--iTP;
if (!foundTPs[iTP]) {
validTPs.erase(validTPs.begin() + iTP);
foundTPs.erase(foundTPs.begin() + iTP);
}
} while (iTP > 0);
if (!validTPs.empty()) {
// for multiple TPs the "first one" is a bit arbitrary, but
// I hope it is rare that a track would have many
// consecutive hits matched to two TPs
best.key = validTPs[0];
} else {
break;
}
std::fill(begin(foundTPs), end(foundTPs), false);
++best.countClusters;
}
// Reqquire >= 3 clusters for a match
return best.countClusters >= 3 ? best : TrackTPMatch();
}
} // namespace
//
// class declaration
//
class TrackingNtuple : public edm::one::EDAnalyzer<edm::one::SharedResources> {
public:
explicit TrackingNtuple(const edm::ParameterSet&);
~TrackingNtuple() override;
static void fillDescriptions(edm::ConfigurationDescriptions& descriptions);
private:
void analyze(const edm::Event&, const edm::EventSetup&) override;
void clearVariables();
enum class HitType { Pixel = 0, Strip = 1, Glued = 2, Invalid = 3, Phase2OT = 4, Unknown = 99 };
// This gives the "best" classification of a reco hit
// In case of reco hit mathing to multiple sim, smaller number is
// considered better
// To be kept in synch with class HitSimType in ntuple.py
enum class HitSimType { Signal = 0, ITPileup = 1, OOTPileup = 2, Noise = 3, Unknown = 99 };
using MVACollection = std::vector<float>;
using QualityMaskCollection = std::vector<unsigned char>;
using PixelMaskContainer = edm::ContainerMask<edmNew::DetSetVector<SiPixelCluster>>;
using StripMaskContainer = edm::ContainerMask<edmNew::DetSetVector<SiStripCluster>>;
using Phase2OTMaskContainer = edm::ContainerMask<edmNew::DetSetVector<Phase2TrackerCluster1D>>;
struct TPHitIndex {
TPHitIndex(unsigned int tp = 0, unsigned int simHit = 0, float to = 0, unsigned int id = 0)
: tpKey(tp), simHitIdx(simHit), tof(to), detId(id) {}
unsigned int tpKey;
unsigned int simHitIdx;
float tof;
unsigned int detId;
};
static bool tpHitIndexListLess(const TPHitIndex& i, const TPHitIndex& j) { return (i.tpKey < j.tpKey); }
static bool tpHitIndexListLessSort(const TPHitIndex& i, const TPHitIndex& j) {
if (i.tpKey == j.tpKey) {
if (edm::isNotFinite(i.tof) && edm::isNotFinite(j.tof)) {
return i.detId < j.detId;
}
return i.tof < j.tof; // works as intended if either one is NaN
}
return i.tpKey < j.tpKey;
}
void fillBeamSpot(const reco::BeamSpot& bs);
void fillPixelHits(const edm::Event& iEvent,
const TrackerGeometry& tracker,
const ClusterTPAssociation& clusterToTPMap,
const TrackingParticleRefKeyToIndex& tpKeyToIndex,
const SimHitTPAssociationProducer::SimHitTPAssociationList& simHitsTPAssoc,
const edm::DetSetVector<PixelDigiSimLink>& digiSimLink,
const TrackerTopology& tTopo,
const SimHitRefKeyToIndex& simHitRefKeyToIndex,
std::set<edm::ProductID>& hitProductIds);
void fillStripRphiStereoHits(const edm::Event& iEvent,
const TrackerGeometry& tracker,
const ClusterTPAssociation& clusterToTPMap,
const TrackingParticleRefKeyToIndex& tpKeyToIndex,
const SimHitTPAssociationProducer::SimHitTPAssociationList& simHitsTPAssoc,
const edm::DetSetVector<StripDigiSimLink>& digiSimLink,
const TrackerTopology& tTopo,
const SimHitRefKeyToIndex& simHitRefKeyToIndex,
std::set<edm::ProductID>& hitProductIds);
void fillStripMatchedHits(const edm::Event& iEvent,
const TrackerGeometry& tracker,
const TrackerTopology& tTopo,
std::vector<std::pair<int, int>>& monoStereoClusterList);
size_t addStripMatchedHit(const SiStripMatchedRecHit2D& hit,
const TrackerGeometry& tracker,
const TrackerTopology& tTopo,
const std::vector<std::pair<uint64_t, StripMaskContainer const*>>& stripMasks,
std::vector<std::pair<int, int>>& monoStereoClusterList);
void fillPhase2OTHits(const edm::Event& iEvent,
const ClusterTPAssociation& clusterToTPMap,
const TrackerGeometry& tracker,
const TrackingParticleRefKeyToIndex& tpKeyToIndex,
const SimHitTPAssociationProducer::SimHitTPAssociationList& simHitsTPAssoc,
const edm::DetSetVector<PixelDigiSimLink>& digiSimLink,
const TrackerTopology& tTopo,
const SimHitRefKeyToIndex& simHitRefKeyToIndex,
std::set<edm::ProductID>& hitProductIds);
void fillSeeds(const edm::Event& iEvent,
const TrackingParticleRefVector& tpCollection,
const TrackingParticleRefKeyToIndex& tpKeyToIndex,
const reco::BeamSpot& bs,
const TrackerGeometry& tracker,
const reco::TrackToTrackingParticleAssociator& associatorByHits,
const ClusterTPAssociation& clusterToTPMap,
const MagneticField& theMF,
const TrackerTopology& tTopo,
std::vector<std::pair<int, int>>& monoStereoClusterList,
const std::set<edm::ProductID>& hitProductIds,
std::map<edm::ProductID, size_t>& seedToCollIndex);
void fillTracks(const edm::RefToBaseVector<reco::Track>& tracks,
const TrackerGeometry& tracker,
const TrackingParticleRefVector& tpCollection,
const TrackingParticleRefKeyToIndex& tpKeyToIndex,
const TrackingParticleRefKeyToCount& tpKeyToClusterCount,
const MagneticField& mf,
const reco::BeamSpot& bs,
const reco::VertexCollection& vertices,
const reco::TrackToTrackingParticleAssociator& associatorByHits,
const ClusterTPAssociation& clusterToTPMap,
const TrackerTopology& tTopo,
const std::set<edm::ProductID>& hitProductIds,
const std::map<edm::ProductID, size_t>& seedToCollIndex,
const std::vector<const MVACollection*>& mvaColls,
const std::vector<const QualityMaskCollection*>& qualColls);
void fillCandidates(const edm::Handle<TrackCandidateCollection>& candsHandle,
int algo,
const TrackingParticleRefVector& tpCollection,
const TrackingParticleRefKeyToIndex& tpKeyToIndex,
const TrackingParticleRefKeyToCount& tpKeyToClusterCount,
const MagneticField& mf,
const reco::BeamSpot& bs,
const reco::VertexCollection& vertices,
const reco::TrackToTrackingParticleAssociator& associatorByHits,
const ClusterTPAssociation& clusterToTPMap,
const TrackerGeometry& tracker,
const TrackerTopology& tTopo,
const std::set<edm::ProductID>& hitProductIds,
const std::map<edm::ProductID, size_t>& seedToCollIndex);
void fillSimHits(const TrackerGeometry& tracker,
const TrackingParticleRefKeyToIndex& tpKeyToIndex,
const SimHitTPAssociationProducer::SimHitTPAssociationList& simHitsTPAssoc,
const TrackerTopology& tTopo,
SimHitRefKeyToIndex& simHitRefKeyToIndex,
std::vector<TPHitIndex>& tpHitList);
void fillTrackingParticles(const edm::Event& iEvent,
const edm::EventSetup& iSetup,
const edm::RefToBaseVector<reco::Track>& tracks,
const reco::BeamSpot& bs,
const TrackingParticleRefVector& tpCollection,
const TrackingVertexRefKeyToIndex& tvKeyToIndex,
const reco::TrackToTrackingParticleAssociator& associatorByHits,
const std::vector<TPHitIndex>& tpHitList,
const TrackingParticleRefKeyToCount& tpKeyToClusterCount);
void fillTrackingParticlesForSeeds(const TrackingParticleRefVector& tpCollection,
const reco::SimToRecoCollection& simRecColl,
const TrackingParticleRefKeyToIndex& tpKeyToIndex,
const unsigned int seedOffset);
void fillVertices(const reco::VertexCollection& vertices, const edm::RefToBaseVector<reco::Track>& tracks);
void fillTrackingVertices(const TrackingVertexRefVector& trackingVertices,
const TrackingParticleRefKeyToIndex& tpKeyToIndex);
struct SimHitData {
std::vector<int> matchingSimHit;
std::vector<float> chargeFraction;
std::vector<float> xySignificance;
std::vector<int> bunchCrossing;
std::vector<int> event;
HitSimType type = HitSimType::Unknown;
};
template <typename SimLink>
SimHitData matchCluster(const OmniClusterRef& cluster,
DetId hitId,
int clusterKey,
const TrackingRecHit& hit,
const ClusterTPAssociation& clusterToTPMap,
const TrackingParticleRefKeyToIndex& tpKeyToIndex,
const SimHitTPAssociationProducer::SimHitTPAssociationList& simHitsTPAssoc,
const edm::DetSetVector<SimLink>& digiSimLinks,
const SimHitRefKeyToIndex& simHitRefKeyToIndex,
HitType hitType);
// ----------member data ---------------------------
const edm::ESGetToken<MagneticField, IdealMagneticFieldRecord> mfToken_;
const edm::ESGetToken<TrackerTopology, TrackerTopologyRcd> tTopoToken_;
const edm::ESGetToken<TrackerGeometry, TrackerDigiGeometryRecord> tGeomToken_;
std::vector<edm::EDGetTokenT<edm::View<reco::Track>>> seedTokens_;
std::vector<edm::EDGetTokenT<std::vector<SeedStopInfo>>> seedStopInfoTokens_;
std::vector<edm::EDGetTokenT<TrackCandidateCollection>> candidateTokens_;
edm::EDGetTokenT<edm::View<reco::Track>> trackToken_;
std::vector<std::tuple<edm::EDGetTokenT<MVACollection>, edm::EDGetTokenT<QualityMaskCollection>>>
mvaQualityCollectionTokens_;
edm::EDGetTokenT<TrackingParticleCollection> trackingParticleToken_;
edm::EDGetTokenT<TrackingParticleRefVector> trackingParticleRefToken_;
edm::EDGetTokenT<ClusterTPAssociation> clusterTPMapToken_;
edm::EDGetTokenT<SimHitTPAssociationProducer::SimHitTPAssociationList> simHitTPMapToken_;
edm::EDGetTokenT<reco::TrackToTrackingParticleAssociator> trackAssociatorToken_;
edm::EDGetTokenT<edm::DetSetVector<PixelDigiSimLink>> pixelSimLinkToken_;
edm::EDGetTokenT<edm::DetSetVector<StripDigiSimLink>> stripSimLinkToken_;
edm::EDGetTokenT<edm::DetSetVector<PixelDigiSimLink>> siphase2OTSimLinksToken_;
bool includeStripHits_, includePhase2OTHits_;
edm::EDGetTokenT<reco::BeamSpot> beamSpotToken_;
edm::EDGetTokenT<SiPixelRecHitCollection> pixelRecHitToken_;
edm::EDGetTokenT<SiStripRecHit2DCollection> stripRphiRecHitToken_;
edm::EDGetTokenT<SiStripRecHit2DCollection> stripStereoRecHitToken_;
edm::EDGetTokenT<SiStripMatchedRecHit2DCollection> stripMatchedRecHitToken_;
edm::EDGetTokenT<Phase2TrackerRecHit1DCollectionNew> phase2OTRecHitToken_;
edm::EDGetTokenT<reco::VertexCollection> vertexToken_;
edm::EDGetTokenT<TrackingVertexCollection> trackingVertexToken_;
edm::EDGetTokenT<edm::ValueMap<unsigned int>> tpNLayersToken_;
edm::EDGetTokenT<edm::ValueMap<unsigned int>> tpNPixelLayersToken_;
edm::EDGetTokenT<edm::ValueMap<unsigned int>> tpNStripStereoLayersToken_;
std::vector<std::pair<unsigned int, edm::EDGetTokenT<PixelMaskContainer>>> pixelUseMaskTokens_;
std::vector<std::pair<unsigned int, edm::EDGetTokenT<StripMaskContainer>>> stripUseMaskTokens_;
std::vector<std::pair<unsigned int, edm::EDGetTokenT<Phase2OTMaskContainer>>> ph2OTUseMaskTokens_;
std::string builderName_;
const bool includeSeeds_;
const bool includeTrackCandidates_;
const bool addSeedCurvCov_;
const bool includeAllHits_;
const bool includeOnTrackHitData_;
const bool includeMVA_;
const bool includeTrackingParticles_;
const bool includeOOT_;
const bool keepEleSimHits_;
const bool saveSimHitsP3_;
const bool simHitBySignificance_;
HistoryBase tracer_;
ParametersDefinerForTP parametersDefiner_;
TTree* t;
// DetId branches
#define BOOK(name) tree->Branch((prefix + "_" + #name).c_str(), &name);
class DetIdCommon {
public:
DetIdCommon(){};
unsigned int operator[](size_t i) const { return detId[i]; }
void book(const std::string& prefix, TTree* tree) {
BOOK(detId);
BOOK(subdet);
BOOK(layer);
BOOK(side);
BOOK(module);
BOOK(moduleType);
}
void push_back(const TrackerGeometry& tracker, const TrackerTopology& tTopo, const DetId& id) {
detId.push_back(id.rawId());
subdet.push_back(id.subdetId());
layer.push_back(tTopo.layer(id));
module.push_back(tTopo.module(id));
moduleType.push_back(static_cast<int>(tracker.getDetectorType(id)));
unsigned short s = 0;
switch (id.subdetId()) {
case StripSubdetector::TIB:
s = tTopo.tibSide(id);
break;
case StripSubdetector::TOB:
s = tTopo.tobSide(id);
break;
default:
s = tTopo.side(id);
}
side.push_back(s);
}
void resize(size_t size) {
detId.resize(size);
subdet.resize(size);
layer.resize(size);
side.resize(size);
module.resize(size);
moduleType.resize(size);
}
void set(size_t index, const TrackerGeometry& tracker, const TrackerTopology& tTopo, const DetId& id) {
detId[index] = id.rawId();
subdet[index] = id.subdetId();
layer[index] = tTopo.layer(id);
side[index] = tTopo.side(id);
module[index] = tTopo.module(id);
moduleType[index] = static_cast<int>(tracker.getDetectorType(id));
}
void clear() {
detId.clear();
subdet.clear();
layer.clear();
side.clear();
module.clear();
moduleType.clear();
}
private:
std::vector<unsigned int> detId;
std::vector<unsigned short> subdet;
std::vector<unsigned short> layer; // or disk/wheel
std::vector<unsigned short> side;
std::vector<unsigned short> module;
std::vector<unsigned int> moduleType;
};
class DetIdPixelOnly {
public:
DetIdPixelOnly() {}
void book(const std::string& prefix, TTree* tree) {
BOOK(ladder);
BOOK(blade);
BOOK(panel);
}
void push_back(const TrackerGeometry& tracker, const TrackerTopology& tTopo, const DetId& id) {
const bool isBarrel = id.subdetId() == PixelSubdetector::PixelBarrel;
ladder.push_back(isBarrel ? tTopo.pxbLadder(id) : 0);
blade.push_back(isBarrel ? 0 : tTopo.pxfBlade(id));
panel.push_back(isBarrel ? 0 : tTopo.pxfPanel(id));
}
void clear() {
ladder.clear();
blade.clear();
panel.clear();
}
private:
std::vector<unsigned short> ladder;
std::vector<unsigned short> blade;
std::vector<unsigned short> panel;
};
class DetIdOTCommon {
public:
DetIdOTCommon() {}
void book(const std::string& prefix, TTree* tree) {
BOOK(order);
BOOK(ring);
BOOK(rod);
}
void push_back(const TrackerGeometry& tracker, const TrackerTopology& tTopo, const DetId& id) {
const auto parsed = parse(tTopo, id);
order.push_back(parsed.order);
ring.push_back(parsed.ring);
rod.push_back(parsed.rod);
}
void resize(size_t size) {
order.resize(size);
ring.resize(size);
rod.resize(size);
}
void set(size_t index, const TrackerGeometry& tracker, const TrackerTopology& tTopo, const DetId& id) {
const auto parsed = parse(tTopo, id);
order[index] = parsed.order;
ring[index] = parsed.ring;
rod[index] = parsed.rod;
}
void clear() {
order.clear();
ring.clear();
rod.clear();
}
private:
struct Parsed {
// use int here instead of short to avoid compilation errors due
// to narrowing conversion (less boilerplate than explicit static_casts)
unsigned int order = 0;
unsigned int ring = 0;
unsigned int rod = 0;
};
Parsed parse(const TrackerTopology& tTopo, const DetId& id) const {
switch (id.subdetId()) {
case StripSubdetector::TIB:
return Parsed{tTopo.tibOrder(id), 0, 0};
case StripSubdetector::TID:
return Parsed{tTopo.tidOrder(id), tTopo.tidRing(id), 0};
case StripSubdetector::TOB:
return Parsed{0, 0, tTopo.tobRod(id)};
case StripSubdetector::TEC:
return Parsed{tTopo.tecOrder(id), tTopo.tecRing(id), 0};
default:
return Parsed{};
};
}
std::vector<unsigned short> order;
std::vector<unsigned short> ring;
std::vector<unsigned short> rod;
};
class DetIdStripOnly {
public:
DetIdStripOnly() {}
void book(const std::string& prefix, TTree* tree) {
BOOK(string);
BOOK(petalNumber);
BOOK(isStereo);
BOOK(isRPhi);
BOOK(isGlued);
}
void push_back(const TrackerGeometry& tracker, const TrackerTopology& tTopo, const DetId& id) {
const auto parsed = parse(tTopo, id);
string.push_back(parsed.string);
petalNumber.push_back(parsed.petalNumber);
isStereo.push_back(tTopo.isStereo(id));
isRPhi.push_back(tTopo.isRPhi(id));
isGlued.push_back(parsed.glued);
}
void resize(size_t size) {
string.resize(size);
petalNumber.resize(size);
isStereo.resize(size);
isRPhi.resize(size);
isGlued.resize(size);
}
void set(size_t index, const TrackerGeometry& tracker, const TrackerTopology& tTopo, const DetId& id) {
const auto parsed = parse(tTopo, id);
string[index] = parsed.string;
petalNumber[index] = parsed.petalNumber;
isStereo[index] = tTopo.isStereo(id);
isRPhi[index] = tTopo.isRPhi(id);
isGlued[index] = parsed.glued;
}
void clear() {
string.clear();
isStereo.clear();
isRPhi.clear();
isGlued.clear();
petalNumber.clear();
}
private:
struct Parsed {
// use int here instead of short to avoid compilation errors due
// to narrowing conversion (less boilerplate than explicit static_casts)
unsigned int string = 0;
unsigned int petalNumber = 0;
bool glued = false;
};
Parsed parse(const TrackerTopology& tTopo, const DetId& id) const {
switch (id.subdetId()) {
case StripSubdetector::TIB:
return Parsed{tTopo.tibString(id), 0, tTopo.tibIsDoubleSide(id)};
case StripSubdetector::TID:
return Parsed{0, 0, tTopo.tidIsDoubleSide(id)};
case StripSubdetector::TOB:
return Parsed{0, 0, tTopo.tobIsDoubleSide(id)};
case StripSubdetector::TEC:
return Parsed{0, tTopo.tecPetalNumber(id), tTopo.tecIsDoubleSide(id)};
default:
return Parsed{};
}
}
std::vector<unsigned short> string;
std::vector<unsigned short> petalNumber;
std::vector<unsigned short> isStereo;
std::vector<unsigned short> isRPhi;
std::vector<unsigned short> isGlued;
};
class DetIdPhase2OTOnly {
public:
DetIdPhase2OTOnly() {}
void book(const std::string& prefix, TTree* tree) {
BOOK(isLower);
BOOK(isUpper);
BOOK(isStack);
}
void push_back(const TrackerGeometry& tracker, const TrackerTopology& tTopo, const DetId& id) {
isLower.push_back(tTopo.isLower(id));
isUpper.push_back(tTopo.isUpper(id));
isStack.push_back(tTopo.stack(id) ==
0); // equivalent to *IsDoubleSide() but without the hardcoded layer+ring requirements
}
void clear() {
isLower.clear();
isUpper.clear();
isStack.clear();
}
private:
std::vector<unsigned short> isLower;
std::vector<unsigned short> isUpper;
std::vector<unsigned short> isStack;
};
#undef BOOK
using DetIdPixel = CombineDetId<DetIdCommon, DetIdPixelOnly>;
using DetIdStrip = CombineDetId<DetIdCommon, DetIdOTCommon, DetIdStripOnly>;
using DetIdPhase2OT = CombineDetId<DetIdCommon, DetIdOTCommon, DetIdPhase2OTOnly>;
using DetIdAll = CombineDetId<DetIdCommon, DetIdPixelOnly, DetIdOTCommon, DetIdStripOnly>;
using DetIdAllPhase2 = CombineDetId<DetIdCommon, DetIdPixelOnly, DetIdOTCommon, DetIdPhase2OTOnly>;
// event
edm::RunNumber_t ev_run;
edm::LuminosityBlockNumber_t ev_lumi;
edm::EventNumber_t ev_event;
////////////////////
// tracks
// (first) index runs through tracks
std::vector<float> trk_px;
std::vector<float> trk_py;
std::vector<float> trk_pz;
std::vector<float> trk_pt;
std::vector<float> trk_inner_px;
std::vector<float> trk_inner_py;
std::vector<float> trk_inner_pz;