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> pt3040 = jetConfig.
getParameter<std::vector<double> >((
"Pt3040_" + lFullCutType).c_str());
77 std::vector<double> pt4050 = jetConfig.
getParameter<std::vector<double> >((
"Pt4050_" + lFullCutType).c_str());
79 for (
int i2 = 0;
i2 < 4;
i2++)
80 mvacut_[i0][0][
i2] = pt010[
i2];
81 for (
int i2 = 0;
i2 < 4;
i2++)
82 mvacut_[i0][1][
i2] = pt1020[
i2];
83 for (
int i2 = 0;
i2 < 4;
i2++)
84 mvacut_[i0][2][
i2] = pt2030[
i2];
85 for (
int i2 = 0;
i2 < 4;
i2++)
86 mvacut_[i0][3][
i2] = pt3040[
i2];
87 for (
int i2 = 0;
i2 < 4;
i2++)
88 mvacut_[i0][4][
i2] = pt4050[
i2];
90 if (cutBased_ &&
i1 == 0) {
91 for (
int i2 = 0;
i2 < 4;
i2++)
92 betaStarCut_[i0][0][
i2] = pt010[
i2];
93 for (
int i2 = 0;
i2 < 4;
i2++)
94 betaStarCut_[i0][1][
i2] = pt1020[
i2];
95 for (
int i2 = 0;
i2 < 4;
i2++)
96 betaStarCut_[i0][2][
i2] = pt2030[
i2];
97 for (
int i2 = 0;
i2 < 4;
i2++)
98 betaStarCut_[i0][3][
i2] = pt3040[
i2];
99 for (
int i2 = 0;
i2 < 4;
i2++)
100 betaStarCut_[i0][4][
i2] = pt4050[
i2];
102 if (cutBased_ &&
i1 == 1) {
103 for (
int i2 = 0;
i2 < 4;
i2++)
104 rmsCut_[i0][0][
i2] = pt010[
i2];
105 for (
int i2 = 0;
i2 < 4;
i2++)
106 rmsCut_[i0][1][
i2] = pt1020[
i2];
107 for (
int i2 = 0;
i2 < 4;
i2++)
108 rmsCut_[i0][2][
i2] = pt2030[
i2];
109 for (
int i2 = 0;
i2 < 4;
i2++)
110 rmsCut_[i0][3][
i2] = pt3040[
i2];
111 for (
int i2 = 0;
i2 < 4;
i2++)
112 rmsCut_[i0][4][
i2] = pt4050[
i2];
118 assert(tmvaMethod_.empty() || ((!tmvaVariables_.empty() || (!tmvaEtaVariables_.empty())) &&
version ==
USER));
121 if ((!cutBased_) && (runMvas_)) {
122 if (etaBinnedWeights_) {
123 for (
int v = 0;
v < nEtaBins_;
v++) {
138 void assign(
const std::vector<float>& vec,
float&
a,
float&
b,
float&
c,
float&
d) {
139 size_t sz = vec.size();
140 a = (sz > 0 ? vec[0] : 0.);
141 b = (sz > 1 ? vec[1] : 0.);
142 c = (sz > 2 ? vec[2] : 0.);
143 d = (sz > 3 ? vec[3] : 0.);
158 const std::unique_ptr<const GBRForest>&
reader) {
159 std::vector<float>
vars;
160 for (std::vector<std::string>::const_iterator it = varList.begin(); it != varList.end(); ++it) {
215 return std::pair<int, int>(ptId, etaId);
220 float betaStarModified = betaStarClassic /
log(nvtx - 0.64);
270 const pat::Jet* patjet = dynamic_cast<const pat::Jet*>(
jet);
272 assert(patjet !=
nullptr || pfjet !=
nullptr);
273 if (patjet !=
nullptr &&
jec == 0.) {
280 const reco::Candidate *lLead =
nullptr, *lSecond =
nullptr, *lLeadNeut =
nullptr, *lLeadEm =
nullptr,
281 *lLeadCh =
nullptr, *lTrail =
nullptr;
282 std::vector<float>
frac, fracCh, fracEm, fracNeut;
283 float cones[] = {0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7};
284 size_t ncones =
sizeof(cones) /
sizeof(
float);
313 TMatrixDSym covMatrix(2);
316 float sumPt = 0., sumPt2 = 0., sumTkPt = 0., sumPtCh = 0, sumPtNe = 0;
317 float multNeut = 0.0;
326 for (
unsigned i = 0;
i <
jet->numberOfSourceCandidatePtrs(); ++
i) {
332 bool isPacked =
true;
333 if (lPack ==
nullptr) {
336 float candPuppiWeight = 1.0;
339 float candPt = (icand->
pt()) * candPuppiWeight;
340 float candPtFrac = candPt /
jetPt;
342 float candDeta = icand->
eta() -
jet->eta();
344 float candPtDr = candPt * candDr;
345 size_t icone =
std::lower_bound(&cones[0], &cones[ncones], candDr) - &cones[0];
351 if (lLead ==
nullptr || candPt > lLead->
pt()) {
354 }
else if ((lSecond ==
nullptr || candPt > lSecond->pt()) && (candPt < lLead->
pt())) {
361 covMatrix(0, 0) += candPt * candPt * candDeta * candDeta;
362 covMatrix(0, 1) += candPt * candPt * candDeta * candDphi;
363 covMatrix(1, 1) += candPt * candPt * candDphi * candDphi;
366 sumPt2 += candPt * candPt;
369 frac.push_back(candPtFrac);
371 if (icone < ncones) {
372 *coneFracs[icone] += candPt;
377 if (lLeadNeut ==
nullptr || candPt > lLeadNeut->pt()) {
381 fracNeut.push_back(candPtFrac);
382 if (icone < ncones) {
383 *coneNeutFracs[icone] += candPt;
387 multNeut += candPuppiWeight;
390 if (icand->
pdgId() == 22) {
391 if (lLeadEm ==
nullptr || candPt > lLeadEm->pt()) {
395 fracEm.push_back(candPtFrac);
396 if (icone < ncones) {
397 *coneEmFracs[icone] += candPt;
401 multNeut += candPuppiWeight;
404 multNeut += candPuppiWeight;
407 if (icand->
charge() != 0) {
408 if (lLeadCh ==
nullptr || candPt > lLeadCh->pt()) {
412 if (lPF &&
std::abs(icand->
pdgId()) == 13 && pfTrk ==
nullptr) {
418 <<
"Found a PFCandidate muon without a trackRef: falling back to Muon::bestTrack ";
421 if (pfTrk ==
nullptr) {
426 }
else if (lPF !=
nullptr) {
438 fracCh.push_back(candPtFrac);
439 if (icone < ncones) {
440 *coneChFracs[icone] += candPt;
445 if (icand->
charge() != 0) {
451 bool inVtx0 =
vtx->trackWeight(lPF->
trackRef()) > 0;
453 bool inAnyOther =
false;
457 for (reco::VertexCollection::const_iterator vi = allvtx.begin(); vi != allvtx.end(); ++vi) {
463 bool isVtx0 = (iv.
position() -
vtx->position()).
r() < 0.02;
466 if (!isVtx0 && !inAnyOther) {
467 inAnyOther =
vtx->trackWeight(lPF->
trackRef()) <= 0;
473 if (inVtx0 && !inAnyOther) {
475 }
else if (!inVtx0 && inAnyOther) {
481 }
else if (dZ < 0.2) {
489 bool inVtxOther =
false;
491 double dZ_tmp = 9999.;
492 for (
unsigned vtx_i = 0; vtx_i < allvtx.size(); vtx_i++) {
493 auto iv = allvtx[vtx_i];
499 bool isVtx0 = (iv.position() -
vtx->position()).
r() < 0.02;
504 if (lPack->
fromPV(vtx_i) == 0)
506 dZ0 = lPack->
dz(iv.position());
509 if (fabs(lPack->
dz(iv.position())) < fabs(dZ_tmp)) {
510 dZ_tmp = lPack->
dz(iv.position());
515 }
else if (inVtxOther) {
518 if (fabs(dZ0) < 0.2) {
520 }
else if (fabs(dZ_tmp) < 0.2) {
527 if (lTrail ==
nullptr || candPt < lTrail->
pt()) {
533 assert(!(lLead ==
nullptr));
535 if (lSecond ==
nullptr) {
538 if (lLeadNeut ==
nullptr) {
541 if (lLeadEm ==
nullptr) {
544 if (lLeadCh ==
nullptr) {
548 if (patjet !=
nullptr) {
569 float sum_deta(0.0), sum_dphi(0.0);
570 float ave_deta(0.0), ave_dphi(0.0);
571 for (
size_t j = 0;
j <
jet->numberOfDaughters();
j++) {
572 const auto&
part =
jet->daughterPtr(
j);
573 if (!(
part.isAvailable() &&
part.isNonnull())) {
577 float partPuppiWeight = 1.0;
580 if (partpack !=
nullptr)
584 float weight = (
part->pt()) * partPuppiWeight;
587 float deta =
part->eta() -
jet->eta();
589 sum_deta += deta * weight2;
590 sum_dphi += dphi * weight2;
592 ave_deta = sum_deta / sumW2;
593 ave_dphi = sum_dphi / sumW2;
597 float ddetaR_sum(0.0), ddphiR_sum(0.0), pull_tmp(0.0);
598 for (
size_t i = 0;
i <
jet->numberOfDaughters();
i++) {
599 const auto&
part =
jet->daughterPtr(
i);
600 if (!(
part.isAvailable() &&
part.isNonnull())) {
604 float partPuppiWeight = 1.0;
607 if (partpack !=
nullptr)
611 float weight = partPuppiWeight * (
part->pt()) * partPuppiWeight * (
part->pt());
612 float deta =
part->eta() -
jet->eta();
614 float ddeta, ddphi, ddR;
615 ddeta = deta - ave_deta;
616 ddphi = dphi - ave_dphi;
617 ddR =
sqrt(ddeta * ddeta + ddphi * ddphi);
618 ddetaR_sum += ddR * ddeta *
weight;
619 ddphiR_sum += ddR * ddphi *
weight;
622 float ddetaR_ave = ddetaR_sum / sumW2;
623 float ddphiR_ave = ddphiR_sum / sumW2;
624 pull_tmp =
sqrt(ddetaR_ave * ddetaR_ave + ddphiR_ave * ddphiR_ave);
635 std::sort(
frac.begin(),
frac.end(), std::greater<float>());
636 std::sort(fracCh.begin(), fracCh.end(), std::greater<float>());
637 std::sort(fracEm.begin(), fracEm.end(), std::greater<float>());
638 std::sort(fracNeut.begin(), fracNeut.end(), std::greater<float>());
650 covMatrix(0, 0) /= sumPt2;
651 covMatrix(0, 1) /= sumPt2;
652 covMatrix(1, 1) /= sumPt2;
653 covMatrix(1, 0) = covMatrix(0, 1);
658 eigVals = TMatrixDSymEigen(covMatrix).GetEigenValues();
666 if (lSecond ==
nullptr) {
675 for (
size_t ic = 0; ic < ncones; ++ic) {
676 *coneFracs[ic] /=
jetPt;
677 *coneEmFracs[ic] /=
jetPt;
678 *coneNeutFracs[ic] /=
jetPt;
679 *coneChFracs[ic] /=
jetPt;
684 for (
unsigned int i0 = 0; i0 <
frac.size(); i0++) {
685 ptRMS += (
frac[i0] - ptMean) * (
frac[i0] - ptMean);
723 std::stringstream
out;
725 out << std::setw(15) << it->first << std::setw(3) <<
"=" << std::setw(5) << *it->second.first <<
" ("
726 << std::setw(5) << it->second.second <<
")" << std::endl;
735 *it->second.first = it->second.second;
740 #define INIT_VARIABLE(NAME, TMVANAME, VAL) \
741 internalId_.NAME##_ = VAL; \
742 variables_[#NAME] = std::make_pair(&internalId_.NAME##_, VAL);