11 #include "TMatrixDSym.h"
12 #include "TMatrixDSymEigen.h"
22 : cutBased_(ps.getParameter<
bool>(
"cutBased")),
23 etaBinnedWeights_(
false),
26 label_(ps.getParameter<
std::
string>(
"label")),
30 std::vector<edm::FileInPath> tmvaEtaWeights;
35 etaBinnedWeights_ = ps.getParameter<
bool>(
"etaBinnedWeights");
36 if (etaBinnedWeights_) {
37 const std::vector<edm::ParameterSet>&
trainings = ps.getParameter<std::vector<edm::ParameterSet> >(
"trainings");
38 nEtaBins_ = ps.getParameter<
int>(
"nEtaBins");
39 for (
int v = 0;
v < nEtaBins_;
v++) {
41 jEtaMin_.push_back(
trainings.at(
v).getParameter<
double>(
"jEtaMin"));
42 jEtaMax_.push_back(
trainings.at(
v).getParameter<
double>(
"jEtaMax"));
44 for (
int v = 0;
v < nEtaBins_;
v++) {
45 tmvaEtaVariables_.push_back(
trainings.at(
v).getParameter<std::vector<std::string> >(
"tmvaVariables"));
48 tmvaVariables_ = ps.getParameter<std::vector<std::string> >(
"tmvaVariables");
50 tmvaMethod_ = ps.getParameter<
std::string>(
"tmvaMethod");
51 tmvaSpectators = ps.getParameter<std::vector<std::string> >(
"tmvaSpectators");
52 version = ps.getParameter<
int>(
"version");
58 for (
int i0 = 0; i0 < 3; i0++) {
67 for (
int i1 = 0;
i1 < nCut;
i1++) {
69 if (cutBased_ &&
i1 == 0)
70 lFullCutType =
"BetaStar" + lCutType;
71 if (cutBased_ &&
i1 == 1)
72 lFullCutType =
"RMS" + lCutType;
73 std::vector<double> pt010 = jetConfig.
getParameter<std::vector<double> >((
"Pt010_" + lFullCutType).c_str());
74 std::vector<double> pt1020 = jetConfig.
getParameter<std::vector<double> >((
"Pt1020_" + lFullCutType).c_str());
75 std::vector<double> pt2030 = jetConfig.
getParameter<std::vector<double> >((
"Pt2030_" + lFullCutType).c_str());
76 std::vector<double> pt3050 = jetConfig.
getParameter<std::vector<double> >((
"Pt3050_" + lFullCutType).c_str());
78 for (
int i2 = 0;
i2 < 4;
i2++)
79 mvacut_[i0][0][
i2] = pt010[
i2];
80 for (
int i2 = 0;
i2 < 4;
i2++)
81 mvacut_[i0][1][
i2] = pt1020[
i2];
82 for (
int i2 = 0;
i2 < 4;
i2++)
83 mvacut_[i0][2][
i2] = pt2030[
i2];
84 for (
int i2 = 0;
i2 < 4;
i2++)
85 mvacut_[i0][3][
i2] = pt3050[
i2];
87 if (cutBased_ &&
i1 == 0) {
88 for (
int i2 = 0;
i2 < 4;
i2++)
89 betaStarCut_[i0][0][
i2] = pt010[
i2];
90 for (
int i2 = 0;
i2 < 4;
i2++)
91 betaStarCut_[i0][1][
i2] = pt1020[
i2];
92 for (
int i2 = 0;
i2 < 4;
i2++)
93 betaStarCut_[i0][2][
i2] = pt2030[
i2];
94 for (
int i2 = 0;
i2 < 4;
i2++)
95 betaStarCut_[i0][3][
i2] = pt3050[
i2];
97 if (cutBased_ &&
i1 == 1) {
98 for (
int i2 = 0;
i2 < 4;
i2++)
99 rmsCut_[i0][0][
i2] = pt010[
i2];
100 for (
int i2 = 0;
i2 < 4;
i2++)
101 rmsCut_[i0][1][
i2] = pt1020[
i2];
102 for (
int i2 = 0;
i2 < 4;
i2++)
103 rmsCut_[i0][2][
i2] = pt2030[
i2];
104 for (
int i2 = 0;
i2 < 4;
i2++)
105 rmsCut_[i0][3][
i2] = pt3050[
i2];
111 assert(tmvaMethod_.empty() || ((!tmvaVariables_.empty() || (!tmvaEtaVariables_.empty())) &&
version ==
USER));
114 if ((!cutBased_) && (runMvas_)) {
115 if (etaBinnedWeights_) {
116 for (
int v = 0;
v < nEtaBins_;
v++) {
131 void assign(
const std::vector<float>& vec,
float&
a,
float&
b,
float&
c,
float&
d) {
132 size_t sz = vec.size();
133 a = (sz > 0 ? vec[0] : 0.);
134 b = (sz > 1 ? vec[1] : 0.);
135 c = (sz > 2 ? vec[2] : 0.);
136 d = (sz > 3 ? vec[3] : 0.);
151 const std::unique_ptr<const GBRForest>&
reader) {
152 std::vector<float>
vars;
153 for (std::vector<std::string>::const_iterator it = varList.begin(); it != varList.end(); ++it) {
206 return std::pair<int, int>(ptId, etaId);
211 float betaStarModified = betaStarClassic /
log(nvtx - 0.64);
261 const pat::Jet* patjet = dynamic_cast<const pat::Jet*>(
jet);
263 assert(patjet !=
nullptr || pfjet !=
nullptr);
264 if (patjet !=
nullptr &&
jec == 0.) {
271 const reco::Candidate *lLead =
nullptr, *lSecond =
nullptr, *lLeadNeut =
nullptr, *lLeadEm =
nullptr,
272 *lLeadCh =
nullptr, *lTrail =
nullptr;
273 std::vector<float>
frac, fracCh, fracEm, fracNeut;
274 float cones[] = {0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7};
275 size_t ncones =
sizeof(cones) /
sizeof(
float);
304 TMatrixDSym covMatrix(2);
307 float sumPt = 0., sumPt2 = 0., sumTkPt = 0., sumPtCh = 0, sumPtNe = 0;
308 float multNeut = 0.0;
317 for (
unsigned i = 0;
i <
jet->numberOfSourceCandidatePtrs(); ++
i) {
323 bool isPacked =
true;
324 if (lPack ==
nullptr) {
327 float candPuppiWeight = 1.0;
330 float candPt = (icand->
pt()) * candPuppiWeight;
331 float candPtFrac = candPt /
jetPt;
333 float candDeta = icand->
eta() -
jet->eta();
335 float candPtDr = candPt * candDr;
336 size_t icone =
std::lower_bound(&cones[0], &cones[ncones], candDr) - &cones[0];
342 if (lLead ==
nullptr || candPt > lLead->
pt()) {
345 }
else if ((lSecond ==
nullptr || candPt > lSecond->pt()) && (candPt < lLead->
pt())) {
352 covMatrix(0, 0) += candPt * candPt * candDeta * candDeta;
353 covMatrix(0, 1) += candPt * candPt * candDeta * candDphi;
354 covMatrix(1, 1) += candPt * candPt * candDphi * candDphi;
357 sumPt2 += candPt * candPt;
360 frac.push_back(candPtFrac);
362 if (icone < ncones) {
363 *coneFracs[icone] += candPt;
368 if (lLeadNeut ==
nullptr || candPt > lLeadNeut->pt()) {
372 fracNeut.push_back(candPtFrac);
373 if (icone < ncones) {
374 *coneNeutFracs[icone] += candPt;
378 multNeut += candPuppiWeight;
381 if (icand->
pdgId() == 22) {
382 if (lLeadEm ==
nullptr || candPt > lLeadEm->pt()) {
386 fracEm.push_back(candPtFrac);
387 if (icone < ncones) {
388 *coneEmFracs[icone] += candPt;
392 multNeut += candPuppiWeight;
395 multNeut += candPuppiWeight;
398 if (icand->
charge() != 0) {
399 if (lLeadCh ==
nullptr || candPt > lLeadCh->pt()) {
403 if (lPF &&
std::abs(icand->
pdgId()) == 13 && pfTrk ==
nullptr) {
409 <<
"Found a PFCandidate muon without a trackRef: falling back to Muon::bestTrack ";
412 if (pfTrk ==
nullptr) {
417 }
else if (lPF !=
nullptr) {
429 fracCh.push_back(candPtFrac);
430 if (icone < ncones) {
431 *coneChFracs[icone] += candPt;
436 if (icand->
charge() != 0) {
442 bool inVtx0 =
vtx->trackWeight(lPF->
trackRef()) > 0;
444 bool inAnyOther =
false;
448 for (reco::VertexCollection::const_iterator vi = allvtx.begin(); vi != allvtx.end(); ++vi) {
454 bool isVtx0 = (iv.
position() -
vtx->position()).
r() < 0.02;
457 if (!isVtx0 && !inAnyOther) {
458 inAnyOther =
vtx->trackWeight(lPF->
trackRef()) <= 0;
464 if (inVtx0 && !inAnyOther) {
466 }
else if (!inVtx0 && inAnyOther) {
472 }
else if (dZ < 0.2) {
480 bool inVtxOther =
false;
482 double dZ_tmp = 9999.;
483 for (
unsigned vtx_i = 0; vtx_i < allvtx.size(); vtx_i++) {
484 auto iv = allvtx[vtx_i];
490 bool isVtx0 = (iv.position() -
vtx->position()).
r() < 0.02;
495 if (lPack->
fromPV(vtx_i) == 0)
497 dZ0 = lPack->
dz(iv.position());
500 if (fabs(lPack->
dz(iv.position())) < fabs(dZ_tmp)) {
501 dZ_tmp = lPack->
dz(iv.position());
506 }
else if (inVtxOther) {
509 if (fabs(dZ0) < 0.2) {
511 }
else if (fabs(dZ_tmp) < 0.2) {
518 if (lTrail ==
nullptr || candPt < lTrail->
pt()) {
524 assert(!(lLead ==
nullptr));
526 if (lSecond ==
nullptr) {
529 if (lLeadNeut ==
nullptr) {
532 if (lLeadEm ==
nullptr) {
535 if (lLeadCh ==
nullptr) {
539 if (patjet !=
nullptr) {
560 float sum_deta(0.0), sum_dphi(0.0);
561 float ave_deta(0.0), ave_dphi(0.0);
562 for (
size_t j = 0;
j <
jet->numberOfDaughters();
j++) {
563 const auto&
part =
jet->daughterPtr(
j);
564 if (!(
part.isAvailable() &&
part.isNonnull())) {
568 float partPuppiWeight = 1.0;
571 if (partpack !=
nullptr)
575 float weight = (
part->pt()) * partPuppiWeight;
578 float deta =
part->eta() -
jet->eta();
580 sum_deta += deta * weight2;
581 sum_dphi += dphi * weight2;
583 ave_deta = sum_deta / sumW2;
584 ave_dphi = sum_dphi / sumW2;
588 float ddetaR_sum(0.0), ddphiR_sum(0.0), pull_tmp(0.0);
589 for (
size_t i = 0;
i <
jet->numberOfDaughters();
i++) {
590 const auto&
part =
jet->daughterPtr(
i);
591 if (!(
part.isAvailable() &&
part.isNonnull())) {
595 float partPuppiWeight = 1.0;
598 if (partpack !=
nullptr)
602 float weight = partPuppiWeight * (
part->pt()) * partPuppiWeight * (
part->pt());
603 float deta =
part->eta() -
jet->eta();
605 float ddeta, ddphi, ddR;
606 ddeta = deta - ave_deta;
607 ddphi = dphi - ave_dphi;
608 ddR =
sqrt(ddeta * ddeta + ddphi * ddphi);
609 ddetaR_sum += ddR * ddeta *
weight;
610 ddphiR_sum += ddR * ddphi *
weight;
613 float ddetaR_ave = ddetaR_sum / sumW2;
614 float ddphiR_ave = ddphiR_sum / sumW2;
615 pull_tmp =
sqrt(ddetaR_ave * ddetaR_ave + ddphiR_ave * ddphiR_ave);
626 std::sort(
frac.begin(),
frac.end(), std::greater<float>());
627 std::sort(fracCh.begin(), fracCh.end(), std::greater<float>());
628 std::sort(fracEm.begin(), fracEm.end(), std::greater<float>());
629 std::sort(fracNeut.begin(), fracNeut.end(), std::greater<float>());
641 covMatrix(0, 0) /= sumPt2;
642 covMatrix(0, 1) /= sumPt2;
643 covMatrix(1, 1) /= sumPt2;
644 covMatrix(1, 0) = covMatrix(0, 1);
649 eigVals = TMatrixDSymEigen(covMatrix).GetEigenValues();
657 if (lSecond ==
nullptr) {
666 for (
size_t ic = 0; ic < ncones; ++ic) {
667 *coneFracs[ic] /=
jetPt;
668 *coneEmFracs[ic] /=
jetPt;
669 *coneNeutFracs[ic] /=
jetPt;
670 *coneChFracs[ic] /=
jetPt;
675 for (
unsigned int i0 = 0; i0 <
frac.size(); i0++) {
676 ptRMS += (
frac[i0] - ptMean) * (
frac[i0] - ptMean);
714 std::stringstream
out;
716 out << std::setw(15) << it->first << std::setw(3) <<
"=" << std::setw(5) << *it->second.first <<
" ("
717 << std::setw(5) << it->second.second <<
")" << std::endl;
726 *it->second.first = it->second.second;
731 #define INIT_VARIABLE(NAME, TMVANAME, VAL) \
732 internalId_.NAME##_ = VAL; \
733 variables_[#NAME] = std::make_pair(&internalId_.NAME##_, VAL);