File indexing completed on 2026-04-10 07:50:29
0001
0002 #include <csignal>
0003 #include "OPTICKS_LOG.hh"
0004
0005 #include "spath.h"
0006 #include "ssys.h"
0007
0008 #include "U4GDML.h"
0009 #include "U4Tree.h"
0010
0011
0012 const char* FOLD = spath::Resolve("$TMP/U4TreeTest");
0013
0014
0015 void test_saveload_get_children(const stree* tree0, const stree* tree1, int nidx )
0016 {
0017 std::vector<int> children0 ;
0018 std::vector<int> children1 ;
0019 tree0->get_children(children0, nidx);
0020 tree1->get_children(children1, nidx);
0021
0022 if( nidx % 10000 == 0 )
0023 std::cout << " nidx " << nidx << " children " << stree::Desc(children0) << std::endl ;
0024
0025 assert( stree::Compare(children0, children1) == 0 );
0026 }
0027 void test_saveload_get_children(const stree* tree0, const stree* tree1)
0028 {
0029 std::cout << "[ test_saveload_get_children " << std::endl ;
0030 assert( tree0->nds.size() == tree1->nds.size() );
0031 for(int nidx=0 ; nidx < int(tree0->nds.size()) ; nidx++)
0032 test_saveload_get_children(tree0, tree1, nidx ) ;
0033 std::cout << "] test_saveload_get_children " << std::endl ;
0034 }
0035
0036 void test_saveload_get_progeny(const stree* tree0, const stree* tree1)
0037 {
0038 std::cout << "test_saveload_get_progeny_r " << std::endl ;
0039 int nidx = 0 ;
0040 std::vector<int> progeny0 ;
0041 std::vector<int> progeny1 ;
0042 tree0->get_progeny(progeny0, nidx);
0043 tree1->get_progeny(progeny1, nidx);
0044 assert( stree::Compare(progeny0, progeny1) == 0 );
0045 std::cout << " nidx " << nidx << " progeny " << stree::Desc(progeny0) << std::endl ;
0046 }
0047
0048 void test_saveload(const stree* st0)
0049 {
0050 std::cout << "[ st0.save " << std::endl ;
0051 st0->save(FOLD);
0052 std::cout << "] st0.save " << st0->desc() << std::endl ;
0053
0054 stree st1 ;
0055 std::cout << "[ st1.load " << std::endl ;
0056 st1.load(FOLD);
0057 std::cout << "] st1.load " << st1.desc() << std::endl ;
0058
0059 test_saveload_get_children(st0, &st1);
0060 test_saveload_get_progeny( st0, &st1);
0061 }
0062
0063 void test_load(const char* fold)
0064 {
0065 stree st ;
0066 st.load(fold);
0067 std::cout << "st.desc_sub" << std::endl << st.desc_sub() << std::endl ;
0068
0069
0070 }
0071
0072
0073 void test_get_pv_0(const U4Tree* tree)
0074 {
0075 std::cout << "[ test_get_pv_0 " << std::endl ;
0076 const stree* st = tree->st ;
0077
0078 const char* q_spec0 = ssys::getenvvar("SPEC0", "HamamatsuR12860sMask_virtual:0:0" );
0079 const char* q_spec1 = ssys::getenvvar("SPEC1", "HamamatsuR12860sMask_virtual:0:-1" );
0080
0081 int nidx0 = st->find_lvid_node(q_spec0 );
0082 int nidx1 = st->find_lvid_node(q_spec1 );
0083
0084 const G4VPhysicalVolume* const pv0_ = tree->get_pv(nidx0) ;
0085 const G4VPhysicalVolume* const pv1_ = tree->get_pv(nidx1) ;
0086
0087 const G4PVPlacement* pv0 = dynamic_cast<const G4PVPlacement*>(pv0_) ;
0088 const G4PVPlacement* pv1 = dynamic_cast<const G4PVPlacement*>(pv1_) ;
0089
0090 int nidx0_ = tree->get_nidx(pv0) ;
0091 int nidx1_ = tree->get_nidx(pv1) ;
0092
0093 bool nidx0_expect = nidx0_ == nidx0 ;
0094 bool nidx1_expect = nidx1_ == nidx1 ;
0095
0096 assert( nidx0_expect );
0097 assert( nidx1_expect );
0098
0099 if(!nidx0_expect) std::raise(SIGINT);
0100 if(!nidx1_expect) std::raise(SIGINT);
0101
0102 int cp0_ = pv0 ? pv0->GetCopyNo() : -1 ;
0103 int cp1_ = pv1 ? pv1->GetCopyNo() : -1 ;
0104
0105 int cp0 = tree->get_pv_copyno(nidx0);
0106 int cp1 = tree->get_pv_copyno(nidx1);
0107
0108 bool cp0_expect = cp0_ == cp0 ;
0109 bool cp1_expect = cp1_ == cp1 ;
0110
0111 assert( cp0_expect );
0112 assert( cp1_expect );
0113
0114 if(!cp0_expect) std::raise(SIGINT);
0115 if(!cp1_expect) std::raise(SIGINT);
0116
0117 std::cout << " q_spec0 " << q_spec0 << " nidx0 " << nidx0 << " cp0 " << cp0 << std::endl ;
0118 std::cout << " q_spec1 " << q_spec1 << " nidx1 " << nidx1 << " cp1 " << cp1 << std::endl ;
0119 std::cout << "] test_get_pv_0 " << std::endl ;
0120 }
0121
0122
0123
0124
0125
0126
0127
0128
0129
0130
0131
0132
0133
0134
0135
0136
0137
0138
0139
0140
0141
0142
0143
0144
0145
0146
0147
0148
0149
0150
0151
0152
0153
0154
0155
0156
0157
0158
0159
0160
0161
0162
0163
0164
0165
0166
0167
0168
0169
0170
0171
0172
0173
0174
0175
0176
0177
0178
0179
0180
0181
0182
0183
0184
0185
0186
0187
0188
0189
0190
0191
0192
0193
0194
0195
0196
0197
0198
0199
0200
0201
0202
0203
0204
0205
0206
0207
0208
0209
0210
0211
0212
0213
0214
0215
0216
0217
0218
0219
0220
0221
0222
0223
0224 void test_get_pv_1(const U4Tree* tree, const char* q_soname)
0225 {
0226 std::cout << "[ test_get_pv_1 " << std::endl ;
0227
0228 unsigned edge = ssys::getenvunsigned("EDGE", 10);
0229
0230 std::vector<int> nodes ;
0231 bool starting = false ;
0232 tree->st->find_lvid_nodes(nodes, q_soname, starting );
0233 std::cout << " q_soname " << q_soname << " nodes.size " << nodes.size() << std::endl ;
0234
0235 int copyno = -1 ;
0236 int prev_copyno = -1 ;
0237 for(unsigned i=0 ; i < nodes.size() ; i++)
0238 {
0239 int nidx = nodes[i] ;
0240 prev_copyno = copyno ;
0241 copyno = tree->get_pv_copyno(nidx);
0242
0243 int copyno_0 = tree->st->get_copyno(nidx);
0244 bool copyno_expect = copyno == copyno_0 ;
0245 assert( copyno_expect );
0246 if(!copyno_expect) std::raise(SIGINT);
0247
0248 if( i < edge || (i > (nodes.size() - edge)))
0249 std::cout
0250 << " i " << std::setw(7) << i
0251 << " nidx " << std::setw(7) << nidx
0252 << " copyno " << std::setw(6) << copyno
0253 << " copyno - prev_copyno " << std::setw(6) << (copyno - prev_copyno)
0254 << std::endl
0255 ;
0256 else if( i == edge )
0257 std::cout
0258 << " ... " << std::endl ;
0259
0260 }
0261 std::cout << "] test_get_pv_1 " << std::endl ;
0262 }
0263
0264 void test_get_pv_1(const U4Tree* tree)
0265 {
0266 std::vector<std::string> sub_sonames ;
0267 tree->st->get_sub_sonames(sub_sonames);
0268
0269 for(unsigned i=0 ; i < sub_sonames.size() ; i++)
0270 {
0271 const char* soname = sub_sonames[i].c_str();
0272 test_get_pv_1(tree, soname );
0273 }
0274 }
0275
0276 int main(int argc, char** argv)
0277 {
0278 OPTICKS_LOG(argc, argv);
0279
0280 const char* _path = "$HOME/.opticks/GEOM/$GEOM/origin.gdml" ;
0281 const char* path = spath::Resolve(_path);
0282 LOG(info)
0283 << " _path [" << _path << "]"
0284 << " path [" << path << "]"
0285 ;
0286 LOG_IF(fatal, path == nullptr) << " " ;
0287 if( path == nullptr ) return 0 ;
0288
0289 const G4VPhysicalVolume* world = U4GDML::Read(path) ;
0290
0291 stree* st = new stree ;
0292 U4Tree* tree = U4Tree::Create(st, world) ;
0293 st->save(FOLD);
0294
0295
0296 test_get_pv_1(tree);
0297
0298 return 0 ;
0299 }