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