Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions MC/bin/o2dpg_sim_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,9 @@ def add(cfg, flatconfig):
if not getattr(args, "with_ZDC", False) and "ZDC" not in getattr(args, "skipModules", []):
add(config, {"ZDCSimParam.buildBeamLine" : "false"})
add(config, {"GenTPCLoopers.colsys" : args.col})
# MC-DATA embedding simulates small signals at the data collisions; no TPC loopers
if len(getattr(args, "data_anchoring", "")) > 0:
add(config, {"GenTPCLoopers.loopersVeto" : "true"})

# ----- apply external overwrites from command line -------
for keyval in externalConfigString.split(";"):
Expand All @@ -199,6 +202,15 @@ def add(cfg, flatconfig):

return config

def qed_included(pdga, pdgb, with_qed, no_qed, data_anchoring):
"""
Whether to add the QED background. On by default for same-species heavy-ion collisions,
off for MC-DATA embedding (the data already carries it). --with-qed forces it on,
--no-qed forces it off and wins over everything.
"""
default = pdga == pdgb and pdga != 2212 and len(data_anchoring) == 0
return (default or with_qed) and not no_qed

def constructConfigKeyArg(config):
# flattens dictionary constructed in create_geant_config
# and constructs the --configKeyValues options for simulation
Expand Down
14 changes: 8 additions & 6 deletions MC/bin/o2dpg_sim_workflow.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@

from o2dpg_workflow_utils import createTask, createGlobalInitTask, dump_workflow, adjust_RECO_environment, isActive, activate_detector, deactivate_detector, compute_n_workers, merge_dicts
from o2dpg_qc_finalization_workflow import include_all_QC_finalization
from o2dpg_sim_config import create_sim_config, create_geant_config, constructConfigKeyArg, option_if_available, overwrite_config
from o2dpg_sim_config import create_sim_config, create_geant_config, constructConfigKeyArg, option_if_available, overwrite_config, qed_included
from o2dpg_dpl_config_tools import dpl_option_from_config, TaskFinalizer, quote_if_needed

# for some JAliEn interaction
Expand Down Expand Up @@ -643,11 +643,9 @@ def getDPL_global_options(bigshm=False, ccdbbackend=True, runcommand=True):

workflow['stages'].append(GRP_TASK)

# QED background is enabled by default only for same-species (heavy-ion) collisions.
# --with-qed forces it on; --no-qed forces it off and wins over both the default and --with-qed
# (the two flags are mutually exclusive at the argparse level, so they can never both be set).
QED_enabled = True if (PDGA==PDGB and PDGA!=2212) else False
includeQED = (QED_enabled or args.with_qed) and not args.no_qed
# QED background: on by default for same-species heavy-ion collisions, off for MC-DATA embedding;
# --with-qed forces it on, --no-qed forces it off (see qed_included)
includeQED = qed_included(PDGA, PDGB, args.with_qed, args.no_qed, args.data_anchoring)
signalprefix='sgn'

# No vertexing for event pool generation; otherwise the vertex comes from CCDB and later from CollContext
Expand Down Expand Up @@ -714,6 +712,10 @@ def getDPL_global_options(bigshm=False, ccdbbackend=True, runcommand=True):
' --nontrivial-mu-distribution ccdb://https://alice-ccdb.cern.ch/FT0/Calib/EventsPerBc',
f'--QEDinteraction {qedspec}' if includeQED else None
], configname = 'precollcontext')
if len(args.data_anchoring) > 0:
# contexts taken from data carry no interaction rate; store the one used for this production
PreCollContextTask['cmd'] += ' && python3 ${O2DPG_ROOT}/MC/utils/o2dpg_data_embedding_utils.py --set-interaction-rate ' + str(INTRATE) \
+ ' --context ' + ' '.join(f'tf{tf}/collisioncontext.root' for tf in range(1, NTIMEFRAMES + 1))
workflow['stages'].append(PreCollContextTask)

if doembedding:
Expand Down
39 changes: 34 additions & 5 deletions MC/bin/o2dpg_sim_workflow_anchored.py
Original file line number Diff line number Diff line change
Expand Up @@ -341,6 +341,16 @@ def shift_anchor_past_ITS_rampup(run_start, first_orbit, orbitsPerTF, ITS_rampup
rampupOrbits = -(-milliseconds_to_orbits(math.ceil(ITS_rampup)) // orbitsPerTF) * orbitsPerTF
return run_start + int(rampupOrbits * LHCOrbitMUS / 1000.), first_orbit + rampupOrbits

def anchor_origin(run_start, first_orbit, orbitsPerTF, ITS_rampup, explicit_timeframe):
"""
Returns the (start of run in ms, first orbit) from which timeframes are counted.
An explicit timeframe ID (MC-DATA embedding) names a timeframe of the data, counted
from the run's first orbit, so it is not moved past the ITS ramp-up.
"""
if explicit_timeframe:
return run_start, first_orbit
return shift_anchor_past_ITS_rampup(run_start, first_orbit, orbitsPerTF, ITS_rampup)

def retrieve_MinBias_CTPScaler_Rate(raw_rate_at, finaltime, trig_eff_arg, NBunches, ColSystem, eCM, run_number = -1):
"""
Turns the raw CTP counting rate at finaltime (in milliseconds) into the interaction rate for
Expand Down Expand Up @@ -506,9 +516,26 @@ def determine_timestamp_from_timeframeID(sor, eor, timeframeID, HBF_per_timefram
# this is a closure test. If we had perfect floating point precision everywhere, it wouldn't fail.
# But since we don't have that and there are some int casts as well, better check again.
assert (timestamp_of_production >= sor)
assert (timestamp_of_production <= eor)
# no upper bound: a timeframe taken from data may start after the end of run recorded in CCDB
return int(timestamp_of_production)

def conditions_timestamp(timestamp, eor):
"""
The timestamp for condition lookups of a timeframe taken from data. A timeframe starting after
the recorded end of run keeps its position in the run, but its conditions are read just before
the end of run, where the run's objects are still valid.
"""
return min(timestamp, eor - 1)

def rate_lookup_time(timestamp, run_start, collisions_end, explicit_timeframe):
"""
The time at which to read the CTP interaction rate. For a timeframe taken from data, which
may lie past the last CTP scaler record, this is clamped to just before the collisions end.
"""
if explicit_timeframe:
return max(run_start, min(timestamp, collisions_end - 1000))
return timestamp

def exclude_timestamp(ts, orbit, run, filename, global_run_params):
"""
Checks if timestamp ts (or orbit) falls within a bad data period.
Expand Down Expand Up @@ -614,7 +641,7 @@ def main():
# Adjust the anchoring point using the ITS ramp-up period
ITS_rampup = retrieve_ITS_RampDuration(ccdbreader, run_start)
print(f"ITS ramp-up time: {ITS_rampup} ms")
effective_run_start, effective_first_orbit = shift_anchor_past_ITS_rampup(run_start, GLOparams["FirstOrbit"], GLOparams["OrbitsPerTF"], ITS_rampup)
effective_run_start, effective_first_orbit = anchor_origin(run_start, GLOparams["FirstOrbit"], GLOparams["OrbitsPerTF"], ITS_rampup, args.timeframeID != -1)
mid_run_timestamp = (effective_run_start + run_end) // 2

# --------
Expand Down Expand Up @@ -685,9 +712,10 @@ def main():
timestamp = 0
prod_offset = 0
if args.timeframeID != -1:
# an explicit timeframe is a fixed position in the run, so bound it by the run as recorded;
# if it falls past the collisions the rate below is zero and the job is excluded
# an explicit timeframe comes from data: it exists even past the recorded end of run,
# and its interaction rate is read at the last time the CTP counted collisions
timestamp = determine_timestamp_from_timeframeID(effective_run_start, GLOparams["EOR"], args.timeframeID, GLOparams["OrbitsPerTF"])
timestamp = conditions_timestamp(timestamp, GLOparams["EOR"])
prod_offset = args.timeframeID
else:
timestamp, prod_offset = determine_timestamp(effective_run_start, run_end, [args.split_id - 1, args.prod_split], args.cycle, args.tf, GLOparams["OrbitsPerTF"])
Expand Down Expand Up @@ -720,7 +748,8 @@ def main():
ctp_local_rate_raw = None

if args.ccdb_IRate == True:
rate, ctp_local_rate_raw = retrieve_MinBias_CTPScaler_Rate(raw_rate_at, timestamp, args.trig_eff, grplhcif.getBunchFilling().getNBunches(), ColSystem, eCM, args.run_number)
rate_time = rate_lookup_time(timestamp, effective_run_start, run_end, args.timeframeID != -1)
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)

# no collisions here, so nothing to simulate: treat it like any other excluded timestamp
if rate == None and ctp_local_rate_raw == 0:
Expand Down
62 changes: 62 additions & 0 deletions MC/bin/tests/test_anchoring_timeframeid.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
#!/usr/bin/env python3
"""Offline test: an explicit timeframe ID is placed from the unshifted run start (MC-DATA embedding)."""
import os
import sys
import unittest

HERE = os.path.dirname(os.path.abspath(__file__))
sys.path.insert(0, os.path.dirname(HERE))

os.environ.setdefault("O2DPG_ROOT", os.path.dirname(os.path.dirname(os.path.dirname(HERE))))

try:
import o2dpg_sim_workflow_anchored as anchored # needs ROOT
except ImportError as exc: # pragma: no cover
anchored = None

SOR = 1778806732526
FIRST_ORBIT = 20505888
ORBITS_PER_TF = 32
ITS_RAMPUP_MS = 5000


@unittest.skipIf(anchored is None, "o2dpg_sim_workflow_anchored not importable (needs ROOT)")
class TestTimeframeIdAnchor(unittest.TestCase):

def test_explicit_timeframe_ignores_rampup_shift(self):
start, orbit = anchored.anchor_origin(SOR, FIRST_ORBIT, ORBITS_PER_TF, ITS_RAMPUP_MS, explicit_timeframe=True)
self.assertEqual((start, orbit), (SOR, FIRST_ORBIT))
# the MC timeframe tf then starts where the data timeframe tf starts
tf = 10
self.assertEqual(orbit + tf * ORBITS_PER_TF, FIRST_ORBIT + 320)

def test_sampled_timeframe_keeps_rampup_shift(self):
self.assertEqual(anchored.anchor_origin(SOR, FIRST_ORBIT, ORBITS_PER_TF, ITS_RAMPUP_MS, explicit_timeframe=False),
anchored.shift_anchor_past_ITS_rampup(SOR, FIRST_ORBIT, ORBITS_PER_TF, ITS_RAMPUP_MS))


@unittest.skipIf(anchored is None, "o2dpg_sim_workflow_anchored not importable (needs ROOT)")
class TestEndOfRun(unittest.TestCase):
"""Data timeframes can lie a little past the end of run recorded in CCDB."""
EOR = SOR + 2232696 # run 544653: last data TF starts about 0.6 s after EOR

def test_explicit_timeframe_past_eor_is_accepted(self):
tf = 784641
ts = anchored.determine_timestamp_from_timeframeID(SOR, self.EOR, tf, ORBITS_PER_TF)
self.assertGreater(ts, self.EOR)

def test_rate_lookup_time_clamped_for_explicit_timeframe(self):
collisions_end = self.EOR - 500
self.assertEqual(anchored.rate_lookup_time(self.EOR + 600, SOR, collisions_end, True), collisions_end - 1000)
self.assertEqual(anchored.rate_lookup_time(SOR + 5000, SOR, collisions_end, True), SOR + 5000)

def test_conditions_timestamp_clamped_to_run(self):
self.assertEqual(anchored.conditions_timestamp(self.EOR + 68, self.EOR), self.EOR - 1)
self.assertEqual(anchored.conditions_timestamp(self.EOR - 5000, self.EOR), self.EOR - 5000)

def test_rate_lookup_time_unchanged_for_sampled(self):
self.assertEqual(anchored.rate_lookup_time(self.EOR + 600, SOR, self.EOR, False), self.EOR + 600)


if __name__ == "__main__":
unittest.main()
54 changes: 54 additions & 0 deletions MC/bin/tests/test_sim_config_embedding.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
#!/usr/bin/env python3
"""Offline tests: MC-DATA embedding switches the TPC looper generator and the QED background off."""
import argparse
import os
import sys
import unittest

HERE = os.path.dirname(os.path.abspath(__file__))
sys.path.insert(0, os.path.dirname(HERE))

import o2dpg_sim_config as simconfig


def args(**kw):
base = dict(gen="pythia8pp", col="PbPb", with_ZDC=False, skipModules=[], data_anchoring="")
base.update(kw)
return argparse.Namespace(**base)


class TestLoopersVeto(unittest.TestCase):

def test_data_anchoring_vetoes_loopers(self):
cfg = simconfig.create_geant_config(args(data_anchoring="ctx.root"), "")
self.assertEqual(cfg["GenTPCLoopers"].get("loopersVeto"), "true")

def test_ordinary_production_keeps_loopers(self):
cfg = simconfig.create_geant_config(args(), "")
self.assertNotIn("loopersVeto", cfg["GenTPCLoopers"])

def test_explicit_key_wins(self):
cfg = simconfig.create_geant_config(args(data_anchoring="ctx.root"), "GenTPCLoopers.loopersVeto=false")
self.assertEqual(cfg["GenTPCLoopers"].get("loopersVeto"), "false")


class TestQED(unittest.TestCase):
PB = 1000822080

def test_default_on_for_heavy_ions_only(self):
self.assertTrue(simconfig.qed_included(self.PB, self.PB, False, False, ""))
self.assertFalse(simconfig.qed_included(2212, 2212, False, False, ""))

def test_flags(self):
self.assertTrue(simconfig.qed_included(2212, 2212, True, False, ""))
self.assertFalse(simconfig.qed_included(self.PB, self.PB, False, True, ""))

def test_data_anchoring_switches_qed_off(self):
self.assertFalse(simconfig.qed_included(self.PB, self.PB, False, False, "ctx.root"))

def test_with_qed_wins_over_data_anchoring(self):
self.assertTrue(simconfig.qed_included(self.PB, self.PB, True, False, "ctx.root"))


if __name__ == "__main__":
unittest.main()
Loading
Loading