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;
302 float multNeut = 0.0;
316 bool isPacked =
true;
317 if (lPack ==
nullptr){
320 float candPuppiWeight = 1.0;
321 if (usePuppi && isPacked) candPuppiWeight = lPack->
puppiWeight();
322 float candPt = (icand->
pt())*candPuppiWeight;
323 float candPtFrac = candPt/
jetPt;
325 float candDeta = icand->
eta() - jet->
eta();
327 float candPtDr = candPt * candDr;
328 size_t icone = std::lower_bound(&cones[0],&cones[ncones],candDr) - &cones[0];
330 if(candDr < dRmin) dRmin = candDr;
333 if( lLead ==
nullptr || candPt > lLead->
pt() ) {
336 }
else if( (lSecond ==
nullptr || candPt > lSecond->pt()) && (candPt < lLead->
pt()) ) {
343 covMatrix(0,0) += candPt*candPt*candDeta*candDeta;
344 covMatrix(0,1) += candPt*candPt*candDeta*candDphi;
345 covMatrix(1,1) += candPt*candPt*candDphi*candDphi;
348 sumPt2 += candPt*candPt;
351 frac.push_back(candPtFrac);
353 if( icone < ncones ) { *coneFracs[icone] += candPt; }
357 if (lLeadNeut ==
nullptr || candPt > lLeadNeut->pt()) { lLeadNeut = icand; }
359 fracNeut.push_back(candPtFrac);
360 if( icone < ncones ) { *coneNeutFracs[icone] += candPt; }
363 multNeut += candPuppiWeight;
366 if( icand->
pdgId() == 22 ) {
367 if(lLeadEm ==
nullptr || candPt > lLeadEm->pt()) { lLeadEm = icand; }
369 fracEm.push_back(candPtFrac);
370 if( icone < ncones ) { *coneEmFracs[icone] += candPt; }
373 multNeut += candPuppiWeight;
375 if((
abs(icand->
pdgId()) == 1) || (
abs(icand->
pdgId()) == 2)) multNeut += candPuppiWeight;
378 if( icand->
charge() != 0 ) {
379 if (lLeadCh ==
nullptr || candPt > lLeadCh->pt()) {
383 if (lPF &&
std::abs(icand->
pdgId()) == 13 && pfTrk ==
nullptr){
388 edm::LogWarning(
"BadMuon")<<
"Found a PFCandidate muon without a trackRef: falling back to Muon::bestTrack ";
397 else if(lPF!=
nullptr) {
410 fracCh.push_back(candPtFrac);
411 if( icone < ncones ) { *coneChFracs[icone] += candPt; }
415 if( icand->
charge() != 0 ) {
423 bool inAnyOther =
false;
427 for(reco::VertexCollection::const_iterator vi=allvtx.begin(); vi!=allvtx.end(); ++vi ) {
429 if( iv.
isFake() || iv.
ndof() < 4 ) {
continue; }
434 if( ! isVtx0 && ! inAnyOther ) {
441 if( inVtx0 && ! inAnyOther ) {
443 }
else if( ! inVtx0 && inAnyOther ) {
449 }
else if( dZ < 0.2 ) {
458 bool inVtxOther =
false;
460 double dZ_tmp = 9999.;
461 for (
unsigned vtx_i = 0 ; vtx_i < allvtx.size() ; vtx_i++ ) {
462 auto iv = allvtx[vtx_i];
468 bool isVtx0 = (iv.position() - vtx->
position()).
r() < 0.02;
472 if (lPack->
fromPV(vtx_i) == 0) inVtxOther =
true;
473 dZ0 = lPack->
dz(iv.position());
476 if (fabs(lPack->
dz(iv.position())) < fabs(dZ_tmp)) {
477 dZ_tmp = lPack->
dz(iv.position());
482 }
else if (inVtxOther){
485 if (fabs(dZ0) < 0.2){
487 }
else if (fabs(dZ_tmp) < 0.2){
494 if( lTrail ==
nullptr || candPt < lTrail->
pt() ) {
501 assert( !(lLead ==
nullptr) );
503 if ( lSecond ==
nullptr ) { lSecond = lTrail; }
504 if ( lLeadNeut ==
nullptr ) { lLeadNeut = lTrail; }
505 if ( lLeadEm ==
nullptr ) { lLeadEm = lTrail; }
506 if ( lLeadCh ==
nullptr ) { lLeadCh = lTrail; }
508 if( patjet !=
nullptr ) {
528 float sum_deta(0.0),sum_dphi(0.0);
529 float ave_deta(0.0), ave_dphi(0.0);
532 if (!(
part.isAvailable() &&
part.isNonnull()) ){
536 float partPuppiWeight=1.0;
539 if (partpack!=
nullptr) partPuppiWeight = partpack->
puppiWeight();
543 float weight2 = weight *
weight;
545 float deta =
part->eta() - jet->
eta();
547 sum_deta += deta*weight2;
548 sum_dphi += dphi*weight2;
550 ave_deta = sum_deta/sumW2;
551 ave_dphi = sum_dphi/sumW2;
555 float ddetaR_sum(0.0), ddphiR_sum(0.0), pull_tmp(0.0);
558 if (!(
part.isAvailable() &&
part.isNonnull()) ){
562 float partPuppiWeight=1.0;
565 if (partpack!=
nullptr) partPuppiWeight = partpack->
puppiWeight();
568 float weight = partPuppiWeight*(
part->pt())*partPuppiWeight*(
part->pt());
569 float deta =
part->eta() - jet->
eta();
571 float ddeta, ddphi, ddR;
572 ddeta = deta - ave_deta ;
573 ddphi = dphi-ave_dphi;
574 ddR =
sqrt(ddeta*ddeta + ddphi*ddphi);
575 ddetaR_sum += ddR*ddeta*
weight;
576 ddphiR_sum += ddR*ddphi*
weight;
579 float ddetaR_ave = ddetaR_sum/sumW2;
580 float ddphiR_ave = ddphiR_sum/sumW2;
581 pull_tmp =
sqrt(ddetaR_ave*ddetaR_ave+ddphiR_ave*ddphiR_ave);
593 std::sort(frac.begin(),frac.end(),std::greater<float>());
594 std::sort(fracCh.begin(),fracCh.end(),std::greater<float>());
595 std::sort(fracEm.begin(),fracEm.end(),std::greater<float>());
596 std::sort(fracNeut.begin(),fracNeut.end(),std::greater<float>());
602 covMatrix(0,0) /= sumPt2;
603 covMatrix(0,1) /= sumPt2;
604 covMatrix(1,1) /= sumPt2;
605 covMatrix(1,0) = covMatrix(0,1);
609 TVectorD eigVals(2); eigVals = TMatrixDSymEigen(covMatrix).GetEigenValues();
622 for(
size_t ic=0; ic<ncones; ++ic){
623 *coneFracs[ic] /=
jetPt;
624 *coneEmFracs[ic] /=
jetPt;
625 *coneNeutFracs[ic] /=
jetPt;
626 *coneChFracs[ic] /=
jetPt;
631 for(
unsigned int i0 = 0; i0 < frac.size(); i0++) {ptRMS+=(frac[i0]-ptMean)*(frac[i0]-ptMean);}
649 if( sumTkPt != 0. ) {
670 std::stringstream
out;
671 for(variables_list_t::const_iterator it=
variables_.begin();
673 out << std::setw(15) << it->first << std::setw(3) <<
"=" 674 << std::setw(5) << *it->second.first
675 <<
" (" << std::setw(5) << it->second.second <<
")" << std::endl;
684 for(variables_list_t::iterator it=
variables_.begin();
686 *it->second.first = it->second.second;
691 #define INIT_VARIABLE(NAME,TMVANAME,VAL) \ 692 internalId_.NAME ## _ = VAL; \ 693 variables_[ # NAME ] = std::make_pair(& internalId_.NAME ## _, VAL); float puppiWeight() const
Set both weights at once (with option for only full PUPPI)
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 computeIdVariables(const reco::Jet *jet, float jec, const reco::Vertex *, const reco::VertexCollection &, double rho, bool usePuppi)
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
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_