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<TauNNTFCache>> {
18 public:
19  explicit L1NNTauProducer(const edm::ParameterSet&, const TauNNTFCache*);
20  ~L1NNTauProducer() override;
21 
22  static void fillDescriptions(edm::ConfigurationDescriptions& descriptions);
23  static std::unique_ptr<TauNNTFCache> initializeGlobalCache(const edm::ParameterSet&);
24  static void globalEndJob(const TauNNTFCache*);
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>(
52  lNNFile.find("v0") == std::string::npos ? "input_1:0" : "dense_1_input:0", cache, lNNFile, fNParticles_);
53  produces<l1t::PFTauCollection>("L1PFTausNN");
54 }
55 std::unique_ptr<TauNNTFCache> L1NNTauProducer::initializeGlobalCache(const edm::ParameterSet& cfg) {
57  std::string lNNFile = cfg.getParameter<std::string>("NNFileName");
58  edm::FileInPath fp(lNNFile);
60  cache->graphDef = tensorflow::loadGraphDef(fp.fullPath());
61  return std::unique_ptr<TauNNTFCache>(cache);
62 }
64  if (cache->graphDef != nullptr) {
65  delete cache->graphDef;
66  }
67 }
70  iEvent.getByToken(fL1PFToken_, l1PFCandidates);
71 
72  std::vector<unique_ptr<l1t::PFCandidate>> pfChargedHadrons_sort_v;
73  std::vector<unique_ptr<l1t::PFCandidate>> pfChargedHadrons_seeds_v;
74  for (const auto& l1PFCand : *l1PFCandidates)
75  if ((l1PFCand.id() == l1t::PFCandidate::ChargedHadron || l1PFCand.id() == l1t::PFCandidate::Electron) &&
76  std::abs(l1PFCand.eta()) < track_trigger_eta_max)
77  pfChargedHadrons_sort_v.push_back(std::make_unique<l1t::PFCandidate>(l1PFCand));
78 
79  std::sort(
80  pfChargedHadrons_sort_v.begin(),
81  pfChargedHadrons_sort_v.end(),
82  [](std::unique_ptr<l1t::PFCandidate>& i, std::unique_ptr<l1t::PFCandidate>& j) { return (i->pt() > j->pt()); });
83 
84  auto lTaus = std::make_unique<l1t::PFTauCollection>();
85  if (pfChargedHadrons_sort_v.empty()) {
86  if (lTaus->empty()) {
87  PFTau dummy;
88  lTaus->push_back(dummy);
89  }
90  iEvent.put(std::move(lTaus), "L1PFTausNN");
91  return;
92  }
93  pfChargedHadrons_seeds_v.push_back(std::move(pfChargedHadrons_sort_v[0]));
94  for (unsigned int i0 = 1; i0 < pfChargedHadrons_sort_v.size(); i0++) {
95  bool pMatch = false;
96  for (unsigned int i1 = 0; i1 < pfChargedHadrons_seeds_v.size(); i1++) {
97  if (reco::deltaR2(*(pfChargedHadrons_seeds_v[i1]), *(pfChargedHadrons_sort_v[i0])) < fConeSize_ * fConeSize_)
98  pMatch = true;
99  }
100  if (pMatch)
101  continue;
102  pfChargedHadrons_seeds_v.push_back(std::move(pfChargedHadrons_sort_v[i0]));
103  if (int(pfChargedHadrons_seeds_v.size()) > fMaxTaus_ - 1)
104  break;
105  }
106  for (unsigned int i0 = 0; i0 < pfChargedHadrons_seeds_v.size(); i0++) {
107  addTau(*(pfChargedHadrons_seeds_v[i0]), (*l1PFCandidates), lTaus);
108  }
109  if (lTaus->empty()) {
110  PFTau dummy;
111  lTaus->push_back(dummy);
112  }
113  std::sort(lTaus->begin(), lTaus->end(), [](l1t::PFTau i, l1t::PFTau j) { return (i.pt() > j.pt()); });
114  iEvent.put(std::move(lTaus), "L1PFTausNN");
115 }
116 
117 // create taus based on grid structure
119  const l1t::PFCandidateCollection& iParts,
120  std::unique_ptr<l1t::PFTauCollection>& outputTaus) {
121  l1t::PFCandidateCollection pfTauCands;
122  math::PtEtaPhiMLorentzVector lTot(0, 0, 0, 0);
123  math::PtEtaPhiMLorentzVector lCand(0, 0, 0, 0);
124  int lId = 0;
125  for (const auto& l1PFCand : iParts) {
126  if (reco::deltaR2(iCand, l1PFCand) > fConeSize_ * fConeSize_)
127  continue;
128  math::PtEtaPhiMLorentzVector pVec(l1PFCand.pt(), l1PFCand.eta(), l1PFCand.phi(), 0);
129  lTot += pVec;
130  if (reco::deltaR2(iCand, l1PFCand) < fTauSize_ * fTauSize_ &&
131  (l1PFCand.id() == l1t::PFCandidate::Electron || l1PFCand.id() == l1t::PFCandidate::ChargedHadron ||
132  l1PFCand.id() == l1t::PFCandidate::Photon)) {
133  lId++;
134  lCand += pVec;
135  }
136  pfTauCands.push_back(l1PFCand);
137  }
138  if (lTot.Pt() < fSeedPt_)
139  return;
140  std::sort(
141  pfTauCands.begin(), pfTauCands.end(), [](l1t::PFCandidate i, l1t::PFCandidate j) { return (i.pt() > j.pt()); });
142  float NN = fTauNNId_->compute(iCand, pfTauCands);
143  math::PtEtaPhiMLorentzVector tempP4(lCand.Pt(), lCand.Eta(), lCand.Phi(), lCand.M());
144  l1t::PFTau l1PFTau(tempP4, NN, 0, lId);
145  outputTaus->push_back(l1PFTau);
146 }
148  // L1NNTauProducer
150  desc.add<std::string>("NNFileName", "L1Trigger/Phase2L1ParticleFlow/data/tau_3layer.pb");
151  desc.add<double>("tausize", 0.1);
152  desc.add<int>("maxtaus", 5);
153  desc.add<int>("nparticles", 10);
154  desc.add<double>("conesize", 0.4);
155  desc.add<double>("seedpt", 20);
156  desc.add<edm::InputTag>("L1PFObjects", edm::InputTag("L1PFProducer", "l1pfCandidates"));
157  descriptions.add("L1NNTauProducer", desc);
158 }
160 
l1t::PFCandidate::Photon
Definition: PFCandidate.h:15
PFCandidate.h
L1NNTauProducer::fL1PFToken_
edm::EDGetTokenT< vector< l1t::PFCandidate > > fL1PFToken_
Definition: L1NNTauProducer.cc:38
mps_fire.i
i
Definition: mps_fire.py:428
L1NNTauProducer::produce
void produce(edm::Event &iEvent, const edm::EventSetup &iSetup) override
Definition: L1NNTauProducer.cc:68
l1t::PFTau
Definition: PFTau.h:22
PFTau.h
L1NNTauProducer::fMaxTaus_
int fMaxTaus_
Definition: L1NNTauProducer.cc:36
edm::EDGetTokenT
Definition: EDGetToken.h:33
edm
HLT enums.
Definition: AlignableModifier.h:19
L1NNTauProducer::fillDescriptions
static void fillDescriptions(edm::ConfigurationDescriptions &descriptions)
Definition: L1NNTauProducer.cc:147
testProducerWithPsetDescEmpty_cfi.i1
i1
Definition: testProducerWithPsetDescEmpty_cfi.py:45
HLT_FULL_cff.InputTag
InputTag
Definition: HLT_FULL_cff.py:89301
edm::ParameterSetDescription
Definition: ParameterSetDescription.h:52
EDProducer.h
track_trigger_eta_max
static constexpr float track_trigger_eta_max
Definition: L1NNTauProducer.cc:41
personalPlayback.fp
fp
Definition: personalPlayback.py:523
L1NNTauProducer::addTau
void addTau(const l1t::PFCandidate &iCand, const l1t::PFCandidateCollection &iParts, std::unique_ptr< PFTauCollection > &outputTaus)
Definition: L1NNTauProducer.cc:118
edm::Handle
Definition: AssociativeIterator.h:50
l1t::PFCandidate::Electron
Definition: PFCandidate.h:15
TauNNTFCache
Definition: TauNNId.h:8
L1NNTauProducer::fSeedPt_
double fSeedPt_
Definition: L1NNTauProducer.cc:33
edm::FileInPath
Definition: FileInPath.h:61
MakerMacros.h
TauNNId.h
DEFINE_FWK_MODULE
#define DEFINE_FWK_MODULE(type)
Definition: MakerMacros.h:16
edm::ConfigurationDescriptions::add
void add(std::string const &label, ParameterSetDescription const &psetDescription)
Definition: ConfigurationDescriptions.cc:57
L1NNTauProducer::initializeGlobalCache
static std::unique_ptr< TauNNTFCache > initializeGlobalCache(const edm::ParameterSet &)
Definition: L1NNTauProducer.cc:55
fillDescriptions
static void fillDescriptions(edm::ConfigurationDescriptions &descriptions)
L1NNTauProducer::fTauNNId_
std::unique_ptr< TauNNId > fTauNNId_
Definition: L1NNTauProducer.cc:27
utilities.cache
def cache(function)
Definition: utilities.py:3
edm::ConfigurationDescriptions
Definition: ConfigurationDescriptions.h:28
L1NNTauProducer::fConeSize_
double fConeSize_
Definition: L1NNTauProducer.cc:34
edm::ParameterSet
Definition: ParameterSet.h:47
Event.h
deltaR.h
l1t
delete x;
Definition: CaloConfig.h:22
jetUpdater_cfi.sort
sort
Definition: jetUpdater_cfi.py:29
reco::deltaR2
constexpr auto deltaR2(const T1 &t1, const T2 &t2) -> decltype(t1.eta())
Definition: deltaR.h:16
l1t::PFCandidate::ChargedHadron
Definition: PFCandidate.h:15
createfilelist.int
int
Definition: createfilelist.py:10
iEvent
int iEvent
Definition: GenABIO.cc:224
trackerHitRTTI::vector
Definition: trackerHitRTTI.h:21
edm::stream::EDProducer
Definition: EDProducer.h:36
tensorflow::setLogging
void setLogging(const std::string &level="3")
Definition: TensorFlow.cc:15
edm::EventSetup
Definition: EventSetup.h:58
l1t::PFCandidateCollection
std::vector< l1t::PFCandidate > PFCandidateCollection
Definition: PFCandidate.h:57
AlCaHLTBitMon_QueryRunRegistry.string
string string
Definition: AlCaHLTBitMon_QueryRunRegistry.py:256
looper.cfg
cfg
Definition: looper.py:296
tensorflow::loadGraphDef
GraphDef * loadGraphDef(const std::string &pbFile)
Definition: TensorFlow.cc:68
submitPVResolutionJobs.desc
string desc
Definition: submitPVResolutionJobs.py:251
eostools.move
def move(src, dest)
Definition: eostools.py:511
L1NNTauProducer::~L1NNTauProducer
~L1NNTauProducer() override
Definition: L1NNTauProducer.cc:159
Frameworkfwd.h
L1NNTauProducer::globalEndJob
static void globalEndJob(const TauNNTFCache *)
Definition: L1NNTauProducer.cc:63
L1NNTauProducer::L1NNTauProducer
L1NNTauProducer(const edm::ParameterSet &, const TauNNTFCache *)
Definition: L1NNTauProducer.cc:43
math::PtEtaPhiMLorentzVector
PtEtaPhiMLorentzVectorD PtEtaPhiMLorentzVector
Lorentz vector with cartesian internal representation.
Definition: LorentzVector.h:25
funct::abs
Abs< T >::type abs(const T &t)
Definition: Abs.h:22
l1t::PFCandidate
Definition: PFCandidate.h:12
L1NNTauProducer::fNParticles_
int fNParticles_
Definition: L1NNTauProducer.cc:37
ParameterSet.h
dummy
Definition: DummySelector.h:38
dqmiolumiharvest.j
j
Definition: dqmiolumiharvest.py:66
L1NNTauProducer::fTauSize_
double fTauSize_
Definition: L1NNTauProducer.cc:35
edm::Event
Definition: Event.h:73
L1NNTauProducer
Definition: L1NNTauProducer.cc:17
edm::InputTag
Definition: InputTag.h:15