Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-04-10 07:50:35

0001 #pragma once
0002 /**
0003 U4UniformRand.h
0004 =================
0005 
0006 As this is headeronly and can benefit from a static UU 
0007 include this header into a compilation unit with::
0008 
0009     #include "U4UniformRand.h"
0010     NP* U4UniformRand::UU = nullptr ;
0011 
0012 And where appropriate set the UU to a reference array of randoms. 
0013 
0014 
0015 Headers for random setup:
0016 
0017 Randomize.hh
0018     Geant4 level setup that includes Randomize.h and does::
0019 
0020         #define G4UniformRand() CLHEP::HepRandom::getTheEngine()->flat()
0021 Randomize.h
0022     CLHEP level setup
0023 
0024 
0025 Tried to avoid duplication between this and SUniformRand.h using::
0026 
0027     #include "Randomize.hh"
0028     typedef SUniformRand<CLHEP::HepRandom> U4UniformRand ;
0029 
0030 It kinda works, see sysrap/tests/SUniformRand_test.cc, but runs 
0031 into duplicate symbol issues.  So are simply duplicating. 
0032 
0033 Actually maybe easier to get rid of U4UniformRand::UU and use some 
0034 another symbol like SEvt::UU (or SLOG::UU) to hold the randoms as 
0035 that can always be made accessible : and avoid duplicate symbols
0036 troubles ?
0037  
0038 **/
0039 
0040 #include <string>
0041 #include <iomanip>
0042 #include "Randomize.hh"
0043 #include "NPX.h"
0044 
0045 struct U4UniformRand
0046 {
0047     static NP* UU ; 
0048     static constexpr const double EPSILON = 1e-6 ; 
0049 
0050     static double Get(); 
0051     static double Burn(int n); 
0052 
0053     static void Get(std::vector<double>& uu); 
0054     static std::string Desc(int n=10); 
0055 
0056     static NP* Get(int n); 
0057     static int Find(double u, const NP* uu=UU ) ; 
0058     static std::string Desc(double u, const NP* uu=UU ) ; 
0059 }; 
0060 
0061 inline double U4UniformRand::Get() // static
0062 {
0063     return G4UniformRand();  // ONLY dependency on Geant4/CLHEP is here 
0064 }
0065 inline double U4UniformRand::Burn(int n) // static
0066 {
0067     double u = -1. ; 
0068     for(int i=0 ; i < n ; i++) u = Get();
0069     return u ; 
0070 }
0071 inline void U4UniformRand::Get(std::vector<double>& uu ) // static
0072 {
0073     for(unsigned i=0 ; i < uu.size() ; i++) uu[i] = Get();
0074 }
0075 inline std::string U4UniformRand::Desc(int n )
0076 {
0077     std::vector<double> uu(n) ; 
0078     Get(uu); 
0079     std::stringstream ss ; 
0080     ss << "U4UniformRand::Desc" << std::endl ; 
0081     for(int i=0 ; i < n ; i++)
0082     {
0083         ss << std::setw(6) << i 
0084            << " " << std::setw(10) << std::fixed << std::setprecision(5) << uu[i] 
0085            << std::endl 
0086            ;
0087     }
0088     std::string s = ss.str(); 
0089     return s; 
0090 }
0091 inline NP* U4UniformRand::Get(int n)
0092 {
0093     std::vector<double> uu(n) ; 
0094     Get(uu); 
0095     return NPX::Make<double>(uu) ; 
0096 }
0097 inline int U4UniformRand::Find(double u, const NP* uu)
0098 {
0099     return uu ? uu->find_value_index(u, EPSILON) : -2 ; 
0100 }
0101 inline std::string U4UniformRand::Desc(double u, const NP* uu)
0102 {
0103     std::stringstream ss ; 
0104     ss << "UU[" 
0105        << std::setw(7) << std::fixed << std::setprecision(5) << u 
0106        << " " 
0107        << std::setw(6) << Find(u, uu) 
0108        << "]"
0109        ;  
0110     std::string s = ss.str(); 
0111     return s; 
0112 }
0113