CMS 3D CMS Logo

L1NNTauProducer.cc
Go to the documentation of this file.
2 #include <cmath>
3 #include <vector>
4 
10 
14 
15 using namespace l1t;
16 
17 class L1NNTauProducer : public edm::stream::EDProducer<edm::GlobalCache<tensorflow::SessionCache>> {
18 public:
20  ~L1NNTauProducer() override;
21 
22  static void fillDescriptions(edm::ConfigurationDescriptions& descriptions);
23  static std::unique_ptr<tensorflow::SessionCache> initializeGlobalCache(const edm::ParameterSet&);
24  static void globalEndJob(const tensorflow::SessionCache*){};
25 
26 private:
27  std::unique_ptr<TauNNId> fTauNNId_;
28  void addTau(const l1t::PFCandidate& iCand,
29  const l1t::PFCandidateCollection& iParts,
30  std::unique_ptr<PFTauCollection>& outputTaus);
31  void produce(edm::Event& iEvent, const edm::EventSetup& iSetup) override;
32 
33  double fSeedPt_;
34  double fConeSize_;
35  double fTauSize_;
36  int fMaxTaus_;
39 };
40 
41 static constexpr float track_trigger_eta_max = 2.5;
42 
44  : fSeedPt_(cfg.getParameter<double>("seedpt")),
45  fConeSize_(cfg.getParameter<double>("conesize")),
46  fTauSize_(cfg.getParameter<double>("tausize")),
47  fMaxTaus_(cfg.getParameter<int>("maxtaus")),
48  fNParticles_(cfg.getParameter<int>("nparticles")),
49  fL1PFToken_(consumes<vector<l1t::PFCandidate>>(cfg.getParameter<edm::InputTag>("L1PFObjects"))) {
50  std::string lNNFile = cfg.getParameter<std::string>("NNFileName"); //,"L1Trigger/Phase2L1Taus/data/tau_3layer.pb");
51  fTauNNId_ = std::make_unique<TauNNId>(lNNFile.find("v0") == std::string::npos ? "input_1:0" : "dense_1_input:0",
52  cache->getSession(),
53  lNNFile,
54  fNParticles_);
55  produces<l1t::PFTauCollection>("L1PFTausNN");
56 }
57 
58 std::unique_ptr<tensorflow::SessionCache> L1NNTauProducer::initializeGlobalCache(const edm::ParameterSet& cfg) {
60  std::string graphPath = edm::FileInPath(cfg.getParameter<std::string>("NNFileName")).fullPath();
61  return std::make_unique<tensorflow::SessionCache>(graphPath);
62 }
63 
66  iEvent.getByToken(fL1PFToken_, l1PFCandidates);
67 
68  std::vector<unique_ptr<l1t::PFCandidate>> pfChargedHadrons_sort_v;
69  std::vector<unique_ptr<l1t::PFCandidate>> pfChargedHadrons_seeds_v;
70  for (const auto& l1PFCand : *l1PFCandidates)
71  if ((l1PFCand.id() == l1t::PFCandidate::ChargedHadron || l1PFCand.id() == l1t::PFCandidate::Electron) &&
72  std::abs(l1PFCand.eta()) < track_trigger_eta_max)
73  pfChargedHadrons_sort_v.push_back(std::make_unique<l1t::PFCandidate>(l1PFCand));
74 
75  std::sort(
76  pfChargedHadrons_sort_v.begin(),
77  pfChargedHadrons_sort_v.end(),
78  [](std::unique_ptr<l1t::PFCandidate>& i, std::unique_ptr<l1t::PFCandidate>& j) { return (i->pt() > j->pt()); });
79 
80  auto lTaus = std::make_unique<l1t::PFTauCollection>();
81  if (pfChargedHadrons_sort_v.empty()) {
82  if (lTaus->empty()) {
83  PFTau dummy;
84  lTaus->push_back(dummy);
85  }
86  iEvent.put(std::move(lTaus), "L1PFTausNN");
87  return;
88  }
89  pfChargedHadrons_seeds_v.push_back(std::move(pfChargedHadrons_sort_v[0]));
90  for (unsigned int i0 = 1; i0 < pfChargedHadrons_sort_v.size(); i0++) {
91  bool pMatch = false;
92  for (unsigned int i1 = 0; i1 < pfChargedHadrons_seeds_v.size(); i1++) {
93  if (reco::deltaR2(*(pfChargedHadrons_seeds_v[i1]), *(pfChargedHadrons_sort_v[i0])) < fConeSize_ * fConeSize_)
94  pMatch = true;
95  }
96  if (pMatch)
97  continue;
98  pfChargedHadrons_seeds_v.push_back(std::move(pfChargedHadrons_sort_v[i0]));
99  if (int(pfChargedHadrons_seeds_v.size()) > fMaxTaus_ - 1)
100  break;
101  }
102  for (unsigned int i0 = 0; i0 < pfChargedHadrons_seeds_v.size(); i0++) {
103  addTau(*(pfChargedHadrons_seeds_v[i0]), (*l1PFCandidates), lTaus);
104  }
105  if (lTaus->empty()) {
106  PFTau dummy;
107  lTaus->push_back(dummy);
108  }
109  std::sort(lTaus->begin(), lTaus->end(), [](l1t::PFTau i, l1t::PFTau j) { return (i.pt() > j.pt()); });
110  iEvent.put(std::move(lTaus), "L1PFTausNN");
111 }
112 
113 // create taus based on grid structure
115  const l1t::PFCandidateCollection& iParts,
116  std::unique_ptr<l1t::PFTauCollection>& outputTaus) {
117  l1t::PFCandidateCollection pfTauCands;
118  math::PtEtaPhiMLorentzVector lTot(0, 0, 0, 0);
119  math::PtEtaPhiMLorentzVector lCand(0, 0, 0, 0);
120  int lId = 0;
121  for (const auto& l1PFCand : iParts) {
122  if (reco::deltaR2(iCand, l1PFCand) > fConeSize_ * fConeSize_)
123  continue;
124  math::PtEtaPhiMLorentzVector pVec(l1PFCand.pt(), l1PFCand.eta(), l1PFCand.phi(), 0);
125  lTot += pVec;
126  if (reco::deltaR2(iCand, l1PFCand) < fTauSize_ * fTauSize_ &&
127  (l1PFCand.id() == l1t::PFCandidate::Electron || l1PFCand.id() == l1t::PFCandidate::ChargedHadron ||
128  l1PFCand.id() == l1t::PFCandidate::Photon)) {
129  lId++;
130  lCand += pVec;
131  }
132  pfTauCands.push_back(l1PFCand);
133  }
134  if (lTot.Pt() < fSeedPt_)
135  return;
136  std::sort(
137  pfTauCands.begin(), pfTauCands.end(), [](l1t::PFCandidate i, l1t::PFCandidate j) { return (i.pt() > j.pt()); });
138  float NN = fTauNNId_->compute(iCand, pfTauCands);
139  math::PtEtaPhiMLorentzVector tempP4(lCand.Pt(), lCand.Eta(), lCand.Phi(), lCand.M());
140  l1t::PFTau l1PFTau(tempP4, NN, 0, lId);
141  outputTaus->push_back(l1PFTau);
142 }
144  // L1NNTauProducer
146  desc.add<std::string>("NNFileName", "L1Trigger/Phase2L1ParticleFlow/data/tau_3layer.pb");
147  desc.add<double>("tausize", 0.1);
148  desc.add<int>("maxtaus", 5);
149  desc.add<int>("nparticles", 10);
150  desc.add<double>("conesize", 0.4);
151  desc.add<double>("seedpt", 20);
152  desc.add<edm::InputTag>("L1PFObjects", edm::InputTag("L1PFProducer", "l1pfCandidates"));
153  descriptions.add("L1NNTauProducer", desc);
154 }
156 
static constexpr float track_trigger_eta_max
std::string fullPath() const
Definition: FileInPath.cc:161
L1NNTauProducer(const edm::ParameterSet &, const tensorflow::SessionCache *)
std::vector< l1t::PFCandidate > PFCandidateCollection
Definition: PFCandidate.h:82
void produce(edm::Event &iEvent, const edm::EventSetup &iSetup) override
delete x;
Definition: CaloConfig.h:22
edm::EDGetTokenT< vector< l1t::PFCandidate > > fL1PFToken_
PtEtaPhiMLorentzVectorD PtEtaPhiMLorentzVector
Lorentz vector with cartesian internal representation.
Definition: LorentzVector.h:25
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
Abs< T >::type abs(const T &t)
Definition: Abs.h:22
#define DEFINE_FWK_MODULE(type)
Definition: MakerMacros.h:16
~L1NNTauProducer() override
void setLogging(const std::string &level="3")
Definition: TensorFlow.cc:14
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 &)
void add(std::string const &label, ParameterSetDescription const &psetDescription)
HLT enums.
def cache(function)
Definition: utilities.py:3
static void globalEndJob(const tensorflow::SessionCache *)
def move(src, dest)
Definition: eostools.py:511