CMS 3D CMS Logo

CandMCMatchTableProducer.cc
Go to the documentation of this file.
10 
11 #include <vector>
12 #include <iostream>
13 
15 public:
17  : objName_(params.getParameter<std::string>("objName")),
18  branchName_(params.getParameter<std::string>("branchName")),
19  doc_(params.getParameter<std::string>("docString")),
20  src_(consumes<reco::CandidateView>(params.getParameter<edm::InputTag>("src"))),
21  candMap_(consumes<edm::Association<reco::GenParticleCollection>>(params.getParameter<edm::InputTag>("mcMap"))) {
22  produces<nanoaod::FlatTable>();
23  const std::string& type = params.getParameter<std::string>("objType");
24  if (type == "Muon")
25  type_ = MMuon;
26  else if (type == "Electron")
27  type_ = MElectron;
28  else if (type == "Tau")
29  type_ = MTau;
30  else if (type == "Photon")
31  type_ = MPhoton;
32  else if (type == "Other")
33  type_ = MOther;
34  else
35  throw cms::Exception("Configuration", "Unsupported objType '" + type + "'\n");
36 
37  switch (type_) {
38  case MMuon:
39  flavDoc_ =
40  "1 = prompt muon (including gamma*->mu mu), 15 = muon from prompt tau, " // continues below
41  "5 = muon from b, 4 = muon from c, 3 = muon from light or unknown, 0 = unmatched";
42  break;
43  case MElectron:
44  flavDoc_ =
45  "1 = prompt electron (including gamma*->mu mu), 15 = electron from prompt tau, 22 = prompt photon (likely "
46  "conversion), " // continues below
47  "5 = electron from b, 4 = electron from c, 3 = electron from light or unknown, 0 = unmatched";
48  break;
49  case MPhoton:
50  flavDoc_ = "1 = prompt photon, 13 = prompt electron, 0 = unknown or unmatched";
51  break;
52  case MTau:
53  flavDoc_ =
54  "1 = prompt electron, 2 = prompt muon, 3 = tau->e decay, 4 = tau->mu decay, 5 = hadronic tau decay, 0 = "
55  "unknown or unmatched";
56  break;
57  case MOther:
58  flavDoc_ = "1 = from hard scatter, 0 = unknown or unmatched";
59  break;
60  }
61 
62  if (type_ == MTau) {
64  consumes<edm::Association<reco::GenParticleCollection>>(params.getParameter<edm::InputTag>("mcMapVisTau"));
65  }
66  }
67 
69 
70  void produce(edm::StreamID id, edm::Event& iEvent, const edm::EventSetup& iSetup) const override {
72  iEvent.getByToken(src_, cands);
73  unsigned int ncand = cands->size();
74 
75  auto tab = std::make_unique<nanoaod::FlatTable>(ncand, objName_, false, true);
76 
78  iEvent.getByToken(candMap_, map);
79 
81  if (type_ == MTau) {
82  iEvent.getByToken(candMapVisTau_, mapVisTau);
83  }
84 
85  std::vector<int> key(ncand, -1), flav(ncand, 0);
86  for (unsigned int i = 0; i < ncand; ++i) {
87  //std::cout << "cand #" << i << ": pT = " << cands->ptrAt(i)->pt() << ", eta = " << cands->ptrAt(i)->eta() << ", phi = " << cands->ptrAt(i)->phi() << std::endl;
88  reco::GenParticleRef match = (*map)[cands->ptrAt(i)];
89  reco::GenParticleRef matchVisTau;
90  if (type_ == MTau) {
91  matchVisTau = (*mapVisTau)[cands->ptrAt(i)];
92  }
93  if (match.isNonnull())
94  key[i] = match.key();
95  else if (matchVisTau.isNonnull())
96  key[i] = matchVisTau.key();
97  else
98  continue;
99  switch (type_) {
100  case MMuon:
101  if (match->isPromptFinalState())
102  flav[i] = 1; // prompt
103  else if (match->isDirectPromptTauDecayProductFinalState())
104  flav[i] = 15; // tau
105  else
106  flav[i] = getParentHadronFlag(match); // 3 = light, 4 = charm, 5 = b
107  break;
108  case MElectron:
109  if (match->isPromptFinalState())
110  flav[i] = (match->pdgId() == 22 ? 22 : 1); // prompt electron or photon
111  else if (match->isDirectPromptTauDecayProductFinalState())
112  flav[i] = 15; // tau
113  else
114  flav[i] = getParentHadronFlag(match); // 3 = light, 4 = charm, 5 = b
115  break;
116  case MPhoton:
117  if (match->isPromptFinalState())
118  flav[i] = (match->pdgId() == 22 ? 1 : 13); // prompt electron or photon
119  break;
120  case MTau:
121  // CV: assignment of status codes according to https://twiki.cern.ch/twiki/bin/viewauth/CMS/HiggsToTauTauWorking2016#MC_Matching
122  if (match.isNonnull() && match->statusFlags().isPrompt() && abs(match->pdgId()) == 11)
123  flav[i] = 1;
124  else if (match.isNonnull() && match->statusFlags().isPrompt() && abs(match->pdgId()) == 13)
125  flav[i] = 2;
126  else if (match.isNonnull() && match->isDirectPromptTauDecayProductFinalState() && abs(match->pdgId()) == 11)
127  flav[i] = 3;
128  else if (match.isNonnull() && match->isDirectPromptTauDecayProductFinalState() && abs(match->pdgId()) == 13)
129  flav[i] = 4;
130  else if (matchVisTau.isNonnull())
131  flav[i] = 5;
132  break;
133  default:
134  flav[i] = match->statusFlags().fromHardProcess();
135  };
136  }
137 
138  tab->addColumn<int>(
139  branchName_ + "Idx", key, "Index into genParticle list for " + doc_, nanoaod::FlatTable::IntColumn);
140  tab->addColumn<uint8_t>(branchName_ + "Flav",
141  flav,
142  "Flavour of genParticle for " + doc_ + ": " + flavDoc_,
144 
145  iEvent.put(std::move(tab));
146  }
147 
149  bool has4 = false;
150  for (unsigned int im = 0, nm = match->numberOfMothers(); im < nm; ++im) {
151  reco::GenParticleRef mom = match->motherRef(im);
152  assert(mom.isNonnull() && mom.isAvailable()); // sanity
153  if (mom.key() >= match.key())
154  continue; // prevent circular refs
155  int id = std::abs(mom->pdgId());
156  if (id / 1000 == 5 || id / 100 == 5 || id == 5)
157  return 5;
158  if (id / 1000 == 4 || id / 100 == 4 || id == 4)
159  has4 = true;
160  if (mom->status() == 2) {
161  id = getParentHadronFlag(mom);
162  if (id == 5)
163  return 5;
164  else if (id == 4)
165  has4 = true;
166  }
167  }
168  return has4 ? 4 : 3;
169  }
170 
173  desc.add<std::string>("objName")->setComment("name of the nanoaod::FlatTable to extend with this table");
174  desc.add<std::string>("branchName")
175  ->setComment(
176  "name of the column to write (the final branch in the nanoaod will be <objName>_<branchName>Idx and "
177  "<objName>_<branchName>Flav");
178  desc.add<std::string>("docString")->setComment("documentation to forward to the output");
179  desc.add<edm::InputTag>("src")->setComment(
180  "physics object collection for the reconstructed objects (e.g. leptons)");
181  desc.add<edm::InputTag>("mcMap")->setComment(
182  "tag to an edm::Association<GenParticleCollection> mapping src to gen, such as the one produced by MCMatcher");
183  desc.add<std::string>("objType")->setComment(
184  "type of object to match (Muon, Electron, Tau, Photon, Other), taylors what's in t Flav branch");
185  desc.addOptional<edm::InputTag>("mcMapVisTau")
186  ->setComment("as mcMap, but pointing to the visible gen taus (only if objType == Tau)");
187  descriptions.add("candMcMatchTable", desc);
188  }
189 
190 protected:
197 };
198 
type
Definition: HCALResponse.h:21
std::vector< GenParticle > GenParticleCollection
collection of GenParticles
T getParameter(std::string const &) const
OrphanHandle< PROD > put(std::unique_ptr< PROD > product)
Put a new product.
Definition: Event.h:131
ParameterDescriptionBase * addOptional(U const &iLabel, T const &value)
bool isNonnull() const
Checks for non-null.
Definition: Ref.h:238
bool getByToken(EDGetToken token, Handle< PROD > &result) const
Definition: Event.h:525
Ptr< value_type > ptrAt(size_type i) const
static void fillDescriptions(edm::ConfigurationDescriptions &descriptions)
enum CandMCMatchTableProducer::MatchType type_
size_type size() const
key_type key() const
Accessor for product key.
Definition: Ref.h:250
edm::EDGetTokenT< edm::Association< reco::GenParticleCollection > > candMapVisTau_
bool isAvailable() const
Definition: Ref.h:537
EDGetTokenT< ProductType > consumes(edm::InputTag const &tag)
int iEvent
Definition: GenABIO.cc:224
#define DEFINE_FWK_MODULE(type)
Definition: MakerMacros.h:16
Abs< T >::type abs(const T &t)
Definition: Abs.h:22
const edm::EDGetTokenT< reco::CandidateView > src_
static int getParentHadronFlag(const reco::GenParticleRef match)
ParameterDescriptionBase * add(U const &iLabel, T const &value)
const edm::EDGetTokenT< edm::Association< reco::GenParticleCollection > > candMap_
void produce(edm::StreamID id, edm::Event &iEvent, const edm::EventSetup &iSetup) const override
void add(std::string const &label, ParameterSetDescription const &psetDescription)
CandMCMatchTableProducer(edm::ParameterSet const &params)
fixed size matrix
HLT enums.
std::pair< typename Association::data_type::first_type, double > match(Reference key, Association association, bool bestMatchByMaxValue)
Generic matching function.
Definition: Utils.h:10
def move(src, dest)
Definition: eostools.py:511
edm::View< Candidate > CandidateView
view of a collection containing candidates
Definition: CandidateFwd.h:23