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
0039 }
0040
0041
0042
0043
0044
0045
0046
0047
0048
0049
0050
0051
0052
0053
0054
0055
0056
0057
0058
0059
0060
0061
0062
0063
0064
0065
0066
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 );
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);
0096 configure_grid();
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
0110
0111
0112
0113
0114
0115
0116
0117
0118
0119
0120
0121
0122
0123
0124
0125
0126
0127
0128
0129
0130
0131
0132
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 );
0161
0162
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
0183
0184
0185
0186
0187
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
0212
0213
0214
0215
0216
0217
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