Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-04-09 07:50:01

0001 #pragma once
0002 /**
0003 U4Random
0004 ==============
0005 
0006 Controlling the random sequence returned by G4UniformRand using 
0007 precooked array of randoms generated by curand 
0008 (based on extracts from cfg4/CAlignEngine.cc)
0009 
0010 Primary control *U4Random::setSequenceIndex* 
0011 to set the photon_id sequence of randoms to consume from  
0012 and then use *G4UniformRand* as normal which will internally use the 
0013 below *U4Random::flat* method while this is enabled.
0014 
0015 The idea is to control the random stream separately for each photon slot. 
0016 
0017 This was developed from examples/Geant4/CerenkovStandalone/OpticksRandom.hh
0018 
0019 **/
0020 
0021 #include <vector>
0022 #include <string>
0023 #include "plog/Severity.h"
0024 #include "CLHEP/Random/RandomEngine.h"
0025 #include "SRandom.h"
0026 #include "U4_API_EXPORT.hh"
0027 struct NP ; 
0028 
0029 struct U4_API U4Random : public CLHEP::HepRandomEngine, public SRandom 
0030 {
0031     friend struct U4RandomTest ; 
0032     static const plog::Severity LEVEL ; 
0033     static std::string Desc(); 
0034 
0035     //static constexpr const char* DEFAULT_SEQPATH = "${PrecookedDir:-$HOME/.opticks/precooked}/QSimTest/rng_sequence/rng_sequence_f_ni1000000_nj16_nk16_tranche100000" ;  
0036     static constexpr const char* DEFAULT_SEQPATH = "${PrecookedDir:-$HOME/.opticks/precooked}/QSimTest/rng_sequence/rng_sequence_f_ni1000000_nj16_nk16_tranche100000/rng_sequence_f_ni100000_nj16_nk16_ioffset000000.npy" ; 
0037 
0038     static constexpr const char* OPTICKS_RANDOM_SEQPATH = "OPTICKS_RANDOM_SEQPATH" ; 
0039     static const char* SeqPath(); 
0040 
0041     static constexpr const char* NOTES = R"LITERAL(
0042 U4Random::init : NOT READY ERROR
0043 =================================
0044 
0045 Instanciation of U4Random failed to load precooked randoms
0046 either from a single .npy or a directory of multiple .npy 
0047 for concatenation on loading. 
0048 
0049 Steps to fix:
0050 
0051 1. Check the value of envvar OPTICKS_RANDOM_SEQPATH points to precooked 
0052    randoms : either a single .npy or a directory containing one or more .npy
0053 
0054 2. Generate the precooked randoms with::
0055 
0056    cd ~/opticks/qudarap/tests 
0057    ./rng_sequence.sh run 
0058 
0059 3. run with envvars set to increase logging OR take actions on unclassified or selected stacks::
0060 
0061    export U4Random=INFO
0062    export U4Random_select_action=interrupt
0063 
0064 
0065 )LITERAL";
0066 
0067 
0068     static const char* NAME ; 
0069     static U4Random* INSTANCE ; 
0070     static U4Random* Get(); 
0071 
0072     static void      SetSequenceIndex(int index); 
0073     static int       GetSequenceIndex(); 
0074 
0075     const char*              m_seqpath_ ; 
0076     const char*              m_seqpath ; 
0077     bool                     m_seqpath_exists ; 
0078     const NP*                m_seq;  
0079     const float*             m_seq_values ; 
0080     int                      m_seq_ni ; 
0081     int                      m_seq_nv ; 
0082 
0083     int                      m_seq_index ; 
0084 
0085     NP*                      m_cur ; 
0086     int*                     m_cur_values ; 
0087     bool                     m_recycle ; 
0088 
0089     CLHEP::HepRandomEngine*  m_default ;
0090 
0091 
0092     const NP*                m_seqmask ; 
0093     int                      m_seqmask_ni ; 
0094     const size_t*            m_seqmask_values ; 
0095  
0096     //bool                     m_flat_debug ;   THIS HAS BECOME ESSENTIAL TO ALIGNMENT 
0097     double                   m_flat_prior ; 
0098     bool                     m_ready ; 
0099     std::vector<int>*        m_select ; 
0100     unsigned                 m_select_action ; 
0101 
0102     std::vector<int>         m_problem_idx ; 
0103 
0104 
0105     bool isSelect(int photon_idx, int flat_cursor) const ; 
0106     std::string descSelect(int photon_idx, int flat_cursor ) const; 
0107 
0108     static void SetSeed(long seed) ;  // non-zero seed required 
0109 
0110     static U4Random* Create(const char* seq_path=nullptr, const char* seqmask_path=nullptr); 
0111 
0112 private:
0113     U4Random(const char* seq_path=nullptr, const char* seqmask_path=nullptr); 
0114     void init() ;
0115 
0116 public: 
0117     bool isReady() const ; 
0118     std::string desc() const ; 
0119     std::string detail() const ; 
0120 
0121 
0122     virtual ~U4Random(); 
0123 
0124     size_t getNumIndices() const ;
0125     size_t getMaskedIndex(int index_);
0126     void setSequenceIndex(int index_);  
0127 #ifndef PRODUCTION
0128 #ifdef DEBUG_TAG
0129     void check_cursor_vs_tagslot(); 
0130 #endif
0131 #endif
0132     void saveProblemIdx(const char* fold) const ; 
0133 
0134     int  getSequenceIndex() const ;
0135 
0136     int    getFlatCursor() const ; 
0137     double getFlatPrior() const ; 
0138 
0139     //unsigned getFlatTag() ; 
0140 
0141 
0142     // mandatory CLHEP::HepRandomEngine methods
0143     double flat();
0144     void flatArray(const int size, double* vect);
0145     void setSeed(long seed, int);
0146     void setSeeds(const long * seeds, int); 
0147     void saveStatus( const char filename[] = "Config.conf") const ;
0148     void restoreStatus( const char filename[] = "Config.conf" ) ;
0149     void showStatus() const ;
0150     std::string name() const ;
0151 
0152     void dump(unsigned n=10); 
0153     // internals
0154     private:
0155         void enable(); 
0156         void disable(); 
0157 
0158 
0159 }; 
0160