PandaRoot
Counters.h
Go to the documentation of this file.
1 //****************************************************************************
2 //* This file is part of PandaRoot. *
3 //* *
4 //* PandaRoot is distributed under the terms of the *
5 //* GNU General Public License (GPL) version 3, *
6 //* copied verbatim in the file "LICENSE". *
7 //* *
8 //* Copyright (C) 2006 - 2024 FAIR GmbH and copyright holders of PandaRoot *
9 //* The copyright holders are listed in the file "COPYRIGHTHOLDERS". *
10 //* The authors are listed in the file "AUTHORS". *
11 //****************************************************************************
12 
17 #ifndef Counters_H
18 #define Counters_H
19 
20 #include <iostream>
21 using std::cout;
22 using std::endl;
23 using std::ios;
24 
25 #include <fstream>
26 #include <iomanip>
27 
28 #include <string>
29 using std::string;
30 
31 #include <vector>
32 using std::vector;
33 
34 #include <map>
35 using std::map;
36 
38 template <typename T>
39 struct TTracksCatCounters // counters for different tracks categories
40 {
41  int NCounters;
42 
43  vector<T> counters;
44 
45  TTracksCatCounters() : NCounters(0), counters() { counters.clear(); };
46  TTracksCatCounters(int nCounters) : NCounters(nCounters), counters() { counters.resize(NCounters, T(0)); };
47 
48  void AddCounter()
49  {
50  NCounters++;
51  counters.push_back(T(0));
52  };
53  void AddCounters(int nCounters)
54  {
55  NCounters += nCounters;
56  counters.resize(NCounters, T(0));
57  };
58 
60  {
61  if (NCounters != a.NCounters) {
62  cout << " TTracksCatCounters: Error. Addition of counters of different sizes: " << NCounters << " " << a.NCounters << endl;
63  } else {
64  for (int iC = 0; iC < NCounters; iC++) {
65  counters[iC] += a.counters[iC];
66  }
67  }
68  return *this;
69  };
70 
72  {
73  TTracksCatCounters res = *this;
74  res += a;
75  return res;
76  };
77 
78  template <typename T2>
80  {
81  TTracksCatCounters<double> b(NCounters);
82  if (NCounters != a.NCounters) {
83  cout << " TTracksCatCounters: Error. Addition of counters of different sizes: " << NCounters << " " << a.NCounters << endl;
84  } else {
85  for (int iC = 0; iC < NCounters; iC++) {
86  b.counters[iC] = Div(counters[iC], a.counters[iC]);
87  }
88  }
89  return b;
90  }
91 
92  template <typename T2>
94  {
95  TTracksCatCounters<T2> b(NCounters);
96  for (int iC = 0; iC < NCounters; iC++) {
97  b.counters[iC] = static_cast<T2>(Div(counters[iC], a));
98  }
99  return b;
100  }
101 
102  friend std::fstream &operator<<(std::fstream &strm, const TTracksCatCounters<T> &a)
103  {
104  strm << a.NCounters << " " << a.counters.size() << " ";
105  for (unsigned int iV = 0; iV < a.counters.size(); iV++)
106  strm << a.counters[iV] << " ";
107  strm << std::endl;
108  return strm;
109  }
110 
111  friend std::ostream &operator<<(std::ostream &strm, const TTracksCatCounters<T> &a)
112  {
113  strm << a.NCounters << " " << a.counters.size() << " ";
114  for (unsigned int iV = 0; iV < a.counters.size(); iV++)
115  strm << a.counters[iV] << " ";
116  strm << std::endl;
117  return strm;
118  }
119 
120  friend std::fstream &operator>>(std::fstream &strm, TTracksCatCounters<T> &a)
121  {
122  int tmp;
123  strm >> tmp;
124  a.NCounters = tmp;
125  strm >> tmp;
126  a.counters.resize(tmp, T(0));
127  for (int iV = 0; iV < tmp; iV++) {
128  T tmp1;
129  strm >> tmp1;
130  a.counters[iV] = tmp1;
131  }
132  return strm;
133  }
134 
135  private:
136  double Div(double a, double b) { return (b > 0) ? a / b : -1.; };
137 };
138 
139 struct TEfficiencies {
141  : ratio_ghosts(0), ratio_clones(0), ghosts(0), clones(0), nEvents(0){
142  // you should add counter with shortname="total" !!
143  };
144  virtual ~TEfficiencies(){};
145 
146  virtual void AddCounter(string shortname, string name);
147 
149  void CalcEff();
150  void Inc(bool isReco, string name); // increment counters according to parameters
151  void IncNEvents() { nEvents++; };
152  void Print();
153 
154  vector<string> names; // names counters indexed by index of counter
155  map<string, int> indices; // indices of counters indexed by a counter shortname
156 
157  TTracksCatCounters<double> ratio_reco;
158  double ratio_ghosts;
159  double ratio_clones;
160 
163  int ghosts;
164  int clones;
165  int nEvents;
166 };
167 
168 inline void TEfficiencies::AddCounter(string shortname, string name)
169 {
170  indices[shortname] = names.size();
171  names.push_back(name);
172 
173  ratio_reco.AddCounter();
174  mc.AddCounter();
175  reco.AddCounter();
176 }
177 
178 inline void TEfficiencies::CalcEff()
179 {
180  ratio_reco = reco / mc;
181  if (mc.counters[indices["total"]] > 0) {
182  ratio_clones = clones / double(mc.counters[indices["total"]]);
183  } else {
184  ratio_clones = -1;
185  }
186  if (reco.counters[indices["total"]] + ghosts > 0) {
187  ratio_ghosts = ghosts / double(reco.counters[indices["total"]] + ghosts);
188  } else {
189  ratio_ghosts = -1;
190  }
191 }
192 
194 {
195  mc += a.mc;
196  reco += a.reco;
197  ghosts += a.ghosts;
198  clones += a.clones;
199  nEvents += a.nEvents;
200 
201  return *this;
202 }
203 
204 inline void TEfficiencies::Inc(bool isReco, string name)
205 {
206  const int index = indices[name];
207 
208  mc.counters[index]++;
209  if (isReco)
210  reco.counters[index]++;
211 }
212 
213 inline void TEfficiencies::Print()
214 {
215 
216  // save original cout flags
217  std::ios_base::fmtflags coutFlags = cout.flags();
218 
219  cout.setf(ios::fixed);
220  cout.setf(ios::showpoint);
221  cout.precision(3);
222  cout << "Track category : "
223  << " Eff "
224  << " | "
225  << "All MC" << endl;
226 
227  int NCounters = mc.NCounters;
228  for (int iC = 0; iC < NCounters; iC++) {
229  cout << names[iC] << " : " << ratio_reco.counters[iC] << " | " << mc.counters[iC] << endl;
230  }
231 
232  cout << "Clone probability : " << ratio_clones << " | " << clones << endl;
233  cout << "Ghost probability : " << ratio_ghosts << " | " << ghosts << endl;
234 
235  // restore original cout flags
236  cout.flags(coutFlags);
237 }
238 
239 #endif
TTracksCatCounters(int nCounters)
Definition: Counters.h:46
TTracksCatCounters< double > operator/(TTracksCatCounters< T2 > &a)
Definition: Counters.h:79
counters used for efficiency calculation
TTracksCatCounters< T2 > operator/(double a)
Definition: Counters.h:93
friend std::fstream & operator>>(std::fstream &strm, TTracksCatCounters< T > &a)
Definition: Counters.h:120
void AddCounter()
Definition: Counters.h:48
void Inc(bool isReco, string name)
TTracksCatCounters & operator+=(TTracksCatCounters &a)
Definition: Counters.h:59
TTracksCatCounters operator+(TTracksCatCounters &a)
Definition: Counters.h:71
void AddCounters(int nCounters)
Definition: Counters.h:53
TTracksCatCounters< int > mc
void IncNEvents()
Definition: Counters.h:151
TTracksCatCounters< int > reco
TEfficiencies & operator+=(TEfficiencies &a)
virtual ~TEfficiencies()
Definition: Counters.h:144
virtual void AddCounter(string shortname, string name)