Skip to content

Commit

Permalink
Merge pull request #109 from MRD2F/CMSSW_10_4_0_pre2_DNN
Browse files Browse the repository at this point in the history
Applied comments
  • Loading branch information
mbluj committed Nov 23, 2018
2 parents 3a721da + 3a40634 commit 6132127
Show file tree
Hide file tree
Showing 2 changed files with 80 additions and 63 deletions.
121 changes: 68 additions & 53 deletions RecoTauTag/RecoTau/plugins/DPFIsolation.cc
Expand Up @@ -22,10 +22,10 @@ inline int getPFCandidateIndex(const edm::Handle<pat::PackedCandidateCollection>

class DPFIsolation : public deep_tau::DeepTauBase {
public:
static OutputCollection& GetOutputs()
static const OutputCollection& GetOutputs()
{
static size_t tau_index = 0;
static OutputCollection outputs_ = { { "VSall", Output({tau_index}, {}) } };
const size_t tau_index = 0;
static const OutputCollection outputs_ = { { "VSall", Output({tau_index}, {}) } };
return outputs_;
};

Expand Down Expand Up @@ -109,10 +109,34 @@ class DPFIsolation : public deep_tau::DeepTauBase {

bool pfCandIsBarrel;

// These variables define ranges further used for standardization
static constexpr float pfCandPt_max = 500.f;
static constexpr float pfCandPz_max = 1000.f;
static constexpr float pfCandPtRel_max = 1.f;
static constexpr float pfCandPzRel_max = 100.f;
static constexpr float pfCandPtRelPtRel_max = 1.f;
static constexpr float pfCandD0_max = 5.f;
static constexpr float pfCandDz_max = 5.f;
static constexpr float pfCandDVx_y_z_1_max = 0.05f;
static constexpr float pfCandD_1_max = 0.1f;
static constexpr float pfCandD0_z_Err_max = 1.f;
static constexpr float pfCandDzSig_max = 3.f;
static constexpr float pfCandD0Sig_max = 1.f;
static constexpr float pfCandDr_max = 0.5f;
static constexpr float pfCandEta_max = 2.75f;
static constexpr float pfCandDEta_max = 0.5f;
static constexpr float pfCandDPhi_max = 0.5f;
static constexpr float pfCandPixHits_max = 7.f;
static constexpr float pfCandHits_max = 30.f;

for(size_t tau_index = 0; tau_index < taus->size(); tau_index++) {
pat::Tau tau = taus->at(tau_index);
bool isGoodTau = false;
if(tau.pt() >= 30 && std::abs(tau.eta()) < 2.3 && tau.isTauIDAvailable("againstMuonLoose3") &&
const float lepRecoPt = tau.pt();
const float lepRecoPz = std::abs(tau.pz());
const float lepRecoPhi = tau.phi();

if(lepRecoPt >= 30 && std::abs(tau.eta()) < 2.3 && tau.isTauIDAvailable("againstMuonLoose3") &&
tau.isTauIDAvailable("againstElectronVLooseMVA6")) {
isGoodTau = (tau.tauID("againstElectronVLooseMVA6") && tau.tauID("againstMuonLoose3") );
}
Expand All @@ -127,9 +151,6 @@ class DPFIsolation : public deep_tau::DeepTauBase {
for(const auto c : tau.signalCands())
signalCandidateInds.push_back(getPFCandidateIndex(pfcands,c));

float lepRecoPt = tau.pt();
float lepRecoPz = std::abs(tau.pz());

// Use of setZero results in warnings in eigen library during compilation.
//tensor.flat<float>().setZero();
const unsigned n_inputs = getNumberOfParticles(graphVersion) * GetNumberOfFeatures(graphVersion);
Expand Down Expand Up @@ -159,11 +180,11 @@ class DPFIsolation : public deep_tau::DeepTauBase {

pfCandDr = deltaR_tau_p;
pfCandDEta = std::abs(tau.eta() - p.eta());
pfCandDPhi = std::abs(deltaPhi(tau.phi(), p.phi()));
pfCandDPhi = std::abs(deltaPhi(lepRecoPhi, p.phi()));
pfCandEta = p.eta();
pfCandIsBarrel = (std::abs(pfCandEta) < 1.4);
pfCandPz = std::abs(std::sinh(pfCandEta)*pfCandPt);
pfCandPzRel = std::abs(std::sinh(pfCandEta)*pfCandPt)/lepRecoPz;
pfCandPzRel = pfCandPz/lepRecoPz;
pfCandPdgID = std::abs(p.pdgId());
pfCandCharge = p.charge();
pfCandDVx_1 = p.vx() - pvx;
Expand Down Expand Up @@ -213,58 +234,52 @@ class DPFIsolation : public deep_tau::DeepTauBase {

pfCandTauIndMatch = pfCandTauIndMatch_temp;
pfCandPtRelPtRel = pfCandPtRel*pfCandPtRel;
if (pfCandPt > 500) pfCandPt = 500.;
pfCandPt = pfCandPt/500.;

if (pfCandPz > 1000) pfCandPz = 1000.;
pfCandPz = pfCandPz/1000.;

if ((pfCandPtRel) > 1 ) pfCandPtRel = 1.;
if ((pfCandPzRel) > 100 ) pfCandPzRel = 100.;
pfCandPzRel = pfCandPzRel/100.;
pfCandDr = pfCandDr/.5;
pfCandEta = pfCandEta/2.75;
pfCandDEta = pfCandDEta/.5;
pfCandDPhi = pfCandDPhi/.5;
pfCandPixHits = pfCandPixHits/7.;
pfCandHits = pfCandHits/30.;

if (pfCandPtRelPtRel > 1) pfCandPtRelPtRel = 1;

if (pfCandD0 > 5.) pfCandD0 = 5.;
if (pfCandD0 < -5.) pfCandD0 = -5.;
pfCandD0 = pfCandD0/5.;

if (pfCandDz > 5.) pfCandDz = 5.;
if (pfCandDz < -5.) pfCandDz = -5.;
pfCandDz = pfCandDz/5.;

if (pfCandD0Err > 1.) pfCandD0Err = 1.;
if (pfCandDzErr > 1.) pfCandDzErr = 1.;
if (pfCandDzSig > 3) pfCandDzSig = 3.;
pfCandDzSig = pfCandDzSig/3.;

if (pfCandD0Sig > 1) pfCandD0Sig = 1.;
pfCandPt = std::min(pfCandPt, pfCandPt_max);
pfCandPt = pfCandPt/pfCandPt_max;

pfCandPz = std::min(pfCandPz, pfCandPz_max);
pfCandPz = pfCandPz/pfCandPz_max;

pfCandPtRel = std::min(pfCandPtRel, pfCandPtRel_max);
pfCandPzRel = std::min(pfCandPzRel, pfCandPzRel_max);
pfCandPzRel = pfCandPzRel/pfCandPzRel_max;
pfCandDr = pfCandDr/pfCandDr_max;
pfCandEta = pfCandEta/pfCandEta_max;
pfCandDEta = pfCandDEta/pfCandDEta_max;
pfCandDPhi = pfCandDPhi/pfCandDPhi_max;
pfCandPixHits = pfCandPixHits/pfCandPixHits_max;
pfCandHits = pfCandHits/pfCandHits_max;

pfCandPtRelPtRel = std::min(pfCandPtRelPtRel, pfCandPtRelPtRel_max);

pfCandD0 = std::clamp(pfCandD0, -pfCandD0_max, pfCandD0_max);
pfCandD0 = pfCandD0/pfCandD0_max;

pfCandDz = std::clamp(pfCandDz, -pfCandDz_max, pfCandDz_max);
pfCandDz = pfCandDz/pfCandDz_max;

pfCandD0Err = std::min(pfCandD0Err, pfCandD0_z_Err_max);
pfCandDzErr = std::min(pfCandDzErr, pfCandD0_z_Err_max);
pfCandDzSig = std::min(pfCandDzSig, pfCandDzSig_max);
pfCandDzSig = pfCandDzSig/pfCandDzSig_max;

pfCandD0Sig = std::min(pfCandD0Sig, pfCandD0Sig_max);
pfCandD0D0 = pfCandD0*pfCandD0;
pfCandDzDz = pfCandDz*pfCandDz;
pfCandD0Dz = pfCandD0*pfCandDz;
pfCandD0Dphi = pfCandD0*pfCandDPhi;

if (pfCandDVx_1 > .05) pfCandDVx_1 = .05;
if (pfCandDVx_1 < -.05) pfCandDVx_1 = -.05;
pfCandDVx_1 = pfCandDVx_1/.05;
pfCandDVx_1 = std::clamp(pfCandDVx_1, -pfCandDVx_y_z_1_max, pfCandDVx_y_z_1_max);
pfCandDVx_1 = pfCandDVx_1/pfCandDVx_y_z_1_max;

if (pfCandDVy_1 > 0.05) pfCandDVy_1 = 0.05;
if (pfCandDVy_1 < -0.05) pfCandDVy_1 = -0.05;
pfCandDVy_1 = pfCandDVy_1/0.05;
pfCandDVy_1 = std::clamp(pfCandDVy_1, -pfCandDVx_y_z_1_max, pfCandDVx_y_z_1_max);
pfCandDVy_1 = pfCandDVy_1/pfCandDVx_y_z_1_max;

if (pfCandDVz_1 > 0.05) pfCandDVz_1 = 0.05;
if (pfCandDVz_1 < -0.05) pfCandDVz_1= -0.05;
pfCandDVz_1 = pfCandDVz_1/0.05;
pfCandDVz_1 = std::clamp(pfCandDVz_1, -pfCandDVx_y_z_1_max, pfCandDVx_y_z_1_max);
pfCandDVz_1 = pfCandDVz_1/pfCandDVx_y_z_1_max;

if (pfCandD_1 > 0.1) pfCandD_1 = 0.1;
if (pfCandD_1 < -0.1) pfCandD_1 = -0.1;
pfCandD_1 = pfCandD_1/.1;
pfCandD_1 = std::clamp(pfCandD_1, -pfCandD_1_max, pfCandD_1_max);
pfCandD_1 = pfCandD_1/ pfCandD_1_max;

if (graphVersion == 0) {
tensor.tensor<float,3>()( 0, 60-1-iPF, 0) = pfCandPt;
Expand Down
22 changes: 12 additions & 10 deletions RecoTauTag/RecoTau/plugins/DeepTauId.cc
Expand Up @@ -305,7 +305,7 @@ class DeepTauId : public deep_tau::DeepTauBase {
tensorflow::Tensor predictions(tensorflow::DT_FLOAT, { static_cast<int>(taus->size()),
dnn_inputs_2017v1::NumberOfOutputs});
for(size_t tau_index = 0; tau_index < taus->size(); ++tau_index) {
const tensorflow::Tensor& inputs = CreateInputs<dnn_inputs_2017v1>(taus->at(tau_index), *electrons, *muons);
const tensorflow::Tensor& inputs = createInputs<dnn_inputs_2017v1>(taus->at(tau_index), *electrons, *muons);
std::vector<tensorflow::Tensor> pred_vector;
tensorflow::run(&(cache_->getSession()), { { input_layer, inputs } }, { output_layer }, &pred_vector);
for(int k = 0; k < dnn_inputs_2017v1::NumberOfOutputs; ++k)
Expand All @@ -315,19 +315,19 @@ class DeepTauId : public deep_tau::DeepTauBase {
}

template<typename dnn>
tensorflow::Tensor CreateInputs(const TauType& tau, const ElectronCollection& electrons,
tensorflow::Tensor createInputs(const TauType& tau, const ElectronCollection& electrons,
const MuonCollection& muons) const
{
static constexpr bool check_all_set = false;
static constexpr float magic_number = -42;
static constexpr float default_value_for_set_check = -42;

tensorflow::Tensor inputs(tensorflow::DT_FLOAT, { 1, dnn_inputs_2017v1::NumberOfInputs});
const auto& get = [&](int var_index) -> float& { return inputs.matrix<float>()(0, var_index); };
auto leadChargedHadrCand = dynamic_cast<const pat::PackedCandidate*>(tau.leadChargedHadrCand().get());

if(check_all_set) {
for(int var_index = 0; var_index < dnn::NumberOfInputs; ++var_index) {
get(var_index) = magic_number;
get(var_index) = default_value_for_set_check;
}
}

Expand Down Expand Up @@ -474,23 +474,23 @@ class DeepTauId : public deep_tau::DeepTauBase {
get(dnn::signalGammaCands_nTotal_outerSigCone));

LorentzVectorXYZ isolationChargedHadrCands_sum;
ProcessIsolationPFComponents(tau, tau.isolationChargedHadrCands(), isolationChargedHadrCands_sum,
processIsolationPFComponents(tau, tau.isolationChargedHadrCands(), isolationChargedHadrCands_sum,
get(dnn::isolationChargedHadrCands_sum_pt),
get(dnn::isolationChargedHadrCands_sum_dEta),
get(dnn::isolationChargedHadrCands_sum_dPhi),
get(dnn::isolationChargedHadrCands_sum_mass),
get(dnn::isolationChargedHadrCands_nTotal));

LorentzVectorXYZ isolationNeutrHadrCands_sum;
ProcessIsolationPFComponents(tau, tau.isolationNeutrHadrCands(), isolationNeutrHadrCands_sum,
processIsolationPFComponents(tau, tau.isolationNeutrHadrCands(), isolationNeutrHadrCands_sum,
get(dnn::isolationNeutrHadrCands_sum_pt),
get(dnn::isolationNeutrHadrCands_sum_dEta),
get(dnn::isolationNeutrHadrCands_sum_dPhi),
get(dnn::isolationNeutrHadrCands_sum_mass),
get(dnn::isolationNeutrHadrCands_nTotal));

LorentzVectorXYZ isolationGammaCands_sum;
ProcessIsolationPFComponents(tau, tau.isolationGammaCands(), isolationGammaCands_sum,
processIsolationPFComponents(tau, tau.isolationGammaCands(), isolationGammaCands_sum,
get(dnn::isolationGammaCands_sum_pt),
get(dnn::isolationGammaCands_sum_dEta),
get(dnn::isolationGammaCands_sum_dPhi),
Expand All @@ -501,7 +501,7 @@ class DeepTauId : public deep_tau::DeepTauBase {

if(check_all_set) {
for(int var_index = 0; var_index < dnn::NumberOfInputs; ++var_index) {
if(get(var_index) == magic_number)
if(get(var_index) == default_value_for_set_check)
throw cms::Exception("DeepTauId: variable with index = ") << var_index << " is not set.";
}
}
Expand Down Expand Up @@ -564,7 +564,7 @@ class DeepTauId : public deep_tau::DeepTauBase {
}

template<typename CandidateCollection>
static void ProcessIsolationPFComponents(const pat::Tau& tau, const CandidateCollection& candidates,
static void processIsolationPFComponents(const pat::Tau& tau, const CandidateCollection& candidates,
LorentzVectorXYZ& p4, float& pt, float& d_eta, float& d_phi, float& m,
float& n)
{
Expand All @@ -584,7 +584,9 @@ class DeepTauId : public deep_tau::DeepTauBase {

static double getInnerSignalConeRadius(double pt)
{
return std::max(.05, std::min(.1, 3./std::max(1., pt)));
static constexpr double min_pt = 30., min_radius = 0.05, cone_opening_coef = 3.;
// This is equivalent of the original formula (std::max(std::min(0.1, 3.0/pt), 0.05)
return std::max(cone_opening_coef / std::max(pt, min_pt), min_radius);
}

// Copied from https://github.com/cms-sw/cmssw/blob/CMSSW_9_4_X/RecoTauTag/RecoTau/plugins/PATTauDiscriminationByMVAIsolationRun2.cc#L218
Expand Down

0 comments on commit 6132127

Please sign in to comment.