CMS 3D CMS Logo

JetPFConstituentVarProducer.cc
Go to the documentation of this file.
1 #include <string>
2 #include <memory>
3 
8 
11 
18 
19 template <typename T>
21 public:
24  static void fillDescriptions(edm::ConfigurationDescriptions& descriptions);
25 
26 private:
27  void produce(edm::Event&, const edm::EventSetup&) override;
28  void PutValueMapInEvent(edm::Event&, const edm::Handle<edm::View<T>>&, const std::vector<float>&, std::string);
29 
32 
35 };
36 
37 //
38 // constructors and destructor
39 //
40 template <typename T>
42  : jet_token_(consumes<edm::View<T>>(iConfig.getParameter<edm::InputTag>("jets"))),
43  fallback_puppi_weight_(iConfig.getParameter<bool>("fallback_puppi_weight")),
44  use_puppi_value_map_(false) {
45  //
46  // Puppi Value Map
47  //
48  const auto& puppi_value_map_tag = iConfig.getParameter<edm::InputTag>("puppi_value_map");
49  if (!puppi_value_map_tag.label().empty()) {
50  puppi_value_map_token_ = consumes<edm::ValueMap<float>>(puppi_value_map_tag);
51  use_puppi_value_map_ = true;
52  }
53  produces<edm::ValueMap<float>>("leadConstNeHadEF");
54  produces<edm::ValueMap<float>>("leadConstChHadEF");
55  produces<edm::ValueMap<float>>("leadConstPhotonEF");
56  produces<edm::ValueMap<float>>("leadConstElectronEF");
57  produces<edm::ValueMap<float>>("leadConstMuonEF");
58  produces<edm::ValueMap<float>>("leadConstHFHADEF");
59  produces<edm::ValueMap<float>>("leadConstHFEMEF");
60  produces<edm::ValueMap<float>>("leadConstNeHadPuppiWeight");
61  produces<edm::ValueMap<float>>("leadConstChHadPuppiWeight");
62  produces<edm::ValueMap<float>>("leadConstPhotonPuppiWeight");
63  produces<edm::ValueMap<float>>("leadConstElectronPuppiWeight");
64  produces<edm::ValueMap<float>>("leadConstMuonPuppiWeight");
65  produces<edm::ValueMap<float>>("leadConstHFHADPuppiWeight");
66  produces<edm::ValueMap<float>>("leadConstHFEMPuppiWeight");
67 }
68 
69 template <typename T>
71 
72 template <typename T>
74  // Input jets
75  auto jets = iEvent.getHandle(jet_token_);
76 
77  // Get puppi value map
78  edm::Handle<edm::ValueMap<float>> puppi_value_map_;
79  if (use_puppi_value_map_) {
80  iEvent.getByToken(puppi_value_map_token_, puppi_value_map_);
81  }
82 
83  std::vector<float> jet_leadConstNeHadEF(jets->size(), 0.f);
84  std::vector<float> jet_leadConstNeHadPuppiWeight(jets->size(), 0.f);
85 
86  std::vector<float> jet_leadConstChHadEF(jets->size(), 0.f);
87  std::vector<float> jet_leadConstChHadPuppiWeight(jets->size(), 0.f);
88 
89  std::vector<float> jet_leadConstPhotonEF(jets->size(), 0.f);
90  std::vector<float> jet_leadConstPhotonPuppiWeight(jets->size(), 0.f);
91 
92  std::vector<float> jet_leadConstElectronEF(jets->size(), 0.f);
93  std::vector<float> jet_leadConstElectronPuppiWeight(jets->size(), 0.f);
94 
95  std::vector<float> jet_leadConstMuonEF(jets->size(), 0.f);
96  std::vector<float> jet_leadConstMuonPuppiWeight(jets->size(), 0.f);
97 
98  std::vector<float> jet_leadConstHFHADEF(jets->size(), 0.f);
99  std::vector<float> jet_leadConstHFHADPuppiWeight(jets->size(), 0.f);
100 
101  std::vector<float> jet_leadConstHFEMEF(jets->size(), 0.f);
102  std::vector<float> jet_leadConstHFEMPuppiWeight(jets->size(), 0.f);
103 
104  // Loop over jet
105  for (std::size_t jet_idx = 0; jet_idx < jets->size(); jet_idx++) {
106  const auto& jet = (*jets)[jet_idx];
107 
108  float jet_energy_raw = jet.energy();
110  jet_energy_raw = jet.correctedJet(0).energy();
111  }
112 
113  reco::CandidatePtr leadConstNeHad;
114  reco::CandidatePtr leadConstChHad;
115  reco::CandidatePtr leadConstPhoton;
116  reco::CandidatePtr leadConstElectron;
117  reco::CandidatePtr leadConstMuon;
118  reco::CandidatePtr leadConstHFHAD;
119  reco::CandidatePtr leadConstHFEM;
120 
121  float leadConstNeHadPuppiWeight = 1.f;
122  float leadConstChHadPuppiWeight = 1.f;
123  float leadConstPhotonPuppiWeight = 1.f;
124  float leadConstElectronPuppiWeight = 1.f;
125  float leadConstMuonPuppiWeight = 1.f;
126  float leadConstHFHADPuppiWeight = 1.f;
127  float leadConstHFEMPuppiWeight = 1.f;
128 
129  //
130  // Loop over jet constituents
131  //
132  for (const reco::CandidatePtr& dau : jet.daughterPtrVector()) {
133  float puppiw = 1.f;
134 
135  //
136  // Get Puppi weight from ValueMap, if provided.
137  //
138  if (use_puppi_value_map_) {
139  puppiw = (*puppi_value_map_)[dau];
140  } else if (!fallback_puppi_weight_) {
141  throw edm::Exception(edm::errors::InvalidReference, "PUPPI value map missing")
142  << "use fallback_puppi_weight option to use " << puppiw << " for cand as default";
143  }
144 
145  //
146  // Find the highest energy constituents for each PF type
147  //
148  if (abs(dau->pdgId()) == 130) {
149  if (leadConstNeHad.isNull() ||
150  (puppiw * dau->energy() > leadConstNeHadPuppiWeight * leadConstNeHad->energy())) {
151  leadConstNeHad = dau;
152  leadConstNeHadPuppiWeight = puppiw;
153  }
154  } else if (abs(dau->pdgId()) == 211) {
155  if (leadConstChHad.isNull() ||
156  (puppiw * dau->energy() > leadConstChHadPuppiWeight * leadConstChHad->energy())) {
157  leadConstChHad = dau;
158  leadConstChHadPuppiWeight = puppiw;
159  }
160  } else if (abs(dau->pdgId()) == 22) {
161  if (leadConstPhoton.isNull() ||
162  (puppiw * dau->energy() > leadConstPhotonPuppiWeight * leadConstPhoton->energy())) {
163  leadConstPhoton = dau;
164  leadConstPhotonPuppiWeight = puppiw;
165  }
166  } else if (abs(dau->pdgId()) == 11) {
167  if (leadConstElectron.isNull() ||
168  (puppiw * dau->energy() > leadConstElectronPuppiWeight * leadConstElectron->energy())) {
169  leadConstElectron = dau;
170  leadConstElectronPuppiWeight = puppiw;
171  }
172  } else if (abs(dau->pdgId()) == 13) {
173  if (leadConstMuon.isNull() || (puppiw * dau->energy() > leadConstMuonPuppiWeight * leadConstMuon->energy())) {
174  leadConstMuon = dau;
175  leadConstMuonPuppiWeight = puppiw;
176  }
177  } else if (abs(dau->pdgId()) == 1) {
178  if (leadConstHFHAD.isNull() ||
179  (puppiw * dau->energy() > leadConstHFHADPuppiWeight * leadConstHFHAD->energy())) {
180  leadConstHFHAD = dau;
181  leadConstHFHADPuppiWeight = puppiw;
182  }
183  } else if (abs(dau->pdgId()) == 2) {
184  if (leadConstHFEM.isNull() || (puppiw * dau->energy() > leadConstHFEMPuppiWeight * leadConstHFEM->energy())) {
185  leadConstHFEM = dau;
186  leadConstHFEMPuppiWeight = puppiw;
187  }
188  }
189  } // End of Jet Constituents Loop
190 
191  if (leadConstNeHad.isNonnull()) {
192  jet_leadConstNeHadEF[jet_idx] = (leadConstNeHad->energy() * leadConstNeHadPuppiWeight) / jet_energy_raw;
193  jet_leadConstNeHadPuppiWeight[jet_idx] = leadConstNeHadPuppiWeight;
194  }
195  if (leadConstChHad.isNonnull()) {
196  jet_leadConstChHadEF[jet_idx] = (leadConstChHad->energy() * leadConstChHadPuppiWeight) / jet_energy_raw;
197  jet_leadConstChHadPuppiWeight[jet_idx] = leadConstChHadPuppiWeight;
198  }
199  if (leadConstPhoton.isNonnull()) {
200  jet_leadConstPhotonEF[jet_idx] = (leadConstPhoton->energy() * leadConstPhotonPuppiWeight) / jet_energy_raw;
201  jet_leadConstPhotonPuppiWeight[jet_idx] = leadConstPhotonPuppiWeight;
202  }
203  if (leadConstElectron.isNonnull()) {
204  jet_leadConstElectronEF[jet_idx] = (leadConstElectron->energy() * leadConstElectronPuppiWeight) / jet_energy_raw;
205  jet_leadConstElectronPuppiWeight[jet_idx] = leadConstElectronPuppiWeight;
206  }
207  if (leadConstMuon.isNonnull()) {
208  jet_leadConstMuonEF[jet_idx] = (leadConstMuon->energy() * leadConstMuonPuppiWeight) / jet_energy_raw;
209  jet_leadConstMuonPuppiWeight[jet_idx] = leadConstMuonPuppiWeight;
210  }
211  if (leadConstHFHAD.isNonnull()) {
212  jet_leadConstHFHADEF[jet_idx] = (leadConstHFHAD->energy() * leadConstHFHADPuppiWeight) / jet_energy_raw;
213  jet_leadConstHFHADPuppiWeight[jet_idx] = leadConstHFHADPuppiWeight;
214  }
215  if (leadConstHFEM.isNonnull()) {
216  jet_leadConstHFEMEF[jet_idx] = (leadConstHFEM->energy() * leadConstHFEMPuppiWeight) / jet_energy_raw;
217  jet_leadConstHFEMPuppiWeight[jet_idx] = leadConstHFEMPuppiWeight;
218  }
219  }
220 
221  PutValueMapInEvent(iEvent, jets, jet_leadConstNeHadEF, "leadConstNeHadEF");
222  PutValueMapInEvent(iEvent, jets, jet_leadConstNeHadPuppiWeight, "leadConstNeHadPuppiWeight");
223 
224  PutValueMapInEvent(iEvent, jets, jet_leadConstChHadEF, "leadConstChHadEF");
225  PutValueMapInEvent(iEvent, jets, jet_leadConstChHadPuppiWeight, "leadConstChHadPuppiWeight");
226 
227  PutValueMapInEvent(iEvent, jets, jet_leadConstPhotonEF, "leadConstPhotonEF");
228  PutValueMapInEvent(iEvent, jets, jet_leadConstPhotonPuppiWeight, "leadConstPhotonPuppiWeight");
229 
230  PutValueMapInEvent(iEvent, jets, jet_leadConstElectronEF, "leadConstElectronEF");
231  PutValueMapInEvent(iEvent, jets, jet_leadConstElectronPuppiWeight, "leadConstElectronPuppiWeight");
232 
233  PutValueMapInEvent(iEvent, jets, jet_leadConstMuonEF, "leadConstMuonEF");
234  PutValueMapInEvent(iEvent, jets, jet_leadConstMuonPuppiWeight, "leadConstMuonPuppiWeight");
235 
236  PutValueMapInEvent(iEvent, jets, jet_leadConstHFHADEF, "leadConstHFHADEF");
237  PutValueMapInEvent(iEvent, jets, jet_leadConstHFHADPuppiWeight, "leadConstHFHADPuppiWeight");
238 
239  PutValueMapInEvent(iEvent, jets, jet_leadConstHFEMEF, "leadConstHFEMEF");
240  PutValueMapInEvent(iEvent, jets, jet_leadConstHFEMPuppiWeight, "leadConstHFEMPuppiWeight");
241 }
242 template <typename T>
244  const edm::Handle<edm::View<T>>& coll,
245  const std::vector<float>& vec_var,
246  std::string VMName) {
247  std::unique_ptr<edm::ValueMap<float>> VM(new edm::ValueMap<float>());
248  edm::ValueMap<float>::Filler fillerVM(*VM);
249  fillerVM.insert(coll, vec_var.begin(), vec_var.end());
250  fillerVM.fill();
251  iEvent.put(std::move(VM), VMName);
252 }
253 
254 template <typename T>
257  desc.add<edm::InputTag>("jets", edm::InputTag("finalJetsPuppi"));
258  desc.add<edm::InputTag>("puppi_value_map", edm::InputTag("packedpuppi"));
259  desc.add<bool>("fallback_puppi_weight", false);
260  descriptions.addWithDefaultLabel(desc);
261 }
262 
void addWithDefaultLabel(ParameterSetDescription const &psetDescription)
T getParameter(std::string const &) const
Definition: ParameterSet.h:307
edm::EDGetTokenT< edm::View< T > > jet_token_
void PutValueMapInEvent(edm::Event &, const edm::Handle< edm::View< T >> &, const std::vector< float > &, std::string)
void insert(const H &h, I begin, I end)
Definition: ValueMap.h:53
static void fillDescriptions(edm::ConfigurationDescriptions &descriptions)
JetPFConstituentVarProducer< pat::Jet > PatJetPFConstituentVarProducer
int iEvent
Definition: GenABIO.cc:224
bool isNull() const
Checks for null.
Definition: Ptr.h:144
JetPFConstituentVarProducer(const edm::ParameterSet &)
bool isNonnull() const
Checks for non-null.
Definition: Ptr.h:148
Abs< T >::type abs(const T &t)
Definition: Abs.h:22
#define DEFINE_FWK_MODULE(type)
Definition: MakerMacros.h:16
edm::EDGetTokenT< edm::ValueMap< float > > puppi_value_map_token_
HLT enums.
void produce(edm::Event &, const edm::EventSetup &) override
long double T
def move(src, dest)
Definition: eostools.py:511