File indexing completed on 2025-08-03 08:20:16
0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015
0016
0017
0018
0019
0020
0021
0022 #ifndef KFMCCounter_H
0023 #define KFMCCounter_H
0024
0025 #include <iostream>
0026 #include <fstream>
0027 #include <vector>
0028
0029
0030
0031
0032
0033
0034
0035
0036
0037
0038 template <typename T>
0039 struct KFMCCounter
0040 {
0041 int NCounters;
0042
0043 std::vector<T> counters;
0044
0045 KFMCCounter():NCounters(0),counters(0) { }
0046 KFMCCounter(int nCounters):NCounters(nCounters), counters(nCounters,T(0)) { }
0047
0048 void AddCounter(){ NCounters++; counters.push_back(T(0)); }
0049 void AddCounters(int nCounters){ NCounters += nCounters; counters.resize( NCounters, T(0)); }
0050
0051
0052 KFMCCounter& operator+=(KFMCCounter& a){
0053 if (NCounters != a.NCounters){
0054 std::cout << " KFMCCounter: Error. Addition of counters of different sizes: " << NCounters << " " << a.NCounters << std::endl;
0055 }
0056 else{
0057 for (int iC = 0; iC < NCounters; iC++){
0058 counters[iC] += a.counters[iC];
0059 }
0060 }
0061 return *this;
0062 };
0063
0064 KFMCCounter operator+(KFMCCounter& a){
0065 KFMCCounter res = *this;
0066 res += a;
0067 return res;
0068 };
0069
0070 template <typename T2>
0071 KFMCCounter<double> operator/(KFMCCounter<T2>& a){
0072 KFMCCounter<double> b(NCounters);
0073 if (NCounters != a.NCounters){
0074 std::cout << " KFMCCounter: Error. Addition of counters of different sizes: " << NCounters << " " << a.NCounters << std::endl;
0075 }
0076 else{
0077 for (int iC = 0; iC < NCounters; iC++){
0078 b.counters[iC] = Div(counters[iC],a.counters[iC]);
0079 }
0080 }
0081 return b;
0082 }
0083
0084 template <typename T2>
0085 KFMCCounter<T2> operator/(double a){
0086 KFMCCounter<T2> b(NCounters);
0087 for (int iC = 0; iC < NCounters; iC++){
0088 b.counters[iC] = (T2)Div(counters[iC],a);
0089 }
0090 return b;
0091 }
0092
0093 friend std::fstream & operator<<(std::fstream &strm, const KFMCCounter<T> &a ){
0094 strm << a.NCounters << " " << a.counters.size() << " ";
0095 for(unsigned int iV=0; iV<a.counters.size(); iV++)
0096 strm << a.counters[iV] << " ";
0097 strm << std::endl;
0098 return strm;
0099 }
0100
0101 friend std::ostream & operator<<(std::ostream &strm, const KFMCCounter<T> &a ){
0102 strm << a.NCounters << " " << a.counters.size() << " ";
0103 for(unsigned int iV=0; iV<a.counters.size(); iV++)
0104 strm << a.counters[iV] << " ";
0105 strm << std::endl;
0106 return strm;
0107 }
0108
0109 friend std::fstream & operator>>(std::fstream &strm, KFMCCounter<T> &a ){
0110 int tmp;
0111 strm >> tmp;
0112 a.NCounters = tmp;
0113 strm >> tmp;
0114 a.counters.resize(tmp,T(0));
0115 for(int iV=0; iV<tmp; iV++)
0116 {
0117 T tmp1;
0118 strm >> tmp1;
0119 a.counters[iV] = tmp1;
0120 }
0121 return strm;
0122 }
0123
0124 private:
0125
0126 double Div(double a, double b){return (b > 0) ? a/b : -1.;}
0127 };
0128
0129 #endif