5. Monte Carlo Truth

This tutorial shows how to add Monte Carlo truth information of your candidates into your tuple. A list of available functors can be found in functorcollections. The tutorial also shows how to use the algorithm mctruth_alg, which also outputs a map between particle and Background Category.

from PyConf.reading import get_particles
import Functors as F
from FunTuple import FunctorCollection
from FunTuple import FunTuple_Particles as Funtuple
from DaVinci import Options, make_config
from DaVinci.algorithms import create_lines_filter


def main(options: Options):
    # Define a dictionary of "field name" -> "decay descriptor component".
    fields = {
        "Bs": "B_s0 -> (J/psi(1S) -> mu+ mu-) (phi(1020) ->K+ K-)",
        "Jpsi": "B_s0 ->^(J/psi(1S) -> mu+ mu-)  (phi(1020) ->K+ K-)",
        "Phi": "B_s0 -> (J/psi(1S) -> mu+ mu-) ^(phi(1020) ->K+ K-)",
        "Mup": "B_s0 ->  (J/psi(1S) ->^mu+ mu-) (phi(1020) ->K+ K-)",
        "Mum": "B_s0 ->  (J/psi(1S) -> mu+ ^mu-) (phi(1020) ->K+ K-)",
        "Kp": "B_s0 ->  (J/psi(1S) -> mu+ mu-) (phi(1020) ->^K+ K-)",
        "Km": "B_s0 ->  (J/psi(1S) -> mu+ mu-) (phi(1020) ->K+ ^K-)",
    }

    # Import functor collections Kinematics, MCKinematics, MCHierarchy
    # (Note: import statements should really be at the top of files for almost all use-cases.
    # The local import is here to be close to the comments.)
    # There is also "MCVertexInfo" but we won't import it here.
    #
    # See what's available at: https://gitlab.cern.ch/lhcb/Analysis/-/blob/master/Phys/FunTuple/python/FunTuple/functorcollections.py
    import FunTuple.functorcollections as FC

    # We can seek help on these functorcollections using following commands (if you run these commands, press "q" to exit and continue).
    # - print(help(FC.MCKinematics))
    # - print(help(FC.MCHierarchy))
    #
    # We see that it takes an input an algorithm MCTruthAndBkgCat(inputdata), so lets import that.
    from DaVinciMCTools import MCTruthAndBkgCat

    # Load data from dst onto a TES
    turbo_line = "Hlt2B2CC_BsToJpsiPhi_Detached"
    input_data = get_particles(f"/Event/HLT2/{turbo_line}/Particles")
    # Define an algorithm that builds a map i.e. one-to-one relation b/w Reco Particle -> Truth MC Particle.
    MCTRUTH = MCTruthAndBkgCat(input_data, name="MCTruthAndBkgCat_tuto")
    # print(mctruth_alg.MCAssocTable)

    # Pass it to collections
    kin = FC.Kinematics()
    mckin = FC.MCKinematics(mctruth_alg=MCTRUTH)
    mchierarchy = FC.MCHierarchy(mctruth_alg=MCTRUTH)
    # print(mckin)
    # print(mchierarchy)

    # Loop over and keep only what's required
    kin = FunctorCollection(
        {k: v for k, v in kin.get_thor_functors().items() if k == "P" or k == "M"}
    )
    mckin = FunctorCollection(
        {k: v for k, v in mckin.get_thor_functors().items() if k == "TRUEP"}
    )
    mchierarchy = FunctorCollection(
        {k: v for k, v in mchierarchy.get_thor_functors().items() if k == "TRUEID"}
    )
    # print(kin)
    # print(mckin)
    # print(mchierarchy)

    # To get truth information with a functor
    extra_info = {
        # Important note: specify an invalid value for integer functors if there exists no truth info.
        #                 The invalid value for floating point functors is set to nan.
        "TRUEEID": F.VALUE_OR(0) @ MCTRUTH(F.PARTICLE_ID),
        "TRUEEPHI": MCTRUTH(F.PHI),
    }
    extra_info = FunctorCollection(extra_info)

    # The algorithm mctruth_alg also outputs a map b/w particle and bkg category which can be obtained using the functor
    # For more info on background category see: https://twiki.cern.ch/twiki/bin/view/LHCb/TupleToolMCBackgroundInfo
    bkg_cat = FunctorCollection({"BKGCAT": MCTRUTH.BkgCat})

    # Define variables dictionary "field name" -> Collections of functor
    variables = {"ALL": kin + mckin + mchierarchy + bkg_cat, "Kp": extra_info}

    # Add a filter
    my_filter = create_lines_filter("HDRFilter_SeeNoEvil", lines=[f"{turbo_line}"])

    # Define instance of FunTuple
    mytuple = Funtuple(
        "TDirectoryName",
        "TTreeName",
        fields=fields,
        variables=variables,
        inputs=input_data,
    )

    config = make_config(options, [my_filter, mytuple])
    return config

To run the example:

lbexec DaVinciTutorials.tutorial5_MCTruth:main $DAVINCITUTORIALSROOT/options.yaml

For reference, these are the options of this example

input_files:
   - 'root://eoslhcb.cern.ch//eos/lhcb/wg/dpa/wp3/tests/hlt2_passthrough_thor_lines.dst'
input_manifest_file: 'root://eoslhcb.cern.ch//eos/lhcb/wg/dpa/wp3/tests/hlt2_passthrough_thor_lines.tck.json'
input_type: ROOT
evt_max: 100
ntuple_file: davinci_ntuple.root
input_process: TurboPass
print_freq: 1
simulation: True
lumi: False
conddb_tag: sim-20180530-vc-md100
dddb_tag: dddb-20180815
conditions_version: master
geometry_version: run3/trunk
persistreco_version: 0.0