Back to home page

EIC code displayed by LXR

 
 

    


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

0001 // -*- C++ -*-
0002 //
0003 // Counter.hpp is a part of myStatistics
0004 // Copyright (C) 2012-2019 Simon Platzer, The Herwig Collaboration
0005 //
0006 // myStatistics is licenced under version 3 of the GPL, see COPYING for details.
0007 //
0008 #ifndef MYSTATISTICS_Counter_hpp_included
0009 #define MYSTATISTICS_Counter_hpp_included
0010 
0011 #include "Herwig/Utilities/XML/Element.h"
0012 
0013 namespace Statistics {
0014 
0015   /**
0016    * \brief A (weighted) counter
0017    * \author Simon Platzer
0018    */
0019   class Counter {
0020 
0021   public:
0022 
0023     /**
0024      * Construct a new counter.
0025      */
0026     Counter();
0027 
0028     /**
0029      * Destruct a counter.
0030      */
0031     virtual ~Counter();
0032 
0033   public:
0034 
0035     /**
0036      * Initialize this counter.
0037      */
0038     void initialize() {}
0039 
0040     /**
0041      * Reset this counter.
0042      */
0043     void reset() { *this = Counter(); }
0044 
0045     /**
0046      * Finalize this counter.
0047      */
0048     void finalize() { if ( isOpen() ) close(); }
0049 
0050   public:
0051 
0052     /**
0053      * Open the counter for the next event of the given id.
0054      */
0055     void open(size_t id);
0056 
0057     /**
0058      * Close the counter and update the overall statistics.
0059      */
0060     void close();
0061 
0062     /**
0063      * Book a contribution to the current event. Implies close() and
0064      * open(id) if the id is different from the currently considered
0065      * event id.
0066      */
0067     void count(double weight, size_t id);
0068 
0069   public:
0070 
0071     /**
0072      * Add a counter to this counter
0073      */
0074     Counter& operator+=(const Counter& other);
0075 
0076     /**
0077      * Subtract a counter from this counter
0078      */
0079     Counter& operator-=(const Counter& other);
0080 
0081   public:
0082 
0083     /**
0084      * Return the sum of weights
0085      */
0086     double sumOfWeights() const { return theSumOfWeights; }
0087 
0088     /**
0089      * Return the sum of squared weights
0090      */
0091     double sumOfSquaredWeights() const { return theSumOfSquaredWeights; }
0092 
0093     /**
0094      * Return the sum of weights before the next event occured
0095      */
0096     double sumOfEventWeights() const { return theSumOfEventWeights; }
0097 
0098     /**
0099      * Return the current event id
0100      */
0101     size_t eventId() const { return theEventId; }
0102 
0103     /**
0104      * Return true, if this counter is open
0105      */
0106     bool isOpen() const { return eventId() != 0; }
0107 
0108     /**
0109      * Return true, if this counter is closed
0110      */
0111     bool isClosed() const { return !isOpen(); }
0112 
0113   public:
0114 
0115     /**
0116      * Given a number of sampled points, return the average of this
0117      * counter
0118      */
0119     double average(double nPoints) const;
0120 
0121     /**
0122      * Given a number of sampled points, return the variance of the
0123      * average of this counter
0124      */
0125     double varianceOfAverage(double nPoints) const;
0126 
0127   public:
0128 
0129     /**
0130      * Fill counter data from an XML element
0131      */
0132     void fromXML(const XML::Element&);
0133 
0134     /**
0135      * Return an XML element for the data of this counter
0136      */
0137     XML::Element toXML() const;
0138 
0139   private:
0140 
0141     /**
0142      * The sum of weights counted
0143      */
0144     double theSumOfWeights;
0145 
0146     /**
0147      * The sum of squared weights counted
0148      */
0149     double theSumOfSquaredWeights;
0150 
0151     /**
0152      * The sum of weights counted before the next event occured;
0153      * within one event, all weights are considered to be 100%
0154      * correlated
0155      */
0156     double theSumOfEventWeights;
0157 
0158     /**
0159      * The current event id. The counter is closed if this is zero.
0160      */
0161     size_t theEventId;
0162 
0163   };
0164 
0165 }
0166 
0167 #endif // MYSTATISTICS_Counter_hpp_included