Back to home page

EIC code displayed by LXR

 
 

    


Warning, file /include/Geant4/G4RNGHelper.hh was not indexed or was modified since last indexation (in which case cross-reference links may be missing, inaccurate or erroneous).

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 // G4TemplateRNGHelper
0027 //
0028 // Class description:
0029 //
0030 // Helper class for RNG Engine seeds.
0031 // Used in MT builds to guarantee event reproducibility.
0032 // The function of this class is to return a RNG Engine seed given its index.
0033 // It is a simple templated container that allows to add seeds (AddOneSeed)
0034 // and retrieve a seed (GetSeed) by index.
0035 //
0036 // The normal use is with G4RNGHelper where each element of the container
0037 // represents a seed. To enforce strong-reproducibility the variant with
0038 // the RNG Engine status file names is available.
0039 
0040 // Author: A.Dotti (SLAC), 5 July 2013
0041 // --------------------------------------------------------------------
0042 #ifndef G4TemplateRNGHelper_hh
0043 #define G4TemplateRNGHelper_hh 1
0044 
0045 #include "globals.hh"
0046 
0047 #include <queue>
0048 #include <vector>
0049 
0050 template<class T>
0051 class G4TemplateRNGHelper
0052 {
0053   public:
0054     using SeedsQueue = std::vector<T>;
0055     using SeedsQueueSize_type = typename SeedsQueue::size_type;
0056 
0057   public:
0058     // The container is modeled as a (shared) singleton
0059     static G4TemplateRNGHelper<T>* GetInstance();
0060     static G4TemplateRNGHelper<T>* GetInstanceIfExist();
0061     virtual ~G4TemplateRNGHelper();
0062 
0063     // Returns seed given id
0064     virtual const T GetSeed(const G4int& sdId)
0065     {
0066       G4int seedId = sdId - 2 * offset;
0067       if (seedId < static_cast<G4int>(seeds.size())) {
0068         T& seed = seeds[seedId];
0069         return seed;
0070       }
0071       G4ExceptionDescription msg;
0072       msg << "No seed number " << seedId << "(" << seeds.size() << " available)\n"
0073           << " Original seed number " << sdId << " filled so far " << offset;
0074       G4Exception("G4RNGHelper::GetSeed", "Run0115", FatalException, msg);
0075       return T();
0076     }
0077 
0078     // Adds one seed to the collection
0079     void AddOneSeed(const T& seed) { seeds.push_back(seed); }
0080 
0081     // Fills N primary seed pairs
0082     void Fill(G4double* dbl, G4int nev, G4int nev_tot, G4int nrpe)
0083     {
0084       seeds.clear();
0085       for (G4int i = 0; i < nrpe * nev; ++i) {
0086         seeds.push_back((G4long)(100000000L * dbl[i]));
0087       }
0088       offset = 0;
0089       nev_filled = nev;
0090       nev_total = nev_tot;
0091       nRandParEvent = nrpe;
0092     }
0093 
0094     void Refill(G4double* dbl, G4int nev)
0095     {
0096       if (nev == 0) return;
0097       seeds.clear();
0098       for (G4int i = 0; i < nRandParEvent * nev; ++i) {
0099         seeds.push_back((G4long)(100000000L * dbl[i]));
0100       }
0101       offset += nev_filled;
0102       nev_filled = nev;
0103     }
0104 
0105     // Number of available seeds
0106     const SeedsQueueSize_type GetNumberSeeds() const { return seeds.size(); }
0107 
0108     // Empty the seeds container
0109     virtual void Clear() { seeds.clear(); }
0110 
0111   protected:
0112     SeedsQueue seeds;
0113     // Note: following numbers are number of events.
0114     //       seeds are generated for nRandParEvent times n_event
0115     G4int offset = 0;
0116     G4int nev_filled = 0;
0117     G4int nev_total = 0;
0118     G4int nRandParEvent = 0;
0119 
0120   private:
0121     G4TemplateRNGHelper() = default;
0122 
0123   private:
0124     static G4TemplateRNGHelper<T>* instance;
0125 };
0126 
0127 using G4RNGHelper = G4TemplateRNGHelper<G4long>;
0128 using G4StringRNGHelper = G4TemplateRNGHelper<G4String>;
0129 using G4SeedsQueue = std::queue<G4long>;
0130 
0131 #endif