Developing experiment software with Gaudi: the toyexp example

The GaudiExamples/toyexp directory of the Gaudi sources contains a small example of how an experiment (Toy Experiment) can build its software on top of Gaudi. It is meant as a template: the structure, not the physics, is what matters.

The processing chain

The software of a HEP experiment is usually organized as a chain of steps, each one an application (a Gaudi job) that reads the output of the previous one:

simulation -> digitization -> filter (trigger) -> reconstruction -> analysis
simulation

generates events (the Monte Carlo truth: particles and vertices) and simulates how they interact with the detector (hits).

digitization

converts the hits into what the readout electronics would deliver (raw data, here digit). From this point the truth must not be used anymore.

filter

the trigger (HLT) selects the events worth keeping. In a real experiment it needs a (fast) reconstruction, so it is often merged with the next step.

reconstruction

finds the particles that produced the signals in the detector (here track).

analysis

extracts physics results from the reconstructed objects.

The example implements a reduced chain: simulation (including digitization, which in this toy is a simple algorithm of the simulation job), reconstruction and analysis. A filter would be an algorithm of the reconstruction job, a Gaudi::Functional::FilterPredicate returning whether an event is accepted, placed in a sequence before the writer so that only accepted events are saved. (Note that the analysis then must not assume that entry n of the output file is event n of the input file.)

Project layout

An experiment is not a single CMake project: toyexp is split in layers (Core, Sim, …) that depend on Gaudi and on the lower layers. Each layer is an ordinary CMake project that gets Gaudi with find_package(Gaudi), or uses the targets already defined when it is built as part of the Gaudi build (-DGAUDI_BUILD_EXAMPLES=ON).

GaudiExamples/toyexp
├── Core
│   ├── EventModel    data classes (MC truth, digits, tracks) + dictionary
│   └── Geometry      detector planes and pixels (header-only library)
├── Sim
│   ├── Gen           event generation (gen_init, elastic)
│   ├── Det           detector simulation (hit_maker)
│   ├── Digi          digitization (digitizer)
│   └── basic_sim_job.py
├── Reco
│   ├── src           pattern recognition (tracking)
│   └── basic_reco_job.py
└── Ana
    ├── src           analysis algorithm (track_counter)
    └── basic_ana_job.py

Why separate projects? Each one has its own dependencies and release cycle: analysis users, for example, need the event model but not the simulation code.

The event model

The event data classes are plain C++ structs (toyexp/mc/*.h, toyexp/raw/digit.h, toyexp/reco/track.h) exposed in a library created with gaudi_add_library. They are grouped by the step that produces them. To make them storable in ROOT files the library comes with a dictionary created with gaudi_add_dictionary (dict.h including the headers, dict.xml selecting the classes, including the containers, like std::vector<toyexp::raw::digit>, that we want to store). No Gaudi base class is required.

The geometry is needed both by the simulation (to compute where particles hit the detector) and by the reconstruction (to interpret the signals), so it is in Core too, as a header-only library (gaudi_add_header_only_library).

Algorithms

All algorithms are functional (see Writing algorithms): they declare in their signature the data they consume and produce, and the framework takes care of reading and registering data in the transient event store. They are packaged in plugins with gaudi_add_module.

toyexp::mc::gen::init

a Transformer that, for each event, produces a gen_header with the random seeds (derived from the BaseSeed property and the event number, so that events are reproducible independently of the processing order).

toyexp::mc::gen::elastic

a Transformer consuming the header and producing a mc::event with an elastic scattering interaction.

toyexp::mc::det::hit_maker

a Transformer propagating the particles in straight lines through the detector planes and producing a std::vector<mc::hit>.

toyexp::mc::digi::digitizer

a Transformer converting hits into raw::digit (pixel indices and ADC counts) using the properties Gain and Threshold.

toyexp::reco::tracking

a Transformer finding straight tracks in the digits (and nothing else: no MC truth).

toyexp::ana::track_counter

a Consumer filling counters (Gaudi::Accumulators::StatCounter) with the number of tracks per event and of digits per track; the counters are printed at the end of the job by Gaudi::Monitoring::MessageSvcSink.

Configuration

Each basic_*_job.py configures an application with GaudiConfig2. The algorithms are connected by assigning the path of the output of one to the input of the next, which defines the data flow graph:

gen.header = gen_init.header.path()

The order of the algorithms in TopAlg does not matter: Gaudi runs them in the order required by the data dependencies.

All the jobs run multi-threaded (HiveWhiteBoard, AvalancheSchedulerSvc and HiveSlimEventLoopMgr): several events are processed at the same time, each with its own transient event store, and algorithms must be thread safe (their call operators are const). For the same reason the generator does not use the shared random number service, but a random engine per event seeded from the event header, which gives reproducible results independently of the order in which events are processed. As events can complete in any order, the order of the entries in the output files is not guaranteed to be the order of the events.

The names given to the files in FileSvc (input, output) are logical names, used by readers and writers to find their file.

Input and output

Data is written with the generic NTuple writer (see NTuple and event collection facilities) into a file managed by FileSvc. The writer is told which transient store objects to store (Inputs) and their C++ types (FieldTypes); the types are persistified through the dictionary of the event model:

writer = Gaudi.NTuple.GenericTTreeWriter(
    OutputFile="NTuple",
    NTupleName="events",
    Inputs=[obj.path() for obj in outputs],
    FieldTypes=[obj.type() for obj in outputs],
)

Use GenericRNTupleWriter instead to produce an RNTuple.

The reconstruction and analysis jobs read the files with Gaudi.NTuple.GenericReader, which publishes the stored objects in the transient store (ExtraOutputs) so that the algorithms can consume them like any other data. As no event selector is used, the number of events to read must be set with EvtMax; asking for more events than stored makes the reader fail with an “out of range” error.

Running and testing

gaudirun.py GaudiExamples/toyexp/Sim/basic_sim_job.py:gen_elastic_1000
gaudirun.py GaudiExamples/toyexp/Reco/basic_reco_job.py:reco_1000
gaudirun.py GaudiExamples/toyexp/Ana/basic_ana_job.py:ana_tracks_1000

The chain is also run as a test (Sim.pytest.test_basic_sim_job and Ana.pytest.test_basic_ana_job, see the tests/pytest directories).