CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
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 
177  reco::PFCandidate cand(
178  0, math::XYZTLorentzVector(p4.X(), p4.Y(), p4.Z(), p4.E()), reco::PFCandidate::ParticleType(0));
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
Abstract base class for a PFBlock element (track, cluster...)
int Charge
electric charge type
Definition: Candidate.h:34
virtual const MuonRef & muonRef() const
ParticleType
particle types
Definition: PFCandidate.h:44
bool isNonnull() const
Checks for non-null.
Definition: Ref.h:238
std::array< float, NUM_ELEMENT_FEATURES > getElementProperties(const reco::PFBlockElement &orig)
Definition: MLPFModel.cc:15
int argMax(std::vector< float > const &vec)
Definition: MLPFModel.cc:158
constexpr bool isNotFinite(T x)
Definition: isFinite.h:9
virtual const PFClusterRef & clusterRef() const
Type type() const
tuple blocks
Definition: gather_cfg.py:90
Log< level::Error, false > LogError
const std::vector< const reco::PFBlockElement * > getPFElements(const reco::PFBlockCollection &blocks)
Definition: MLPFModel.cc:185
constexpr std::array< uint8_t, layerIndexSize > layer
XYZTLorentzVectorD XYZTLorentzVector
Lorentz vector with cylindrical internal representation using pseudorapidity.
Definition: LorentzVector.h:29
Abs< T >::type abs(const T &t)
Definition: Abs.h:22
ROOT::Math::LorentzVector< ROOT::Math::PtEtaPhiE4D< double > > PtEtaPhiELorentzVectorD
Lorentz vector with cartesian internal representation.
Definition: LorentzVector.h:12
std::vector< PFBlock > PFBlockCollection
collection of PFBlock objects
Definition: PFBlockFwd.h:10
virtual const PFRecTrackRef & trackRefPF() const
static const std::map< int, int > elem_type_encoding
Definition: MLPFModel.h:38
tuple mlpf
Definition: mlpf_cff.py:4
void setCandidateRefs(reco::PFCandidate &cand, const std::vector< const reco::PFBlockElement * > elems, size_t ielem_originator)
Definition: MLPFModel.cc:204
constexpr valType make0To2pi(valType angle)
Definition: deltaPhi.h:67
float normalize(float in)
Definition: MLPFModel.cc:149
Particle reconstructed by the particle flow algorithm.
Definition: PFCandidate.h:41
void setMuonRef(const reco::MuonRef &ref)
set muon reference
Definition: PFCandidate.cc:432
virtual const reco::TrackRef & trackRef() const
void setTrackRef(const reco::TrackRef &ref)
set track reference
Definition: PFCandidate.cc:415
reco::PFCandidate makeCandidate(int pred_pid, int pred_charge, float pred_e, float pred_eta, float pred_phi)
Definition: MLPFModel.cc:162
void setPdgId(int pdgId) final
int charge() const final
electric charge
static constexpr int NUM_MAX_ELEMENTS_BATCH
Definition: MLPFModel.h:13