CMS 3D CMS Logo

L1TrackQuality.cc
Go to the documentation of this file.
1 /*
2 Track Quality Body file
3 C.Brown & C.Savard 07/2020
4 */
5 
7 
8 //Constructors
9 
11 
12 L1TrackQuality::L1TrackQuality(const edm::ParameterSet& qualityParams) : useHPH_(false), bonusFeatures_() {
13  // Unpacks EDM parameter set itself to save unecessary processing within TrackProducers
14  setModel(qualityParams.getParameter<edm::FileInPath>("model"),
15  qualityParams.getParameter<std::vector<std::string>>("featureNames"));
16 }
17 
19  std::vector<std::string> const& featureNames) {
20  // List input features for MVA in proper order below, the current features options are
21  // {"phi", "eta", "z0", "bendchi2_bin", "nstub", "nlaymiss_interior", "chi2rphi_bin",
22  // "chi2rz_bin"}
23  //
24  // To use more features, they must be created here and added to feature_map below
25 
26  std::vector<float> transformedFeatures;
27 
28  // Define feature map, filled as features are generated
29  std::map<std::string, float> feature_map;
30 
31  // -------- calculate feature variables --------
32 
33  // calculate number of missed interior layers from hitpattern
34  int tmp_trk_hitpattern = aTrack.hitPattern();
35  int nbits = floor(log2(tmp_trk_hitpattern)) + 1;
36  int lay_i = 0;
37  int tmp_trk_nlaymiss_interior = 0;
38  bool seq = false;
39  for (int i = 0; i < nbits; i++) {
40  lay_i = ((1 << i) & tmp_trk_hitpattern) >> i; //0 or 1 in ith bit (right to left)
41 
42  if (lay_i && !seq)
43  seq = true; //sequence starts when first 1 found
44  if (!lay_i && seq)
45  tmp_trk_nlaymiss_interior++;
46  }
47 
48  // binned chi2 variables
49  int tmp_trk_bendchi2_bin = aTrack.getBendChi2Bits();
50  int tmp_trk_chi2rphi_bin = aTrack.getChi2RPhiBits();
51  int tmp_trk_chi2rz_bin = aTrack.getChi2RZBits();
52 
53  // get the nstub
54  std::vector<edm::Ref<edmNew::DetSetVector<TTStub<Ref_Phase2TrackerDigi_>>, TTStub<Ref_Phase2TrackerDigi_>>> stubRefs =
55  aTrack.getStubRefs();
56  int tmp_trk_nstub = stubRefs.size();
57 
58  // get other variables directly from TTTrack
59  float tmp_trk_z0 = aTrack.z0();
60  float tmp_trk_z0_scaled = tmp_trk_z0 / abs(aTrack.minZ0);
61  float tmp_trk_phi = aTrack.phi();
62  float tmp_trk_eta = aTrack.eta();
63  float tmp_trk_tanl = aTrack.tanL();
64 
65  // -------- fill the feature map ---------
66 
67  feature_map["nstub"] = float(tmp_trk_nstub);
68  feature_map["z0"] = tmp_trk_z0;
69  feature_map["z0_scaled"] = tmp_trk_z0_scaled;
70  feature_map["phi"] = tmp_trk_phi;
71  feature_map["eta"] = tmp_trk_eta;
72  feature_map["nlaymiss_interior"] = float(tmp_trk_nlaymiss_interior);
73  feature_map["bendchi2_bin"] = tmp_trk_bendchi2_bin;
74  feature_map["chi2rphi_bin"] = tmp_trk_chi2rphi_bin;
75  feature_map["chi2rz_bin"] = tmp_trk_chi2rz_bin;
76  feature_map["tanl"] = tmp_trk_tanl;
77 
78  // fill tensor with track params
79  transformedFeatures.reserve(featureNames.size());
80  for (const std::string& feature : featureNames)
81  transformedFeatures.push_back(feature_map[feature]);
82 
83  return transformedFeatures;
84 }
85 
87  // load in bdt
88  conifer::BDT<float, float> bdt(this->model_.fullPath());
89 
90  // collect features and classify using bdt
91  std::vector<float> inputs = featureTransform(aTrack, this->featureNames_);
92  std::vector<float> output = bdt.decision_function(inputs);
93  aTrack.settrkMVA1(1. / (1. + exp(-output.at(0))));
94 }
95 
96 float L1TrackQuality::runEmulatedTQ(std::vector<ap_fixed<10, 5>> inputFeatures) {
97  // load in bdt
98 
99  conifer::BDT<ap_fixed<10, 5>, ap_fixed<10, 5>> bdt(this->model_.fullPath());
100 
101  // collect features and classify using bdt
102  std::vector<ap_fixed<10, 5>> output = bdt.decision_function(inputFeatures);
103  return output.at(0).to_float(); // need logistic sigmoid fcn applied to xgb output
104 }
105 
106 void L1TrackQuality::setModel(edm::FileInPath const& model, std::vector<std::string> const& featureNames) {
107  //Convert algorithm string to Enum class for track by track comparison
108  model_ = model;
110 }
111 
112 void L1TrackQuality::setBonusFeatures(std::vector<float> bonusFeatures) {
113  bonusFeatures_ = bonusFeatures;
114  useHPH_ = true;
115 }
T getParameter(std::string const &) const
Definition: ParameterSet.h:307
double eta() const
Track eta.
Definition: TTTrack.h:310
std::string fullPath() const
Definition: FileInPath.cc:161
unsigned int getChi2RZBits() const
void setL1TrackQuality(TTTrack< Ref_Phase2TrackerDigi_ > &aTrack)
static constexpr double minZ0
unsigned int getChi2RPhiBits() const
edm::FileInPath model_
double phi() const
Track phi.
Definition: TTTrack.h:315
double tanL() const
Track tanL.
Definition: TTTrack.h:305
unsigned int hitPattern() const
Hit Pattern.
Definition: TTTrack.h:424
void settrkMVA1(double atrkMVA1)
Definition: TTTrack.h:382
Abs< T >::type abs(const T &t)
Definition: Abs.h:22
float runEmulatedTQ(std::vector< ap_fixed< 10, 5 >> inputFeatures)
Class to store the L1 Track Trigger stubs.
Definition: TTStub.h:22
std::vector< edm::Ref< edmNew::DetSetVector< TTStub< T > >, TTStub< T > > > getStubRefs() const
Track components.
Definition: TTTrack.h:93
void setModel(edm::FileInPath const &model, std::vector< std::string > const &featureNames)
std::vector< float > featureTransform(TTTrack< Ref_Phase2TrackerDigi_ > &aTrack, std::vector< std::string > const &featureNames)
Class to store the L1 Track Trigger tracks.
Definition: TTTrack.h:29
std::vector< float > bonusFeatures_
std::vector< std::string > featureNames_
unsigned int getBendChi2Bits() const
double z0() const
Track z0.
Definition: TTTrack.h:330
Definition: output.py:1
void setBonusFeatures(std::vector< float > bonusFeatures)