27 declareInterface<IMuonTrackTruthTool>(
this);
46 return StatusCode::SUCCESS;
61 return operator()(r1.second, r2.second);
66 if (
t1.truthTrack && !
t2.truthTrack)
return true;
67 if (!
t1.truthTrack &&
t2.truthTrack)
return false;
68 if (!
t1.truthTrack && !
t2.truthTrack)
return false;
69 if (
t1.truthTrack->barcode() ==
t2.truthTrack->barcode())
return t1.numberOfMatchedHits() >
t2.numberOfMatchedHits();
70 return t1.truthTrack->barcode() <
t2.truthTrack->barcode();
80 for (;
tit != tit_end; ++
tit) {
82 if (!
match.truthTrack)
continue;
84 if (
match.numberOfMatchedHits() == 0)
continue;
97 const std::vector<const MuonSegment*>& segments)
const {
99 result.reserve(segments.size());
102 std::vector<const MuonSegment*>::const_iterator sit = segments.begin();
103 std::vector<const MuonSegment*>::const_iterator sit_end = segments.end();
104 for (; sit != sit_end; ++sit) {
117 const std::vector<const MuonSimDataCollection*>& muonSimData,
119 std::map<int, int> barcode_map;
121 if (truthTrackCol->
empty()) {
126 const HepMC::GenEvent* genEvent =
nullptr;
127 if (!mcEventCollection->
empty()) {
129 if (mcEventCollection->
size() == 1) genEvent = mcEventCollection->
front();
136 for (; tr_it != tr_it_end; ++tr_it) {
137 int PDGCode((*tr_it).GetPDGCode());
138 int barcode = (*tr_it).barcode();
145 if (barcode_map.count(
barcode)) {
151 std::unique_ptr<TruthTrajectory> truthTrajectory;
156 truthTrajectory = std::make_unique<TruthTrajectory>();
158 if (!truthTrajectory->empty()) {
160 barcode = truthTrajectory->front().barcode();
163 auto particle = truthTrajectory->front().cptr();
165 if (
particle->production_vertex()) {
167 <<
particle->production_vertex()->position().z());
172 std::vector<HepMcParticleLink>::const_iterator pit = truthTrajectory->begin();
173 std::vector<HepMcParticleLink>::const_iterator pit_end = truthTrajectory->end();
174 for (; pit != pit_end; ++pit) {
175 int code = (*pit).barcode();
182 <<
particle->production_vertex()->position().z());
205 entry.truthTrack = &(*tr_it);
207 entry.truthTrajectory = std::move(truthTrajectory);
215 unsigned int ngood(0);
216 std::vector<int> badBarcodes;
221 unsigned int nhits =
it->second.mdtHits.size() +
it->second.rpcHits.size() +
it->second.tgcHits.size() +
222 it->second.cscHits.size() +
it->second.stgcHits.size() +
it->second.mmHits.size();
223 if (!
it->second.truthTrack) erase =
true;
227 ATH_MSG_VERBOSE(
" Erasing entry: barcode " <<
it->second.truthTrack->barcode() <<
" manip "
229 badBarcodes.push_back(
it->first);
232 ATH_MSG_VERBOSE(
" Keeping entry: barcode " <<
it->second.truthTrack->barcode() <<
" manip "
239 for (; badIt != badIt_end; ++badIt)
truth_tree.erase(*badIt);
249 for (;
it != it_end; ++
it) {
250 if (!
it->second.truthTrack)
253 ATH_MSG_INFO(
" PDG " <<
it->second.truthTrack->GetPDGCode() <<
" barcode " <<
it->second.truthTrack->barcode()
256 if (!
it->second.mdtHits.empty())
ATH_MSG_INFO(
" mdt " <<
it->second.mdtHits.size());
257 if (!
it->second.rpcHits.empty())
ATH_MSG_INFO(
" rpc " <<
it->second.rpcHits.size());
258 if (!
it->second.tgcHits.empty())
ATH_MSG_INFO(
" tgc " <<
it->second.tgcHits.size());
259 if (!
it->second.cscHits.empty())
ATH_MSG_INFO(
" csc " <<
it->second.cscHits.size());
260 if (!
it->second.stgcHits.empty())
ATH_MSG_INFO(
" stgc " <<
it->second.stgcHits.size());
261 if (!
it->second.mmHits.empty())
ATH_MSG_INFO(
" mm " <<
it->second.mmHits.size());
262 if (
it->second.mdtHits.empty() &&
it->second.rpcHits.empty() &&
it->second.tgcHits.empty() &&
it->second.cscHits.empty() &&
263 it->second.stgcHits.empty() &&
it->second.mmHits.empty())
274 MuonSimDataCollection::const_iterator
it = simDataCol->begin();
275 MuonSimDataCollection::const_iterator it_end = simDataCol->end();
276 for (;
it != it_end; ++
it) {
280 std::vector<MuonSimData::Deposit>::const_iterator dit =
it->second.getdeposits().begin();
281 std::vector<MuonSimData::Deposit>::const_iterator dit_end =
it->second.getdeposits().end();
282 for (; dit != dit_end; ++dit) {
283 int barcodeIn = dit->first.barcode();
284 std::map<int, int>::const_iterator bit = barcode_map.find(barcodeIn);
285 if (bit == barcode_map.end()) {
287 <<
" " <<
m_idHelperSvc->toString(
id) <<
" barcode " << barcodeIn);
306 eit->second.mdtHits.insert(*
it);
319 eit->second.rpcHits.insert(*
it);
326 eit->second.tgcHits.insert(*
it);
333 eit->second.stgcHits.insert(*
it);
340 eit->second.mmHits.insert(*
it);
353 CscSimDataCollection::const_iterator
it = simDataCol->begin();
354 CscSimDataCollection::const_iterator it_end = simDataCol->end();
355 for (;
it != it_end; ++
it) {
359 std::vector<CscSimData::Deposit>::const_iterator dit =
it->second.getdeposits().begin();
360 std::vector<CscSimData::Deposit>::const_iterator dit_end =
it->second.getdeposits().end();
361 for (; dit != dit_end; ++dit) {
363 std::map<int, int>::const_iterator bit = barcode_map.find(barcodeIn);
364 if (bit == barcode_map.end()) {
366 <<
" " <<
m_idHelperSvc->toString(
id) <<
" barcode " << barcodeIn);
383 eit->second.cscHits.insert(*
it);
398 bool restrictedTruth)
const {
399 std::set<Identifier>
ids;
400 std::vector<const Trk::MeasurementBase*> measurements;
401 std::vector<const MuonSegment*>::const_iterator sit = segments.begin();
402 std::vector<const MuonSegment*>::const_iterator sit_end = segments.end();
403 for (; sit != sit_end; ++sit) {
404 std::vector<const Trk::MeasurementBase*>::const_iterator mit = (*sit)->containedMeasurements().begin();
405 std::vector<const Trk::MeasurementBase*>::const_iterator mit_end = (*sit)->containedMeasurements().end();
406 for (; mit != mit_end; ++mit) {
415 if (!
id.is_valid() || !
m_idHelperSvc->mdtIdHelper().is_muon(
id))
continue;
416 if (
ids.count(
id))
continue;
417 measurements.push_back(meas);
425 bool restrictedTruth)
const {
430 unsigned int nmatchedHitsBest = 0;
434 for (;
tit != tit_end; ++
tit) {
435 unsigned int nhits =
tit->second.mdtHits.size() +
tit->second.cscHits.size() +
tit->second.rpcHits.size() +
436 tit->second.tgcHits.size() +
tit->second.stgcHits.size() +
tit->second.mmHits.size();
437 if (nhits == 0)
continue;
441 ATH_MSG_DEBUG(
" performed truth match for particle with barcode: " <<
tit->first <<
" overlap " << nmatchedHits <<
" fraction "
442 << (
double)nmatchedHits / (
double)nhits);
443 if (nmatchedHits > 0 && nmatchedHits > nmatchedHitsBest) {
444 bestMatch = trackTruth;
445 nmatchedHitsBest = nmatchedHits;
458 std::vector<const Trk::MeasurementBase*>::const_iterator mit = measurements.begin();
459 std::vector<const Trk::MeasurementBase*>::const_iterator mit_end = measurements.end();
460 for (; mit != mit_end; ++mit) {
463 if (!meas) {
continue; }
472 if (!
id.is_valid() || !
m_idHelperSvc->mdtIdHelper().is_muon(
id))
continue;
503 MuonSimDataCollection::const_iterator
it = simCol.begin();
504 MuonSimDataCollection::const_iterator it_end = simCol.end();
505 for (;
it != it_end; ++
it) {
510 int isOnTrack =
ids.count(
id);
511 if (isOnTrack)
continue;
515 bool chamberHasHits = chids.count(chid);
516 if (restrictedTruth && !chamberHasHits)
continue;
519 std::vector<MuonSimData::Deposit>::const_iterator dit =
it->second.getdeposits().begin();
520 std::vector<MuonSimData::Deposit>::const_iterator dit_end =
it->second.getdeposits().end();
521 for (; dit != dit_end; ++dit) {
532 CscSimDataCollection::const_iterator
it = simCol.begin();
533 CscSimDataCollection::const_iterator it_end = simCol.end();
534 for (;
it != it_end; ++
it) {
537 int isOnTrack =
ids.count(
id);
538 if (isOnTrack)
continue;
542 bool chamberHasHits = chids.count(chid);
543 if (restrictedTruth && !chamberHasHits)
continue;
546 std::vector<CscSimData::Deposit>::const_iterator dit =
it->second.getdeposits().begin();
547 std::vector<CscSimData::Deposit>::const_iterator dit_end =
it->second.getdeposits().end();
548 for (; dit != dit_end; ++dit) {
567 MuonSimDataCollection::const_iterator
it = simCol.find(
id);
568 if (
it == simCol.end()) {
574 std::vector<MuonSimData::Deposit>::const_iterator dit =
it->second.getdeposits().begin();
575 std::vector<MuonSimData::Deposit>::const_iterator dit_end =
it->second.getdeposits().end();
576 for (; dit != dit_end; ++dit) {
577 double radius = dit->second.firstEntry();
581 if (checkSign >= 0) {
596 MuonSimDataCollection::const_iterator
it = simCol.end();
598 bool goodCluster =
false;
603 if (!cluster)
continue;
606 if (
it != simCol.end()) {
618 const std::vector<Identifier> &rdoList = prd->
rdoList();
619 std::vector<Identifier>::const_iterator rit = rdoList.begin();
620 std::vector<Identifier>::const_iterator rit_end = rdoList.end();
621 for (; rit != rit_end; ++rit) {
622 it = simCol.find(*rit);
623 if (
it != simCol.end()) {
629 it = simCol.find(
id);
630 if (
it != simCol.end()) goodCluster =
true;
634 if (!goodCluster ||
it == simCol.end()) {
639 std::vector<MuonSimData::Deposit>::const_iterator dit =
it->second.getdeposits().begin();
640 std::vector<MuonSimData::Deposit>::const_iterator dit_end =
it->second.getdeposits().end();
641 for (; dit != dit_end; ++dit) {
652 CscSimDataCollection::const_iterator
it = simCol.find(
id);
653 if (
it == simCol.end()) {
659 std::vector<CscSimData::Deposit>::const_iterator dit =
it->second.getdeposits().begin();
660 std::vector<CscSimData::Deposit>::const_iterator dit_end =
it->second.getdeposits().end();
661 for (; dit != dit_end; ++dit) {
669 int pdgFinal = ((traj.empty()) ? -999 : traj.front().cptr()->pdg_id());
670 bool foundBC =
false;
671 for (
const auto& pit : traj) {
673 if (pit.barcode() == barcodeIn || foundBC) {
681 if (
particle->pdg_id() != pdgFinal) {
690 bool foundBC =
false;
691 for (
const auto& pit : traj) {
693 if (pit.barcode() == barcodeIn || foundBC) {
709 const int barcodeIn)
const {
710 std::pair<HepMC::ConstGenParticlePtr, unsigned int> thePair(
nullptr, 0);
711 unsigned int scat = 0;
713 bool foundBC =
false;
717 for (
auto pit = traj.begin(); pit != traj.end(); ++pit) {
718 if ((*pit).barcode() == barcodeIn || foundBC) {
726 if (
particle->pdg_id() == pdgFinal) {
727 const auto& pit_p = *pit;
728 if ((theFirst != pit_p.scptr()) && (
particle->momentum().t() != ePrev))
733 theFirst = (*pit).scptr();
740 theFirst = (*pit).cptr();
741 pdgFinal = (*pit)->pdg_id();
743 if ((*pit)->pdg_id() == pdgFinal) {
745 if ((theFirst != pit_p.cptr()) && ((*pit).cptr()->momentum().t() != ePrev))
750 theFirst = (*pit).cptr();
760 if (theFirst && theFirst->pdg_id() != pdgFinal)
ATH_MSG_ERROR(
"Wrong pdgId association in getFirst()");
761 ATH_MSG_DEBUG(
"Number of scatters = " << scat <<
" pdgId = " << pdgFinal);
763 thePair.first = theFirst;
764 thePair.second = scat;