Skip to content

Commit

Permalink
Merge pull request #82 from steggema/TauClusterVarRefactor_10_3_X
Browse files Browse the repository at this point in the history
Address code review comments, including modernisation of code
  • Loading branch information
mbluj authored Aug 30, 2018
2 parents 23da7ae + eef7b80 commit 1ad3da6
Show file tree
Hide file tree
Showing 5 changed files with 121 additions and 125 deletions.
57 changes: 40 additions & 17 deletions RecoTauTag/RecoTau/interface/PFRecoTauClusterVariables.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,38 +16,61 @@
#include "DataFormats/PatCandidates/interface/Tau.h"
#include "DataFormats/ParticleFlowCandidate/interface/PFCandidateFwd.h"

namespace reco { namespace tau { namespace mva {
namespace reco { namespace tau {
/// return chi2 of the leading track ==> deprecated? <==
float tau_leadTrackChi2(const reco::PFTau& tau);
float lead_track_chi2(const reco::PFTau& tau);
/// return ratio of energy in ECAL over sum of energy in ECAL and HCAL
float tau_Eratio(const reco::PFTau& tau);
float tau_Eratio(const pat::Tau& tau);
float eratio(const reco::PFTau& tau);
float eratio(const pat::Tau& tau);
/// return sum of pt weighted values of distance to tau candidate for all pf photon candidates,
/// which are associated to signal; depending on var the distance is in 0=:dr, 1=:deta, 2=:dphi
float pt_weighted_dx(const reco::PFTau& tau, int mode = 0, int var = 0, int decaymode = -1);
float pt_weighted_dx(const pat::Tau& tau, int mode = 0, int var = 0, int decaymode = -1);
/// return sum of pt weighted values of dr relative to tau candidate for all pf photon candidates,
/// which are associated to signal
float tau_pt_weighted_dr_signal(const reco::PFTau& tau, int dm);
float tau_pt_weighted_dr_signal(const pat::Tau& tau, int dm);
inline float pt_weighted_dr_signal(const reco::PFTau& tau, int dm) {
return pt_weighted_dx(tau, 0, 0, dm);
}
inline float pt_weighted_dr_signal(const pat::Tau& tau, int dm) {
return pt_weighted_dx(tau, 0, 0, dm);
}
/// return sum of pt weighted values of deta relative to tau candidate for all pf photon candidates,
/// which are associated to signal
float tau_pt_weighted_deta_strip(const reco::PFTau& tau, int dm);
float tau_pt_weighted_deta_strip(const pat::Tau& tau, int dm);
inline float pt_weighted_deta_strip(const reco::PFTau& tau, int dm) {
return pt_weighted_dx(tau, dm==10 ? 2 : 1, 1, dm);
}
inline float pt_weighted_deta_strip(const pat::Tau& tau, int dm) {
return pt_weighted_dx(tau, dm==10 ? 2 : 1, 1, dm);
}
/// return sum of pt weighted values of dphi relative to tau candidate for all pf photon candidates,
/// which are associated to signal
float tau_pt_weighted_dphi_strip(const reco::PFTau& tau, int dm);
float tau_pt_weighted_dphi_strip(const pat::Tau& tau, int dm);
inline float pt_weighted_dphi_strip(const reco::PFTau& tau, int dm) {
return pt_weighted_dx(tau, dm==10 ? 2 : 1, 2, dm);
}
inline float pt_weighted_dphi_strip(const pat::Tau& tau, int dm) {
return pt_weighted_dx(tau, dm==10 ? 2 : 1, 2, dm);
}
/// return sum of pt weighted values of dr relative to tau candidate for all pf photon candidates,
/// which are inside an isolation conde but not associated to signal
float tau_pt_weighted_dr_iso(const reco::PFTau& tau, int dm);
float tau_pt_weighted_dr_iso(const pat::Tau& tau, int dm);
inline float pt_weighted_dr_iso(const reco::PFTau& tau, int dm) {
return pt_weighted_dx(tau, 2, 0, dm);
}
inline float pt_weighted_dr_iso(const pat::Tau& tau, int dm) {
return pt_weighted_dx(tau, 2, 0, dm);
}
/// return sum of pt weighted values of dr relative to tau candidate for all pf photon candidates,
/// which are inside an isolation conde but not associated to signal
float pt_weighted_dr_iso(const reco::PFTau& tau, int dm);
float pt_weighted_dr_iso(const pat::Tau& tau, int dm);
/// return total number of pf photon candidates with pT>500 MeV, which are associated to signal
unsigned int tau_n_photons_total(const reco::PFTau& tau);
unsigned int tau_n_photons_total(const pat::Tau& tau);
unsigned int n_photons_total(const reco::PFTau& tau);
unsigned int n_photons_total(const pat::Tau& tau);

enum { kOldDMwoLT, kOldDMwLT, kNewDMwoLT, kNewDMwLT, kDBoldDMwLT, kDBnewDMwLT, kPWoldDMwLT, kPWnewDMwLT, kDBoldDMwLTwGJ, kDBnewDMwLTwGJ };
bool fillMVAInputs(float* mvaInput, const pat::Tau& tau, int mvaOpt, const std::string nameCharged, const std::string nameNeutral, const std::string namePu, const std::string nameOutside, const std::string nameFootprint);
}}} // namespaces
enum {kOldDMwoLT, kOldDMwLT, kNewDMwoLT, kNewDMwLT, kDBoldDMwLT, kDBnewDMwLT, kPWoldDMwLT, kPWnewDMwLT,
kDBoldDMwLTwGJ, kDBnewDMwLTwGJ};
bool fillIsoMVARun2Inputs(float* mvaInput, const pat::Tau& tau, int mvaOpt, const std::string& nameCharged,
const std::string& nameNeutral, const std::string& namePu,
const std::string& nameOutside, const std::string& nameFootprint);
}} // namespaces

#endif
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,8 @@ namespace
}
}

namespace reco { namespace tau {

class PATTauDiscriminationByMVAIsolationRun2 : public PATTauDiscriminationProducerBase
{
public:
Expand Down Expand Up @@ -143,7 +145,6 @@ class PATTauDiscriminationByMVAIsolationRun2 : public PATTauDiscriminationProduc
bool loadMVAfromDB_;
edm::FileInPath inputFileName_;
const GBRForest* mvaReader_;
enum { kOldDMwoLT, kOldDMwLT, kNewDMwoLT, kNewDMwLT, kDBoldDMwLT, kDBnewDMwLT, kPWoldDMwLT, kPWnewDMwLT, kDBoldDMwLTwGJ, kDBnewDMwLTwGJ };
int mvaOpt_;
float* mvaInput_;

Expand Down Expand Up @@ -183,7 +184,7 @@ double PATTauDiscriminationByMVAIsolationRun2::discriminate(const TauRef& tau) c
// CV: computation of MVA value requires presence of leading charged hadron
if ( tau->leadChargedHadrCand().isNull() ) return 0.;

if (reco::tau::mva::fillMVAInputs(mvaInput_, *tau, mvaOpt_, chargedIsoPtSums_, neutralIsoPtSums_, puCorrPtSums_, photonPtSumOutsideSignalCone_, footprintCorrection_)) {
if (reco::tau::fillIsoMVARun2Inputs(mvaInput_, *tau, mvaOpt_, chargedIsoPtSums_, neutralIsoPtSums_, puCorrPtSums_, photonPtSumOutsideSignalCone_, footprintCorrection_)) {
double mvaValue = mvaReader_->GetClassifier(mvaInput_);
if ( verbosity_ ) {
edm::LogPrint("PATTauDiscByMVAIsolRun2") << "<PATTauDiscriminationByMVAIsolationRun2::discriminate>:";
Expand All @@ -207,3 +208,5 @@ void PATTauDiscriminationByMVAIsolationRun2::endEvent(edm::Event& evt)
}

DEFINE_FWK_MODULE(PATTauDiscriminationByMVAIsolationRun2);

}} //namespace
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,8 @@ namespace
}
}

namespace reco { namespace tau {

class PFRecoTauDiscriminationByMVAIsolationRun2 : public PFTauDiscriminationProducerBase
{
public:
Expand Down Expand Up @@ -138,7 +140,6 @@ class PFRecoTauDiscriminationByMVAIsolationRun2 : public PFTauDiscriminationProd
bool loadMVAfromDB_;
edm::FileInPath inputFileName_;
const GBRForest* mvaReader_;
enum { kOldDMwoLT, kOldDMwLT, kNewDMwoLT, kNewDMwLT, kDBoldDMwLT, kDBnewDMwLT, kPWoldDMwLT, kPWnewDMwLT, kDBoldDMwLTwGJ, kDBnewDMwLTwGJ };
int mvaOpt_;
float* mvaInput_;

Expand Down Expand Up @@ -218,13 +219,13 @@ double PFRecoTauDiscriminationByMVAIsolationRun2::discriminate(const PFTauRef& t
float decayDistZ = tauLifetimeInfo.flightLength().z();
float decayDistMag = std::sqrt(decayDistX*decayDistX + decayDistY*decayDistY + decayDistZ*decayDistZ);

float nPhoton = (float)reco::tau::mva::tau_n_photons_total(*tau);
float ptWeightedDetaStrip = reco::tau::mva::tau_pt_weighted_deta_strip(*tau, tauDecayMode);
float ptWeightedDphiStrip = reco::tau::mva::tau_pt_weighted_dphi_strip(*tau, tauDecayMode);
float ptWeightedDrSignal = reco::tau::mva::tau_pt_weighted_dr_signal(*tau, tauDecayMode);
float ptWeightedDrIsolation = reco::tau::mva::tau_pt_weighted_dr_iso(*tau, tauDecayMode);
float leadingTrackChi2 = reco::tau::mva::tau_leadTrackChi2(*tau);
float eRatio = reco::tau::mva::tau_Eratio(*tau);
float nPhoton = (float)reco::tau::n_photons_total(*tau);
float ptWeightedDetaStrip = reco::tau::pt_weighted_deta_strip(*tau, tauDecayMode);
float ptWeightedDphiStrip = reco::tau::pt_weighted_dphi_strip(*tau, tauDecayMode);
float ptWeightedDrSignal = reco::tau::pt_weighted_dr_signal(*tau, tauDecayMode);
float ptWeightedDrIsolation = reco::tau::pt_weighted_dr_iso(*tau, tauDecayMode);
float leadingTrackChi2 = reco::tau::lead_track_chi2(*tau);
float eRatio = reco::tau::eratio(*tau);

// Difference between measured and maximally allowed Gottfried-Jackson angle
float gjAngleDiff = -999;
Expand Down Expand Up @@ -359,3 +360,5 @@ void PFRecoTauDiscriminationByMVAIsolationRun2::endEvent(edm::Event& evt)
}

DEFINE_FWK_MODULE(PFRecoTauDiscriminationByMVAIsolationRun2);

}} //namespace
99 changes: 33 additions & 66 deletions RecoTauTag/RecoTau/src/PFRecoTauClusterVariables.cc
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,10 @@ namespace {
}
/// decide if photon candidate is inside the cone to be associated to the tau signal
bool isInside(float photon_pt, float deta, float dphi) {
const double stripEtaAssociationDistance_0p95_p0 = 0.197077;
const double stripEtaAssociationDistance_0p95_p1 = 0.658701;
const double stripPhiAssociationDistance_0p95_p0 = 0.352476;
const double stripPhiAssociationDistance_0p95_p1 = 0.707716;
constexpr double stripEtaAssociationDistance_0p95_p0 = 0.197077;
constexpr double stripEtaAssociationDistance_0p95_p1 = 0.658701;
constexpr double stripPhiAssociationDistance_0p95_p0 = 0.352476;
constexpr double stripPhiAssociationDistance_0p95_p1 = 0.707716;
if(photon_pt==0){
return false;
} if((dphi<0.3 && dphi<std::max(0.05, stripPhiAssociationDistance_0p95_p0*std::pow(photon_pt, -stripPhiAssociationDistance_0p95_p1))) && (deta<0.15 && deta<std::max(0.05, stripEtaAssociationDistance_0p95_p0*std::pow(photon_pt, -stripEtaAssociationDistance_0p95_p1)))){
Expand All @@ -30,9 +30,9 @@ namespace {
}
}

namespace reco { namespace tau { namespace mva {
namespace reco { namespace tau {
/// return chi2 of the leading track ==> deprecated? <==
float tau_leadTrackChi2(const reco::PFTau& tau) {
float lead_track_chi2(const reco::PFTau& tau) {
float LeadingTracknormalizedChi2 = 0;
const reco::PFCandidatePtr& leadingPFCharged = tau.leadPFChargedHadrCand() ;
if (leadingPFCharged.isNonnull()) {
Expand All @@ -44,30 +44,27 @@ namespace reco { namespace tau { namespace mva {
return LeadingTracknormalizedChi2;
}
/// return ratio of energy in ECAL over sum of energy in ECAL and HCAL
float tau_Eratio(const reco::PFTau& tau) {
std::vector<reco::PFCandidatePtr> constsignal = tau.signalPFCands();
float EcalEnInSignalPFCands = 0;
float HcalEnInSignalPFCands = 0;
typedef std::vector <reco::PFCandidatePtr>::iterator constituents_iterator;
for(constituents_iterator it=constsignal.begin(); it != constsignal.end(); ++it) {
reco::PFCandidatePtr & icand = *it;
EcalEnInSignalPFCands += icand -> ecalEnergy();
HcalEnInSignalPFCands += icand -> hcalEnergy();
float eratio(const reco::PFTau& tau) {
float ecal_en_in_signal_pf_cands = 0;
float hcal_en_in_signal_pf_cands = 0;
for (const auto& signal_cand : tau.signalPFCands()) {
ecal_en_in_signal_pf_cands += signal_cand->ecalEnergy();
hcal_en_in_signal_pf_cands += signal_cand->hcalEnergy();
}
float total = EcalEnInSignalPFCands + HcalEnInSignalPFCands;
float total = ecal_en_in_signal_pf_cands + hcal_en_in_signal_pf_cands;
if(total==0){
return -1;
}
return EcalEnInSignalPFCands/total;
return ecal_en_in_signal_pf_cands/total;
}
float tau_Eratio(const pat::Tau& tau) {
float EcalEnInSignalCands = tau.ecalEnergy();
float HcalEnInSignalCands = tau.hcalEnergy();
float total = EcalEnInSignalCands + HcalEnInSignalCands;
float eratio(const pat::Tau& tau) {
float ecal_en_in_signal_cands = tau.ecalEnergy();
float hcal_en_in_signal_cands = tau.hcalEnergy();
float total = ecal_en_in_signal_cands + hcal_en_in_signal_cands;
if(total == 0){
return -1;
}
return EcalEnInSignalCands/total;
return ecal_en_in_signal_cands/total;
}
/// return sum of pt weighted values of distance to tau candidate for all pf photon candidates,
/// which are associated to signal; depending on var the distance is in 0=:dr, 1=:deta, 2=:dphi
Expand Down Expand Up @@ -159,52 +156,20 @@ namespace reco { namespace tau { namespace mva {
}
return 0.;
}
/// return sum of pt weighted values of dr relative to tau candidate for all pf photon candidates,
/// which are associated to signal
float tau_pt_weighted_dr_signal(const reco::PFTau& tau, int dm) {
return pt_weighted_dx(tau, 0, 0, dm);
}
float tau_pt_weighted_dr_signal(const pat::Tau& tau, int dm) {
return pt_weighted_dx(tau, 0, 0, dm);
}
/// return sum of pt weighted values of deta relative to tau candidate for all pf photon candidates,
/// which are associated to signal
float tau_pt_weighted_deta_strip(const reco::PFTau& tau, int dm) {
return pt_weighted_dx(tau, dm==10 ? 2 : 1, 1, dm);
}
float tau_pt_weighted_deta_strip(const pat::Tau& tau, int dm) {
return pt_weighted_dx(tau, dm==10 ? 2 : 1, 1, dm);
}
/// return sum of pt weighted values of dphi relative to tau candidate for all pf photon candidates,
/// which are associated to signal
float tau_pt_weighted_dphi_strip(const reco::PFTau& tau, int dm) {
return pt_weighted_dx(tau, dm==10 ? 2 : 1, 2, dm);
}
float tau_pt_weighted_dphi_strip(const pat::Tau& tau, int dm) {
return pt_weighted_dx(tau, dm==10 ? 2 : 1, 2, dm);
}
/// return sum of pt weighted values of dr relative to tau candidate for all pf photon candidates,
/// which are inside an isolation conde but not associated to signal
float tau_pt_weighted_dr_iso(const reco::PFTau& tau, int dm) {
return pt_weighted_dx(tau, 2, 0, dm);
}
float tau_pt_weighted_dr_iso(const pat::Tau& tau, int dm) {
return pt_weighted_dx(tau, 2, 0, dm);
}
/// return total number of pf photon candidates with pT>500 MeV, which are associated to signal
unsigned int tau_n_photons_total(const reco::PFTau& tau) {
unsigned int n_photons_total(const reco::PFTau& tau) {
unsigned int n_photons = 0;
for (auto& cand : tau.signalPFGammaCands()) {
if ((float)cand->pt() > 0.5)
if (cand->pt() > 0.5)
++n_photons;
}
for (auto& cand : tau.isolationPFGammaCands()) {
if ((float)cand->pt() > 0.5)
if (cand->pt() > 0.5)
++n_photons;
}
return n_photons;
}
unsigned int tau_n_photons_total(const pat::Tau& tau) {
unsigned int n_photons_total(const pat::Tau& tau) {
unsigned int n_photons = 0;
for (auto& cand : tau.signalGammaCands()) {
if (cand->pt() > 0.5)
Expand All @@ -217,7 +182,9 @@ namespace reco { namespace tau { namespace mva {
return n_photons;
}

bool fillMVAInputs(float* mvaInput, const pat::Tau& tau, int mvaOpt, const std::string nameCharged, const std::string nameNeutral, const std::string namePu, const std::string nameOutside, const std::string nameFootprint)
bool fillIsoMVARun2Inputs(float* mvaInput, const pat::Tau& tau, int mvaOpt, const std::string& nameCharged,
const std::string& nameNeutral, const std::string& namePu,
const std::string& nameOutside, const std::string& nameFootprint)
{
int tauDecayMode = tau.decayMode();

Expand All @@ -241,14 +208,14 @@ namespace reco { namespace tau { namespace mva {

// --- The following 5 variables differ slightly between AOD & MiniAOD
// because they are recomputed using packedCandidates saved in the tau
float nPhoton = (float)reco::tau::mva::tau_n_photons_total(tau);
float ptWeightedDetaStrip = reco::tau::mva::tau_pt_weighted_deta_strip(tau, tauDecayMode);
float ptWeightedDphiStrip = reco::tau::mva::tau_pt_weighted_dphi_strip(tau, tauDecayMode);
float ptWeightedDrSignal = reco::tau::mva::tau_pt_weighted_dr_signal(tau, tauDecayMode);
float ptWeightedDrIsolation = reco::tau::mva::tau_pt_weighted_dr_iso(tau, tauDecayMode);
float nPhoton = reco::tau::n_photons_total(tau);
float ptWeightedDetaStrip = reco::tau::pt_weighted_deta_strip(tau, tauDecayMode);
float ptWeightedDphiStrip = reco::tau::pt_weighted_dphi_strip(tau, tauDecayMode);
float ptWeightedDrSignal = reco::tau::pt_weighted_dr_signal(tau, tauDecayMode);
float ptWeightedDrIsolation = reco::tau::pt_weighted_dr_iso(tau, tauDecayMode);
// ---
float leadingTrackChi2 = tau.leadingTrackNormChi2();
float eRatio = reco::tau::mva::tau_Eratio(tau);
float eRatio = reco::tau::eratio(tau);

// Difference between measured and maximally allowed Gottfried-Jackson angle
float gjAngleDiff = -999;
Expand Down Expand Up @@ -364,4 +331,4 @@ namespace reco { namespace tau { namespace mva {
return false;
}

}}} // namespaces
}} // namespaces
Loading

0 comments on commit 1ad3da6

Please sign in to comment.