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