CbmRoot
Loading...
Searching...
No Matches
CbmL1Counters.h
Go to the documentation of this file.
1/* Copyright (C) 2010-2017 Frankfurt Institute for Advanced Studies, Goethe-Universität Frankfurt, Frankfurt
2 SPDX-License-Identifier: GPL-3.0-only
3 Authors: Igor Kulakov [committer], Maksym Zyzak */
4
5#ifndef CbmL1Counters_H
6#define CbmL1Counters_H
7
8#include "CaVector.h"
9#include "TString.h"
10
11#include <Logger.h>
12
13#include <fstream>
14#include <iomanip>
15#include <map>
16#include <sstream>
17
18namespace
19{
20 namespace cacore = cbm::algo::ca;
21}
22
24template<typename T>
25struct TL1TracksCatCounters // counters for different tracks categories
26{
27 TL1TracksCatCounters() { counters.reserve(20); }
28
29 TL1TracksCatCounters(int nCounters) { counters.reset(nCounters, T(0)); };
30
31 int GetNcounters() const { return counters.size(); }
32
33 void AddCounter() { counters.push_back_no_warning(T(0)); };
34
35 void AddCounters(int nCounters) { counters.enlarge(GetNcounters() + nCounters, T(0)); };
36
38 {
39 if (GetNcounters() != a.GetNcounters()) {
40 LOG(error) << " TL1TracksCatCounters: Error. Addition of counters of "
41 "different sizes: "
42 << GetNcounters() << " " << a.GetNcounters();
43 }
44 else {
45 for (int iC = 0; iC < GetNcounters(); iC++) {
46 counters[iC] += a.counters[iC];
47 }
48 }
49 return *this;
50 };
51
53 {
54 TL1TracksCatCounters res = *this;
55 res += a;
56 return res;
57 };
58
59 template<typename T2>
61 {
63 if (GetNcounters() != a.GetNcounters()) {
64 LOG(error) << " TL1TracksCatCounters: Error. Addition of counters of "
65 "different sizes: "
66 << GetNcounters() << " " << a.GetNcounters();
67 }
68 else {
69 for (int iC = 0; iC < GetNcounters(); iC++) {
70 b.counters[iC] = Div(counters[iC], a.counters[iC]);
71 }
72 }
73 return b;
74 };
75
76 template<typename T2>
78 {
80 for (int iC = 0; iC < GetNcounters(); iC++) {
81 b.counters[iC] = static_cast<T2>(Div(counters[iC], a));
82 }
83 return b;
84 };
85
86 friend std::fstream& operator<<(std::fstream& strm, const TL1TracksCatCounters<T>& a)
87 {
88 strm << a.GetNcounters() << " " << a.counters.size() << " ";
89 for (unsigned int iV = 0; iV < a.counters.size(); iV++)
90 strm << a.counters[iV] << " ";
91 strm << std::endl;
92 return strm;
93 }
94
95 friend std::ostream& operator<<(std::ostream& strm, const TL1TracksCatCounters<T>& a)
96 {
97 strm << a.counters.size() << " ";
98 for (unsigned int iV = 0; iV < a.counters.size(); iV++)
99 strm << a.counters[iV] << " ";
100 strm << std::endl;
101 return strm;
102 }
103
104 friend std::fstream& operator>>(std::fstream& strm, TL1TracksCatCounters<T>& a)
105 {
106 int tmp;
107 strm >> tmp;
108 a.counters.clear();
109 a.counters.reset(tmp, T(0));
110 for (int iV = 0; iV < tmp; iV++) {
111 T tmp1;
112 strm >> tmp1;
113 a.counters[iV] = tmp1;
114 }
115 return strm;
116 }
117
118 private:
119 double Div(double a, double b) { return (b > 0) ? a / b : -1.; };
120
121 public:
122 cacore::Vector<T> counters{"TL1TracksCatCounters::counters"};
123};
124
127 : names()
128 , indices()
129 , ratio_reco()
130 , ratio_ghosts(0)
131 , ratio_clones(0)
132 , mc()
133 , reco()
134 , ghosts(0)
135 , clones(0)
136 , nEvents(0){
137 // you should add counter with shortname="total" !!
138 };
139
140 virtual ~TL1Efficiencies(){};
141
142 virtual void AddCounter(const TString& shortname, const TString& name);
143
145 void CalcEff();
146 void Inc(bool isReco,
147 const TString& name); // increment counters according to parameters
148 void IncNEvents() { nEvents++; };
149
150 std::string ToString() const;
151
152
153 std::vector<TString> names; // names counters indexed by index of counter
154 std::map<TString, int> indices; // indices of counters indexed by a counter shortname
155
159
165};
166
167inline void TL1Efficiencies::AddCounter(const TString& shortname, const TString& name)
168{
169 indices[shortname] = names.size();
170 names.push_back(name);
171
173 mc.AddCounter();
175}
176
178{
179 ratio_reco = reco / mc;
180 const double total = reco.counters[indices["total"]] + ghosts + clones;
181 if (total > 0) {
182 ratio_clones = clones / total;
183 ratio_ghosts = ghosts / total;
184 }
185 else {
186 ratio_clones = -1;
187 ratio_ghosts = -1;
188 }
189};
190
192{
193 mc += a.mc;
194 reco += a.reco;
195 ghosts += a.ghosts;
196 clones += a.clones;
197 nEvents += a.nEvents;
198
199 return *this;
200};
201
202inline void TL1Efficiencies::Inc(bool isReco, const TString& name)
203{
204 const int index = indices[name];
205
206 mc.counters[index]++;
207 if (isReco) reco.counters[index]++;
208};
209
210inline std::string TL1Efficiencies::ToString() const
211{
212 std::stringstream ss;
213 ss.setf(std::ios::fixed);
214 ss.setf(std::ios::showpoint);
215 ss.precision(3);
216 ss.setf(std::ios::right);
217 ss << "Track category : "
218 << " Eff "
219 << " | "
220 << "All MC"
221 << "\n";
222
223 int NCounters = mc.GetNcounters();
224 for (int iC = 0; iC < NCounters; iC++) {
225 ss << names[iC] << " : " << ratio_reco.counters[iC] << " | " << mc.counters[iC] << "\n";
226 }
227
228 ss << "Clone probability : " << ratio_clones << " | " << clones << "\n";
229 ss << "Ghost probability : " << ratio_ghosts << " | " << ghosts << "\n";
230 return ss.str();
231};
232
233#endif
TODO: SZh 8.11.2022: add selection of parameterisation.
Definition CaBranch.h:14
virtual void AddCounter(const TString &shortname, const TString &name)
std::map< TString, int > indices
void Inc(bool isReco, const TString &name)
TL1TracksCatCounters< int > reco
std::vector< TString > names
TL1TracksCatCounters< int > mc
TL1Efficiencies & operator+=(TL1Efficiencies &a)
std::string ToString() const
virtual ~TL1Efficiencies()
TL1TracksCatCounters< double > ratio_reco
counters used for efficiency calculation
TL1TracksCatCounters operator+(TL1TracksCatCounters &a)
TL1TracksCatCounters & operator+=(TL1TracksCatCounters &a)
void AddCounters(int nCounters)
int GetNcounters() const
double Div(double a, double b)
friend std::fstream & operator<<(std::fstream &strm, const TL1TracksCatCounters< T > &a)
TL1TracksCatCounters< double > operator/(TL1TracksCatCounters< T2 > &a)
cacore::Vector< T > counters
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)