CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
MuonHLTSeedMVAClassifier.cc
Go to the documentation of this file.
1 
2 // Package: RecoMuon_TrackerSeedGenerator
3 // Class: MuonHLTSeedMVAClassifier
4 
5 // Original Author: Won Jun, OH Minseok
6 // Created: Fri, 28 May 2021
7 
8 // system include files
9 #include <memory>
10 #include <cmath>
11 
12 // user include files
15 
18 
21 
22 // Geometry
25 
26 // TrajectorySeed
33 
35 
36 // class declaration
37 bool sortByMvaScore(const std::pair<unsigned, double>& A, const std::pair<unsigned, double>& B) {
38  return (A.second > B.second);
39 };
40 
42 public:
44  ~MuonHLTSeedMVAClassifier() override = default;
45 
46  static void fillDescriptions(edm::ConfigurationDescriptions& descriptions);
47 
48 private:
49  void produce(edm::Event&, const edm::EventSetup&) override;
50 
51  // member data
56 
57  typedef std::pair<std::unique_ptr<const SeedMvaEstimator>, std::unique_ptr<const SeedMvaEstimator>>
60 
61  const bool rejectAll_;
62  const bool isFromL1_;
63 
66 
67  const std::vector<double> mvaScaleMeanB_;
68  const std::vector<double> mvaScaleStdB_;
69  const std::vector<double> mvaScaleMeanE_;
70  const std::vector<double> mvaScaleStdE_;
71 
72  const bool doSort_;
73  const int nSeedsMaxB_;
74  const int nSeedsMaxE_;
75 
76  const double etaEdge_;
77  const double mvaCutB_;
78  const double mvaCutE_;
79 
80  const int minL1Qual_;
81  const double baseScore_;
82 
83  double getSeedMva(const PairSeedMvaEstimator& pairMvaEstimator,
84  const TrajectorySeed& seed,
85  const GlobalVector& global_p,
86  const l1t::MuonBxCollection& l1Muons,
88 };
89 
91  : seedToken_(consumes<TrajectorySeedCollection>(iConfig.getParameter<edm::InputTag>("src"))),
92  l1MuonToken_(consumes<l1t::MuonBxCollection>(iConfig.getParameter<edm::InputTag>("L1Muon"))),
93  l2MuonToken_(consumes<reco::RecoChargedCandidateCollection>(iConfig.getParameter<edm::InputTag>("L2Muon"))),
95 
96  rejectAll_(iConfig.getParameter<bool>("rejectAll")),
97  isFromL1_(iConfig.getParameter<bool>("isFromL1")),
98 
99  mvaFileB_(iConfig.getParameter<edm::FileInPath>(isFromL1_ ? "mvaFileBL1" : "mvaFileBL2")),
100  mvaFileE_(iConfig.getParameter<edm::FileInPath>(isFromL1_ ? "mvaFileEL1" : "mvaFileEL2")),
101 
102  mvaScaleMeanB_(iConfig.getParameter<std::vector<double>>(isFromL1_ ? "mvaScaleMeanBL1" : "mvaScaleMeanBL2")),
103  mvaScaleStdB_(iConfig.getParameter<std::vector<double>>(isFromL1_ ? "mvaScaleStdBL1" : "mvaScaleStdBL2")),
104  mvaScaleMeanE_(iConfig.getParameter<std::vector<double>>(isFromL1_ ? "mvaScaleMeanEL1" : "mvaScaleMeanEL2")),
105  mvaScaleStdE_(iConfig.getParameter<std::vector<double>>(isFromL1_ ? "mvaScaleStdEL1" : "mvaScaleStdEL2")),
106 
107  doSort_(iConfig.getParameter<bool>("doSort")),
108  nSeedsMaxB_(iConfig.getParameter<int>("nSeedsMaxB")),
109  nSeedsMaxE_(iConfig.getParameter<int>("nSeedsMaxE")),
110 
111  etaEdge_(iConfig.getParameter<double>("etaEdge")),
112  mvaCutB_(iConfig.getParameter<double>("mvaCutB")),
113  mvaCutE_(iConfig.getParameter<double>("mvaCutE")),
114 
115  minL1Qual_(iConfig.getParameter<int>("minL1Qual")),
116  baseScore_(iConfig.getParameter<double>("baseScore")) {
117  if (!rejectAll_) {
118  mvaEstimator_ = std::make_pair(
119  std::make_unique<SeedMvaEstimator>(mvaFileB_, mvaScaleMeanB_, mvaScaleStdB_, isFromL1_, minL1Qual_),
120  std::make_unique<SeedMvaEstimator>(mvaFileE_, mvaScaleMeanE_, mvaScaleStdE_, isFromL1_, minL1Qual_));
121  }
122 
123  produces<TrajectorySeedCollection>();
124 }
125 
126 // -- method called on each new Event
128  auto result = std::make_unique<TrajectorySeedCollection>();
129 
130  if (rejectAll_) {
131  iEvent.put(std::move(result));
132  return;
133  }
134 
135  if (doSort_ && nSeedsMaxB_ <= 0 && nSeedsMaxE_ <= 0) {
136  iEvent.put(std::move(result));
137  return;
138  }
139 
140  if (!doSort_ && mvaCutB_ > 1. && mvaCutE_ > 1.) {
141  iEvent.put(std::move(result));
142  return;
143  }
144 
145  const TrajectorySeedCollection& seeds = iEvent.get(seedToken_);
146  const l1t::MuonBxCollection& l1Muons = iEvent.get(l1MuonToken_);
147  const reco::RecoChargedCandidateCollection& l2Muons = iEvent.get(l2MuonToken_);
148  const TrackerGeometry& trkGeom = iEventSetup.getData(trackerGeometryToken_);
149 
150  std::vector<std::pair<unsigned, double>> pairSeedIdxMvaScoreB = {};
151  std::vector<std::pair<unsigned, double>> pairSeedIdxMvaScoreE = {};
152  for (auto& seed : seeds) {
153  const GlobalVector global_p =
154  trkGeom.idToDet(seed.startingState().detId())->surface().toGlobal(seed.startingState().parameters().momentum());
155 
156  bool isB = (std::abs(global_p.eta()) < etaEdge_);
157 
158  if (doSort_) {
159  if (isB) {
160  if (nSeedsMaxB_ <= 0) {
161  continue;
162  }
163  } else {
164  if (nSeedsMaxE_ <= 0) {
165  continue;
166  }
167  }
168  } else {
169  if (isB) {
170  if (mvaCutB_ > 1.0) {
171  continue;
172  } else if (mvaCutB_ <= 0.) {
173  result->emplace_back(seed);
174  continue;
175  }
176  } else {
177  if (mvaCutE_ > 1.0) {
178  continue;
179  } else if (mvaCutE_ <= 0.) {
180  result->emplace_back(seed);
181  continue;
182  }
183  }
184  }
185 
186  double mva = getSeedMva(mvaEstimator_, seed, global_p, l1Muons, l2Muons);
187 
188  double score = 1. / (1. + std::exp(-1. * mva));
189  bool passMva = isB ? score > mvaCutB_ : score > mvaCutE_;
190  if (!passMva)
191  continue;
192 
193  if (doSort_) {
194  if (isB)
195  pairSeedIdxMvaScoreB.push_back(std::make_pair(&seed - &seeds.at(0), score));
196  else
197  pairSeedIdxMvaScoreE.push_back(std::make_pair(&seed - &seeds.at(0), score));
198  } else {
199  result->emplace_back(seed);
200  }
201  }
202 
203  if (doSort_) {
204  std::sort(pairSeedIdxMvaScoreB.begin(), pairSeedIdxMvaScoreB.end(), sortByMvaScore);
205  std::sort(pairSeedIdxMvaScoreE.begin(), pairSeedIdxMvaScoreE.end(), sortByMvaScore);
206 
207  for (auto i = 0U; i < pairSeedIdxMvaScoreB.size(); ++i) {
208  if ((int)i == nSeedsMaxB_)
209  break;
210  const auto& seed(seeds.at(pairSeedIdxMvaScoreB.at(i).first));
211  result->emplace_back(seed);
212  }
213 
214  for (auto i = 0U; i < pairSeedIdxMvaScoreE.size(); ++i) {
215  if ((int)i == nSeedsMaxE_)
216  break;
217  const auto& seed(seeds.at(pairSeedIdxMvaScoreE.at(i).first));
218  result->emplace_back(seed);
219  }
220  }
221 
222  iEvent.put(std::move(result));
223 }
224 
226  const TrajectorySeed& seed,
227  const GlobalVector& global_p,
228  const l1t::MuonBxCollection& l1Muons,
229  const reco::RecoChargedCandidateCollection& l2Muons) {
230  double mva = 0.;
231  if (std::abs(global_p.eta()) < etaEdge_) {
232  mva = pairMvaEstimator.first->computeMva(seed, global_p, l1Muons, l2Muons);
233  } else {
234  mva = pairMvaEstimator.second->computeMva(seed, global_p, l1Muons, l2Muons);
235  }
236 
237  return (mva + baseScore_);
238 }
239 
240 // -- method fills 'descriptions' with the allowed parameters for the module ------------
243  desc.add<edm::InputTag>("src", edm::InputTag("hltIter2IterL3MuonPixelSeeds", ""));
244  desc.add<edm::InputTag>("L1Muon", edm::InputTag("hltGtStage2Digis", "Muon"));
245  desc.add<edm::InputTag>("L2Muon", edm::InputTag("hltL2MuonCandidates", ""));
246 
247  desc.add<bool>("rejectAll", false);
248  desc.add<bool>("isFromL1", false);
249 
250  desc.add<edm::FileInPath>("mvaFileBL1",
251  edm::FileInPath("RecoMuon/TrackerSeedGenerator/data/xgb_Run3_Iter2FromL1Seeds_barrel.xml"));
252  desc.add<edm::FileInPath>("mvaFileEL1",
253  edm::FileInPath("RecoMuon/TrackerSeedGenerator/data/xgb_Run3_Iter2FromL1Seeds_endcap.xml"));
254  desc.add<edm::FileInPath>("mvaFileBL2",
255  edm::FileInPath("RecoMuon/TrackerSeedGenerator/data/xgb_Run3_Iter2Seeds_barrel.xml"));
256  desc.add<edm::FileInPath>("mvaFileEL2",
257  edm::FileInPath("RecoMuon/TrackerSeedGenerator/data/xgb_Run3_Iter2Seeds_endcap.xml"));
258  desc.add<std::vector<double>>("mvaScaleMeanBL1", {0., 0., 0., 0., 0., 0., 0., 0.});
259  desc.add<std::vector<double>>("mvaScaleStdBL1", {1., 1., 1., 1., 1., 1., 1., 1.});
260  desc.add<std::vector<double>>("mvaScaleMeanEL1", {0., 0., 0., 0., 0., 0., 0., 0.});
261  desc.add<std::vector<double>>("mvaScaleStdEL1", {1., 1., 1., 1., 1., 1., 1., 1.});
262  desc.add<std::vector<double>>("mvaScaleMeanBL2", {0., 0., 0., 0., 0., 0., 0., 0., 0., 0.});
263  desc.add<std::vector<double>>("mvaScaleStdBL2", {1., 1., 1., 1., 1., 1., 1., 1., 1., 1.});
264  desc.add<std::vector<double>>("mvaScaleMeanEL2", {0., 0., 0., 0., 0., 0., 0., 0., 0., 0.});
265  desc.add<std::vector<double>>("mvaScaleStdEL2", {1., 1., 1., 1., 1., 1., 1., 1., 1., 1.});
266 
267  desc.add<bool>("doSort", false);
268  desc.add<int>("nSeedsMaxB", 1e6);
269  desc.add<int>("nSeedsMaxE", 1e6);
270 
271  desc.add<double>("etaEdge", 1.2);
272  desc.add<double>("mvaCutB", -1.);
273  desc.add<double>("mvaCutE", -1.);
274 
275  desc.add<int>("minL1Qual", 7);
276  desc.add<double>("baseScore", 0.5);
277 
278  descriptions.add("MuonHLTSeedMVAClassifier", desc);
279 }
280 
281 //define this as a plug-in
OrphanHandle< PROD > put(std::unique_ptr< PROD > product)
Put a new product.
Definition: Event.h:133
Definition: APVGainStruct.h:7
#define DEFINE_FWK_MODULE(type)
Definition: MakerMacros.h:16
GlobalPoint toGlobal(const Local2DPoint &lp) const
Conversion to the global R.F. from the R.F. of the GeomDet.
Definition: GeomDet.h:49
void produce(edm::Event &, const edm::EventSetup &) override
const std::vector< double > mvaScaleStdB_
MuonHLTSeedMVAClassifier(const edm::ParameterSet &)
const std::vector< double > mvaScaleMeanB_
Exp< T >::type exp(const T &t)
Definition: Exp.h:22
double getSeedMva(const PairSeedMvaEstimator &pairMvaEstimator, const TrajectorySeed &seed, const GlobalVector &global_p, const l1t::MuonBxCollection &l1Muons, const reco::RecoChargedCandidateCollection &l2Muons)
tuple result
Definition: mps_fire.py:311
const edm::ESGetToken< TrackerGeometry, TrackerDigiGeometryRecord > trackerGeometryToken_
bool getData(T &iHolder) const
Definition: EventSetup.h:122
int iEvent
Definition: GenABIO.cc:224
const edm::EDGetTokenT< l1t::MuonBxCollection > l1MuonToken_
std::vector< TrajectorySeed > TrajectorySeedCollection
def move
Definition: eostools.py:511
Abs< T >::type abs(const T &t)
Definition: Abs.h:22
bool get(ProductID const &oid, Handle< PROD > &result) const
Definition: Event.h:346
~MuonHLTSeedMVAClassifier() override=default
ParameterDescriptionBase * add(U const &iLabel, T const &value)
static void fillDescriptions(edm::ConfigurationDescriptions &descriptions)
const TrackerGeomDet * idToDet(DetId) const override
BXVector< Muon > MuonBxCollection
Definition: Muon.h:11
std::vector< RecoChargedCandidate > RecoChargedCandidateCollection
collectin of RecoChargedCandidate objects
PairSeedMvaEstimator mvaEstimator_
void add(std::string const &label, ParameterSetDescription const &psetDescription)
std::pair< std::unique_ptr< const SeedMvaEstimator >, std::unique_ptr< const SeedMvaEstimator > > PairSeedMvaEstimator
T eta() const
Definition: PV3DBase.h:73
bool sortByMvaScore(const std::pair< unsigned, double > &A, const std::pair< unsigned, double > &B)
trackerGeometryToken_(iC.esConsumes())
Definition: APVGainStruct.h:7
const std::vector< double > mvaScaleMeanE_
ESGetTokenH3DDVariant esConsumes(std::string const &Reccord, edm::ConsumesCollector &)
Definition: DeDxTools.cc:283
const std::vector< double > mvaScaleStdE_
const edm::EDGetTokenT< TrajectorySeedCollection > seedToken_
const edm::EDGetTokenT< reco::RecoChargedCandidateCollection > l2MuonToken_