65 #include <boost/foreach.hpp>
66 #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];
121 const HepMC::GenVertex *vtx = gp->production_vertex();
122 if (vtx != 0 && vtx->particles_in_size() > 0) {
123 return *vtx->particles_in_const_begin();
130 int fetch(
const edm::Handle<std::vector<int> > & genBarcodes,
int barcode)
const;
139 const edm::Handle<std::vector<int> > & genBarcodes)
const ;
143 muons_(iConfig.getParameter<edm::InputTag>(
"muons")),
144 hasMuonCut_(iConfig.existsAs<std::
string>(
"muonPreselection")),
145 muonCut_(hasMuonCut_ ? iConfig.getParameter<std::
string>(
"muonPreselection") :
""),
146 trackingParticles_(iConfig.getParameter<edm::InputTag>(
"trackingParticles")),
147 associatorLabel_(iConfig.getParameter< std::
string >(
"associatorLabel")),
148 decayRho_(iConfig.getParameter<double>(
"decayRho")),
149 decayAbsZ_(iConfig.getParameter<double>(
"decayAbsZ")),
150 linkToGenParticles_(iConfig.getParameter<bool>(
"linkToGenParticles")),
151 genParticles_(linkToGenParticles_ ? iConfig.getParameter<edm::InputTag>(
"genParticles") : edm::InputTag(
"NONE"))
158 else throw cms::Exception(
"Configuration") <<
"Track type '" << trackType <<
"' not supported.\n";
160 produces<edm::ValueMap<int> >();
161 produces<edm::ValueMap<int> >(
"ext");
162 produces<edm::ValueMap<int> >(
"flav");
163 produces<edm::ValueMap<int> >(
"hitsPdgId");
164 produces<edm::ValueMap<int> >(
"momPdgId");
165 produces<edm::ValueMap<int> >(
"momFlav");
166 produces<edm::ValueMap<int> >(
"momStatus");
167 produces<edm::ValueMap<int> >(
"gmomPdgId");
168 produces<edm::ValueMap<int> >(
"gmomFlav");
169 produces<edm::ValueMap<int> >(
"hmomFlav");
170 produces<edm::ValueMap<int> >(
"tpId");
171 produces<edm::ValueMap<float> >(
"prodRho");
172 produces<edm::ValueMap<float> >(
"prodZ");
173 produces<edm::ValueMap<float> >(
"momRho");
174 produces<edm::ValueMap<float> >(
"momZ");
175 produces<edm::ValueMap<float> >(
"tpAssoQuality");
177 produces<reco::GenParticleCollection>(
"secondaries");
178 produces<edm::Association<reco::GenParticleCollection> >(
"toPrimaries");
179 produces<edm::Association<reco::GenParticleCollection> >(
"toSecondaries");
208 if (assoByHits == 0)
throw cms::Exception(
"Configuration") <<
"The Track Associator with label '" <<
associatorLabel_ <<
"' is not a MuonAssociatorByHits.\n";
212 edm::LogVerbatim(
"MuonMCClassifier") <<
"\n ***************************************************************** ";
214 edm::LogVerbatim(
"MuonMCClassifier") <<
" ***************************************************************** \n";
219 selMuons = muons->refVector();
223 for (
size_t i = 0,
n = muons->size();
i <
n; ++
i) {
230 for (
size_t i = 0,
n = trackingParticles->size();
i <
n; ++
i) {
240 edm::LogVerbatim(
"MuonMCClassifier") <<
"\n ***************************************************************** ";
241 edm::LogVerbatim(
"MuonMCClassifier") <<
" STANDALONE (UpdAtVtx) MUON association ";
242 edm::LogVerbatim(
"MuonMCClassifier") <<
" ***************************************************************** \n";
244 allTPs, &iEvent, &iSetup);
247 typedef MuonAssociatorByHits::MuonToSimCollection::const_iterator r2s_it;
248 typedef MuonAssociatorByHits::SimToMuonCollection::const_iterator s2r_it;
250 size_t nmu = muons->size();
251 edm::LogVerbatim(
"MuonMCClassifier") <<
"\n There are "<<nmu<<
" reco::Muons.";
253 std::vector<int> classif(nmu, 0), ext(nmu, 0);
254 std::vector<int> hitsPdgId(nmu, 0), momPdgId(nmu, 0), gmomPdgId(nmu, 0), momStatus(nmu, 0);
255 std::vector<int> flav(nmu, 0), momFlav(nmu, 0), gmomFlav(nmu, 0), hmomFlav(nmu, 0);
256 std::vector<int> tpId(nmu, -1);
257 std::vector<float> prodRho(nmu, 0.0), prodZ(nmu, 0.0), momRho(nmu, 0.0), momZ(nmu, 0.0);
258 std::vector<float> tpAssoQuality(nmu, -1);
260 std::auto_ptr<reco::GenParticleCollection> secondaries;
261 std::map<TrackingParticleRef, int> tpToSecondaries;
262 std::vector<int> muToPrimary(nmu, -1), muToSecondary(nmu, -1);
265 for(
size_t i = 0;
i < nmu; ++
i) {
269 edm::LogVerbatim(
"MuonMCClassifier") <<
"\t muon didn't pass the selection. classified as -99 and skipped";
270 classif[
i] = -99;
continue;
275 r2s_it
match = recSimColl.find(mu);
277 if (match != recSimColl.end()) {
280 tp = match->second.front().first;
282 tpAssoQuality[
i] = match->second.front().second;
283 s2r_it matchback = simRecColl.find(tp);
284 if (matchback != simRecColl.end()) {
285 muMatchBack = matchback->second.front().first;
287 edm::LogWarning(
"MuonMCClassifier") <<
"\n***WARNING: This I do NOT understand: why no match back? *** \n";
292 r2s_it matchSta = UpdSTA_recSimColl.find(mu);
293 if (matchSta != UpdSTA_recSimColl.end()) {
294 edm::LogVerbatim(
"MuonMCClassifier") <<
"\t RtS matched Ok... from the UpdSTA_recSimColl ";
295 tp = matchSta->second.front().first;
297 tpAssoQuality[
i] = matchSta->second.front().second;
298 s2r_it matchback = UpdSTA_simRecColl.find(tp);
299 if (matchback != UpdSTA_simRecColl.end()) {
300 muMatchBack = matchback->second.front().first;
302 edm::LogWarning(
"MuonMCClassifier") <<
"\n***WARNING: This I do NOT understand: why no match back in UpdSTA? *** \n";
307 bool isGhost = muMatchBack !=
mu;
308 if (isGhost)
edm::LogVerbatim(
"MuonMCClassifier") <<
"\t This seems a GHOST ! classif[i] will be < 0";
310 hitsPdgId[
i] = tp->pdgId();
311 prodRho[
i] = tp->vertex().Rho();
312 prodZ[
i] = tp->vertex().Z();
313 edm::LogVerbatim(
"MuonMCClassifier") <<
"\t TP pdgId = "<<hitsPdgId[
i] <<
", vertex rho = " << prodRho[
i] <<
", z = " << prodZ[
i];
317 if (!tp->genParticles().empty()) {
318 #warning "This file has been modified just to get it to compile without any regard as to whether it still functions as intended"
319 #ifdef REMOVED_JUST_TO_GET_IT_TO_COMPILE__THIS_CODE_NEEDS_TO_BE_CHECKED
325 momPdgId[
i] = tp->pdgId();
326 momStatus[
i] = tp->status();
327 if (genMom->production_vertex()) {
328 const HepMC::ThreeVector & momVtx = genMom->production_vertex()->point3d();
329 momRho[
i] = momVtx.perp() * 0.1; momZ[
i] = momVtx.z() * 0.1;
331 edm::LogVerbatim(
"MuonMCClassifier") <<
"\t Particle pdgId = "<<hitsPdgId[
i] <<
" produced at rho = " << prodRho[
i] <<
", z = " << prodZ[
i] <<
", has GEN mother pdgId = " << momPdgId[
i];
334 gmomPdgId[
i] = genGMom->pdg_id();
335 edm::LogVerbatim(
"MuonMCClassifier") <<
"\t\t mother prod. vertex rho = " << momRho[
i] <<
", z = " << momZ[
i] <<
", grand-mom pdgId = " << gmomPdgId[
i];
339 nMom != 0 &&
abs(nMom->pdg_id()) >= 100;
341 int flav =
flavour(nMom->pdg_id());
342 if (hmomFlav[
i] < flav) hmomFlav[
i] = flav;
343 edm::LogVerbatim(
"MuonMCClassifier") <<
"\t\t backtracking flavour: mom pdgId = "<<nMom->pdg_id()<<
", flavour = " << flav <<
", heaviest so far = " << hmomFlav[
i];
349 momPdgId[
i] = simMom->pdgId();
350 momRho[
i] = simMom->vertex().Rho();
351 momZ[
i] = simMom->vertex().Z();
352 edm::LogVerbatim(
"MuonMCClassifier") <<
"\t Particle pdgId = "<<hitsPdgId[
i] <<
" produced at rho = " << prodRho[
i] <<
", z = " << prodZ[
i] <<
353 ", has SIM mother pdgId = " << momPdgId[
i] <<
" produced at rho = " << simMom->vertex().Rho() <<
", z = " << simMom->vertex().Z();
354 if (!simMom->genParticles().empty()) {
355 momStatus[
i] = simMom->genParticles()[0]->status();
356 #warning "This file has been modified just to get it to compile without any regard as to whether it still functions as intended"
357 gmomPdgId[
i] = simMom->pdgId();
358 edm::LogVerbatim(
"MuonMCClassifier") <<
"\t\t SIM mother is in GEN (status " << momStatus[
i] <<
"), grand-mom id = " << gmomPdgId[
i];
362 if (simGMom.
isNonnull()) gmomPdgId[
i] = simGMom->pdgId();
363 edm::LogVerbatim(
"MuonMCClassifier") <<
"\t\t SIM mother is in SIM only, grand-mom id = " << gmomPdgId[
i];
366 edm::LogVerbatim(
"MuonMCClassifier") <<
"\t Particle pdgId = "<<hitsPdgId[
i] <<
" produced at rho = " << prodRho[
i] <<
", z = " << prodZ[
i] <<
", has NO mother!";
370 gmomFlav[
i] =
flavour(gmomPdgId[i]);
373 if (
abs(tp->pdgId()) != 13) {
374 classif[
i] = isGhost ? -1 : 1;
375 ext[
i] = isGhost ? -1 : 1;
376 edm::LogVerbatim(
"MuonMCClassifier") <<
"\t This is not a muon. Sorry. classif[i] = " << classif[
i];
381 if (!tp->genParticles().empty() && (momPdgId[
i] != 0)) {
382 if (
abs(momPdgId[i]) < 100 && (
abs(momPdgId[i]) != 15)) {
383 classif[
i] = isGhost ? -4 : 4;
384 flav[
i] = (
abs(momPdgId[i]) == 15 ? 15 : 13);
385 edm::LogVerbatim(
"MuonMCClassifier") <<
"\t This seems PRIMARY MUON ! classif[i] = " << classif[
i];
387 }
else if (momFlav[i] == 4 || momFlav[i] == 5 || momFlav[i] == 15) {
388 classif[
i] = isGhost ? -3 : 3;
389 flav[
i] = momFlav[
i];
390 if (momFlav[i] == 15) ext[
i] = 9;
391 else if (momFlav[i] == 5) ext[
i] = 8;
392 else if (hmomFlav[i] == 5) ext[
i] = 7;
394 edm::LogVerbatim(
"MuonMCClassifier") <<
"\t This seems HEAVY FLAVOUR ! classif[i] = " << classif[
i];
396 classif[
i] = isGhost ? -2 : 2;
397 flav[
i] = momFlav[
i];
398 edm::LogVerbatim(
"MuonMCClassifier") <<
"\t This seems LIGHT FLAVOUR ! classif[i] = " << classif[
i];
401 classif[
i] = isGhost ? -2 : 2;
402 flav[
i] = momFlav[
i];
403 edm::LogVerbatim(
"MuonMCClassifier") <<
"\t This seems LIGHT FLAVOUR ! classif[i] = " << classif[
i];
406 if (momPdgId[i] == 0) ext[
i] = 2;
407 else if (
abs(momPdgId[i]) < 100) ext[
i] = (momFlav[
i] == 15 ? 9 : 10);
408 else if (momFlav[i] == 5) ext[
i] = 8;
409 else if (momFlav[i] == 4) ext[
i] = (hmomFlav[
i] == 5 ? 7 : 6);
410 else if (momStatus[i] != -1) {
411 int id =
abs(momPdgId[i]);
412 if (
id != 211 &&
id != 321 &&
id != 130 ) ext[
i] = 5;
416 if (isGhost) ext[
i] = -ext[
i];
420 if (!tp->genParticles().empty() &&
abs(ext[i]) >= 5) {
421 #warning "This file has been modified just to get it to compile without any regard as to whether it still functions as intended"
422 #ifdef REMOVED_JUST_TO_GET_IT_TO_COMPILE__THIS_CODE_NEEDS_TO_BE_CHECKED
423 muToPrimary[
i] =
fetch(genBarcodes, tp->genParticle()[0]->barcode());
427 int &indexPlus1 = tpToSecondaries[tp];
429 muToSecondary[
i] = indexPlus1 - 1;
432 edm::LogVerbatim(
"MuonMCClassifier") <<
"\t Extended classification code = " << ext[
i];
451 writeValueMap(iEvent, muons, tpAssoQuality,
"tpAssoQuality");
460 fillPri.insert(muons, muToPrimary.begin(), muToPrimary.end());
461 fillSec.
insert(muons, muToSecondary.begin(), muToSecondary.end());
462 fillPri.fill(); fillSec.
fill();
463 iEvent.
put(outPri,
"toPrimaries");
464 iEvent.
put(outSec,
"toSecondaries");
472 const std::vector<T> &
values,
481 iEvent.
put(valMap, label);
486 int flav =
abs(pdgId);
489 if (flav <= 37 && flav != 21)
return flav;
491 int bflav = ((flav / 1000) % 10);
492 if (bflav != 0)
return bflav;
494 int mflav = ((flav / 100) % 10);
495 if (mflav != 0)
return mflav;
501 std::vector<int>::const_iterator it =
std::find(genBarcodes->begin(), genBarcodes->end(), barcode);
502 return (it == genBarcodes->end()) ? -1 : (it - genBarcodes->begin());
511 const edm::Handle<std::vector<int> > & genBarcodes)
const {
513 if (simMom.
isNonnull() && !simMom->genParticles().empty()) {
514 #warning "This file has been modified just to get it to compile without any regard as to whether it still functions as intended"
515 #ifdef REMOVED_JUST_TO_GET_IT_TO_COMPILE__THIS_CODE_NEEDS_TO_BE_CHECKED
516 int momIdx =
fetch(genBarcodes, simMom->genParticle()[0]->barcode());
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
#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.
Point vertex() const
Parent vertex position.
void insert(const H &h, I begin, I end)
const_iterator end() const
bool isGlobalMuon() const
void find(edm::Handle< EcalRecHitCollection > &hits, DetId thisDet, std::vector< EcalRecHitCollection::const_iterator > &hit, bool debug=false)
int status() const
Status word.
edm::InputTag trackingParticles_
The TrackingParticle objects.
bool isNonnull() const
Checks for non-null.
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.
int fetch(const edm::Handle< std::vector< int > > &genBarcodes, int barcode) const
Find the index of a genParticle given it's barcode. -1 if not found.
int convertAndPush(const TrackingParticle &tp, reco::GenParticleCollection &out, const TrackingParticleRef &momRef, const edm::Handle< reco::GenParticleCollection > &genParticles, const edm::Handle< std::vector< int > > &genBarcodes) const
TrackingParticleRef getTpMother(TrackingParticleRef tp)
MuonMCClassifier(const edm::ParameterSet &)
std::string associatorLabel_
The Associations.
bool getByLabel(InputTag const &tag, Handle< PROD > &result) const
MuonAssociatorByHits::MuonTrackType trackType_
Track to use.
const LorentzVector & p4() const
Four-momentum Lorentz vector. Note this is taken from the first SimTrack only.
key_type key() const
Accessor for product key.
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.
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.
int charge() const
Electric charge. Note this is taken from the first SimTrack only.
std::pair< typename Association::data_type::first_type, double > match(Reference key, Association association, bool bestMatchByMaxValue)
Generic matching function.
edm::InputTag muons_
The RECO objects.
T const * get() const
Returns C++ pointer to the item.
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
const HepMC::GenParticle * getGpMother(const HepMC::GenParticle *gp)
StringCutObjectSelector< pat::Muon > muonCut_