CMS 3D CMS Logo

CandMCMatchTableProducer.cc
Go to the documentation of this file.
12 
13 #include <vector>
14 #include <iostream>
15 
17 public:
19  : objName_(params.getParameter<std::string>("objName")),
20  branchName_(params.getParameter<std::string>("branchName")),
21  doc_(params.getParameter<std::string>("docString")),
22  src_(consumes<reco::CandidateView>(params.getParameter<edm::InputTag>("src"))),
23  candMap_(consumes<edm::Association<reco::GenParticleCollection>>(params.getParameter<edm::InputTag>("mcMap"))) {
24  produces<nanoaod::FlatTable>();
25  const std::string& type = params.getParameter<std::string>("objType");
26  if (type == "Muon")
27  type_ = MMuon;
28  else if (type == "Electron")
29  type_ = MElectron;
30  else if (type == "Tau")
31  type_ = MTau;
32  else if (type == "Photon")
33  type_ = MPhoton;
34  else if (type == "Other")
35  type_ = MOther;
36  else
37  throw cms::Exception("Configuration", "Unsupported objType '" + type + "'\n");
38 
39  switch (type_) {
40  case MMuon:
41  flavDoc_ =
42  "1 = prompt muon (including gamma*->mu mu), 15 = muon from prompt tau, " // continues below
43  "5 = muon from b, 4 = muon from c, 3 = muon from light or unknown, 0 = unmatched";
44  break;
45  case MElectron:
46  flavDoc_ =
47  "1 = prompt electron (including gamma*->mu mu), 15 = electron from prompt tau, 22 = prompt photon (likely "
48  "conversion), " // continues below
49  "5 = electron from b, 4 = electron from c, 3 = electron from light or unknown, 0 = unmatched";
50  break;
51  case MPhoton:
52  flavDoc_ = "1 = prompt photon, 11 = prompt electron, 0 = unknown or unmatched";
53  break;
54  case MTau:
55  flavDoc_ =
56  "1 = prompt electron, 2 = prompt muon, 3 = tau->e decay, 4 = tau->mu decay, 5 = hadronic tau decay, 0 = "
57  "unknown or unmatched";
58  break;
59  case MOther:
60  flavDoc_ = "1 = from hard scatter, 0 = unknown or unmatched";
61  break;
62  }
63 
64  if (type_ == MTau) {
66  consumes<edm::Association<reco::GenParticleCollection>>(params.getParameter<edm::InputTag>("mcMapVisTau"));
67  }
68 
69  if (type_ == MElectron) {
71  consumes<edm::Association<reco::GenJetCollection>>(params.getParameter<edm::InputTag>("mcMapDressedLep"));
72  mapTauAnc_ = consumes<edm::ValueMap<bool>>(params.getParameter<edm::InputTag>("mapTauAnc"));
73  genPartsToken_ = consumes<reco::GenParticleCollection>(params.getParameter<edm::InputTag>("genparticles"));
74  }
75  }
76 
78 
79  void produce(edm::StreamID id, edm::Event& iEvent, const edm::EventSetup& iSetup) const override {
81  iEvent.getByToken(src_, cands);
82  unsigned int ncand = cands->size();
83 
84  auto tab = std::make_unique<nanoaod::FlatTable>(ncand, objName_, false, true);
85 
87  iEvent.getByToken(candMap_, map);
88 
90  if (type_ == MTau) {
91  iEvent.getByToken(candMapVisTau_, mapVisTau);
92  }
93 
97  if (type_ == MElectron) {
98  iEvent.getByToken(candMapDressedLep_, mapDressedLep);
99  iEvent.getByToken(mapTauAnc_, mapTauAnc);
100  iEvent.getByToken(genPartsToken_, genParts);
101  }
102 
103  std::vector<int> key(ncand, -1), flav(ncand, 0);
104  for (unsigned int i = 0; i < ncand; ++i) {
105  //std::cout << "cand #" << i << ": pT = " << cands->ptrAt(i)->pt() << ", eta = " << cands->ptrAt(i)->eta() << ", phi = " << cands->ptrAt(i)->phi() << std::endl;
106  reco::GenParticleRef match = (*map)[cands->ptrAt(i)];
107  reco::GenParticleRef matchVisTau;
108  reco::GenJetRef matchDressedLep;
109  bool hasTauAnc = false;
110  if (type_ == MTau) {
111  matchVisTau = (*mapVisTau)[cands->ptrAt(i)];
112  }
113  if (type_ == MElectron) {
114  matchDressedLep = (*mapDressedLep)[cands->ptrAt(i)];
115  if (matchDressedLep.isNonnull()) {
116  hasTauAnc = (*mapTauAnc)[matchDressedLep];
117  }
118  }
119  if (match.isNonnull())
120  key[i] = match.key();
121  else if (matchVisTau.isNonnull())
122  key[i] = matchVisTau.key();
123  else if (type_ != MElectron)
124  continue; // go ahead with electrons, as those may be matched to a dressed lepton
125 
126  switch (type_) {
127  case MMuon:
128  if (match->isPromptFinalState())
129  flav[i] = 1; // prompt
130  else if (match->isDirectPromptTauDecayProductFinalState())
131  flav[i] = 15; // tau
132  else
133  flav[i] = getParentHadronFlag(match); // 3 = light, 4 = charm, 5 = b
134  break;
135  case MElectron:
136  if (matchDressedLep.isNonnull()) {
137  if (matchDressedLep->pdgId() == 22)
138  flav[i] = 22;
139  else
140  flav[i] = (hasTauAnc) ? 15 : 1;
141 
142  float minpt = 0;
143  const reco::GenParticle* highestPtConstituent = nullptr;
144  for (auto& consti : matchDressedLep->getGenConstituents()) {
145  if (abs(consti->pdgId()) != 11)
146  continue;
147  if (consti->pt() < minpt)
148  continue;
149  minpt = consti->pt();
150  highestPtConstituent = consti;
151  }
152  if (highestPtConstituent) {
153  auto iter =
154  std::find_if(genParts->begin(), genParts->end(), [highestPtConstituent](reco::GenParticle genp) {
155  return (abs(genp.pdgId()) == 11) && (deltaR(genp, *highestPtConstituent) < 0.01) &&
156  (abs(genp.pt() - highestPtConstituent->pt()) / highestPtConstituent->pt() < 0.01);
157  });
158  if (iter != genParts->end()) {
159  key[i] = iter - genParts->begin();
160  }
161  }
162  } else if (!match.isNonnull())
163  flav[i] = 0;
164  else if (match->isPromptFinalState())
165  flav[i] = (match->pdgId() == 22 ? 22 : 1); // prompt electron or photon
166  else if (match->isDirectPromptTauDecayProductFinalState())
167  flav[i] = 15; // tau
168  else
169  flav[i] = getParentHadronFlag(match); // 3 = light, 4 = charm, 5 = b
170  break;
171  case MPhoton:
172  if (match->isPromptFinalState() && match->pdgId() == 22)
173  flav[i] = 1; // prompt photon
174  else if ((match->isPromptFinalState() || match->isDirectPromptTauDecayProductFinalState()) &&
175  abs(match->pdgId()) == 11)
176  flav[i] = 11; // prompt electron
177  break;
178  case MTau:
179  // CV: assignment of status codes according to https://twiki.cern.ch/twiki/bin/viewauth/CMS/HiggsToTauTauWorking2016#MC_Matching
180  if (match.isNonnull() && match->statusFlags().isPrompt() && abs(match->pdgId()) == 11)
181  flav[i] = 1;
182  else if (match.isNonnull() && match->statusFlags().isPrompt() && abs(match->pdgId()) == 13)
183  flav[i] = 2;
184  else if (match.isNonnull() && match->isDirectPromptTauDecayProductFinalState() && abs(match->pdgId()) == 11)
185  flav[i] = 3;
186  else if (match.isNonnull() && match->isDirectPromptTauDecayProductFinalState() && abs(match->pdgId()) == 13)
187  flav[i] = 4;
188  else if (matchVisTau.isNonnull())
189  flav[i] = 5;
190  break;
191  default:
192  flav[i] = match->statusFlags().fromHardProcess();
193  };
194  }
195 
196  tab->addColumn<int>(branchName_ + "Idx", key, "Index into genParticle list for " + doc_);
197  tab->addColumn<uint8_t>(branchName_ + "Flav",
198  flav,
199  "Flavour of genParticle (DressedLeptons for electrons) for " + doc_ + ": " + flavDoc_);
200 
201  iEvent.put(std::move(tab));
202  }
203 
205  bool has4 = false;
206  for (unsigned int im = 0, nm = match->numberOfMothers(); im < nm; ++im) {
207  reco::GenParticleRef mom = match->motherRef(im);
208  assert(mom.isNonnull() && mom.isAvailable()); // sanity
209  if (mom.key() >= match.key())
210  continue; // prevent circular refs
211  int id = std::abs(mom->pdgId());
212  if (id / 1000 == 5 || id / 100 == 5 || id == 5)
213  return 5;
214  if (id / 1000 == 4 || id / 100 == 4 || id == 4)
215  has4 = true;
216  if (mom->status() == 2) {
217  id = getParentHadronFlag(mom);
218  if (id == 5)
219  return 5;
220  else if (id == 4)
221  has4 = true;
222  }
223  }
224  return has4 ? 4 : 3;
225  }
226 
229  desc.add<std::string>("objName")->setComment("name of the nanoaod::FlatTable to extend with this table");
230  desc.add<std::string>("branchName")
231  ->setComment(
232  "name of the column to write (the final branch in the nanoaod will be <objName>_<branchName>Idx and "
233  "<objName>_<branchName>Flav");
234  desc.add<std::string>("docString")->setComment("documentation to forward to the output");
235  desc.add<edm::InputTag>("src")->setComment(
236  "physics object collection for the reconstructed objects (e.g. leptons)");
237  desc.add<edm::InputTag>("mcMap")->setComment(
238  "tag to an edm::Association<GenParticleCollection> mapping src to gen, such as the one produced by MCMatcher");
239  desc.add<std::string>("objType")->setComment(
240  "type of object to match (Muon, Electron, Tau, Photon, Other), taylors what's in t Flav branch");
241  desc.addOptional<edm::InputTag>("mcMapVisTau")
242  ->setComment("as mcMap, but pointing to the visible gen taus (only if objType == Tau)");
243  desc.addOptional<edm::InputTag>("mcMapDressedLep")
244  ->setComment("as mcMap, but pointing to gen dressed leptons (only if objType == Electrons)");
245  desc.addOptional<edm::InputTag>("mapTauAnc")
246  ->setComment("Value map of matched gen electrons containing info on the tau ancestry");
247  desc.addOptional<edm::InputTag>("genparticles")->setComment("Collection of genParticles to be stored.");
248  descriptions.add("candMcMatchTable", desc);
249  }
250 
251 protected:
261 };
262 
ConfigurationDescriptions.h
edm::StreamID
Definition: StreamID.h:30
GenJetCollection.h
reco::CandidateView
edm::View< Candidate > CandidateView
view of a collection containing candidates
Definition: CandidateFwd.h:23
mps_fire.i
i
Definition: mps_fire.py:428
CandMCMatchTableProducer::MPhoton
Definition: CandMCMatchTableProducer.cc:259
CandMCMatchTableProducer::candMap_
const edm::EDGetTokenT< edm::Association< reco::GenParticleCollection > > candMap_
Definition: CandMCMatchTableProducer.cc:254
CandMCMatchTableProducer
Definition: CandMCMatchTableProducer.cc:16
reco::GenParticle
Definition: GenParticle.h:21
EgammaValidation_cff.genp
genp
produce generated paricles in acceptance #
Definition: EgammaValidation_cff.py:114
CalibrationSummaryClient_cfi.params
params
Definition: CalibrationSummaryClient_cfi.py:14
edm::Ref::isAvailable
bool isAvailable() const
Definition: Ref.h:537
CandMCMatchTableProducer::src_
const edm::EDGetTokenT< reco::CandidateView > src_
Definition: CandMCMatchTableProducer.cc:253
edm::EDGetTokenT
Definition: EDGetToken.h:33
edm
HLT enums.
Definition: AlignableModifier.h:19
HLT_FULL_cff.InputTag
InputTag
Definition: HLT_FULL_cff.py:89301
reco::GenParticleCollection
std::vector< GenParticle > GenParticleCollection
collection of GenParticles
Definition: GenParticleFwd.h:13
edm::ParameterSetDescription
Definition: ParameterSetDescription.h:52
cms::cuda::assert
assert(be >=bs)
CandMCMatchTableProducer::mapTauAnc_
edm::EDGetTokenT< edm::ValueMap< bool > > mapTauAnc_
Definition: CandMCMatchTableProducer.cc:257
reco::LeafCandidate::pt
double pt() const final
transverse momentum
Definition: LeafCandidate.h:146
CandMCMatchTableProducer::CandMCMatchTableProducer
CandMCMatchTableProducer(edm::ParameterSet const &params)
Definition: CandMCMatchTableProducer.cc:18
reco
fixed size matrix
Definition: AlignmentAlgorithmBase.h:46
CandMCMatchTableProducer::~CandMCMatchTableProducer
~CandMCMatchTableProducer() override
Definition: CandMCMatchTableProducer.cc:77
edm::Handle
Definition: AssociativeIterator.h:50
HiEvtPlane_cfi.minpt
minpt
Definition: HiEvtPlane_cfi.py:17
CandMCMatchTableProducer::MatchType
MatchType
Definition: CandMCMatchTableProducer.cc:259
edm::Ref< GenParticleCollection >
GenParticle.h
MakerMacros.h
CandMCMatchTableProducer::flavDoc_
std::string flavDoc_
Definition: CandMCMatchTableProducer.cc:260
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
CandMCMatchTableProducer::type_
enum CandMCMatchTableProducer::MatchType type_
electrons_cff.hasTauAnc
hasTauAnc
Definition: electrons_cff.py:488
CandMCMatchTableProducer::branchName_
const std::string branchName_
Definition: CandMCMatchTableProducer.cc:252
CandMCMatchTableProducer::MElectron
Definition: CandMCMatchTableProducer.cc:259
ParameterSetDescription.h
PbPb_ZMuSkimMuonDPG_cff.deltaR
deltaR
Definition: PbPb_ZMuSkimMuonDPG_cff.py:63
edm::global::EDProducer
Definition: EDProducer.h:32
edm::ConfigurationDescriptions
Definition: ConfigurationDescriptions.h:28
HLT_FULL_cff.cands
cands
Definition: HLT_FULL_cff.py:15161
edm::ParameterSet
Definition: ParameterSet.h:47
Event.h
deltaR.h
type
type
Definition: SiPixelVCal_PayloadInspector.cc:39
edm::Ref::isNonnull
bool isNonnull() const
Checks for non-null.
Definition: Ref.h:238
CandMCMatchTableProducer::fillDescriptions
static void fillDescriptions(edm::ConfigurationDescriptions &descriptions)
Definition: CandMCMatchTableProducer.cc:227
match
std::pair< typename Association::data_type::first_type, double > match(Reference key, Association association, bool bestMatchByMaxValue)
Generic matching function.
Definition: Utils.h:10
iEvent
int iEvent
Definition: GenABIO.cc:224
CandMCMatchTableProducer::produce
void produce(edm::StreamID id, edm::Event &iEvent, const edm::EventSetup &iSetup) const override
Definition: CandMCMatchTableProducer.cc:79
edm::EventSetup
Definition: EventSetup.h:58
CandMCMatchTableProducer::objName_
const std::string objName_
Definition: CandMCMatchTableProducer.cc:252
AlCaHLTBitMon_QueryRunRegistry.string
string string
Definition: AlCaHLTBitMon_QueryRunRegistry.py:256
FlatTable.h
CandMCMatchTableProducer::candMapDressedLep_
edm::EDGetTokenT< edm::Association< reco::GenJetCollection > > candMapDressedLep_
Definition: CandMCMatchTableProducer.cc:256
submitPVResolutionJobs.desc
string desc
Definition: submitPVResolutionJobs.py:251
eostools.move
def move(src, dest)
Definition: eostools.py:511
std
Definition: JetResolutionObject.h:76
CandMCMatchTableProducer::getParentHadronFlag
static int getParentHadronFlag(const reco::GenParticleRef match)
Definition: CandMCMatchTableProducer.cc:204
CandMCMatchTableProducer::candMapVisTau_
edm::EDGetTokenT< edm::Association< reco::GenParticleCollection > > candMapVisTau_
Definition: CandMCMatchTableProducer.cc:255
Exception
Definition: hltDiff.cc:245
CandMCMatchTableProducer::MTau
Definition: CandMCMatchTableProducer.cc:259
CandMCMatchTableProducer::doc_
const std::string doc_
Definition: CandMCMatchTableProducer.cc:252
CandMCMatchTableProducer::genPartsToken_
edm::EDGetTokenT< reco::GenParticleCollection > genPartsToken_
Definition: CandMCMatchTableProducer.cc:258
CandMCMatchTableProducer::MOther
Definition: CandMCMatchTableProducer.cc:259
edm::Ref::key
key_type key() const
Accessor for product key.
Definition: Ref.h:250
Candidate.h
funct::abs
Abs< T >::type abs(const T &t)
Definition: Abs.h:22
genParticles_cff.map
map
Definition: genParticles_cff.py:11
View.h
ParameterSet.h
edm::EDConsumerBase::consumes
EDGetTokenT< ProductType > consumes(edm::InputTag const &tag)
Definition: EDConsumerBase.h:159
EDProducer.h
edm::Event
Definition: Event.h:73
crabWrapper.key
key
Definition: crabWrapper.py:19
CandMCMatchTableProducer::MMuon
Definition: CandMCMatchTableProducer.cc:259
edm::InputTag
Definition: InputTag.h:15
electrons_cff.mapTauAnc
mapTauAnc
Definition: electrons_cff.py:517