65 #include <boost/foreach.hpp> 66 #define foreach BOOST_FOREACH 110 const std::vector<T> &
values,
114 if (tp.
isNonnull() && tp->parentVertex().
isNonnull() && !tp->parentVertex()->sourceTracks().empty()) {
115 return tp->parentVertex()->sourceTracks()[0];
137 decayRho_(iConfig.getParameter<double>(
"decayRho")),
138 decayAbsZ_(iConfig.getParameter<double>(
"decayAbsZ")),
149 else throw cms::Exception(
"Configuration") <<
"Track type '" << trackType <<
"' not supported.\n";
154 produces<edm::ValueMap<int> >();
155 produces<edm::ValueMap<int> >(
"ext");
156 produces<edm::ValueMap<int> >(
"flav");
157 produces<edm::ValueMap<int> >(
"hitsPdgId");
158 produces<edm::ValueMap<int> >(
"G4processType");
159 produces<edm::ValueMap<int> >(
"momPdgId");
160 produces<edm::ValueMap<int> >(
"momFlav");
161 produces<edm::ValueMap<int> >(
"momStatus");
162 produces<edm::ValueMap<int> >(
"gmomPdgId");
163 produces<edm::ValueMap<int> >(
"gmomFlav");
164 produces<edm::ValueMap<int> >(
"hmomFlav");
165 produces<edm::ValueMap<int> >(
"tpId");
166 produces<edm::ValueMap<int> >(
"tpEv");
167 produces<edm::ValueMap<int> >(
"tpBx");
168 produces<edm::ValueMap<float> >(
"signp");
169 produces<edm::ValueMap<float> >(
"pt");
170 produces<edm::ValueMap<float> >(
"eta");
171 produces<edm::ValueMap<float> >(
"phi");
172 produces<edm::ValueMap<float> >(
"prodRho");
173 produces<edm::ValueMap<float> >(
"prodZ");
174 produces<edm::ValueMap<float> >(
"momRho");
175 produces<edm::ValueMap<float> >(
"momZ");
176 produces<edm::ValueMap<float> >(
"tpAssoQuality");
178 produces<reco::GenParticleCollection>(
"secondaries");
179 produces<edm::Association<reco::GenParticleCollection> >(
"toPrimaries");
180 produces<edm::Association<reco::GenParticleCollection> >(
"toSecondaries");
208 edm::LogVerbatim(
"MuonMCClassifier") <<
"\n ***************************************************************** ";
210 edm::LogVerbatim(
"MuonMCClassifier") <<
" ***************************************************************** \n";
215 for (
size_t i = 0,
n = muons->size();
i <
n; ++
i) {
222 for (
size_t i = 0,
n = muons->size();
i <
n; ++
i) {
229 for (
size_t i = 0,
n = trackingParticles->size();
i <
n; ++
i) {
239 edm::LogVerbatim(
"MuonMCClassifier") <<
"\n ***************************************************************** ";
240 edm::LogVerbatim(
"MuonMCClassifier") <<
" STANDALONE (UpdAtVtx) MUON association ";
241 edm::LogVerbatim(
"MuonMCClassifier") <<
" ***************************************************************** \n";
246 typedef reco::MuonToSimCollection::const_iterator r2s_it;
247 typedef reco::SimToMuonCollection::const_iterator s2r_it;
249 size_t nmu = muons->size();
250 edm::LogVerbatim(
"MuonMCClassifier") <<
"\n There are "<<nmu<<
" reco::Muons.";
252 std::vector<int> classif(nmu, 0),
ext(nmu, 0);
253 std::vector<int> hitsPdgId(nmu, 0), G4processType(nmu, 0), momPdgId(nmu, 0), gmomPdgId(nmu, 0), momStatus(nmu, 0);
254 std::vector<int> flav(nmu, 0), momFlav(nmu, 0), gmomFlav(nmu, 0), hmomFlav(nmu, 0);
255 std::vector<int> tpId(nmu, -1), tpBx(nmu, 999), tpEv(nmu, 999);
256 std::vector<float> signp(nmu, 0.0),
pt(nmu, 0.0),
eta(nmu, -99.),
phi(nmu, -99.);
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::unique_ptr<reco::GenParticleCollection> secondaries;
261 std::map<TrackingParticleRef, int> tpToSecondaries;
262 std::vector<int> muToPrimary(nmu, -1), muToSecondary(nmu, -1);
266 for(
size_t i = 0;
i < nmu; ++
i) {
269 LogTrace(
"MuonMCClassifier") <<
"\n reco::Muon # "<<
i<<
"didn't pass the selection. classified as -99 and skipped";
270 classif[
i] = -99;
continue;
276 r2s_it
match = recSimColl.find(mu);
278 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 tp = matchSta->second.front().first;
296 tpAssoQuality[
i] = matchSta->second.front().second;
297 s2r_it matchback = UpdSTA_simRecColl.find(tp);
298 if (matchback != UpdSTA_simRecColl.end()) {
299 muMatchBack = matchback->second.front().first;
301 edm::LogWarning(
"MuonMCClassifier") <<
"\n***WARNING: This I do NOT understand: why no match back in UpdSTA? *** \n";
305 edm::LogVerbatim(
"MuonMCClassifier") <<
"\t No matching TrackingParticle is found ";
309 bool isGhost = muMatchBack !=
mu;
310 if (isGhost)
edm::LogVerbatim(
"MuonMCClassifier") <<
"\t *** This seems a Duplicate muon ! classif[i] will be < 0 ***";
313 tpBx[
i] = tp->eventId().bunchCrossing();
314 tpEv[
i] = tp->eventId().event();
316 hitsPdgId[
i] = tp->pdgId();
317 prodRho[
i] = tp->vertex().Rho();
318 prodZ[
i] = tp->vertex().Z();
321 const std::vector<SimVertex> & G4Vs = tp->parentVertex()->g4Vertices();
322 G4processType[
i] = G4Vs[0].processType();
324 signp[
i] = tp->charge() * tp->p();
331 if (!tp->genParticles().empty()) {
337 if (genMom->pdgId() != tp->pdgId()) {
338 momPdgId[
i] = genMom->pdgId();
339 momStatus[
i] = genMom->status();
340 momRho[
i] = genMom->vertex().Rho();
341 momZ[
i] = genMom->vz();
346 while (mMom->pdgId() == tp->pdgId()) {
349 if (mMom->numberOfMothers() > 0) {
350 mMom = mMom->motherRef();
353 <<
"\t\t backtracking mother "<<jm<<
", pdgId = "<<mMom->pdgId()<<
", status= " <<mMom->status();
356 momPdgId[
i] = genMom->pdgId();
357 momStatus[
i] = genMom->status();
358 momRho[
i] = genMom->vertex().Rho();
359 momZ[
i] = genMom->vz();
362 <<
"\t Particle pdgId = "<<hitsPdgId[
i] <<
", (Event,Bx) = "<<
"(" <<tpEv[
i]<<
","<<tpBx[
i]<<
")" 363 <<
"\n\t q*p = "<<signp[
i]<<
", pT = "<<
pt[
i]<<
", eta = "<<
eta[
i]<<
", phi = "<<phi[
i]
364 <<
"\n\t produced at vertex rho = " << prodRho[
i] <<
", z = " << prodZ[
i]
365 <<
", (GEANT4 process = "<< G4processType[
i]<<
")" 366 <<
"\n\t has GEN mother pdgId = " << momPdgId[
i] <<
" (status = "<<momStatus[
i] <<
")";
371 gmomPdgId[
i] = genGMom->pdgId();
372 edm::LogVerbatim(
"MuonMCClassifier") <<
"\t\t mother prod. vertex rho = " << momRho[
i] <<
", z = " << momZ[
i]
373 <<
", grand-mom pdgId = " << gmomPdgId[
i];
379 int flav =
flavour(nMom->pdgId());
380 if (hmomFlav[
i] < flav) hmomFlav[
i] = flav;
382 <<
"\t\t backtracking flavour: mom pdgId = "<<nMom->pdgId()
383 <<
", flavour = " << flav <<
", heaviest so far = " << hmomFlav[
i];
389 <<
"\t GenParticle with pdgId = "<<hitsPdgId[
i] <<
", (Event,Bx) = "<<
"(" <<tpEv[
i]<<
","<<tpBx[
i]<<
")" 390 <<
"\n\t q*p = "<<signp[
i]<<
", pT = "<<
pt[
i]<<
", eta = "<<
eta[
i]<<
", phi = "<<phi[
i]
391 <<
"\n\t produced at vertex rho = " << prodRho[
i] <<
", z = " << prodZ[
i]
392 <<
", (GEANT4 process = "<< G4processType[
i]<<
")" 393 <<
"\n\t has NO mother!";
399 momPdgId[
i] = simMom->pdgId();
400 momRho[
i] = simMom->vertex().Rho();
401 momZ[
i] = simMom->vertex().Z();
403 <<
"\t Particle pdgId = "<<hitsPdgId[
i] <<
", (Event,Bx) = "<<
"(" <<tpEv[
i]<<
","<<tpBx[
i]<<
")" 404 <<
"\n\t q*p = "<<signp[
i]<<
", pT = "<<
pt[
i]<<
", eta = "<<
eta[
i]<<
", phi = "<<phi[
i]
405 <<
"\n\t produced at vertex rho = " << prodRho[
i] <<
", z = " << prodZ[
i]
406 <<
", (GEANT4 process = "<< G4processType[
i]<<
")" 407 <<
"\n\t has SIM mother pdgId = " << momPdgId[
i]
408 <<
" produced at rho = " << simMom->vertex().Rho() <<
", z = " << simMom->vertex().Z();
410 if (!simMom->genParticles().empty()) {
411 momStatus[
i] = simMom->genParticles()[0]->status();
413 if (genGMom.
isNonnull()) gmomPdgId[
i] = genGMom->pdgId();
415 <<
"\t\t SIM mother is in GEN (status " << momStatus[
i] <<
"), grand-mom id = " << gmomPdgId[
i];
420 if (simGMom.
isNonnull()) gmomPdgId[
i] = simGMom->pdgId();
421 edm::LogVerbatim(
"MuonMCClassifier") <<
"\t\t SIM mother is in SIM only, grand-mom id = " << gmomPdgId[
i];
425 <<
"\t Particle pdgId = "<<hitsPdgId[
i] <<
", (Event,Bx) = "<<
"(" <<tpEv[
i]<<
","<<tpBx[
i]<<
")" 426 <<
"\n\t q*p = "<<signp[
i]<<
", pT = "<<
pt[
i]<<
", eta = "<<
eta[
i]<<
", phi = "<<phi[
i]
427 <<
"\n\t produced at vertex rho = " << prodRho[
i] <<
", z = " << prodZ[
i]
428 <<
", (GEANT4 process = "<< G4processType[
i]<<
")" 429 <<
"\n\t has NO mother!";
433 gmomFlav[
i] =
flavour(gmomPdgId[i]);
436 if (
abs(tp->pdgId()) != 13) {
437 if (
abs(tp->pdgId()) == 11) {
438 classif[
i] = isGhost ? -11 : 11;
439 ext[
i] = isGhost ? -11 : 11;
440 edm::LogVerbatim(
"MuonMCClassifier") <<
"\t This is electron/positron. classif[i] = " << classif[
i];
443 classif[
i] = isGhost ? -1 : 1;
444 ext[
i] = isGhost ? -1 : 1;
445 edm::LogVerbatim(
"MuonMCClassifier") <<
"\t This is not a muon. Sorry. classif[i] = " << classif[
i];
452 if (!tp->genParticles().empty() && (momPdgId[
i] != 0)) {
453 if (
abs(momPdgId[i]) < 100 && (
abs(momPdgId[i]) != 15)) {
454 classif[
i] = isGhost ? -4 : 4;
457 edm::LogVerbatim(
"MuonMCClassifier") <<
"\t This seems PRIMARY MUON ! classif[i] = " << classif[
i];
458 }
else if (momFlav[i] == 4 || momFlav[i] == 5 || momFlav[i] == 15) {
459 classif[
i] = isGhost ? -3 : 3;
460 flav[
i] = momFlav[
i];
461 if (momFlav[i] == 15) ext[
i] = 9;
462 else if (momFlav[i] == 5) ext[
i] = 8;
463 else if (hmomFlav[i] == 5) ext[
i] = 7;
465 edm::LogVerbatim(
"MuonMCClassifier") <<
"\t This seems HEAVY FLAVOUR ! classif[i] = " << classif[
i];
467 classif[
i] = isGhost ? -2 : 2;
468 flav[
i] = momFlav[
i];
469 edm::LogVerbatim(
"MuonMCClassifier") <<
"\t This seems LIGHT FLAVOUR ! classif[i] = " << classif[
i];
472 classif[
i] = isGhost ? -2 : 2;
473 flav[
i] = momFlav[
i];
474 edm::LogVerbatim(
"MuonMCClassifier") <<
"\t This seems LIGHT FLAVOUR ! classif[i] = " << classif[
i];
477 if (momPdgId[i] == 0) ext[
i] = 2;
478 else if (
abs(momPdgId[i]) < 100) ext[
i] = (momFlav[
i] == 15 ? 9 : 10);
479 else if (momFlav[i] == 5) ext[
i] = 8;
480 else if (momFlav[i] == 4) ext[
i] = (hmomFlav[
i] == 5 ? 7 : 6);
481 else if (momStatus[i] != -1) {
483 if (
id != 211 &&
id != 321 &&
id != 130 ) ext[
i] = 5;
487 if (isGhost) ext[
i] = -ext[
i];
491 if (!tp->genParticles().empty() &&
std::abs(ext[i]) >= 5) {
492 if (genParticles.
id() != tp->genParticles().
id()) {
493 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";
495 muToPrimary[
i] = tp->genParticles()[0].
key();
498 int &indexPlus1 = tpToSecondaries[tp];
500 muToSecondary[
i] = indexPlus1 - 1;
503 edm::LogVerbatim(
"MuonMCClassifier") <<
"\t Extended classification code = " << ext[
i];
514 writeValueMap(iEvent, muons, G4processType,
"G4processType");
529 writeValueMap(iEvent, muons, tpAssoQuality,
"tpAssoQuality");
538 fillPri.insert(muons, muToPrimary.begin(), muToPrimary.end());
539 fillSec.
insert(muons, muToSecondary.begin(), muToSecondary.end());
540 fillPri.fill(); fillSec.
fill();
551 const std::vector<T> &
values,
556 unique_ptr<ValueMap<T> > valMap(
new ValueMap<T>());
568 if (flav <= 37 && flav != 21)
return flav;
570 int bflav = ((flav / 1000) % 10);
571 if (bflav != 0)
return bflav;
573 int mflav = ((flav / 100) % 10);
574 if (mflav != 0)
return mflav;
585 if (simMom.
isNonnull() && !simMom->genParticles().empty()) {
586 if (genParticles.
id() != simMom->genParticles().
id()) {
587 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";
589 out.back().addMother(simMom->genParticles()[0]);
edm::InputTag genParticles_
bool linkToGenParticles_
Create a link to the generator level particles.
const LorentzVector & p4() const
Four-momentum Lorentz vector. Note this is taken from the first SimTrack only.
std::vector< GenParticle > GenParticleCollection
collection of GenParticles
T getParameter(std::string const &) const
std::map< edm::RefToBase< reco::Muon >, std::vector< std::pair< TrackingParticleRef, double > >, RefToBaseSort > MuonToSimCollection
OrphanHandle< PROD > put(std::unique_ptr< PROD > product)
Put a new product.
bool isNonnull() const
Checks for non-null.
std::vector< TrackingParticle > TrackingParticleCollection
bool getByToken(EDGetToken token, Handle< PROD > &result) const
#define DEFINE_FWK_MODULE(type)
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
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.
EDGetTokenT< ProductType > consumes(edm::InputTag const &tag)
reco::MuonTrackType trackType_
Track to use.
std::map< TrackingParticleRef, std::vector< std::pair< edm::RefToBase< reco::Muon >, double > > > SimToMuonCollection
~MuonMCClassifier() override
float charge() const
Electric charge. Note this is taken from the first SimTrack only.
bool isGlobalMuon() const override
void associateMuons(MuonToSimCollection &recoToSim, SimToMuonCollection &simToReco, const edm::RefToBaseVector< reco::Muon > &muons, MuonTrackType type, const edm::RefVector< TrackingParticleCollection > &tpColl) const
Abs< T >::type abs(const T &t)
TrackingParticleRef getTpMother(TrackingParticleRef tp)
MuonMCClassifier(const edm::ParameterSet &)
edm::InputTag associatorLabel_
The Associations.
edm::EDGetTokenT< reco::GenParticleCollection > genParticlesToken_
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.
edm::EDGetTokenT< reco::MuonToTrackingParticleAssociator > muAssocToken_
const_iterator begin() const
Point vertex() const
Parent vertex position.
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.
void produce(edm::Event &, const edm::EventSetup &) override
Analysis-level muon class.
edm::Ref< TrackingParticleCollection > TrackingParticleRef
edm::EDGetTokenT< TrackingParticleCollection > trackingParticlesToken_
The TrackingParticle objects.
StringCutObjectSelector< pat::Muon > muonCut_