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