@@ -121,6 +121,57 @@ def _clip_altitude_if_close(altitude):
121121 return altitude
122122
123123
124+ def _split_history (history ):
125+ tel_history = {0 : []}
126+ tel = 0
127+
128+ for timestamp , line in history :
129+ line = line .decode ("utf-8" ).strip ()
130+
131+ if line .startswith ("# Telescope-specific configuration follows" ):
132+ tel += 1
133+ tel_history [tel ] = []
134+
135+ tel_history [tel ].append ((timestamp , line ))
136+
137+ global_history = tel_history .pop (0 )
138+ return global_history , tel_history
139+
140+
141+ def _has_true_image (history ):
142+ global_history , tel_history = _split_history (history )
143+
144+ # merge_simtel does not include photoelectrons, so always missing
145+ if "merge_simtel" in global_history [0 ][1 ]:
146+ return False
147+
148+ # first, check for global default
149+ save_photons = 0
150+ global_store_pe = - 1
151+ for _ , line in global_history :
152+ if line .lower ().startswith ("save_photons" ):
153+ save_photons = int (line .split ()[1 ])
154+
155+ if line .lower ().startswith ("store_photoelectrons" ):
156+ global_store_pe = int (line .split ()[1 ])
157+
158+ if (save_photons & 0b10 ) > 0 :
159+ return True
160+
161+ if global_store_pe > 0 :
162+ return True
163+
164+ # if global default is not set, we still might have true images
165+ # via the telescope configuration
166+ store_photoelectrons = {}
167+ for tel , config in tel_history .items ():
168+ for _ , line in config :
169+ if "store_photoelectrons" in line .lower ():
170+ store_photoelectrons [tel ] = int (line .split ()[1 ])
171+
172+ return any (v > 0 for v in store_photoelectrons .values ())
173+
174+
124175@enum .unique
125176class MirrorClass (enum .Enum ):
126177 """Enum for the sim_telarray MIRROR_CLASS values"""
@@ -575,7 +626,13 @@ def __init__(self, input_url=Undefined, config=None, parent=None, **kwargs):
575626 self .file_ , kind = self .atmosphere_profile_choice
576627 )
577628
578- self ._has_true_image = None
629+ try :
630+ self ._has_true_image = _has_true_image (self .file_ .history )
631+ except Exception :
632+ self .log .exception (
633+ "Error trying to determine whether file has true_image, assuming yes"
634+ )
635+ self ._has_true_image = True
579636 self .log .debug (f"Using gain selector { self .gain_selector } " )
580637
581638 def __exit__ (self , exc_type , exc_val , exc_tb ):
@@ -895,9 +952,6 @@ def _generate_events(self):
895952 self .telescope_indices_original [tel_id ]
896953 ]
897954
898- if self ._has_true_image is None :
899- self ._has_true_image = true_image is not None
900-
901955 if self ._has_true_image and true_image is None :
902956 if true_image_sum > MISSING_IMAGE_BRIGHTNESS_LIMIT :
903957 self .log .warning (
0 commit comments