CMS 3D CMS Logo

MuonHLTSeedMVAClassifierPhase2.cc
Go to the documentation of this file.
1 // Package: RecoMuon_TrackerSeedGenerator
2 // Class: MuonHLTSeedMVAClassifierPhase2
3 
4 // Original Author: OH Minseok, Sungwon Kim, Won Jun
5 // Created: Mon, 08 Jun 2020 06:20:44 GMT
6 
7 // system include files
8 #include <memory>
9 #include <cmath>
10 
11 // user include files
21 
22 // Geometry
25 
26 // TrajectorySeed
33 
34 // -- for L1TkMu propagation
44 
46 
47 // class declaration
48 bool sortByMvaScorePhase2(const std::pair<unsigned, double>& A, const std::pair<unsigned, double>& B) {
49  return (A.second > B.second);
50 };
51 
53 public:
55  ~MuonHLTSeedMVAClassifierPhase2() override = default;
56 
57  static void fillDescriptions(edm::ConfigurationDescriptions& descriptions);
58 
59 private:
60  void produce(edm::Event&, const edm::EventSetup&) override;
61 
62  // ----------member data ---------------------------
65 
66  typedef std::pair<std::unique_ptr<const SeedMvaEstimatorPhase2>, std::unique_ptr<const SeedMvaEstimatorPhase2>>
69 
72 
73  const std::vector<double> mvaScaleMean_B_;
74  const std::vector<double> mvaScaleStd_B_;
75  const std::vector<double> mvaScaleMean_E_;
76  const std::vector<double> mvaScaleStd_E_;
77 
78  const double etaEdge_;
79  const double mvaCut_B_;
80  const double mvaCut_E_;
81 
82  const bool doSort_;
83  const int nSeedsMax_B_;
84  const int nSeedsMax_E_;
85 
86  const double baseScore_;
87 
93 
94  double getSeedMva(const pairSeedMvaEstimator& pairMvaEstimator,
95  const TrajectorySeed& seed,
96  const GlobalVector& global_p,
97  const GlobalPoint& global_x,
99  const edm::ESHandle<MagneticField>& magfieldH,
101  const GeometricSearchTracker& geomTracker);
102 };
103 
104 // constructors and destructor
106  : t_Seed_(consumes<TrajectorySeedCollection>(iConfig.getParameter<edm::InputTag>("src"))),
107  t_L1TkMu_(consumes<l1t::TrackerMuonCollection>(iConfig.getParameter<edm::InputTag>("L1TkMu"))),
108 
109  mvaFile_B_0_(iConfig.getParameter<edm::FileInPath>("mvaFile_B_0")),
110  mvaFile_E_0_(iConfig.getParameter<edm::FileInPath>("mvaFile_E_0")),
111 
112  mvaScaleMean_B_(iConfig.getParameter<std::vector<double>>("mvaScaleMean_B")),
113  mvaScaleStd_B_(iConfig.getParameter<std::vector<double>>("mvaScaleStd_B")),
114  mvaScaleMean_E_(iConfig.getParameter<std::vector<double>>("mvaScaleMean_E")),
115  mvaScaleStd_E_(iConfig.getParameter<std::vector<double>>("mvaScaleStd_E")),
116 
117  etaEdge_(iConfig.getParameter<double>("etaEdge")),
118  mvaCut_B_(iConfig.getParameter<double>("mvaCut_B")),
119  mvaCut_E_(iConfig.getParameter<double>("mvaCut_E")),
120 
121  doSort_(iConfig.getParameter<bool>("doSort")),
122  nSeedsMax_B_(iConfig.getParameter<int>("nSeedsMax_B")),
123  nSeedsMax_E_(iConfig.getParameter<int>("nSeedsMax_E")),
124 
125  baseScore_(iConfig.getParameter<double>("baseScore")),
126 
127  trackerTopologyESToken_(esConsumes<TrackerTopology, TrackerTopologyRcd>()),
128  trackerGeometryESToken_(esConsumes<TrackerGeometry, TrackerDigiGeometryRecord>()),
129  magFieldESToken_(esConsumes<MagneticField, IdealMagneticFieldRecord>()),
130  geomDetESToken_(esConsumes<GeometricDet, IdealGeometryRecord>()),
131  propagatorESToken_(
132  esConsumes<Propagator, TrackingComponentsRecord>(edm::ESInputTag("", "PropagatorWithMaterialParabolicMf"))) {
133  produces<TrajectorySeedCollection>();
134 
135  mvaEstimator_ =
136  std::make_pair(std::make_unique<SeedMvaEstimatorPhase2>(mvaFile_B_0_, mvaScaleMean_B_, mvaScaleStd_B_),
137  std::make_unique<SeedMvaEstimatorPhase2>(mvaFile_E_0_, mvaScaleMean_E_, mvaScaleStd_E_));
138 }
139 
140 // member functions
141 // ------------ method called on each new Event ------------
143  auto result = std::make_unique<TrajectorySeedCollection>();
144 
148 
150  GeometricSearchTracker geomTracker = *(builder.build(&(*geomDet), &(*trkGeom), &(*trkTopo)));
151 
153  bool hasL1TkMu = iEvent.getByToken(t_L1TkMu_, h_L1TkMu);
154 
156  bool hasSeed = iEvent.getByToken(t_Seed_, h_Seed);
157 
159  edm::ESHandle<Propagator> propagatorAlongH = iSetup.getHandle(propagatorESToken_);
160  std::unique_ptr<Propagator> propagatorAlong = SetPropagationDirection(*propagatorAlongH, alongMomentum);
161 
162  if (!(hasL1TkMu && hasSeed)) {
163  edm::LogError("SeedClassifierError") << "Error! Cannot find L1TkMuon or TrajectorySeed\n"
164  << "hasL1TkMu : " << hasL1TkMu << "\n"
165  << "hasSeed : " << hasSeed << "\n";
166  return;
167  }
168 
169  // -- sort seeds by MVA score and chooes top nSeedsMax_B_ / nSeedsMax_E_
170  if (doSort_) {
171  std::vector<std::pair<unsigned, double>> pairSeedIdxMvaScore_B = {};
172  std::vector<std::pair<unsigned, double>> pairSeedIdxMvaScore_E = {};
173 
174  for (auto i = 0U; i < h_Seed->size(); ++i) {
175  const auto& seed(h_Seed->at(i));
176 
177  GlobalVector global_p = trkGeom->idToDet(seed.startingState().detId())
178  ->surface()
179  .toGlobal(seed.startingState().parameters().momentum());
180  GlobalPoint global_x = trkGeom->idToDet(seed.startingState().detId())
181  ->surface()
182  .toGlobal(seed.startingState().parameters().position());
183 
184  bool isB = (std::abs(global_p.eta()) < etaEdge_);
185 
186  if (isB && nSeedsMax_B_ < 0) {
187  result->emplace_back(seed);
188  continue;
189  }
190 
191  if (!isB && nSeedsMax_E_ < 0) {
192  result->emplace_back(seed);
193  continue;
194  }
195 
196  double mva = getSeedMva(
197  mvaEstimator_, seed, global_p, global_x, h_L1TkMu, magfieldH, *(propagatorAlong.get()), geomTracker);
198 
199  double logistic = 1 / (1 + std::exp(-mva));
200 
201  if (isB)
202  pairSeedIdxMvaScore_B.push_back(make_pair(i, logistic));
203  else
204  pairSeedIdxMvaScore_E.push_back(make_pair(i, logistic));
205  }
206 
207  std::sort(pairSeedIdxMvaScore_B.begin(), pairSeedIdxMvaScore_B.end(), sortByMvaScorePhase2);
208  std::sort(pairSeedIdxMvaScore_E.begin(), pairSeedIdxMvaScore_E.end(), sortByMvaScorePhase2);
209 
210  for (auto i = 0U; i < pairSeedIdxMvaScore_B.size(); ++i) {
211  if ((int)i == nSeedsMax_B_)
212  break;
213  const auto& seed(h_Seed->at(pairSeedIdxMvaScore_B.at(i).first));
214  result->emplace_back(seed);
215  }
216 
217  for (auto i = 0U; i < pairSeedIdxMvaScore_E.size(); ++i) {
218  if ((int)i == nSeedsMax_E_)
219  break;
220  const auto& seed(h_Seed->at(pairSeedIdxMvaScore_E.at(i).first));
221  result->emplace_back(seed);
222  }
223  }
224 
225  // -- simple fitering based on Mva threshold
226  else {
227  for (auto i = 0U; i < h_Seed->size(); ++i) {
228  const auto& seed(h_Seed->at(i));
229 
230  GlobalVector global_p = trkGeom->idToDet(seed.startingState().detId())
231  ->surface()
232  .toGlobal(seed.startingState().parameters().momentum());
233  GlobalPoint global_x = trkGeom->idToDet(seed.startingState().detId())
234  ->surface()
235  .toGlobal(seed.startingState().parameters().position());
236 
237  bool isB = (std::abs(global_p.eta()) < etaEdge_);
238 
239  if (isB && mvaCut_B_ <= 0.) {
240  result->emplace_back(seed);
241  continue;
242  }
243 
244  if (!isB && mvaCut_E_ <= 0.) {
245  result->emplace_back(seed);
246  continue;
247  }
248 
249  double mva = getSeedMva(
250  mvaEstimator_, seed, global_p, global_x, h_L1TkMu, magfieldH, *(propagatorAlong.get()), geomTracker);
251 
252  double logistic = 1 / (1 + std::exp(-mva));
253 
254  bool passMva = ((isB && (logistic > mvaCut_B_)) || (!isB && (logistic > mvaCut_E_)));
255 
256  if (passMva)
257  result->emplace_back(seed);
258  }
259  }
260 
261  iEvent.put(std::move(result));
262 }
263 
265  const TrajectorySeed& seed,
266  const GlobalVector& global_p,
267  const GlobalPoint& global_x,
269  const edm::ESHandle<MagneticField>& magfieldH,
271  const GeometricSearchTracker& geomTracker) {
272  double mva = 0.;
273 
274  if (std::abs(global_p.eta()) < etaEdge_) {
275  mva =
276  pairMvaEstimator.first->computeMva(seed, global_p, global_x, h_L1TkMu, magfieldH, propagatorAlong, geomTracker);
277  } else {
278  mva = pairMvaEstimator.second->computeMva(
279  seed, global_p, global_x, h_L1TkMu, magfieldH, propagatorAlong, geomTracker);
280  }
281 
282  return (baseScore_ + mva);
283 }
284 
285 // ------------ method fills 'descriptions' with the allowed parameters for the module ------------
288  desc.add<edm::InputTag>("src", edm::InputTag("hltIter2Phase2L3FromL1TkMuonPixelSeeds", ""));
289  desc.add<edm::InputTag>("L1TkMu", edm::InputTag("L1TkMuons", "Muon"));
290  desc.add<edm::FileInPath>("mvaFile_B_0",
291  edm::FileInPath("RecoMuon/TrackerSeedGenerator/data/xgb_Phase2_Iter2FromL1_barrel_v0.xml"));
292  desc.add<edm::FileInPath>("mvaFile_E_0",
293  edm::FileInPath("RecoMuon/TrackerSeedGenerator/data/xgb_Phase2_Iter2FromL1_endcap_v0.xml"));
294  desc.add<std::vector<double>>("mvaScaleMean_B",
295  {0.00033113700731766336,
296  1.6825601468762878e-06,
297  1.790932122524803e-06,
298  0.010534608406382916,
299  0.005969459957330139,
300  0.0009605022254971113,
301  0.04384189672781466,
302  7.846741237608237e-05,
303  0.40725050850004824,
304  0.41125151617410227,
305  0.39815551065544846});
306  desc.add<std::vector<double>>("mvaScaleStd_B",
307  {0.0006042948363798624,
308  2.445644111872427e-06,
309  3.454992543447134e-06,
310  0.09401581628887255,
311  0.7978806947573766,
312  0.4932933044535928,
313  0.04180518265631776,
314  0.058296511682094855,
315  0.4071857009373577,
316  0.41337782307392973,
317  0.4101160349549534});
318  desc.add<std::vector<double>>("mvaScaleMean_E",
319  {0.00022658482374555603,
320  5.358921973784045e-07,
321  1.010003713549798e-06,
322  0.0007886873612224615,
323  0.001197730548842408,
324  -0.0030252353426003594,
325  0.07151944804171254,
326  -0.0006940626775109026,
327  0.20535152195939896,
328  0.2966816533783824,
329  0.28798220230180455});
330  desc.add<std::vector<double>>("mvaScaleStd_E",
331  {0.0003857726789049956,
332  1.4853721474087994e-06,
333  6.982997036736564e-06,
334  0.04071340757666084,
335  0.5897606560095399,
336  0.33052121398064654,
337  0.05589386786541949,
338  0.08806273533388546,
339  0.3254586902665612,
340  0.3293354496231377,
341  0.3179899794578072});
342  desc.add<bool>("doSort", true);
343  desc.add<int>("nSeedsMax_B", 20);
344  desc.add<int>("nSeedsMax_E", 20);
345  desc.add<double>("mvaCut_B", 0.);
346  desc.add<double>("mvaCut_E", 0.);
347  desc.add<double>("etaEdge", 1.2);
348  desc.add<double>("baseScore", 0.5);
349  descriptions.add("MuonHLTSeedMVAClassifierPhase2", desc);
350 }
351 //define this as a plug-in
ESGetTokenH3DDVariant esConsumes(std::string const &Record, edm::ConsumesCollector &)
Definition: DeDxTools.cc:283
Definition: APVGainStruct.h:7
bool sortByMvaScorePhase2(const std::pair< unsigned, double > &A, const std::pair< unsigned, double > &B)
const edm::EDGetTokenT< TrajectorySeedCollection > t_Seed_
double getSeedMva(const pairSeedMvaEstimator &pairMvaEstimator, const TrajectorySeed &seed, const GlobalVector &global_p, const GlobalPoint &global_x, const edm::Handle< l1t::TrackerMuonCollection > &h_L1TkMu, const edm::ESHandle< MagneticField > &magfieldH, const Propagator &propagatorAlong, const GeometricSearchTracker &geomTracker)
T eta() const
Definition: PV3DBase.h:73
delete x;
Definition: CaloConfig.h:22
Log< level::Error, false > LogError
const edm::ESGetToken< MagneticField, IdealMagneticFieldRecord > magFieldESToken_
std::unique_ptr< Propagator > SetPropagationDirection(Propagator const &iprop, PropagationDirection dir)
int iEvent
Definition: GenABIO.cc:224
std::vector< TrajectorySeed > TrajectorySeedCollection
Abs< T >::type abs(const T &t)
Definition: Abs.h:22
#define DEFINE_FWK_MODULE(type)
Definition: MakerMacros.h:16
GeometricSearchTracker * build(const GeometricDet *theGeometricTracker, const TrackerGeometry *theGeomDetGeometry, const TrackerTopology *tTopo, const bool usePhase2Stacks=false) __attribute__((cold))
static void fillDescriptions(edm::ConfigurationDescriptions &descriptions)
const edm::EDGetTokenT< l1t::TrackerMuonCollection > t_L1TkMu_
ESHandle< T > getHandle(const ESGetToken< T, R > &iToken) const
Definition: EventSetup.h:130
const edm::ESGetToken< Propagator, TrackingComponentsRecord > propagatorESToken_
const edm::ESGetToken< TrackerTopology, TrackerTopologyRcd > trackerTopologyESToken_
MuonHLTSeedMVAClassifierPhase2(const edm::ParameterSet &)
const edm::ESGetToken< GeometricDet, IdealGeometryRecord > geomDetESToken_
std::vector< TrackerMuon > TrackerMuonCollection
Definition: TrackerMuon.h:17
~MuonHLTSeedMVAClassifierPhase2() override=default
void add(std::string const &label, ParameterSetDescription const &psetDescription)
std::pair< std::unique_ptr< const SeedMvaEstimatorPhase2 >, std::unique_ptr< const SeedMvaEstimatorPhase2 > > pairSeedMvaEstimator
HLT enums.
void produce(edm::Event &, const edm::EventSetup &) override
const edm::ESGetToken< TrackerGeometry, TrackerDigiGeometryRecord > trackerGeometryESToken_
Definition: APVGainStruct.h:7
def move(src, dest)
Definition: eostools.py:511