Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-18 09:59:06

0001 //
0002 // ********************************************************************
0003 // * License and Disclaimer                                           *
0004 // *                                                                  *
0005 // * The  Geant4 software  is  copyright of the Copyright Holders  of *
0006 // * the Geant4 Collaboration.  It is provided  under  the terms  and *
0007 // * conditions of the Geant4 Software License,  included in the file *
0008 // * LICENSE and available at  http://cern.ch/geant4/license .  These *
0009 // * include a list of copyright holders.                             *
0010 // *                                                                  *
0011 // * Neither the authors of this software system, nor their employing *
0012 // * institutes,nor the agencies providing financial support for this *
0013 // * work  make  any representation or  warranty, express or implied, *
0014 // * regarding  this  software system or assume any liability for its *
0015 // * use.  Please see the license in the file  LICENSE  and URL above *
0016 // * for the full disclaimer and the limitation of liability.         *
0017 // *                                                                  *
0018 // * This  code  implementation is the result of  the  scientific and *
0019 // * technical work of the GEANT4 collaboration.                      *
0020 // * By using,  copying,  modifying or  distributing the software (or *
0021 // * any work based  on the software)  you  agree  to acknowledge its *
0022 // * use  in  resulting  scientific  publications,  and indicate your *
0023 // * acceptance of all terms of the Geant4 Software license.          *
0024 // ********************************************************************
0025 //
0026 // G4StatAnalysis
0027 //
0028 // Class description:
0029 //
0030 //  Class for statistical analysis of random variable
0031 //
0032 //  Adapted from:
0033 //  Lux, I.
0034 //      Monte Carlo particle transport methods: neutron and photon
0035 //      calculations/authors, Ivan Lux and Laszlo Koblinger.
0036 //      ISBN 0-8493-6074-9
0037 //  1. Neutron transport theory. 2. Photon transport theory.
0038 //  3. Monte Carlo method. I. Koblinger, Laszlo. II. Title.
0039 //     QC793.5.N4628L88 1990 530.1 '38'20
0040 
0041 // Author: J.Madsen, 25.10.2018
0042 // --------------------------------------------------------------------
0043 #ifndef G4StatAnalysis_hh
0044 #define G4StatAnalysis_hh 1
0045 
0046 #include <cmath>
0047 #include <fstream>
0048 #include <iomanip>
0049 #include <iostream>
0050 #include <limits>
0051 #include <optional>
0052 
0053 #include "globals.hh"
0054 #include "tls.hh"
0055 
0056 #include "G4Allocator.hh"
0057 #include "G4Timer.hh"
0058 #include "G4Types.hh"
0059 #include "G4ios.hh"
0060 
0061 class G4StatAnalysis
0062 {
0063  public:
0064   inline G4StatAnalysis();
0065   inline ~G4StatAnalysis() {}
0066 
0067   // Accumulated values
0068   inline G4double GetMean() const;
0069   inline const G4double& GetSum() const;
0070   inline const G4double& GetSumSquared() const;
0071   inline const G4double& GetSum1() const;
0072   inline const G4double& GetSum2() const;
0073   inline const G4int& GetHits() const;
0074   inline G4int GetNumNonZero() const;
0075   inline G4int GetNumZero() const;
0076 
0077   // Some control over accumulated variables
0078   inline void SetSum(const G4double& val);
0079   inline void SetSumSquared(const G4double& val);
0080   inline void SetSum1(const G4double& val);
0081   inline void SetSum2(const G4double& val);
0082   inline void SetHits(const G4int& val);
0083   inline void SetZero(const G4int& val);
0084 
0085   // Computed values
0086   inline G4double GetFOM() const;
0087   inline G4double GetRelativeError() const;
0088   inline G4double GetStdDev() const;
0089   inline G4double GetVariance() const;
0090   inline G4double GetCoeffVariation() const;
0091   inline G4double GetEfficiency() const;
0092   inline G4double GetR2Int() const;
0093   inline G4double GetR2Eff() const;
0094 
0095   // Conversion
0096   inline operator G4double() const;
0097 
0098   // Modifications
0099   inline void Reset();
0100   inline void Add(const G4double& _val, const G4double& _weight = 1.0);
0101   inline void Rescale(const G4double& factor);
0102 
0103   // Output
0104   inline void PrintInfo(std::ostream& os, const std::string& = "") const;
0105 
0106   // Operators
0107   inline G4StatAnalysis& operator+=(const G4double& _val);
0108   inline G4StatAnalysis& operator/=(const G4double& _val);
0109   inline G4StatAnalysis& operator+=(const G4StatAnalysis&);
0110   inline G4StatAnalysis& operator-=(const G4StatAnalysis&);
0111 
0112   // Allocators
0113   inline void* operator new(std::size_t);
0114   inline void operator delete(void*);
0115 
0116   // Timing (member functions)
0117   inline G4double GetCpuTime() const;
0118   // Timing (static functions)
0119   static tms* GetCpuClock()
0120   {
0121     G4ThreadLocalStatic std::optional<tms> _instance(std::nullopt);
0122     if(_instance == std::nullopt)
0123     {
0124       _instance = tms();
0125       times(&_instance.value());
0126     }
0127     return &_instance.value();
0128   }
0129   // Note: this above implementation was implemented in such a way as to
0130   // conserve memory by eliminated every instance from requiring their own
0131   // timing variables. The ResetCpuClock function below is called at the
0132   // beginning of the run (G4Run constructor) to attempt to ensure the
0133   // FOM is not skewed by multiple runs -- it may be necessary to
0134   // manually invoke in some situations
0135   static void ResetCpuClock()
0136   {
0137     tms* _clock = GetCpuClock();
0138     times(_clock);
0139   }
0140 
0141   // friend operator for output
0142   friend std::ostream& operator<<(std::ostream& os, const G4StatAnalysis& obj)
0143   {
0144     obj.PrintInfo(os);
0145     return os;
0146   }
0147   // friend operator for addition
0148   friend const G4StatAnalysis operator+(const G4StatAnalysis& lhs,
0149                                         const G4StatAnalysis& rhs)
0150   {
0151     return G4StatAnalysis(lhs) += rhs;
0152   }
0153   // friend operator for subtraction
0154   friend const G4StatAnalysis operator-(const G4StatAnalysis& lhs,
0155                                         const G4StatAnalysis& rhs)
0156   {
0157     return G4StatAnalysis(lhs) -= rhs;
0158   }
0159 
0160  private:
0161   G4double fSum1 = 0.0;  // summation of each history^1
0162   G4double fSum2 = 0.0;  // summation from each history^2
0163   G4int fHits    = 0;    // number of scoring histories
0164   G4int fZero    = 0;    // number of histories that were not greater than 0.0
0165 };
0166 
0167 #include "G4StatAnalysis.icc"
0168 
0169 #endif