Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-04-09 07:49:55

0001 #pragma once
0002 /**
0003 SUniformRand.h
0004 =================
0005 
0006 This adapts u4/U4UniformRand.h to try to get rid of Geant4 dependency. 
0007 
0008 As this is headeronly and can benefit from a static UU 
0009 include this header into a compilation unit with::
0010 
0011     #include "SUniformRand.h"
0012     NP* SUniformRand::UU = nullptr ;
0013 
0014 And where appropriate set the UU to a reference array of randoms. 
0015 
0016 
0017 Headers for random setup:
0018 
0019 Randomize.hh
0020     Geant4 level setup that includes Randomize.h and does::
0021 
0022         #define G4UniformRand() CLHEP::HepRandom::getTheEngine()->flat()
0023 
0024 Randomize.h
0025     CLHEP level setup
0026 
0027 Random.h
0028     CLHEP::HepRandom class with static method CLHEP::HepRandom::getTheEngine()
0029 
0030 
0031 g4-cls Randomize
0032 g4-cls Random
0033 
0034 
0035 
0036 **/
0037 
0038 #include <string>
0039 #include <iomanip>
0040 #include "Randomize.hh"
0041 #include "NPX.h"
0042 
0043 template<typename E>
0044 struct SUniformRand
0045 {
0046     // static NP* UU ;  // often causes symbol issues, maybe easier to just pass SEvt::UU as argument 
0047     static constexpr const double EPSILON = 1e-6 ; 
0048     static std::string Desc(int n=10); 
0049     static void Get(std::vector<double>& uu); 
0050     static NP* Get(int n=1000); 
0051     static int Find(double u, const NP* uu ) ; 
0052     static std::string Desc(double u, const NP* uu ) ; 
0053 }; 
0054 
0055 
0056 template<typename E>
0057 inline void SUniformRand<E>::Get(std::vector<double>& uu ) // static
0058 {
0059     unsigned n = uu.size(); 
0060     for(unsigned i=0 ; i < n ; i++) uu[i] = E::getTheEngine()->flat() ; 
0061 }
0062 
0063 template<typename E>
0064 inline std::string SUniformRand<E>::Desc(int n )
0065 {
0066     std::vector<double> uu(n) ; 
0067     Get(uu); 
0068     std::stringstream ss ; 
0069     ss << "U4UniformRand::Desc" << std::endl ; 
0070     for(int i=0 ; i < n ; i++)
0071     {
0072         ss << std::setw(6) << i 
0073            << " " << std::setw(10) << std::fixed << std::setprecision(5) << uu[i] 
0074            << std::endl 
0075            ;
0076     }
0077     std::string s = ss.str(); 
0078     return s; 
0079 }
0080 
0081 
0082 template<typename E>
0083 inline NP* SUniformRand<E>::Get(int n)
0084 {
0085     std::vector<double> uu(n) ; 
0086     Get(uu); 
0087     return NPX::Make<double>(uu) ; 
0088 }
0089 
0090 template<typename E>
0091 inline int SUniformRand<E>::Find(double u, const NP* uu)
0092 {
0093     return uu ? uu->find_value_index(u, EPSILON) : -2 ; 
0094 }
0095 
0096 template<typename E>
0097 inline std::string SUniformRand<E>::Desc(double u, const NP* uu)
0098 {
0099     std::stringstream ss ; 
0100     ss << "UU[" 
0101        << std::setw(7) << std::fixed << std::setprecision(5) << u 
0102        << " " 
0103        << std::setw(6) << Find(u, uu) 
0104        << "]"
0105        ;  
0106     std::string s = ss.str(); 
0107     return s; 
0108 }
0109