Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-08-06 09:24:23

0001 // -*- C++ -*-
0002 //
0003 // GeneralStatictis.h is a part of Herwig - A multi-purpose Monte Carlo event generator
0004 // Copyright (C) 2002-2019 The Herwig Collaboration
0005 //
0006 // Herwig is licenced under version 3 of the GPL, see COPYING for details.
0007 // Please respect the MCnet academic guidelines, see GUIDELINES for details.
0008 //
0009 #ifndef Herwig_GeneralStatistics_H
0010 #define Herwig_GeneralStatistics_H
0011 //
0012 // This is the declaration of the GeneralStatistics class.
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  * \ingroup Matchbox
0026  * \author Simon Platzer
0027  *
0028  * \brief General Monte Carlo statistics.
0029  *
0030  */
0031 class GeneralStatistics {
0032 
0033 public:
0034 
0035   /** @name Standard constructors and destructors. */
0036   //@{
0037   /**
0038    * The default constructor.
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    * The destructor.
0049    */
0050   virtual ~GeneralStatistics();
0051   //@}
0052 
0053 public:
0054 
0055   /**
0056    * Return the last calculated chi^2.
0057    */
0058   virtual double chi2() const { return 0.; }
0059 
0060   /**
0061    * Reset these statistics.
0062    */
0063   void reset() {
0064     *this = GeneralStatistics();
0065   }
0066 
0067 public:
0068 
0069   /**
0070    * Return the last weight encountered.
0071    */
0072   double lastWeight() const { return theLastWeight; }
0073 
0074   /**
0075    * Return the maximum absolute weight
0076    */
0077   double maxWeight() const { return theMaxWeight; }
0078 
0079   /**
0080    * Return the minimum absolute weight
0081    */
0082   double minWeight() const { return theMinWeight; }
0083 
0084   /**
0085    * Set the maximum absolute weight
0086    */
0087   void maxWeight(double w) { theMaxWeight = w; }
0088 
0089   /**
0090    * Set the minimum absolute weight
0091    */
0092   void minWeight(double w) { theMinWeight = w; }
0093 
0094   /**
0095    * Return the sum of weights
0096    */
0097   double sumWeights() const { return theSumWeights; }
0098 
0099   /**
0100    * Return the sum of squared weights 
0101    */
0102   double sumSquaredWeights() const { return theSumSquaredWeights; }
0103 
0104   /**
0105    * Return the sum of absolute weights 
0106    */
0107   double sumAbsWeights() const { return theSumAbsWeights; }
0108 
0109   /**
0110    * Return the number of selected points.
0111    */
0112   unsigned long selectedPoints() const { return theSelectedPoints; }
0113 
0114   /**
0115    * Return the nnumber of accepted points.
0116    */
0117   unsigned long acceptedPoints() const { return theAcceptedPoints; }
0118 
0119   /**
0120    * Return the number of points where a nan or inf weight has been
0121    * encountered.
0122    */
0123   unsigned long nanPoints() const { return theNanPoints; }
0124 
0125   /**
0126    * Return the number of all points.
0127    */
0128   unsigned long allPoints() const { return theAllPoints; }
0129 
0130   /**
0131    * Return the average weight.
0132    */
0133   virtual double averageWeight() const {
0134     return selectedPoints() > 0 ? sumWeights()/selectedPoints() : 0.;
0135   }
0136 
0137   /**
0138    * Return the average absolute weight.
0139    */
0140   virtual double averageAbsWeight() const {
0141     return selectedPoints() > 0 ? sumAbsWeights()/selectedPoints() : 0.;
0142   }
0143 
0144   /**
0145    * Return the variance of weights.
0146    */
0147   double weightVariance() const {
0148     return 
0149       selectedPoints() > 1 ? 
0150       abs(sumSquaredWeights() - sqr(sumWeights())/selectedPoints())/(selectedPoints()-1) : 0.;
0151   }
0152 
0153   /**
0154    * Return the variance of absolute weights.
0155    */
0156   double absWeightVariance() const {
0157     return 
0158       selectedPoints() > 1 ? 
0159       abs(sumSquaredWeights() - sqr(sumAbsWeights())/selectedPoints())/(selectedPoints()-1) : 0.;
0160   }
0161 
0162   /**
0163    * Return the variance of the average weight.
0164    */
0165   virtual double averageWeightVariance() const {
0166     return selectedPoints() > 1 ? weightVariance()/selectedPoints() : 0.;
0167   }
0168 
0169   /**
0170    * Return the variance of the average absolute weight.
0171    */
0172   virtual double averageAbsWeightVariance() const {
0173     return selectedPoints() > 1 ? absWeightVariance()/selectedPoints() : 0;
0174   }
0175 
0176   /**
0177    * Select an event
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    * Accept an event.
0200    */
0201   virtual void accept() {
0202     theAcceptedPoints += 1;
0203   }
0204 
0205   /**
0206    * Reject an event.
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   /** @name Functions used by the persistent I/O system. */
0225   //@{
0226   /**
0227    * Function used to write out object persistently.
0228    * @param os the persistent output stream written to.
0229    */
0230   void put(PersistentOStream & os) const;
0231 
0232   /**
0233    * Function used to read in object persistently.
0234    * @param is the persistent input stream read from.
0235    * @param version the version number of the object when written.
0236    */
0237   void get(PersistentIStream & is);
0238   //@}
0239 
0240   /**
0241    * Fill statistics data from an XML element
0242    */
0243   void fromXML(const XML::Element&);
0244 
0245   /**
0246    * Return an XML element for the data of this statistics
0247    */
0248   XML::Element toXML() const;
0249 
0250 private:
0251 
0252   /**
0253    * The maximum weight encountered.
0254    */
0255   double theMaxWeight;
0256 
0257   /**
0258    * The minimum weight encountered.
0259    */
0260   double theMinWeight;
0261 
0262   /**
0263    * The sum of weights.
0264    */
0265   double theSumWeights;
0266 
0267   /**
0268    * The sum of weights squared.
0269    */
0270   double theSumSquaredWeights;
0271 
0272   /**
0273    * The sum of absolute values of weights
0274    */
0275   double theSumAbsWeights;
0276 
0277   /**
0278    * The number of selected points
0279    */
0280   unsigned long theSelectedPoints;
0281 
0282   /**
0283    * The number of accepted points
0284    */
0285   unsigned long theAcceptedPoints;
0286 
0287   /**
0288    * The number of points where an nan or inf weight was encountered.
0289    */
0290   unsigned long theNanPoints;
0291 
0292   /**
0293    * The number of all points.
0294    */
0295   unsigned long theAllPoints;
0296 
0297   /**
0298    * The last weight encountered
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 /* Herwig_GeneralStatistics_H */