File indexing completed on 2026-04-09 07:49:37
0001 #include <iostream>
0002 #include <iomanip>
0003 #include <sstream>
0004 #include <bitset>
0005 #include <algorithm>
0006 #include <cstring>
0007
0008 #include "ssys.h"
0009 #include "sstr.h"
0010
0011 #include "SBit.hh" // TODO: sbit.h
0012 #include "SBitSet.h"
0013
0014 #include "SGeoConfig.hh"
0015 #include "SName.h"
0016 #include "SLabel.h"
0017
0018 #include "SLOG.hh"
0019
0020 const plog::Severity SGeoConfig::LEVEL = SLOG::EnvLevel("SGeoConfig", "DEBUG");
0021
0022
0023 const char* SGeoConfig::_GEOM = ssys::getenvvar(kGEOM, nullptr );
0024 unsigned long long SGeoConfig::_EMM = SBit::FromEString(kEMM, "~0");
0025 const char* SGeoConfig::_ELVSelection = ssys::getenvvar(kELVSelection, nullptr );
0026 const char* SGeoConfig::_SolidSelection = ssys::getenvvar(kSolidSelection, nullptr );
0027 const char* SGeoConfig::_SolidTrimesh = ssys::getenvvar(kSolidTrimesh, nullptr );
0028 const char* SGeoConfig::_FlightConfig = ssys::getenvvar(kFlightConfig , nullptr );
0029 const char* SGeoConfig::_ArglistPath = ssys::getenvvar(kArglistPath , nullptr );
0030 const char* SGeoConfig::_CXSkipLV = ssys::getenvvar(kCXSkipLV , nullptr );
0031 const char* SGeoConfig::_CXSkipLV_IDXList = ssys::getenvvar(kCXSkipLV_IDXList, nullptr );
0032
0033 void SGeoConfig::SetELVSelection( const char* es){ _ELVSelection = es ? strdup(es) : nullptr ; }
0034 void SGeoConfig::SetSolidSelection(const char* ss){ _SolidSelection = ss ? strdup(ss) : nullptr ; }
0035 void SGeoConfig::SetSolidTrimesh( const char* st){ _SolidTrimesh = st ? strdup(st) : nullptr ; }
0036 void SGeoConfig::SetFlightConfig( const char* fc){ _FlightConfig = fc ? strdup(fc) : nullptr ; }
0037 void SGeoConfig::SetArglistPath( const char* ap){ _ArglistPath = ap ? strdup(ap) : nullptr ; }
0038 void SGeoConfig::SetCXSkipLV( const char* cx){ _CXSkipLV = cx ? strdup(cx) : nullptr ; }
0039
0040 unsigned long long SGeoConfig::EnabledMergedMesh(){ return _EMM ; }
0041 const char* SGeoConfig::SolidSelection(){ return _SolidSelection ; }
0042 const char* SGeoConfig::SolidTrimesh(){ return _SolidTrimesh ; }
0043 const char* SGeoConfig::FlightConfig(){ return _FlightConfig ; }
0044 const char* SGeoConfig::ArglistPath(){ return _ArglistPath ; }
0045 const char* SGeoConfig::CXSkipLV(){ return _CXSkipLV ? _CXSkipLV : "" ; }
0046 const char* SGeoConfig::CXSkipLV_IDXList(){ return _CXSkipLV_IDXList ? _CXSkipLV_IDXList : "" ; }
0047
0048
0049 const char* SGeoConfig::GEOM(){ return _GEOM ; }
0050 const char* SGeoConfig::ELVSelection(){ return _ELVSelection ; }
0051
0052
0053
0054
0055
0056
0057
0058
0059
0060
0061
0062
0063
0064
0065
0066
0067
0068
0069
0070
0071
0072
0073
0074
0075
0076
0077
0078
0079
0080
0081
0082
0083
0084
0085
0086
0087
0088
0089
0090
0091
0092
0093
0094
0095
0096 const char* SGeoConfig::ELVSelection(const SName* id )
0097 {
0098 const char* elv_selection_ = ELVSelection() ;
0099 const char* elv = nullptr ;
0100 char delim = ',' ;
0101 bool VERBOSE = ssys::getenvbool(ELVSelection_VERBOSE);
0102
0103 if(VERBOSE) std::cerr
0104 << "SGeoConfig::ELVSelection.0."
0105 << " [" << ELVSelection_VERBOSE << "] "
0106 << " elv_selection_ " << ( elv_selection_ ? elv_selection_ : "-" )
0107 << std::endl
0108 ;
0109
0110
0111
0112 bool allow_missing_names = true ;
0113
0114 if( elv_selection_ )
0115 {
0116 const char* prefix = ELVPrefix(elv_selection_);
0117
0118 if(VERBOSE) std::cerr
0119 << "SGeoConfig::ELVSelection.1."
0120 << " prefix " << ( prefix ? prefix : "-" )
0121 << " strlen(prefix) " << ( prefix ? strlen(prefix) : 0 )
0122 << "\n"
0123 ;
0124
0125
0126 if( SName::Has_STARTING( elv_selection_))
0127 {
0128 std::vector<std::string>* qq_missing = nullptr ;
0129 elv = id->getIDXListFromNames(elv_selection_, delim, prefix, qq_missing );
0130 }
0131 else
0132 {
0133 if( allow_missing_names )
0134 {
0135 std::vector<std::string>* qq_missing = new std::vector<std::string> ;
0136 elv = id->getIDXListFromNames(elv_selection_, delim, prefix, qq_missing );
0137
0138 int num_missing_names = qq_missing ? qq_missing->size() : -1 ;
0139
0140 if(VERBOSE) std::cerr
0141 << "SGeoConfig::ELVSelection.5."
0142 << " elv_selection_[" << elv_selection_ << "]"
0143 << " allow_missing_names " << ( allow_missing_names ? "YES" : "NO " )
0144 << " num_missing_names " << num_missing_names
0145 << "\n"
0146 ;
0147
0148 if(num_missing_names > 0) for(int i=0 ; i < num_missing_names ; i++ ) std::cerr
0149 << "SGeoConfig::ELVSelection.6. missing_name[" << (*qq_missing)[i] << "]\n" ;
0150 }
0151 else
0152 {
0153 std::vector<std::string>* qq_missing = nullptr ;
0154 std::stringstream ss ;
0155 bool has_names = id->hasNames(elv_selection_, delim, prefix, qq_missing, &ss );
0156
0157 if(VERBOSE) std::cerr
0158 << "SGeoConfig::ELVSelection.2."
0159 << " elv_selection_[" << elv_selection_ << "]"
0160 << " has_names " << ( has_names ? "YES" : "NO " )
0161 << " allow_missing_names " << ( allow_missing_names ? "YES" : "NO " )
0162 << " qq_missing.size " << ( qq_missing ? qq_missing->size() : -1 )
0163 << "\n"
0164 ;
0165
0166 if(!has_names) std::cout
0167 << "SGeoConfig::ELVSelection.3."
0168 << " has_names " << ( has_names ? "YES" : "NO " ) << "\n"
0169 << " qq_missing.size " << ( qq_missing ? qq_missing->size() : -1 )
0170 << "[haslog[\n"
0171 << ss.str()
0172 << "]haslog[\n"
0173 << "[id.detail\n"
0174 << id->detail()
0175 << "]id.detail\n"
0176 ;
0177
0178 if(has_names)
0179 {
0180 elv = id->getIDXListFromNames(elv_selection_, delim, prefix, qq_missing );
0181 }
0182 else
0183 {
0184 elv = elv_selection_ ;
0185 }
0186 }
0187 }
0188 }
0189 return elv ;
0190 }
0191
0192
0193
0194
0195
0196
0197
0198
0199
0200
0201
0202
0203
0204
0205 const char* SGeoConfig::ELVPrefix(const char* elvarg)
0206 {
0207 if(elvarg == nullptr) return nullptr ;
0208 if(strlen(elvarg) == 0) return nullptr ;
0209
0210 std::string prefix ;
0211 if(sstr::StartsWith(elvarg, "t:") || sstr::StartsWith(elvarg, "~:"))
0212 {
0213 prefix.assign("t:") ;
0214 }
0215 else if( sstr::StartsWith(elvarg,"t" ) || sstr::StartsWith(elvarg,"~") )
0216 {
0217 prefix.assign("t") ;
0218 }
0219 return prefix.empty() ? nullptr : strdup(prefix.c_str()) ;
0220 }
0221
0222
0223
0224
0225
0226
0227
0228
0229
0230
0231
0232
0233
0234
0235
0236
0237
0238
0239
0240
0241
0242 const char* SGeoConfig::ELVString(const SName* id)
0243 {
0244 const char* elv = ELVSelection(id) ;
0245 LOG(LEVEL) << " elv " << ( elv ? elv : "-" ) ;
0246 return elv ;
0247 }
0248
0249
0250
0251
0252
0253
0254
0255
0256
0257
0258 const SBitSet* SGeoConfig::ELV(const SName* id)
0259 {
0260 unsigned num_meshname = id->getNumName();
0261 const char* elv_ = ELVString(id);
0262 SBitSet* elv = elv_ ? SBitSet::Create(num_meshname, elv_ ) : nullptr ;
0263
0264 LOG(LEVEL)
0265 << " num_meshname " << num_meshname
0266 << " elv_ " << ( elv_ ? elv_ : "-" )
0267 << " elv " << ( elv ? elv->desc() : "-" )
0268 ;
0269
0270 return elv ;
0271 }
0272
0273
0274
0275
0276
0277
0278
0279
0280
0281
0282
0283 std::string SGeoConfig::Desc()
0284 {
0285 std::stringstream ss ;
0286 ss << std::endl ;
0287 ss << std::setw(25) << kGEOM << " : " << ( _GEOM ? _GEOM : "-" ) << std::endl ;
0288 ss << std::setw(25) << kEMM << " : " << SBit::HexString(_EMM) << " 0x" << std::hex << _EMM << std::dec << std::endl ;
0289 ss << std::setw(25) << kELVSelection << " : " << ( _ELVSelection ? _ELVSelection : "-" ) << std::endl ;
0290 ss << std::setw(25) << kSolidSelection << " : " << ( _SolidSelection ? _SolidSelection : "-" ) << std::endl ;
0291 ss << std::setw(25) << kSolidTrimesh << " : " << ( _SolidTrimesh ? _SolidTrimesh : "-" ) << std::endl ;
0292 ss << std::setw(25) << kFlightConfig << " : " << ( _FlightConfig ? _FlightConfig : "-" ) << std::endl ;
0293 ss << std::setw(25) << kArglistPath << " : " << ( _ArglistPath ? _ArglistPath : "-" ) << std::endl ;
0294 ss << std::setw(25) << kCXSkipLV << " : " << CXSkipLV() << std::endl ;
0295 ss << std::setw(25) << kCXSkipLV_IDXList << " : " << CXSkipLV_IDXList() << std::endl ;
0296 std::string str = ss.str();
0297 return str ;
0298 }
0299
0300 std::string SGeoConfig::desc() const
0301 {
0302 std::stringstream ss ;
0303 ss << "SGeoConfig::desc\n" ;
0304 std::string str = ss.str();
0305 return str ;
0306 }
0307
0308
0309
0310 bool SGeoConfig::IsEnabledMergedMesh(unsigned mm)
0311 {
0312 bool emptylistdefault = true ;
0313 bool emm = true ;
0314 if(mm < 64)
0315 {
0316 std::bitset<64> bs(_EMM);
0317 emm = bs.count() == 0 ? emptylistdefault : bs[mm] ;
0318 }
0319 return emm ;
0320 }
0321
0322
0323 std::string SGeoConfig::DescEMM()
0324 {
0325 std::stringstream ss ;
0326 ss << "SGeoConfig::DescEMM " ;
0327 ss << std::setw(25) << kEMM << " : " << SBit::HexString(_EMM) << " 0x" << std::hex << _EMM << std::dec << std::endl ;
0328
0329 for(unsigned i=0 ; i < 64 ; i++)
0330 {
0331 bool emm = SGeoConfig::IsEnabledMergedMesh(i) ;
0332 if(emm) ss << i << " " ;
0333 }
0334 std::string s = ss.str();
0335 return s ;
0336 }
0337
0338
0339 std::vector<std::string>* SGeoConfig::Arglist()
0340 {
0341 return sstr::LoadList( _ArglistPath, '\n' );
0342 }
0343
0344
0345
0346
0347
0348
0349
0350
0351
0352
0353 void SGeoConfig::SetCXSkipLV_IDXList(const SName* id)
0354 {
0355 const char* cxskiplv_ = CXSkipLV() ;
0356 bool has_names = cxskiplv_ ? id->hasNames(cxskiplv_ ) : false ;
0357 _CXSkipLV_IDXList = has_names ? id->getIDXListFromNames(cxskiplv_, ',' ) : nullptr ;
0358 }
0359
0360
0361
0362
0363
0364
0365
0366
0367
0368
0369
0370
0371
0372
0373
0374
0375
0376
0377
0378
0379
0380
0381
0382
0383
0384
0385
0386
0387
0388 bool SGeoConfig::IsCXSkipLV(int lv)
0389 {
0390 if( _CXSkipLV_IDXList == nullptr ) return false ;
0391 std::vector<int> cxskip ;
0392 sstr::split<int>(cxskip, _CXSkipLV_IDXList, ',' );
0393
0394 return std::count( cxskip.begin(), cxskip.end(), lv ) == 1 ;
0395 }
0396
0397
0398
0399
0400
0401
0402
0403
0404
0405
0406
0407
0408
0409
0410
0411
0412
0413
0414
0415
0416
0417
0418
0419
0420
0421
0422
0423
0424
0425
0426
0427
0428
0429
0430
0431 void SGeoConfig::GeometrySpecificSetup(const SName* id)
0432 {
0433 bool _JUNO_Detected = JUNO_Detected(id) ;
0434 LOG(LEVEL) << " _JUNO_Detected " << _JUNO_Detected ;
0435 if(_JUNO_Detected)
0436 {
0437
0438 const char* skips = nullptr ;
0439
0440 SetCXSkipLV(skips);
0441 SetCXSkipLV_IDXList(id);
0442
0443
0444
0445 }
0446 }
0447
0448 bool SGeoConfig::JUNO_Detected(const SName* id)
0449 {
0450 const char* JUNO_names = "HamamatsuR12860sMask,HamamatsuR12860_PMT_20inch,NNVTMCPPMT_PMT_20inch" ;
0451 bool with_JUNO_names = id ? id->hasNames(JUNO_names) : false ;
0452 return with_JUNO_names ;
0453 }
0454
0455