11 #include "TMatrixDSym.h" 12 #include "TMatrixDSymEigen.h" 13 #include "TMVA/MethodBDT.h" 23 cutBased_(ps.getParameter<
bool>(
"cutBased")),
24 etaBinnedWeights_(
false),
27 label_(ps.getParameter<
std::
string>(
"label")),
34 std::vector<std::string> tmvaEtaWeights;
41 const std::vector<edm::ParameterSet>&
trainings = ps.getParameter<std::vector <edm::ParameterSet> >(
"trainings");
42 nEtaBins_ = ps.getParameter<
int>(
"nEtaBins");
45 jEtaMin_.push_back( trainings.at(
v).getParameter<
double>(
"jEtaMin") );
46 jEtaMax_.push_back( trainings.at(
v).getParameter<
double>(
"jEtaMax") );
49 tmvaEtaVariables_.push_back( trainings.at(
v).getParameter<std::vector<std::string> >(
"tmvaVariables") );
53 tmvaVariables_ = ps.getParameter<std::vector<std::string> >(
"tmvaVariables");
56 tmvaSpectators = ps.getParameter<std::vector<std::string> >(
"tmvaSpectators");
57 version = ps.getParameter<
int>(
"version");
63 for (
int i0 = 0; i0 < 3; i0++) {
69 for (
int i1 = 0; i1 < nCut; i1++) {
71 if (
cutBased_ && i1 == 0) lFullCutType =
"BetaStar"+ lCutType;
72 if (
cutBased_ && i1 == 1) 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++)
mvacut_[i0][0][i2] = pt010 [i2];
79 for (
int i2 = 0; i2 < 4; i2++)
mvacut_[i0][1][i2] = pt1020[i2];
80 for (
int i2 = 0; i2 < 4; i2++)
mvacut_[i0][2][i2] = pt2030[i2];
81 for (
int i2 = 0; i2 < 4; i2++)
mvacut_[i0][3][i2] = pt3050[i2];
84 for (
int i2 = 0; i2 < 4; i2++)
betaStarCut_[i0][0][i2] = pt010 [i2];
85 for (
int i2 = 0; i2 < 4; i2++)
betaStarCut_[i0][1][i2] = pt1020[i2];
86 for (
int i2 = 0; i2 < 4; i2++)
betaStarCut_[i0][2][i2] = pt2030[i2];
87 for (
int i2 = 0; i2 < 4; i2++)
betaStarCut_[i0][3][i2] = pt3050[i2];
90 for (
int i2 = 0; i2 < 4; i2++)
rmsCut_[i0][0][i2] = pt010 [i2];
91 for (
int i2 = 0; i2 < 4; i2++)
rmsCut_[i0][1][i2] = pt1020[i2];
92 for (
int i2 = 0; i2 < 4; i2++)
rmsCut_[i0][2][i2] = pt2030[i2];
93 for (
int i2 = 0; i2 < 4; i2++)
rmsCut_[i0][3][i2] = pt3050[i2];
113 std::unique_ptr<const GBRForest>
121 TMVA::Reader tmpTMVAReader(
"!Color:Silent:!Error" );
122 for (
auto const& varName : varList) {
124 tmpTMVAReader.AddVariable( varName, std::get<float *,float>(algo.
getVariables().at(
tmvaNames_[varName])) );
126 for (
auto const& spectatorName : tmvaSpectators) {
128 tmpTMVAReader.AddSpectator( spectatorName, std::get<float *,float>(algo.
getVariables().at(
tmvaNames_[spectatorName])) );
131 return ( std::make_unique<const GBRForest> ( dynamic_cast<TMVA::MethodBDT*>( tmpTMVAReader.FindMVA(
tmvaMethod_.c_str()) ) ) );
146 void assign(
const std::vector<float> & vec,
float &
a,
float &
b,
float &
c,
float &
d )
148 size_t sz = vec.size();
149 a = ( sz > 0 ? vec[0] : 0. );
150 b = ( sz > 1 ? vec[1] : 0. );
151 c = ( sz > 2 ? vec[2] : 0. );
152 d = ( sz > 3 ? vec[3] : 0. );
172 std::vector<float> vars;
173 for(std::vector<std::string>::const_iterator it=varList.begin(); it!=varList.end(); ++it) {
175 vars.push_back( *var.first );
177 return reader->GetClassifier(vars.data());
211 if(jetPt >= 10 && jetPt < 20) ptId = 1;
212 if(jetPt >= 20 && jetPt < 30) ptId = 2;
213 if(jetPt >= 30 ) ptId = 3;
220 return std::pair<int,int>(ptId,etaId);
225 std::pair<int,int> jetIdKey =
getJetIdKey(jetPt,jetEta);
226 float betaStarModified = betaStarClassic/
log(nvtx-0.64);
244 std::pair<int,int> jetIdKey =
getJetIdKey(jetPt,jetEta);
279 assert( patjet !=
nullptr || pfjet !=
nullptr );
280 if( patjet !=
nullptr && jec == 0. ) {
287 const reco::Candidate* lLead =
nullptr, *lSecond =
nullptr, *lLeadNeut =
nullptr, *lLeadEm =
nullptr, *lLeadCh =
nullptr, *lTrail =
nullptr;
288 std::vector<float>
frac, fracCh, fracEm, fracNeut;
289 float cones[] = { 0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7 };
290 size_t ncones =
sizeof(cones)/
sizeof(
float);
299 TMatrixDSym covMatrix(2); covMatrix = 0.;
301 float sumPt = 0., sumPt2 = 0., sumTkPt = 0.,sumPtCh=0,sumPtNe = 0;
315 bool isPacked =
true;
316 if (lPack ==
nullptr){
320 float candPt = icand->
pt();
321 float candPtFrac = candPt/
jetPt;
323 float candDeta = icand->
eta() - jet->
eta();
325 float candPtDr = candPt * candDr;
326 size_t icone = std::lower_bound(&cones[0],&cones[ncones],candDr) - &cones[0];
328 if(candDr < dRmin) dRmin = candDr;
331 if( lLead ==
nullptr || candPt > lLead->
pt() ) {
334 }
else if( (lSecond ==
nullptr || candPt > lSecond->pt()) && (candPt < lLead->
pt()) ) {
341 covMatrix(0,0) += candPt*candPt*candDeta*candDeta;
342 covMatrix(0,1) += candPt*candPt*candDeta*candDphi;
343 covMatrix(1,1) += candPt*candPt*candDphi*candDphi;
346 sumPt2 += candPt*candPt;
349 frac.push_back(candPtFrac);
351 if( icone < ncones ) { *coneFracs[icone] += candPt; }
355 if (lLeadNeut ==
nullptr || candPt > lLeadNeut->pt()) { lLeadNeut = icand; }
357 fracNeut.push_back(candPtFrac);
358 if( icone < ncones ) { *coneNeutFracs[icone] += candPt; }
363 if( icand->
pdgId() == 22 ) {
364 if(lLeadEm ==
nullptr || candPt > lLeadEm->pt()) { lLeadEm = icand; }
366 fracEm.push_back(candPtFrac);
367 if( icone < ncones ) { *coneEmFracs[icone] += candPt; }
372 if( icand->
charge() != 0 ) {
373 if (lLeadCh ==
nullptr || candPt > lLeadCh->pt()) {
377 if (lPF &&
std::abs(icand->
pdgId()) == 13 && pfTrk ==
nullptr){
382 edm::LogWarning(
"BadMuon")<<
"Found a PFCandidate muon without a trackRef: falling back to Muon::bestTrack ";
391 else if(lPF!=
nullptr) {
404 fracCh.push_back(candPtFrac);
405 if( icone < ncones ) { *coneChFracs[icone] += candPt; }
409 if( icand->
charge() != 0 ) {
417 bool inAnyOther =
false;
421 for(reco::VertexCollection::const_iterator vi=allvtx.begin(); vi!=allvtx.end(); ++vi ) {
423 if( iv.
isFake() || iv.
ndof() < 4 ) {
continue; }
428 if( ! isVtx0 && ! inAnyOther ) {
435 if( inVtx0 && ! inAnyOther ) {
437 }
else if( ! inVtx0 && inAnyOther ) {
443 }
else if( dZ < 0.2 ) {
452 bool inVtxOther =
false;
454 double dZ_tmp = 9999.;
455 for (
unsigned vtx_i = 0 ; vtx_i < allvtx.size() ; vtx_i++ ) {
456 auto iv = allvtx[vtx_i];
462 bool isVtx0 = (iv.position() - vtx->
position()).
r() < 0.02;
466 if (lPack->
fromPV(vtx_i) == 0) inVtxOther =
true;
467 dZ0 = lPack->
dz(iv.position());
470 if (fabs(lPack->
dz(iv.position())) < fabs(dZ_tmp)) {
471 dZ_tmp = lPack->
dz(iv.position());
476 }
else if (inVtxOther){
479 if (fabs(dZ0) < 0.2){
481 }
else if (fabs(dZ_tmp) < 0.2){
487 if( lTrail ==
nullptr || candPt < lTrail->
pt() ) {
494 assert( !(lLead ==
nullptr) );
496 if ( lSecond ==
nullptr ) { lSecond = lTrail; }
497 if ( lLeadNeut ==
nullptr ) { lLeadNeut = lTrail; }
498 if ( lLeadEm ==
nullptr ) { lLeadEm = lTrail; }
499 if ( lLeadCh ==
nullptr ) { lLeadCh = lTrail; }
501 if( patjet !=
nullptr ) {
520 float sum_deta(0.0),sum_dphi(0.0);
521 float ave_deta(0.0), ave_dphi(0.0);
524 if (!(
part.isAvailable() &&
part.isNonnull()) ){
529 float weight2 = weight *
weight;
531 float deta =
part->eta() - jet->
eta();
533 sum_deta += deta*weight2;
534 sum_dphi += dphi*weight2;
536 ave_deta = sum_deta/sumW2;
537 ave_dphi = sum_dphi/sumW2;
541 float ddetaR_sum(0.0), ddphiR_sum(0.0), pull_tmp(0.0);
544 if (!(
part.isAvailable() &&
part.isNonnull()) ){
548 float deta =
part->eta() - jet->
eta();
550 float ddeta, ddphi, ddR;
551 ddeta = deta - ave_deta ;
552 ddphi = dphi-ave_dphi;
553 ddR =
sqrt(ddeta*ddeta + ddphi*ddphi);
554 ddetaR_sum += ddR*ddeta*
weight;
555 ddphiR_sum += ddR*ddphi*
weight;
558 float ddetaR_ave = ddetaR_sum/sumW2;
559 float ddphiR_ave = ddphiR_sum/sumW2;
560 pull_tmp =
sqrt(ddetaR_ave*ddetaR_ave+ddphiR_ave*ddphiR_ave);
572 std::sort(frac.begin(),frac.end(),std::greater<float>());
573 std::sort(fracCh.begin(),fracCh.end(),std::greater<float>());
574 std::sort(fracEm.begin(),fracEm.end(),std::greater<float>());
575 std::sort(fracNeut.begin(),fracNeut.end(),std::greater<float>());
581 covMatrix(0,0) /= sumPt2;
582 covMatrix(0,1) /= sumPt2;
583 covMatrix(1,1) /= sumPt2;
584 covMatrix(1,0) = covMatrix(0,1);
588 TVectorD eigVals(2); eigVals = TMatrixDSymEigen(covMatrix).GetEigenValues();
601 for(
size_t ic=0; ic<ncones; ++ic){
602 *coneFracs[ic] /=
jetPt;
603 *coneEmFracs[ic] /=
jetPt;
604 *coneNeutFracs[ic] /=
jetPt;
605 *coneChFracs[ic] /=
jetPt;
610 for(
unsigned int i0 = 0; i0 < frac.size(); i0++) {ptRMS+=(frac[i0]-ptMean)*(frac[i0]-ptMean);}
628 if( sumTkPt != 0. ) {
649 std::stringstream
out;
650 for(variables_list_t::const_iterator it=
variables_.begin();
652 out << std::setw(15) << it->first << std::setw(3) <<
"=" 653 << std::setw(5) << *it->second.first
654 <<
" (" << std::setw(5) << it->second.second <<
")" << std::endl;
663 for(variables_list_t::iterator it=
variables_.begin();
665 *it->second.first = it->second.second;
670 #define INIT_VARIABLE(NAME,TMVANAME,VAL) \ 671 internalId_.NAME ## _ = VAL; \ 672 variables_[ # NAME ] = std::make_pair(& internalId_.NAME ## _, VAL);
virtual float dz(size_t ipv=0) const
dz with respect to the PV[ipv]
constexpr double deltaPhi(double phi1, double phi2)
T getParameter(std::string const &) const
void set(const PileupJetIdentifier &)
bool isNonnull() const
Checks for non-null.
AlgoGBRForestsAndConstants const * cache_
double eta() const final
momentum pseudorapidity
#define INIT_VARIABLE(NAME, TMVANAME, VAL)
AlgoGBRForestsAndConstants(edm::ParameterSet const &, bool runMvas)
float chargedHadronEnergy() const
chargedHadronEnergy
float chargedEmEnergy() const
chargedEmEnergy
std::vector< double > const & jEtaMin() const
float neutralEmEnergy() const
neutralEmEnergy
std::map< std::string, std::string > tmvaNames_
std::vector< std::unique_ptr< const GBRForest > > etaReader_
virtual const Track * bestTrack() const
T const * get() const
Returns C++ pointer to the item.
Base class for all types of Jets.
float neutralHadronEnergy() const
neutralHadronEnergy
std::pair< int, int > getJetIdKey(float jetPt, float jetEta)
size_type numberOfSourceCandidatePtrs() const override
std::vector< double > jEtaMin_
double pt() const final
transverse momentum
std::vector< Vertex > VertexCollection
collection of Vertex objects
int neutralMultiplicity() const
neutralMultiplicity
int computeCutIDflag(float betaStarClassic, float dR2Mean, float nvtx, float jetPt, float jetEta)
std::vector< double > const & jEtaMax() const
int chargedMultiplicity() const
chargedMultiplicity
const Track * bestTrack() const override
const Point & position() const
position
const variables_list_t & getVariables() const
const PileupJetIdentifier::variables_list_t & getVariables() const { return variables_; }; ...
Jets made from PFObjects.
float neutralEmEnergy() const
neutralEmEnergy
float chargedEmEnergy() const
chargedEmEnergy
size_t numberOfDaughters() const override
number of daughters
reco::TrackRef trackRef() const
std::vector< std::string > tmvaVariables_
PileupJetIdentifier internalId_
void swap(edm::DataFrameContainer &lhs, edm::DataFrameContainer &rhs)
virtual int pdgId() const =0
PDG identifier.
const PVAssoc fromPV(size_t ipv=0) const
float betaStarCut_[3][4][4]
std::string dumpVariables() const
double energy() const final
energy
Abs< T >::type abs(const T &t)
void assign(const std::vector< float > &vec, float &a, float &b, float &c, float &d)
float trackWeight(const TREF &r) const
returns the weight with which a Track has contributed to the vertex-fit.
T const * get() const
Returns C++ pointer to the item.
std::unique_ptr< const GBRForest > const & reader() const
std::vector< std::vector< std::string > > tmvaEtaVariables_
int computeIDflag(float mva, float jetPt, float jetEta)
int neutralMultiplicity() const
neutralMultiplicity
array_t const & mvacut() const
reco::MuonRef muonRef() const
constexpr auto deltaR(const T1 &t1, const T2 &t2) -> decltype(t1.eta())
double dz() const
dz parameter (= dsz/cos(lambda)). This is the track z0 w.r.t (0,0,0) only if the refPoint is close to...
void setPtEtaPhi(const reco::Candidate &p, float &pt, float &eta, float &phi)
virtual double eta() const =0
momentum pseudorapidity
PileupJetIdentifier computeIdVariables(const reco::Jet *jet, float jec, const reco::Vertex *, const reco::VertexCollection &, double rho)
virtual double pt() const =0
transverse momentum
virtual CandidatePtr daughterPtr(size_type i) const
reference to daughter at given position
std::unique_ptr< const GBRForest > getMVA(std::vector< std::string > const &varList, std::string const &tmvaWeights, std::vector< std::string > const &tmvaSpectators)
PileupJetIdentifier computeMva()
Analysis-level calorimeter jet class.
std::vector< std::unique_ptr< const GBRForest > > const & etaReader() const
TMVA::IMethod * loadTMVAWeights(TMVA::Reader *reader, const std::string &method, const std::string &weightFile, bool verbose=false)
virtual int charge() const =0
electric charge
Particle reconstructed by the particle flow algorithm.
virtual int nConstituents() const
of constituents
reco::GsfTrackRef gsfTrackRef() const
CandidatePtr sourceCandidatePtr(size_type i) const override
std::vector< std::vector< std::string > > const & tmvaEtaVariables() const
variables_list_t variables_
std::string fullPath() const
float neutralHadronEnergy() const
neutralHadronEnergy
bool etaBinnedWeights() const
virtual float dxy() const
dxy with respect to the PV ref
double dxy() const
dxy parameter. (This is the transverse impact parameter w.r.t. to (0,0,0) ONLY if refPoint is close t...
Jet correctedJet(const std::string &level, const std::string &flavor="none", const std::string &set="") const
std::vector< std::string > const & tmvaVariables() const
float getMVAval(const std::vector< std::string > &, const std::unique_ptr< const GBRForest > &)
virtual double phi() const =0
momentum azimuthal angle
int chargedMultiplicity() const
chargedMultiplicity
float chargedHadronEnergy() const
chargedHadronEnergy
std::unique_ptr< const GBRForest > reader_
PileupJetIdAlgo(AlgoGBRForestsAndConstants const *cache)
double mass() const final
mass
std::vector< double > jEtaMax_