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5 ] [ 10.5.p1 ] [ 10.6 ] [ 10.6.p1 ] [ 10.6.p2 ] [ 10.6.p3 ] [ 10.7 ] [ 10.7.p1 ] [ 10.7.p2 ] [ 10.7.p3 ] [ 10.7.p4 ] [ 11.0 ] [ 11.0.p1 ] [ 11.0.p2 ] [ 11.0.p3, ] [ 11.0.p4 ] [ 11.1 ] [ 11.1.1 ] [ 11.1.2 ] [ 11.1.3 ] [ 11.2 ] [ 11.2.1 ] [ 11.2.2 ] [ 11.3.0 ]

Diff markup

Differences between /examples/extended/parameterisations/Par04/training/root2h5.py (Version 11.3.0) and /examples/extended/parameterisations/Par04/training/root2h5.py (Version 10.4.p3)


  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:]))