@@ -341,6 +341,16 @@ def shift_anchor_past_ITS_rampup(run_start, first_orbit, orbitsPerTF, ITS_rampup
341341 rampupOrbits = - (- milliseconds_to_orbits (math .ceil (ITS_rampup )) // orbitsPerTF ) * orbitsPerTF
342342 return run_start + int (rampupOrbits * LHCOrbitMUS / 1000. ), first_orbit + rampupOrbits
343343
344+ def anchor_origin (run_start , first_orbit , orbitsPerTF , ITS_rampup , explicit_timeframe ):
345+ """
346+ Returns the (start of run in ms, first orbit) from which timeframes are counted.
347+ An explicit timeframe ID (MC-DATA embedding) names a timeframe of the data, counted
348+ from the run's first orbit, so it is not moved past the ITS ramp-up.
349+ """
350+ if explicit_timeframe :
351+ return run_start , first_orbit
352+ return shift_anchor_past_ITS_rampup (run_start , first_orbit , orbitsPerTF , ITS_rampup )
353+
344354def retrieve_MinBias_CTPScaler_Rate (raw_rate_at , finaltime , trig_eff_arg , NBunches , ColSystem , eCM , run_number = - 1 ):
345355 """
346356 Turns the raw CTP counting rate at finaltime (in milliseconds) into the interaction rate for
@@ -506,9 +516,26 @@ def determine_timestamp_from_timeframeID(sor, eor, timeframeID, HBF_per_timefram
506516 # this is a closure test. If we had perfect floating point precision everywhere, it wouldn't fail.
507517 # But since we don't have that and there are some int casts as well, better check again.
508518 assert (timestamp_of_production >= sor )
509- assert ( timestamp_of_production <= eor )
519+ # no upper bound: a timeframe taken from data may start after the end of run recorded in CCDB
510520 return int (timestamp_of_production )
511521
522+ def conditions_timestamp (timestamp , eor ):
523+ """
524+ The timestamp for condition lookups of a timeframe taken from data. A timeframe starting after
525+ the recorded end of run keeps its position in the run, but its conditions are read just before
526+ the end of run, where the run's objects are still valid.
527+ """
528+ return min (timestamp , eor - 1 )
529+
530+ def rate_lookup_time (timestamp , run_start , collisions_end , explicit_timeframe ):
531+ """
532+ The time at which to read the CTP interaction rate. For a timeframe taken from data, which
533+ may lie past the last CTP scaler record, this is clamped to just before the collisions end.
534+ """
535+ if explicit_timeframe :
536+ return max (run_start , min (timestamp , collisions_end - 1000 ))
537+ return timestamp
538+
512539def exclude_timestamp (ts , orbit , run , filename , global_run_params ):
513540 """
514541 Checks if timestamp ts (or orbit) falls within a bad data period.
@@ -614,7 +641,7 @@ def main():
614641 # Adjust the anchoring point using the ITS ramp-up period
615642 ITS_rampup = retrieve_ITS_RampDuration (ccdbreader , run_start )
616643 print (f"ITS ramp-up time: { ITS_rampup } ms" )
617- effective_run_start , effective_first_orbit = shift_anchor_past_ITS_rampup (run_start , GLOparams ["FirstOrbit" ], GLOparams ["OrbitsPerTF" ], ITS_rampup )
644+ effective_run_start , effective_first_orbit = anchor_origin (run_start , GLOparams ["FirstOrbit" ], GLOparams ["OrbitsPerTF" ], ITS_rampup , args . timeframeID != - 1 )
618645 mid_run_timestamp = (effective_run_start + run_end ) // 2
619646
620647 # --------
@@ -685,9 +712,10 @@ def main():
685712 timestamp = 0
686713 prod_offset = 0
687714 if args .timeframeID != - 1 :
688- # an explicit timeframe is a fixed position in the run, so bound it by the run as recorded;
689- # if it falls past the collisions the rate below is zero and the job is excluded
715+ # an explicit timeframe comes from data: it exists even past the recorded end of run,
716+ # and its interaction rate is read at the last time the CTP counted collisions
690717 timestamp = determine_timestamp_from_timeframeID (effective_run_start , GLOparams ["EOR" ], args .timeframeID , GLOparams ["OrbitsPerTF" ])
718+ timestamp = conditions_timestamp (timestamp , GLOparams ["EOR" ])
691719 prod_offset = args .timeframeID
692720 else :
693721 timestamp , prod_offset = determine_timestamp (effective_run_start , run_end , [args .split_id - 1 , args .prod_split ], args .cycle , args .tf , GLOparams ["OrbitsPerTF" ])
@@ -720,7 +748,8 @@ def main():
720748 ctp_local_rate_raw = None
721749
722750 if args .ccdb_IRate == True :
723- rate , ctp_local_rate_raw = retrieve_MinBias_CTPScaler_Rate (raw_rate_at , timestamp , args .trig_eff , grplhcif .getBunchFilling ().getNBunches (), ColSystem , eCM , args .run_number )
751+ rate_time = rate_lookup_time (timestamp , effective_run_start , run_end , args .timeframeID != - 1 )
752+ rate , ctp_local_rate_raw = retrieve_MinBias_CTPScaler_Rate (raw_rate_at , rate_time , args .trig_eff , grplhcif .getBunchFilling ().getNBunches (), ColSystem , eCM , args .run_number )
724753
725754 # no collisions here, so nothing to simulate: treat it like any other excluded timestamp
726755 if rate == None and ctp_local_rate_raw == 0 :
0 commit comments