Writing a Fragment

Last update: 17 Nov 2021 [History] [Edit]

Beamspot Reweighting

Have a look at BeamSpotReweightingAlg and try to write the configuration fragment for it. This time make sure you configure all the dependencies and properties. There are two event-level inputs, one conditions input, one decoration output and one other property.

Some notes:

  • As this algorithm depends on conditions, the relevant conditions algorithm needs to be set up (BeamSpotCondAlgCfg).
  • EventInfo and InputMcEventCollection usually come from the input file so explicit algorithmic dependencies do not need to be set up. It is good to set input keys for all inputs, so data dependencies are also clear from the configuration side, but they are often ommited for common inputs like EventInfo.
  • Input_beam_sigma_z can be set from flags.Digitization.InputBeamSigmaZ.
  • This is an event algorithm so addEventAlgo should be used.
  • Namespaces can get automatically resolved in component factory so CompFactory.Simulation.BeamSpotReweightingAlg is the full access path to this algorithm.

Compare your result with the current implementation in athena. What do you observe?

def BeamSpotReweightingAlgCfg(flags, name="BeamSpotReweightingAlg", **kwargs):
    # conditions dependency
    from BeamSpotConditions.BeamSpotConditionsConfig import BeamSpotCondAlgCfg
    acc = BeamSpotCondAlgCfg(flags)

    # properties from flags
    kwargs.setdefault("Input_beam_sigma_z", flags.Digitization.InputBeamSigmaZ)

    # data handles
    kwargs.setdefault("InputMcEventCollection", "TruthEvent")
    kwargs.setdefault("EventInfo", "EventInfo")
    kwargs.setdefault("BeamSpotKey", "BeamSpotData")
    kwargs.setdefault("beamSpotWeight", "EventInfo.beamSpotWeight")

    acc.addEventAlgo(CompFactory.Simulation.BeamSpotReweightingAlg(name, **kwargs))
    return acc

Add the algorithm to the job. You will see that by default the algorithm does nothing as ConfigFlags.Digitization.InputBeamSigmaZ is set to -1. You should and play with the properties a bit and look what happens:

  • set the debug output level to the algorithm using cfg.getEventAlgo("BeamSpotReweightingAlg").OutputLevel = DEBUG (you may need to import DEBUG from AthenaCommon.Constants)
  • change the value of ConfigFlags.Digitization.InputBeamSigmaZ to 50
  • change the decoration name to beamSpotWeightForTutorial
  • remove BeamSpotCondAlgCfg from the main job and see what happens

Summary

We learned how to configure an algorithm and change its properties. next section you will learn a bit more about inspecting the changes in the configuration. The final configuration should look something like this:

#!/usr/bin/env python

from AthenaConfiguration.ComponentFactory import CompFactory


def BeamSpotReweightingAlgCfg(flags, name="BeamSpotReweightingAlg", **kwargs):
    # conditions dependency
    from BeamSpotConditions.BeamSpotConditionsConfig import BeamSpotCondAlgCfg
    acc = BeamSpotCondAlgCfg(flags)

    # properties from flags
    kwargs.setdefault("Input_beam_sigma_z", flags.Digitization.InputBeamSigmaZ)

    # data handles
    kwargs.setdefault("InputMcEventCollection", "TruthEvent")
    kwargs.setdefault("EventInfo", "EventInfo")
    kwargs.setdefault("BeamSpotKey", "BeamSpotData")
    kwargs.setdefault("beamSpotWeight", "EventInfo.beamSpotWeightForTutorial")

    acc.addEventAlgo(CompFactory.Simulation.BeamSpotReweightingAlg(name, **kwargs))
    return acc


if __name__ == "__main__":
    from AthenaCommon.Configurable import Configurable
    Configurable.configurableRun3Behavior = 1

    from AthenaConfiguration.AllConfigFlags import ConfigFlags
    from AthenaConfiguration.TestDefaults import defaultTestFiles
    ConfigFlags.Input.Files = defaultTestFiles.HITS
    ConfigFlags.Output.RDOFileName = "myRDO.pool.root"
    ConfigFlags.Digitization.InputBeamSigmaZ = 50
    ConfigFlags.lock()

    from AthenaConfiguration.MainServicesConfig import MainServicesCfg
    cfg = MainServicesCfg(ConfigFlags)

    from AthenaPoolCnvSvc.PoolReadConfig import PoolReadCfg
    cfg.merge(PoolReadCfg(ConfigFlags))

    # Check if running on legacy HITS
    if "EventInfo" not in ConfigFlags.Input.Collections:
        from xAODEventInfoCnv.xAODEventInfoCnvConfig import EventInfoCnvAlgCfg
        cfg.merge(EventInfoCnvAlgCfg(ConfigFlags))

    cfg.merge(BeamSpotReweightingAlgCfg(ConfigFlags))

    if ConfigFlags.Output.doWriteRDO:
        from OutputStreamAthenaPool.OutputStreamConfig import OutputStreamCfg
        cfg.merge(OutputStreamCfg(ConfigFlags, "RDO", ItemList=[
            "xAOD::EventInfo#EventInfo",
            "xAOD::EventAuxInfo#EventInfoAux.",
        ]))

    from AthenaCommon.Constants import DEBUG
    cfg.getEventAlgo("BeamSpotReweightingAlg").OutputLevel = DEBUG

    with open("BeamSpot.pkl", "wb") as f:
        cfg.store(f)

    import sys
    sys.exit(not cfg.run(maxEvents=3).isSuccess())