Skip to content

Rewritten Run-3 TPC event builder for the new Run3 TPC firmware#4292

Merged
osbornjd merged 35 commits into
sPHENIX-Collaboration:masterfrom
blackcathj:TPCTimeFrameBuilder6
Jun 11, 2026
Merged

Rewritten Run-3 TPC event builder for the new Run3 TPC firmware#4292
osbornjd merged 35 commits into
sPHENIX-Collaboration:masterfrom
blackcathj:TPCTimeFrameBuilder6

Conversation

@blackcathj

@blackcathj blackcathj commented Jun 8, 2026

Copy link
Copy Markdown
Member

Types of changes

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to not work for users)
  • Requiring change in macros repository (Please provide links to the macros pull request in the last section)
  • I am a member of GitHub organization of sPHENIX Collaboration, EIC, or ECCE (contact Chris Pinkenburg to join)

What kind of change does this PR introduce? (Bug fix, feature, ...)

This is a rewritten Run-3 TPC timeframe and event builder for the Run3 TPC firmware (IDTPCFEEV5/IDTPCFEEV6 packets) while preserving the existing TpcTimeFrameBuilder behavior for legacy Run2 firmware (IDTPCFEEV4). Run3 has a deterministic clock frequency, locking FEE to FELIX clock. By separating Run3 event building from Run2, it allows for a clean event building with a deterministic association of FEE waveforms to GL1 trigger.

This PR also implement another long planned feature to rebuild/add the SAMPA RawHit at event building stage to recover full-time-length SAMPA waveforms that was truncated by the next trigger if it comes with in 100us in spacing. This is needed to allow for use all statistics for the extended streaming window, allowing collecting the entire TPC drift window which during data taking is truncated to be associated to the next event trigger.

List of main changes:

  • Adds TpcTimeFrameBuilderRun3 with exact integer GTM-to-FEE clock matching using the 30/8 clock ratio and 20-bit FEE rollover handling.
  • Triage in SingleTpcTimeFrameInput to automatically select for the new event builder for Run3
  • QA histograms for all new function
  • Adds a TPC event builder base interface so input code can hold Run-2 and Run-3 builders uniformly.

AI usage disclosure

The agentic AI tool Codex was used to write majority of the code under the "goal mode", in which specific instruction were given to codex on how to perform event building then Codex implemented it in code and run code checker/performance bench marker.

The cost of both writing and testing was about 7M input tokens and 0.5M output tokens. Codex speed up code writing by about x10 and debugging by about x2.

Validations

Tested code checker and performance check with https://github.com/blackcathj/macros/blob/f8f7e2ed6b61233531bd7fdb4e13e32f8f8372d6/StreamingProduction/Fun4All_TPC_SingleStream_Combiner.C

output QA ROOT histograms.

The QA plot generated with 0.5M events are summarized in this notebook :
https://nbviewer.sphenix.bnl.gov/github/blackcathj/macros/blob/f8f7e2ed6b61233531bd7fdb4e13e32f8f8372d6/StreamingProduction/qa_plot.ipynb

In particular, it shows a sharp coincidence peak within 1 RHIC BCO which 4 FEE clock (40Mhz)
image

It also shows the expected second trigger truncation (1st plot) and after the truncation recovery refilling of the ADC bins (2nd plot)

image image

Callgrind shows the new Run-3 QA/cache/fill path is a small fraction of runtime

The dominating part is still CRC calculation + DST ROOT compression

callgrind out fulltest200 profile

Massif memory usage is stable and below 2GB

Fun4All_TPC_SingleStream_Combiner_fulltest200 massif-timeline

TODO

  • Testing with production and offline analysis by Joe and Christof
  • 100% streaming event building, which require not only fetching for future waveforms, but also past waveforms, which would be defined relative to a time window marker such as INTT strobe instead of GL1 trigger.

Summary

Motivation / Context

Enable correct TPC timeframe/event building for Run‑3 firmware (IDTPCFEEV5/IDTPCFEEV6) while preserving Run‑2 behavior (IDTPCFEEV4). Provide exact GTM↔FEE clock alignment and recover full SAMPA waveforms truncated by nearby triggers so the full TPC drift window is available for reconstruction and QA.

Key changes

  • Added TpcTimeFrameBuilderBase: abstract interface to allow uniform caller use of Run‑2 and Run‑3 builders.
  • Added TpcTimeFrameBuilderRun3:
    • Implements exact integer GTM→FEE clock matching using the 30/8 ratio and 20‑bit FEE rollover handling.
    • GTM/FEE BCO matching bookkeeping and prediction utilities.
    • SAMPA waveform reconstruction/recovery to restore full-length RawHits truncated by subsequent triggers (~100 µs window).
    • Digital‑current payload parsing, QA histograms, profiling hooks (Callgrind, Massif) and a diagnostic TTree.
    • Many Run‑3 specific parsing helpers, CRC/parity and bit‑order utilities.
  • Adapted existing TpcTimeFrameBuilder (Run‑2) to inherit from the new base interface and added override annotations.
  • SingleTpcTimeFrameInput:
    • Auto‑selects Run‑2 vs Run‑3 builder based on packet hit format (enforces per‑packet_id consistency).
    • Propagates per‑iterator FillPool status and performs robust cleanup on early exit.
  • Fun4AllStreamingInputManager: honors and propagates FillPoolStatus from iterators.
  • TpcRawHitv3: exposes type aliases and a const getter for ADC waveform containers to support waveform recovery/consumption.
  • Build changes: install/build additions for the new headers and Run‑3 source compilation.
  • QA artifacts and validation notebooks/images included (0.5M events QA, profiling results).

Potential risk areas

  • IO/Parsing correctness: Run‑3 parsing is substantially more complex (DMA/GTM/FEE payloads, CRC/parity, bit reversals, rollover corrections). Must be validated against real detector data and edge cases.
  • Reconstruction behavior: Restored waveforms change hit shapes and integrated quantities — cross‑validation with legacy reconstruction and physics QA is required.
  • Memory & performance: New caches, waveform buffers and QA histograms increase memory; profiling showed ~1.4 GB peaks in a small Combiner test. Performance at full data rates must be monitored and tuned.
  • Thread safety & ownership: Polymorphic builders, additional caches and lifetime management introduce concurrency and ownership concerns; ensure safe use in multi‑threaded contexts or document single‑threaded requirements.
  • Runtime robustness: The code aborts on in‑stream hit‑format inconsistencies by design; this can affect inputs with mixed or mis‑marked packet formats and should be handled at the ingest level if mixed streams are possible.

Possible future improvements

  • Implement full streaming event building: fetch past and future waveforms relative to arbitrary time markers instead of relying on GL1 triggers.
  • Scale validation: run production/offline tests across full detector datasets and integrate regression/QA tests.
  • Optimize memory and algorithms: shrink matching caches, reduce per‑event footprint, and tune hot paths for high‑rate streaming.
  • Add unit/integration tests for parsing, CRC/parity, rollover logic and rare edge cases.
  • Consider clearer run‑time diagnostics or configurable fallback behaviors for mixed/ambiguous input formats.

Note on AI assistance: portions of the implementation and tests were developed with AI assistance (Codex). AI can be mistaken—review clock‑matching, BCO‑rollover handling, parsing and waveform‑recovery logic carefully and validate with independent tests.

@blackcathj blackcathj requested a review from osbornjd June 8, 2026 20:37
@coderabbitai

coderabbitai Bot commented Jun 8, 2026

Copy link
Copy Markdown
Contributor

Review Change Stack

Important

Review skipped

Review was skipped as selected files did not have any reviewable changes.

💤 Files selected but had no reviewable changes (1)
  • offline/framework/fun4allraw/TpcTimeFrameBuilderRun3.cc
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro

Run ID: ec8ffa0c-ffae-4c06-b7e3-8bbea0f90ad6

📥 Commits

Reviewing files that changed from the base of the PR and between f57ffff and 204e425.

📒 Files selected for processing (1)
  • offline/framework/fun4allraw/TpcTimeFrameBuilderRun3.cc

You can disable this status message by setting the reviews.review_status to false in the CodeRabbit configuration file.

Use the checkbox below for a quick retry:

  • 🔍 Trigger review
📝 Walkthrough

Walkthrough

Adds a TpcTimeFrameBuilderBase interface; refactors TpcTimeFrameBuilder to inherit and override it; introduces TpcTimeFrameBuilderRun3 with Run3 parsing/BCO matching; updates SingleTpcTimeFrameInput to select builders at runtime, propagate FillPool status, and adds TpcRawHitv3 ADC waveform accessors.

Changes

TPC Time-Frame Builder Polymorphism

Layer / File(s) Summary
Builder Interface Definition
offline/framework/fun4allraw/TpcTimeFrameBuilderBase.h
New abstract TpcTimeFrameBuilderBase class defines pure virtual methods for packet processing, timeframe retrieval, data sufficiency checks, cleanup, verbosity, bad-fee mapping, and debug output saving.
Existing Builder Refactoring
offline/framework/fun4allraw/TpcTimeFrameBuilder.h
TpcTimeFrameBuilder now inherits from TpcTimeFrameBuilderBase with overridden methods to match the base contract.
Run3 Builder Implementation and build files
offline/framework/fun4allraw/TpcTimeFrameBuilderRun3.h, offline/framework/fun4allraw/Makefile.am
New TpcTimeFrameBuilderRun3 implements Run3-specific packet parsing, payload structs, BCO matching (BcoMatchingInformation), waveform recovery/caching, QA histograms/timers, and a DigitalCurrentDebugTTree; build files updated to install/compile new headers/implementation.
Streaming Input Migration
offline/framework/fun4allraw/SingleTpcTimeFrameInput.h, offline/framework/fun4allraw/SingleTpcTimeFrameInput.cc
Input code now uses TpcTimeFrameBuilderBase* map, validates per-packet hit-format consistency, creates appropriate builders at runtime (v4 → TpcTimeFrameBuilder, v5/v6 → TpcTimeFrameBuilderRun3), adds FillPoolStatus() tracking, and aborts/cleans up on errors.
Framework Integration and Data Exposure
offline/framework/fun4allraw/SingleStreamingInput.h, offline/framework/fun4allraw/Fun4AllStreamingInputManager.cc, offline/framework/ffarawobjects/TpcRawHitv3.h
SingleStreamingInput adds FillPoolStatus() virtual; Fun4AllStreamingInputManager::FillTpcPool() checks FillPoolStatus() after pool fill and returns on failure; TpcRawHitv3 adds ADC waveform type aliases and a const getter get_adc_waveforms().

Possibly Related PRs


Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 1


ℹ️ Review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro

Run ID: f8a19664-e6f1-4a8e-bd1b-fac9da58e06c

📥 Commits

Reviewing files that changed from the base of the PR and between 82cee04 and c2e3c3a.

📒 Files selected for processing (10)
  • offline/framework/ffarawobjects/TpcRawHitv3.h
  • offline/framework/fun4allraw/Fun4AllStreamingInputManager.cc
  • offline/framework/fun4allraw/Makefile.am
  • offline/framework/fun4allraw/SingleStreamingInput.h
  • offline/framework/fun4allraw/SingleTpcTimeFrameInput.cc
  • offline/framework/fun4allraw/SingleTpcTimeFrameInput.h
  • offline/framework/fun4allraw/TpcTimeFrameBuilder.h
  • offline/framework/fun4allraw/TpcTimeFrameBuilderBase.h
  • offline/framework/fun4allraw/TpcTimeFrameBuilderRun3.cc
  • offline/framework/fun4allraw/TpcTimeFrameBuilderRun3.h

Comment thread offline/framework/fun4allraw/TpcTimeFrameBuilderRun3.h
@blackcathj

Copy link
Copy Markdown
Member Author

clang-tidy errors are from header file mismatch in nightly builds

@sphenix-jenkins-ci

Copy link
Copy Markdown

Build & test report

Report for commit c2e3c3aef1b18971f2e40e52921c1889a2d2eff6:
Jenkins on fire


Automatically generated by sPHENIX Jenkins continuous integration
sPHENIX             jenkins.io

@sphenix-jenkins-ci

Copy link
Copy Markdown

Build & test report

Report for commit c53fbbf0e43a56c7d184cc3e092d7e0163766bce:
Jenkins on fire


Automatically generated by sPHENIX Jenkins continuous integration
sPHENIX             jenkins.io

@blackcathj

blackcathj commented Jun 11, 2026

Copy link
Copy Markdown
Member Author

Latest patches handle a rare operation scenario that GL1 trigger is paused for a long time while TPC kept taking streaming data. In the past this lead to increased memory usage to ~10GB to buffer the hits in searching for the next trigger in TPC. The new patch drop hits unless within 7ms of the trigger (1/4 turn of FEE clock rollover) to mitigate memory usage.

@osbornjd

Copy link
Copy Markdown
Contributor
image Christof's latest QA check suggests that the improvement can also be verified in the downstream clusterizer

@osbornjd osbornjd left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

After some testing I believe this is ready to go in, and clang tidy warnings should be addressed. The errors are spurious

@sphenix-jenkins-ci

Copy link
Copy Markdown

Build & test report

Report for commit f57ffffae66eb256056101a3a7f561cb6378ce22:
Jenkins on fire


Automatically generated by sPHENIX Jenkins continuous integration
sPHENIX             jenkins.io

@sphenix-jenkins-ci

Copy link
Copy Markdown

Build & test report

Report for commit 204e425afa9e1a9ea15bb61d32613e6d60329c18:
Jenkins on fire


Automatically generated by sPHENIX Jenkins continuous integration
sPHENIX             jenkins.io

@osbornjd osbornjd merged commit 0ffd526 into sPHENIX-Collaboration:master Jun 11, 2026
19 of 22 checks passed
@hupereir hupereir mentioned this pull request Jun 11, 2026
5 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants