Skip to content

Commit 54ec2ac

Browse files
sawenzelclaude
andcommitted
Anchor explicit timeframe IDs to their place in the data
This fixes the anchoring of data timeframes for MC-DATA embedding and adds unit tests. - An explicit timeframe ID is counted from the run's first orbit, as in the data, not from the start moved past the ITS ramp-up. - A data timeframe may start after the end of run recorded in CCDB; it is no longer refused. - Its interaction rate is read at the last time the CTP counted collisions. - Its conditions are read just before the end of run, where the run's objects are valid. - Sampled anchored jobs are unchanged. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
1 parent dc51651 commit 54ec2ac

2 files changed

Lines changed: 96 additions & 5 deletions

File tree

‎MC/bin/o2dpg_sim_workflow_anchored.py‎

Lines changed: 34 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -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+
344354
def 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+
512539
def 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:
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
#!/usr/bin/env python3
2+
"""Offline test: an explicit timeframe ID is placed from the unshifted run start (MC-DATA embedding)."""
3+
import os
4+
import sys
5+
import unittest
6+
7+
HERE = os.path.dirname(os.path.abspath(__file__))
8+
sys.path.insert(0, os.path.dirname(HERE))
9+
10+
os.environ.setdefault("O2DPG_ROOT", os.path.dirname(os.path.dirname(os.path.dirname(HERE))))
11+
12+
try:
13+
import o2dpg_sim_workflow_anchored as anchored # needs ROOT
14+
except ImportError as exc: # pragma: no cover
15+
anchored = None
16+
17+
SOR = 1778806732526
18+
FIRST_ORBIT = 20505888
19+
ORBITS_PER_TF = 32
20+
ITS_RAMPUP_MS = 5000
21+
22+
23+
@unittest.skipIf(anchored is None, "o2dpg_sim_workflow_anchored not importable (needs ROOT)")
24+
class TestTimeframeIdAnchor(unittest.TestCase):
25+
26+
def test_explicit_timeframe_ignores_rampup_shift(self):
27+
start, orbit = anchored.anchor_origin(SOR, FIRST_ORBIT, ORBITS_PER_TF, ITS_RAMPUP_MS, explicit_timeframe=True)
28+
self.assertEqual((start, orbit), (SOR, FIRST_ORBIT))
29+
# the MC timeframe tf then starts where the data timeframe tf starts
30+
tf = 10
31+
self.assertEqual(orbit + tf * ORBITS_PER_TF, FIRST_ORBIT + 320)
32+
33+
def test_sampled_timeframe_keeps_rampup_shift(self):
34+
self.assertEqual(anchored.anchor_origin(SOR, FIRST_ORBIT, ORBITS_PER_TF, ITS_RAMPUP_MS, explicit_timeframe=False),
35+
anchored.shift_anchor_past_ITS_rampup(SOR, FIRST_ORBIT, ORBITS_PER_TF, ITS_RAMPUP_MS))
36+
37+
38+
@unittest.skipIf(anchored is None, "o2dpg_sim_workflow_anchored not importable (needs ROOT)")
39+
class TestEndOfRun(unittest.TestCase):
40+
"""Data timeframes can lie a little past the end of run recorded in CCDB."""
41+
EOR = SOR + 2232696 # run 544653: last data TF starts about 0.6 s after EOR
42+
43+
def test_explicit_timeframe_past_eor_is_accepted(self):
44+
tf = 784641
45+
ts = anchored.determine_timestamp_from_timeframeID(SOR, self.EOR, tf, ORBITS_PER_TF)
46+
self.assertGreater(ts, self.EOR)
47+
48+
def test_rate_lookup_time_clamped_for_explicit_timeframe(self):
49+
collisions_end = self.EOR - 500
50+
self.assertEqual(anchored.rate_lookup_time(self.EOR + 600, SOR, collisions_end, True), collisions_end - 1000)
51+
self.assertEqual(anchored.rate_lookup_time(SOR + 5000, SOR, collisions_end, True), SOR + 5000)
52+
53+
def test_conditions_timestamp_clamped_to_run(self):
54+
self.assertEqual(anchored.conditions_timestamp(self.EOR + 68, self.EOR), self.EOR - 1)
55+
self.assertEqual(anchored.conditions_timestamp(self.EOR - 5000, self.EOR), self.EOR - 5000)
56+
57+
def test_rate_lookup_time_unchanged_for_sampled(self):
58+
self.assertEqual(anchored.rate_lookup_time(self.EOR + 600, SOR, self.EOR, False), self.EOR + 600)
59+
60+
61+
if __name__ == "__main__":
62+
unittest.main()

0 commit comments

Comments
 (0)