CMS 3D CMS Logo

GenPartIsoProducer.cc
Go to the documentation of this file.
1 // system include files
2 #include <memory>
3 
4 // user include files
7 
10 
13 
16 
21 
23 
24 #include <tuple>
25 #include <string>
26 #include <vector>
27 #include <TLorentzVector.h>
28 
29 using namespace std;
30 
32 public:
33  explicit GenPartIsoProducer(const edm::ParameterSet& iConfig)
34  : finalGenParticleToken(consumes<reco::GenParticleCollection>(iConfig.getParameter<edm::InputTag>("genPart"))),
35  packedGenParticlesToken(
36  consumes<pat::PackedGenParticleCollection>(iConfig.getParameter<edm::InputTag>("packedGenPart"))),
37  additionalPdgId_(iConfig.getParameter<int>("additionalPdgId")) {
38  produces<edm::ValueMap<float>>();
39  }
40  ~GenPartIsoProducer() override;
41 
42  static void fillDescriptions(edm::ConfigurationDescriptions& descriptions);
43 
44 private:
45  void produce(edm::Event&, const edm::EventSetup&) override;
46  float computeIso(TLorentzVector thisPart,
48  std::set<int> gen_fsrset,
49  bool skip_leptons);
50  std::vector<float> Lepts_RelIso;
54 };
55 
57 
59  using namespace edm;
60 
61  auto finalParticles = iEvent.getHandle(finalGenParticleToken);
62  auto packedGenParticles = iEvent.getHandle(packedGenParticlesToken);
63 
64  reco::GenParticleCollection::const_iterator genPart;
65 
66  Lepts_RelIso.clear();
67 
68  for (genPart = finalParticles->begin(); genPart != finalParticles->end(); genPart++) {
69  if (abs(genPart->pdgId()) == 11 || abs(genPart->pdgId()) == 13 || abs(genPart->pdgId()) == 15) {
70  TLorentzVector lep_dressed;
71  lep_dressed.SetPtEtaPhiE(genPart->pt(), genPart->eta(), genPart->phi(), genPart->energy());
72  std::set<int> gen_fsrset;
73  for (size_t k = 0; k < packedGenParticles->size(); k++) {
74  if ((*packedGenParticles)[k].status() != 1)
75  continue;
76  if ((*packedGenParticles)[k].pdgId() != 22)
77  continue;
78  double this_dR_lgamma = reco::deltaR(
79  genPart->eta(), genPart->phi(), (*packedGenParticles)[k].eta(), (*packedGenParticles)[k].phi());
80  bool idmatch = false;
81  if ((*packedGenParticles)[k].mother(0)->pdgId() == genPart->pdgId())
82  idmatch = true;
83  const reco::Candidate* mother = (*packedGenParticles)[k].mother(0);
84  for (size_t m = 0; m < mother->numberOfMothers(); m++) {
85  if ((*packedGenParticles)[k].mother(m)->pdgId() == genPart->pdgId())
86  idmatch = true;
87  }
88  if (!idmatch)
89  continue;
90  if (this_dR_lgamma < 0.3) {
91  gen_fsrset.insert(k);
92  TLorentzVector gamma;
93  gamma.SetPtEtaPhiE((*packedGenParticles)[k].pt(),
94  (*packedGenParticles)[k].eta(),
95  (*packedGenParticles)[k].phi(),
97  lep_dressed = lep_dressed + gamma;
98  }
99  }
100  float this_GENiso = 0.0;
101  TLorentzVector thisLep;
102  thisLep.SetPtEtaPhiM(lep_dressed.Pt(), lep_dressed.Eta(), lep_dressed.Phi(), lep_dressed.M());
103  this_GENiso = computeIso(thisLep, packedGenParticles, gen_fsrset, true);
104  Lepts_RelIso.push_back(this_GENiso);
105  } else if (abs(genPart->pdgId()) == additionalPdgId_) {
106  float this_GENiso = 0.0;
107  std::set<int> gen_fsrset_nolep;
108  TLorentzVector thisPart;
109  thisPart.SetPtEtaPhiE(genPart->pt(), genPart->eta(), genPart->phi(), genPart->energy());
110  this_GENiso = computeIso(thisPart, packedGenParticles, gen_fsrset_nolep, false);
111  Lepts_RelIso.push_back(this_GENiso);
112  } else {
113  float this_GENiso = 0.0;
114  Lepts_RelIso.push_back(this_GENiso);
115  }
116  }
117 
118  auto isoV = std::make_unique<edm::ValueMap<float>>();
119  edm::ValueMap<float>::Filler fillerIsoMap(*isoV);
120  fillerIsoMap.insert(finalParticles, Lepts_RelIso.begin(), Lepts_RelIso.end());
121  fillerIsoMap.fill();
122  iEvent.put(std::move(isoV));
123 }
124 
125 float GenPartIsoProducer::computeIso(TLorentzVector thisPart,
127  std::set<int> gen_fsrset,
128  bool skip_leptons) {
129  double this_GENiso = 0.0;
130  for (size_t k = 0; k < packedGenParticles->size(); k++) {
131  if ((*packedGenParticles)[k].status() != 1)
132  continue;
133  if (abs((*packedGenParticles)[k].pdgId()) == 12 || abs((*packedGenParticles)[k].pdgId()) == 14 ||
134  abs((*packedGenParticles)[k].pdgId()) == 16)
135  continue;
136  if (reco::deltaR(thisPart.Eta(), thisPart.Phi(), (*packedGenParticles)[k].eta(), (*packedGenParticles)[k].phi()) <
137  0.001)
138  continue;
139  if (skip_leptons == true) {
140  if ((abs((*packedGenParticles)[k].pdgId()) == 11 || abs((*packedGenParticles)[k].pdgId()) == 13))
141  continue;
142  if (gen_fsrset.find(k) != gen_fsrset.end())
143  continue;
144  }
145  double this_dRvL_nolep =
146  reco::deltaR(thisPart.Eta(), thisPart.Phi(), (*packedGenParticles)[k].eta(), (*packedGenParticles)[k].phi());
147  if (this_dRvL_nolep < 0.3) {
148  this_GENiso = this_GENiso + (*packedGenParticles)[k].pt();
149  }
150  }
151  this_GENiso = this_GENiso / thisPart.Pt();
152  return this_GENiso;
153 }
154 
156  // Description of external inputs requered by this module
157  // genPart: collection of gen particles for which to compute Iso
158  // packedGenPart: collection of particles to be used for leptons dressing
159  // additionalPdgId: additional particle (besides leptons) for which Iso is computed
161  desc.add<edm::InputTag>("genPart")->setComment("input physics object collection");
162  desc.add<edm::InputTag>("packedGenPart")->setComment("input stable hadrons collection");
163  desc.add<int>("additionalPdgId")->setComment("additional pdgId for Iso computation");
164  descriptions.addDefault(desc);
165 }
166 
167 //define this as a plug-in
std::vector< GenParticle > GenParticleCollection
collection of GenParticles
float computeIso(TLorentzVector thisPart, edm::Handle< pat::PackedGenParticleCollection > packedGenParticles, std::set< int > gen_fsrset, bool skip_leptons)
virtual const Candidate * mother(size_type i=0) const =0
return pointer to mother
virtual size_type numberOfMothers() const =0
number of mothers (zero or one in most of but not all the cases)
Definition: HeavyIon.h:7
int iEvent
Definition: GenABIO.cc:224
void addDefault(ParameterSetDescription const &psetDescription)
void produce(edm::Event &, const edm::EventSetup &) override
Abs< T >::type abs(const T &t)
Definition: Abs.h:22
#define DEFINE_FWK_MODULE(type)
Definition: MakerMacros.h:16
GenPartIsoProducer(const edm::ParameterSet &iConfig)
~GenPartIsoProducer() override
constexpr auto deltaR(const T1 &t1, const T2 &t2) -> decltype(t1.eta())
Definition: deltaR.h:30
static void fillDescriptions(edm::ConfigurationDescriptions &descriptions)
static void fillDescriptions(edm::ConfigurationDescriptions &descriptions)
edm::EDGetTokenT< reco::GenParticleCollection > finalGenParticleToken
std::vector< pat::PackedGenParticle > PackedGenParticleCollection
edm::EDGetTokenT< pat::PackedGenParticleCollection > packedGenParticlesToken
fixed size matrix
HLT enums.
std::vector< float > Lepts_RelIso
def move(src, dest)
Definition: eostools.py:511