9 #include <Math/DistFunc.h>
23 useVertices_(
cfg.getParameter<
bool>(
"useVertices")),
24 useVtxError_(
cfg.getParameter<
bool>(
"useVtxError"))
30 edm::LogWarning(
"MultiTRackSelector") <<
"you are executing buggy code, if intentional please help to fix it";
36 if (
cfg.exists(
"useAnyMVA"))
40 if (
cfg.exists(
"GBRForestFileName")) {
45 std::vector<edm::ParameterSet> trkSelectors(
cfg.getParameter<std::vector<edm::ParameterSet>>(
"trackSelectors"));
49 res_par_.reserve(trkSelectors.size());
52 d0_par1_.reserve(trkSelectors.size());
53 dz_par1_.reserve(trkSelectors.size());
54 d0_par2_.reserve(trkSelectors.size());
55 dz_par2_.reserve(trkSelectors.size());
57 max_d0_.reserve(trkSelectors.size());
58 max_z0_.reserve(trkSelectors.size());
59 nSigmaZ_.reserve(trkSelectors.size());
72 min_eta_.reserve(trkSelectors.size());
73 max_eta_.reserve(trkSelectors.size());
74 useMVA_.reserve(trkSelectors.size());
77 min_MVA_.reserve(trkSelectors.size());
78 mvaType_.reserve(trkSelectors.size());
80 forest_.reserve(trkSelectors.size());
82 produces<edm::ValueMap<float>>(
"MVAVals");
85 produces<MVACollection>(
"MVAValues");
87 for (
unsigned int i = 0;
i < trkSelectors.size();
i++) {
93 res_par_.push_back(trkSelectors[
i].getParameter<std::vector<double>>(
"res_par"));
94 chi2n_par_.push_back(trkSelectors[
i].getParameter<double>(
"chi2n_par"));
96 d0_par1_.push_back(trkSelectors[
i].getParameter<std::vector<double>>(
"d0_par1"));
97 dz_par1_.push_back(trkSelectors[
i].getParameter<std::vector<double>>(
"dz_par1"));
98 d0_par2_.push_back(trkSelectors[
i].getParameter<std::vector<double>>(
"d0_par2"));
99 dz_par2_.push_back(trkSelectors[
i].getParameter<std::vector<double>>(
"dz_par2"));
103 max_d0_.push_back(trkSelectors[
i].getParameter<double>(
"max_d0"));
104 max_z0_.push_back(trkSelectors[
i].getParameter<double>(
"max_z0"));
105 nSigmaZ_.push_back(trkSelectors[
i].getParameter<double>(
"nSigmaZ"));
107 min_layers_.push_back(trkSelectors[
i].getParameter<uint32_t>(
"minNumberLayers"));
108 min_3Dlayers_.push_back(trkSelectors[
i].getParameter<uint32_t>(
"minNumber3DLayers"));
109 max_lostLayers_.push_back(trkSelectors[
i].getParameter<uint32_t>(
"maxNumberLostLayers"));
110 min_hits_bypass_.push_back(trkSelectors[
i].getParameter<uint32_t>(
"minHitsToBypassChecks"));
113 keepAllTracks_.push_back(trkSelectors[
i].getParameter<bool>(
"keepAllTracks"));
114 max_relpterr_.push_back(trkSelectors[
i].getParameter<double>(
"max_relpterr"));
115 min_nhits_.push_back(trkSelectors[
i].getParameter<uint32_t>(
"min_nhits"));
117 ? trkSelectors[
i].getParameter<int32_t>(
"max_minMissHitOutOrIn")
120 ? trkSelectors[
i].getParameter<double>(
"max_lostHitFraction")
122 min_eta_.push_back(trkSelectors[
i].existsAs<double>(
"min_eta") ? trkSelectors[
i].getParameter<double>(
"min_eta")
124 max_eta_.push_back(trkSelectors[
i].existsAs<double>(
"max_eta") ? trkSelectors[
i].getParameter<double>(
"max_eta")
129 if (!qualityStr.empty()) {
131 qualityToSet_[
i] = TrackBase::qualityByName(trkSelectors[
i].getParameter<std::string>(
"qualityBit"));
136 <<
"You can't set the quality bit " << trkSelectors[
i].getParameter<
std::string>(
"qualityBit")
137 <<
" as it is 'undefQuality' or unknown.\n";
140 max_d0NoPV_.push_back(trkSelectors[
i].getParameter<double>(
"max_d0NoPV"));
141 max_z0NoPV_.push_back(trkSelectors[
i].getParameter<double>(
"max_z0NoPV"));
147 name_.push_back(trkSelectors[
i].getParameter<std::string>(
"name"));
152 if (!pfName.empty()) {
153 bool foundPF =
false;
154 for (
unsigned int j = 0;
j <
i;
j++)
160 throw cms::Exception(
"Configuration") <<
"Invalid prefilter name in MultiTrackSelector "
161 << trkSelectors[
i].getParameter<
std::string>(
"preFilterName");
165 produces<edm::ValueMap<int>>(
name_[
i]).setBranchAlias(
name_[
i] +
"TrackQuals");
166 produces<QualityMaskCollection>(
name_[
i]).setBranchAlias(
name_[
i] +
"QualityMasks");
168 bool thisMVA =
false;
169 if (trkSelectors[
i].exists(
"useMVA"))
170 thisMVA = trkSelectors[
i].getParameter<
bool>(
"useMVA");
174 if (trkSelectors[
i].exists(
"minMVA"))
175 minVal = trkSelectors[
i].getParameter<
double>(
"minMVA");
177 mvaType_.push_back(trkSelectors[
i].exists(
"mvaType") ? trkSelectors[
i].getParameter<std::string>(
"mvaType")
179 forestLabel_.push_back(trkSelectors[
i].exists(
"GBRForestLabel")
180 ? trkSelectors[
i].getParameter<std::string>(
"GBRForestLabel")
181 :
"MVASelectorIter0");
182 useMVAonly_.push_back(trkSelectors[
i].exists(
"useMVAonly") ? trkSelectors[
i].getParameter<bool>(
"useMVAonly")
217 using namespace reco;
225 edm::LogWarning(
"MultiTrackSelector") <<
"could not get Track collection";
242 edm::LogWarning(
"MultiTrackSelector") <<
"could not get Vertex collection";
246 std::vector<int> selTracksSave(
qualityToSet_.size() * trkSize, 0);
248 std::vector<Point>
points;
249 std::vector<float> vterr, vzerr;
254 std::vector<float> mvaVals_(
srcTracks.size(), -99.f);
255 processMVA(evt, es, vertexBeamSpot, *(hVtx.
product()),
i, mvaVals_,
i == 0 ?
true :
false);
256 std::vector<int> selTracks(trkSize, 0);
257 auto selTracksValueMap = std::make_unique<edm::ValueMap<int>>();
265 for (TrackCollection::const_iterator it =
srcTracks.begin(), ed =
srcTracks.end(); it != ed; ++it, ++current) {
266 const Track& trk = *it;
269 LogTrace(
"TrackSelection") <<
"ready to check track with pt=" << trk.
pt();
274 selTracks[current] = -1;
281 mvaVal = mvaVals_[current];
282 ok =
select(
i, vertexBeamSpot, srcHits, trk,
points, vterr, vzerr, mvaVal);
284 LogTrace(
"TrackSelection") <<
"track with pt=" << trk.
pt() <<
" NOT selected";
286 selTracks[current] = -1;
290 LogTrace(
"TrackSelection") <<
"track with pt=" << trk.
pt() <<
" selected";
294 selTracks[current] = selTracksSave[
preFilter_[
i] * trkSize + current];
299 selTracks[current] = (selTracks[current] | (1 <<
qualityToSet_[
i]));
301 selTracks[current] = (selTracks[current] | (1 << TrackBase::loose));
303 selTracks[current] = (selTracks[current] | (1 << TrackBase::loose));
304 selTracks[current] = (selTracks[current] | (1 << TrackBase::tight));
308 selTracks[current] = (selTracks[current] | (1 << TrackBase::looseSetWithPV));
310 selTracks[current] = (selTracks[current] | (1 << TrackBase::looseSetWithPV));
311 selTracks[current] = (selTracks[current] | (1 << TrackBase::highPuritySetWithPV));
316 for (
unsigned int j = 0;
j < trkSize;
j++)
317 selTracksSave[
j +
i * trkSize] = selTracks[
j];
318 filler.insert(hSrcTrack, selTracks.begin(), selTracks.end());
323 for (
auto&
q : selTracks)
325 auto quals = std::make_unique<QualityMaskCollection>(selTracks.begin(), selTracks.end());
334 const std::vector<Point>&
points,
335 std::vector<float>& vterr,
336 std::vector<float>& vzerr,
337 double mvaVal)
const {
349 if (tk.
ndof() < 1E-5)
370 LogDebug(
"TrackSelection") <<
"cuts on nlayers: " <<
nlayers <<
" " << nlayers3D <<
" " << nlayersLost <<
" vs "
378 LogTrace(
"TrackSelection") <<
"cuts on nlayers passed";
381 float chi2n_no1Dmod =
chi2n;
384 auto ith = tk.
extra()->firstRecHit();
386 for (; ith < edh; ++ith) {
388 if (
hit.dimension() == 1)
391 if (count1dhits > 0) {
417 int minLost =
std::min(lostIn, lostOut);
431 float nomdzE = nomd0E * (std::cosh(
eta));
439 bool primaryVertexZCompatibility(
false);
440 bool primaryVertexD0Compatibility(
false);
445 primaryVertexZCompatibility =
true;
448 primaryVertexD0Compatibility =
true;
453 LogTrace(
"TrackSelection") <<
"Test track w.r.t. vertex with z position " <<
point->z();
454 if (primaryVertexZCompatibility && primaryVertexD0Compatibility)
459 float dzErrPV =
std::sqrt(dzE * dzE + vzerr[iv] * vzerr[iv]);
460 float d0ErrPV =
std::sqrt(d0E * d0E + vterr[iv] * vterr[iv]);
464 primaryVertexZCompatibility =
true;
467 primaryVertexD0Compatibility =
true;
470 primaryVertexZCompatibility =
true;
472 primaryVertexD0Compatibility =
true;
474 LogTrace(
"TrackSelection") <<
"distances " << dzPV <<
" " << d0PV <<
" vs " <<
dzCut <<
" " <<
d0Cut;
483 if (
abs(
d0) >
max_d0_[tsNum] && !primaryVertexD0Compatibility)
485 LogTrace(
"TrackSelection") <<
"absolute cuts on d0 passed";
486 if (
abs(
dz) >
max_z0_[tsNum] && !primaryVertexZCompatibility)
488 LogTrace(
"TrackSelection") <<
"absolute cuts on dz passed";
492 <<
" d0 compatibility? " << primaryVertexD0Compatibility <<
" z compatibility? "
493 << primaryVertexZCompatibility;
496 return (primaryVertexD0Compatibility && primaryVertexZCompatibility);
504 std::vector<Point>&
points,
505 std::vector<float>& vterr,
506 std::vector<float>& vzerr)
const {
508 using namespace reco;
510 for (VertexCollection::const_iterator it = vtxs.begin(), ed = vtxs.end(); it != ed; ++it) {
511 LogDebug(
"SelectVertex") <<
" select vertex with z position " << it->z() <<
" " << it->chi2() <<
" " << it->ndof()
512 <<
" " << TMath::Prob(it->chi2(), static_cast<int32_t>(it->ndof()));
516 points.push_back(it->position());
517 vterr.push_back(
sqrt(it->yError() * it->xError()));
518 vzerr.push_back(it->zError());
519 LogTrace(
"SelectVertex") <<
" SELECTED vertex with z position " << it->z();
532 std::vector<float>& mvaVals_,
533 bool writeIt)
const {
536 using namespace reco;
550 auto mvaValValueMap = std::make_unique<edm::ValueMap<float>>();
555 mvaFiller.insert(hSrcTrack, mvaVals_.begin(), mvaVals_.end());
558 auto mvas = std::make_unique<MVACollection>(mvaVals_.begin(), mvaVals_.end());
563 if (!
useMVA_[selIndex] && !writeIt)
567 for (TrackCollection::const_iterator it =
srcTracks.begin(), ed =
srcTracks.end(); it != ed; ++it, ++current) {
568 const Track& trk = *it;
570 auto tmva_ndof_ = trk.
ndof();
572 auto tmva_nlayers3D_ =
576 float chi2n_no1Dmod =
chi2n;
579 auto ith = trk.
extra()->firstRecHit();
581 for (; ith < edh; ++ith) {
583 if (
hit.dimension() == 1)
586 if (count1dhits > 0) {
591 auto tmva_chi2n_ =
chi2n;
592 auto tmva_chi2n_no1dmod_ = chi2n_no1Dmod;
593 auto tmva_eta_ = trk.
eta();
598 auto tmva_minlost_ =
std::min(lostIn, lostOut);
600 auto tmva_absd0_ = fabs(-trk.
dxy(
beamspot.position()));
601 auto tmva_absdz_ = fabs(trk.
dz(
beamspot.position()));
603 auto tmva_absd0PV_ = fabs(trk.
dxy(bestVertex));
604 auto tmva_absdzPV_ = fabs(trk.
dz(bestVertex));
605 auto tmva_pt_ = trk.
pt();
611 forest = forestHandle.
product();
615 gbrVals_[0] = tmva_pt_;
616 gbrVals_[1] = tmva_lostmidfrac_;
617 gbrVals_[2] = tmva_minlost_;
618 gbrVals_[3] = tmva_nhits_;
619 gbrVals_[4] = tmva_relpterr_;
620 gbrVals_[5] = tmva_eta_;
621 gbrVals_[6] = tmva_chi2n_no1dmod_;
622 gbrVals_[7] = tmva_chi2n_;
623 gbrVals_[8] = tmva_nlayerslost_;
624 gbrVals_[9] = tmva_nlayers3D_;
625 gbrVals_[10] = tmva_nlayers_;
626 gbrVals_[11] = tmva_ndof_;
627 gbrVals_[12] = tmva_absd0PV_;
628 gbrVals_[13] = tmva_absdzPV_;
629 gbrVals_[14] = tmva_absdz_;
630 gbrVals_[15] = tmva_absd0_;
632 if (
mvaType_[selIndex] ==
"Prompt") {
634 mvaVals_[current] = gbrVal;
636 float detachedGbrVals_[12];
637 for (
int jjj = 0; jjj < 12; jjj++)
638 detachedGbrVals_[jjj] = gbrVals_[jjj];
640 mvaVals_[current] = gbrVal;
645 mvaFiller.insert(hSrcTrack, mvaVals_.begin(), mvaVals_.end());
648 auto mvas = std::make_unique<MVACollection>(mvaVals_.begin(), mvaVals_.end());
655 Point p_dz(0, 0, -99999);
656 float bestWeight = 0;
658 bool weightMatch =
false;
662 if (
w > bestWeight) {
667 float dz = fabs(
track.get()->dz(v_pos));