CMS 3D CMS Logo

CandMCMatchTableProducer.cc
Go to the documentation of this file.
10 
11 #include <vector>
12 #include <iostream>
13 
14 
16  public:
18  objName_(params.getParameter<std::string>("objName")),
19  branchName_(params.getParameter<std::string>("branchName")),
20  doc_(params.getParameter<std::string>("docString")),
21  src_(consumes<reco::CandidateView>(params.getParameter<edm::InputTag>("src"))),
22  candMap_(consumes<edm::Association<reco::GenParticleCollection>>(params.getParameter<edm::InputTag>("mcMap")))
23  {
24  produces<nanoaod::FlatTable>();
25  const std::string & type = params.getParameter<std::string>("objType");
26  if (type == "Muon") type_ = MMuon;
27  else if (type == "Electron") type_ = MElectron;
28  else if (type == "Tau") type_ = MTau;
29  else if (type == "Photon") type_ = MPhoton;
30  else if (type == "Other") type_ = MOther;
31  else throw cms::Exception("Configuration", "Unsupported objType '"+type+"'\n");
32 
33  switch(type_) {
34  case MMuon: flavDoc_ = "1 = prompt muon (including gamma*->mu mu), 15 = muon from prompt tau, " // continues below
35  "5 = muon from b, 4 = muon from c, 3 = muon from light or unknown, 0 = unmatched"; break;
36  case MElectron: flavDoc_ = "1 = prompt electron (including gamma*->mu mu), 15 = electron from prompt tau, 22 = prompt photon (likely conversion), " // continues below
37  "5 = electron from b, 4 = electron from c, 3 = electron from light or unknown, 0 = unmatched"; break;
38  case MPhoton: flavDoc_ = "1 = prompt photon, 13 = prompt electron, 0 = unknown or unmatched"; break;
39  case MTau: flavDoc_ = "1 = prompt electron, 2 = prompt muon, 3 = tau->e decay, 4 = tau->mu decay, 5 = hadronic tau decay, 0 = unknown or unmatched"; break;
40  case MOther: flavDoc_ = "1 = from hard scatter, 0 = unknown or unmatched"; break;
41  }
42 
43  if ( type_ == MTau ) {
44  candMapVisTau_ = consumes<edm::Association<reco::GenParticleCollection>>(params.getParameter<edm::InputTag>("mcMapVisTau"));
45  }
46  }
47 
49 
50  void produce(edm::StreamID id, edm::Event& iEvent, const edm::EventSetup& iSetup) const override {
51 
53  iEvent.getByToken(src_, cands);
54  unsigned int ncand = cands->size();
55 
56  auto tab = std::make_unique<nanoaod::FlatTable>(ncand, objName_, false, true);
57 
59  iEvent.getByToken(candMap_, map);
60 
62  if ( type_ == MTau ) {
63  iEvent.getByToken(candMapVisTau_, mapVisTau);
64  }
65 
66  std::vector<int> key(ncand, -1), flav(ncand, 0);
67  for (unsigned int i = 0; i < ncand; ++i) {
68  //std::cout << "cand #" << i << ": pT = " << cands->ptrAt(i)->pt() << ", eta = " << cands->ptrAt(i)->eta() << ", phi = " << cands->ptrAt(i)->phi() << std::endl;
69  reco::GenParticleRef match = (*map)[cands->ptrAt(i)];
70  reco::GenParticleRef matchVisTau;
71  if ( type_ == MTau ) {
72  matchVisTau = (*mapVisTau)[cands->ptrAt(i)];
73  }
74  if ( match.isNonnull() ) key[i] = match.key();
75  else if ( matchVisTau.isNonnull() ) key[i] = matchVisTau.key();
76  else continue;
77  switch(type_) {
78  case MMuon:
79  if (match->isPromptFinalState()) flav[i] = 1; // prompt
80  else if (match->isDirectPromptTauDecayProductFinalState()) flav[i] = 15; // tau
81  else flav[i] = getParentHadronFlag(match); // 3 = light, 4 = charm, 5 = b
82  break;
83  case MElectron:
84  if (match->isPromptFinalState()) flav[i] = (match->pdgId() == 22 ? 22 : 1); // prompt electron or photon
85  else if (match->isDirectPromptTauDecayProductFinalState()) flav[i] = 15; // tau
86  else flav[i] = getParentHadronFlag(match); // 3 = light, 4 = charm, 5 = b
87  break;
88  case MPhoton:
89  if (match->isPromptFinalState()) flav[i] = (match->pdgId() == 22 ? 1 : 13); // prompt electron or photon
90  break;
91  case MTau:
92  // CV: assignment of status codes according to https://twiki.cern.ch/twiki/bin/viewauth/CMS/HiggsToTauTauWorking2016#MC_Matching
93  if ( match.isNonnull() && match->isPromptFinalState() && abs(match->pdgId()) == 11 ) flav[i] = 1;
94  else if ( match.isNonnull() && match->isPromptFinalState() && abs(match->pdgId()) == 13 ) flav[i] = 2;
95  else if ( match.isNonnull() && match->isDirectPromptTauDecayProductFinalState() && abs(match->pdgId()) == 11 ) flav[i] = 3;
96  else if ( match.isNonnull() && match->isDirectPromptTauDecayProductFinalState() && abs(match->pdgId()) == 13 ) flav[i] = 4;
97  else if ( matchVisTau.isNonnull() ) flav[i] = 5;
98  break;
99  default:
100  flav[i] = match->statusFlags().fromHardProcess();
101  };
102  }
103 
104  tab->addColumn<int>(branchName_+"Idx", key, "Index into genParticle list for "+doc_, nanoaod::FlatTable::IntColumn);
105  tab->addColumn<uint8_t>(branchName_+"Flav", flav, "Flavour of genParticle for "+doc_+": "+flavDoc_, nanoaod::FlatTable::UInt8Column);
106 
107  iEvent.put(std::move(tab));
108  }
109 
111  bool has4 = false;
112  for (unsigned int im = 0, nm = match->numberOfMothers(); im < nm; ++im) {
113  reco::GenParticleRef mom = match->motherRef(im);
114  assert(mom.isNonnull() && mom.isAvailable()); // sanity
115  if (mom.key() >= match.key()) continue; // prevent circular refs
116  int id = std::abs(mom->pdgId());
117  if (id / 1000 == 5 || id / 100 == 5 || id == 5) return 5;
118  if (id / 1000 == 4 || id / 100 == 4 || id == 4) has4 = true;
119  if (mom->status() == 2) {
120  id = getParentHadronFlag(mom);
121  if (id == 5) return 5;
122  else if (id == 4) has4 = true;
123  }
124  }
125  return has4 ? 4 : 3;
126  }
127 
128  static void fillDescriptions(edm::ConfigurationDescriptions & descriptions) {
130  desc.add<std::string>("objName")->setComment("name of the nanoaod::FlatTable to extend with this table");
131  desc.add<std::string>("branchName")->setComment("name of the column to write (the final branch in the nanoaod will be <objName>_<branchName>Idx and <objName>_<branchName>Flav");
132  desc.add<std::string>("docString")->setComment("documentation to forward to the output");
133  desc.add<edm::InputTag>("src")->setComment("physics object collection for the reconstructed objects (e.g. leptons)");
134  desc.add<edm::InputTag>("mcMap")->setComment("tag to an edm::Association<GenParticleCollection> mapping src to gen, such as the one produced by MCMatcher");
135  desc.add<std::string>("objType")->setComment("type of object to match (Muon, Electron, Tau, Photon, Other), taylors what's in t Flav branch");
136  desc.addOptional<edm::InputTag>("mcMapVisTau")->setComment("as mcMap, but pointing to the visible gen taus (only if objType == Tau)");
137  descriptions.add("candMcMatchTable", desc);
138  }
139 
140  protected:
147 };
148 
151 
bool isAvailable() const
Definition: Ref.h:577
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:137
ParameterDescriptionBase * addOptional(U const &iLabel, T const &value)
bool isNonnull() const
Checks for non-null.
Definition: Ref.h:253
bool getByToken(EDGetToken token, Handle< PROD > &result) const
Definition: Event.h:579
Ptr< value_type > ptrAt(size_type i) const
#define DEFINE_FWK_MODULE(type)
Definition: MakerMacros.h:17
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:265
edm::EDGetTokenT< edm::Association< reco::GenParticleCollection > > candMapVisTau_
EDGetTokenT< ProductType > consumes(edm::InputTag const &tag)
int iEvent
Definition: GenABIO.cc:230
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