ATLAS Offline Software
TrigInDetTrackTruth.cxx
Go to the documentation of this file.
1 /*
2  Copyright (C) 2002-2024 CERN for the benefit of the ATLAS collaboration
3 */
4 
5 
6 /**************************************************************************
7  **
8  ** File: TrigInDetTrackTruth.cxx
9  **
10  ** Description: - Stores a vector of pointers to GenParticles which match
11  ** a TrigInDetTrack and associated matching quality
12  ** quantities (nr of common hits only for now)
13  **
14  ** Author: R.Goncalo
15  **
16  ** Created: Sat Jan 18 19:55:56 GMT 2006
17  ** Modified: RG 1/9/06 - added method for object to update own family tree
18  ** RG 2/9/06 - was going to change set of two vectors acting
19  ** as a map<HepMcPart.Link,TrigIDHitStats> into a GaudiUtils::VectorMap to
20  ** improve readout speed but decided against this since it complicates
21  ** matters and creates dependencies, and because only a few (~1-5) GenParticles
22  ** can be expected to match any TrigInDetTrack, so the overhead is small.
23  ** Changed m_family_tree from map<unsigned int,unsigned int> into
24  ** vector< pair<unsigned int,unsigned int>> to avoid persistency problems and
25  ** also to make it easier to use either mother or daughter indices as keys
26  **************************************************************************/
27 
30 #include "GaudiKernel/IMessageSvc.h"
31 #include "AtlasHepMC/Operators.h"
32 
35 {
36  std::string thisName("TrigInDetTrackTruth::addMatch");
37  MsgStream log(Athena::getMessageSvc(), thisName);
38 
39  log << MSG::DEBUG<< "Inserting HepMcParticleLink and TrigIDHitStats to TrigInDetTrackTruth map" << endmsg;
40 
41  int indx = index(p_tru_part);
42  if ( indx >= 0 ) {
43  // HepMcParticleLink already exists: replace in vectors
44  log << MSG::DEBUG<< "HepMcParticleLink already in map: replacing"
45  << endmsg;
46  m_true_part_vec[indx] = p_tru_part;
47  m_nr_common_hits[indx] = hits;
48  } else {
49  // push back into vectors to use HepMcParticleLink as key
50  m_true_part_vec.push_back(p_tru_part);
51  m_nr_common_hits.push_back( hits );
52  indx = m_true_part_vec.size() - 1;
53  }
54 
55  // to know the best match in total nr.hits
56  if ( m_best_match_hits == -1 ||
57  (unsigned int)hits.total() > nrCommonHits(m_best_match_hits) )
59 
60  if ( m_best_Si_match_hits == -1 ||
61  (unsigned int)(hits.pixhits() + hits.scthits())> nrCommonSiHits(m_best_Si_match_hits) )
63 
64  if ( m_best_TRT_match_hits == -1 ||
65  (unsigned int)hits.trthits() > nrCommonTRTHits(m_best_TRT_match_hits) )
67 
68  return indx;
69 }
70 
75 
76  std::vector<HepMcParticleLink>::const_iterator it1, end=m_true_part_vec.end();
77  int indx = 0;
78  for (it1=m_true_part_vec.begin(); it1 != end; ++it1) {
79  if (hep_link == (*it1)) {
80  // HepMcParticleLink found in vector: return index
81  return indx;
82  }
83  ++indx;
84  }
85  // HepMcParticleLink not found: return default -1
86  return -1;
87 }
88 
96 {
97 
98  std::string thisName("TrigInDetTrackTruth::updateFamilyTree");
99  MsgStream log(Athena::getMessageSvc(), thisName);
100 
101  log << MSG::DEBUG<< "In TrigInDetTrackTruth::updateFamilyTree()" << endmsg;
102 
103  int nr_mothers_found=0;
104  int child=-1;
106 
107  for (it1=m_true_part_vec.begin(); it1 != end; ++it1)
108  {
109  child++;
110  /* get production vertex GenParticle pointed to by this link */
111  log << MSG::DEBUG<< "Looking for mother of matching particle nr "<<child<<endmsg;
112 
113  // first get GenParticle pointer
114  if ( !it1->isValid() ) continue;
115 
116  auto p_child = (*it1).cptr();
117  log << MSG::DEBUG << "GenParticle " << child << " (" << p_child << "); PDG id="
118  << p_child->pdg_id() << "; status=" << p_child->status()
119  << "; pT=" << p_child->momentum().perp()
120  << "; searches mother..."
121  << endmsg;
122 
123  // then get production vertex (check against null)
124  auto p_child_vtx = p_child->production_vertex();
125  if ( !p_child_vtx)
126  {
127  log << MSG::DEBUG<<"GenVertex pointer null: jump to next particle"<<endmsg;
128  continue;
129  }
130  log << MSG::DEBUG<< "GenParticle "<< child << " comes from vertex with pointer "
131  << p_child_vtx << endmsg;
132 
133  /* find mother: there should be only one for final state particles
134  (particles which can leave energy deposits in detectors) */
135 
136  // check a mother was found
137  if ( p_child_vtx->particles_in_size()==0)
138  {
139  log << MSG::DEBUG<< "Mother not found: go to next particle" <<endmsg;
140  continue;
141  }
142 #ifdef HEPMC3
143  auto p_mum = p_child_vtx->particles_in().begin();
144 #else
145  HepMC::GenVertex::particles_in_const_iterator p_mum = p_child_vtx->particles_in_const_begin();
146 #endif
147  log << MSG::DEBUG<< "Mother GenParticle (" << *p_mum << ") found; PDG id="
148  << (*p_mum)->pdg_id() << "; status=" << (*p_mum)->status()
149  << "; pT=" << (*p_mum)->momentum().perp()
150  << "; does it match track?"
151  << endmsg;
152  // mother is (*p_mum); still have to see if it is a match to this track
154 
155  bool mum_found=false;
156  for (unsigned int mum=0; it2 != end; ++it2, ++mum)
157  {
158  log << MSG::DEBUG << "* Trying daughter index=" << child
159  << " and mother index=" << mum << endmsg;
160  auto p2 = *it2;
161  if ( *p_mum == p2 )
162  { // mother also matches track
163  m_family_tree.push_back( std::pair<unsigned int, unsigned int>(mum,child) );
164  mum_found=true;
165  nr_mothers_found++;
166 
167  log << MSG::DEBUG << "* Mother also matches track! "
168  << nr_mothers_found
169  << " mother-daughter relations found so far" << endmsg;
170  log << MSG::DEBUG << "Daughter "<< child <<" (PDG id="
171  << p_child->pdg_id() << "; pT=" << p_child->momentum().perp()
172  << ") comes from mother " << mum << " (PDG id="
173  << (*p_mum)->status() << "; pT=" << p_child->momentum().perp()
174  << ")" << endmsg;
175  }
176  }
177  if (!mum_found) log << MSG::DEBUG << "* Mother doesn't match track"
178  << endmsg;
179  }
180  return nr_mothers_found;
181 }
182 
185 {
187 }
188 
191 {
193 }
194 
197 {
199 }
200 
203 {
204  if (i < m_true_part_vec.size()) return &(m_true_part_vec[i]);
205  else return NULL;
206 }
207 
208 
210 unsigned int TrigInDetTrackTruth::nrCommonHits(unsigned int i) const
211 {
212  if (i < m_true_part_vec.size()) return m_nr_common_hits[i].total();
213  else return 0;
214 }
215 
217 unsigned int TrigInDetTrackTruth::nrCommonSiHits(unsigned int i) const
218 {
219  if (i < m_true_part_vec.size()) return (m_nr_common_hits[i].pixhits() + m_nr_common_hits[i].scthits());
220  else return 0;
221 }
222 
224 unsigned int TrigInDetTrackTruth::nrCommonTRTHits(unsigned int i) const
225 {
226  if (i < m_true_part_vec.size()) return m_nr_common_hits[i].trthits();
227  else return 0;
228 }
229 
232 {
234 }
235 
238 {
239  return m_nr_common_hits[m_best_TRT_match_hits].trthits();
240 }
241 
242 
244 unsigned int TrigInDetTrackTruth::nrMatches() const
245 {
246  return m_true_part_vec.size();
247 }
248 
250 const std::vector< std::pair<unsigned int, unsigned int> >& TrigInDetTrackTruth::getFamilyTree() const
251 {
252  return m_family_tree;
253 }
254 
257 int TrigInDetTrackTruth::motherIndexInChain(unsigned int daughter) const {
258 
259  if ( m_family_tree.empty() ) return -1;
260 
261  std::vector< std::pair<unsigned int, unsigned int> >::const_iterator it,it_end = m_family_tree.end();
262 
263  for (it = m_family_tree.begin(); it != it_end; ++it) {
264  if (daughter == (*it).second) return (int)(*it).first;
265  }
266  return -1;
267 }
268 
271 bool TrigInDetTrackTruth::motherInChain(unsigned int daughter) const {
272  return ( motherIndexInChain(daughter) >=0 );
273 }
274 
277 std::vector<unsigned int> TrigInDetTrackTruth::daughterIndicesInChain(unsigned int mother) const {
278 
279  std::vector<unsigned int> v_indx;
280  v_indx.clear();
281  if ( m_family_tree.empty() ) return v_indx;
282 
283  std::vector< std::pair<unsigned int, unsigned int> >::const_iterator it,it_end = m_family_tree.end();
284  unsigned int i=0;
285  for (it = m_family_tree.begin(); it != it_end; ++it) {
286  if (mother == (*it).first) {
287  v_indx.push_back(i);
288  }
289  ++i;
290  }
291  return v_indx;
292 }
293 
296 bool TrigInDetTrackTruth::daughtersInChain(unsigned int mother) const {
297  std::vector<unsigned int> v_indx = daughterIndicesInChain(mother);
298  return ( !(v_indx.empty()) );
299 }
xAOD::iterator
JetConstituentVector::iterator iterator
Definition: JetConstituentVector.cxx:68
TrigInDetTrackTruth::getFamilyTree
const std::vector< std::pair< unsigned int, unsigned int > > & getFamilyTree() const
returns copy of family tree "map"
Definition: TrigInDetTrackTruth.cxx:250
TrigInDetTrackTruth::m_best_match_hits
int m_best_match_hits
Definition: TrigInDetTrackTruth.h:109
TRTCalib_Extractor.hits
hits
Definition: TRTCalib_Extractor.py:35
TrigInDetTrackTruth::nrCommonHitsBestTRT
unsigned int nrCommonHitsBestTRT() const
returns total number of common hits from best match true particle and TrigInDetTrack
Definition: TrigInDetTrackTruth.cxx:237
getMessageSvc.h
singleton-like access to IMessageSvc via open function and helper
TrigInDetTrackTruth::m_true_part_vec
std::vector< HepMcParticleLink > m_true_part_vec
Definition: TrigInDetTrackTruth.h:114
TrigInDetTrackTruth::index
int index(HepMcParticleLink &) const
method to find if a given HepMcParticleLink already exists in "map" and, if so, what is its index; if...
Definition: TrigInDetTrackTruth.cxx:74
skel.it
it
Definition: skel.GENtoEVGEN.py:396
TrigInDetTrackTruth::bestSiMatch
const HepMcParticleLink * bestSiMatch() const
returns best match according to the number of hits
Definition: TrigInDetTrackTruth.cxx:190
TrigInDetTrackTruth::m_family_tree
std::vector< std::pair< unsigned int, unsigned int > > m_family_tree
Definition: TrigInDetTrackTruth.h:118
TrigInDetTrackTruth::m_best_Si_match_hits
int m_best_Si_match_hits
Definition: TrigInDetTrackTruth.h:110
TrigIDHitStats
Definition: TrigIDHitStats.h:18
TrigInDetTrackTruth::bestMatch
const HepMcParticleLink * bestMatch() const
returns best match according to the number of hits
Definition: TrigInDetTrackTruth.cxx:184
TrigInDetTrackTruth::nrCommonTRTHits
unsigned int nrCommonTRTHits(unsigned int i) const
returns number of common hits from true particle i and TrigInDetTrack
Definition: TrigInDetTrackTruth.cxx:224
Athena::getMessageSvc
IMessageSvc * getMessageSvc(bool quiet=false)
Definition: getMessageSvc.cxx:20
mergePhysValFiles.end
end
Definition: DataQuality/DataQualityUtils/scripts/mergePhysValFiles.py:93
TrigInDetTrackTruth::truthMatch
const HepMcParticleLink * truthMatch(unsigned int i) const
returns matching true particle number i
Definition: TrigInDetTrackTruth.cxx:202
TRTCalib_cfilter.p2
p2
Definition: TRTCalib_cfilter.py:131
lumiFormat.i
int i
Definition: lumiFormat.py:85
DetDescrDictionaryDict::it1
std::vector< HWIdentifier >::iterator it1
Definition: DetDescrDictionaryDict.h:17
TrigInDetTrackTruth.h
endmsg
#define endmsg
Definition: AnalysisConfig_Ntuple.cxx:63
TrigInDetTrackTruth::nrCommonHitsBestSi
unsigned int nrCommonHitsBestSi() const
returns total number of common hits from best match true particle and TrigInDetTrack
Definition: TrigInDetTrackTruth.cxx:231
TrigInDetTrackTruth::m_nr_common_hits
std::vector< TrigIDHitStats > m_nr_common_hits
Definition: TrigInDetTrackTruth.h:115
TrigInDetTrackTruth::daughtersInChain
bool daughtersInChain(unsigned int) const
given index of a GenParticle which matches the track returns true if it has stable a daughter which a...
Definition: TrigInDetTrackTruth.cxx:296
TrigInDetTrackTruth::nrCommonSiHits
unsigned int nrCommonSiHits(unsigned int i) const
returns number of common hits from true particle i and TrigInDetTrack
Definition: TrigInDetTrackTruth.cxx:217
TrigInDetTrackTruth::m_best_TRT_match_hits
int m_best_TRT_match_hits
Definition: TrigInDetTrackTruth.h:111
TrigInDetTrackTruth::bestTRTMatch
const HepMcParticleLink * bestTRTMatch() const
returns best match according to the number of hits
Definition: TrigInDetTrackTruth.cxx:196
TrigInDetTrackTruth::addMatch
int addMatch(HepMcParticleLink p_tru_part, TrigIDHitStats hits)
accessor to fill object: returns index of new entry in vectors
Definition: TrigInDetTrackTruth.cxx:34
DEBUG
#define DEBUG
Definition: page_access.h:11
python.CaloCondTools.log
log
Definition: CaloCondTools.py:20
TrigInDetTrackTruth::daughterIndicesInChain
std::vector< unsigned int > daughterIndicesInChain(unsigned int) const
given index of a GenParticle which matches the track returns vector with indices of its daughters,...
Definition: TrigInDetTrackTruth.cxx:277
TrigInDetTrackTruth::nrMatches
unsigned int nrMatches() const
returns number of matching particles
Definition: TrigInDetTrackTruth.cxx:244
TrigInDetTrackTruth::motherIndexInChain
int motherIndexInChain(unsigned int) const
given index of a GenParticle which matches the track returns index of its mother,...
Definition: TrigInDetTrackTruth.cxx:257
TrigInDetTrackTruth::motherInChain
bool motherInChain(unsigned int) const
given index of a GenParticle which matches the track returns true if its mother also matches the trac...
Definition: TrigInDetTrackTruth.cxx:271
TrigInDetTrackTruth::updateFamilyTree
int updateFamilyTree()
accessor to fill family tree: for each HepMcParticleLink in the internal vector of HepMC::GenParticle...
Definition: TrigInDetTrackTruth.cxx:95
Operators.h
TrigInDetTrackTruth::nrCommonHits
unsigned int nrCommonHits(unsigned int i) const
returns number of common hits from true particle i and TrigInDetTrack
Definition: TrigInDetTrackTruth.cxx:210