|
| 1 | +from larcv import larcv |
| 2 | +from ROOT import TChain |
| 3 | +import numpy as np |
| 4 | +import argparse, gdown, os, sys |
| 5 | + |
| 6 | +def main(): |
| 7 | + parser = argparse.ArgumentParser( |
| 8 | + description="Check two larcv files have the same contents") |
| 9 | + |
| 10 | + parser.add_argument('--ref_file', '-r', |
| 11 | + help='Path to the reference file', |
| 12 | + type=str, required=True) |
| 13 | + parser.add_argument('--test_file', '-t', |
| 14 | + help='Path to the target file to be tested', |
| 15 | + type=str, required=True) |
| 16 | + parser.add_argument('--key_tensor3d', |
| 17 | + help='Name key string to retrieve a Tensor3D object', |
| 18 | + type=str, required=False, default="pcluster") |
| 19 | + parser.add_argument('--key_cluster3d', |
| 20 | + help='Name key string to retrieve a Cluster3D object', |
| 21 | + type=str, required=False, default="pcluster") |
| 22 | + parser.add_argument('--key_particle', |
| 23 | + help='Name key string to retrieve a Particle object', |
| 24 | + type=str, required=False, default="pcluster") |
| 25 | + |
| 26 | + args = parser.parse_args() |
| 27 | + |
| 28 | + ref_file=args.ref_file |
| 29 | + test_file=args.test_file |
| 30 | + |
| 31 | + if not os.path.isfile(ref_file): |
| 32 | + FileNotFoundError(f'Reference file not found: {ref_file}') |
| 33 | + |
| 34 | + if not os.path.isfile(test_file): |
| 35 | + FileNotFoundError(f'Test target file not found: {test_file}') |
| 36 | + |
| 37 | + error_code=0 |
| 38 | + try: |
| 39 | + test_tensor3d (ref_file, test_file, args.key_tensor3d ) |
| 40 | + except AssertionError: |
| 41 | + print('Found incompatible tensor3d') |
| 42 | + error_code+=1 |
| 43 | + |
| 44 | + try: |
| 45 | + test_cluster3d (ref_file, test_file, args.key_cluster3d ) |
| 46 | + except AssertionError: |
| 47 | + print('Found incompatible cluster3d') |
| 48 | + error_code+=2 |
| 49 | + |
| 50 | + try: |
| 51 | + test_particles (ref_file, test_file, args.key_particle ) |
| 52 | + except AssertionError: |
| 53 | + print('Found incompatible particle') |
| 54 | + error_code+=4 |
| 55 | + |
| 56 | + sys.exit(error_code) |
| 57 | + |
| 58 | +def compare_voxel_sets(vs0,vs1,meta=None): |
| 59 | + |
| 60 | + assert vs0.size() == vs1.size() |
| 61 | + |
| 62 | + pts0=np.zeros(shape=(vs0.size(),4),dtype=np.float32) |
| 63 | + pts1=np.zeros(shape=(vs1.size(),4),dtype=np.float32) |
| 64 | + |
| 65 | + if meta is None: |
| 66 | + larcv.fill_3d_pcloud(vs0,pts0) |
| 67 | + larcv.fill_3d_pcloud(vs1,pts1) |
| 68 | + else: |
| 69 | + larcv.fill_3d_pcloud(vs0,meta,pts0) |
| 70 | + larcv.fill_3d_pcloud(vs1,meta,pts1) |
| 71 | + |
| 72 | + assert pts0.sum()==pts1.sum() |
| 73 | + assert (pts1-pts0).sum() == np.float32(0.) |
| 74 | + assert (pts0 == pts1).sum() == np.prod(pts0.shape) |
| 75 | + |
| 76 | + |
| 77 | +def test_tensor3d(ref_file,target_file,name_tensor3d): |
| 78 | + |
| 79 | + tree_name = 'sparse3d_'+name_tensor3d+'_tree' |
| 80 | + branch_name = 'sparse3d_'+name_tensor3d+'_branch' |
| 81 | + ch0=TChain(tree_name) |
| 82 | + ch1=TChain(tree_name) |
| 83 | + |
| 84 | + ch0.AddFile(ref_file) |
| 85 | + ch1.AddFile(target_file) |
| 86 | + |
| 87 | + assert ch0.GetEntries() == ch1.GetEntries() |
| 88 | + |
| 89 | + for entry in range(ch0.GetEntries()): |
| 90 | + |
| 91 | + ch0.GetEntry(entry) |
| 92 | + ch1.GetEntry(entry) |
| 93 | + |
| 94 | + br0=getattr(ch0,branch_name) |
| 95 | + br1=getattr(ch1,branch_name) |
| 96 | + |
| 97 | + compare_voxel_sets(br0,br1) |
| 98 | + |
| 99 | +def test_cluster3d(ref_file,target_file,name_cluster3d): |
| 100 | + |
| 101 | + tree_name = 'cluster3d_'+name_cluster3d+'_tree' |
| 102 | + branch_name = 'cluster3d_'+name_cluster3d+'_branch' |
| 103 | + ch0=TChain(tree_name) |
| 104 | + ch1=TChain(tree_name) |
| 105 | + |
| 106 | + ch0.AddFile(ref_file) |
| 107 | + ch1.AddFile(target_file) |
| 108 | + |
| 109 | + assert ch0.GetEntries() == ch1.GetEntries() |
| 110 | + |
| 111 | + for entry in range(ch0.GetEntries()): |
| 112 | + |
| 113 | + ch0.GetEntry(entry) |
| 114 | + ch1.GetEntry(entry) |
| 115 | + |
| 116 | + br0=getattr(ch0,branch_name) |
| 117 | + br1=getattr(ch1,branch_name) |
| 118 | + |
| 119 | + assert br0.size() == br1.size() |
| 120 | + assert br0.meta() == br1.meta() |
| 121 | + |
| 122 | + for i in range(br0.size()): |
| 123 | + |
| 124 | + vs0=br0.as_vector()[i] |
| 125 | + vs1=br1.as_vector()[i] |
| 126 | + |
| 127 | + compare_voxel_sets(vs0,vs1,br0.meta()) |
| 128 | + |
| 129 | +def test_particles(ref_file,target_file,name_particle): |
| 130 | + |
| 131 | + tree_name = 'particle_'+name_particle+'_tree' |
| 132 | + branch_name = 'particle_'+name_particle+'_branch' |
| 133 | + ch0=TChain(tree_name) |
| 134 | + ch1=TChain(tree_name) |
| 135 | + |
| 136 | + ch0.AddFile(ref_file) |
| 137 | + ch1.AddFile(target_file) |
| 138 | + |
| 139 | + assert ch0.GetEntries() == ch1.GetEntries() |
| 140 | + |
| 141 | + for entry in range(ch0.GetEntries()): |
| 142 | + |
| 143 | + ch0.GetEntry(entry) |
| 144 | + ch1.GetEntry(entry) |
| 145 | + |
| 146 | + br0=getattr(ch0,branch_name) |
| 147 | + br1=getattr(ch1,branch_name) |
| 148 | + |
| 149 | + assert br0.size() == br1.size() |
| 150 | + |
| 151 | + for i in range(br0.size()): |
| 152 | + |
| 153 | + p0 = br0.as_vector()[i] |
| 154 | + p1 = br1.as_vector()[i] |
| 155 | + |
| 156 | + assert p0.position() == p1.position() |
| 157 | + assert p0.end_position() == p1.end_position() |
| 158 | + assert p0.first_step() == p1.first_step() |
| 159 | + assert p0.last_step() == p1.last_step() |
| 160 | + assert p0.pdg_code() == p1.pdg_code() |
| 161 | + assert p0.track_id() == p1.track_id() |
| 162 | + assert p0.parent_track_id() == p1.parent_track_id() |
| 163 | + assert p0.id() == p1.id() |
| 164 | + assert p0.parent_id() == p1.parent_id() |
| 165 | + assert p0.ancestor_track_id() == p1.ancestor_track_id() |
| 166 | + assert p0.shape() == p1.shape() |
| 167 | + |
| 168 | +if __name__ == '__main__': |
| 169 | + |
| 170 | + main() |
0 commit comments