title: IndexError: index 4 is out of bounds for axis 0 with size 4
body: <p>Hey I am having this Index Error where I am trying to composite events but my indices start at 0 and not 1 and while have tried to do a number of things like trying to .append[i+1] I am unable to fix this error I am having.</p>
  <p>Theres apparently something wrong with this specific line of code : <code>dset_IDX[offset:offset_next] = event_id[file_indices]</code></p>
  <p>While the .py file is over a 1000 lines of code so I can not show all of it I am able to show the part of the function that is giving me the error</p>
  <pre><code>def count_events(files):                                                           
      # Because we want to remove events with 0 hits,                                
      # we need to count the events beforehand (to create the h5 file).              
      # This function counts and indexes the events with more than 0 hits.           
      # Files need to be iterated in the same order to use the indexes.              
      &quot;&quot;&quot; This is where we manually specify the file&quot;&quot;&quot;                              
      num_events = 0                                                                 
      nonzero_file_events = []                                                       
      for file_index, f in enumerate(files):                                         
          data = np.load(f, allow_pickle=True)                                       
          nonzero_file_events.append([])                                             
          hits = data['digi_hit_pmt']                                                
          for i in range(len(hits)):                                                 
              if len(hits[i]) != 0:                                                  
                  nonzero_file_events[file_index].append(i)                          
                  num_events += 1                                                    
      return num_events, nonzero_file_events                                         
                                                                                     
                                                                                     
  def GenMapping(csv_file):                                                          
      mPMT_to_index = {}                                                             
      with open(csv_file) as f:                                                      
          rows = f.readline().split(&quot;,&quot;)[1:]                                         
          rows = [int(r.strip()) for r in rows]                                      
                                                                                     
          for line in f:                                                             
              line_split = line.split(&quot;,&quot;)                                           
              col = int(line_split[0].strip())                                       
              for row, value in zip(rows, line_split[1:]):                           
                  value = value.strip()                                              
                  if value:  # If the value is not empty                             
                      mPMT_to_index[int(value)] = [col, row]                         
      npmap = np.zeros((max(mPMT_to_index) + 1, 2), dtype=np.int)                    
      for k, v in mPMT_to_index.items():                                             
          npmap[k] = v                                                               
      return npmap                                                                   
                                                                                     
  </code></pre>
  <pre><code>def GenerateMultiMuonSample_h5(avg_mu_per_ev=2.5, sigma_time_offset=21.2):
      &quot;&quot;&quot;                                                                                                       
      Inputs:                                                                                                   
       avg_mu_per_ev == Poisson distribution mean for number of muons in each spill                             
       sigma_time_offset == Width of spill (Gaussian) in nanoseconds                                            
      &quot;&quot;&quot;
      files = ['event998.npz']
  
      # Remove whitespace                                                                                       
      files = [x.strip() for x in files]
  
      # Check that files were provided                                                                          
      if len(files) == 0:
          raise ValueError(&quot;No files provided!!&quot;)
      print(&quot;Merging &quot; + str(len(files)) + &quot; files&quot;)
  
      # Start merging                                                                                           
  
      num_nonzero_events, nonzero_event_indexes = count_events(files)
      print(num_nonzero_events)
  
      # np.random.poisson( avg_mu_per_ev, number_of_throws )                                                    
      num_muons = np.random.poisson(avg_mu_per_ev, num_nonzero_events - 2954)
  
      # creates h5 file to generate the h5 file                                                                  
  
      dtype_events = np.dtype(np.float32)
      dtype_labels = np.dtype(np.int32)
      dtype_energies = np.dtype(np.float32)
      dtype_positions = np.dtype(np.float32)
      dtype_IDX = np.dtype(np.int32)
      dtype_PATHS = h5py.special_dtype(vlen=str)
      dtype_angles = np.dtype(np.float32)
      # sets h5 file to be written                                                                               
      h5_file = h5py.File('multimuonfile(2).h5', 'w')
      dset_event_data = h5_file.create_dataset(&quot;event_data&quot;,
                                               shape=(num_nonzero_events,) + IMAGE_SHAPE,
                                               dtype=dtype_events)
      dset_labels = h5_file.create_dataset(&quot;labels&quot;,
                                           shape=(num_nonzero_events,),
                                           dtype=dtype_labels)
      dset_energies = h5_file.create_dataset(&quot;energies&quot;,
                                             shape=(num_nonzero_events, 1),
                                             dtype=dtype_energies)
      dset_positions = h5_file.create_dataset(&quot;positions&quot;,
                                              shape=(num_nonzero_events, 1, 3),
                                              dtype=dtype_positions)
      dset_IDX = h5_file.create_dataset(&quot;event_ids&quot;,
                                        shape=(num_nonzero_events,),
                                        dtype=dtype_IDX)
      dset_PATHS = h5_file.create_dataset(&quot;root_files&quot;,
                                          shape=(num_nonzero_events,),
                                          dtype=dtype_PATHS)
      dset_angles = h5_file.create_dataset(&quot;angles&quot;,
                                           shape=(num_nonzero_events, 2),
                                           dtype=dtype_angles)
  
      # 22 -&gt; gamma, 11 -&gt; electron, 13 -&gt; muon                                                                 
      # corresponds to labelling used in CNN with only barrel                                                   
      # IWCDmPMT_4pi_full_tank_gamma_E0to1000MeV_unif-pos-R371-y521cm_4pi-dir_3000evts_329.npz has an event     
      # with pid 11 though....                                                                                  
      # pid_to_label = {22:0, 11:1, 13:2}                                                                       
  
      offset = 0
      offset_next = 0
      mPMT_to_index = GenMapping(PMT_LABELS)
      # Loop over files                                                                                         
      for file_index, filename in enumerate(files):
          data = np.load(filename, allow_pickle=True)
          nonzero_events_in_file = len(nonzero_event_indexes[file_index])
          x_data = np.zeros((nonzero_events_in_file,) + IMAGE_SHAPE,
                            dtype=dtype_events)
          digi_hit_pmt = data['digi_hit_pmt']
          # digi_hit_charge = data['digi_hit_charge']                                                           
          # digi_hit_time = data['digi_hit_time']                                                               
          # digi_hit_trigger = data['digi_hit_trigger']                                                         
          # trigger_time = data['trigger_time']                                                                 
          delay = 0
          # Loop over events in file                                                                            
          # Loop over number of muons in each event                                                             
          event_id = np.array([], dtype=np.int32)
          root_file = np.array([], dtype=np.str)
          pid = np.array([])
          position = np.array([])
          direction = np.array([])
          energy = np.array([])
          labels = np.array([])
  
          # with open(&quot;ResultFile.txt&quot;, &quot;w&quot;) as text_file:                                                       
          # sys.stdout = open(&quot;Result2.txt&quot;, &quot;w&quot;)                                                                
  
          for i, nmu in enumerate(num_muons):
              # np.savetxt(text_file, i, nmu,fmt=&quot;%d&quot;)                                                           
              # text_file.write(&quot;processing output entry &quot; + str(i) + &quot; with &quot; + nmu + &quot; muons&quot;)                 
              print(&quot;processing output entry &quot;, i, &quot; with &quot;, nmu, &quot; muons&quot;)
              indices = np.random.randint(0, len(digi_hit_pmt), max(1, nmu))
              time_offs = [0.]
              if nmu &gt; 1:
                  time_offs = np.append(time_offs, np.random.normal(0., sigma_time_offset, nmu - 1))
              hit_pmts, charge, time = SumEvents(indices, time_offs, data, nmu == 0)
              hit_mpmts = hit_pmts // 19
              pmt_channels = hit_pmts % 19
              rows = mPMT_to_index[hit_mpmts, 0]
              cols = mPMT_to_index[hit_mpmts, 1]
              x_data[i - delay, rows, cols, pmt_channels] = charge
              x_data[i - delay, rows, cols, pmt_channels + 19] = time
  
              # fix below!!!                                                                                    
              idx0 = indices[0]
              event_id = np.append(event_id, data['event_id'][idx0])
              root_file = np.append(root_file, data['root_file'][idx0])
              pid = np.append(pid, data['pid'][idx0])
              position = np.append(position, data['position'][idx0])
              direction = np.append(direction, data['direction'][idx0])
              energy = np.append(energy, np.sum(data['energy'][indices]))
              labels = np.append(labels, nmu)
  
          offset_next += nonzero_events_in_file
  
          file_indices = nonzero_event_indexes[file_index]
  
          dset_IDX[offset:offset_next] = event_id[file_indices]
          dset_PATHS[offset:offset_next] = root_file[file_indices]
          dset_energies[offset:offset_next, :] = energy[file_indices].reshape(-1, 1)
          dset_positions[offset:offset_next, :, :] = position[file_indices].reshape(-1, 1, 3)
          dset_labels[offset:offset_next] = labels[file_indices]
          print(event_id)
          direction = direction[file_indices]
          polar = np.arccos(direction[:, 1])
          azimuth = np.arctan2(direction[:, 2], direction[:, 0])
          dset_angles[offset:offset_next, :] = np.hstack((polar.reshape(-1, 1), azimuth.reshape(-1, 1)))
          dset_event_data[offset:offset_next, :] = x_data
  
          offset = offset_next                                         
          print(&quot;Finished file: {}&quot;.format(filename))                  
                                                                       
      #sys.stdout.close()                                              
      print(&quot;Saving&quot;)                                                  
      #h5_file.close()                                                 
      print(&quot;Finished&quot;)                                                
                                                                       
                                                                       
  # In[ ]:                                                             
                                                                       
                                                                       
  GenerateMultiMuonSample_h5(avg_mu_per_ev=2.5, sigma_time_offset=21.2)
                                                                                                                              
  </code></pre>
  <p>Traceback</p>
  <pre><code>Merging 1 files
  2958
  processing output entry  0  with  3  muons
  processing output entry  1  with  1  muons
  processing output entry  2  with  3  muons
  processing output entry  3  with  3  muons
  
  Traceback (most recent call last):
    File &quot;C:/Users/abdul/OneDrive/Desktop/ISSP/ISSP-AA/TriumfCNN-AA/EventDisplay.py&quot;, line 1068, in &lt;module&gt;
      GenerateMultiMuonSample_h5(avg_mu_per_ev=2.5, sigma_time_offset=21.2)
    File &quot;C:/Users/abdul/OneDrive/Desktop/ISSP/ISSP-AA/TriumfCNN-AA/EventDisplay.py&quot;, line 1044, in GenerateMultiMuonSample_h5
      dset_IDX[offset:offset_next] = event_id[file_indices]
  IndexError: index 4 is out of bounds for axis 0 with size 4
  </code></pre>