Geant4  10.03
G4RNGHelper.hh
Go to the documentation of this file.
1 //
2 // ********************************************************************
3 // * License and Disclaimer *
4 // * *
5 // * The Geant4 software is copyright of the Copyright Holders of *
6 // * the Geant4 Collaboration. It is provided under the terms and *
7 // * conditions of the Geant4 Software License, included in the file *
8 // * LICENSE and available at http://cern.ch/geant4/license . These *
9 // * include a list of copyright holders. *
10 // * *
11 // * Neither the authors of this software system, nor their employing *
12 // * institutes,nor the agencies providing financial support for this *
13 // * work make any representation or warranty, express or implied, *
14 // * regarding this software system or assume any liability for its *
15 // * use. Please see the license in the file LICENSE and URL above *
16 // * for the full disclaimer and the limitation of liability. *
17 // * *
18 // * This code implementation is the result of the scientific and *
19 // * technical work of the GEANT4 collaboration. *
20 // * By using, copying, modifying or distributing the software (or *
21 // * any work based on the software) you agree to acknowledge its *
22 // * use in resulting scientific publications, and indicate your *
23 // * acceptance of all terms of the Geant4 Software license. *
24 // ********************************************************************
25 //
26 //
27 // Helper class for RNG Engine seeds.
28 // Used in MT builds to guarantee reproducibility.
29 // The function of this class is to return a RNG Engine seed
30 // given its index. It is a simple templated container that
31 // Allows to add seeds (AddOneSeed) and retrieve a seed (GetSeed)
32 // by index.
33 //
34 // The normal use is with G4RNGHelper where each element of th econtainer
35 // represents a seed. To enforce strong-reproducibility the variant with
36 // the RNG Engine status file names is avilable
37 
38 #ifndef G4RNGHELPER_HH
39 #define G4RNGHELPER_HH
40 
41 #include "G4Types.hh"
42 #include <vector>
43 #include <queue>
44 #include "globals.hh"
45 
46 template <class T>
48 {
49  public:
50  // The container is modeled as a (shared) singleton
52  typedef std::vector<T> SeedsQueue;
53  typedef typename SeedsQueue::size_type SeedsQueueSize_type;
54 
56  {
57  Clear();
58  instance = 0;
59  }
60 
61  //Returns seed given id
62  virtual const T GetSeed(const G4int& sdId )
63  {
64  G4int seedId = sdId - 2*offset;
65  if ( seedId < static_cast<G4int>(seeds.size()) )
66  {
67  T& seed = seeds[seedId];
68  return seed;
69  }
71  msg << "No seed number "<<seedId<<"("<<seeds.size()<<" available)\n"
72  << " Original seed number "<<sdId<<" filled so far "<<offset;
73  G4Exception("G4RNGHelper::GetSeed","Run0115", FatalException,msg);
74  return T();
75  }
76 
77  //Adds one seed to the collection
78  void AddOneSeed( const T& seed ) { seeds.push_back(seed); }
79 
80  //Fills N primary seed pairs
81  void Fill(G4double* dbl,G4int nev,G4int nev_tot,G4int nrpe)
82  {
83  seeds.clear();
84  for(G4int i=0;i<nrpe*nev;i++)
85  { seeds.push_back((G4long)(100000000L*dbl[i])); }
86  offset = 0;
87  nev_filled = nev;
88  nev_total = nev_tot;
89  nRandParEvent = nrpe;
90  }
91 
92  void Refill(G4double* dbl, G4int nev)
93  {
94  if(nev==0) return;
95  seeds.clear();
96  for(G4int i=0;i<nRandParEvent*nev;i++)
97  { seeds.push_back((G4long)(100000000L*dbl[i])); }
98  offset += nev_filled;
99  nev_filled = nev;
100  }
101 
102  //Number of available seeds
103  const SeedsQueueSize_type GetNumberSeeds() const { return seeds.size(); }
104 
105  //Empty the seeds container
106  virtual void Clear() { seeds.clear(); }
107 
108  protected:
109  SeedsQueue seeds;
110  // Note: following numbers are number of events.
111  // seeds are generated for nRandParEvent times n_event
116 
117  private:
119  {
120  offset=0;
121  nev_filled=0;
122  nev_total=0;
123  nRandParEvent=0;
124  }
125 
126  private:
128 };
129 
132 typedef std::queue<G4long> G4SeedsQueue;
133 
134 #endif
std::queue< G4long > G4SeedsQueue
Definition: G4RNGHelper.hh:132
static G4TemplateRNGHelper< T > * instance
Definition: G4RNGHelper.hh:127
std::ostringstream G4ExceptionDescription
Definition: globals.hh:76
std::vector< T > SeedsQueue
Definition: G4RNGHelper.hh:52
void AddOneSeed(const T &seed)
Definition: G4RNGHelper.hh:78
long G4long
Definition: G4Types.hh:80
virtual const T GetSeed(const G4int &sdId)
Definition: G4RNGHelper.hh:62
G4TemplateRNGHelper< G4long > * instance
Definition: G4RNGHelper.cc:31
int G4int
Definition: G4Types.hh:78
void Refill(G4double *dbl, G4int nev)
Definition: G4RNGHelper.hh:92
void Fill(G4double *dbl, G4int nev, G4int nev_tot, G4int nrpe)
Definition: G4RNGHelper.hh:81
long seed
Definition: chem4.cc:68
virtual void Clear()
Definition: G4RNGHelper.hh:106
const SeedsQueueSize_type GetNumberSeeds() const
Definition: G4RNGHelper.hh:103
void G4Exception(const char *originOfException, const char *exceptionCode, G4ExceptionSeverity severity, const char *comments)
Definition: G4Exception.cc:41
G4TemplateRNGHelper< G4String > G4StringRNGHelper
Definition: G4RNGHelper.hh:131
static G4TemplateRNGHelper< T > * GetInstance()
Definition: G4RNGHelper.cc:37
SeedsQueue::size_type SeedsQueueSize_type
Definition: G4RNGHelper.hh:53
double G4double
Definition: G4Types.hh:76
static constexpr double L
Definition: G4SIunits.hh:124
virtual ~G4TemplateRNGHelper()
Definition: G4RNGHelper.hh:55
G4TemplateRNGHelper< G4long > G4RNGHelper
Definition: G4RNGHelper.hh:130