From 62c181e2f1fcb24e07d350a56544db563064668f Mon Sep 17 00:00:00 2001
From: "lihn@ihep.ac.cn" <lihn@ihep.ac.cn>
Date: Thu, 20 Jun 2024 15:35:38 +0000
Subject: [PATCH] fix a crash in TrueMuonTag due to missing input collections

---
 .../DetCRD/scripts/TDR_o1_v01/tracking.py     |  1 +
 .../src/TrueMuonTagAlg.cpp                    | 45 ++++++++++---------
 .../RecAssociationMaker/src/TrueMuonTagAlg.h  |  2 +-
 3 files changed, 26 insertions(+), 22 deletions(-)

diff --git a/Detector/DetCRD/scripts/TDR_o1_v01/tracking.py b/Detector/DetCRD/scripts/TDR_o1_v01/tracking.py
index 1cc1894f..88076cdd 100644
--- a/Detector/DetCRD/scripts/TDR_o1_v01/tracking.py
+++ b/Detector/DetCRD/scripts/TDR_o1_v01/tracking.py
@@ -227,6 +227,7 @@ from Configurables import TrueMuonTagAlg
 tmt = TrueMuonTagAlg("TrueMuonTag")
 tmt.MCParticleCollection = "MCParticle"
 tmt.TrackList = ["CompleteTracks"]
+tmt.TrackerAssociationList = ["VXDTrackerHitAssociation", "SITTrackerHitAssociation", "SETTrackerHitAssociation", "FTDTrackerHitAssociation", "TPCTrackerHitAss"]
 tmt.MuonTagEfficiency = 0.95 # muon true tag efficiency, default is 1.0 (100%)
 tmt.MuonDetTanTheta = 1.2 # muon det barrel/endcap separation tan(theta)
 #tmt.OutputLevel = DEBUG
diff --git a/Reconstruction/RecAssociationMaker/src/TrueMuonTagAlg.cpp b/Reconstruction/RecAssociationMaker/src/TrueMuonTagAlg.cpp
index 3ae934bf..5a151590 100644
--- a/Reconstruction/RecAssociationMaker/src/TrueMuonTagAlg.cpp
+++ b/Reconstruction/RecAssociationMaker/src/TrueMuonTagAlg.cpp
@@ -135,29 +135,32 @@ StatusCode TrueMuonTagAlg::execute() {
 	//  ass.setSim(it->first);
 	//}
 
-        // tore to new track collection
-        edm4hep::MutableTrack new_track = track.clone();
-
-        // find the max nhits mcparticle 
-        auto max_nhits_iter = std::max_element(mapParticleNHits.begin(), mapParticleNHits.end(),
-                                     [](const auto& lhs, const auto& rhs) {return lhs.second < rhs.second;} );
-        // calculate the barrel/endcap theta
-        auto a_particle = max_nhits_iter->first;
-        auto p_pdg_id = a_particle.getPDG();
-        auto p_status = a_particle.getGeneratorStatus();
-        auto p_px = a_particle.getMomentum()[0];
-        auto p_py = a_particle.getMomentum()[1];
-        auto p_pz = a_particle.getMomentum()[2];
-        auto p_tantheta = sqrt(p_px*p_px+p_py*p_py)/fabs(p_pz);
-      
+        // fill a new track collection only if one found a mc particle above
+        if (!mapParticleNHits.empty()) {
+
+          // store to new track collection
+          edm4hep::MutableTrack new_track = track.clone();
+
+          // find the max nhits mcparticle 
+          auto max_nhits_iter = std::max_element(mapParticleNHits.begin(), mapParticleNHits.end(),
+                                       [](const auto& lhs, const auto& rhs) {return lhs.second < rhs.second;} );
+          // calculate the barrel/endcap theta
+          auto a_particle = max_nhits_iter->first;
+          auto p_pdg_id = a_particle.getPDG();
+          auto p_status = a_particle.getGeneratorStatus();
+          auto p_px = a_particle.getMomentum()[0];
+          auto p_py = a_particle.getMomentum()[1];
+          auto p_pz = a_particle.getMomentum()[2];
+          auto p_tantheta = sqrt(p_px*p_px+p_py*p_py)/fabs(p_pz);
+        
        
-        if (abs(p_pdg_id)==13 && fRandom.Rndm()<_m_muonTagEff ) {
-          debug() << " particle PDG = " << p_pdg_id << "; status = " << p_status << endmsg;
-          if (p_tantheta>_m_muonDetTanTheta) new_track.setType( new_track.getType()| (1<<lcio::ILDDetID::YOKE) ) ;
-          else new_track.setType( new_track.getType()| (1<<lcio::ILDDetID::YOKE_ENDCAP) ) ;
+          if (abs(p_pdg_id)==13 && fRandom.Rndm()<_m_muonTagEff ) {
+            debug() << " particle PDG = " << p_pdg_id << "; status = " << p_status << endmsg;
+            if (p_tantheta>_m_muonDetTanTheta) new_track.setType( new_track.getType()| (1<<lcio::ILDDetID::YOKE) ) ;
+            else new_track.setType( new_track.getType()| (1<<lcio::ILDDetID::YOKE_ENDCAP) ) ;
+          }
+          outTrackCol->push_back(new_track);
         }
-        outTrackCol->push_back(new_track);
-
       }
       
       if (msgLevel(MSG::DEBUG)) {
diff --git a/Reconstruction/RecAssociationMaker/src/TrueMuonTagAlg.h b/Reconstruction/RecAssociationMaker/src/TrueMuonTagAlg.h
index e61faf1e..e7d16a9d 100644
--- a/Reconstruction/RecAssociationMaker/src/TrueMuonTagAlg.h
+++ b/Reconstruction/RecAssociationMaker/src/TrueMuonTagAlg.h
@@ -33,7 +33,7 @@ class TrueMuonTagAlg : public GaudiAlgorithm {
   // input TrackerAssociation to link TrackerHit and SimTrackerHit
   std::vector<DataHandle<edm4hep::MCRecoTrackerAssociationCollection>* > m_inAssociationColHdls;
   Gaudi::Property<std::vector<std::string> >                             m_inAssociationCollectionNames{this, "TrackerAssociationList", {"VXDTrackerHitAssociation",
-        "SITTrackerHitAssociation", "SETTrackerHitAssociation", "FTDTrackerHitAssociation"}};
+        "SITTrackerHitAssociation", "SETTrackerHitAssociation", "FTDTrackerHitAssociation", "TPCTrackerHitAss"}};
 
   // output TrackParticleAssociation
   //std::vector<DataHandle<edm4hep::MCRecoTrackParticleAssociationCollection>* > m_outColHdls;
-- 
GitLab