Back to home page

EIC code displayed by LXR

 
 

    


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

0001 #pragma once
0002 /**
0003 SGenerate.h
0004 =============
0005 
0006 This exists for a very specific purpose : to enable
0007 comparison between Opticks and Geant4 by providing
0008 a way for photons from some types of gensteps to
0009 be CPU generated in order to allow giving those
0010 photons to Geant4.
0011 
0012 
0013 This started in SGenstep.h but because it relies on
0014 the MOCK_CURAND macro the method was moved to this header only SGenerate.h
0015 to allow switching on MOCK_CURAND in the "user" code
0016 rather than the widely used sysrap library.
0017 
0018 **/
0019 
0020 
0021 struct NP ;
0022 struct SGenerate
0023 {
0024     static constexpr const char* RNG_PRECOOKED = "SGenerate__GeneratePhotons_RNG_PRECOOKED" ;
0025     static NP* GeneratePhotons(const NP* gs);
0026 };
0027 
0028 
0029 #include "scuda.h"
0030 #include "squad.h"
0031 #include "sphoton.h"
0032 
0033 #include "srngcpu.h"
0034 using RNG = srngcpu ;
0035 
0036 #include "storch.h"
0037 #include "scarrier.h"
0038 
0039 #include "SGenstep.h"
0040 #include "SEvt.hh"
0041 #include "SEvent.hh"
0042 #include "OpticksGenstep.h"
0043 #include "NP.hh"
0044 
0045 
0046 /**
0047 SGenerate::GeneratePhotons
0048 ----------------------------
0049 
0050 Does high level genstep handling, prepares MOCK CURAND,
0051 creates seeds, creates photon array.
0052 The details of the generation are done by storch::generate or scarrier:generate
0053 
0054 NB : currently only limited gentype can be generated with this
0055 
0056 Q: Does MOCK_CURAND generate the same photons as without ?
0057 A: YES, see SGenerate__test.sh : MOCK_CURAND is to allow
0058    code intended to run via CUDA to see GPU like API on the CPU
0059 
0060 **/
0061 
0062 inline NP* SGenerate::GeneratePhotons(const NP* gs_ )
0063 {
0064     bool rng_precooked = ssys::getenvbool(RNG_PRECOOKED);
0065     std::cerr
0066         << "SGenerate::GeneratePhotons"
0067         << " " << RNG_PRECOOKED
0068         << " : "
0069         << ( rng_precooked ? "YES" : "NO " )
0070         << std::endl
0071         ;
0072 
0073     const quad6* gg = (quad6*)gs_->bytes() ;
0074     NP* se = SEvent::MakeSeed(gs_) ;
0075     const int*   seed = (int*)se->bytes() ;
0076 
0077     int tot_photon = se->shape[0] ;
0078     NP* ph = NP::Make<float>( tot_photon, 4, 4);
0079     sphoton* pp = (sphoton*)ph->bytes() ;
0080 
0081     unsigned rng_seed = 1u ;
0082     RNG rng ;
0083     rng.seed = rng_seed ;
0084 
0085     for(int i=0 ; i < tot_photon ; i++ )
0086     {
0087         unsigned photon_id = i ;
0088         unsigned genstep_id = seed[photon_id] ;
0089         sphoton& p = pp[photon_id] ;
0090         const quad6& gs = gg[genstep_id] ;
0091         int gencode = SGenstep::GetGencode(gs);
0092 
0093         if(rng_precooked) rng.setSequenceIndex(i);
0094         switch(gencode)
0095         {
0096             case OpticksGenstep_CARRIER:         scarrier::generate(     p, rng, gs, photon_id, genstep_id)  ; break ;
0097             case OpticksGenstep_TORCH:           storch::generate(       p, rng, gs, photon_id, genstep_id ) ; break ;
0098             case OpticksGenstep_INPUT_PHOTON:    assert(0)  ; break ;
0099         }
0100         if(rng_precooked) rng.setSequenceIndex(-1);
0101     }
0102     delete se ;
0103     return ph ;
0104 }
0105 
0106