CMS 3D CMS Logo

L1NNTauProducer.cc
Go to the documentation of this file.
2 #include <TLorentzVector.h>
3 #include <cmath>
4 #include <vector>
5 
11 
16 
17 #include "ap_int.h"
18 #include "ap_fixed.h"
19 
20 using namespace l1t;
21 
22 class L1NNTauProducer : public edm::stream::EDProducer<edm::GlobalCache<tensorflow::SessionCache>> {
23 public:
25  ~L1NNTauProducer() override;
26 
27  static void fillDescriptions(edm::ConfigurationDescriptions& descriptions);
28  static std::unique_ptr<tensorflow::SessionCache> initializeGlobalCache(const edm::ParameterSet&);
29  static void globalEndJob(const tensorflow::SessionCache*){};
30 
31 private:
32  std::unique_ptr<TauNNId> fTauNNId_;
33  std::unique_ptr<TauNNIdHW> fTauNNIdHW_;
34 
35  void produce(edm::Event& iEvent, const edm::EventSetup& iSetup) override;
36  void process_SW(const l1t::PFCandidateCollection& parts, std::unique_ptr<l1t::PFTauCollection>& iTaus);
37  void process_HW(const l1t::PFCandidateCollection& parts, std::unique_ptr<l1t::PFTauCollection>& iTaus);
38  void makeTau_HW(const l1t::PFCandidate& seed,
40  std::unique_ptr<l1t::PFTauCollection>& iTaus);
41 
42  void addTau(const l1t::PFCandidate& iCand,
43  const l1t::PFCandidateCollection& iParts,
44  std::unique_ptr<PFTauCollection>& outputTaus);
45 
46  double fSeedPt_;
47  double fConeSize_;
48  double fTauSize_;
49  int fMaxTaus_;
51  const bool fHW;
52  const bool fEMSeed;
53  const bool fDebug;
55 };
56 
57 static constexpr float track_trigger_eta_max = 2.5;
58 
60  : fSeedPt_(cfg.getParameter<double>("seedpt")),
61  fConeSize_(cfg.getParameter<double>("conesize")),
62  fTauSize_(cfg.getParameter<double>("tausize")),
63  fMaxTaus_(cfg.getParameter<int>("maxtaus")),
64  fNParticles_(cfg.getParameter<int>("nparticles")),
65  fHW(cfg.getParameter<bool>("HW")),
66  fEMSeed(cfg.getParameter<bool>("emseed")),
67  fDebug(cfg.getParameter<bool>("debug")),
68  fL1PFToken_(consumes<vector<l1t::PFCandidate>>(cfg.getParameter<edm::InputTag>("L1PFObjects"))) {
69  std::string lNNFile = cfg.getParameter<std::string>("NNFileName"); //,"L1Trigger/Phase2L1Taus/data/tau_3layer.pb");
70  if (fHW) {
71  fTauNNIdHW_ = std::make_unique<TauNNIdHW>();
72  fTauNNIdHW_->initialize("input_1:0", fNParticles_);
73  } else {
74  fTauNNId_ = std::make_unique<TauNNId>(lNNFile.find("v0") == std::string::npos ? "input_1:0" : "dense_1_input:0",
75  cache->getSession(),
76  lNNFile,
77  fNParticles_);
78  }
79  produces<l1t::PFTauCollection>("L1PFTausNN");
80 }
81 
82 std::unique_ptr<tensorflow::SessionCache> L1NNTauProducer::initializeGlobalCache(const edm::ParameterSet& cfg) {
84  std::string graphPath = edm::FileInPath(cfg.getParameter<std::string>("NNFileName")).fullPath();
85  return std::make_unique<tensorflow::SessionCache>(graphPath);
86 }
87 
90  iEvent.getByToken(fL1PFToken_, l1PFCandidates);
91  auto lTaus = std::make_unique<l1t::PFTauCollection>();
92 
93  if (fHW) {
94  process_HW(*l1PFCandidates, lTaus);
95  } else {
96  process_SW(*l1PFCandidates, lTaus);
97  }
98 
99  if (lTaus->empty()) {
100  PFTau dummy;
101  lTaus->push_back(dummy);
102  }
103  std::sort(lTaus->begin(), lTaus->end(), [](l1t::PFTau i, l1t::PFTau j) { return (i.pt() > j.pt()); });
104  iEvent.put(std::move(lTaus), "L1PFTausNN");
105 }
107  std::unique_ptr<l1t::PFTauCollection>& iTaus) {
108  std::vector<unique_ptr<l1t::PFCandidate>> pfChargedHadrons;
109  std::vector<unique_ptr<l1t::PFCandidate>> pfChargedHadrons_sort_v;
110  std::vector<unique_ptr<l1t::PFCandidate>> pfChargedHadrons_seeds_v;
111  for (const auto& l1PFCand : parts)
112  if ((l1PFCand.id() == l1t::PFCandidate::ChargedHadron || l1PFCand.id() == l1t::PFCandidate::Electron) &&
113  std::abs(l1PFCand.eta()) < track_trigger_eta_max)
114  pfChargedHadrons_sort_v.push_back(std::make_unique<l1t::PFCandidate>(l1PFCand));
115 
116  if (pfChargedHadrons_sort_v.empty())
117  return;
118  std::sort(
119  pfChargedHadrons_sort_v.begin(),
120  pfChargedHadrons_sort_v.end(),
121  [](std::unique_ptr<l1t::PFCandidate>& i, std::unique_ptr<l1t::PFCandidate>& j) { return (i->pt() > j->pt()); });
122 
123  pfChargedHadrons_seeds_v.push_back(std::move(pfChargedHadrons_sort_v[0]));
124  for (unsigned int i0 = 1; i0 < pfChargedHadrons_sort_v.size(); i0++) {
125  bool pMatch = false;
126  for (unsigned int i1 = 0; i1 < pfChargedHadrons_seeds_v.size(); i1++) {
127  if (reco::deltaR2(*(pfChargedHadrons_seeds_v[i1]), *(pfChargedHadrons_sort_v[i0])) < fConeSize_ * fConeSize_)
128  pMatch = true;
129  }
130  if (pMatch)
131  continue;
132  pfChargedHadrons_seeds_v.push_back(std::move(pfChargedHadrons_sort_v[i0]));
133  if (int(pfChargedHadrons_seeds_v.size()) > fMaxTaus_ - 1)
134  break;
135  }
136  for (unsigned int i0 = 0; i0 < pfChargedHadrons_seeds_v.size(); i0++) {
137  addTau(*(pfChargedHadrons_seeds_v[i0]), parts, iTaus);
138  }
139 }
140 
141 // create taus based on grid structure
143  const l1t::PFCandidateCollection& iParts,
144  std::unique_ptr<l1t::PFTauCollection>& outputTaus) {
145  l1t::PFCandidateCollection pfTauCands;
146  math::PtEtaPhiMLorentzVector lTot(0, 0, 0, 0);
147  math::PtEtaPhiMLorentzVector lCand(0, 0, 0, 0);
148  int lId = 0;
149  float z0 = 0;
150  float dxy = 0;
151  for (const auto& l1PFCand : iParts) {
152  if (reco::deltaR2(iCand, l1PFCand) > fConeSize_ * fConeSize_)
153  continue;
154  math::PtEtaPhiMLorentzVector pVec(l1PFCand.pt(), l1PFCand.eta(), l1PFCand.phi(), 0);
155  lTot += pVec;
156  if (reco::deltaR2(iCand, l1PFCand) < fTauSize_ * fTauSize_ &&
157  (l1PFCand.id() == l1t::PFCandidate::Electron || l1PFCand.id() == l1t::PFCandidate::ChargedHadron ||
158  l1PFCand.id() == l1t::PFCandidate::Photon)) {
159  lId++;
160  lCand += pVec;
161  if (z0 == 0 && l1PFCand.id() == l1t::PFCandidate::ChargedHadron) {
162  z0 = l1PFCand.z0();
163  dxy = l1PFCand.dxy();
164  }
165  }
166  pfTauCands.push_back(l1PFCand);
167  }
168  if (lTot.Pt() < fSeedPt_)
169  return;
170  std::sort(
171  pfTauCands.begin(), pfTauCands.end(), [](l1t::PFCandidate i, l1t::PFCandidate j) { return (i.pt() > j.pt()); });
172  float NN = fTauNNId_->compute(iCand, pfTauCands);
173  float* lNNVector = fTauNNId_->NNVectorVar();
174  math::PtEtaPhiMLorentzVector tempP4(lCand.Pt(), lCand.Eta(), lCand.Phi(), lCand.M() * lCand.M());
175  l1t::PFTau l1PFTau(tempP4, lNNVector, NN, 0, lId);
176  l1PFTau.setZ0(z0);
177  l1PFTau.setDxy(dxy);
178  outputTaus->push_back(l1PFTau);
179 }
181  // L1NNTauProducer
183  desc.add<std::string>("NNFileName", "L1Trigger/Phase2L1ParticleFlow/data/tau_3layer.pb");
184  desc.add<double>("tausize", 0.1);
185  desc.add<int>("maxtaus", 5);
186  desc.add<int>("nparticles", 10);
187  desc.add<double>("conesize", 0.4);
188  desc.add<double>("seedpt", 20);
189  desc.add<bool>("HW", true);
190  desc.add<bool>("emseed", true);
191  desc.add<bool>("debug", false);
192  desc.add<edm::InputTag>("L1PFObjects", edm::InputTag("L1PFProducer", "l1pfCandidates"));
193  descriptions.add("L1NNTauProducer", desc);
194 }
195 
198  std::unique_ptr<l1t::PFTauCollection>& iTaus) {
199  // Seed Cone Jet algorithm with ap_fixed types and hardware emulation
200  L1TauEmu::detaphi_t rCone2 =
202  unsigned lId = 0;
203 
204  input2_t p1_tot = 0;
205  input2_t p1x_tot = 0;
206  input2_t p1y_tot = 0;
207  input2_t p1z_tot = 0;
208 
209  input_t e1ta_1 = seed.eta();
210  input_t p1hi_1 = seed.phi();
211  L1TauEmu::pt_t pt = 0;
212  L1TauEmu::z0_t z0 = 0;
213  L1TauEmu::dxy_t dxy = 0;
214 
215  for (unsigned i0 = 0; i0 < parts.size(); i0++) {
216  if (L1TauEmu::inCone(seed, (parts[i0]), rCone2)) {
218  parts[i0].id() == l1t::PFCandidate::Photon) {
219  lId++;
220  pt = pt + L1TauEmu::pt_t(parts[i0].pt());
221 
222  input2_t d1eta = input_t(parts[i0].eta()) - e1ta_1;
223  input2_t d1phi = input_t(parts[i0].phi()) - p1hi_1;
224  input2_t d1r2 = d1eta * d1eta + d1phi * d1phi;
225  input2_t tmppt = input_t(parts[i0].pt());
226  input2_t half = 0.5;
227  p1z_tot = p1z_tot + tmppt * (1 - d1r2 * half);
228  p1y_tot = p1y_tot + tmppt * d1phi;
229  p1x_tot = p1x_tot + tmppt * d1eta;
230  p1_tot = p1_tot + tmppt;
231 
232  if (z0 == 0 && parts[i0].id() == l1t::PFCandidate::ChargedHadron) {
233  z0 = parts[i0].hwZ0();
234  dxy = parts[i0].hwDxy();
235  }
236  }
237  }
238  }
239 
240  //Compute the mass
241  input2_t tmpmass1 = (p1_tot * p1_tot - p1x_tot * p1x_tot - p1y_tot * p1y_tot - p1z_tot * p1z_tot);
242  if (tmpmass1 < 0)
243  tmpmass1 = 0;
244  L1TauEmu::pt_t mass = l1ct::pt_t(tmpmass1);
245 
246  if (pt < fSeedPt_)
247  return;
248 
249  result_t NN = fTauNNIdHW_->compute(seed, parts);
250  input_t* lNNVector = fTauNNIdHW_->NNVectorVar();
251 
252  float pNNVec[80];
253  for (unsigned i0 = 0; i0 < 80; i0++)
254  pNNVec[i0] = float(lNNVector[i0]);
255 
256  //Firmware Tau
257  l1ct::Tau l1ctTau;
258  l1ctTau.hwPt = l1ct::pt_t(pt); //l1gt is <16,11> and currently <16,14>
259  l1ctTau.hwEta = l1ct::Scales::makeGlbEta(seed.eta()); // seed.eta() and seed.phi() are in physical coordinates
260  l1ctTau.hwPhi = l1ct::Scales::makeGlbPhi(seed.phi());
261 
262  l1ctTau.hwSeedPt = seed.pt();
263  l1ctTau.hwSeedZ0 = seed.hwZ0();
264  l1ctTau.hwCharge = seed.charge();
265 
266  l1ctTau.hwType = l1ct::Tau::type_t(lId);
267  l1ctTau.hwRawId = ap_uint<10>(NN * 1024); //NN Output is ap_fixed<16, 8> so need to cast.
268 
269  //Convert to GT format and pack to encodedTau of PFTau
270  l1gt::Tau l1gtTau = l1ctTau.toGT();
271  l1gt::PackedTau packed_Tau = l1gtTau.pack();
272 
273  //Make PFTau
274  //Save pt, eta and phi in gt scales
276  l1gt::Scales::floatEta(l1gtTau.v3.eta),
277  l1gt::Scales::floatPhi(l1gtTau.v3.phi),
278  float(mass));
279 
280  l1t::PFTau l1PFTau(tempP4, pNNVec, NN, 0, lId);
281  l1PFTau.setZ0(float(z0) * 0.05); //L1TauEmu::z0_base);
282  l1PFTau.setDxy(float(dxy) * 0.05); //L1TauEmu::dxy_base);
283 
284  l1PFTau.set_encodedTau(packed_Tau);
285 
286  iTaus->push_back(l1PFTau);
287 }
288 
290  std::unique_ptr<l1t::PFTauCollection>& iTaus) {
291  // The fixed point algorithm emulation
292  using namespace L1TauEmu;
293  std::vector<l1t::PFCandidate> work;
294  work.resize(parts.size());
295  std::transform(parts.begin(), parts.end(), work.begin(), [](const l1t::PFCandidate& part) { return part; });
296  std::sort(work.begin(), work.end(), [](l1t::PFCandidate i, l1t::PFCandidate j) {
297  return (l1ct::pt_t(i.pt()) > l1ct::pt_t(j.pt()));
298  });
299 
300  std::vector<l1t::PFCandidate> seeds;
302  if (fEMSeed)
303  lSeed = l1t::PFCandidate::Photon;
304  std::copy_if(work.begin(), work.end(), std::back_inserter(seeds), [&](const l1t::PFCandidate& part) {
306  part.id() == lSeed) &&
308  });
309  // It would be nice to transform the inputs to the etaphi_base of the FW here, as in the line below
310  // However the phi may wrap around if the etaphi_base > 1, so don't do it...
311  //std::for_each(work.begin(), work.end(), [](l1t::PFCandidate& x){x.setP4(math::PtEtaPhiMLorentzVector(pt_t(x.pt()), etaphi_t(x.eta()*etaphi_base), etaphi_t(x.phi()*etaphi_base), x.mass()));});
313 
314  iTaus->reserve(fMaxTaus_);
315  while (!seeds.empty() && iTaus->size() < unsigned(fMaxTaus_)) {
316  // Take the first (highest pt) candidate as a seed
317  l1t::PFCandidate seed = seeds.at(0);
318  // Get the particles within a _coneSize of the seed
319  std::vector<l1t::PFCandidate> particlesInCone;
320  std::copy_if(work.begin(), work.end(), std::back_inserter(particlesInCone), [&](l1t::PFCandidate& part) {
321  return inCone(seed, part, rCone2);
322  });
323  makeTau_HW(seed, particlesInCone, iTaus);
324  // remove the clustered particles
325  work.erase(std::remove_if(
326  work.begin(), work.end(), [&](const l1t::PFCandidate& part) { return inCone(seed, part, rCone2); }),
327  work.end());
328 
329  seeds.erase(
330  std::remove_if(
331  seeds.begin(), seeds.end(), [&](const l1t::PFCandidate& part) { return inCone(seed, part, rCone2); }),
332  seeds.end());
333  }
334 }
335 
337 
void set_encodedTau(l1gt::PackedTau encodedTau)
Definition: PFTau.h:78
glbeta_t makeGlbEta(float eta)
Definition: datatypes.h:202
float floatPhi(phi_t phi)
Definition: gt_datatypes.h:46
float floatEta(eta_t eta)
Definition: gt_datatypes.h:45
static constexpr float track_trigger_eta_max
std::string fullPath() const
Definition: FileInPath.cc:161
L1NNTauProducer(const edm::ParameterSet &, const tensorflow::SessionCache *)
static constexpr float etaphi_base
Definition: TauNNIdHW.h:35
std::unique_ptr< TauNNIdHW > fTauNNIdHW_
void setDxy(float dxy)
Definition: PFTau.h:52
std::vector< l1t::PFCandidate > PFCandidateCollection
Definition: PFCandidate.h:86
void produce(edm::Event &iEvent, const edm::EventSetup &iSetup) override
glbphi_t makeGlbPhi(float phi)
Definition: datatypes.h:205
delete x;
Definition: CaloConfig.h:22
ap_fixed< 12, 6 > detaphi_t
Definition: TauNNIdHW.h:38
void makeTau_HW(const l1t::PFCandidate &seed, l1t::PFCandidateCollection &parts, std::unique_ptr< l1t::PFTauCollection > &iTaus)
type_t hwType
Definition: taus.h:21
edm::EDGetTokenT< vector< l1t::PFCandidate > > fL1PFToken_
std::array< uint64_t, 2 > PackedTau
Definition: gt_datatypes.h:37
PtEtaPhiMLorentzVectorD PtEtaPhiMLorentzVector
Lorentz vector with cartesian internal representation.
Definition: LorentzVector.h:25
ap_fixed< 16, 8 > input_t
void addTau(const l1t::PFCandidate &iCand, const l1t::PFCandidateCollection &iParts, std::unique_ptr< PFTauCollection > &outputTaus)
std::unique_ptr< TauNNId > fTauNNId_
int iEvent
Definition: GenABIO.cc:224
constexpr Matriplex::idx_t NN
Definition: Matrix.h:43
ap_uint< 2 > type_t
Definition: taus.h:11
Abs< T >::type abs(const T &t)
Definition: Abs.h:22
float floatPt(pt_t pt)
Definition: gt_datatypes.h:44
#define DEFINE_FWK_MODULE(type)
Definition: MakerMacros.h:16
bool hwCharge
Definition: taus.h:20
glbphi_t hwPhi
Definition: taus.h:17
pt_t hwPt
Definition: taus.h:15
Definition: taus.h:10
z0_t hwSeedZ0
Definition: taus.h:19
~L1NNTauProducer() override
void setLogging(const std::string &level="3")
Definition: TensorFlow.cc:81
ap_ufixed< 14, 12, AP_TRN, AP_SAT > pt_t
Definition: datatypes.h:19
constexpr auto deltaR2(const T1 &t1, const T2 &t2) -> decltype(t1.eta())
Definition: deltaR.h:16
static void fillDescriptions(edm::ConfigurationDescriptions &descriptions)
static void fillDescriptions(edm::ConfigurationDescriptions &descriptions)
static std::unique_ptr< tensorflow::SessionCache > initializeGlobalCache(const edm::ParameterSet &)
part
Definition: HCALResponse.h:20
pt_t hwSeedPt
Definition: taus.h:18
l1gt::Tau toGT() const
Definition: taus.h:108
void add(std::string const &label, ParameterSetDescription const &psetDescription)
void process_HW(const l1t::PFCandidateCollection &parts, std::unique_ptr< l1t::PFTauCollection > &iTaus)
glbeta_t hwEta
Definition: taus.h:16
ap_fixed< 24, 12 > input2_t
HLT enums.
ap_ufixed< 16, 14 > pt_t
Definition: TauNNIdHW.h:36
def cache(function)
Definition: utilities.py:3
ap_fixed< 16, 8 > result_t
ap_int< 10 > z0_t
Definition: TauNNIdHW.h:42
const bool fEMSeed
void setZ0(float z0)
Definition: PFTau.h:51
ap_int< 8 > dxy_t
Definition: TauNNIdHW.h:41
PackedTau pack() const
Definition: gt_datatypes.h:208
bool inCone(l1t::PFCandidate seed, l1t::PFCandidate part, detaphi_t cone2)
Definition: TauNNIdHW.h:131
static void globalEndJob(const tensorflow::SessionCache *)
rawid_t hwRawId
Definition: taus.h:22
void process_SW(const l1t::PFCandidateCollection &parts, std::unique_ptr< l1t::PFTauCollection > &iTaus)
def move(src, dest)
Definition: eostools.py:511
unsigned transform(const HcalDetId &id, unsigned transformCode)