diff --git a/docs/source/guide/stages.rst b/docs/source/guide/stages.rst new file mode 100644 index 000000000..af7423aec --- /dev/null +++ b/docs/source/guide/stages.rst @@ -0,0 +1,110 @@ +Stages +====== + +A Phlex job is usually one step of a larger processing workflow. +For example, a simulation job writes generated events to a file, a +digitization job reads that file and writes the simulated detector response, +and a reconstruction job reads the digitized data and writes reconstructed +objects. +Each of these steps is a *processing stage*, and each Phlex job runs one +stage. +A job can therefore see products from several stages: products read from +its input files, created by earlier stages, alongside the products it +creates itself. + +A *stage name* identifies the processing stage that created a data product. +Examples of stage names include ``simulation``, ``digitization``, and +``reconstruction``. +Together with the creator, suffix, data layer and type, it is part of a +product's identity. +It allows Phlex to distinguish between products that otherwise look the same +but were created by different Phlex jobs, such as a ``hits`` product read +from a simulation file and a ``hits`` product recomputed in the current job. + +Choose a name that stays the same when an equivalent job is rerun. +Timestamps, host names and batch-job IDs are poor stage names. + +Naming the stage of a job +------------------------- + +Every job must name its stage, either with the top-level ``stage`` key or +with the ``--stage`` option, which takes precedence: + +.. code-block:: jsonnet + + { + stage: 'reconstruction', + driver: { /* ... */ }, + modules: { /* ... */ }, + } + +.. code-block:: console + + phlex -c workflow.jsonnet --stage reconstruction + +A job fails at startup if no stage is given, if it is empty, or if it is +``"CURRENT"``, which is reserved (see below). +The stage requirement was introduced in Phlex v0.4.0; configurations written +for earlier versions need a ``stage`` key added. + +Stages of created products +-------------------------- + +Every product created by a transform, fold or unfold in the job carries the +job's stage. + +Providers registered with ``PHLEX_REGISTER_PROVIDERS`` declare their +product with ``output_product(creator, suffix, layer, stage)``. +The ``stage`` argument defaults to ``"CURRENT"``, which Phlex replaces by the +job's stage name. +A provider that reads products written by an earlier job can instead pass +that job's stage name, so that its products keep their original identity. + +Implicit providers, which a ``phlex::source`` returns from +``create_providers()``, must set ``provider_bundle::stage`` to a real stage +name. +Returning ``"CURRENT"`` is an error, because no substitution takes place for +implicit providers. + +.. note:: + + In Phlex v0.4 a ``phlex::source`` has no access to the job's stage, so a + source that creates new products, such as an event generator, must take + the stage name from its own configuration. + Whether sources should support the ``"CURRENT"`` stage name is under + discussion in + `issue #957 `_. + +Selecting products by stage +--------------------------- + +``product_selector`` has an optional ``stage`` field; in a configuration +file a selector object accepts a ``stage`` key. + +Not specified + Products of any stage match. + +``"CURRENT"`` + Only products of the job's own stage match. + Sources are the exception for now: the selector reaches + ``create_providers()`` with the literal ``"CURRENT"``, which a source that + checks candidates with ``selector.match(spec, layer, stage)`` will not + match (see issue #957). + To select a product that a source supplies, leave ``stage`` unspecified + or name the stage explicitly. + +Any other name + Only products of that stage match. + If it is not the job's own stage, Phlex looks for the product only among + providers, because everything computed in the job carries the job's stage. + +.. code-block:: cpp + + m.transform("refit", refit, concurrency::unlimited) + .input_family(product_selector{ + .creator = "tracks", .layer = "event", .stage = "reconstruction"}); + +Selectors written in Python cannot set ``stage`` yet. + +An algorithm that takes a ``phlex::handle`` can read the stage of the +product it received with ``handle::stage()``. diff --git a/phlex/core/registration_api.hpp b/phlex/core/registration_api.hpp index c89e4865a..cae1d8cf4 100644 --- a/phlex/core/registration_api.hpp +++ b/phlex/core/registration_api.hpp @@ -201,6 +201,14 @@ namespace phlex::detail { { } + /// @brief Declares the data product created by this provider. + /// + /// @param creator Creator name recorded for the product, as matched by + /// @c product_selector::creator. + /// @param suffix Suffix recorded for the product. + /// @param output_layer Data layer whose data cells this provider is called for. + /// @param stage Stage recorded for the product. The default, @c "CURRENT", + /// is replaced by the stage configured for the job. auto output_product(phlex::experimental::algorithm_name creator, phlex::experimental::identifier suffix, phlex::experimental::identifier output_layer, diff --git a/phlex/model/handle.hpp b/phlex/model/handle.hpp index 3c126402c..9a4baa94a 100644 --- a/phlex/model/handle.hpp +++ b/phlex/model/handle.hpp @@ -94,6 +94,9 @@ namespace phlex { } std::string_view suffix() const noexcept { return std::string_view(spec_->suffix()); } std::string_view layer() const noexcept { return std::string_view(index_->layer_name()); } + /// @brief Name of the processing stage that created the product. + /// + /// This is the job's configured stage for products created by transforms, folds, and unfolds. std::string_view stage() const noexcept { return std::string_view(*stage_); } std::string layer_path() const { return index_->layer_path().to_string(); }