From 34a79316ad613249aa3b8166c1b73fdc5513f6b9 Mon Sep 17 00:00:00 2001 From: Joosep Pata Date: Fri, 6 Nov 2020 14:46:35 +0200 Subject: [PATCH] format --- .../PFProducer/interface/MLPFModel.h | 84 +++--- .../PFProducer/plugins/MLPFProducerSonic.cc | 13 +- RecoParticleFlow/PFProducer/src/MLPFModel.cpp | 260 +++++++++--------- 3 files changed, 178 insertions(+), 179 deletions(-) diff --git a/RecoParticleFlow/PFProducer/interface/MLPFModel.h b/RecoParticleFlow/PFProducer/interface/MLPFModel.h index e08c49edae47c..d5be1839dd22f 100644 --- a/RecoParticleFlow/PFProducer/interface/MLPFModel.h +++ b/RecoParticleFlow/PFProducer/interface/MLPFModel.h @@ -12,54 +12,54 @@ #include "DataFormats/ParticleFlowCandidate/interface/PFCandidate.h" namespace reco::mlpf { - //The model takes the following number of features for each input PFElement - static constexpr unsigned int NUM_ELEMENT_FEATURES = 15; + //The model takes the following number of features for each input PFElement + static constexpr unsigned int NUM_ELEMENT_FEATURES = 15; - //these are defined at model creation time and set the random LSH codebook size - static constexpr int NUM_MAX_ELEMENTS_BATCH = 20000; - static constexpr int LSH_BIN_SIZE = 100; + //these are defined at model creation time and set the random LSH codebook size + static constexpr int NUM_MAX_ELEMENTS_BATCH = 20000; + static constexpr int LSH_BIN_SIZE = 100; - //In CPU mode, we only want to evaluate each event separately - static constexpr int BATCH_SIZE = 1; + //In CPU mode, we only want to evaluate each event separately + static constexpr int BATCH_SIZE = 1; - //The model has 12 outputs for each particle: - // out[0-7]: particle classification logits - // out[8]: regressed eta - // out[9]: regressed phi - // out[10]: regressed energy - // out[11]: regressed charge logit - static constexpr unsigned int NUM_OUTPUTS = 12; - static constexpr unsigned int NUM_CLASS = 7; - static constexpr unsigned int IDX_ETA = 8; - static constexpr unsigned int IDX_PHI = 9; - static constexpr unsigned int IDX_ENERGY = 10; - static constexpr unsigned int IDX_CHARGE = 11; + //The model has 12 outputs for each particle: + // out[0-7]: particle classification logits + // out[8]: regressed eta + // out[9]: regressed phi + // out[10]: regressed energy + // out[11]: regressed charge logit + static constexpr unsigned int NUM_OUTPUTS = 12; + static constexpr unsigned int NUM_CLASS = 7; + static constexpr unsigned int IDX_ETA = 8; + static constexpr unsigned int IDX_PHI = 9; + static constexpr unsigned int IDX_ENERGY = 10; + static constexpr unsigned int IDX_CHARGE = 11; - //index [0, N_pdgids) -> PDGID - //this maps the absolute values of the predicted PDGIDs to an array of ascending indices - static const std::vector pdgid_encoding = {0, 1, 2, 11, 13, 22, 130, 211}; + //index [0, N_pdgids) -> PDGID + //this maps the absolute values of the predicted PDGIDs to an array of ascending indices + static const std::vector pdgid_encoding = {0, 1, 2, 11, 13, 22, 130, 211}; - //PFElement::type -> index [0, N_types) - //this maps the type of the PFElement to an ascending index that is used by the model to distinguish between different elements - static const std::map elem_type_encoding = { - {0, 0}, - {1, 1}, - {2, 2}, - {3, 3}, - {4, 4}, - {5, 5}, - {6, 6}, - {7, 7}, - {8, 8}, - {9, 9}, - {10, 10}, - {11, 11}, - }; + //PFElement::type -> index [0, N_types) + //this maps the type of the PFElement to an ascending index that is used by the model to distinguish between different elements + static const std::map elem_type_encoding = { + {0, 0}, + {1, 1}, + {2, 2}, + {3, 3}, + {4, 4}, + {5, 5}, + {6, 6}, + {7, 7}, + {8, 8}, + {9, 9}, + {10, 10}, + {11, 11}, + }; - std::array get_element_properties(const reco::PFBlockElement& orig); - float normalize(float in); + std::array get_element_properties(const reco::PFBlockElement& orig); + float normalize(float in); - int arg_max(std::vector const& vec); -}; + int arg_max(std::vector const& vec); +}; // namespace reco::mlpf #endif \ No newline at end of file diff --git a/RecoParticleFlow/PFProducer/plugins/MLPFProducerSonic.cc b/RecoParticleFlow/PFProducer/plugins/MLPFProducerSonic.cc index c8dd85c95791e..d5819b1418a07 100644 --- a/RecoParticleFlow/PFProducer/plugins/MLPFProducerSonic.cc +++ b/RecoParticleFlow/PFProducer/plugins/MLPFProducerSonic.cc @@ -36,7 +36,7 @@ // git clone https://github.com/jpata/mlpf-models.git // mv mlpf-models/triton/models ./ // singularity exec --nv docker://nvcr.io/nvidia/tritonserver:20.10-py3 tritonserver --model-repository models --log-verbose=1 --log-error=1 --log-info=1 -// +// // process.mlpfproducergpu = cms.EDProducer("MLPFProducerSonicTriton", // Client = cms.PSet( // mode = cms.string("Sync"), @@ -51,7 +51,6 @@ // ) // ) - class MLPFProducerSonicTriton : public SonicEDProducer { public: //needed because base class has dependent scope @@ -136,24 +135,24 @@ class MLPFProducerSonicTriton : public SonicEDProducer { //get the coefficients in the output corresponding to the class probabilities (raw logits) std::vector pred_id_logits; for (unsigned int idx_id = 0; idx_id <= NUM_CLASS; idx_id++) { - pred_id_logits.push_back(out_data[0][ielem*NUM_OUTPUTS + idx_id]); + pred_id_logits.push_back(out_data[0][ielem * NUM_OUTPUTS + idx_id]); } //get the most probable class PDGID int pred_pid = pdgid_encoding[arg_max(pred_id_logits)]; //get the predicted momentum components - float pred_eta = out_data[0][ielem*NUM_OUTPUTS + IDX_ETA]; - float pred_phi = out_data[0][ielem*NUM_OUTPUTS + IDX_PHI]; + float pred_eta = out_data[0][ielem * NUM_OUTPUTS + IDX_ETA]; + float pred_phi = out_data[0][ielem * NUM_OUTPUTS + IDX_PHI]; //put phi into a modular range pred_phi = TMath::ATan2(TMath::Sin(pred_phi), TMath::Cos(pred_phi)); - float pred_e = out_data[0][ielem*NUM_OUTPUTS + IDX_ENERGY]; + float pred_e = out_data[0][ielem * NUM_OUTPUTS + IDX_ENERGY]; //currently, set the pT from a massless approximation. //later versions of the model may predict predict both the energy and pT of the particle float pred_pt = pred_e / cosh(pred_eta); - float pred_charge = out_data[0][ielem*NUM_OUTPUTS + IDX_CHARGE]; + float pred_charge = out_data[0][ielem * NUM_OUTPUTS + IDX_CHARGE]; //a particle was predicted for this PFElement if (pred_pid != 0) { diff --git a/RecoParticleFlow/PFProducer/src/MLPFModel.cpp b/RecoParticleFlow/PFProducer/src/MLPFModel.cpp index 16bf2a5e4ebeb..daab9d6610c59 100644 --- a/RecoParticleFlow/PFProducer/src/MLPFModel.cpp +++ b/RecoParticleFlow/PFProducer/src/MLPFModel.cpp @@ -2,69 +2,45 @@ namespace reco::mlpf { -//Prepares the input array of floats for a single PFElement -std::array get_element_properties(const reco::PFBlockElement& orig) { - const auto type = orig.type(); - float pt = 0.0; - //these are placeholders for the the future - [[maybe_unused]] float deltap = 0.0; - [[maybe_unused]] float sigmadeltap = 0.0; - [[maybe_unused]] float px = 0.0; - [[maybe_unused]] float py = 0.0; - [[maybe_unused]] float pz = 0.0; - float eta = 0.0; - float phi = 0.0; - float energy = 0.0; - float trajpoint = 0.0; - float eta_ecal = 0.0; - float phi_ecal = 0.0; - float eta_hcal = 0.0; - float phi_hcal = 0.0; - float charge = 0; - float layer = 0; - float depth = 0; - float muon_dt_hits = 0.0; - float muon_csc_hits = 0.0; + //Prepares the input array of floats for a single PFElement + std::array get_element_properties(const reco::PFBlockElement& orig) { + const auto type = orig.type(); + float pt = 0.0; + //these are placeholders for the the future + [[maybe_unused]] float deltap = 0.0; + [[maybe_unused]] float sigmadeltap = 0.0; + [[maybe_unused]] float px = 0.0; + [[maybe_unused]] float py = 0.0; + [[maybe_unused]] float pz = 0.0; + float eta = 0.0; + float phi = 0.0; + float energy = 0.0; + float trajpoint = 0.0; + float eta_ecal = 0.0; + float phi_ecal = 0.0; + float eta_hcal = 0.0; + float phi_hcal = 0.0; + float charge = 0; + float layer = 0; + float depth = 0; + float muon_dt_hits = 0.0; + float muon_csc_hits = 0.0; - if (type == reco::PFBlockElement::TRACK) { - const auto& matched_pftrack = orig.trackRefPF(); - if (matched_pftrack.isNonnull()) { - const auto& atECAL = matched_pftrack->extrapolatedPoint(reco::PFTrajectoryPoint::ECALShowerMax); - const auto& atHCAL = matched_pftrack->extrapolatedPoint(reco::PFTrajectoryPoint::HCALEntrance); - if (atHCAL.isValid()) { - eta_hcal = atHCAL.positionREP().eta(); - phi_hcal = atHCAL.positionREP().phi(); + if (type == reco::PFBlockElement::TRACK) { + const auto& matched_pftrack = orig.trackRefPF(); + if (matched_pftrack.isNonnull()) { + const auto& atECAL = matched_pftrack->extrapolatedPoint(reco::PFTrajectoryPoint::ECALShowerMax); + const auto& atHCAL = matched_pftrack->extrapolatedPoint(reco::PFTrajectoryPoint::HCALEntrance); + if (atHCAL.isValid()) { + eta_hcal = atHCAL.positionREP().eta(); + phi_hcal = atHCAL.positionREP().phi(); + } + if (atECAL.isValid()) { + eta_ecal = atECAL.positionREP().eta(); + phi_ecal = atECAL.positionREP().phi(); + } } - if (atECAL.isValid()) { - eta_ecal = atECAL.positionREP().eta(); - phi_ecal = atECAL.positionREP().phi(); - } - } - const auto& ref = ((const reco::PFBlockElementTrack*)&orig)->trackRef(); - pt = ref->pt(); - px = ref->px(); - py = ref->py(); - pz = ref->pz(); - eta = ref->eta(); - phi = ref->phi(); - energy = ref->pt() * cosh(ref->eta()); - charge = ref->charge(); - - reco::MuonRef muonRef = orig.muonRef(); - if (muonRef.isNonnull()) { - reco::TrackRef standAloneMu = muonRef->standAloneMuon(); - if (standAloneMu.isNonnull()) { - muon_dt_hits = standAloneMu->hitPattern().numberOfValidMuonDTHits(); - muon_csc_hits = standAloneMu->hitPattern().numberOfValidMuonCSCHits(); - } - } - - } else if (type == reco::PFBlockElement::BREM) { - const auto* orig2 = (const reco::PFBlockElementBrem*)&orig; - const auto& ref = orig2->GsftrackRef(); - if (ref.isNonnull()) { - deltap = orig2->DeltaP(); - sigmadeltap = orig2->SigmaDeltaP(); + const auto& ref = ((const reco::PFBlockElementTrack*)&orig)->trackRef(); pt = ref->pt(); px = ref->px(); py = ref->py(); @@ -72,80 +48,104 @@ std::array get_element_properties(const reco::PFBlo eta = ref->eta(); phi = ref->phi(); energy = ref->pt() * cosh(ref->eta()); - trajpoint = orig2->indTrajPoint(); charge = ref->charge(); + + reco::MuonRef muonRef = orig.muonRef(); + if (muonRef.isNonnull()) { + reco::TrackRef standAloneMu = muonRef->standAloneMuon(); + if (standAloneMu.isNonnull()) { + muon_dt_hits = standAloneMu->hitPattern().numberOfValidMuonDTHits(); + muon_csc_hits = standAloneMu->hitPattern().numberOfValidMuonCSCHits(); + } + } + + } else if (type == reco::PFBlockElement::BREM) { + const auto* orig2 = (const reco::PFBlockElementBrem*)&orig; + const auto& ref = orig2->GsftrackRef(); + if (ref.isNonnull()) { + deltap = orig2->DeltaP(); + sigmadeltap = orig2->SigmaDeltaP(); + pt = ref->pt(); + px = ref->px(); + py = ref->py(); + pz = ref->pz(); + eta = ref->eta(); + phi = ref->phi(); + energy = ref->pt() * cosh(ref->eta()); + trajpoint = orig2->indTrajPoint(); + charge = ref->charge(); + } + } else if (type == reco::PFBlockElement::GSF) { + //requires to keep GsfPFRecTracks + const auto* orig2 = (const reco::PFBlockElementGsfTrack*)&orig; + pt = orig2->Pin().pt(); + px = orig2->Pin().px(); + py = orig2->Pin().py(); + pz = orig2->Pin().pz(); + eta = orig2->Pin().eta(); + phi = orig2->Pin().phi(); + energy = pt * cosh(eta); + if (!orig2->GsftrackRefPF().isNull()) { + charge = orig2->GsftrackRefPF()->charge(); + } + } else if (type == reco::PFBlockElement::ECAL || type == reco::PFBlockElement::PS1 || + type == reco::PFBlockElement::PS2 || type == reco::PFBlockElement::HCAL || + type == reco::PFBlockElement::HO || type == reco::PFBlockElement::HFHAD || + type == reco::PFBlockElement::HFEM) { + const auto& ref = ((const reco::PFBlockElementCluster*)&orig)->clusterRef(); + if (ref.isNonnull()) { + eta = ref->eta(); + phi = ref->phi(); + px = ref->position().x(); + py = ref->position().y(); + pz = ref->position().z(); + energy = ref->energy(); + layer = ref->layer(); + depth = ref->depth(); + } + } else if (type == reco::PFBlockElement::SC) { + const auto& clref = ((const reco::PFBlockElementSuperCluster*)&orig)->superClusterRef(); + if (clref.isNonnull()) { + eta = clref->eta(); + phi = clref->phi(); + px = clref->position().x(); + py = clref->position().y(); + pz = clref->position().z(); + energy = clref->energy(); + } } - } else if (type == reco::PFBlockElement::GSF) { - //requires to keep GsfPFRecTracks - const auto* orig2 = (const reco::PFBlockElementGsfTrack*)&orig; - pt = orig2->Pin().pt(); - px = orig2->Pin().px(); - py = orig2->Pin().py(); - pz = orig2->Pin().pz(); - eta = orig2->Pin().eta(); - phi = orig2->Pin().phi(); - energy = pt * cosh(eta); - if (!orig2->GsftrackRefPF().isNull()) { - charge = orig2->GsftrackRefPF()->charge(); - } - } else if (type == reco::PFBlockElement::ECAL || type == reco::PFBlockElement::PS1 || - type == reco::PFBlockElement::PS2 || type == reco::PFBlockElement::HCAL || - type == reco::PFBlockElement::HO || type == reco::PFBlockElement::HFHAD || - type == reco::PFBlockElement::HFEM) { - const auto& ref = ((const reco::PFBlockElementCluster*)&orig)->clusterRef(); - if (ref.isNonnull()) { - eta = ref->eta(); - phi = ref->phi(); - px = ref->position().x(); - py = ref->position().y(); - pz = ref->position().z(); - energy = ref->energy(); - layer = ref->layer(); - depth = ref->depth(); - } - } else if (type == reco::PFBlockElement::SC) { - const auto& clref = ((const reco::PFBlockElementSuperCluster*)&orig)->superClusterRef(); - if (clref.isNonnull()) { - eta = clref->eta(); - phi = clref->phi(); - px = clref->position().x(); - py = clref->position().y(); - pz = clref->position().z(); - energy = clref->energy(); - } - } - float typ_idx = static_cast(elem_type_encoding.at(orig.type())); + float typ_idx = static_cast(elem_type_encoding.at(orig.type())); - //Must be the same order as in tf_model.py - return std::array({{typ_idx, - pt, - eta, - phi, - energy, - layer, - depth, - charge, - trajpoint, - eta_ecal, - phi_ecal, - eta_hcal, - phi_hcal, - muon_dt_hits, - muon_csc_hits}}); -} + //Must be the same order as in tf_model.py + return std::array({{typ_idx, + pt, + eta, + phi, + energy, + layer, + depth, + charge, + trajpoint, + eta_ecal, + phi_ecal, + eta_hcal, + phi_hcal, + muon_dt_hits, + muon_csc_hits}}); + } -float normalize(float in) { - if (std::abs(in) > 1e4) { - return 0.0; - } else if (std::isnan(in)) { - return 0.0; + float normalize(float in) { + if (std::abs(in) > 1e4) { + return 0.0; + } else if (std::isnan(in)) { + return 0.0; + } + return in; } - return in; -} -int arg_max(std::vector const& vec) { - return static_cast(std::distance(vec.begin(), max_element(vec.begin(), vec.end()))); -} + int arg_max(std::vector const& vec) { + return static_cast(std::distance(vec.begin(), max_element(vec.begin(), vec.end()))); + } -}; //namespace \ No newline at end of file +}; // namespace reco::mlpf \ No newline at end of file