00001 #ifndef ANP_ALGDATA_H
00002 #define ANP_ALGDATA_H
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012 #include <algorithm>
00013 #include <cmath>
00014 #include <iomanip>
00015 #include <iostream>
00016 #include <vector>
00017
00018
00019 #include "Handle.h"
00020 #include "Record.h"
00021
00022 namespace Anp
00023 {
00024 template<class T>
00025 class AlgCaddy
00026 {
00027 public:
00028
00029 AlgCaddy();
00030 AlgCaddy(Handle<T> alg_, const std::string &name_);
00031 ~AlgCaddy();
00032
00033 void AllEvent(double weight);
00034 void OutEvent(double weight);
00035
00036 void AllSnarl(const Record &record);
00037 void OutSnarl(const Record &record);
00038
00039 unsigned int Width(unsigned int number) const;
00040
00041 void Print(unsigned int nw, unsigned int cw, const std::string &opt, std::ostream& o = std::cout) const;
00042
00043 public:
00044
00045 Handle<T> alg;
00046
00047 std::string name;
00048
00049 unsigned int NSnarlAll;
00050 unsigned int NSnarlOut;
00051
00052 unsigned int NEventAll;
00053 unsigned int NEventOut;
00054
00055 double WEventAll;
00056 double WEventOut;
00057 };
00058
00059 template<class T>
00060 unsigned int NameWidth(const std::vector<AlgCaddy<T> > &vec)
00061 {
00062 unsigned int width = 0;
00063 for(typename std::vector<AlgCaddy<T> >::const_iterator it = vec.begin(); it != vec.end(); ++it)
00064 {
00065 width = std::max<unsigned int>(width, (it -> name).size());
00066 }
00067
00068 return width;
00069 }
00070
00071 template<class T>
00072 unsigned int NumbWidth(const std::vector<AlgCaddy<T> > &vec)
00073 {
00074 unsigned int width = 0;
00075 for(typename std::vector<AlgCaddy<T> >::const_iterator it = vec.begin(); it != vec.end(); ++it)
00076 {
00077 width = std::max<unsigned int>(width, it -> Width(it -> NSnarlAll));
00078 width = std::max<unsigned int>(width, it -> Width(it -> NSnarlOut));
00079 width = std::max<unsigned int>(width, it -> Width(it -> NEventAll));
00080 width = std::max<unsigned int>(width, it -> Width(it -> NEventAll));
00081 width = std::max<unsigned int>(width, it -> Width(static_cast<unsigned int>(it -> WEventAll)));
00082 width = std::max<unsigned int>(width, it -> Width(static_cast<unsigned int>(it -> WEventOut)));
00083 }
00084
00085 return width;
00086 }
00087
00088
00089
00090
00091
00092 template<class T>
00093 AlgCaddy<T>::AlgCaddy()
00094 : alg(0), name(),
00095 NSnarlAll(0), NSnarlOut(0), NEventAll(0), NEventOut(0), WEventAll(0.0), WEventOut(0.0)
00096 {
00097 }
00098
00099 template<class T>
00100 AlgCaddy<T>::AlgCaddy(Handle<T> alg_, const std::string &name_)
00101 : alg(alg_), name(name_),
00102 NSnarlAll(0), NSnarlOut(0), NEventAll(0), NEventOut(0), WEventAll(0.0), WEventOut(0.0)
00103 {
00104 }
00105
00106 template<class T>
00107 AlgCaddy<T>::~AlgCaddy()
00108 {
00109 }
00110
00111 template<class T>
00112 void AlgCaddy<T>::AllEvent(const double weight)
00113 {
00114 ++NEventAll;
00115 WEventAll += weight;
00116 }
00117
00118 template<class T>
00119 void AlgCaddy<T>::OutEvent(const double weight)
00120 {
00121 ++NEventOut;
00122 WEventOut += weight;
00123 }
00124
00125 template<class T>
00126 void AlgCaddy<T>::AllSnarl(const Record &record)
00127 {
00128 ++NSnarlAll;
00129 for(EventIter ievent = record.EventBeg(); ievent != record.EventEnd(); ++ievent)
00130 {
00131 ++NEventAll;
00132 WEventAll += ievent -> Weight();
00133 }
00134 }
00135
00136 template<class T>
00137 void AlgCaddy<T>::OutSnarl(const Record &record)
00138 {
00139 ++NSnarlOut;
00140 for(EventIter ievent = record.EventBeg(); ievent != record.EventEnd(); ++ievent)
00141 {
00142 ++NEventOut;
00143 WEventOut += ievent -> Weight();
00144 }
00145 }
00146
00147 template<class T>
00148 unsigned int AlgCaddy<T>::Width(const unsigned int number) const
00149 {
00150 if(number > 0)
00151 {
00152 return static_cast<unsigned int>(1 + std::log10(double(number)));
00153 }
00154
00155 return 1;
00156 }
00157
00158 template<class T>
00159 void AlgCaddy<T>::Print(unsigned int nw, unsigned int cw, const std::string &opt, std::ostream& o) const
00160 {
00161 if(nw < name.size())
00162 {
00163 nw = name.size();
00164 }
00165 if(cw < 1)
00166 {
00167 cw = std::max<unsigned int>(cw, Width(NSnarlAll));
00168 cw = std::max<unsigned int>(cw, Width(NSnarlOut));
00169 cw = std::max<unsigned int>(cw, Width(NEventAll));
00170 cw = std::max<unsigned int>(cw, Width(NEventOut));
00171 }
00172
00173 if(opt.find("snarl") != std::string::npos)
00174 {
00175 if(nw > 0)
00176 {
00177 o << std::setfill(' ') << std::setw(nw) << std::left << name << " ";
00178 }
00179
00180 o << " snarl pass/total = ";
00181 o << std::setfill(' ') << std::setw(cw) << std::right << NSnarlOut << "/";
00182 o << std::setfill(' ') << std::setw(cw) << std::right << NSnarlAll;
00183
00184 if(NSnarlAll > 0)
00185 {
00186 o << " = " << std::setfill(' ')
00187 << std::setprecision(5) << std::showpoint << double(NSnarlOut)/double(NSnarlAll);
00188 }
00189 o << std::noshowpoint << std::endl;
00190 }
00191
00192 if(opt.find("event") != std::string::npos)
00193 {
00194 if(nw > 0)
00195 {
00196 o << std::setfill(' ') << std::setw(nw) << std::left << name << " ";
00197 }
00198
00199 o << " event pass/total = ";
00200 o << std::setfill(' ') << std::setw(cw) << std::right << NEventOut << "/";
00201 o << std::setfill(' ') << std::setw(cw) << std::right << NEventAll;
00202
00203 if(NEventAll > 0)
00204 {
00205 o << " = " << std::setfill(' ')
00206 << std::setprecision(5) << std::showpoint << double(NEventOut)/double(NEventAll);
00207 }
00208 o << std::noshowpoint << std::endl;
00209 }
00210
00211 if(opt.find("weight") != std::string::npos)
00212 {
00213 if(nw > 0)
00214 {
00215 o << std::setfill(' ') << std::setw(nw) << std::left << name << " ";
00216 }
00217
00218 o << "weight pass/total = "
00219 << std::setfill(' ') << std::setw(cw) << std::right << static_cast<long int>(WEventOut) << "/"
00220 << std::setfill(' ') << std::setw(cw) << std::right << static_cast<long int>(WEventAll);
00221
00222 if(WEventAll > 0.0)
00223 {
00224 o << " = " << std::setfill(' ')
00225 << std::setprecision(5) << std::showpoint << WEventOut/WEventAll;
00226 }
00227 o << std::noshowpoint << std::endl;
00228 }
00229 }
00230 }
00231
00232 #endif