102 template <
typename T>
105 const std::vector<T> &
values,
109 if (tp.
isNonnull() && tp->parentVertex().
isNonnull() && !tp->parentVertex()->sourceTracks().empty()) {
110 return tp->parentVertex()->sourceTracks()[0];
130 consumes<
reco::MuonToTrackingParticleAssociator>(iConfig.getParameter<
edm::InputTag>(
"associatorLabel"))),
131 decayRho_(iConfig.getParameter<double>(
"decayRho")),
132 decayAbsZ_(iConfig.getParameter<double>(
"decayAbsZ")),
138 if (trackType ==
"inner")
140 else if (trackType ==
"outer")
142 else if (trackType ==
"global")
144 else if (trackType ==
"segments")
146 else if (trackType ==
"glb_or_trk")
149 throw cms::Exception(
"Configuration") <<
"Track type '" << trackType <<
"' not supported.\n";
154 produces<edm::ValueMap<reco::MuonSimInfo>>();
156 produces<reco::GenParticleCollection>(
"secondaries");
157 produces<edm::Association<reco::GenParticleCollection>>(
"toPrimaries");
158 produces<edm::Association<reco::GenParticleCollection>>(
"toSecondaries");
166 LogTrace(
"MuonSimClassifier") <<
"\t Particle pdgId = " << simInfo.
pdgId <<
", (Event,Bx) = " 167 <<
"(" << simInfo.
tpEvent <<
"," << simInfo.
tpBX <<
")" 168 <<
"\n\t q*p = " << simInfo.
charge * simInfo.
p4.P() <<
", pT = " << simInfo.
p4.pt()
169 <<
", eta = " << simInfo.
p4.eta() <<
", phi = " << simInfo.
p4.phi()
170 <<
"\n\t produced at vertex rho = " << simInfo.
vertex.Rho()
193 LogTrace(
"MuonSimClassifier") <<
"\n " 194 "***************************************************************** ";
196 LogTrace(
"MuonSimClassifier") <<
" ******************************************" 197 "*********************** \n";
200 for (
size_t i = 0,
n = muons->size();
i <
n; ++
i) {
205 for (
size_t i = 0, n = trackingParticles->size();
i <
n; ++
i) {
216 LogTrace(
"MuonSimClassifier") <<
"\n " 217 "***************************************************************** ";
218 LogTrace(
"MuonSimClassifier") <<
" STANDALONE (UpdAtVtx) MUON association ";
219 LogTrace(
"MuonSimClassifier") <<
" ****************************************" 220 "************************* \n";
224 typedef reco::MuonToSimCollection::const_iterator r2s_it;
225 typedef reco::SimToMuonCollection::const_iterator s2r_it;
227 size_t nmu = muons->size();
228 LogTrace(
"MuonSimClassifier") <<
"\n There are " << nmu <<
" reco::Muons.";
230 std::vector<reco::MuonSimInfo> simInfo;
232 std::unique_ptr<reco::GenParticleCollection> secondaries;
233 std::map<TrackingParticleRef, int> tpToSecondaries;
234 std::vector<int> muToPrimary(nmu, -1), muToSecondary(nmu,
240 for (
size_t i = 0;
i < nmu; ++
i) {
242 LogTrace(
"MuonSimClassifier") <<
"\n reco::Muon # " <<
i;
246 r2s_it
match = recSimColl.find(allMuons.
at(
i));
248 if (match != recSimColl.end()) {
251 tp = match->second.front().first;
253 simInfo[
i].tpAssoQuality = match->second.front().second;
254 s2r_it matchback = simRecColl.find(tp);
255 if (matchback != simRecColl.end()) {
256 muMatchBack = matchback->second.front().first;
258 LogTrace(
"MuonSimClassifier") <<
"\n***WARNING: This I do NOT understand: why no match back? " 264 r2s_it matchSta = updSTA_recSimColl.find(allMuons.
at(
i));
265 if (matchSta != updSTA_recSimColl.end()) {
266 tp = matchSta->second.front().first;
269 simInfo[
i].tpAssoQuality = matchSta->second.front().second;
270 s2r_it matchback = updSTA_simRecColl.find(tp);
271 if (matchback != updSTA_simRecColl.end()) {
272 muMatchBack = matchback->second.front().first;
274 LogTrace(
"MuonSimClassifier") <<
"\n***WARNING: This I do NOT understand: why no match back " 279 LogTrace(
"MuonSimClassifier") <<
"\t No matching TrackingParticle is found ";
284 bool isGhost = muMatchBack != allMuons.
at(
i);
286 LogTrace(
"MuonSimClassifier") <<
"\t *** This seems a Duplicate muon ! " 287 "classif[i] will be < 0 ***";
290 simInfo[
i].tpBX = tp->eventId().bunchCrossing();
291 simInfo[
i].tpEvent = tp->eventId().event();
293 simInfo[
i].pdgId = tp->pdgId();
294 simInfo[
i].vertex = tp->vertex();
297 const std::vector<SimVertex> &g4Vs = tp->parentVertex()->g4Vertices();
298 simInfo[
i].g4processType = g4Vs[0].processType();
300 simInfo[
i].charge = tp->charge();
301 simInfo[
i].p4 = tp->p4();
305 if (!tp->genParticles().empty()) {
311 if (genMom->pdgId() != tp->pdgId()) {
312 simInfo[
i].motherPdgId = genMom->pdgId();
313 simInfo[
i].motherStatus = genMom->status();
314 simInfo[
i].motherVertex = genMom->vertex();
319 while (mMom->pdgId() == tp->pdgId()) {
321 if (mMom->numberOfMothers() > 0) {
322 mMom = mMom->motherRef();
324 LogTrace(
"MuonSimClassifier") <<
"\t No Mother is found ";
328 LogTrace(
"MuonSimClassifier") <<
"\t\t backtracking mother " << jm <<
", pdgId = " << mMom->pdgId()
329 <<
", status= " << mMom->status();
332 simInfo[
i].motherPdgId = genMom->pdgId();
333 simInfo[
i].motherStatus = genMom->status();
334 simInfo[
i].motherVertex = genMom->vertex();
337 LogTrace(
"MuonSimClassifier") <<
"\t has GEN mother pdgId = " << simInfo[
i].motherPdgId
338 <<
" (status = " << simInfo[
i].motherStatus <<
")";
343 simInfo[
i].grandMotherPdgId = genGMom->pdgId();
345 <<
"\t\t mother prod. vertex rho = " << simInfo[
i].motherVertex.Rho()
346 <<
", z = " << simInfo[
i].motherVertex.Z() <<
", grand-mom pdgId = " << simInfo[
i].grandMotherPdgId;
353 int flav =
flavour(nMom->pdgId());
354 if (simInfo[i].heaviestMotherFlavour < flav)
355 simInfo[
i].heaviestMotherFlavour = flav;
357 <<
"\t\t backtracking flavour: mom pdgId = " << nMom->pdgId() <<
", flavour = " << flav
358 <<
", heaviest so far = " << simInfo[
i].heaviestMotherFlavour;
362 LogTrace(
"MuonSimClassifier") <<
"\t has NO mother!";
367 simInfo[
i].motherPdgId = simMom->pdgId();
368 simInfo[
i].motherVertex = simMom->vertex();
370 LogTrace(
"MuonSimClassifier") <<
"\t has SIM mother pdgId = " << simInfo[
i].motherPdgId
371 <<
" produced at rho = " << simMom->vertex().Rho()
372 <<
", z = " << simMom->vertex().Z();
374 if (!simMom->genParticles().empty()) {
375 simInfo[
i].motherStatus = simMom->genParticles()[0]->status();
377 (simMom->genParticles()[0]->numberOfMothers() > 0 ? simMom->genParticles()[0]->motherRef()
380 simInfo[i].grandMotherPdgId = genGMom->pdgId();
381 LogTrace(
"MuonSimClassifier") <<
"\t\t SIM mother is in GEN (status " << simInfo[
i].motherStatus
382 <<
"), grand-mom id = " << simInfo[
i].grandMotherPdgId;
384 simInfo[
i].motherStatus = -1;
387 simInfo[i].grandMotherPdgId = simGMom->pdgId();
389 <<
"\t\t SIM mother is in SIM only, grand-mom id = " << simInfo[
i].grandMotherPdgId;
393 LogTrace(
"MuonSimClassifier") <<
"\t has NO mother!";
396 simInfo[
i].motherFlavour =
flavour(simInfo[
i].motherPdgId);
397 simInfo[
i].grandMotherFlavour =
flavour(simInfo[
i].grandMotherPdgId);
403 simInfo[
i].extendedClass =
405 LogTrace(
"MuonSimClassifier") <<
"\t This is electron/positron. classif[i] = " << simInfo[
i].primaryClass;
407 simInfo[
i].primaryClass =
411 LogTrace(
"MuonSimClassifier") <<
"\t This is not a muon. Sorry. classif[i] = " << simInfo[
i].primaryClass;
417 if (!tp->genParticles().empty() && (simInfo[
i].motherPdgId != 0)) {
418 if (
std::abs(simInfo[
i].motherPdgId) < 100 && (
std::abs(simInfo[
i].motherPdgId) != 15)) {
419 simInfo[
i].primaryClass =
421 simInfo[
i].flavour = 13;
424 LogTrace(
"MuonSimClassifier") <<
"\t This seems PRIMARY MUON ! classif[i] = " << simInfo[
i].primaryClass;
425 }
else if (simInfo[
i].motherFlavour == 4 || simInfo[
i].motherFlavour == 5 || simInfo[
i].motherFlavour == 15) {
426 simInfo[
i].primaryClass =
428 simInfo[
i].flavour = simInfo[
i].motherFlavour;
429 if (simInfo[
i].motherFlavour == 15)
430 simInfo[
i].extendedClass =
432 else if (simInfo[
i].motherFlavour == 5)
433 simInfo[
i].extendedClass =
435 else if (simInfo[
i].heaviestMotherFlavour == 5)
436 simInfo[
i].extendedClass =
439 simInfo[
i].extendedClass =
441 LogTrace(
"MuonSimClassifier") <<
"\t This seems HEAVY FLAVOUR ! classif[i] = " << simInfo[
i].primaryClass;
443 simInfo[
i].primaryClass =
445 simInfo[
i].flavour = simInfo[
i].motherFlavour;
446 LogTrace(
"MuonSimClassifier") <<
"\t This seems LIGHT FLAVOUR ! classif[i] = " << simInfo[
i].primaryClass;
449 simInfo[
i].primaryClass =
451 simInfo[
i].flavour = simInfo[
i].motherFlavour;
452 LogTrace(
"MuonSimClassifier") <<
"\t This seems LIGHT FLAVOUR ! classif[i] = " << simInfo[
i].primaryClass;
457 if (simInfo[
i].motherPdgId == 0)
461 else if (
std::abs(simInfo[
i].motherPdgId) < 100) {
462 if (simInfo[
i].motherFlavour == 15)
463 simInfo[
i].extendedClass =
468 }
else if (simInfo[
i].motherFlavour == 5)
469 simInfo[
i].extendedClass =
471 else if (simInfo[
i].motherFlavour == 4) {
472 if (simInfo[
i].heaviestMotherFlavour == 5)
473 simInfo[
i].extendedClass =
476 simInfo[
i].extendedClass =
478 }
else if (simInfo[
i].motherStatus != -1) {
479 int id =
std::abs(simInfo[
i].motherPdgId);
480 if (
id != 211 &&
id != 321 &&
id != 130 )
500 if (!tp->genParticles().empty() &&
std::abs(simInfo[
i].extendedClass) >= 5) {
501 if (genParticles.
id() != tp->genParticles().
id()) {
503 <<
"Product ID mismatch between the genParticle collection (" <<
genParticles_ <<
", id " 504 << genParticles.
id() <<
") and the references in the TrackingParticles (id " << tp->genParticles().
id()
507 muToPrimary[
i] = tp->genParticles()[0].
key();
510 int &indexPlus1 = tpToSecondaries[tp];
514 muToSecondary[
i] = indexPlus1 - 1;
517 LogTrace(
"MuonSimClassifier") <<
"\t Extended classification code = " << simInfo[
i].extendedClass;
530 std::unique_ptr<edm::Association<reco::GenParticleCollection>> outPri(
532 std::unique_ptr<edm::Association<reco::GenParticleCollection>> outSec(
535 fillPri.insert(muons, muToPrimary.begin(), muToPrimary.end());
536 fillSec.
insert(muons, muToSecondary.begin(), muToSecondary.end());
544 template <
typename T>
547 const std::vector<T> &
values,
562 if (flav <= 37 && flav != 21)
565 int bflav = ((flav / 1000) % 10);
569 int mflav = ((flav / 100) % 10);
582 if (simMom.
isNonnull() && !simMom->genParticles().empty()) {
583 if (genParticles.
id() != simMom->genParticles().
id()) {
585 <<
"Product ID mismatch between the genParticle collection (" <<
genParticles_ <<
", id " << genParticles.
id()
586 <<
") and the references in the TrackingParticles (id " << simMom->genParticles().
id() <<
")\n";
588 out.back().addMother(simMom->genParticles()[0]);
590 return out.size() - 1;
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
TrackingParticleRef getTpMother(TrackingParticleRef tp)
edm::EDGetTokenT< edm::View< reco::Muon > > muonsToken_
The RECO objects.
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.
void produce(edm::Event &, const edm::EventSetup &) override
int pdgId() const
PDG ID.
reco::MuonTrackType trackType_
Track to use.
edm::Ref< GenParticleCollection > GenParticleRef
persistent reference to a GenParticle
void insert(const H &h, I begin, I end)
int flavour(int pdgId) const
Returns the flavour given a pdg id code.
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.
value_type at(size_type idx) const
int status() const
Status word.
ProductID id() const
Accessor for product ID.
std::map< TrackingParticleRef, std::vector< std::pair< edm::RefToBase< reco::Muon >, double > > > SimToMuonCollection
float charge() const
Electric charge. Note this is taken from the first SimTrack only.
edm::InputTag genParticles_
#define DEFINE_FWK_MODULE(type)
void associateMuons(MuonToSimCollection &recoToSim, SimToMuonCollection &simToReco, const edm::RefToBaseVector< reco::Muon > &muons, MuonTrackType type, const edm::RefVector< TrackingParticleCollection > &tpColl) const
MuonSimClassifier(const edm::ParameterSet &)
Abs< T >::type abs(const T &t)
edm::EDGetTokenT< reco::GenParticleCollection > genParticlesToken_
edm::EDGetTokenT< reco::MuonToTrackingParticleAssociator > muAssocToken_
T const * product() const
edm::EDGetTokenT< TrackingParticleCollection > trackingParticlesToken_
The TrackingParticle objects.
void dumpFormatedInfo(const reco::MuonSimInfo &simInfo)
edm::InputTag associatorLabel_
The Associations.
Point vertex() const
Parent vertex position.
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.
std::pair< typename Association::data_type::first_type, double > match(Reference key, Association association, bool bestMatchByMaxValue)
Generic matching function.
bool linkToGenParticles_
Create a link to the generator level particles.
edm::Ref< TrackingParticleCollection > TrackingParticleRef
~MuonSimClassifier() override