@@ -206,3 +206,179 @@ def create_table_and_save_to_sql(
206206 integer_primary_key = integer_primary_key ,
207207 )
208208 save_to_sql (df , table_name = table_name , database_path = database_path )
209+
210+
211+ def get_first_pulse_times (
212+ database_path : str ,
213+ pulses_table_name : str = "SRTInIcePulses" ,
214+ time_column : str = "dom_time" ,
215+ index_column : str = "event_no" ,
216+ ) -> pd .DataFrame :
217+ """Get the first pulse time for each event.
218+
219+ Args:
220+ database_path: Path to the database.
221+ pulses_table_name: Name of the pulses table.
222+ time_column: Name of the time column in the pulses table.
223+ index_column: Name of the index column in the pulses table.
224+
225+ Returns:
226+ DataFrame with two columns: `event_no` and `first_pulse_time`.
227+ """
228+ query = (
229+ f"SELECT { index_column } , MIN({ time_column } ) AS first_pulse_time "
230+ f"FROM { pulses_table_name } "
231+ f"GROUP BY { index_column } ;"
232+ )
233+ return query_database (database_path , query )
234+
235+
236+ def add_first_pulse_time_to_truth (
237+ database_path : str ,
238+ truth_table_name : str = "truth" ,
239+ pulses_table_name : str = "SRTInIcePulses" ,
240+ time_column : str = "dom_time" ,
241+ index_column : str = "event_no" ,
242+ ) -> None :
243+ """Add the first pulse time to the truth table.
244+
245+ Args:
246+ database_path: Path to the database.
247+ truth_table_name: Name of the truth table.
248+ pulses_table_name: Name of the pulses table.
249+ time_column: Name of the time column in the pulses table.
250+ index_column: Name of the index column in both tables.
251+ """
252+
253+ # Get first pulse times
254+ df = get_first_pulse_times (
255+ database_path = database_path ,
256+ pulses_table_name = pulses_table_name ,
257+ time_column = time_column ,
258+ index_column = index_column ,
259+ )
260+ print (f"Finished getting first pulse times for { len (df )} events." )
261+ # Create temporary table for first pulse times
262+ temp_table_name = "temp_first_pulse_times"
263+
264+ query = f"DROP TABLE IF EXISTS { temp_table_name } ;"
265+ run_sql_code (database_path , query )
266+
267+ create_table (
268+ columns = ["event_no" , "first_pulse_time" ],
269+ table_name = temp_table_name ,
270+ database_path = database_path ,
271+ index_column = index_column ,
272+ default_type = "FLOAT" ,
273+ integer_primary_key = True ,
274+ )
275+ print (f"Created temporary table { temp_table_name } for first pulse times." )
276+ # Save first pulse times to temporary table
277+ save_to_sql (
278+ df = df ,
279+ table_name = temp_table_name ,
280+ database_path = database_path ,
281+ )
282+
283+ # Create the column and update it in the truth table remove if already exists
284+ query = (
285+ f"ALTER TABLE { truth_table_name } "
286+ f"ADD COLUMN first_pulse_time FLOAT;"
287+ )
288+ print (f"Adding column 'first_pulse_time' to { truth_table_name } ." )
289+
290+ run_sql_code (database_path , query )
291+ query = (
292+ f"UPDATE { truth_table_name } "
293+ f"SET first_pulse_time = (SELECT first_pulse_time "
294+ f"FROM { temp_table_name } "
295+ f"WHERE { temp_table_name } .{ index_column } = { truth_table_name } .{ index_column } );"
296+ )
297+
298+ run_sql_code (database_path , query )
299+ print (
300+ f"Updated { truth_table_name } with first pulse times from { temp_table_name } ."
301+ )
302+ # Drop the temporary table
303+ query = f"DROP TABLE IF EXISTS { temp_table_name } ;"
304+ print (f"Dropping temporary table { temp_table_name } ." )
305+ run_sql_code (database_path , query )
306+
307+
308+ def add_starting (
309+ database_path : str ,
310+ truth_table_name : str = "truth" ,
311+ containment_column : str = "containment_type" ,
312+ index_column : str = "event_no" ,
313+ ) -> None :
314+ """Add the starting to the truth table.
315+
316+ Args:
317+ database_path: Path to the database.
318+ truth_table_name: Name of the truth table.
319+ index_column: Name of the index column in both tables.
320+ """
321+
322+ # mapping from containment enum to starting
323+ map_dict = {
324+ 1 : 0 , # no intersect: not starting
325+ 2 : 0 , # through-going: not starting
326+ 3 : 1 , # contained: starting
327+ 4 : 1 , # tau-to-mu: starting
328+ 5 : 1 , # uncontained-starting: starting
329+ 6 : 0 , # stopping: not starting
330+ 7 : 0 , # decayed: not starting
331+ 8 : 0 , # through-going bundle: not starting
332+ 9 : 0 , # stopping bundle: not starting
333+ 10 : 1 , # partial-contained: starting
334+ }
335+
336+ containment_type_query = (
337+ f"SELECT { index_column } , { containment_column } "
338+ f"FROM { truth_table_name } ;"
339+ )
340+
341+ containment_df = query_database (database_path , containment_type_query )
342+
343+ # convert containment type to starting using map_dict
344+ containment_df ["starting" ] = (
345+ containment_df [containment_column ].astype (int ).map (map_dict )
346+ )
347+
348+ temp_table_name = "temp_starting"
349+ query = f"DROP TABLE IF EXISTS { temp_table_name } ;"
350+ run_sql_code (database_path , query )
351+
352+ create_table (
353+ columns = [index_column , "starting" ],
354+ table_name = temp_table_name ,
355+ database_path = database_path ,
356+ index_column = index_column ,
357+ default_type = "INTEGER" ,
358+ integer_primary_key = True ,
359+ )
360+
361+ print (f"Created temporary table { temp_table_name } for starting." )
362+ # Save starting to temporary table
363+ save_to_sql (
364+ df = containment_df [[index_column , "starting" ]],
365+ table_name = temp_table_name ,
366+ database_path = database_path ,
367+ )
368+ # Create the column and update it in the truth table remove if already exists
369+ query = f"ALTER TABLE { truth_table_name } " f"ADD COLUMN starting INTEGER;"
370+ print (f"Adding column 'starting' to { truth_table_name } ." )
371+ run_sql_code (database_path , query )
372+ query = (
373+ f"UPDATE { truth_table_name } "
374+ f"SET starting = (SELECT starting "
375+ f"FROM { temp_table_name } "
376+ f"WHERE { temp_table_name } .{ index_column } = { truth_table_name } .{ index_column } );"
377+ )
378+
379+ run_sql_code (database_path , query )
380+ print (f"Updated { truth_table_name } with starting from { temp_table_name } ." )
381+ # Drop the temporary table
382+ query = f"DROP TABLE IF EXISTS { temp_table_name } ;"
383+ print (f"Dropping temporary table { temp_table_name } ." )
384+ run_sql_code (database_path , query )
0 commit comments