Geant4 Cross Reference |
1 """ 2 Converts a ROOT file to an HDF5 file, saving t 3 4 Args: 5 file_name: Path to the ROOT file with a na 6 output_<NAME>_angle<ANGLE>_<NU 7 output_dir: Path to the output directory, 8 output_dir/<NAME>_Angle_<ANGLE 9 10 Returns: 11 None 12 """ 13 14 #!/bin/env python 15 import sys 16 import argparse 17 import numpy as np 18 import os 19 import uproot 20 import h5py 21 22 # Number of cells in the r, phi & z directions 23 NB_CELLS_R = 18 24 NB_CELLS_PHI = 50 25 NB_CELLS_Z = 45 26 27 28 def find_between(s, first, last): 29 """ 30 Find a substring between two other substri 31 32 Args: 33 s (str): The string to search in. 34 first (str): The first substring. 35 last (str): The last substring. 36 37 Returns: 38 str: The substring found between 'firs 39 """ 40 try: 41 start = s.index(first) + len(first) 42 end = s.index(last, start) 43 return s[start:end] 44 except ValueError: 45 return "" 46 47 48 def parse_args(argv): 49 p = argparse.ArgumentParser() 50 p.add_argument("--outDir", type=str, defau 51 p.add_argument("--fName", type=str, defaul 52 args = p.parse_args() 53 return args 54 55 56 def main(argv): 57 # Parse commandline arguments 58 args = parse_args(argv) 59 file_name = args.fName 60 output_dir = args.outDir 61 # The energy and angle of the particle are 62 # of the ROOT's file name so they can be e 63 # Get the energy value of the particle 64 energy_particle = find_between(file_name, 65 # Get the angle value of the particule 66 angle_particle = find_between(file_name, " 67 # Get the number of showers 68 num_showers = find_between(file_name, "_", 69 # Get Ids specific to the generated files 70 file_id = find_between(file_name, "fullSim 71 if os.stat(file_name).st_size > 0: 72 h5_file = h5py.File( 73 f"{output_dir}/{energy_particle}_A 74 ) 75 # Read the Root file 76 file = uproot.open(file_name) 77 energy_particle = file["global"]["Ener 78 cell_r = file["virtualReadout"]["rhoCe 79 cell_phi = file["virtualReadout"]["phi 80 cell_energy = file["virtualReadout"][" 81 cell_z = file["virtualReadout"]["zCell 82 all_events = [] 83 # loop over events 84 for event in range(len(energy_particle 85 # Initialize a 3D array with shape 86 data = np.zeros((NB_CELLS_R, NB_CE 87 for ind in range(len(cell_r[event] 88 # This if statement is added t 89 if ( 90 (cell_r[event][ind] < NB_C 91 and (cell_phi[event][ind] 92 and (cell_z[event][ind] < 93 ): 94 data[cell_r[event][ind]][c 95 cell_z[event][ind] 96 ] = cell_energy[event][ind 97 all_events.append(data) 98 # Save dataset 99 h5_file.create_dataset( 100 f"{energy_particle}", 101 data=all_events, 102 compression="gzip", 103 compression_opts=9, 104 ) 105 h5_file.close() 106 107 108 if __name__ == "__main__": 109 exit(main(sys.argv[1:]))