Geant4  10.01.p01
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 private:
50 public:
51  // The container is modeled as a (shared) singleton
53  if (!instance)
54  {
55  instance = new G4TemplateRNGHelper();
56  }
57  return instance;
58  }
59  typedef std::vector<T> SeedsQueue;
60  typedef typename SeedsQueue::size_type SeedsQueueSize_type;
61 
62 private:
64  {
65  offset=0;
66  nev_filled=0;
67  nev_total=0;
68  nRandParEvent=0;
69  }
70 
71 public:
73  Clear();
74  instance = 0;
75  }
76 
77  //Returns seed given id
78  virtual const T GetSeed(const G4int& sdId ) {
79  G4int seedId = sdId - 2*offset;
80  if ( seedId < static_cast<G4int>(seeds.size()) )
81  {
82  T& seed = seeds[seedId];
83  return seed;
84  }
86  msg << "No seed number "<<seedId<<"("<<seeds.size()<<" available)\n"
87  << " Original seed number "<<sdId<<" filled so far "<<offset;
88  G4Exception("G4RNGHelper::GetSeed","Run0035", FatalException,msg);
89  return T();
90  }
91 
92  //Adds one seed to the collection
93  void AddOneSeed( const T& seed ) { seeds.push_back(seed); }
94 
95  //Fills N primary seed pairs
96  void Fill(double* dbl,int nev,int nev_tot,int nrpe)
97  {
98  seeds.clear();
99  for(int i=0;i<nrpe*nev;i++)
100  { seeds.push_back((long)(100000000L*dbl[i])); }
101  offset = 0;
102  nev_filled = nev;
103  nev_total = nev_tot;
104  nRandParEvent = nrpe;
105  }
106 
107  void Refill(double* dbl, int nev)
108  {
109  if(nev==0) return;
110  seeds.clear();
111  for(int i=0;i<nRandParEvent*nev;i++)
112  { seeds.push_back((long)(100000000L*dbl[i])); }
113  offset += nev_filled;
114  nev_filled = nev;
115  }
116 
117  //Number of available seeds
118  const SeedsQueueSize_type GetNumberSeeds() const { return seeds.size(); }
119 
120  //Empty the seeds container
121  virtual void Clear() { seeds.clear(); }
122 protected:
123  SeedsQueue seeds;
124  // Note: following numbers are number of events.
125  // seeds are generated for nRandParEvent times n_event
126  int offset;
130 };
131 
134 typedef std::queue<long> G4SeedsQueue;
135 
136 #endif
static G4TemplateRNGHelper * GetInstance()
Definition: G4RNGHelper.hh:52
void Fill(double *dbl, int nev, int nev_tot, int nrpe)
Definition: G4RNGHelper.hh:96
std::ostringstream G4ExceptionDescription
Definition: globals.hh:76
std::vector< T > SeedsQueue
Definition: G4RNGHelper.hh:59
void AddOneSeed(const T &seed)
Definition: G4RNGHelper.hh:93
virtual const T GetSeed(const G4int &sdId)
Definition: G4RNGHelper.hh:78
int G4int
Definition: G4Types.hh:78
G4TemplateRNGHelper< long > G4RNGHelper
Definition: G4RNGHelper.hh:132
std::queue< long > G4SeedsQueue
Definition: G4RNGHelper.hh:134
static const G4int L[nN]
void Refill(double *dbl, int nev)
Definition: G4RNGHelper.hh:107
virtual void Clear()
Definition: G4RNGHelper.hh:121
const SeedsQueueSize_type GetNumberSeeds() const
Definition: G4RNGHelper.hh:118
G4TemplateRNGHelper< long > * instance
Definition: G4RNGHelper.cc:31
void G4Exception(const char *originOfException, const char *exceptionCode, G4ExceptionSeverity severity, const char *comments)
Definition: G4Exception.cc:41
static G4TemplateRNGHelper * instance
Definition: G4RNGHelper.hh:49
G4TemplateRNGHelper< G4String > G4StringRNGHelper
Definition: G4RNGHelper.hh:133
SeedsQueue::size_type SeedsQueueSize_type
Definition: G4RNGHelper.hh:60
virtual ~G4TemplateRNGHelper()
Definition: G4RNGHelper.hh:72