CMS 3D CMS Logo

L1NNCaloTauProducer.cc
Go to the documentation of this file.
1 /* -*- C++ -*-
2 
3 Package: L1CaloTrigger
4 Class: L1NNCaloTauProducer
5 Frinedly name: The TauMinator
6 
7 \class L1NNCaloTauProducer L1NNCaloTauProducer.cc
8 
9 Description:
10 Perform reconstruction and identification of tau
11 candidates at L1 Trigger with a CNN.
12 
13 Implementation:
14 Take as input the HCAL TPs, the ECAL TPs from
15 l1tEGammaClusterEmuProducer, and the HGCAL TPs
16 from l1tHGCalTowerProducer and l1tHGCalBackEndLayer2Producer.
17 Proceed to clustering of trigger towers in NxM
18 clusters, match to HGcal 3D clusters in the endcap.
19 Finally apply the CNNs.
20 
21 Original Author: Jona Motta
22 Created: Tue May 30th 2023
23 
24 */
25 
26 #include <iostream>
27 #include <vector>
28 #include <cmath>
29 
30 #include "boost/property_tree/ptree.hpp"
31 #include "boost/property_tree/json_parser.hpp"
32 
42 
50 
53 
57 
59 
60 struct NNmodels_GlobalCache {
64 
69  boost::property_tree::ptree FeatScaler_CE;
70 
71  tensorflow::GraphDef* CNNmodel_CB;
72  tensorflow::GraphDef* DNNident_CB;
73  tensorflow::GraphDef* DNNcalib_CB;
74 
75  tensorflow::Session* CNNmodel_CBsession;
76  tensorflow::Session* DNNident_CBsession;
77  tensorflow::Session* DNNcalib_CBsession;
78 
79  tensorflow::GraphDef* CNNmodel_CE;
80  tensorflow::GraphDef* DNNident_CE;
81  tensorflow::GraphDef* DNNcalib_CE;
82 
83  tensorflow::Session* CNNmodel_CEsession;
84  tensorflow::Session* DNNident_CEsession;
85  tensorflow::Session* DNNcalib_CEsession;
86 };
87 
88 class L1NNCaloTauProducer : public edm::stream::EDProducer<edm::GlobalCache<NNmodels_GlobalCache>> {
89 public:
91 
92  static void fillDescriptions(edm::ConfigurationDescriptions& descriptions);
93  static std::unique_ptr<NNmodels_GlobalCache> initializeGlobalCache(const edm::ParameterSet&);
94  static void globalEndJob(const NNmodels_GlobalCache*){/*do nothing*/};
95 
96 private:
97  //----edm control---
98  void produce(edm::Event&, const edm::EventSetup&) override;
99 
100  //----private functions----
101  int tower_dIPhi(int& iPhi_1, int& iPhi_2) const;
102  int tower_dIEta(int& iEta_1, int& iEta_2) const;
103  int endcap_iphi(float& phi) const;
104  int endcap_ieta(float& eta) const;
105  float inputQuantizer(float inputF, float LSB, int nbits);
106  float inputScaler(float inputF, std::string feature);
107 
108  //----tokens and handles----
111 
114 
117 
118  //----private variables----
123 
128  double CB_CE_split;
129 
130  double IdWp90_CB;
131  double IdWp95_CB;
132  double IdWp99_CB;
133 
134  double IdWp90_CE;
135  double IdWp95_CE;
136  double IdWp99_CE;
137 
138  bool DEBUG;
139 
140  // hardoced dimensions of the tower clusters
141  const int seedIdx = 22;
142  const int IEta_dim = 5;
143  const int IPhi_dim = 9;
144  const float Eta_dim = 0.2;
145  const float Phi_dim = 0.4;
146  const float Eta_dim_seed = 0.35;
147  const float Phi_dim_seed = 0.7;
148  const float Eta_limit = 2.83;
149 
150  // classes of objects used only in this producer
152  public:
153  float towerEta = -99.;
154  float towerPhi = -99.;
155  float towerEm = 0.;
156  float towerHad = 0.;
157  float l1egTowerEt = 0.;
158  float towerEt = 0.;
159  int towerIeta = -99;
160  int towerIphi = -99;
161  bool isBarrel = true;
162  bool stale = false;
163  bool stale4seed = false;
164  };
165 
167  public:
168  bool barrelSeeded = false;
169  int seedIeta = -99;
170  int seedIphi = -99;
171  float seedEta = -99.;
172  float seedPhi = -99.;
173  float rawEt = 0.;
174  float IDscore = -99.;
175  float calibPt = -99.;
176 
177  std::vector<SimpleTowerHit> towerHits;
178 
179  void InitHits(int N, int M) { towerHits.resize(N * M); }
180  };
181 
183  public:
184  float pt = -99.;
185  float eta = -99.;
186  float phi = -99.;
187  float showerlength = -99.;
188  float coreshowerlength = -99.;
189  float spptot = -99.;
190  float szz = -99.;
191  float srrtot = -99.;
192  float meanz = -99.;
193  bool stale = false;
194  };
195 };
196 
197 /*
198 ████████ ██ ██ ██████ ████████ █████ ██ ██ ███ ███ ██ ███ ██ █████ ████████ ██████ ██████
199  ██ ██ ██ ██ ██ ██ ██ ██ ██ ████ ████ ██ ████ ██ ██ ██ ██ ██ ██ ██ ██
200  ██ ███████ █████ ██ ███████ ██ ██ ██ ████ ██ ██ ██ ██ ██ ███████ ██ ██ ██ ██████
201  ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██
202  ██ ██ ██ ██████ ██ ██ ██ ███████ ██ ██ ██ ██ ████ ██ ██ ██ ██████ ██ ██
203 */
204 
205 std::unique_ptr<NNmodels_GlobalCache> L1NNCaloTauProducer::initializeGlobalCache(const edm::ParameterSet& iConfig) {
206  edm::LogInfo("Initialization") << "Init NN models Global Cache " << std::endl;
207 
208  std::unique_ptr<NNmodels_GlobalCache> GlobalCache(new NNmodels_GlobalCache);
209 
210  GlobalCache->CNNmodel_CB_path = iConfig.getParameter<std::string>("CNNmodel_CB_path");
211  GlobalCache->DNNident_CB_path = iConfig.getParameter<std::string>("DNNident_CB_path");
212  GlobalCache->DNNcalib_CB_path = iConfig.getParameter<std::string>("DNNcalib_CB_path");
213  GlobalCache->CNNmodel_CE_path = iConfig.getParameter<std::string>("CNNmodel_CE_path");
214  GlobalCache->DNNident_CE_path = iConfig.getParameter<std::string>("DNNident_CE_path");
215  GlobalCache->DNNcalib_CE_path = iConfig.getParameter<std::string>("DNNcalib_CE_path");
216  GlobalCache->FeatScaler_CE_path = iConfig.getParameter<std::string>("FeatScaler_CE_path");
217 
218  // Create sessions for Tensorflow inferece
219  (GlobalCache->CNNmodel_CB) = tensorflow::loadGraphDef(edm::FileInPath((GlobalCache->CNNmodel_CB_path)).fullPath());
220  (GlobalCache->CNNmodel_CBsession) = tensorflow::createSession((GlobalCache->CNNmodel_CB));
221 
222  (GlobalCache->DNNident_CB) = tensorflow::loadGraphDef(edm::FileInPath((GlobalCache->DNNident_CB_path)).fullPath());
223  (GlobalCache->DNNident_CBsession) = tensorflow::createSession((GlobalCache->DNNident_CB));
224 
225  (GlobalCache->DNNcalib_CB) = tensorflow::loadGraphDef(edm::FileInPath((GlobalCache->DNNcalib_CB_path)).fullPath());
226  (GlobalCache->DNNcalib_CBsession) = tensorflow::createSession((GlobalCache->DNNcalib_CB));
227 
228  (GlobalCache->CNNmodel_CE) = tensorflow::loadGraphDef(edm::FileInPath((GlobalCache->CNNmodel_CE_path)).fullPath());
229  (GlobalCache->CNNmodel_CEsession) = tensorflow::createSession((GlobalCache->CNNmodel_CE));
230 
231  (GlobalCache->DNNident_CE) = tensorflow::loadGraphDef(edm::FileInPath((GlobalCache->DNNident_CE_path)).fullPath());
232  (GlobalCache->DNNident_CEsession) = tensorflow::createSession((GlobalCache->DNNident_CE));
233 
234  (GlobalCache->DNNcalib_CE) = tensorflow::loadGraphDef(edm::FileInPath((GlobalCache->DNNcalib_CE_path)).fullPath());
235  (GlobalCache->DNNcalib_CEsession) = tensorflow::createSession((GlobalCache->DNNcalib_CE));
236 
237  // Read features scaler
238  boost::property_tree::read_json(edm::FileInPath((GlobalCache->FeatScaler_CE_path)).fullPath(),
239  (GlobalCache->FeatScaler_CE));
240 
241  return GlobalCache;
242 }
243 
244 // ----Constructor and Destructor -----
246  : l1TowersToken(consumes<l1tp2::CaloTowerCollection>(iConfig.getParameter<edm::InputTag>("l1CaloTowers"))),
247  hgcalTowersToken(consumes<l1t::HGCalTowerBxCollection>(iConfig.getParameter<edm::InputTag>("hgcalTowers"))),
248 
249  HGClusterToken(
250  consumes<l1t::HGCalMulticlusterBxCollection>(iConfig.getParameter<edm::InputTag>("HgcalClusters"))),
251  scenario(UseEmInterp::No),
252  preEmId(iConfig.getParameter<std::string>("preEmId")),
253  VsPuId(iConfig.getParameter<edm::ParameterSet>("VsPuId")),
254 
255  EcalEtMinForClustering(iConfig.getParameter<double>("EcalEtMinForClustering")),
256  HcalEtMinForClustering(iConfig.getParameter<double>("HcalEtMinForClustering")),
257  EtMinForSeeding(iConfig.getParameter<double>("EtMinForSeeding")),
258  EtaRestriction(iConfig.getParameter<double>("EtaRestriction")),
259  CB_CE_split(iConfig.getParameter<double>("CB_CE_split")),
260 
261  IdWp90_CB(iConfig.getParameter<double>("IdWp90_CB")),
262  IdWp95_CB(iConfig.getParameter<double>("IdWp95_CB")),
263  IdWp99_CB(iConfig.getParameter<double>("IdWp99_CB")),
264 
265  IdWp90_CE(iConfig.getParameter<double>("IdWp90_CE")),
266  IdWp95_CE(iConfig.getParameter<double>("IdWp95_CE")),
267  IdWp99_CE(iConfig.getParameter<double>("IdWp99_CE")),
268 
269  DEBUG(iConfig.getParameter<bool>("DEBUG")) {
270  // Initialize HGCAL BDTs
271  if (!VsPuId.method().empty()) {
273  }
274 
275  // Create produced outputs
276  produces<BXVector<l1t::Tau>>("L1NNCaloTauCollectionBXV");
277 
278  // Settings output
279  edm::LogInfo("Settings") << "EtaRestriction = " << EtaRestriction << " , CB_CE_split = " << CB_CE_split
280  << " , EtMinForSeeding = " << EtMinForSeeding
281  << " , HcalTpEtMin = " << HcalEtMinForClustering
282  << " , EcalTpEtMin = " << EcalEtMinForClustering << std::endl;
283 }
284 
286  // Output collection
287  std::unique_ptr<BXVector<l1t::Tau>> L1NNCaloTauCollectionBXV(new l1t::TauBxCollection);
288 
289  // Create and Fill collection of all calotowers and their attributes
290  std::vector<SimpleTowerHit> l1CaloTowers;
291 
293  int warnings = 0;
294  for (auto& hit : *l1CaloTowerHandle.product()) {
295  // Skip this weird towers and store warning
296  if (hit.towerIEta() == -1016 && hit.towerIPhi() == -962) {
297  warnings += 1;
298  continue;
299  }
300 
301  SimpleTowerHit l1Hit;
302  l1Hit.isBarrel = true;
303  l1Hit.l1egTowerEt = hit.l1egTowerEt();
304  l1Hit.towerEta = hit.towerEta();
305  l1Hit.towerPhi = hit.towerPhi();
306  l1Hit.towerEm = hit.ecalTowerEt();
307  l1Hit.towerHad = hit.hcalTowerEt();
308  l1Hit.towerEt = l1Hit.towerEm + l1Hit.towerHad + l1Hit.l1egTowerEt;
309  l1Hit.towerIeta = hit.towerIEta();
310  l1Hit.towerIphi = hit.towerIPhi();
311 
312  l1CaloTowers.push_back(l1Hit);
313  }
314  if (warnings != 0 && DEBUG) {
315  edm::LogWarning("BrokenTowers") << " ** WARNING : FOUND " << warnings
316  << " TOWERS WITH towerIeta=-1016 AND towerIphi=-962" << std::endl;
317  }
318 
320  for (auto& hit : *hgcalTowersHandle.product()) {
321  SimpleTowerHit l1Hit;
322  l1Hit.isBarrel = false;
323  l1Hit.l1egTowerEt = 0.0;
324  l1Hit.towerEta = hit.eta();
325  l1Hit.towerPhi = hit.phi();
326  l1Hit.towerEm = hit.etEm();
327  l1Hit.towerHad = hit.etHad();
328  l1Hit.towerEt = l1Hit.towerEm + l1Hit.towerHad;
329  l1Hit.towerIeta = endcap_ieta(l1Hit.towerEta); // computed and filled but not used
330  l1Hit.towerIphi = endcap_iphi(l1Hit.towerPhi); // computed and filled but not used
331 
332  l1CaloTowers.push_back(l1Hit);
333  }
334 
335  // Sort the ECAL+HCAL+L1EGs tower sums based on total ET
337  return a.towerEt > b.towerEt;
338  });
339 
340  // Create and Fill the collection of 3D clusters and their attributes
341  std::vector<SimpleHGCluster> AllHGClusters;
343 
344  for (auto cl3dIt = HGClusterHandle->begin(0); cl3dIt != HGClusterHandle->end(0); ++cl3dIt) {
345  auto& cl3d = *cl3dIt;
346 
347  // Implement cl3d PU ID as done in
348  // https://github.com/cms-sw/cmssw/blob/master/L1Trigger/Phase2L1ParticleFlow/plugins/PFClusterProducerFromHGC3DClusters.cc#L120
349  bool isEM = preEmId(*cl3dIt);
350  l1t::PFCluster cluster(cl3d.pt(), cl3d.eta(), cl3d.phi(), cl3d.hOverE());
351  if (scenario == UseEmInterp::EmOnly) // for emID objs, use EM interp as pT and set H = 0
352  {
353  if (isEM) {
354  float pt_new = cl3d.iPt(l1t::HGCalMulticluster::EnergyInterpretation::EM);
355  float hoe_new = 0.;
356  cluster = l1t::PFCluster(pt_new, cl3d.eta(), cl3d.phi(), hoe_new, isEM);
357  }
358  } else if (scenario == UseEmInterp::AllKeepHad) // for all objs, replace EM part with EM interp, preserve H
359  {
360  float had_old = cl3d.pt() - cluster.emEt();
361  float em_new = cl3d.iPt(l1t::HGCalMulticluster::EnergyInterpretation::EM);
362  float pt_new = had_old + em_new;
363  float hoe_new = em_new > 0 ? (had_old / em_new) : -1;
364  cluster = l1t::PFCluster(pt_new, cl3d.eta(), cl3d.phi(), hoe_new, isEM);
365  } else if (scenario == UseEmInterp::AllKeepTot) // for all objs, replace EM part with EM interp, preserve pT
366  {
367  float em_new = cl3d.iPt(l1t::HGCalMulticluster::EnergyInterpretation::EM);
368  float hoe_new = em_new > 0 ? (cl3d.pt() / em_new - 1) : -1;
369  cluster = l1t::PFCluster(cl3d.pt(), cl3d.eta(), cl3d.phi(), hoe_new, isEM);
370  }
371 
372  if (!VsPuId.method().empty()) {
373  int id = VsPuId.passID(*cl3dIt, cluster);
374  if (!id) {
375  continue;
376  } // skip cl3d if it does not pass puid
377  }
378 
379  SimpleHGCluster HGCluster;
380  HGCluster.pt = cl3d.pt();
381  HGCluster.eta = cl3d.eta();
382  HGCluster.phi = cl3d.phi();
383  HGCluster.showerlength = cl3d.showerLength();
384  HGCluster.coreshowerlength = cl3d.coreShowerLength();
385  HGCluster.spptot = cl3d.sigmaPhiPhiTot();
386  HGCluster.szz = cl3d.sigmaZZ();
387  HGCluster.srrtot = cl3d.sigmaRRTot();
388  HGCluster.meanz = cl3d.zBarycenter();
389 
390  AllHGClusters.push_back(HGCluster);
391  }
392 
393  // Order the collection in pt (the input to the GCT will be pt ordered)
394  std::sort(begin(AllHGClusters), end(AllHGClusters), [](const SimpleHGCluster& a, SimpleHGCluster& b) {
395  return a.pt > b.pt;
396  });
397 
398  // Make NxM TowerClusters and HGClusters collections for TauMinator
399  std::vector<SimpleTowerCluster> l1TowerClustersNxM_CB;
400  std::vector<SimpleTowerCluster> l1TowerClustersNxM_CE;
401  std::vector<SimpleHGCluster> HGClusters;
402 
403  // Supporting collection of endcap clusters before cl3d matching
404  std::vector<SimpleTowerCluster> AllL1TowerClustersNxM_CE;
405 
406  bool caloTauSeedingFinished = false;
407  // Loop for seeding of clNxM objects
408  while (!caloTauSeedingFinished) {
409  SimpleTowerCluster clNxM;
410  clNxM.InitHits(IEta_dim, IPhi_dim);
411  bool seeded = false;
412 
413  for (auto& l1CaloTower : l1CaloTowers) {
414  // Skip seeding in towers that would make the cluster extend in HF
415  // Skip l1CaloTowers which are already used by this clusters' mask
416  if (abs(l1CaloTower.towerEta) > Eta_limit || abs(l1CaloTower.towerEta) > EtaRestriction ||
417  l1CaloTower.stale4seed) {
418  continue;
419  }
420 
421  // If not seded do the seeding
422  if (!seeded) {
423  // The leading unused tower has ET < min, stop jet clustering
424  if (l1CaloTower.towerEt < EtMinForSeeding) {
425  caloTauSeedingFinished = true;
426  continue;
427  }
428 
429  clNxM.seedIeta = l1CaloTower.towerIeta;
430  clNxM.seedIphi = l1CaloTower.towerIphi;
431  clNxM.seedEta = l1CaloTower.towerEta;
432  clNxM.seedPhi = l1CaloTower.towerPhi;
433  if (l1CaloTower.isBarrel) {
434  clNxM.barrelSeeded = true;
435  }
436 
437  clNxM.rawEt += l1CaloTower.towerEt;
438  clNxM.towerHits[seedIdx] = l1CaloTower;
439  l1CaloTower.stale4seed = true;
440  l1CaloTower.stale = true;
441  seeded = true;
442 
443  continue;
444  }
445 
446  int d_iEta = 99;
447  int d_iPhi = 99;
448  float d_Eta = 99.;
449  float d_Phi = 99.;
450  // Ese iEta/iPhi comparisons in the barrel and eta/phi in HGCal
451  if (clNxM.barrelSeeded && l1CaloTower.isBarrel) {
452  d_iEta = tower_dIEta(l1CaloTower.towerIeta, clNxM.seedIeta);
453  d_iPhi = tower_dIPhi(l1CaloTower.towerIphi, clNxM.seedIphi);
454  } else {
455  d_Eta = l1CaloTower.towerEta - clNxM.seedEta;
456  d_Phi = reco::deltaPhi(l1CaloTower.towerPhi, clNxM.seedPhi);
457  }
458 
459  // Stale tower for seeding if it would lead to overalp between clusters
460  if ((abs(d_iEta) <= IEta_dim - 1 && abs(d_iPhi) <= IPhi_dim - 1) ||
461  (abs(d_Eta) < Eta_dim_seed && abs(d_Phi) < Phi_dim_seed)) {
462  l1CaloTower.stale4seed = true;
463  }
464 
465  } // End for loop over TPs
466 
467  // Pushback seeds split in barrel and endcap
468  if (seeded) {
469  if (abs(clNxM.seedEta) < CB_CE_split) {
470  l1TowerClustersNxM_CB.push_back(clNxM);
471  } else {
472  AllL1TowerClustersNxM_CE.push_back(clNxM);
473  }
474  }
475 
476  } // End while loop of TowerClusters seeding
477 
478  // Loop for barrel NxM TowerClusters clustering starting from the seeds
479  for (auto& clNxM : l1TowerClustersNxM_CB) {
480  for (auto& l1CaloTower : l1CaloTowers) {
481  // Skip l1CaloTowers which are already used
482  if (l1CaloTower.stale) {
483  continue;
484  }
485 
486  int d_iEta = 99;
487  int d_iPhi = 99;
488  float d_Eta = 99.;
489  float d_Phi = 99.;
490  int hitIdx = 99.;
491  // Use iEta/iPhi comparisons in the barrel and use eta/phi in HGCal
492  if (l1CaloTower.isBarrel) {
493  d_iEta = tower_dIEta(l1CaloTower.towerIeta, clNxM.seedIeta);
494  d_iPhi = tower_dIPhi(l1CaloTower.towerIphi, clNxM.seedIphi);
495 
496  hitIdx = d_iEta * IPhi_dim + d_iPhi + seedIdx;
497  } else {
498  d_Eta = l1CaloTower.towerEta - clNxM.seedEta;
499  d_Phi = reco::deltaPhi(l1CaloTower.towerPhi, clNxM.seedPhi);
500 
501  int dieta = d_Eta / 0.0807; // minimal difference in endcap is 0.0808
502  int diphi = d_Phi / 0.0872;
503  hitIdx = dieta * IPhi_dim + diphi + seedIdx;
504  }
505 
506  // Cluster all towers in a NxM towers mask
507  if ((abs(d_iEta) <= (IEta_dim - 1) / 2 && abs(d_iPhi) <= (IPhi_dim - 1) / 2) ||
508  (abs(d_Eta) < Eta_dim && abs(d_Phi) < Phi_dim)) {
509  clNxM.rawEt += l1CaloTower.towerEt;
510  clNxM.towerHits[hitIdx] = l1CaloTower;
511  l1CaloTower.stale = true;
512  }
513 
514  } // End for loop over TPs
515 
516  } // End while loop of barrel TowerClusters creation
517 
518  // In the endcap cross-loop over clNxM and cl3d to match them
519  // (we can do it before full clustering just using the seed info)
520  for (auto& clNxM : AllL1TowerClustersNxM_CE) {
521  bool matched = false;
522  for (auto& HGCluster : AllHGClusters) {
523  // In case the clNxM or HGCluster have already been matched just continue through the list to the end
524  // only use cl3ds above 4GeV
525  if (matched || HGCluster.stale || HGCluster.pt < 4) {
526  continue;
527  }
528 
529  float d_Eta = HGCluster.eta - clNxM.seedEta;
530  float d_Phi = reco::deltaPhi(HGCluster.phi, clNxM.seedPhi);
531  float d_R2 = pow(d_Eta, 2) + pow(d_Phi, 2);
532 
533  if (d_R2 < 0.25) {
534  HGCluster.stale = true;
535  HGClusters.push_back(HGCluster);
536  l1TowerClustersNxM_CE.push_back(clNxM);
537  matched = true;
538  }
539 
540  } // End for loop over cl3ds
541 
542  } // End for loop over clNxM
543 
544  // Loop for endcap matched NxM TowerClusters clustering starting from the seeds just found
545  for (auto& clNxM : l1TowerClustersNxM_CE) {
546  for (auto& l1CaloTower : l1CaloTowers) {
547  // Skip l1CaloTowers which are already used
548  if (l1CaloTower.stale) {
549  continue;
550  }
551 
552  int d_iEta = 99;
553  int d_iPhi = 99;
554  float d_Eta = 99.;
555  float d_Phi = 99.;
556  int hitIdx = 99.;
557  // Use iEta/iPhi comparisons in the endcap and use eta/phi in HGCal
558  if (l1CaloTower.isBarrel) {
559  d_iEta = tower_dIEta(l1CaloTower.towerIeta, clNxM.seedIeta);
560  d_iPhi = tower_dIPhi(l1CaloTower.towerIphi, clNxM.seedIphi);
561 
562  hitIdx = d_iEta * IPhi_dim + d_iPhi + seedIdx;
563  } else {
564  d_Eta = l1CaloTower.towerEta - clNxM.seedEta;
565  d_Phi = reco::deltaPhi(l1CaloTower.towerPhi, clNxM.seedPhi);
566 
567  int dieta = d_Eta / 0.0807; // minimal difference in endcap is 0.0808
568  int diphi = d_Phi / 0.0872;
569  hitIdx = dieta * IPhi_dim + diphi + seedIdx;
570  }
571 
572  // Cluster all towers in a NxM towers mask
573  if ((abs(d_iEta) <= (IEta_dim - 1) / 2 && abs(d_iPhi) <= (IPhi_dim - 1) / 2) ||
574  (abs(d_Eta) < Eta_dim && abs(d_Phi) < Phi_dim)) {
575  clNxM.rawEt += l1CaloTower.towerEt;
576  clNxM.towerHits[hitIdx] = l1CaloTower;
577  l1CaloTower.stale = true;
578  }
579 
580  } // End for loop over TPs
581 
582  } // End while loop of endcap TowerClusters creation
583 
584  // Barrel TauMinator application
586  int batchSize_CB = (int)(l1TowerClustersNxM_CB.size());
587  tensorflow::TensorShape imageShape_CB({batchSize_CB, IEta_dim, IPhi_dim, 2});
588  tensorflow::TensorShape positionShape_CB({batchSize_CB, 2});
589  tensorflow::Tensor TowerClusterImage_CB(tensorflow::DT_FLOAT, imageShape_CB);
590  tensorflow::Tensor TowerClusterPosition_CB(tensorflow::DT_FLOAT, positionShape_CB);
591 
592  int clIdx = 0;
593  for (auto& clNxM : l1TowerClustersNxM_CB) {
594  // Fill inputs for Tensorflow inference
595  for (int eta = 0; eta < IEta_dim; ++eta) {
596  for (int phi = 0; phi < IPhi_dim; ++phi) {
597  int towerIdx = eta * IPhi_dim + phi;
598  TowerClusterImage_CB.tensor<float, 4>()(clIdx, eta, phi, 0) =
599  inputQuantizer(clNxM.towerHits[towerIdx].l1egTowerEt + clNxM.towerHits[towerIdx].towerEm, 0.25, 10);
600  TowerClusterImage_CB.tensor<float, 4>()(clIdx, eta, phi, 1) =
601  inputQuantizer(clNxM.towerHits[towerIdx].towerHad, 0.25, 10);
602  }
603  }
604 
605  TowerClusterPosition_CB.tensor<float, 2>()(clIdx, 0) = clNxM.seedEta;
606  TowerClusterPosition_CB.tensor<float, 2>()(clIdx, 1) = clNxM.seedPhi;
607 
608  clIdx++; // Increase batch index
609  }
610 
611  if (batchSize_CB >
612  0) // from CMSSW_14_0_X tensorflow does not seem to be able to deal with a tensor of dimension 0 anymore
613  {
614  // Apply CNN model
615  tensorflow::NamedTensorList CNNmodel_CBinputList = {{"TowerClusterImage", TowerClusterImage_CB},
616  {"TowerClusterPosition", TowerClusterPosition_CB}};
617  std::vector<tensorflow::Tensor> CNNmodel_CBoutputs;
618  tensorflow::run((globalCache()->CNNmodel_CBsession),
619  CNNmodel_CBinputList,
620  {"TauMinator_CB_conv/middleMan/concat"},
621  &CNNmodel_CBoutputs);
622  tensorflow::NamedTensorList DNN_CBinputsList = {{"middleMan", CNNmodel_CBoutputs[0]}};
623 
624  // Apply DNN for identification
625  std::vector<tensorflow::Tensor> DNN_CBoutputsIdent;
626  tensorflow::run((globalCache()->DNNident_CBsession),
627  DNN_CBinputsList,
628  {"TauMinator_CB_ident/sigmoid_IDout/Sigmoid"},
629  &DNN_CBoutputsIdent);
630 
631  // Apply DNN for calibration
632  std::vector<tensorflow::Tensor> DNN_CBoutputsCalib;
633  tensorflow::run((globalCache()->DNNcalib_CBsession),
634  DNN_CBinputsList,
635  {"TauMinator_CB_calib/LIN_DNNout/Relu"},
636  &DNN_CBoutputsCalib);
637 
638  // Fill TauMinator output variables of TowerClusters
639  clIdx = 0;
640  for (auto& clNxM : l1TowerClustersNxM_CB) {
641  clNxM.IDscore = DNN_CBoutputsIdent[0].matrix<float>()(0, clIdx);
642  clNxM.calibPt = DNN_CBoutputsCalib[0].matrix<float>()(0, clIdx);
643  clIdx++; // Increase batch index
644  }
645  }
646 
647  // Endcap TauMinator application
648  int batchSize_CE = (int)(l1TowerClustersNxM_CE.size());
649  tensorflow::TensorShape imageShape_CE({batchSize_CE, IEta_dim, IPhi_dim, 2});
650  tensorflow::TensorShape positionShape_CE({batchSize_CE, 2});
651  tensorflow::TensorShape cl3dfeatShape_CE({batchSize_CE, 8});
652  tensorflow::Tensor TowerClusterImage_CE(tensorflow::DT_FLOAT, imageShape_CE);
653  tensorflow::Tensor TowerClusterPosition_CE(tensorflow::DT_FLOAT, positionShape_CE);
654  tensorflow::Tensor Cl3dShapeFeatures_CE(tensorflow::DT_FLOAT, cl3dfeatShape_CE);
655 
656  clIdx = 0;
657  for (auto& clNxM : l1TowerClustersNxM_CE) {
658  // Indexing of cl3ds is the same as the one of clNxMs
659  SimpleHGCluster HGClu = HGClusters[clIdx];
660 
661  // Fill inputs for Tensorflow inference
662  for (int eta = 0; eta < IEta_dim; ++eta) {
663  for (int phi = 0; phi < IPhi_dim; ++phi) {
664  int towerIdx = eta * IPhi_dim + phi;
665  TowerClusterImage_CE.tensor<float, 4>()(clIdx, eta, phi, 0) =
666  inputQuantizer(clNxM.towerHits[towerIdx].l1egTowerEt + clNxM.towerHits[towerIdx].towerEm, 0.25, 10);
667  TowerClusterImage_CE.tensor<float, 4>()(clIdx, eta, phi, 1) =
668  inputQuantizer(clNxM.towerHits[towerIdx].towerHad, 0.25, 10);
669  }
670  }
671 
672  TowerClusterPosition_CE.tensor<float, 2>()(clIdx, 0) = clNxM.seedEta;
673  TowerClusterPosition_CE.tensor<float, 2>()(clIdx, 1) = clNxM.seedPhi;
674 
675  Cl3dShapeFeatures_CE.tensor<float, 2>()(clIdx, 0) = inputScaler(inputQuantizer(HGClu.pt, 0.25, 14), "pt");
676  Cl3dShapeFeatures_CE.tensor<float, 2>()(clIdx, 1) =
677  inputScaler(inputQuantizer(abs(HGClu.eta) - 1.321, 0.004, 9), "eta");
678  Cl3dShapeFeatures_CE.tensor<float, 2>()(clIdx, 2) = inputScaler(HGClu.showerlength, "showerlength");
679  Cl3dShapeFeatures_CE.tensor<float, 2>()(clIdx, 3) = inputScaler(HGClu.coreshowerlength, "coreshowerlength");
680  Cl3dShapeFeatures_CE.tensor<float, 2>()(clIdx, 4) =
681  inputScaler(inputQuantizer(HGClu.spptot, 0.0000153, 16), "spptot");
682  Cl3dShapeFeatures_CE.tensor<float, 2>()(clIdx, 5) = inputScaler(inputQuantizer(HGClu.szz, 0.00153, 16), "szz");
683  Cl3dShapeFeatures_CE.tensor<float, 2>()(clIdx, 6) =
684  inputScaler(inputQuantizer(HGClu.srrtot, 0.0000153, 16), "srrtot");
685  Cl3dShapeFeatures_CE.tensor<float, 2>()(clIdx, 7) =
686  inputScaler(inputQuantizer(10 * (abs(HGClu.meanz) - 321.05), 0.5, 12), "meanz");
687 
688  clIdx++; // Increase batch index
689  }
690 
691  if (batchSize_CE >
692  0) // from CMSSW_14_0_X tensorflow does not seem to be able to deal with a tensor of dimension 0 anymore
693  {
694  // Apply CNN model
695  tensorflow::NamedTensorList CNNmodel_CEinputList = {{"TowerClusterImage", TowerClusterImage_CE},
696  {"TowerClusterPosition", TowerClusterPosition_CE},
697  {"AssociatedCl3dFeatures", Cl3dShapeFeatures_CE}};
698  std::vector<tensorflow::Tensor> CNNmodel_CEoutputs;
699  tensorflow::run((globalCache()->CNNmodel_CEsession),
700  CNNmodel_CEinputList,
701  {"TauMinator_CE_conv/middleMan/concat"},
702  &CNNmodel_CEoutputs);
703  tensorflow::NamedTensorList DNN_CEinputsList = {{"middleMan", CNNmodel_CEoutputs[0]}};
704 
705  // Apply DNN for identification
706  std::vector<tensorflow::Tensor> DNN_CEoutputsIdent;
707  tensorflow::run((globalCache()->DNNident_CEsession),
708  DNN_CEinputsList,
709  {"TauMinator_CE_ident/sigmoid_IDout/Sigmoid"},
710  &DNN_CEoutputsIdent);
711 
712  // Apply DNN for calibration
713  std::vector<tensorflow::Tensor> DNN_CEoutputsCalib;
714  tensorflow::run((globalCache()->DNNcalib_CEsession),
715  DNN_CEinputsList,
716  {"TauMinator_CE_calib/LIN_DNNout/Relu"},
717  &DNN_CEoutputsCalib);
718 
719  // Fill TauMinator output variables of TowerClusters
720  clIdx = 0;
721  for (auto& clNxM : l1TowerClustersNxM_CE) {
722  clNxM.IDscore = DNN_CEoutputsIdent[0].matrix<float>()(0, clIdx);
723  clNxM.calibPt = DNN_CEoutputsCalib[0].matrix<float>()(0, clIdx);
724  clIdx++; // Increase batch index
725  }
726  }
727 
728  // Fill the output collection of L1 taus
729  for (auto& clNxM : l1TowerClustersNxM_CB) {
730  // Apply eta restriction
731  if (abs(clNxM.seedEta) > EtaRestriction) {
732  continue;
733  }
734 
735  // Assign increasing quality to higher scoring candidates
736  int quality = 0;
737  // 99% WP
738  if (clNxM.IDscore > IdWp99_CB) {
739  quality = 1;
740  }
741  // 95% WP
742  if (clNxM.IDscore > IdWp95_CB) {
743  quality = 2;
744  }
745  // 90% WP
746  if (clNxM.IDscore > IdWp90_CB) {
747  quality = 3;
748  }
749 
751  reco::Candidate::PolarLorentzVector(clNxM.calibPt, clNxM.seedEta, clNxM.seedPhi, 0);
752 
753  // store ID score multiplied by 10E4 to have good precision even using the Phase1 tau int iso format
754  // (this is stored just in case for possible additional offline studies)
755  // tau initialisation = (p4, pt, eta, phi, qual, iso)
756  l1t::Tau l1Tau = l1t::Tau(tauP4, clNxM.calibPt, clNxM.seedEta, clNxM.seedPhi, quality, clNxM.IDscore * 10E4);
757  l1Tau.setTowerIEta(clNxM.seedIeta);
758  l1Tau.setTowerIPhi(clNxM.seedIphi);
759  l1Tau.setRawEt(clNxM.rawEt);
760 
761  L1NNCaloTauCollectionBXV->push_back(0, l1Tau);
762  }
763 
764  for (auto& clNxM : l1TowerClustersNxM_CE) {
765  // Apply eta restriction
766  if (abs(clNxM.seedEta) > EtaRestriction) {
767  continue;
768  }
769 
770  // Assign increasing quality to higher scoring candidates
771  int quality = 0;
772  // 99% WP
773  if (clNxM.IDscore > IdWp99_CE) {
774  quality = 1;
775  }
776  // 95% WP
777  if (clNxM.IDscore > IdWp95_CE) {
778  quality = 2;
779  }
780  // 90% WP
781  if (clNxM.IDscore > IdWp90_CE) {
782  quality = 3;
783  }
784 
786  reco::Candidate::PolarLorentzVector(clNxM.calibPt, clNxM.seedEta, clNxM.seedPhi, 0);
787 
788  // store ID score multiplied by 10E4 to have good precision even using the Phase1 tau int iso format
789  // (this is stored just in case for possible additional offline studies)
790  // tau initialisation = (p4, pt, eta, phi, qual, iso)
791  l1t::Tau l1Tau = l1t::Tau(tauP4, clNxM.calibPt, clNxM.seedEta, clNxM.seedPhi, quality, clNxM.IDscore * 10E4);
792  l1Tau.setTowerIEta(clNxM.seedIeta);
793  l1Tau.setTowerIPhi(clNxM.seedIphi);
794  l1Tau.setRawEt(clNxM.rawEt);
795 
796  L1NNCaloTauCollectionBXV->push_back(0, l1Tau);
797  }
798 
799  // Fill output
800  iEvent.put(std::move(L1NNCaloTauCollectionBXV), "L1NNCaloTauCollectionBXV");
801 
802 } // End of produce function
803 
805  const int PI = 36;
806  int result = iPhi_1 - iPhi_2;
807  if (result > PI) {
808  result -= 2 * PI;
809  }
810  if (result <= -PI) {
811  result += 2 * PI;
812  }
813  return result;
814 }
815 
816 int L1NNCaloTauProducer::tower_dIEta(int& iEta_1, int& iEta_2) const {
817  if (iEta_1 * iEta_2 > 0) {
818  return iEta_1 - iEta_2;
819  } else {
820  if (iEta_1 > 0) {
821  return iEta_1 - iEta_2 - 1;
822  } else {
823  return iEta_1 - iEta_2 + 1;
824  }
825  }
826 }
827 
829  const float phi_step = 0.0872664;
830  if (phi > 0) {
831  return floor(phi / phi_step) + 1;
832  } else {
833  return floor(phi / phi_step) + 73;
834  }
835 }
836 
838  const float eta_step = 0.0845;
839  return floor(abs(eta) / eta_step) * std::copysign(1, eta);
840 }
841 
842 float L1NNCaloTauProducer::inputQuantizer(float inputF, float LSB, int nbits) {
843  return min(floor(inputF / LSB), float(pow(2, nbits) - 1)) * LSB;
844 }
845 
846 float L1NNCaloTauProducer::inputScaler(float inputF, std::string feature) {
847  float mean = (globalCache()->FeatScaler_CE).get_child(feature).get<float>("mean");
848  float std = (globalCache()->FeatScaler_CE).get_child(feature).get<float>("std");
849 
850  return (inputF - mean) / std;
851 }
852 
855 
856  desc.add<edm::InputTag>("l1CaloTowers", edm::InputTag("l1tEGammaClusterEmuProducer", "L1CaloTowerCollection"));
857  desc.add<edm::InputTag>("hgcalTowers", edm::InputTag("l1tHGCalTowerProducer", "HGCalTowerProcessor"));
858  desc.add<edm::InputTag>("HgcalClusters",
859  edm::InputTag("l1tHGCalBackEndLayer2Producer", "HGCalBackendLayer2Processor3DClustering"));
860 
861  desc.add<std::string>("preEmId", "hOverE < 0.3 && hOverE >= 0");
862  {
864  psd0.add<bool>("isPUFilter", true);
865  psd0.add<std::string>("preselection", "");
866  psd0.add<std::string>("method", "BDT");
867  {
869  vpsd2.add<std::string>("name");
870  vpsd2.add<std::string>("value");
871  std::vector<edm::ParameterSet> temp2;
872  temp2.reserve(5);
873  {
874  edm::ParameterSet temp3;
875  temp3.addParameter<std::string>("name", "eMax");
876  temp3.addParameter<std::string>("value", "eMax()");
877  temp2.push_back(temp3);
878  }
879  {
880  edm::ParameterSet temp3;
881  temp3.addParameter<std::string>("name", "eMaxOverE");
882  temp3.addParameter<std::string>("value", "eMax()/energy()");
883  temp2.push_back(temp3);
884  }
885  {
886  edm::ParameterSet temp3;
887  temp3.addParameter<std::string>("name", "sigmaPhiPhiTot");
888  temp3.addParameter<std::string>("value", "sigmaPhiPhiTot()");
889  temp2.push_back(temp3);
890  }
891  {
892  edm::ParameterSet temp3;
893  temp3.addParameter<std::string>("name", "sigmaRRTot");
894  temp3.addParameter<std::string>("value", "sigmaRRTot()");
895  temp2.push_back(temp3);
896  }
897  {
898  edm::ParameterSet temp3;
899  temp3.addParameter<std::string>("name", "triggerCells90percent");
900  temp3.addParameter<std::string>("value", "triggerCells90percent()");
901  temp2.push_back(temp3);
902  }
903  psd0.addVPSet("variables", vpsd2, temp2);
904  }
905  psd0.add<std::string>(
906  "weightsFile", "L1Trigger/Phase2L1ParticleFlow/data/hgcal_egID/Photon_Pion_vs_Neutrino_BDTweights_1116.xml.gz");
907  psd0.add<std::string>("wp", "-0.10");
908  desc.add<edm::ParameterSetDescription>("VsPuId", psd0);
909  }
910 
911  desc.add<double>("EcalEtMinForClustering", 0.0);
912  desc.add<double>("HcalEtMinForClustering", 0.0);
913  desc.add<double>("EtMinForSeeding", 2.5);
914  desc.add<double>("EtaRestriction", 2.4);
915  desc.add<double>("CB_CE_split", 1.55);
916 
917  desc.add<std::string>("CNNmodel_CB_path", "L1Trigger/L1CaloTrigger/data/Phase2_NNCaloTaus/v22/CNNmodel_CB.pb");
918  desc.add<std::string>("DNNident_CB_path", "L1Trigger/L1CaloTrigger/data/Phase2_NNCaloTaus/v22/DNNident_CB.pb");
919  desc.add<std::string>("DNNcalib_CB_path", "L1Trigger/L1CaloTrigger/data/Phase2_NNCaloTaus/v22/DNNcalib_CB.pb");
920  desc.add<std::string>("CNNmodel_CE_path", "L1Trigger/L1CaloTrigger/data/Phase2_NNCaloTaus/v22/CNNmodel_CE.pb");
921  desc.add<std::string>("DNNident_CE_path", "L1Trigger/L1CaloTrigger/data/Phase2_NNCaloTaus/v22/DNNident_CE.pb");
922  desc.add<std::string>("DNNcalib_CE_path", "L1Trigger/L1CaloTrigger/data/Phase2_NNCaloTaus/v22/DNNcalib_CE.pb");
923  desc.add<std::string>("FeatScaler_CE_path", "L1Trigger/L1CaloTrigger/data/Phase2_NNCaloTaus/Cl3dFeatScaler_CE.json");
924 
925  desc.add<double>("IdWp90_CB", 0.706);
926  desc.add<double>("IdWp95_CB", 0.3432);
927  desc.add<double>("IdWp99_CB", 0.0337);
928  desc.add<double>("IdWp90_CE", 0.5711);
929  desc.add<double>("IdWp95_CE", 0.2742);
930  desc.add<double>("IdWp99_CE", 0.0394);
931 
932  desc.add<bool>("DEBUG", false);
933 
934  descriptions.add("l1tNNCaloTauProducer", desc);
935 }
936 
edm::Handle< l1t::HGCalMulticlusterBxCollection > HGClusterHandle
boost::property_tree::ptree FeatScaler_CE
tensorflow::Session * CNNmodel_CEsession
constexpr double deltaPhi(double phi1, double phi2)
Definition: deltaPhi.h:26
tensorflow::GraphDef * DNNident_CB
std::vector< NamedTensor > NamedTensorList
Definition: TensorFlow.h:31
T getParameter(std::string const &) const
Definition: ParameterSet.h:307
tensorflow::GraphDef * CNNmodel_CB
ParameterDescriptionBase * addVPSet(U const &iLabel, ParameterSetDescription const &validator, std::vector< ParameterSet > const &defaults)
StringCutObjectSelector< l1t::HGCalMulticluster > preEmId
double pt() const final
transverse momentum
std::string fullPath() const
Definition: FileInPath.cc:161
int tower_dIEta(int &iEta_1, int &iEta_2) const
GraphDef * loadGraphDef(const std::string &pbFile)
Definition: TensorFlow.cc:129
Definition: Tau.h:20
T const * product() const
Definition: Handle.h:70
tensorflow::Session * DNNident_CBsession
tensorflow::Session * DNNcalib_CEsession
edm::Handle< l1t::HGCalTowerBxCollection > hgcalTowersHandle
static void fillDescriptions(edm::ConfigurationDescriptions &descriptions)
delete x;
Definition: CaloConfig.h:22
std::vector< SimpleTowerHit > towerHits
scenario
Definition: constants.h:219
tensorflow::Session * DNNident_CEsession
BXVector< HGCalTower > HGCalTowerBxCollection
Definition: HGCalTower.h:10
int endcap_ieta(float &eta) const
string quality
int iEvent
Definition: GenABIO.cc:224
tensorflow::GraphDef * DNNcalib_CE
int tower_dIPhi(int &iPhi_1, int &iPhi_2) const
void addParameter(std::string const &name, T const &value)
Definition: ParameterSet.h:136
L1NNCaloTauProducer(const edm::ParameterSet &, const NNmodels_GlobalCache *)
BXVector< HGCalMulticluster > HGCalMulticlusterBxCollection
void run(Session *session, const NamedTensorList &inputs, const std::vector< std::string > &outputNames, std::vector< Tensor > *outputs, const thread::ThreadPoolOptions &threadPoolOptions)
Definition: TensorFlow.cc:281
Abs< T >::type abs(const T &t)
Definition: Abs.h:22
tensorflow::GraphDef * CNNmodel_CE
#define DEFINE_FWK_MODULE(type)
Definition: MakerMacros.h:16
edm::EDGetTokenT< l1t::HGCalMulticlusterBxCollection > HGClusterToken
ParameterDescriptionBase * add(U const &iLabel, T const &value)
#define PI
Definition: QcdUeDQM.h:37
float inputScaler(float inputF, std::string feature)
edm::Handle< l1tp2::CaloTowerCollection > l1CaloTowerHandle
tensorflow::Session * DNNcalib_CBsession
float inputQuantizer(float inputF, float LSB, int nbits)
Session * createSession()
Definition: TensorFlow.cc:146
Log< level::Info, false > LogInfo
void setLogging(const std::string &level="3")
Definition: TensorFlow.cc:90
static std::unique_ptr< NNmodels_GlobalCache > initializeGlobalCache(const edm::ParameterSet &)
#define N
Definition: blowfish.cc:9
void produce(edm::Event &, const edm::EventSetup &) override
tensorflow::Session * CNNmodel_CBsession
double b
Definition: hdecay.h:120
void add(std::string const &label, ParameterSetDescription const &psetDescription)
int endcap_iphi(float &phi) const
l1tpf::HGC3DClusterEgID VsPuId
static void globalEndJob(const NNmodels_GlobalCache *)
HLT enums.
tensorflow::GraphDef * DNNcalib_CB
double a
Definition: hdecay.h:121
#define DEBUG
Definition: DMRChecker.cc:120
edm::EDGetToken hgcalTowersToken
edm::EDGetTokenT< l1tp2::CaloTowerCollection > l1TowersToken
Log< level::Warning, false > LogWarning
float passID(l1t::HGCalMulticluster c, l1t::PFCluster &cpf)
tensorflow::GraphDef * DNNident_CE
Power< A, B >::type pow(const A &a, const B &b)
Definition: Power.h:29
def move(src, dest)
Definition: eostools.py:511
math::PtEtaPhiMLorentzVector PolarLorentzVector
Lorentz vector.
Definition: Candidate.h:38