@@ -266,7 +266,7 @@ def __init__(
266266 """
267267 super ().__init__ (pulsemap , exclude , extractor_name )
268268 self ._time_window = time_window
269- self ._mctree = mctree
269+ self .mctree = mctree
270270 self ._mcpe_map = mcpe_map
271271 self ._mcpe_map_id = mcpe_map_id
272272 self ._pulse_source_map = pulse_source_map
@@ -418,45 +418,14 @@ def _get_mcpe_info(
418418 npe_list : list [float ] = []
419419 track_like_list : list [float ] = []
420420 noise_list : list [bool ] = []
421- try :
422- mcpe_info = np .array (frame [self ._mcpe_map ][om_key ])
423- except KeyError as e :
424- if self ._mcpe_map in str (e ):
425- self .warning_once (
426- f"MCPE map { self ._mcpe_map } not found in frame."
427- )
428- return (
429- np .array (npe_list ),
430- np .array (times ),
431- np .array (nu_bool ),
432- np .array (track_like_list ),
433- np .array (noise_list ),
434- )
435- elif "Invalid key" in str (e ):
436- return (
437- np .array (npe_list ),
438- np .array (times ),
439- np .array (nu_bool ),
440- np .array (track_like_list ),
441- np .array (noise_list ),
442- )
443- else :
444- raise e
445-
446- mcpe_id_map_keys = []
447- mcpe_index_map = []
448- try :
449- for id_key , id_vals in frame [self ._mcpe_map_id ][om_key ].items ():
450- mcpe_id_map_keys .extend ([id_key ] * len (id_vals ))
451- # mcpe_id_map_vals.extend(mcpe_info[id_vals])
452- mcpe_index_map .extend (id_vals )
453421
454- except KeyError :
455- # This just means all the pulses are noise hits (do nothing)
456- pass
457-
458- mcpe_id_map_keys = np .array (mcpe_id_map_keys )
459- mcpe_index_map = np .array (mcpe_index_map )
422+ mcpe_info , mcpe_id_map_keys , mcpe_index_map = (
423+ self ._get_info_map_and_index (frame , om_key )
424+ )
425+ if len (mcpe_info ) == 0 :
426+ return self ._format_return (
427+ npe_list , times , nu_bool , track_like_list , noise_list
428+ )
460429
461430 for i , mcpe in enumerate (mcpe_info ):
462431 is_noise = False
@@ -468,20 +437,31 @@ def _get_mcpe_info(
468437
469438 elif mcpe .ID != dataclasses .I3ParticleID (0 , - 1 ):
470439 # Not a multiple parent hit - use direct method
471- particle = frame [self ._mctree ].get_particle (mcpe .ID )
472- track_like = float (particle .is_track )
473- neutrino_bool = particle .is_neutrino
440+ try :
441+ particle = frame [self .mctree ].get_particle (mcpe .ID )
442+ track_like = float (particle .is_track )
443+ neutrino_bool = particle .is_neutrino
444+ except RuntimeError as e :
445+ if "particleID not found" in str (e ):
446+ self ._missing_mcpe_warning (mcpe .ID , frame , om_key )
447+ # continue but raise warning as this is unexpected.
448+ track_like = 0.0
449+ neutrino_bool = False
474450
475451 else :
476452 # Hit with multiple parent particles - need to loop over all parent particles
477- particle_list = [
478- frame [self ._mctree ].get_particle (p )
479- for p in mcpe_id_map_keys [i == mcpe_index_map ]
480- ]
453+ particle_list = []
454+
455+ for p in mcpe_id_map_keys [i == mcpe_index_map ]:
456+ try :
457+ particle_list .append (
458+ frame [self .mctree ].get_particle (p )
459+ )
460+ except RuntimeError as e :
461+ if "particleID not found" in str (e ):
462+ self ._missing_mcpe_warning (p , frame , om_key )
463+
481464 if len (particle_list ) == 0 :
482- warning_string = f"No parent particles found for MCPE with ID { mcpe .ID } in OMKey { om_key } .\n "
483- warning_string += f"{ frame ['I3EventHeader' ]} \n "
484- self .warning (warning_string )
485465 track_like = 0.0
486466 neutrino_bool = False
487467 else :
@@ -496,12 +476,8 @@ def _get_mcpe_info(
496476 npe_list .append (mcpe .npe )
497477 noise_list .append (is_noise )
498478
499- return (
500- np .array (npe_list ),
501- np .array (times ),
502- np .array (nu_bool ),
503- np .array (track_like_list ),
504- np .array (noise_list ),
479+ return self ._format_return (
480+ npe_list , times , nu_bool , track_like_list , noise_list
505481 )
506482
507483 def _get_pulse_info (
@@ -519,6 +495,63 @@ def _get_gaussian_weight(
519495 - 0.5 * (time_distance_matrix / (self ._time_window / 2 )) ** 2
520496 )
521497
498+ def _missing_mcpe_warning (
499+ self , id : Any , frame : "icetray.I3Frame" , om_key : "icetray.OMKey"
500+ ) -> None :
501+ warning_string = f"Could not find particle for MCPE with ID { id } in OMKey { om_key } .\n "
502+ warning_string += f"{ frame ['I3EventHeader' ]} \n "
503+ self .warning (warning_string )
504+
505+ def _get_info_map_and_index (
506+ self , frame : "icetray.I3Frame" , om_key : "icetray.OMKey"
507+ ) -> tuple [np .ndarray , np .ndarray , np .ndarray ]:
508+
509+ mcpe_id_map_keys = []
510+ mcpe_index_map = []
511+
512+ try :
513+ mcpe_info = np .array (frame [self ._mcpe_map ][om_key ])
514+ except KeyError as e :
515+ if self ._mcpe_map in str (e ):
516+ self .warning_once (
517+ f"MCPE map { self ._mcpe_map } not found in frame."
518+ )
519+ return np .array ([]), np .array ([]), np .array ([])
520+ elif "Invalid key" in str (e ):
521+ return np .array ([]), np .array ([]), np .array ([])
522+ else :
523+ raise e
524+
525+ try :
526+ for id_key , id_vals in frame [self ._mcpe_map_id ][om_key ].items ():
527+ mcpe_id_map_keys .extend ([id_key ] * len (id_vals ))
528+ mcpe_index_map .extend (id_vals )
529+
530+ except KeyError :
531+ # This just means all the pulses are noise hits (do nothing)
532+ pass
533+
534+ mcpe_id_map_keys = np .array (mcpe_id_map_keys )
535+ mcpe_index_map = np .array (mcpe_index_map )
536+
537+ return mcpe_info , mcpe_id_map_keys , mcpe_index_map
538+
539+ def _format_return (
540+ self ,
541+ npe_list : List [float ],
542+ times : List [float ],
543+ nu_bool : List [bool ],
544+ track_like_list : List [float ],
545+ noise_bool : List [bool ],
546+ ) -> tuple [np .ndarray , np .ndarray , np .ndarray , np .ndarray , np .ndarray ]:
547+ return (
548+ np .array (npe_list ),
549+ np .array (times ),
550+ np .array (nu_bool ),
551+ np .array (track_like_list ),
552+ np .array (noise_bool ),
553+ )
554+
522555
523556class I3FeatureExtractorIceCubeDeepCore (I3FeatureExtractorIceCube86 ):
524557 """Class for extracting reconstructed features for IceCube-DeepCore."""
0 commit comments