Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-04-09 07:48:56

0001 #include "SStr.hh"
0002 #include "SPath.hh"
0003 #include "SSys.hh"
0004 #include "SLOG.hh"
0005 
0006 #include "scuda.h"
0007 #include "sqat4.h"
0008 #include "SEvent.hh"
0009 #include "NP.hh"
0010 
0011 #include "CSGFoundry.h"
0012 #include "CSGGenstep.h"
0013 
0014 
0015 const plog::Severity CSGGenstep::LEVEL = SLOG::EnvLevel("CSGGenstep", "DEBUG"); 
0016 
0017 CSGGenstep::CSGGenstep( const CSGFoundry* foundry_ )
0018     :
0019     foundry(foundry_), 
0020     gridscale(SSys::getenvfloat("GRIDSCALE", 1.0 )), 
0021     moi(nullptr),
0022     midx(-1),
0023     mord(-1),
0024     iidx(-1),
0025     ce(make_float4(0.f, 0.f, 0.f, 100.f)),
0026     m2w(qat4::identity()),
0027     w2m(qat4::identity()),
0028     geotran(nullptr),
0029     gs(nullptr),
0030     pp(nullptr)
0031 {
0032     init(); 
0033 }
0034 
0035 void CSGGenstep::init()
0036 {
0037     SSys::getenvintvec("CEGS", cegs, ':', "5:0:5:1000" ); 
0038     // expect 4 or 7 ints delimited by colon nx:ny:nz:num_pho OR nx:px:ny:py:nz:py:num_pho 
0039 }
0040 
0041 /**
0042 CSGGenstep::create
0043 --------------------
0044 
0045 Invoked from CSGOptiX/tests/CSGOptiXSimtraceTest.cc:main which is used by CSGOptiX/cxs.sh scripts. 
0046 
0047 moi
0048    string identifying piece of geometry mName:mOrdinal:mInstance eg Hama:0:1000
0049    ordinal is to pick between global geom and instance between instanced 
0050 
0051 ce_offset
0052    typically false for instanced geometry and true for global  
0053 
0054    this is kinda a noddy way of getting a transform 
0055    so this should be false when using tangential transforms 
0056 
0057 ce_scale
0058    typically true for old style global  
0059 
0060 TODO: eliminate ce_offset/ce_scale by using transform approach always
0061 
0062 
0063 1. identify piece of geometry from moi
0064 2. get location and transform for the geometry
0065 3. configure grid to probe the geometry
0066 4. create grid of gensteps 
0067 
0068 **/
0069 
0070 NP* CSGGenstep::create()
0071 {
0072     const char* moi = SSys::getenvvar("MOI", "sWorld:0:0");  
0073     bool ce_offset = SSys::getenvint("CE_OFFSET", 0) > 0 ; 
0074     bool ce_scale = SSys::getenvint("CE_SCALE", 0) > 0 ;   
0075 
0076     create(moi, ce_offset, ce_scale ); // SEvent::MakeCenterExtentGensteps
0077 
0078     return gs ; 
0079 }
0080 
0081 
0082 void CSGGenstep::create(const char* moi_, bool ce_offset, bool ce_scale )
0083 {
0084     moi = strdup(moi_); 
0085 
0086     LOG(info) << " moi " << moi << " ce_offset " << ce_offset << " ce_scale " << ce_scale ; 
0087 
0088     if( strcmp(moi, "FAKE") == 0 ) 
0089     {
0090         std::vector<int> photon_counts_per_genstep = { 3, 5, 2, 0, 1, 3, 4, 2, 4 };
0091         gs = SEvent::MakeCountGensteps(photon_counts_per_genstep, nullptr ) ;
0092     }
0093     else
0094     {
0095         locate(moi);        // sets: ce, geotran 
0096         configure_grid();   // standardize cegs 
0097         gs = SEvent::MakeCenterExtentGensteps(ce, cegs, gridscale, geotran, ce_offset, ce_scale ); 
0098     }
0099 
0100     gs->set_meta<std::string>("moi", moi ); 
0101     gs->set_meta<int>("midx", midx); 
0102     gs->set_meta<int>("mord", mord); 
0103     gs->set_meta<int>("iidx", iidx); 
0104     gs->set_meta<float>("gridscale", gridscale ); 
0105 }
0106 
0107 
0108 /**
0109 CSGGenstep::locate
0110 -----------------------
0111 
0112 1. parseMoi string giving midx:mord:iidx 
0113 2. get the *ce* and instance transform (populating qat4 members *m2w* *w2m*) 
0114    from the midx:mord:iidx using CSGTarget::getCenterExtent 
0115    which branches depending on iidx:
0116 
0117    iidx==-1 CSGTarget::getLocalCenterExtent 
0118    iidx>-1  CSGTarget::getGlobalCenterExtent 
0119 
0120 3. form geotran from the instance transform  
0121 
0122 Using CSGGenstepTest observed that with global non-instanced geometry 
0123 are just using the identity transform from the single global "instance". 
0124 Have added experimental way to use the tangential rtp transforms.
0125 
0126 
0127 HMM: try encapsulating this into::
0128    
0129     Tran<double>* CSGTarget::getFrame(const char* moi) const ;  
0130 
0131 So can elimate CSGGenstep, and move CE-gensteps creation down to SEvent
0132 by passing the "geotran" ? 
0133 
0134 **/
0135 
0136 void CSGGenstep::locate(const char* moi_)
0137 {
0138     moi = strdup(moi_) ; 
0139 
0140     foundry->parseMOI(midx, mord, iidx, moi );  
0141 
0142     LOG(info) << " moi " << moi << " midx " << midx << " mord " << mord << " iidx " << iidx ;   
0143     if( midx == -1 )
0144     {    
0145         LOG(fatal)
0146             << " failed CSGFoundry::parseMOI for moi [" << moi << "]" 
0147             ;
0148         return ; 
0149     }
0150 
0151 
0152     int rc = foundry->getCenterExtent(ce, midx, mord, iidx, m2w, w2m ) ;
0153 
0154     LOG(info) << " rc " << rc << " MOI.ce (" 
0155               << ce.x << " " << ce.y << " " << ce.z << " " << ce.w << ")" ;           
0156 
0157     LOG(info) << "m2w" << *m2w ;
0158     LOG(info) << "w2m" << *w2m ;
0159  
0160     geotran = Tran<double>::FromPair( m2w, w2m, 1e-6 );    // Tran from stran.h 
0161 
0162     //override_locate(); 
0163 }
0164 
0165 void CSGGenstep::override_locate()
0166 {
0167     std::vector<int> override_ce ; 
0168     SSys::getenvintvec("CXS_OVERRIDE_CE",  override_ce, ':', "0:0:0:0" ); 
0169 
0170     if( override_ce.size() == 4 && override_ce[3] > 0 )
0171     {
0172         ce.x = float(override_ce[0]); 
0173         ce.y = float(override_ce[1]); 
0174         ce.z = float(override_ce[2]); 
0175         ce.w = float(override_ce[3]); 
0176         LOG(info) << "override the MOI.ce with CXS_OVERRIDE_CE (" << ce.x << " " << ce.y << " " << ce.z << " " << ce.w << ")" ;  
0177     } 
0178 }
0179 
0180 
0181 /**
0182 CSGGenstep::configure_grid
0183 ----------------------------
0184 
0185 Standarizes the grid param and uses the param to obtain the mn mx bounds of the grid. 
0186 
0187 HUH : the mn/mx not actually used ? just for debug 
0188 
0189 **/
0190 
0191 void CSGGenstep::configure_grid()
0192 {
0193     SEvent::StandardizeCEGS(ce, cegs, gridscale ); 
0194     assert( cegs.size() == 7 ); 
0195 
0196     float3 mn ;   
0197     float3 mx ; 
0198 
0199     bool ce_offset_bb = true ; 
0200     SEvent::GetBoundingBox( mn, mx, ce, cegs, gridscale, ce_offset_bb ); 
0201 
0202     LOG(info) 
0203         << " ce_offset_bb " << ce_offset_bb
0204         << " mn " << mn 
0205         << " mx " << mx
0206         ; 
0207 }
0208 
0209 
0210 /**
0211 CSGGenstep::generate_photons_cpu
0212 ----------------------------------
0213 
0214 The GPU equivalent should aim to match 
0215 
0216    CSGOptiX/OptiX7Test.cu/simulate  
0217    quadrap/qsim.h/generate_photon
0218 
0219 **/
0220 
0221 void CSGGenstep::generate_photons_cpu()
0222 {
0223     pp = SEvent::GenerateCenterExtentGenstepsPhotons_( gs, gridscale );  
0224     std::cout 
0225          << "gs " << gs->sstr() 
0226          << "pp " << pp->sstr() 
0227          << std::endl 
0228          ;
0229 }
0230 
0231 void CSGGenstep::save(const char* basedir) const 
0232 {
0233     const char* base = SPath::Resolve(basedir, moi, DIRPATH ); 
0234     LOG(info) << " save to " << base ; 
0235     if(gs) gs->save(base, "gs.npy"); 
0236     if(pp) pp->save(base, "pp.npy"); 
0237 }
0238