File indexing completed on 2026-08-06 09:24:23
0001
0002
0003
0004
0005
0006
0007
0008
0009 #ifndef Herwig_GeneralStatistics_H
0010 #define Herwig_GeneralStatistics_H
0011
0012
0013
0014
0015 #include "ThePEG/Persistency/PersistentOStream.h"
0016 #include "ThePEG/Persistency/PersistentIStream.h"
0017
0018 #include "Herwig/Utilities/XML/Element.h"
0019
0020 namespace Herwig {
0021
0022 using namespace ThePEG;
0023
0024
0025
0026
0027
0028
0029
0030
0031 class GeneralStatistics {
0032
0033 public:
0034
0035
0036
0037
0038
0039
0040 GeneralStatistics()
0041 : theMaxWeight(0.), theMinWeight(Constants::MaxDouble),
0042 theSumWeights(0.), theSumSquaredWeights(0.), theSumAbsWeights(0.),
0043 theSelectedPoints(0), theAcceptedPoints(0),
0044 theNanPoints(0), theAllPoints(0),
0045 theLastWeight(0.) {}
0046
0047
0048
0049
0050 virtual ~GeneralStatistics();
0051
0052
0053 public:
0054
0055
0056
0057
0058 virtual double chi2() const { return 0.; }
0059
0060
0061
0062
0063 void reset() {
0064 *this = GeneralStatistics();
0065 }
0066
0067 public:
0068
0069
0070
0071
0072 double lastWeight() const { return theLastWeight; }
0073
0074
0075
0076
0077 double maxWeight() const { return theMaxWeight; }
0078
0079
0080
0081
0082 double minWeight() const { return theMinWeight; }
0083
0084
0085
0086
0087 void maxWeight(double w) { theMaxWeight = w; }
0088
0089
0090
0091
0092 void minWeight(double w) { theMinWeight = w; }
0093
0094
0095
0096
0097 double sumWeights() const { return theSumWeights; }
0098
0099
0100
0101
0102 double sumSquaredWeights() const { return theSumSquaredWeights; }
0103
0104
0105
0106
0107 double sumAbsWeights() const { return theSumAbsWeights; }
0108
0109
0110
0111
0112 unsigned long selectedPoints() const { return theSelectedPoints; }
0113
0114
0115
0116
0117 unsigned long acceptedPoints() const { return theAcceptedPoints; }
0118
0119
0120
0121
0122
0123 unsigned long nanPoints() const { return theNanPoints; }
0124
0125
0126
0127
0128 unsigned long allPoints() const { return theAllPoints; }
0129
0130
0131
0132
0133 virtual double averageWeight() const {
0134 return selectedPoints() > 0 ? sumWeights()/selectedPoints() : 0.;
0135 }
0136
0137
0138
0139
0140 virtual double averageAbsWeight() const {
0141 return selectedPoints() > 0 ? sumAbsWeights()/selectedPoints() : 0.;
0142 }
0143
0144
0145
0146
0147 double weightVariance() const {
0148 return
0149 selectedPoints() > 1 ?
0150 abs(sumSquaredWeights() - sqr(sumWeights())/selectedPoints())/(selectedPoints()-1) : 0.;
0151 }
0152
0153
0154
0155
0156 double absWeightVariance() const {
0157 return
0158 selectedPoints() > 1 ?
0159 abs(sumSquaredWeights() - sqr(sumAbsWeights())/selectedPoints())/(selectedPoints()-1) : 0.;
0160 }
0161
0162
0163
0164
0165 virtual double averageWeightVariance() const {
0166 return selectedPoints() > 1 ? weightVariance()/selectedPoints() : 0.;
0167 }
0168
0169
0170
0171
0172 virtual double averageAbsWeightVariance() const {
0173 return selectedPoints() > 1 ? absWeightVariance()/selectedPoints() : 0;
0174 }
0175
0176
0177
0178
0179 virtual void select(double weight, bool doIntegral = true) {
0180 if ( ! isfinite(weight) ) {
0181 theLastWeight = weight;
0182 theNanPoints += 1;
0183 theAllPoints += 1;
0184 return;
0185 }
0186 theLastWeight = weight;
0187 theMaxWeight = max(theMaxWeight,abs(weight));
0188 theMinWeight = min(theMinWeight,abs(weight));
0189 if ( !doIntegral )
0190 return;
0191 theSumWeights += weight;
0192 theSumSquaredWeights += sqr(weight);
0193 theSumAbsWeights += abs(weight);
0194 theSelectedPoints += 1;
0195 theAllPoints += 1;
0196 }
0197
0198
0199
0200
0201 virtual void accept() {
0202 theAcceptedPoints += 1;
0203 }
0204
0205
0206
0207
0208 virtual void reject() {
0209 if ( ! isfinite(lastWeight()) ) {
0210 theNanPoints -= 1;
0211 theAllPoints -= 1;
0212 return;
0213 }
0214 theSumWeights -= lastWeight();
0215 theSumSquaredWeights -= sqr(lastWeight());
0216 theSumAbsWeights -= abs(lastWeight());
0217 theSelectedPoints -= 1;
0218 theAcceptedPoints -= 1;
0219 theAllPoints -= 1;
0220 }
0221
0222 public:
0223
0224
0225
0226
0227
0228
0229
0230 void put(PersistentOStream & os) const;
0231
0232
0233
0234
0235
0236
0237 void get(PersistentIStream & is);
0238
0239
0240
0241
0242
0243 void fromXML(const XML::Element&);
0244
0245
0246
0247
0248 XML::Element toXML() const;
0249
0250 private:
0251
0252
0253
0254
0255 double theMaxWeight;
0256
0257
0258
0259
0260 double theMinWeight;
0261
0262
0263
0264
0265 double theSumWeights;
0266
0267
0268
0269
0270 double theSumSquaredWeights;
0271
0272
0273
0274
0275 double theSumAbsWeights;
0276
0277
0278
0279
0280 unsigned long theSelectedPoints;
0281
0282
0283
0284
0285 unsigned long theAcceptedPoints;
0286
0287
0288
0289
0290 unsigned long theNanPoints;
0291
0292
0293
0294
0295 unsigned long theAllPoints;
0296
0297
0298
0299
0300 double theLastWeight;
0301
0302 };
0303
0304 inline PersistentOStream& operator<<(PersistentOStream& os, const GeneralStatistics& s) {
0305 s.put(os); return os;
0306 }
0307
0308 inline PersistentIStream& operator>>(PersistentIStream& is, GeneralStatistics& s) {
0309 s.get(is); return is;
0310 }
0311
0312 }
0313
0314 #endif