ATLAS Offline Software
TrigInDetTrackTruthMap.cxx
Go to the documentation of this file.
1 /*
2  Copyright (C) 2002-2017 CERN for the benefit of the ATLAS collaboration
3 */
4 
5 
6 /**************************************************************************
7  **
8  ** File: TrigInDetTrackTruthMap.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:
18  **
19  **
20  **
21  **************************************************************************/
22 
25 #include "GaudiKernel/IMessageSvc.h"
26 
27 #include <iomanip>
28 #include <iostream>
29 #include <cassert>
30 
33  unsigned int trk_indx,
34  TrigInDetTrackTruth& p_trk_tru)
35 {
36  std::string thisName("TrigInDetTrackTruthMap::addMatch");
37  MsgStream log(Athena::getMessageSvc(), thisName);
38 
39  log << MSG::DEBUG << "Adding TrigInDetTrack to map" << endmsg;
40 
41  // check entry for this track doesn't exist (otherwise we have a multimap)
42  const TrigInDetTrack* p_trig_trk((*trkColl)[trk_indx]);
43 
44  if ( !hasTruth( p_trig_trk ) ) {
45  ElementLink< TrigInDetTrackCollection > p_trk_lnk(*trkColl,trk_indx);
46  m_elink_vec.push_back(p_trk_lnk);
48  if (!status)
49  log << MSG::DEBUG << "ERROR: could not set ElementLink persistent" << endmsg;
50  m_truth_vec.push_back(p_trk_tru);
51  } else {
52  log << MSG::DEBUG << "TrigInDetTrack already in map!" << endmsg;
53  }
54 }
55 
57 // returns true if truth association exists for track
58 bool TrigInDetTrackTruthMap::hasTruth(const TrigInDetTrack* p_trig_trk) const
59 {
60  // must loop over map because ElementLink is a unidirectional link
61  bool found=false;
62  for (unsigned int i=0; i < m_elink_vec.size(); ++i) {
63  // if ( p_trig_trk == *(m_elink_vec[i]) ) {
64  if(!m_elink_vec[i].isValid())
65  continue;
66  if ((*(m_elink_vec[i]))->param()) {
67  if ((*(m_elink_vec[i]))->algorithmId() == p_trig_trk->algorithmId() &&
68  (*(m_elink_vec[i]))->param()->pT() == p_trig_trk->param()->pT() &&
69  (*(m_elink_vec[i]))->param()->eta() == p_trig_trk->param()->eta() &&
70  (*(m_elink_vec[i]))->param()->phi0() == p_trig_trk->param()->phi0()) {
71  found=true;
72  break;
73  }
74  }
75  }
76  // true if this track pointer exists at least once in the vector
77  return (found);
78 }
79 
80 
81 // returns the track truth association object
83 {
84  std::string thisName("TrigInDetTrackTruthMap::truth");
85  MsgStream log(Athena::getMessageSvc(), thisName);
86 
87  log << MSG::DEBUG<<"Searching truth for track at ptr="<<p_trig_trk<<endmsg;
88 
89  // must loop over map because ElementLink is a unidirectional link
90  for (unsigned int i=0; i < m_elink_vec.size(); ++i) {
91  // if ( p_trig_trk == *(m_elink_vec[i]) ) {
92  if(!m_elink_vec[i].isValid())
93  continue;
94  if ((*(m_elink_vec[i]))->param()) {
95  if ((*(m_elink_vec[i]))->algorithmId() == p_trig_trk->algorithmId() &&
96  (*(m_elink_vec[i]))->param()->pT() == p_trig_trk->param()->pT() &&
97  (*(m_elink_vec[i]))->param()->eta() == p_trig_trk->param()->eta() &&
98  (*(m_elink_vec[i]))->param()->phi0() == p_trig_trk->param()->phi0()) {
99  // found position in vector corresponding to this track pointer
100  log << MSG::DEBUG << "Truth match for track at ptr=" << p_trig_trk
101  << " found in map at index " << i <<endmsg;
102  return &m_truth_vec[i];
103  }
104  }
105  }
106  // didn't find it: return null pointer
107  log << MSG::DEBUG <<"Truth match for track at ptr="<<p_trig_trk <<" not in map"<<endmsg;
108  return NULL;
109 }
110 
111 
112 // to make the map more useful: return the link to GenParticle which better
113 // matches this track according to number of common hits
115 {
116  if ( hasTruth(p_trig_trk) ) {
117  return ( truth(p_trig_trk)->bestSiMatch() );
118  } else {
119  return NULL;
120  }
121 }
122 
124 {
125  if ( hasTruth(p_trig_trk) ) {
126  return ( truth(p_trig_trk)->nrCommonHitsBestSi() );
127  } else {
128  return 0;
129  }
130 }
131 
132 // to make the map more useful: return the link to GenParticle which better
133 // matches this track according to number of common hits
135 {
136  if ( hasTruth(p_trig_trk) ) {
137  return ( truth(p_trig_trk)->bestTRTMatch() );
138  } else {
139  return NULL;
140  }
141 }
142 
144 {
145  if ( hasTruth(p_trig_trk) ) {
146  return ( truth(p_trig_trk)->nrCommonHitsBestTRT() );
147  } else {
148  return 0;
149  }
150 }
151 
152 
154 {
155  std::string thisName("TrigInDetTrackTruthMap::print");
156  MsgStream log(Athena::getMessageSvc(), thisName);
157 
158  std::ostringstream oss;
159  oss << "TrigInDetTruthMap: " << m_elink_vec.size()
160  << " track-truth associations" << std::endl;
161 
162  oss << "---------------------------------------------------------------------------------------------------------------------------------" << std::endl;
163  oss << "#track|algo| pointer | pT | eta | phi |#match|mother|Sihits|TRThits|ev.index| barcode | pdg id | pT | eta | phi |"<< std::endl;
164  for (unsigned int i=0; i < m_elink_vec.size(); ++i) {
165  oss << std::setiosflags(std::ios::dec) << std::setw(6) << i << "|"
166  << std::setiosflags(std::ios::dec) << std::setw(4) << (*(m_elink_vec[i]))->algorithmId() << "|"
167  << std::setw(11) << *(m_elink_vec[i]) << "|";
168 
169  if(!m_elink_vec[i].isValid()) {
170  oss << std::setiosflags(std::ios::dec) << "Invalid TrigInDetTrack link !"
171  << std::setiosflags(std::ios::dec) << std::setw(8) << "|";
172  }
173  else {
174  // print reconstructed track parameters
175  if ((*(m_elink_vec[i]))->param()) {
176  oss << std::setiosflags(std::ios::dec) << std::setw(14) << (*(m_elink_vec[i]))->param()->pT() << "|"
177  << std::setiosflags(std::ios::dec) << std::setw(10) << (*(m_elink_vec[i]))->param()->eta() << "|"
178  << std::setiosflags(std::ios::dec) << std::setw(10) << (*(m_elink_vec[i]))->param()->phi0()<< "|";
179  } else {
180  oss << std::setiosflags(std::ios::dec) << std::setw(15) << "|"
181  << std::setiosflags(std::ios::dec) << std::setw(11) << "|"
182  << std::setiosflags(std::ios::dec) << std::setw(11) << "|";
183  }
184  }
185 
186  if (m_truth_vec[i].nrMatches() == 0) oss << std::endl;
187  // print parameters of truth particles which have hits in common with track
188  for (unsigned int j=0; j < m_truth_vec[i].nrMatches(); ++j) {
189 
190  // find if this matching true particle has a mother which also matches the track
191  HepMcParticleLink p_link( *((m_truth_vec[i]).truthMatch(j)) );
192  int child_indx = (m_truth_vec[i]).index(p_link);
193  int mother_indx = -1;
194  if (child_indx >= 0) mother_indx = (m_truth_vec[i]).motherIndexInChain(child_indx);
195 
196  if (j>0) {
197 
198  oss << std::setiosflags(std::ios::dec) << std::setw(7) << "|"
199  << std::setiosflags(std::ios::dec) << std::setw(5) << "|";
200  oss << std::setiosflags(std::ios::dec) << std::setw(15) << "|"
201  << std::setiosflags(std::ios::dec) << std::setw(11) << "|"
202  << std::setiosflags(std::ios::dec) << std::setw(11) << "|";
203  }
204 
205  oss << std::setiosflags(std::ios::dec) << std::setw(6) << j+1 << "|";
206 
207  if (mother_indx >= 0) {
208  oss << std::setiosflags(std::ios::dec) << std::setw(6) << mother_indx << "|";
209  } else {
210  oss << " -- |";
211  }
212  oss << std::setiosflags(std::ios::dec) << std::setw(6) << (m_truth_vec[i]).nrCommonSiHits(j) << "|"
213  << std::setiosflags(std::ios::dec) << std::setw(7) << (m_truth_vec[i]).nrCommonTRTHits(j) << "|"
214  << std::setiosflags(std::ios::dec) << std::setw(8) << (m_truth_vec[i]).truthMatch(j)->eventIndex() << "|"
215  << std::setiosflags(std::ios::dec) << std::setw(9) << (m_truth_vec[i]).truthMatch(j)->barcode() << "|";
216 
217  if ((m_truth_vec[i]).truthMatch(j)->cptr()) {
218 
219  oss << std::setiosflags(std::ios::dec) << std::setw(10) << (m_truth_vec[i]).truthMatch(j)->cptr()->pdg_id() << "|"
220  << std::setiosflags(std::ios::dec) << std::setw(14) << (m_truth_vec[i]).truthMatch(j)->cptr()->momentum().perp()<< "|"
221  << std::setiosflags(std::ios::dec) << std::setw(10) << (m_truth_vec[i]).truthMatch(j)->cptr()->momentum().eta() << "|"
222  << std::setiosflags(std::ios::dec) << std::setw(10) << (m_truth_vec[i]).truthMatch(j)->cptr()->momentum().phi() << "|";
223  } else {
224  oss << std::setiosflags(std::ios::dec) << std::setw(11) << "|"
225  << std::setiosflags(std::ios::dec) << std::setw(15) << "|"
226  << std::setiosflags(std::ios::dec) << std::setw(11) << "|"
227  << std::setiosflags(std::ios::dec) << std::setw(11) << "|";
228  }
229  oss << std::endl;
230  }
231  }
232  oss << "---------------------------------------------------------------------------------------------------------------------------------" << std::endl;
233 
234  log << MSG::DEBUG << oss.str() << endmsg;
235 }
236 
238 {
239  assert (m_elink_vec.size() == m_truth_vec.size());
240  return m_elink_vec.size();
241 }
242 
243 const TrigInDetTrackTruth*
245 {
246  assert (i < m_truth_vec.size());
247  return &m_truth_vec[i];
248 }
249 
250 const TrigInDetTrack*
252 {
253  assert (i < m_elink_vec.size());
254  return *m_elink_vec[i];
255 }
256 
259 {
260  assert (i < m_elink_vec.size());
261  return m_elink_vec[i];
262 }
TrigInDetTrackTruthMap::print
void print() const
Definition: TrigInDetTrackTruthMap.cxx:153
TrigInDetTrack::param
void param(const TrigInDetTrackFitPar *param)
Definition: TrigInDetTrack.h:126
TrigInDetTrackTruthMap::addMatch
void addMatch(const TrigInDetTrackCollection *trkColl, unsigned int trk_indx, TrigInDetTrackTruth &p_trk_tru)
accessors to fill map
Definition: TrigInDetTrackTruthMap.cxx:32
getMessageSvc.h
singleton-like access to IMessageSvc via open function and helper
TrigInDetTrackCollection
Definition: TrigInDetTrackCollection.h:13
TrigInDetTrackTruthMap::trackiLink
const ElementLink< TrigInDetTrackCollection > trackiLink(size_t i) const
Definition: TrigInDetTrackTruthMap.cxx:258
TrigInDetTrackTruthMap::tracki
const TrigInDetTrack * tracki(size_t i) const
Definition: TrigInDetTrackTruthMap.cxx:251
isValid
bool isValid(const T &p)
Definition: AtlasPID.h:225
Athena::getMessageSvc
IMessageSvc * getMessageSvc(bool quiet=false)
Definition: getMessageSvc.cxx:20
ElementLinkVector::push_back
void push_back(const ElemLink &link)
Definition: AthLinks/ElementLinkVector.h:316
TrigInDetTrack
Definition: TrigInDetTrack.h:34
TrigInDetTrackTruthMap::bestMatchTRT
const HepMcParticleLink * bestMatchTRT(const TrigInDetTrack *p_trig_trk) const
Definition: TrigInDetTrackTruthMap.cxx:134
TrigInDetTrackTruthMap::hasTruth
bool hasTruth(const TrigInDetTrack *p_trig_trk) const
methods to get truth-match objects
Definition: TrigInDetTrackTruthMap.cxx:58
TrigInDetTrackTruthMap::size
size_t size() const
Definition: TrigInDetTrackTruthMap.cxx:237
lumiFormat.i
int i
Definition: lumiFormat.py:85
endmsg
#define endmsg
Definition: AnalysisConfig_Ntuple.cxx:63
TrigInDetTrackTruthMap::m_elink_vec
ElementLinkVector< TrigInDetTrackCollection > m_elink_vec
Definition: TrigInDetTrackTruthMap.h:103
TrigInDetTrackTruth
Definition: TrigInDetTrackTruth.h:36
TrigInDetTrackTruthMap.h
TrigInDetTrackTruthMap::bestMatchSi
const HepMcParticleLink * bestMatchSi(const TrigInDetTrack *p_trig_trk) const
Definition: TrigInDetTrackTruthMap.cxx:114
DeMoScan.index
string index
Definition: DeMoScan.py:364
ElementLinkVector::size
size_type size() const
Definition: AthLinks/ElementLinkVector.h:292
TrigInDetTrackTruthMap::m_truth_vec
std::vector< TrigInDetTrackTruth > m_truth_vec
Definition: TrigInDetTrackTruthMap.h:104
CondAlgsOpts.found
int found
Definition: CondAlgsOpts.py:101
ElementLinkVector::back
const_reference back() const
Definition: AthLinks/ElementLinkVector.h:311
TrigInDetTrackTruthMap::bestMatchSiHits
int bestMatchSiHits(const TrigInDetTrack *p_trig_trk) const
Definition: TrigInDetTrackTruthMap.cxx:123
DEBUG
#define DEBUG
Definition: page_access.h:11
python.CaloCondTools.log
log
Definition: CaloCondTools.py:20
TrigInDetTrackTruthMap::truthi
const TrigInDetTrackTruth * truthi(size_t i) const
Definition: TrigInDetTrackTruthMap.cxx:244
TrigInDetTrackTruthMap::truth
const TrigInDetTrackTruth * truth(const TrigInDetTrack *p_trig_trk) const
Definition: TrigInDetTrackTruthMap.cxx:82
merge.status
status
Definition: merge.py:17
TrigInDetTrack::algorithmId
void algorithmId(const AlgoId id)
Definition: TrigInDetTrack.h:125
TrigInDetTrackTruthMap::bestMatchTRTHits
int bestMatchTRTHits(const TrigInDetTrack *p_trig_trk) const
Definition: TrigInDetTrackTruthMap.cxx:143