PandaRoot
PndCACountersBase.h
Go to the documentation of this file.
1 
5 #ifndef PNDCACOUNTERSBASE_H
6 #define PNDCACOUNTERSBASE_H
7 
8 #include <iostream>
9 using std::cout;
10 using std::endl;
11 using std::ios;
12 
13 #include <fstream>
14 #include <iomanip>
15 
16 #include <string>
17 using std::string;
18 
19 #include <vector>
20 using std::vector;
21 
22 #include <map>
23 using std::map;
24 
26 template <typename T>
27 struct TTracksCatCounters // counters for different tracks categories
28 {
29  int NCounters;
30 
31  vector<T> counters;
32 
33  TTracksCatCounters() : NCounters(0), counters() { counters.clear(); };
34  TTracksCatCounters(int nCounters) : NCounters(nCounters), counters() { counters.resize(NCounters, T(0)); };
35 
36  void AddCounter()
37  {
38  NCounters++;
39  counters.push_back(T(0));
40  };
41  void AddCounters(int nCounters)
42  {
43  NCounters += nCounters;
44  counters.resize(NCounters, T(0));
45  };
46 
48  {
49  if (NCounters != a.NCounters) {
50  cout << " TTracksCatCounters: Error. Addition of counters of different sizes: " << NCounters << " " << a.NCounters << endl;
51  } else {
52  for (int iC = 0; iC < NCounters; iC++) {
53  counters[iC] += a.counters[iC];
54  }
55  }
56  return *this;
57  };
58 
60  {
61  TTracksCatCounters res = *this;
62  res += a;
63  return res;
64  };
65 
66  template <typename T2>
68  {
69  TTracksCatCounters<double> b(NCounters);
70  if (NCounters != a.NCounters) {
71  cout << " TTracksCatCounters: Error. Addition of counters of different sizes: " << NCounters << " " << a.NCounters << endl;
72  } else {
73  for (int iC = 0; iC < NCounters; iC++) {
74  b.counters[iC] = Div(counters[iC], a.counters[iC]);
75  }
76  }
77  return b;
78  }
79 
80  template <typename T2>
82  {
83  TTracksCatCounters<T2> b(NCounters);
84  for (int iC = 0; iC < NCounters; iC++) {
85  b.counters[iC] = static_cast<T2>(Div(counters[iC], a));
86  }
87  return b;
88  }
89 
90  friend std::fstream &operator<<(std::fstream &strm, const TTracksCatCounters<T> &a)
91  {
92  strm << a.NCounters << " " << a.counters.size() << " ";
93  for (unsigned int iV = 0; iV < a.counters.size(); iV++)
94  strm << a.counters[iV] << " ";
95  strm << std::endl;
96  return strm;
97  }
98 
99  friend std::ostream &operator<<(std::ostream &strm, const TTracksCatCounters<T> &a)
100  {
101  strm << a.NCounters << " " << a.counters.size() << " ";
102  for (unsigned int iV = 0; iV < a.counters.size(); iV++)
103  strm << a.counters[iV] << " ";
104  strm << std::endl;
105  return strm;
106  }
107 
108  friend std::fstream &operator>>(std::fstream &strm, TTracksCatCounters<T> &a)
109  {
110  int tmp;
111  strm >> tmp;
112  a.NCounters = tmp;
113  strm >> tmp;
114  a.counters.resize(tmp, T(0));
115  for (int iV = 0; iV < tmp; iV++) {
116  T tmp1;
117  strm >> tmp1;
118  a.counters[iV] = tmp1;
119  }
120  return strm;
121  }
122 
123  private:
124  double Div(double a, double b) { return (b > 0) ? a / b : -1.; };
125 };
126 
129  : ratio_ghosts(0), ratio_clones(0), ghosts(0), clones(0), nEvents(0){
130  // you should add counter with shortname="total" !!
131  };
132  virtual ~TEfficiencies(){};
133 
134  virtual void AddCounter(string shortname, string name);
135 
137  void CalcEff();
138  void Inc(bool isReco, string name); // increment counters according to parameters
139  void IncNEvents() { nEvents++; };
140  void Print();
141 
142  vector<string> names; // names counters indexed by index of counter
143  map<string, int> indices; // indices of counters indexed by a counter shortname
144 
146  double ratio_ghosts;
147  double ratio_clones;
148 
151  int ghosts;
152  int clones;
153  int nEvents;
154 };
155 
156 inline void TEfficiencies::AddCounter(string shortname, string name)
157 {
158  indices[shortname] = names.size();
159  names.push_back(name);
160 
161  ratio_reco.AddCounter();
162  mc.AddCounter();
163  reco.AddCounter();
164 }
165 
167 {
168  ratio_reco = reco / mc;
169  if (mc.counters[indices["total"]] > 0) {
170  ratio_clones = clones / double(mc.counters[indices["total"]]);
171  } else {
172  ratio_clones = -1;
173  }
174  if (reco.counters[indices["total"]] + ghosts > 0) {
175  ratio_ghosts = ghosts / double(reco.counters[indices["total"]] + ghosts);
176  } else {
177  ratio_ghosts = -1;
178  }
179 }
180 
182 {
183  mc += a.mc;
184  reco += a.reco;
185  ghosts += a.ghosts;
186  clones += a.clones;
187  nEvents += a.nEvents;
188 
189  return *this;
190 }
191 
192 inline void TEfficiencies::Inc(bool isReco, string name)
193 {
194  const int index = indices[name];
195 
196  mc.counters[index]++;
197  if (isReco)
198  reco.counters[index]++;
199 }
200 
201 inline void TEfficiencies::Print()
202 {
203  cout.setf(ios::fixed);
204  cout.setf(ios::showpoint);
205  cout.precision(3);
206  cout << "Track category : "
207  << " Eff "
208  << " | "
209  << "All MC" << endl;
210 
211  int NCounters = mc.NCounters;
212  for (int iC = 0; iC < NCounters; iC++) {
213  cout << names[iC] << " : " << ratio_reco.counters[iC] << " | " << mc.counters[iC] << endl;
214  }
215 
216  cout << "Clone probability : " << ratio_clones << " | " << clones << endl;
217  cout << "Ghost probability : " << ratio_ghosts << " | " << ghosts << endl;
218 }
219 
220 #endif
TTracksCatCounters(int nCounters)
TTracksCatCounters< double > operator/(TTracksCatCounters< T2 > &a)
counters used for efficiency calculation
TTracksCatCounters< T2 > operator/(double a)
friend std::fstream & operator>>(std::fstream &strm, TTracksCatCounters< T > &a)
void Inc(bool isReco, string name)
TTracksCatCounters & operator+=(TTracksCatCounters &a)
map< string, int > indices
TTracksCatCounters< double > ratio_reco
TTracksCatCounters operator+(TTracksCatCounters &a)
void AddCounters(int nCounters)
TTracksCatCounters< int > mc
vector< string > names
TTracksCatCounters< int > reco
TEfficiencies & operator+=(TEfficiencies &a)
virtual ~TEfficiencies()
virtual void AddCounter(string shortname, string name)