64 #include <boost/foreach.hpp>
65 #define foreach BOOST_FOREACH
108 const std::vector<T> &
values,
112 if (tp.
isNonnull() && tp->parentVertex().
isNonnull() && !tp->parentVertex()->sourceTracks().empty()) {
113 return tp->parentVertex()->sourceTracks()[0];
129 muonsToken_(consumes<edm::
View<
reco::
Muon> >(iConfig.getParameter<edm::InputTag>(
"muons"))),
130 hasMuonCut_(iConfig.existsAs<std::
string>(
"muonPreselection")),
131 muonCut_(hasMuonCut_ ? iConfig.getParameter<std::
string>(
"muonPreselection") :
""),
133 associatorLabel_(iConfig.getParameter< std::
string >(
"associatorLabel")),
134 decayRho_(iConfig.getParameter<double>(
"decayRho")),
135 decayAbsZ_(iConfig.getParameter<double>(
"decayAbsZ")),
136 linkToGenParticles_(iConfig.getParameter<bool>(
"linkToGenParticles")),
137 genParticles_(linkToGenParticles_ ? iConfig.getParameter<edm::InputTag>(
"genParticles") : edm::InputTag(
"NONE"))
144 else throw cms::Exception(
"Configuration") <<
"Track type '" << trackType <<
"' not supported.\n";
149 produces<edm::ValueMap<int> >();
150 produces<edm::ValueMap<int> >(
"ext");
151 produces<edm::ValueMap<int> >(
"flav");
152 produces<edm::ValueMap<int> >(
"hitsPdgId");
153 produces<edm::ValueMap<int> >(
"momPdgId");
154 produces<edm::ValueMap<int> >(
"momFlav");
155 produces<edm::ValueMap<int> >(
"momStatus");
156 produces<edm::ValueMap<int> >(
"gmomPdgId");
157 produces<edm::ValueMap<int> >(
"gmomFlav");
158 produces<edm::ValueMap<int> >(
"hmomFlav");
159 produces<edm::ValueMap<int> >(
"tpId");
160 produces<edm::ValueMap<float> >(
"prodRho");
161 produces<edm::ValueMap<float> >(
"prodZ");
162 produces<edm::ValueMap<float> >(
"momRho");
163 produces<edm::ValueMap<float> >(
"momZ");
164 produces<edm::ValueMap<float> >(
"tpAssoQuality");
166 produces<reco::GenParticleCollection>(
"secondaries");
167 produces<edm::Association<reco::GenParticleCollection> >(
"toPrimaries");
168 produces<edm::Association<reco::GenParticleCollection> >(
"toSecondaries");
195 if (assoByHits == 0)
throw cms::Exception(
"Configuration") <<
"The Track Associator with label '" <<
associatorLabel_ <<
"' is not a MuonAssociatorByHits.\n";
199 edm::LogVerbatim(
"MuonMCClassifier") <<
"\n ***************************************************************** ";
201 edm::LogVerbatim(
"MuonMCClassifier") <<
" ***************************************************************** \n";
206 for (
size_t i = 0,
n = muons->size();
i <
n; ++
i) {
213 for (
size_t i = 0,
n = muons->size();
i <
n; ++
i) {
220 for (
size_t i = 0,
n = trackingParticles->size();
i <
n; ++
i) {
230 edm::LogVerbatim(
"MuonMCClassifier") <<
"\n ***************************************************************** ";
231 edm::LogVerbatim(
"MuonMCClassifier") <<
" STANDALONE (UpdAtVtx) MUON association ";
232 edm::LogVerbatim(
"MuonMCClassifier") <<
" ***************************************************************** \n";
234 allTPs, &iEvent, &iSetup);
237 typedef MuonAssociatorByHits::MuonToSimCollection::const_iterator r2s_it;
238 typedef MuonAssociatorByHits::SimToMuonCollection::const_iterator s2r_it;
240 size_t nmu = muons->size();
241 edm::LogVerbatim(
"MuonMCClassifier") <<
"\n There are "<<nmu<<
" reco::Muons.";
243 std::vector<int> classif(nmu, 0), ext(nmu, 0);
244 std::vector<int> hitsPdgId(nmu, 0), momPdgId(nmu, 0), gmomPdgId(nmu, 0), momStatus(nmu, 0);
245 std::vector<int> flav(nmu, 0), momFlav(nmu, 0), gmomFlav(nmu, 0), hmomFlav(nmu, 0);
246 std::vector<int> tpId(nmu, -1);
247 std::vector<float> prodRho(nmu, 0.0), prodZ(nmu, 0.0), momRho(nmu, 0.0), momZ(nmu, 0.0);
248 std::vector<float> tpAssoQuality(nmu, -1);
250 std::auto_ptr<reco::GenParticleCollection> secondaries;
251 std::map<TrackingParticleRef, int> tpToSecondaries;
252 std::vector<int> muToPrimary(nmu, -1), muToSecondary(nmu, -1);
255 for(
size_t i = 0;
i < nmu; ++
i) {
259 edm::LogVerbatim(
"MuonMCClassifier") <<
"\t muon didn't pass the selection. classified as -99 and skipped";
260 classif[
i] = -99;
continue;
265 r2s_it
match = recSimColl.find(mu);
267 if (match != recSimColl.end()) {
270 tp = match->second.front().first;
272 tpAssoQuality[
i] = match->second.front().second;
273 s2r_it matchback = simRecColl.find(tp);
274 if (matchback != simRecColl.end()) {
275 muMatchBack = matchback->second.front().first;
277 edm::LogWarning(
"MuonMCClassifier") <<
"\n***WARNING: This I do NOT understand: why no match back? *** \n";
282 r2s_it matchSta = UpdSTA_recSimColl.find(mu);
283 if (matchSta != UpdSTA_recSimColl.end()) {
284 edm::LogVerbatim(
"MuonMCClassifier") <<
"\t RtS matched Ok... from the UpdSTA_recSimColl ";
285 tp = matchSta->second.front().first;
287 tpAssoQuality[
i] = matchSta->second.front().second;
288 s2r_it matchback = UpdSTA_simRecColl.find(tp);
289 if (matchback != UpdSTA_simRecColl.end()) {
290 muMatchBack = matchback->second.front().first;
292 edm::LogWarning(
"MuonMCClassifier") <<
"\n***WARNING: This I do NOT understand: why no match back in UpdSTA? *** \n";
297 bool isGhost = muMatchBack !=
mu;
298 if (isGhost)
edm::LogVerbatim(
"MuonMCClassifier") <<
"\t This seems a GHOST ! classif[i] will be < 0";
300 hitsPdgId[
i] = tp->pdgId();
301 prodRho[
i] = tp->vertex().Rho();
302 prodZ[
i] = tp->vertex().Z();
303 edm::LogVerbatim(
"MuonMCClassifier") <<
"\t TP pdgId = "<<hitsPdgId[
i] <<
", vertex rho = " << prodRho[
i] <<
", z = " << prodZ[
i];
307 if (!tp->genParticles().empty()) {
311 momPdgId[
i] = genMom->pdgId();
312 momStatus[
i] = genMom->status();
313 momRho[
i] = genMom->vertex().Rho(); momZ[
i] = genMom->vz();
314 edm::LogVerbatim(
"MuonMCClassifier") <<
"\t Particle pdgId = "<<hitsPdgId[
i] <<
" produced at rho = " << prodRho[
i] <<
", z = " << prodZ[
i] <<
", has GEN mother pdgId = " << momPdgId[
i];
317 gmomPdgId[
i] = genGMom->pdgId();
318 edm::LogVerbatim(
"MuonMCClassifier") <<
"\t\t mother prod. vertex rho = " << momRho[
i] <<
", z = " << momZ[
i] <<
", grand-mom pdgId = " << gmomPdgId[
i];
324 int flav =
flavour(nMom->pdgId());
325 if (hmomFlav[
i] < flav) hmomFlav[
i] = flav;
326 edm::LogVerbatim(
"MuonMCClassifier") <<
"\t\t backtracking flavour: mom pdgId = "<<nMom->pdgId()<<
", flavour = " << flav <<
", heaviest so far = " << hmomFlav[
i];
332 momPdgId[
i] = simMom->pdgId();
333 momRho[
i] = simMom->vertex().Rho();
334 momZ[
i] = simMom->vertex().Z();
335 edm::LogVerbatim(
"MuonMCClassifier") <<
"\t Particle pdgId = "<<hitsPdgId[
i] <<
" produced at rho = " << prodRho[
i] <<
", z = " << prodZ[
i] <<
336 ", has SIM mother pdgId = " << momPdgId[
i] <<
" produced at rho = " << simMom->vertex().Rho() <<
", z = " << simMom->vertex().Z();
337 if (!simMom->genParticles().empty()) {
338 momStatus[
i] = simMom->genParticles()[0]->status();
340 if (genGMom.
isNonnull()) gmomPdgId[
i] = genGMom->pdgId();
341 edm::LogVerbatim(
"MuonMCClassifier") <<
"\t\t SIM mother is in GEN (status " << momStatus[
i] <<
"), grand-mom id = " << gmomPdgId[
i];
345 if (simGMom.
isNonnull()) gmomPdgId[
i] = simGMom->pdgId();
346 edm::LogVerbatim(
"MuonMCClassifier") <<
"\t\t SIM mother is in SIM only, grand-mom id = " << gmomPdgId[
i];
349 edm::LogVerbatim(
"MuonMCClassifier") <<
"\t Particle pdgId = "<<hitsPdgId[
i] <<
" produced at rho = " << prodRho[
i] <<
", z = " << prodZ[
i] <<
", has NO mother!";
353 gmomFlav[
i] =
flavour(gmomPdgId[i]);
356 if (
abs(tp->pdgId()) != 13) {
357 classif[
i] = isGhost ? -1 : 1;
358 ext[
i] = isGhost ? -1 : 1;
359 edm::LogVerbatim(
"MuonMCClassifier") <<
"\t This is not a muon. Sorry. classif[i] = " << classif[
i];
364 if (!tp->genParticles().empty() && (momPdgId[
i] != 0)) {
365 if (
abs(momPdgId[i]) < 100 && (
abs(momPdgId[i]) != 15)) {
366 classif[
i] = isGhost ? -4 : 4;
367 flav[
i] = (
abs(momPdgId[i]) == 15 ? 15 : 13);
368 edm::LogVerbatim(
"MuonMCClassifier") <<
"\t This seems PRIMARY MUON ! classif[i] = " << classif[
i];
370 }
else if (momFlav[i] == 4 || momFlav[i] == 5 || momFlav[i] == 15) {
371 classif[
i] = isGhost ? -3 : 3;
372 flav[
i] = momFlav[
i];
373 if (momFlav[i] == 15) ext[
i] = 9;
374 else if (momFlav[i] == 5) ext[
i] = 8;
375 else if (hmomFlav[i] == 5) ext[
i] = 7;
377 edm::LogVerbatim(
"MuonMCClassifier") <<
"\t This seems HEAVY FLAVOUR ! classif[i] = " << classif[
i];
379 classif[
i] = isGhost ? -2 : 2;
380 flav[
i] = momFlav[
i];
381 edm::LogVerbatim(
"MuonMCClassifier") <<
"\t This seems LIGHT FLAVOUR ! classif[i] = " << classif[
i];
384 classif[
i] = isGhost ? -2 : 2;
385 flav[
i] = momFlav[
i];
386 edm::LogVerbatim(
"MuonMCClassifier") <<
"\t This seems LIGHT FLAVOUR ! classif[i] = " << classif[
i];
389 if (momPdgId[i] == 0) ext[
i] = 2;
390 else if (
abs(momPdgId[i]) < 100) ext[
i] = (momFlav[
i] == 15 ? 9 : 10);
391 else if (momFlav[i] == 5) ext[
i] = 8;
392 else if (momFlav[i] == 4) ext[
i] = (hmomFlav[
i] == 5 ? 7 : 6);
393 else if (momStatus[i] != -1) {
394 int id =
abs(momPdgId[i]);
395 if (
id != 211 &&
id != 321 &&
id != 130 ) ext[
i] = 5;
399 if (isGhost) ext[
i] = -ext[
i];
403 if (!tp->genParticles().empty() &&
abs(ext[i]) >= 5) {
404 if (genParticles.
id() != tp->genParticles().
id()) {
405 throw cms::Exception(
"Configuration") <<
"Product ID mismatch between the genParticle collection (" <<
genParticles_ <<
", id " << genParticles.
id() <<
") and the references in the TrackingParticles (id " << tp->genParticles().
id() <<
")\n";
407 muToPrimary[
i] = tp->genParticles()[0].
key();
410 int &indexPlus1 = tpToSecondaries[tp];
412 muToSecondary[
i] = indexPlus1 - 1;
415 edm::LogVerbatim(
"MuonMCClassifier") <<
"\t Extended classification code = " << ext[
i];
434 writeValueMap(iEvent, muons, tpAssoQuality,
"tpAssoQuality");
443 fillPri.insert(muons, muToPrimary.begin(), muToPrimary.end());
444 fillSec.
insert(muons, muToSecondary.begin(), muToSecondary.end());
445 fillPri.fill(); fillSec.
fill();
446 iEvent.
put(outPri,
"toPrimaries");
447 iEvent.
put(outSec,
"toSecondaries");
455 const std::vector<T> &
values,
464 iEvent.
put(valMap, label);
469 int flav =
abs(pdgId);
472 if (flav <= 37 && flav != 21)
return flav;
474 int bflav = ((flav / 1000) % 10);
475 if (bflav != 0)
return bflav;
477 int mflav = ((flav / 100) % 10);
478 if (mflav != 0)
return mflav;
489 if (simMom.
isNonnull() && !simMom->genParticles().empty()) {
490 if (genParticles.
id() != simMom->genParticles().
id()) {
491 throw cms::Exception(
"Configuration") <<
"Product ID mismatch between the genParticle collection (" <<
genParticles_ <<
", id " << genParticles.
id() <<
") and the references in the TrackingParticles (id " << simMom->genParticles().
id() <<
")\n";
493 out.back().addMother(simMom->genParticles()[0]);
edm::InputTag genParticles_
bool linkToGenParticles_
Create a link to the generator level particles.
std::vector< GenParticle > GenParticleCollection
collection of GenParticles
T getParameter(std::string const &) const
bool isNonnull() const
Checks for non-null.
std::vector< TrackingParticle > TrackingParticleCollection
bool getByToken(EDGetToken token, Handle< PROD > &result) const
#define DEFINE_FWK_MODULE(type)
void associateMuons(MuonToSimCollection &recoToSim, SimToMuonCollection &simToReco, const edm::RefToBaseVector< reco::Muon > &, MuonTrackType, const edm::RefVector< TrackingParticleCollection > &, const edm::Event *event=0, const edm::EventSetup *setup=0) const
int pdgId() const
PDG ID.
edm::Ref< GenParticleCollection > GenParticleRef
persistent reference to a GenParticle
void insert(const H &h, I begin, I end)
const_iterator end() const
bool isGlobalMuon() const
int convertAndPush(const TrackingParticle &tp, reco::GenParticleCollection &out, const TrackingParticleRef &momRef, const edm::Handle< reco::GenParticleCollection > &genParticles) const
key_type key() const
Accessor for product key.
void find(edm::Handle< EcalRecHitCollection > &hits, DetId thisDet, std::vector< EcalRecHitCollection::const_iterator > &hit, bool debug=false)
int status() const
Status word.
ProductID id() const
Accessor for product ID.
float charge() const
Gives charge in unit of quark charge (should be 3 time the abaove)
std::map< TrackingParticleRef, std::vector< std::pair< edm::RefToBase< reco::Muon >, double > > > SimToMuonCollection
OrphanHandle< PROD > put(std::auto_ptr< PROD > product)
Put a new product.
Abs< T >::type abs(const T &t)
TrackingParticleRef getTpMother(TrackingParticleRef tp)
MuonMCClassifier(const edm::ParameterSet &)
std::string associatorLabel_
The Associations.
tuple genp
produce generated paricles in acceptance #
edm::EDGetTokenT< reco::GenParticleCollection > genParticlesToken_
MuonAssociatorByHits::MuonTrackType trackType_
Track to use.
const LorentzVector & p4() const
Four-momentum Lorentz vector. Note this is taken from the first SimTrack only.
T const * product() const
void writeValueMap(edm::Event &iEvent, const edm::Handle< edm::View< reco::Muon > > &handle, const std::vector< T > &values, const std::string &label) const
Write a ValueMap<int> in the event.
const_iterator begin() const
double decayRho_
Cylinder to use to decide if a decay is early or late.
edm::EDGetTokenT< edm::View< reco::Muon > > muonsToken_
The RECO objects.
void push_back(const RefToBase< T > &)
void push_back(value_type const &ref)
Add a Ref<C, T> to the RefVector.
Monte Carlo truth information used for tracking validation.
std::pair< typename Association::data_type::first_type, double > match(Reference key, Association association, bool bestMatchByMaxValue)
Generic matching function.
int flavour(int pdgId) const
Returns the flavour given a pdg id code.
virtual void produce(edm::Event &, const edm::EventSetup &) override
Analysis-level muon class.
std::map< edm::RefToBase< reco::Muon >, std::vector< std::pair< TrackingParticleRef, double > >, RefToBaseSort > MuonToSimCollection
edm::Ref< TrackingParticleCollection > TrackingParticleRef
edm::EDGetTokenT< TrackingParticleCollection > trackingParticlesToken_
The TrackingParticle objects.
StringCutObjectSelector< pat::Muon > muonCut_