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