CMS 3D CMS Logo

L1SeedConePFJetProducer.cc
Go to the documentation of this file.
1 
2 #include <vector>
3 #include <numeric>
4 #include <string>
5 
7 // FRAMEWORK HEADERS
13 
18 
19 // bitwise emulation headers
22 
24 public:
26  ~L1SeedConePFJetProducer() override;
28 
29 private:
32  void produce(edm::StreamID, edm::Event& iEvent, const edm::EventSetup& iSetup) const override;
34 
35  const float coneSize;
36  const unsigned nJets;
37  const bool HW;
38  const bool debug;
39  const bool doCorrections;
43 
44  std::vector<l1t::PFJet> processEvent_SW(std::vector<edm::Ptr<l1t::PFCandidate>>& parts) const;
45  std::vector<l1t::PFJet> processEvent_HW(std::vector<edm::Ptr<l1t::PFCandidate>>& parts) const;
46 
48 
49  std::pair<std::vector<L1SCJetEmu::Particle>, std::unordered_map<const l1t::PFCandidate*, edm::Ptr<l1t::PFCandidate>>>
51 
52  std::vector<l1t::PFJet> convertHWToEDM(
53  std::vector<L1SCJetEmu::Jet> hwJets,
54  std::unordered_map<const l1t::PFCandidate*, edm::Ptr<l1t::PFCandidate>> constituentMap) const;
55 };
56 
58  : coneSize(cfg.getParameter<double>("coneSize")),
59  nJets(cfg.getParameter<unsigned>("nJets")),
60  HW(cfg.getParameter<bool>("HW")),
61  debug(cfg.getParameter<bool>("debug")),
62  doCorrections(cfg.getParameter<bool>("doCorrections")),
64  l1PFToken(consumes<std::vector<l1t::PFCandidate>>(cfg.getParameter<edm::InputTag>("L1PFObjects"))) {
65  produces<l1t::PFJetCollection>();
66  if (doCorrections) {
68  cfg.getParameter<std::string>("correctorFile"), cfg.getParameter<std::string>("correctorDir"), -1., debug, HW);
69  }
70 }
71 
74  const edm::EventSetup& iSetup) const {
75  std::unique_ptr<l1t::PFJetCollection> newPFJetCollection(new l1t::PFJetCollection);
76 
78  iEvent.getByToken(l1PFToken, l1PFCandidates);
79 
80  std::vector<edm::Ptr<l1t::PFCandidate>> particles;
81  for (unsigned i = 0; i < (*l1PFCandidates).size(); i++) {
82  particles.push_back(edm::Ptr<l1t::PFCandidate>(l1PFCandidates, i));
83  }
84 
85  std::vector<l1t::PFJet> jets;
86  if (HW) {
88  } else {
90  }
91 
92  std::sort(jets.begin(), jets.end(), [](l1t::PFJet i, l1t::PFJet j) { return (i.pt() > j.pt()); });
93  newPFJetCollection->swap(jets);
94  iEvent.put(std::move(newPFJetCollection));
95 }
96 
98 // DESTRUCTOR
100 
102  l1t::PFCandidate seed = *parts.at(0);
103 
104  auto sumpt = [](float a, const edm::Ptr<l1t::PFCandidate>& b) { return a + b->pt(); };
105 
106  // Sum the pt
107  float pt = std::accumulate(parts.begin(), parts.end(), 0., sumpt);
108 
109  // pt weighted d eta
110  std::vector<float> pt_deta;
111  pt_deta.resize(parts.size());
112  std::transform(parts.begin(), parts.end(), pt_deta.begin(), [&seed, &pt](const edm::Ptr<l1t::PFCandidate>& part) {
113  return (part->pt() / pt) * (part->eta() - seed.eta());
114  });
115  // Accumulate the pt weighted etas. Init to the seed eta, start accumulating at begin()+1 to skip seed
116  float eta = std::accumulate(pt_deta.begin() + 1, pt_deta.end(), seed.eta());
117 
118  // pt weighted d phi
119  std::vector<float> pt_dphi;
120  pt_dphi.resize(parts.size());
121  std::transform(parts.begin(), parts.end(), pt_dphi.begin(), [&seed, &pt](const edm::Ptr<l1t::PFCandidate>& part) {
122  return (part->pt() / pt) * reco::deltaPhi(part->phi(), seed.phi());
123  });
124  // Accumulate the pt weighted phis. Init to the seed phi, start accumulating at begin()+1 to skip seed
125  float phi = std::accumulate(pt_dphi.begin() + 1, pt_dphi.end(), seed.phi());
126 
127  l1t::PFJet jet(pt, eta, phi);
128  for (auto it = parts.begin(); it != parts.end(); it++) {
129  jet.addConstituent(*it);
130  }
131 
132  if (doCorrections) {
133  jet.calibratePt(corrector.correctedPt(jet.pt(), jet.eta()));
134  }
135 
136  return jet;
137 }
138 
140  // The floating point algorithm simulation
141  std::stable_sort(work.begin(), work.end(), [](edm::Ptr<l1t::PFCandidate> i, edm::Ptr<l1t::PFCandidate> j) {
142  return (i->pt() > j->pt());
143  });
144  std::vector<l1t::PFJet> jets;
145  jets.reserve(nJets);
146  while (!work.empty() && jets.size() < nJets) {
147  // Take the first (highest pt) candidate as a seed
149  // Get the particles within a coneSize of the seed
150  std::vector<edm::Ptr<l1t::PFCandidate>> particlesInCone;
151  std::copy_if(
152  work.begin(), work.end(), std::back_inserter(particlesInCone), [&](const edm::Ptr<l1t::PFCandidate>& part) {
153  return reco::deltaR<l1t::PFCandidate, l1t::PFCandidate>(*seed, *part) <= coneSize;
154  });
155  jets.push_back(makeJet_SW(particlesInCone));
156  // remove the clustered particles
157  work.erase(std::remove_if(work.begin(),
158  work.end(),
159  [&](const edm::Ptr<l1t::PFCandidate>& part) {
160  return reco::deltaR<l1t::PFCandidate, l1t::PFCandidate>(*seed, *part) <= coneSize;
161  }),
162  work.end());
163  }
164  return jets;
165 }
166 
168  // The fixed point emulator
169  // Convert the EDM format to the hardware format, and call the standalone emulator
170  std::pair<std::vector<L1SCJetEmu::Particle>, std::unordered_map<const l1t::PFCandidate*, edm::Ptr<l1t::PFCandidate>>>
172  std::vector<L1SCJetEmu::Jet> jets = emulator.emulateEvent(particles.first);
173  return convertHWToEDM(jets, particles.second);
174 }
175 
176 std::pair<std::vector<L1SCJetEmu::Particle>, std::unordered_map<const l1t::PFCandidate*, edm::Ptr<l1t::PFCandidate>>>
178  std::vector<l1ct::PuppiObjEmu> hwParticles;
179  std::unordered_map<const l1t::PFCandidate*, edm::Ptr<l1t::PFCandidate>> candidateMap;
180  std::for_each(edmParticles.begin(), edmParticles.end(), [&](edm::Ptr<l1t::PFCandidate>& edmParticle) {
181  l1ct::PuppiObjEmu particle;
182  particle.initFromBits(edmParticle->encodedPuppi64());
183  particle.srcCand = edmParticle.get();
184  candidateMap.insert(std::make_pair(edmParticle.get(), edmParticle));
185  hwParticles.push_back(particle);
186  });
187  return std::make_pair(hwParticles, candidateMap);
188 }
189 
191  std::vector<L1SCJetEmu::Jet> hwJets,
192  std::unordered_map<const l1t::PFCandidate*, edm::Ptr<l1t::PFCandidate>> constituentMap) const {
193  std::vector<l1t::PFJet> edmJets;
194  std::for_each(hwJets.begin(), hwJets.end(), [&](L1SCJetEmu::Jet jet) {
195  if (doCorrections) {
196  float correctedPt = corrector.correctedPt(jet.floatPt(), jet.floatEta());
197  jet.hwPt = correctedPt;
198  }
199  l1gt::Jet gtJet = jet.toGT();
200  l1t::PFJet edmJet(l1gt::Scales::floatPt(gtJet.v3.pt),
203  /*mass=*/0.,
204  gtJet.v3.pt.V,
205  gtJet.v3.eta.V,
206  gtJet.v3.phi.V);
208  edmJet.setEncodedJet(l1t::PFJet::HWEncoding::GT, jet.toGT().pack());
209  // get back the references to the constituents
210  std::vector<edm::Ptr<l1t::PFCandidate>> constituents;
211  std::for_each(jet.constituents.begin(), jet.constituents.end(), [&](auto constituent) {
212  edmJet.addConstituent(constituentMap[constituent.srcCand]);
213  });
214  edmJets.push_back(edmJet);
215  });
216  return edmJets;
217 }
218 
221  desc.add<edm::InputTag>("L1PFObjects", edm::InputTag("l1tLayer1", "Puppi"));
222  desc.add<uint32_t>("nJets", 16);
223  desc.add<double>("coneSize", 0.4);
224  desc.add<bool>("HW", false);
225  desc.add<bool>("debug", false);
226  desc.add<bool>("doCorrections", false);
227  desc.add<std::string>("correctorFile", "");
228  desc.add<std::string>("correctorDir", "");
229  descriptions.addWithDefaultLabel(desc);
230 }
231 
constexpr double deltaPhi(double phi1, double phi2)
Definition: deltaPhi.h:26
void addWithDefaultLabel(ParameterSetDescription const &psetDescription)
float floatPhi(phi_t phi)
Definition: gt_datatypes.h:46
float floatEta(eta_t eta)
Definition: gt_datatypes.h:45
std::vector< l1t::PFJet > processEvent_SW(std::vector< edm::Ptr< l1t::PFCandidate >> &parts) const
float correctedPt(float et, float emEt, float eta) const
Definition: corrector.cc:224
l1t::PFJet makeJet_SW(const std::vector< edm::Ptr< l1t::PFCandidate >> &parts) const
static void fillDescriptions(edm::ConfigurationDescriptions &description)
void setEncodedJet(const HWEncoding encoding, const PackedJet jet)
Definition: PFJet.h:47
delete x;
Definition: CaloConfig.h:22
T get() const
get a component
const l1t::PFCandidate * srcCand
std::vector< l1t::PFJet > PFJetCollection
Definition: PFJet.h:61
std::pair< std::vector< L1SCJetEmu::Particle >, std::unordered_map< const l1t::PFCandidate *, edm::Ptr< l1t::PFCandidate > > > convertEDMToHW(std::vector< edm::Ptr< l1t::PFCandidate >> &edmParticles) const
int iEvent
Definition: GenABIO.cc:224
float floatPt(pt_t pt)
Definition: gt_datatypes.h:44
#define DEFINE_FWK_MODULE(type)
Definition: MakerMacros.h:16
L1SeedConePFJetProducer(const edm::ParameterSet &)
std::vector< Jet > emulateEvent(std::vector< Particle > &parts) const
void addConstituent(const edm::Ptr< l1t::PFCandidate > &cand)
adds a candidate to this cluster; note that this only records the information, it&#39;s up to you to also...
Definition: PFJet.h:32
void initFromBits(const ap_uint< BITWIDTH > &src)
Definition: puppi.h:171
#define debug
Definition: HDRShower.cc:19
std::vector< l1t::PFJet > processEvent_HW(std::vector< edm::Ptr< l1t::PFCandidate >> &parts) const
part
Definition: HCALResponse.h:20
double b
Definition: hdecay.h:120
const float coneSize
///////////////// ///
HLT enums.
std::vector< l1t::PFJet > convertHWToEDM(std::vector< L1SCJetEmu::Jet > hwJets, std::unordered_map< const l1t::PFCandidate *, edm::Ptr< l1t::PFCandidate >> constituentMap) const
double a
Definition: hdecay.h:121
edm::EDGetTokenT< std::vector< l1t::PFCandidate > > l1PFToken
void produce(edm::StreamID, edm::Event &iEvent, const edm::EventSetup &iSetup) const override
def move(src, dest)
Definition: eostools.py:511
unsigned transform(const HcalDetId &id, unsigned transformCode)