Patch for STACO muons: Copy the truth information from the muon back to the combined track to avoid file corruptions reported in ATLASRECTS-6454
one more thing: need to have muonlink set for all truth particles to avoid ELReset errors
89 {
92 std::unique_ptr<SG::WriteDecorHandle<xAOD::TruthParticleContainer, ElementLink<xAOD::MuonContainer>>> muonTruthParticleRecoLink{};
94 muonTruthParticleRecoLink = std::make_unique<SG::WriteDecorHandle<xAOD::TruthParticleContainer, ElementLink<xAOD::MuonContainer>>>(
m_muonTruthRecoLink, ctx);
95 }
96
97 SG::WriteDecorHandle<xAOD::MuonContainer, ElementLink<xAOD::TruthParticleContainer> > muonTruthParticleLink(
m_muonTruthParticleLink,
98 ctx);
99 if (!muonTruthParticleLink.isValid()) {
101 return StatusCode::FAILURE;
102 }
106 ctx);
109
110
111 bool saw_staco = false;
112 bool decor_staco = false;
113
114
115 for (
const xAOD::Muon* muon : *muonTruthParticleLink) {
116
117
122 tp =
muon->trackParticle(xAOD::Muon::InnerDetectorTrackParticle);
123 } else {
124 tp =
muon->primaryTrackParticle();
125 }
126
127 bool foundTruth{false}, setOrigin{false};
128 if (tp) {
129
130
131
132 if (acc_origin.isAvailable(*tp) && acc_origin(*tp) != 0) {
133 muonTruthParticleOrigin(*muon) = acc_origin(*tp);
134 muonTruthParticleType(*muon) = acc_type(*tp);
135 setOrigin = true;
136 }
137
138 ElementLink<xAOD::TruthParticleContainer> truthLink;
139 if (acc_link.isAvailable(*tp)) {
140 truthLink = acc_link(*tp);
141 } else {
142 ATH_MSG_DEBUG(
"Could not find any truth link associated with track having pt:"<<
tp->pt()<<
" MeV, eta: "<<
tp->eta()<<
", phi: "<<
tp->phi()<<
", charge: "<<
tp->charge()<<
". d0:"<<
tp->d0()<<
", z0: "<<
tp->z0());
143 }
144
147
154 continue;
155 }
157 foundTruth = true;
159 ElementLink<xAOD::TruthParticleContainer> muonTruthLink{*muonTruthContainer,
160 truthParticle->index(),
161 ctx};
163 muonTruthParticleLink(*muon) = muonTruthLink;
164 if (!setOrigin) {
165 muonTruthParticleOrigin(*muon) = acc_origin(*tp);
166 muonTruthParticleType(*muon) = acc_type(*tp);
167 setOrigin = true;
168 }
170 if (muonTruthParticleRecoLink && muonTruthParticleRecoLink->operator()(*truthParticle).isValid()){
171 const xAOD::Muon* decor_muon = *muonTruthParticleRecoLink->operator()(*truthParticle);
172 ATH_MSG_VERBOSE(
"Truth particle is already decorated with reco muon "<<decor_muon->
pt()*1.e-3
173 <<
" eta: "<<decor_muon->
eta()<<
" phi: "<<decor_muon->
phi()<<
" charge: "<<
174 decor_muon->
charge()<<
" author: "<<decor_muon->
author()<<
" all authors: "<<
176
177
179 ATH_MSG_DEBUG(
"Author of the decorated muon is better than the one of the new candidate");
180 continue;
181 }
183 const int com_score = (
muon->allAuthors() & com_bit) - (decor_muon->
allAuthors() &com_bit);
184 if (com_score > 0){
185 ATH_MSG_DEBUG(
"Found two muons reconstructed by an equivalent author. But this one is from the commissioning chain");
186 continue;
187 }
189 if (
deltaR2(muon,truthParticle) >=
deltaR2(muon, decor_muon))
continue;
190 }
191
192
193 ElementLink<xAOD::MuonContainer> muonLink{
muon, *muonTruthParticleLink, ctx};
194
196 std::vector<unsigned int> nprecHitsPerChamberLayer(
toInt(ChIndex::ChIndexMax), dummy_unsigned);
197 std::vector<unsigned int> nphiHitsPerChamberLayer(
toInt(PhiIndex::PhiIndexMax), dummy_unsigned);
198 std::vector<unsigned int> ntrigEtaHitsPerChamberLayer(
toInt(PhiIndex::PhiIndexMax), dummy_unsigned);
199
200 constexpr int author_sel = (1<<xAOD::Muon::MuidCo) | (1<<xAOD::Muon::MuidSA) | (1<<xAOD::Muon::MuGirl);
202 ? truthParticle
203 : nullptr,
204 tp->track(), nprecHitsPerChamberLayer, nphiHitsPerChamberLayer, ntrigEtaHitsPerChamberLayer);
206 muonTruthParticleNPrecMatched(*muon) = nprecHitsPerChamberLayer;
207 muonTruthParticleNPhiMatched(*muon) = nphiHitsPerChamberLayer;
208 muonTruthParticleNTrigEtaMatched(*muon) = ntrigEtaHitsPerChamberLayer;
209
210 if (muonTruthParticleRecoLink) (*muonTruthParticleRecoLink)(*truthParticle) = muonLink;
211 break;
212 }
213 } else {
215 }
216 } else {
217 ATH_MSG_WARNING(
"Could not find the appropiate track particle for muon with pT: " <<
muon->pt() * 1.e-3 <<
" GeV, eta: "
218 <<
muon->eta() <<
", phi: " <<
muon->phi()
219 <<
" author: " <<
muon->author());
220 }
221
222 if (!setOrigin) {
223 muonTruthParticleOrigin(*muon) = 0;
224 muonTruthParticleType(*muon) = 0;
225 }
226 if (!foundTruth) {
227 muonTruthParticleLink(*muon) = ElementLink<xAOD::TruthParticleContainer>();
228
229 muonTruthParticleNPrecMatched(*muon) = std::vector<unsigned int>{};
230 muonTruthParticleNPhiMatched(*muon) = std::vector<unsigned int>{};
231 muonTruthParticleNTrigEtaMatched(*muon) = std::vector<unsigned int>{};
232 }
235 if (
muon->author() == xAOD::Muon::STACO) {
237 if (!cmb_trk){
239 continue;
240 } else {
241 if (!saw_staco) {
242 saw_staco = true;
243 decor_staco = !dec_origin.isAvailable (*cmb_trk);
244 }
245 if (decor_staco) {
246 dec_origin(*cmb_trk) = acc_origin(*muon);
247 dec_type(*cmb_trk) = acc_type(*muon);
248 dec_link(*cmb_trk) = acc_link(*muon);
249 }
250 }
251 }
252 }
254 if (muonTruthParticleRecoLink && !muonTruthParticleRecoLink->isAvailable()) {
257 (*muonTruthParticleRecoLink)(*truthParticle) = ElementLink<xAOD::MuonContainer>();
258 }
259 }
260
261 return StatusCode::SUCCESS;
262}
#define ATH_CHECK
Evaluate an expression and check for errors.
#define ATH_MSG_VERBOSE(x)
#define ATH_MSG_WARNING(x)
bool toPersistent()
Dummy function provinding the offline interface.
bool isValid() const
Test to see if the link can be dereferenced.
SG::WriteDecorHandleKey< xAOD::MuonContainer > m_muonTruthParticleOrigin
SG::WriteDecorHandleKey< xAOD::MuonContainer > m_muonTruthParticleNPhiMatched
Gaudi::Property< bool > m_associateWithInDetTP
SG::WriteDecorHandleKey< xAOD::TruthParticleContainer > m_muonTruthRecoLink
SG::WriteDecorHandleKey< xAOD::MuonContainer > m_muonTruthParticleType
SG::WriteDecorHandleKey< xAOD::MuonContainer > m_muonTruthParticleLink
SG::WriteDecorHandleKey< xAOD::MuonContainer > m_muonTruthParticleNTrigEtaMatched
void count_chamber_layers(const xAOD::IParticle *truth_particle, const Trk::Track *ptrk, std::vector< unsigned int > &nprecHitsPerChamberLayer, std::vector< unsigned int > &nphiHitsPerChamberLayer, std::vector< unsigned int > &ntrigEtaHitsPerChamberLayer) const
SG::ReadHandleKey< xAOD::TruthParticleContainer > m_truthMuKey
Key to the filtered muon truth particles.
SG::WriteDecorHandleKey< xAOD::MuonContainer > m_muonTruthParticleNPrecMatched
virtual double eta() const
The pseudorapidity ( ) of the particle.
virtual double phi() const
The azimuthal angle ( ) of the particle.
virtual double pt() const
The transverse momentum ( ) of the particle.
uint16_t allAuthors() const
Get all the authors of this Muon.
bool is_sim_descendant(const T1 &p1, const T2 &p2)
Method to check if the first particle is a descendant of the second in the simulation,...
bool isStable(const T &p)
Identify if the particle is stable, i.e. has not decayed.
int authorRank(const xAOD::Muon::Author &a)
const T * get(const ReadCondHandleKey< T > &key, const EventContext &ctx)
Convenience function to retrieve an object given a ReadCondHandleKey.
double deltaR2(double rapidity1, double phi1, double rapidity2, double phi2)
from bare rapidity,phi
TrackParticle_v1 TrackParticle
Reference the current persistent version:
TruthParticle_v1 TruthParticle
Typedef to implementation.
Muon_v1 Muon
Reference the current persistent version:
TruthParticleContainer_v1 TruthParticleContainer
Declare the latest version of the truth particle container.