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