Belle II Software  release-08-01-10
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(nullptr)
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 == nullptr) {
54  _pairs = new const pair<unsigned, unsigned>* [n];
55  map<unsigned, unsigned>::const_iterator it = _relations.begin();
56  unsigned i = 0;
57  while (it != _relations.end()) {
58  _pairs[i] = new const pair<unsigned, unsigned>(it->first, it->second);
59  ++it;
60  ++i;
61  }
62 
63  //...Sorting...
64  for (unsigned k = 0; k < n - 1; k++) {
65  for (unsigned j = i + 1; j < n; j++) {
66  if (_pairs[k]->second < _pairs[j]->second) {
67  const pair<unsigned, unsigned>* tmp = _pairs[k];
68  _pairs[k] = _pairs[j];
69  _pairs[j] = tmp;
70  }
71  }
72  }
73  }
74 
75  return _pairs[a]->first;
76  }
77 
78  const MCParticle&
79  TRGCDCRelation::mcParticle(unsigned a) const
80  {
81  const unsigned id = contributor(a);
82  StoreArray<MCParticle> mcParticles;
83  const unsigned nMcParticles = mcParticles.getEntries();
84  if (nMcParticles == 0) cout << "[Error] TRGCDCRelation::mcParticle() => There are no mc particles in MCParticle store array." <<
85  endl;
86  return * mcParticles[id];
87  }
88 
89  float
90  TRGCDCRelation::purity(unsigned a) const
91  {
92 
93  //...Get target trkID...
94  const unsigned trkID = contributor(a);
95 
96  //...Count # of hits...
97  unsigned n = 0;
98  unsigned na = 0;
99  map<unsigned, unsigned>::const_iterator it = _relations.begin();
100  while (it != _relations.end()) {
101  n += it->second;
102  if (it->first == trkID)
103  na = it->second;
104  ++it;
105  }
106 
107  return float(na) / float(n);
108  }
109 
110  float
111  TRGCDCRelation::purity3D(unsigned trkID) const
112  {
113 
114  //...Count # of hits...
115  unsigned n = 0;
116  unsigned na = 0;
117  map<unsigned, unsigned>::const_iterator it = _relations.begin();
118  while (it != _relations.end()) {
119  n += it->second;
120  if (it->first == trkID)
121  na = it->second;
122  ++it;
123  }
124 
125  return float(na) / float(n);
126  }
127 
128  float
129  TRGCDCRelation::efficiency3D(unsigned trkID, std::map<unsigned, unsigned>& numTSsParticle) const
130  {
131 
132  // # of found TSs/ # of true TSs
133  unsigned nFoundTS = 0;
134  unsigned nTrueTS = 0;
135 
136  // Find number of found TSs. One for each layer.
137  for (unsigned iStereoSuperLayer = 0; iStereoSuperLayer < 4; iStereoSuperLayer++) {
138  if (((this->track()).links(2 * iStereoSuperLayer + 1)).size() > 0) nFoundTS += 1;
139  }
140 
141  // Find number of true TSs.
142  map<unsigned, unsigned>::iterator itTSF = numTSsParticle.find(trkID);
143  if (itTSF != numTSsParticle.end()) nTrueTS = itTSF->second;
144  else {
145  //cout<<"[Error] TRGCDCRelation::efficiency3D. No mc stereo TSs for track."<<endl;
146  return -1;
147  }
148 
149  //cout<<"[JB] Found TS: "<< nFoundTS <<" nTrue TS: " << nTrueTS << endl;
150 
151  return float(nFoundTS) / float(nTrueTS);
152 
153  }
154 
155  void
156  TRGCDCRelation::dump(const std::string&,
157  const std::string& prefix) const
158  {
159 
160  const unsigned n = nContributors();
161  cout << prefix << "#contributions=" << n << endl;
162  const string tab = prefix + " ";
163  for (unsigned i = 0; i < n; i++) {
164  cout << tab << i << ":MCTrkID=" << contributor(i)
165  << ",purity=" << purity(i) * 100 << "%"
166  << " [PDG=" << mcParticle(i).getPDG() << "]" << endl;
167  }
168  }
169 
171 } // 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:129
const MCParticle & mcParticle(unsigned i=0) const
returns i'th contributor.
Definition: Relation.cc:79
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:156
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:90
float purity3D(unsigned trkID) const
returns purity for 3D for trkID particle which should be from 2D.
Definition: Relation.cc:111
Abstract base class for different kinds of events.