CMS 3D CMS Logo

MLPFModel.cc
Go to the documentation of this file.
11 
12 namespace reco::mlpf {
13 
14  //Prepares the input array of floats for a single PFElement
15  std::array<float, NUM_ELEMENT_FEATURES> getElementProperties(const reco::PFBlockElement& orig) {
16  const auto type = orig.type();
17  float pt = 0.0;
18  //these are placeholders for the the future
19  [[maybe_unused]] float deltap = 0.0;
20  [[maybe_unused]] float sigmadeltap = 0.0;
21  [[maybe_unused]] float px = 0.0;
22  [[maybe_unused]] float py = 0.0;
23  [[maybe_unused]] float pz = 0.0;
24  float eta = 0.0;
25  float phi = 0.0;
26  float energy = 0.0;
27  float trajpoint = 0.0;
28  float eta_ecal = 0.0;
29  float phi_ecal = 0.0;
30  float eta_hcal = 0.0;
31  float phi_hcal = 0.0;
32  float charge = 0;
33  float layer = 0;
34  float depth = 0;
35  float muon_dt_hits = 0.0;
36  float muon_csc_hits = 0.0;
37 
39  const auto& matched_pftrack = orig.trackRefPF();
40  if (matched_pftrack.isNonnull()) {
41  const auto& atECAL = matched_pftrack->extrapolatedPoint(reco::PFTrajectoryPoint::ECALShowerMax);
42  const auto& atHCAL = matched_pftrack->extrapolatedPoint(reco::PFTrajectoryPoint::HCALEntrance);
43  if (atHCAL.isValid()) {
44  eta_hcal = atHCAL.positionREP().eta();
45  phi_hcal = atHCAL.positionREP().phi();
46  }
47  if (atECAL.isValid()) {
48  eta_ecal = atECAL.positionREP().eta();
49  phi_ecal = atECAL.positionREP().phi();
50  }
51  }
52  const auto& ref = ((const reco::PFBlockElementTrack*)&orig)->trackRef();
53  pt = ref->pt();
54  px = ref->px();
55  py = ref->py();
56  pz = ref->pz();
57  eta = ref->eta();
58  phi = ref->phi();
59  energy = ref->p();
60  charge = ref->charge();
61 
62  reco::MuonRef muonRef = orig.muonRef();
63  if (muonRef.isNonnull()) {
64  reco::TrackRef standAloneMu = muonRef->standAloneMuon();
65  if (standAloneMu.isNonnull()) {
66  muon_dt_hits = standAloneMu->hitPattern().numberOfValidMuonDTHits();
67  muon_csc_hits = standAloneMu->hitPattern().numberOfValidMuonCSCHits();
68  }
69  }
70 
71  } else if (type == reco::PFBlockElement::BREM) {
72  const auto* orig2 = (const reco::PFBlockElementBrem*)&orig;
73  const auto& ref = orig2->GsftrackRef();
74  if (ref.isNonnull()) {
75  deltap = orig2->DeltaP();
76  sigmadeltap = orig2->SigmaDeltaP();
77  pt = ref->pt();
78  px = ref->px();
79  py = ref->py();
80  pz = ref->pz();
81  eta = ref->eta();
82  phi = ref->phi();
83  energy = ref->p();
84  trajpoint = orig2->indTrajPoint();
85  charge = ref->charge();
86  }
87  } else if (type == reco::PFBlockElement::GSF) {
88  //requires to keep GsfPFRecTracks
89  const auto* orig2 = (const reco::PFBlockElementGsfTrack*)&orig;
90  const auto& vec = orig2->Pin();
91  pt = vec.pt();
92  px = vec.px();
93  py = vec.py();
94  pz = vec.pz();
95  eta = vec.eta();
96  phi = vec.phi();
97  energy = vec.energy();
98  if (!orig2->GsftrackRefPF().isNull()) {
99  charge = orig2->GsftrackRefPF()->charge();
100  }
105  const auto& ref = ((const reco::PFBlockElementCluster*)&orig)->clusterRef();
106  if (ref.isNonnull()) {
107  eta = ref->eta();
108  phi = ref->phi();
109  px = ref->position().x();
110  py = ref->position().y();
111  pz = ref->position().z();
112  energy = ref->energy();
113  layer = ref->layer();
114  depth = ref->depth();
115  }
116  } else if (type == reco::PFBlockElement::SC) {
117  const auto& clref = ((const reco::PFBlockElementSuperCluster*)&orig)->superClusterRef();
118  if (clref.isNonnull()) {
119  eta = clref->eta();
120  phi = clref->phi();
121  px = clref->position().x();
122  py = clref->position().y();
123  pz = clref->position().z();
124  energy = clref->energy();
125  }
126  }
127 
128  float typ_idx = static_cast<float>(elem_type_encoding.at(orig.type()));
129 
130  //Must be the same order as in tf_model.py
131  return std::array<float, NUM_ELEMENT_FEATURES>({{typ_idx,
132  pt,
133  eta,
134  phi,
135  energy,
136  layer,
137  depth,
138  charge,
139  trajpoint,
140  eta_ecal,
141  phi_ecal,
142  eta_hcal,
143  phi_hcal,
144  muon_dt_hits,
145  muon_csc_hits}});
146  }
147 
148  //to make sure DNN inputs are within numerical bounds, use the same in training
149  float normalize(float in) {
150  if (std::abs(in) > 1e4f) {
151  return 0.0;
152  } else if (edm::isNotFinite(in)) {
153  return 0.0;
154  }
155  return in;
156  }
157 
158  int argMax(std::vector<float> const& vec) {
159  return static_cast<int>(std::distance(vec.begin(), max_element(vec.begin(), vec.end())));
160  }
161 
162  reco::PFCandidate makeCandidate(int pred_pid, int pred_charge, float pred_e, float pred_eta, float pred_phi) {
163  pred_phi = angle0to2pi::make0To2pi(pred_phi);
164 
165  //currently, set the pT from a massless approximation.
166  //later versions of the model may predict predict both the energy and pT of the particle
167  float pred_pt = pred_e / cosh(pred_eta);
168 
169  //set the charge to +1 or -1 for PFCandidates that are charged, according to the sign of the predicted charge
171  if (pred_pid == 11 || pred_pid == 13 || pred_pid == 211) {
172  charge = pred_charge > 0 ? +1 : -1;
173  }
174 
175  math::PtEtaPhiELorentzVectorD p4(pred_pt, pred_eta, pred_phi, pred_e);
176 
179  cand.setPdgId(pred_pid);
180  cand.setCharge(charge);
181 
182  return cand;
183  }
184 
185  const std::vector<const reco::PFBlockElement*> getPFElements(const reco::PFBlockCollection& blocks) {
186  std::vector<reco::PFCandidate> pOutputCandidateCollection;
187 
188  std::vector<const reco::PFBlockElement*> all_elements;
189  for (const auto& block : blocks) {
190  const auto& elems = block.elements();
191  for (const auto& elem : elems) {
192  if (all_elements.size() < NUM_MAX_ELEMENTS_BATCH) {
193  all_elements.push_back(&elem);
194  } else {
195  //model needs to be created with a bigger LSH codebook size
196  edm::LogError("MLPFProducer") << "too many input PFElements for predefined model size: " << elems.size();
197  break;
198  }
199  }
200  }
201  return all_elements;
202  }
203 
205  const std::vector<const reco::PFBlockElement*> elems,
206  size_t ielem_originator) {
207  const reco::PFBlockElement* elem = elems[ielem_originator];
208  //set the track ref in case the originating element was a track
209  if (elem->type() == reco::PFBlockElement::TRACK && cand.charge() != 0 && elem->trackRef().isNonnull()) {
210  cand.setTrackRef(elem->trackRef());
211 
212  //set the muon ref in case the originator was a muon
213  const auto& muonref = elem->muonRef();
214  if (muonref.isNonnull()) {
215  cand.setMuonRef(muonref);
216  }
217  }
218  }
219 
220 }; // namespace reco::mlpf
reco::PFBlockElement::HO
Definition: PFBlockElement.h:42
math::PtEtaPhiELorentzVectorD
ROOT::Math::LorentzVector< ROOT::Math::PtEtaPhiE4D< double > > PtEtaPhiELorentzVectorD
Lorentz vector with cartesian internal representation.
Definition: LorentzVector.h:12
reco::PFBlockElement::trackRefPF
virtual const PFRecTrackRef & trackRefPF() const
Definition: PFBlockElement.h:89
MessageLogger.h
PFBlockElementCluster.h
edm::isNotFinite
constexpr bool isNotFinite(T x)
Definition: isFinite.h:9
DiDispStaMuonMonitor_cfi.pt
pt
Definition: DiDispStaMuonMonitor_cfi.py:39
reco::mlpf::normalize
float normalize(float in)
Definition: MLPFModel.cc:149
multPhiCorr_741_25nsDY_cfi.py
py
Definition: multPhiCorr_741_25nsDY_cfi.py:12
reco::PFBlockElementSuperCluster
Cluster Element.
Definition: PFBlockElementSuperCluster.h:15
reco::PFBlockElement::SC
Definition: PFBlockElement.h:41
reco::Candidate::Charge
int Charge
electric charge type
Definition: Candidate.h:34
reco::PFBlockElement::HCAL
Definition: PFBlockElement.h:36
PFBlockElementBrem.h
MLPFModel.h
reco::mlpf::makeCandidate
reco::PFCandidate makeCandidate(int pred_pid, int pred_charge, float pred_e, float pred_eta, float pred_phi)
Definition: MLPFModel.cc:162
reco::mlpf::elem_type_encoding
static const std::map< int, int > elem_type_encoding
Definition: MLPFModel.h:38
edm::Ref< MuonCollection >
reco::mlpf::setCandidateRefs
void setCandidateRefs(reco::PFCandidate &cand, const std::vector< const reco::PFBlockElement * > elems, size_t ielem_originator)
Definition: MLPFModel.cc:204
reco::mlpf::NUM_MAX_ELEMENTS_BATCH
static constexpr int NUM_MAX_ELEMENTS_BATCH
Definition: MLPFModel.h:13
reco::PFBlockElement::TRACK
Definition: PFBlockElement.h:32
PVValHelper::eta
Definition: PVValidationHelpers.h:70
reco::PFTrajectoryPoint::ECALShowerMax
Definition: PFTrajectoryPoint.h:46
PFCluster.h
reco::PFBlockElementBrem
Track Element.
Definition: PFBlockElementBrem.h:17
HCALHighEnergyHPDFilter_cfi.energy
energy
Definition: HCALHighEnergyHPDFilter_cfi.py:5
LEDCalibrationChannels.depth
depth
Definition: LEDCalibrationChannels.py:65
reco::mlpf::getElementProperties
std::array< float, NUM_ELEMENT_FEATURES > getElementProperties(const reco::PFBlockElement &orig)
Definition: MLPFModel.cc:15
phase1PixelTopology::layer
constexpr std::array< uint8_t, layerIndexSize > layer
Definition: phase1PixelTopology.h:99
reco::PFBlockCollection
std::vector< PFBlock > PFBlockCollection
collection of PFBlock objects
Definition: PFBlockFwd.h:10
ALCARECOTkAlJpsiMuMu_cff.charge
charge
Definition: ALCARECOTkAlJpsiMuMu_cff.py:47
reco::PFBlockElement::HFEM
Definition: PFBlockElement.h:39
reco::PFBlockElement::GSF
Definition: PFBlockElement.h:37
type
type
Definition: SiPixelVCal_PayloadInspector.cc:39
edm::Ref::isNonnull
bool isNonnull() const
Checks for non-null.
Definition: Ref.h:238
recoMuon::in
Definition: RecoMuonEnumerators.h:6
reco::PFBlockElement::ECAL
Definition: PFBlockElement.h:35
reco::PFTrajectoryPoint::HCALEntrance
HCAL front face.
Definition: PFTrajectoryPoint.h:48
cand
Definition: decayParser.h:32
reco::PFBlockElement::BREM
Definition: PFBlockElement.h:38
PFBlockElementSuperCluster.h
reco::PFBlockElement::trackRef
virtual const reco::TrackRef & trackRef() const
Definition: PFBlockElement.h:88
reco::PFBlockElement::muonRef
virtual const MuonRef & muonRef() const
Definition: PFBlockElement.h:93
p4
double p4[4]
Definition: TauolaWrapper.h:92
groupFilesInBlocks.block
block
Definition: groupFilesInBlocks.py:150
reco::PFBlockElement::HFHAD
Definition: PFBlockElement.h:40
edm::LogError
Log< level::Error, false > LogError
Definition: MessageLogger.h:123
reco::mlpf::getPFElements
const std::vector< const reco::PFBlockElement * > getPFElements(const reco::PFBlockCollection &blocks)
Definition: MLPFModel.cc:185
reco::PFBlockElement
Abstract base class for a PFBlock element (track, cluster...)
Definition: PFBlockElement.h:26
angle0to2pi::make0To2pi
constexpr valType make0To2pi(valType angle)
Definition: deltaPhi.h:67
PFBlock.h
multPhiCorr_741_25nsDY_cfi.px
px
Definition: multPhiCorr_741_25nsDY_cfi.py:10
reco::PFBlockElementGsfTrack
Track Element.
Definition: PFBlockElementGsfTrack.h:18
isFinite.h
math::XYZTLorentzVector
XYZTLorentzVectorD XYZTLorentzVector
Lorentz vector with cylindrical internal representation using pseudorapidity.
Definition: LorentzVector.h:29
reco::PFBlockElementCluster
Cluster Element.
Definition: PFBlockElementCluster.h:16
reco::PFBlockElementTrack
Track Element.
Definition: PFBlockElementTrack.h:17
reco::PFBlockElement::type
Type type() const
Definition: PFBlockElement.h:69
reco::PFCandidate
Particle reconstructed by the particle flow algorithm.
Definition: PFCandidate.h:41
reco::mlpf
Definition: MLPFModel.h:8
funct::abs
Abs< T >::type abs(const T &t)
Definition: Abs.h:22
reco::mlpf::argMax
int argMax(std::vector< float > const &vec)
Definition: MLPFModel.cc:158
reco::PFBlockElement::PS1
Definition: PFBlockElement.h:33
reco::PFCandidate::ParticleType
ParticleType
particle types
Definition: PFCandidate.h:44
HLT_FULL_cff.distance
distance
Definition: HLT_FULL_cff.py:7746
gather_cfg.blocks
blocks
Definition: gather_cfg.py:90
PFBlockElementGsfTrack.h
PFBlockElementTrack.h
reco::PFBlockElement::PS2
Definition: PFBlockElement.h:34