Writing a Fragment

Last update: 05 Jul 2023 [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 flags.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 flags.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 AthenaConfiguration.AllConfigFlags import initConfigFlags
    from AthenaConfiguration.TestDefaults import defaultTestFiles
    flags = initConfigFlags()
    flags.Input.Files = defaultTestFiles.HITS
    flags.Output.RDOFileName = "myRDO.pool.root"
    flags.Digitization.InputBeamSigmaZ = 50
    flags.lock()

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

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

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

    cfg.merge(BeamSpotReweightingAlgCfg(flags))

    if flags.Output.doWriteRDO:
        from OutputStreamAthenaPool.OutputStreamConfig import OutputStreamCfg
        cfg.merge(OutputStreamCfg(flags, "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())