CMS 3D CMS Logo

MLPFModel.cc
Go to the documentation of this file.
10 
11 namespace reco::mlpf {
12 
13  //Prepares the input array of floats for a single PFElement
14  std::array<float, NUM_ELEMENT_FEATURES> getElementProperties(const reco::PFBlockElement& orig) {
15  const auto type = orig.type();
16  float pt = 0.0;
17  //these are placeholders for the the future
18  [[maybe_unused]] float deltap = 0.0;
19  [[maybe_unused]] float sigmadeltap = 0.0;
20  [[maybe_unused]] float px = 0.0;
21  [[maybe_unused]] float py = 0.0;
22  [[maybe_unused]] float pz = 0.0;
23  float eta = 0.0;
24  float phi = 0.0;
25  float energy = 0.0;
26  float trajpoint = 0.0;
27  float eta_ecal = 0.0;
28  float phi_ecal = 0.0;
29  float eta_hcal = 0.0;
30  float phi_hcal = 0.0;
31  float charge = 0;
32  float layer = 0;
33  float depth = 0;
34  float muon_dt_hits = 0.0;
35  float muon_csc_hits = 0.0;
36 
38  const auto& matched_pftrack = orig.trackRefPF();
39  if (matched_pftrack.isNonnull()) {
40  const auto& atECAL = matched_pftrack->extrapolatedPoint(reco::PFTrajectoryPoint::ECALShowerMax);
41  const auto& atHCAL = matched_pftrack->extrapolatedPoint(reco::PFTrajectoryPoint::HCALEntrance);
42  if (atHCAL.isValid()) {
43  eta_hcal = atHCAL.positionREP().eta();
44  phi_hcal = atHCAL.positionREP().phi();
45  }
46  if (atECAL.isValid()) {
47  eta_ecal = atECAL.positionREP().eta();
48  phi_ecal = atECAL.positionREP().phi();
49  }
50  }
51  const auto& ref = ((const reco::PFBlockElementTrack*)&orig)->trackRef();
52  pt = ref->pt();
53  px = ref->px();
54  py = ref->py();
55  pz = ref->pz();
56  eta = ref->eta();
57  phi = ref->phi();
58  energy = ref->p();
59  charge = ref->charge();
60 
61  reco::MuonRef muonRef = orig.muonRef();
62  if (muonRef.isNonnull()) {
63  reco::TrackRef standAloneMu = muonRef->standAloneMuon();
64  if (standAloneMu.isNonnull()) {
65  muon_dt_hits = standAloneMu->hitPattern().numberOfValidMuonDTHits();
66  muon_csc_hits = standAloneMu->hitPattern().numberOfValidMuonCSCHits();
67  }
68  }
69 
70  } else if (type == reco::PFBlockElement::BREM) {
71  const auto* orig2 = (const reco::PFBlockElementBrem*)&orig;
72  const auto& ref = orig2->GsftrackRef();
73  if (ref.isNonnull()) {
74  deltap = orig2->DeltaP();
75  sigmadeltap = orig2->SigmaDeltaP();
76  pt = ref->pt();
77  px = ref->px();
78  py = ref->py();
79  pz = ref->pz();
80  eta = ref->eta();
81  phi = ref->phi();
82  energy = ref->p();
83  trajpoint = orig2->indTrajPoint();
84  charge = ref->charge();
85  }
86  } else if (type == reco::PFBlockElement::GSF) {
87  //requires to keep GsfPFRecTracks
88  const auto* orig2 = (const reco::PFBlockElementGsfTrack*)&orig;
89  const auto& vec = orig2->Pin();
90  pt = vec.pt();
91  px = vec.px();
92  py = vec.py();
93  pz = vec.pz();
94  eta = vec.eta();
95  phi = vec.phi();
96  energy = vec.energy();
97  if (!orig2->GsftrackRefPF().isNull()) {
98  charge = orig2->GsftrackRefPF()->charge();
99  }
104  const auto& ref = ((const reco::PFBlockElementCluster*)&orig)->clusterRef();
105  if (ref.isNonnull()) {
106  eta = ref->eta();
107  phi = ref->phi();
108  px = ref->position().x();
109  py = ref->position().y();
110  pz = ref->position().z();
111  energy = ref->energy();
112  layer = ref->layer();
113  depth = ref->depth();
114  }
115  } else if (type == reco::PFBlockElement::SC) {
116  const auto& clref = ((const reco::PFBlockElementSuperCluster*)&orig)->superClusterRef();
117  if (clref.isNonnull()) {
118  eta = clref->eta();
119  phi = clref->phi();
120  px = clref->position().x();
121  py = clref->position().y();
122  pz = clref->position().z();
123  energy = clref->energy();
124  }
125  }
126 
127  float typ_idx = static_cast<float>(elem_type_encoding.at(orig.type()));
128 
129  //Must be the same order as in tf_model.py
130  return std::array<float, NUM_ELEMENT_FEATURES>({{typ_idx,
131  pt,
132  eta,
133  phi,
134  energy,
135  layer,
136  depth,
137  charge,
138  trajpoint,
139  eta_ecal,
140  phi_ecal,
141  eta_hcal,
142  phi_hcal,
143  muon_dt_hits,
144  muon_csc_hits}});
145  }
146 
147  //to make sure DNN inputs are within numerical bounds, use the same in training
148  float normalize(float in) {
149  if (std::abs(in) > 1e4f) {
150  return 0.0;
151  } else if (std::isnan(in)) {
152  return 0.0;
153  }
154  return in;
155  }
156 
157  int argMax(std::vector<float> const& vec) {
158  return static_cast<int>(std::distance(vec.begin(), max_element(vec.begin(), vec.end())));
159  }
160 
161  reco::PFCandidate makeCandidate(int pred_pid, int pred_charge, float pred_e, float pred_eta, float pred_phi) {
162  pred_phi = angle0to2pi::make0To2pi(pred_phi);
163 
164  //currently, set the pT from a massless approximation.
165  //later versions of the model may predict predict both the energy and pT of the particle
166  float pred_pt = pred_e / cosh(pred_eta);
167 
168  //set the charge to +1 or -1 for PFCandidates that are charged, according to the sign of the predicted charge
170  if (pred_pid == 11 || pred_pid == 13 || pred_pid == 211) {
171  charge = pred_charge > 0 ? +1 : -1;
172  }
173 
174  math::PtEtaPhiELorentzVectorD p4(pred_pt, pred_eta, pred_phi, pred_e);
175 
178  cand.setPdgId(pred_pid);
179  cand.setCharge(charge);
180 
181  return cand;
182  }
183 
184  const std::vector<const reco::PFBlockElement*> getPFElements(const reco::PFBlockCollection& blocks) {
185  std::vector<reco::PFCandidate> pOutputCandidateCollection;
186 
187  std::vector<const reco::PFBlockElement*> all_elements;
188  for (const auto& block : blocks) {
189  const auto& elems = block.elements();
190  for (const auto& elem : elems) {
191  if (all_elements.size() < NUM_MAX_ELEMENTS_BATCH) {
192  all_elements.push_back(&elem);
193  } else {
194  //model needs to be created with a bigger LSH codebook size
195  edm::LogError("MLPFProducer") << "too many input PFElements for predefined model size: " << elems.size();
196  break;
197  }
198  }
199  }
200  return all_elements;
201  }
202 
204  const std::vector<const reco::PFBlockElement*> elems,
205  size_t ielem_originator) {
206  const reco::PFBlockElement* elem = elems[ielem_originator];
207  //set the track ref in case the originating element was a track
208  if (elem->type() == reco::PFBlockElement::TRACK && cand.charge() != 0 && elem->trackRef().isNonnull()) {
209  cand.setTrackRef(elem->trackRef());
210 
211  //set the muon ref in case the originator was a muon
212  const auto& muonref = elem->muonRef();
213  if (muonref.isNonnull()) {
214  cand.setMuonRef(muonref);
215  }
216  }
217  }
218 
219 }; // 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
DiDispStaMuonMonitor_cfi.pt
pt
Definition: DiDispStaMuonMonitor_cfi.py:39
reco::mlpf::normalize
float normalize(float in)
Definition: MLPFModel.cc:148
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:161
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:203
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
CommonMethods.isnan
def isnan(num)
Definition: CommonMethods.py:98
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:14
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:37
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:184
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
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:157
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:7733
gather_cfg.blocks
blocks
Definition: gather_cfg.py:90
PFBlockElementGsfTrack.h
PFBlockElementTrack.h
reco::PFBlockElement::PS2
Definition: PFBlockElement.h:34