CMS 3D CMS Logo

SeedMvaEstimatorPhase2.cc
Go to the documentation of this file.
2 
5 
8 
9 #include <cmath>
10 
11 using namespace std;
12 
13 namespace Phase2 {
15  kTsosErr0, // 0
16  kTsosErr2, // 1
17  kTsosErr5, // 2
18  kTsosDxdz, // 3
19  kTsosDydz, // 4
20  kTsosQbp, // 5
26  kLast // 11
27  };
28 }
29 
31  const std::vector<double>& scale_mean,
32  const std::vector<double>& scale_std)
33  : scale_mean_(scale_mean), scale_std_(scale_std) {
34  gbrForest_ = createGBRForest(weightsfile);
35 }
36 
38 
40  const GlobalVector& global_p,
41  const GlobalPoint& global_x,
43  float& DRL1TkMu,
44  float& DPhiL1TkMu) const {
45  for (auto L1TkMu = h_L1TkMu->begin(); L1TkMu != h_L1TkMu->end(); ++L1TkMu) {
46  auto TkRef = L1TkMu->trkPtr();
47  float DRL1TkMu_tmp = reco::deltaR(*TkRef, global_p);
48  float DPhiL1TkMu_tmp = reco::deltaPhi(TkRef->phi(), global_p.phi());
49  if (DRL1TkMu_tmp < DRL1TkMu) {
50  DRL1TkMu = DRL1TkMu_tmp;
51  DPhiL1TkMu = DPhiL1TkMu_tmp;
52  }
53  }
54 }
55 
57  const edm::ESHandle<MagneticField>& magfieldH,
59  const GeometricSearchTracker& geomTracker) const {
60  vector<LayerTSOS> v_tsos = {};
61 
62  std::vector<BarrelDetLayer const*> const& bpix = geomTracker.pixelBarrelLayers();
63  std::vector<ForwardDetLayer const*> const& nfpix = geomTracker.negPixelForwardLayers();
64  std::vector<ForwardDetLayer const*> const& pfpix = geomTracker.posPixelForwardLayers();
65 
66  int chargeTk = l1tk.rInv() > 0. ? 1 : -1;
67  GlobalPoint gpos = l1tk.POCA();
68  GlobalVector gmom = l1tk.momentum();
69 
70  FreeTrajectoryState fts = FreeTrajectoryState(gpos, gmom, chargeTk, magfieldH.product());
71 
72  for (auto it = bpix.begin(); it != bpix.end(); ++it) {
73  TrajectoryStateOnSurface tsos = propagatorAlong.propagate(fts, (**it).specificSurface());
74  if (!tsos.isValid())
75  continue;
76 
77  auto z0 = std::abs(tsos.globalPosition().z() - (**it).specificSurface().position().z());
78  auto deltaZ = 0.5f * (**it).surface().bounds().length();
79  deltaZ += 0.5f * (**it).surface().bounds().thickness() * std::abs(tsos.globalDirection().z()) /
80  tsos.globalDirection().perp();
81  bool is_compatible = (z0 < deltaZ);
82 
83  if (is_compatible) {
84  v_tsos.push_back(make_pair((const DetLayer*)(*it), tsos));
85  }
86  }
87  for (auto it = nfpix.begin(); it != nfpix.end(); ++it) {
88  TrajectoryStateOnSurface tsos = propagatorAlong.propagate(fts, (**it).specificSurface());
89  if (!tsos.isValid())
90  continue;
91 
92  auto r2 = tsos.localPosition().perp2();
93  float deltaR = 0.5f * (**it).surface().bounds().thickness() * tsos.localDirection().perp() /
94  std::abs(tsos.localDirection().z());
95  auto ri2 = std::max((**it).specificSurface().innerRadius() - deltaR, 0.f);
96  ri2 *= ri2;
97  auto ro2 = (**it).specificSurface().outerRadius() + deltaR;
98  ro2 *= ro2;
99  bool is_compatible = ((r2 > ri2) && (r2 < ro2));
100 
101  if (is_compatible) {
102  v_tsos.push_back(make_pair((const DetLayer*)(*it), tsos));
103  }
104  }
105  for (auto it = pfpix.begin(); it != pfpix.end(); ++it) {
106  TrajectoryStateOnSurface tsos = propagatorAlong.propagate(fts, (**it).specificSurface());
107  if (!tsos.isValid())
108  continue;
109 
110  auto r2 = tsos.localPosition().perp2();
111  float deltaR = 0.5f * (**it).surface().bounds().thickness() * tsos.localDirection().perp() /
112  std::abs(tsos.localDirection().z());
113  auto ri2 = std::max((**it).specificSurface().innerRadius() - deltaR, 0.f);
114  ri2 *= ri2;
115  auto ro2 = (**it).specificSurface().outerRadius() + deltaR;
116  ro2 *= ro2;
117  bool is_compatible = ((r2 > ri2) && (r2 < ro2));
118  if (is_compatible) {
119  v_tsos.push_back(make_pair((const DetLayer*)(*it), tsos));
120  }
121  }
122  return v_tsos;
123 }
124 
125 // FIX HERE
126 vector<pair<LayerHit, LayerTSOS> > SeedMvaEstimatorPhase2::getHitTsosPairs(
127  const TrajectorySeed& seed,
128  const edm::Handle<l1t::TrackerMuonCollection>& L1TkMuonHandle,
129  const edm::ESHandle<MagneticField>& magfieldH,
131  const GeometricSearchTracker& geomTracker) const {
132  vector<pair<LayerHit, LayerTSOS> > out = {};
133  float av_dr_min = 999.;
134 
135  // -- loop on L1TkMu
136  for (auto L1TkMu = L1TkMuonHandle->begin(); L1TkMu != L1TkMuonHandle->end(); ++L1TkMu) {
137  const vector<LayerTSOS> v_tsos = getTsosOnPixels(*L1TkMu->trkPtr(), magfieldH, propagatorAlong, geomTracker);
138 
139  // -- loop on recHits
140  vector<int> v_tsos_skip(v_tsos.size(), 0);
141  vector<pair<LayerHit, LayerTSOS> > hitTsosPair = {};
142  int ihit = 0;
143  for (const auto& hit : seed.recHits()) {
144  // -- look for closest tsos by absolute distance
145  int the_tsos = -99999;
146  float dr_min = 20.;
147  for (auto i = 0U; i < v_tsos.size(); ++i) {
148  if (v_tsos_skip.at(i))
149  continue;
150  float dr = (v_tsos.at(i).second.globalPosition() - hit.globalPosition()).mag();
151  if (dr < dr_min) {
152  dr_min = dr;
153  the_tsos = i;
154  v_tsos_skip.at(i) = 1;
155  }
156  }
157  if (the_tsos > -1) {
158  const DetLayer* thelayer = geomTracker.idToLayer(hit.geographicalId());
159  hitTsosPair.push_back(make_pair(make_pair(thelayer, &hit), v_tsos.at(the_tsos)));
160  }
161  ihit++;
162  } // loop on recHits
163 
164  // -- find tsos for all recHits?
165  if ((int)hitTsosPair.size() == ihit) {
166  float av_dr = 0.;
167  for (auto it = hitTsosPair.begin(); it != hitTsosPair.end(); ++it) {
168  auto hit = it->first.second;
169  auto tsos = it->second.second;
170  av_dr += (hit->globalPosition() - tsos.globalPosition()).mag();
171  }
172  av_dr = av_dr > 0 ? av_dr / (float)hitTsosPair.size() : 1.e6;
173  if (av_dr < av_dr_min) {
174  av_dr_min = av_dr;
175  out = hitTsosPair;
176  }
177  }
178  } // loop on L1TkMu
179  return out;
180 }
181 
183  const edm::Handle<l1t::TrackerMuonCollection>& L1TkMuonHandle,
184  const edm::ESHandle<MagneticField>& magfieldH,
186  const GeometricSearchTracker& geomTracker,
187  float& expd2HitL1Tk1,
188  float& expd2HitL1Tk2,
189  float& expd2HitL1Tk3) const {
190  vector<pair<LayerHit, LayerTSOS> > hitTsosPair =
191  getHitTsosPairs(seed, L1TkMuonHandle, magfieldH, propagatorAlong, geomTracker);
192 
193  bool found = (!hitTsosPair.empty());
194 
195  if (found) {
196  float l1x1 = 99999.;
197  float l1y1 = 99999.;
198  float l1z1 = 99999.;
199  float hitx1 = 99999.;
200  float hity1 = 99999.;
201  float hitz1 = 99999.;
202 
203  float l1x2 = 99999.;
204  float l1y2 = 99999.;
205  float l1z2 = 99999.;
206  float hitx2 = 99999.;
207  float hity2 = 99999.;
208  float hitz2 = 99999.;
209 
210  float l1x3 = 99999.;
211  float l1y3 = 99999.;
212  float l1z3 = 99999.;
213  float hitx3 = 99999.;
214  float hity3 = 99999.;
215  float hitz3 = 99999.;
216 
217  if (hitTsosPair.size() > 1) {
218  auto hit1 = hitTsosPair.at(0).first.second;
219  auto tsos1 = hitTsosPair.at(0).second.second;
220 
221  l1x1 = tsos1.globalPosition().x();
222  l1y1 = tsos1.globalPosition().y();
223  l1z1 = tsos1.globalPosition().z();
224  hitx1 = hit1->globalPosition().x();
225  hity1 = hit1->globalPosition().y();
226  hitz1 = hit1->globalPosition().z();
227 
228  auto hit2 = hitTsosPair.at(1).first.second;
229  auto tsos2 = hitTsosPair.at(1).second.second;
230 
231  l1x2 = tsos2.globalPosition().x();
232  l1y2 = tsos2.globalPosition().y();
233  l1z2 = tsos2.globalPosition().z();
234  hitx2 = hit2->globalPosition().x();
235  hity2 = hit2->globalPosition().y();
236  hitz2 = hit2->globalPosition().z();
237  }
238  if (hitTsosPair.size() > 2) {
239  auto hit3 = hitTsosPair.at(2).first.second;
240  auto tsos3 = hitTsosPair.at(2).second.second;
241 
242  l1x3 = tsos3.globalPosition().x();
243  l1y3 = tsos3.globalPosition().y();
244  l1z3 = tsos3.globalPosition().z();
245  hitx3 = hit3->globalPosition().x();
246  hity3 = hit3->globalPosition().y();
247  hitz3 = hit3->globalPosition().z();
248  }
249 
250  // If hit == 99999. >> cannot find hit info >> distance btw hit~tsos is large >> expd2HitL1Tk ~ 0
251  if (hitx1 != 99999.) {
252  expd2HitL1Tk1 = exp(-(pow((l1x1 - hitx1), 2) + pow((l1y1 - hity1), 2) + pow((l1z1 - hitz1), 2)));
253  } else {
254  expd2HitL1Tk1 = 0.;
255  }
256 
257  if (hitx2 != 99999.) {
258  expd2HitL1Tk2 = exp(-(pow((l1x2 - hitx2), 2) + pow((l1y2 - hity2), 2) + pow((l1z2 - hitz2), 2)));
259  } else {
260  expd2HitL1Tk2 = 0.;
261  }
262 
263  if (hitx3 != 99999.) {
264  expd2HitL1Tk3 = exp(-(pow((l1x3 - hitx3), 2) + pow((l1y3 - hity3), 2) + pow((l1z3 - hitz3), 2)));
265  } else {
266  expd2HitL1Tk3 = 0.;
267  }
268  }
269 }
270 
272  const GlobalVector& global_p,
273  const GlobalPoint& global_x,
275  const edm::ESHandle<MagneticField>& magfieldH,
277  const GeometricSearchTracker& geomTracker) const {
278  float Phase2var[Phase2::kLast]{};
279 
280  Phase2var[Phase2::kTsosErr0] = seed.startingState().error(0);
281  Phase2var[Phase2::kTsosErr2] = seed.startingState().error(2);
282  Phase2var[Phase2::kTsosErr5] = seed.startingState().error(5);
283  Phase2var[Phase2::kTsosDxdz] = seed.startingState().parameters().dxdz();
284  Phase2var[Phase2::kTsosDydz] = seed.startingState().parameters().dydz();
285  Phase2var[Phase2::kTsosQbp] = seed.startingState().parameters().qbp();
286 
287  float initDRdPhi = 99999.;
288 
289  float DRL1TkMuSeedP = initDRdPhi;
290  float DPhiL1TkMuSeedP = initDRdPhi;
291  getL1TTVariables(seed, global_p, global_x, h_L1TkMu, DRL1TkMuSeedP, DPhiL1TkMuSeedP);
292  Phase2var[Phase2::kDRdRL1TkMuSeedP] = DRL1TkMuSeedP;
293  Phase2var[Phase2::kDRdPhiL1TkMuSeedP] = DPhiL1TkMuSeedP;
294 
295  float expd2HitL1Tk1 = initDRdPhi;
296  float expd2HitL1Tk2 = initDRdPhi;
297  float expd2HitL1Tk3 = initDRdPhi;
299  seed, h_L1TkMu, magfieldH, propagatorAlong, geomTracker, expd2HitL1Tk1, expd2HitL1Tk2, expd2HitL1Tk3);
300  Phase2var[Phase2::kExpd2HitL1Tk1] = expd2HitL1Tk1;
301  Phase2var[Phase2::kExpd2HitL1Tk2] = expd2HitL1Tk2;
302  Phase2var[Phase2::kExpd2HitL1Tk3] = expd2HitL1Tk3;
303 
304  for (int iv = 0; iv < Phase2::kLast; ++iv) {
305  Phase2var[iv] = (Phase2var[iv] - scale_mean_.at(iv)) / scale_std_.at(iv);
306  }
307 
308  return gbrForest_->GetResponse(Phase2var);
309 }
constexpr double deltaPhi(double phi1, double phi2)
Definition: deltaPhi.h:26
vector< LayerTSOS > getTsosOnPixels(const TTTrack< Ref_Phase2TrackerDigi_ > &, const edm::ESHandle< MagneticField > &, const Propagator &, const GeometricSearchTracker &) const
T perp() const
Definition: PV3DBase.h:69
SeedMvaEstimatorPhase2(const edm::FileInPath &weightsfile, const std::vector< double > &scale_mean, const std::vector< double > &scale_std)
vector< pair< LayerHit, LayerTSOS > > getHitTsosPairs(const TrajectorySeed &, const edm::Handle< l1t::TrackerMuonCollection > &, const edm::ESHandle< MagneticField > &, const Propagator &, const GeometricSearchTracker &) const
T z() const
Definition: PV3DBase.h:61
Geom::Phi< T > phi() const
Definition: PV3DBase.h:66
GlobalVector momentum() const
Track momentum.
Definition: TTTrack.h:295
std::vector< ForwardDetLayer const * > const & posPixelForwardLayers() const
const std::vector< double > scale_std_
const DetLayer * idToLayer(const DetId &detId) const override
Give the DetId of a module, returns the pointer to the corresponding DetLayer.
std::unique_ptr< const GBRForest > gbrForest_
T const * product() const
Definition: ESHandle.h:86
GlobalPoint globalPosition() const
LocalVector localDirection() const
std::vector< ForwardDetLayer const * > const & negPixelForwardLayers() const
Abs< T >::type abs(const T &t)
Definition: Abs.h:22
double rInv() const
Track curvature.
Definition: TTTrack.h:300
constexpr auto deltaR(const T1 &t1, const T2 &t2) -> decltype(t1.eta())
Definition: deltaR.h:30
std::vector< BarrelDetLayer const * > const & pixelBarrelLayers() const
T mag() const
The vector magnitude. Equivalent to sqrt(vec.mag2())
Class to store the L1 Track Trigger tracks.
Definition: TTTrack.h:29
GlobalVector globalDirection() const
void getL1TTVariables(const TrajectorySeed &, const GlobalVector &, const GlobalPoint &, const edm::Handle< l1t::TrackerMuonCollection > &, float &, float &) const
T perp2() const
Definition: PV3DBase.h:68
double computeMva(const TrajectorySeed &, const GlobalVector &, const GlobalPoint &, const edm::Handle< l1t::TrackerMuonCollection > &, const edm::ESHandle< MagneticField > &, const Propagator &, const GeometricSearchTracker &) const
void getHitL1TkVariables(const TrajectorySeed &, const edm::Handle< l1t::TrackerMuonCollection > &, const edm::ESHandle< MagneticField > &, const Propagator &, const GeometricSearchTracker &, float &, float &, float &) const
const std::vector< double > scale_mean_
Power< A, B >::type pow(const A &a, const B &b)
Definition: Power.h:29
GlobalPoint POCA() const
POCA.
Definition: TTTrack.h:335
std::unique_ptr< const GBRForest > createGBRForest(const std::string &weightsFile)