PhysicsTools
NanoAOD
plugins
CandMCMatchTableProducer.cc
Go to the documentation of this file.
1
#include "
FWCore/Framework/interface/global/EDProducer.h
"
2
#include "
FWCore/Framework/interface/Event.h
"
3
#include "
FWCore/ParameterSet/interface/ParameterSet.h
"
4
#include "
FWCore/ParameterSet/interface/ConfigurationDescriptions.h
"
5
#include "
FWCore/ParameterSet/interface/ParameterSetDescription.h
"
6
#include "
DataFormats/NanoAOD/interface/FlatTable.h
"
7
#include "
DataFormats/Common/interface/View.h
"
8
#include "
DataFormats/Candidate/interface/Candidate.h
"
9
#include "
DataFormats/HepMCCandidate/interface/GenParticle.h
"
10
11
#include <vector>
12
#include <iostream>
13
14
class
CandMCMatchTableProducer
:
public
edm::global::EDProducer
<> {
15
public
:
16
CandMCMatchTableProducer
(
edm::ParameterSet
const
&
params
)
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, 11 = 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
) {
63
candMapVisTau_
=
64
consumes<edm::Association<reco::GenParticleCollection>>(
params
.getParameter<
edm::InputTag
>(
"mcMapVisTau"
));
65
}
66
}
67
68
~CandMCMatchTableProducer
()
override
{}
69
70
void
produce
(
edm::StreamID
id
,
edm::Event
&
iEvent
,
const
edm::EventSetup
& iSetup)
const override
{
71
edm::Handle<reco::CandidateView>
cands
;
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
77
edm::Handle<edm::Association<reco::GenParticleCollection>
>
map
;
78
iEvent
.getByToken(
candMap_
,
map
);
79
80
edm::Handle<edm::Association<reco::GenParticleCollection>
> mapVisTau;
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() &&
match
->pdgId() == 22)
118
flav[
i
] = 1;
// prompt photon
119
else
if
((
match
->isPromptFinalState() ||
match
->isDirectPromptTauDecayProductFinalState()) &&
120
abs
(
match
->pdgId()) == 11)
121
flav[
i
] = 11;
// prompt electron
122
break
;
123
case
MTau
:
124
// CV: assignment of status codes according to https://twiki.cern.ch/twiki/bin/viewauth/CMS/HiggsToTauTauWorking2016#MC_Matching
125
if
(
match
.isNonnull() &&
match
->statusFlags().isPrompt() &&
abs
(
match
->pdgId()) == 11)
126
flav[
i
] = 1;
127
else
if
(
match
.isNonnull() &&
match
->statusFlags().isPrompt() &&
abs
(
match
->pdgId()) == 13)
128
flav[
i
] = 2;
129
else
if
(
match
.isNonnull() &&
match
->isDirectPromptTauDecayProductFinalState() &&
abs
(
match
->pdgId()) == 11)
130
flav[
i
] = 3;
131
else
if
(
match
.isNonnull() &&
match
->isDirectPromptTauDecayProductFinalState() &&
abs
(
match
->pdgId()) == 13)
132
flav[
i
] = 4;
133
else
if
(matchVisTau.
isNonnull
())
134
flav[
i
] = 5;
135
break
;
136
default
:
137
flav[
i
] =
match
->statusFlags().fromHardProcess();
138
};
139
}
140
141
tab->addColumn<
int
>(
142
branchName_
+
"Idx"
,
key
,
"Index into genParticle list for "
+
doc_
,
nanoaod::FlatTable::IntColumn
);
143
tab->addColumn<uint8_t>(
branchName_
+
"Flav"
,
144
flav,
145
"Flavour of genParticle for "
+
doc_
+
": "
+
flavDoc_
,
146
nanoaod::FlatTable::UInt8Column
);
147
148
iEvent
.put(
std::move
(tab));
149
}
150
151
static
int
getParentHadronFlag
(
const
reco::GenParticleRef
match
) {
152
bool
has4 =
false
;
153
for
(
unsigned
int
im = 0, nm =
match
->numberOfMothers(); im < nm; ++im) {
154
reco::GenParticleRef
mom =
match
->motherRef(im);
155
assert
(mom.
isNonnull
() && mom.
isAvailable
());
// sanity
156
if
(mom.
key
() >=
match
.key())
157
continue
;
// prevent circular refs
158
int
id
=
std::abs
(mom->pdgId());
159
if
(
id
/ 1000 == 5 ||
id
/ 100 == 5 ||
id
== 5)
160
return
5;
161
if
(
id
/ 1000 == 4 ||
id
/ 100 == 4 ||
id
== 4)
162
has4 =
true
;
163
if
(mom->status() == 2) {
164
id
=
getParentHadronFlag
(mom);
165
if
(
id
== 5)
166
return
5;
167
else
if
(
id
== 4)
168
has4 =
true
;
169
}
170
}
171
return
has4 ? 4 : 3;
172
}
173
174
static
void
fillDescriptions
(
edm::ConfigurationDescriptions
& descriptions) {
175
edm::ParameterSetDescription
desc;
176
desc.
add
<
std::string
>(
"objName"
)->setComment(
"name of the nanoaod::FlatTable to extend with this table"
);
177
desc.
add
<
std::string
>(
"branchName"
)
178
->setComment(
179
"name of the column to write (the final branch in the nanoaod will be <objName>_<branchName>Idx and "
180
"<objName>_<branchName>Flav"
);
181
desc.
add
<
std::string
>(
"docString"
)->setComment(
"documentation to forward to the output"
);
182
desc.
add
<
edm::InputTag
>(
"src"
)->setComment(
183
"physics object collection for the reconstructed objects (e.g. leptons)"
);
184
desc.
add
<
edm::InputTag
>(
"mcMap"
)->setComment(
185
"tag to an edm::Association<GenParticleCollection> mapping src to gen, such as the one produced by MCMatcher"
);
186
desc.
add
<
std::string
>(
"objType"
)->setComment(
187
"type of object to match (Muon, Electron, Tau, Photon, Other), taylors what's in t Flav branch"
);
188
desc.
addOptional
<
edm::InputTag
>(
"mcMapVisTau"
)
189
->setComment(
"as mcMap, but pointing to the visible gen taus (only if objType == Tau)"
);
190
descriptions.
add
(
"candMcMatchTable"
, desc);
191
}
192
193
protected
:
194
const
std::string
objName_
,
branchName_
,
doc_
;
195
const
edm::EDGetTokenT<reco::CandidateView>
src_
;
196
const
edm::EDGetTokenT<edm::Association<reco::GenParticleCollection>
>
candMap_
;
197
edm::EDGetTokenT<edm::Association<reco::GenParticleCollection>
>
candMapVisTau_
;
198
enum
MatchType
{
MMuon
,
MElectron
,
MTau
,
MPhoton
,
MOther
}
type_
;
199
std::string
flavDoc_
;
200
};
201
202
#include "
FWCore/Framework/interface/MakerMacros.h
"
203
DEFINE_FWK_MODULE
(
CandMCMatchTableProducer
);
ConfigurationDescriptions.h
edm::StreamID
Definition:
StreamID.h:30
reco::CandidateView
edm::View< Candidate > CandidateView
view of a collection containing candidates
Definition:
CandidateFwd.h:23
mps_fire.i
i
Definition:
mps_fire.py:355
edm::ParameterSetDescription::add
ParameterDescriptionBase * add(U const &iLabel, T const &value)
Definition:
ParameterSetDescription.h:95
CandMCMatchTableProducer::MPhoton
Definition:
CandMCMatchTableProducer.cc:198
CandMCMatchTableProducer::candMap_
const edm::EDGetTokenT< edm::Association< reco::GenParticleCollection > > candMap_
Definition:
CandMCMatchTableProducer.cc:196
CandMCMatchTableProducer
Definition:
CandMCMatchTableProducer.cc:14
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:195
edm::EDGetTokenT
Definition:
EDGetToken.h:33
edm
HLT enums.
Definition:
AlignableModifier.h:19
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::CandMCMatchTableProducer
CandMCMatchTableProducer(edm::ParameterSet const ¶ms)
Definition:
CandMCMatchTableProducer.cc:16
reco
fixed size matrix
Definition:
AlignmentAlgorithmBase.h:45
CandMCMatchTableProducer::~CandMCMatchTableProducer
~CandMCMatchTableProducer() override
Definition:
CandMCMatchTableProducer.cc:68
edm::Handle
Definition:
AssociativeIterator.h:50
edm::ParameterSetDescription::addOptional
ParameterDescriptionBase * addOptional(U const &iLabel, T const &value)
Definition:
ParameterSetDescription.h:105
CandMCMatchTableProducer::MatchType
MatchType
Definition:
CandMCMatchTableProducer.cc:198
edm::Ref< GenParticleCollection >
GenParticle.h
MakerMacros.h
CandMCMatchTableProducer::flavDoc_
std::string flavDoc_
Definition:
CandMCMatchTableProducer.cc:199
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_
CandMCMatchTableProducer::branchName_
const std::string branchName_
Definition:
CandMCMatchTableProducer.cc:194
CandMCMatchTableProducer::MElectron
Definition:
CandMCMatchTableProducer.cc:198
ParameterSetDescription.h
edm::global::EDProducer
Definition:
EDProducer.h:32
edm::ConfigurationDescriptions
Definition:
ConfigurationDescriptions.h:28
AlCaHLTBitMon_QueryRunRegistry.string
string
Definition:
AlCaHLTBitMon_QueryRunRegistry.py:256
HLT_2018_cff.InputTag
InputTag
Definition:
HLT_2018_cff.py:79016
edm::ParameterSet
Definition:
ParameterSet.h:36
Event.h
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:174
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:70
edm::EventSetup
Definition:
EventSetup.h:57
nanoaod::FlatTable::UInt8Column
Definition:
FlatTable.h:41
CandMCMatchTableProducer::objName_
const std::string objName_
Definition:
CandMCMatchTableProducer.cc:194
FlatTable.h
nanoaod::FlatTable::IntColumn
Definition:
FlatTable.h:40
type
type
Definition:
HCALResponse.h:21
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:151
CandMCMatchTableProducer::candMapVisTau_
edm::EDGetTokenT< edm::Association< reco::GenParticleCollection > > candMapVisTau_
Definition:
CandMCMatchTableProducer.cc:197
Exception
Definition:
hltDiff.cc:246
CandMCMatchTableProducer::MTau
Definition:
CandMCMatchTableProducer.cc:198
CandMCMatchTableProducer::doc_
const std::string doc_
Definition:
CandMCMatchTableProducer.cc:194
HLT_2018_cff.cands
cands
Definition:
HLT_2018_cff.py:13762
CandMCMatchTableProducer::MOther
Definition:
CandMCMatchTableProducer.cc:198
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:126
EDProducer.h
edm::Event
Definition:
Event.h:73
crabWrapper.key
key
Definition:
crabWrapper.py:19
CandMCMatchTableProducer::MMuon
Definition:
CandMCMatchTableProducer.cc:198
edm::InputTag
Definition:
InputTag.h:15
Generated for CMSSW Reference Manual by
1.8.16