Belle II Software  release-05-01-25
Relation.cc
1 //-----------------------------------------------------------------------------
2 // $Id$
3 //-----------------------------------------------------------------------------
4 // Filename : Relation.cc
5 // Section : TrackBase
6 // Owner : Yoshihito Iwasaki
7 // Email : yoshihito.iwasaki@kek.jp
8 //-----------------------------------------------------------------------------
9 // Description : A class to represent relations between TrackBase and MC track
10 //-----------------------------------------------------------------------------
11 // $Log$
12 //-----------------------------------------------------------------------------
13 
14 #define TRGCDC_SHORT_NAMES
15 
16 #include "framework/datastore/StoreArray.h"
17 #include "mdst/dataobjects/MCParticle.h"
18 #include "trg/cdc/Relation.h"
19 
20 using namespace std;
21 
22 namespace Belle2 {
28  TRGCDCRelation::TRGCDCRelation(const TRGCDCTrackBase& track,
29  const std::map<unsigned, unsigned>& relation)
30  : _track(track),
31  _relations(relation),
32  _pairs(0)
33  {
34  }
35 
37  {
38  delete [] _pairs;
39  }
40 
41  unsigned
42  TRGCDCRelation::contributor(unsigned a) const
43  {
44 
45  const unsigned n = _relations.size();
46 
47  if (n == 0) {
48  return 99999;
49  } else if (n == 1) {
50  return _relations.begin()->first;
51  }
52 
53  //...Preparation...
54  if (_pairs == 0) {
55  _pairs = (const pair<unsigned, unsigned>**)
56  malloc(sizeof(pair<unsigned, unsigned>) * n);
57  map<unsigned, unsigned>::const_iterator it = _relations.begin();
58  unsigned i = 0;
59  while (it != _relations.end()) {
60  _pairs[i] =
61  new const pair<unsigned, unsigned>(it->first, it->second);
62  ++it;
63  ++i;
64  }
65 
66  //...Sorting...
67  for (unsigned k = 0; k < n - 1; k++) {
68  for (unsigned j = i + 1; j < n; j++) {
69  if (_pairs[k]->second < _pairs[j]->second) {
70  const pair<unsigned, unsigned>* tmp = _pairs[k];
71  _pairs[k] = _pairs[j];
72  _pairs[j] = tmp;
73  }
74  }
75  }
76  }
77 
78  return _pairs[a]->first;
79  }
80 
81  const MCParticle&
82  TRGCDCRelation::mcParticle(unsigned a) const
83  {
84  const unsigned id = contributor(a);
85  StoreArray<MCParticle> mcParticles;
86  const unsigned nMcParticles = mcParticles.getEntries();
87  if (nMcParticles == 0) cout << "[Error] TRGCDCRelation::mcParticle() => There are no mc particles in MCParticle store array." <<
88  endl;
89  return * mcParticles[id];
90  }
91 
92  float
93  TRGCDCRelation::purity(unsigned a) const
94  {
95 
96  //...Get target trkID...
97  const unsigned trkID = contributor(a);
98 
99  //...Count # of hits...
100  unsigned n = 0;
101  unsigned na = 0;
102  map<unsigned, unsigned>::const_iterator it = _relations.begin();
103  while (it != _relations.end()) {
104  n += it->second;
105  if (it->first == trkID)
106  na = it->second;
107  ++it;
108  }
109 
110  return float(na) / float(n);
111  }
112 
113  float
114  TRGCDCRelation::purity3D(unsigned trkID) const
115  {
116 
117  //...Count # of hits...
118  unsigned n = 0;
119  unsigned na = 0;
120  map<unsigned, unsigned>::const_iterator it = _relations.begin();
121  while (it != _relations.end()) {
122  n += it->second;
123  if (it->first == trkID)
124  na = it->second;
125  ++it;
126  }
127 
128  return float(na) / float(n);
129  }
130 
131  float
132  TRGCDCRelation::efficiency3D(unsigned trkID, std::map<unsigned, unsigned>& numTSsParticle) const
133  {
134 
135  // # of found TSs/ # of true TSs
136  unsigned nFoundTS = 0;
137  unsigned nTrueTS = 0;
138 
139  // Find number of found TSs. One for each layer.
140  for (unsigned iStereoSuperLayer = 0; iStereoSuperLayer < 4; iStereoSuperLayer++) {
141  if (((this->track()).links(2 * iStereoSuperLayer + 1)).size() > 0) nFoundTS += 1;
142  }
143 
144  // Find number of true TSs.
145  map<unsigned, unsigned>::iterator itTSF = numTSsParticle.find(trkID);
146  if (itTSF != numTSsParticle.end()) nTrueTS = itTSF->second;
147  else {
148  //cout<<"[Error] TRGCDCRelation::efficiency3D. No mc stereo TSs for track."<<endl;
149  return -1;
150  }
151 
152  //cout<<"[JB] Found TS: "<< nFoundTS <<" nTrue TS: " << nTrueTS << endl;
153 
154  return float(nFoundTS) / float(nTrueTS);
155 
156  }
157 
158  void
159  TRGCDCRelation::dump(const std::string&,
160  const std::string& prefix) const
161  {
162 
163  const unsigned n = nContributors();
164  cout << prefix << "#contributions=" << n << endl;
165  const string tab = prefix + " ";
166  for (unsigned i = 0; i < n; i++) {
167  cout << tab << i << ":MCTrkID=" << contributor(i)
168  << ",purity=" << purity(i) * 100 << "%"
169  << " [PDG=" << mcParticle(i).getPDG() << "]" << endl;
170  }
171  }
172 
174 } // namespace Belle2
Belle2::TRGCDCRelation::dump
void dump(const std::string &message=std::string(""), const std::string &prefix=std::string("")) const
Dumps debug information.
Definition: Relation.cc:159
Belle2::TRGCDCRelation::track
const TRGCDCTrackBase & track(void) const
returns a track.
Definition: Relation.h:91
Belle2::TRGCDCRelation::~TRGCDCRelation
virtual ~TRGCDCRelation()
Destructor.
Definition: Relation.cc:36
Belle2
Abstract base class for different kinds of events.
Definition: MillepedeAlgorithm.h:19
Belle2::MCParticle::getPDG
int getPDG() const
Return PDG code of particle.
Definition: MCParticle.h:123
Belle2::TRGCDCRelation::purity
float purity(unsigned i=0) const
returns i'th purity.
Definition: Relation.cc:93
Belle2::TRGCDCRelation::contributor
unsigned contributor(unsigned i=0) const
returns i'th contributor of MCParticle.
Definition: Relation.cc:42
Belle2::TRGCDCRelation::nContributors
unsigned nContributors(void) const
returns /# of contributors.
Definition: Relation.h:98
Belle2::TRGCDCRelation::purity3D
float purity3D(unsigned trkID) const
returns purity for 3D for trkID particle which should be from 2D.
Definition: Relation.cc:114
Belle2::TRGCDCTrackBase
A class to represent a track object in TRGCDC.
Definition: TrackBase.h:41
Belle2::MCParticle
A Class to store the Monte Carlo particle information.
Definition: MCParticle.h:43
Belle2::StoreArray< MCParticle >
Belle2::TRGCDCRelation::_pairs
const std::pair< unsigned, unsigned > ** _pairs
Pairs.
Definition: Relation.h:84
Belle2::TRGCDCRelation::_relations
const std::map< unsigned, unsigned > _relations
Map.
Definition: Relation.h:81
Belle2::StoreArray::getEntries
int getEntries() const
Get the number of objects in the array.
Definition: StoreArray.h:226
Belle2::TRGCDCRelation::efficiency3D
float efficiency3D(unsigned trkID, std::map< unsigned, unsigned > &numTSsParticle) const
returns efficiency of TS for 3D
Definition: Relation.cc:132
Belle2::TRGCDCRelation::mcParticle
const MCParticle & mcParticle(unsigned i=0) const
returns i'th contributor.
Definition: Relation.cc:82