Skip to content
Snippets Groups Projects
Commit 0e94ca5f authored by Patrick Koppenburg's avatar Patrick Koppenburg :leaves:
Browse files

Merge branch 'erodrigu-test-for-issue-97' into 'master'

Add test for issue 97

See merge request !851
parents c2d85958 08bc89df
No related branches found
No related tags found
2 merge requests!1103Draft: Add AnalysisHelpers to DaVinci Stack,!851Add test for issue 97
Pipeline #5225791 passed
###############################################################################
# (c) Copyright 2000-2021 CERN for the benefit of the LHCb Collaboration #
# (c) Copyright 2000-2023 CERN for the benefit of the LHCb Collaboration #
# #
# This software is distributed under the terms of the GNU General Public #
# Licence version 3 (GPL Version 3), copied verbatim in the file "COPYING". #
......@@ -13,6 +13,16 @@ DaVinciTests
------------
#]=======================================================================]
gaudi_install(PYTHON)
gaudi_add_tests(QMTest)
gaudi_install(PYTHON)
if(BUILD_TESTING AND USE_DD4HEP)
# Disable some tests that are not for dd4hep
set_property(
TEST
DaVinciTests.mc.test_davinci-issue-97_bkgcat_mc-truth
PROPERTY
DISABLED TRUE
)
endif()
###############################################################################
# (c) Copyright 2023 CERN for the benefit of the LHCb Collaboration #
# #
# This software is distributed under the terms of the GNU General Public #
# Licence version 3 (GPL Version 3), copied verbatim in the file "COPYING". #
# #
# In applying this licence, CERN does not waive the privileges and immunities #
# granted to it by virtue of its status as an Intergovernmental Organization #
# or submit itself to any jurisdiction. #
###############################################################################
"""
rst_title: Test for DaVinci issue 97
rst_description: Test for DaVinci issue 97 on an incorrect behaviour
of the `MCTruthAndBkgCat` helper in Analysis's `Phys/DaVinciMCTools` package,
traced back to a bug in one of the tools internally called.
rst_running: lbexec DaVinciTests.mc.option_davinci-issue-97_bkgcat_mc-truth:main "$DAVINCITESTSROOT/python/DaVinciTests/mc/option_davinci-issue-97_bkgcat_mc-truth.yaml"
rst_yaml: ../DaVinciTests/python/DaVinciTests/mc/option_davinci-issue-97_bkgcat_mc-truth.yaml
"""
import Functors as F
from PyConf.reading import get_particles
from FunTuple import FunTuple_Particles as Funtuple
from FunTuple import FunctorCollection
import FunTuple.functorcollections as FC
from DaVinciMCTools import MCTruthAndBkgCat
from DaVinci import make_config, Options
from DaVinci.algorithms import add_filter
my_line = "Hlt2Lb2JpsiLambda"
def make_branches():
return {
"Lb":
"[Lambda_b0 -> ( J/psi(1S) -> mu+ mu- ) ( Lambda0 -> p+ pi- )]CC",
"Jpsi":
"[Lambda_b0 -> ^( J/psi(1S) -> mu+ mu- ) ( Lambda0 -> p+ pi- )]CC",
"Lambda0":
"[Lambda_b0 -> ( J/psi(1S) -> mu+ mu- ) ^( Lambda0 -> p+ pi- )]CC",
"mu_plus":
"[Lambda_b0 -> ( J/psi(1S) -> ^mu+ mu- ) ( Lambda0 -> p+ pi- )]CC",
"mu_minus":
"[Lambda_b0-> ( J/psi(1S) -> mu+ ^mu- ) ( Lambda0 -> p+ pi- )]CC",
"p_plus":
"[Lambda_b0 -> ( J/psi(1S) ->mu+ mu- ) ( Lambda0 -> ^p+ pi- )]CC",
"pi_minus":
"[Lambda_b0 -> ( J/psi(1S) -> mu+ mu- ) ( Lambda0 -> p+ ^pi- )]CC"
}
def variables_all():
return FunctorCollection({
"MASS": F.MASS,
"PT": F.PT,
"ETA": F.ETA,
"PHI": F.PHI,
})
def make_variables():
return {
"ALL": variables_all() # adds these variables to all branches
}
def make_trueid_bkgcat_info(mctruth):
MCTRUTH = mctruth
trueid_bkgcat_info = {
"BKGCAT": MCTRUTH.BkgCat,
"TRUEPT": MCTRUTH(F.PT),
"TRUEORIGINVZ": MCTRUTH(F.ORIGIN_VZ),
"TRUEORIGINVX": MCTRUTH(F.ORIGIN_VX),
"TRUEORIGINVY": MCTRUTH(F.ORIGIN_VY)
}
return trueid_bkgcat_info
def main(options: Options):
my_data = get_particles(f"/Event/HLT2/{my_line}/Particles")
my_filter = add_filter("LoKi__HDRFilter", f"HLT_PASS('{my_line}Decision')")
mctruth = MCTruthAndBkgCat(input_particles=my_data)
trueid_bkgcat_info = make_trueid_bkgcat_info(mctruth)
mchierarchy_info = FC.MCHierarchy(mctruth_alg=mctruth)
variables = make_variables()
for branch in variables.keys():
variables[branch] += FunctorCollection(trueid_bkgcat_info)
variables[branch] += mchierarchy_info
evt_variables = FC.SelectionInfo(
selection_type="Hlt2", trigger_lines=[my_line])
my_tuple = Funtuple(
tuple_name="Tuple",
name=f"{my_line}Tuple",
fields=make_branches(),
variables=variables,
event_variables=evt_variables,
inputs=my_data)
algs = {my_line: [my_filter, my_tuple]}
return make_config(options, algs)
input_files:
- 'root://eoslhcb.cern.ch//eos/lhcb/wg/dpa/wp3/tests/test_davinci-issue-97_bkgcat_mc-truth.dst'
annsvc_config: 'root://eoslhcb.cern.ch//eos/lhcb/wg/dpa/wp3/tests/test_davinci-issue-97_bkgcat_mc-truth.tck.json'
data_type: Upgrade
input_type: ROOT
simulation: true
input_process: 'Hlt2'
stream: 'default'
conddb_tag: sim-20210617-vc-mu100
dddb_tag: dddb-20210617
evt_max: -1
input_raw_format: 0.3
lumi: false
print_freq: 100
msg_svc_format: '% F%80W%S %7W%R%T %0W%M'
<?xml version="1.0" ?>
<!--
###############################################################################
# (c) Copyright 2023 CERN for the benefit of the LHCb Collaboration #
# #
# This software is distributed under the terms of the GNU General Public #
# Licence version 3 (GPL Version 3), copied verbatim in the file "COPYING". #
# #
# In applying this licence, CERN does not waive the privileges and immunities #
# granted to it by virtue of its status as an Intergovernmental Organization #
# or submit itself to any jurisdiction. #
###############################################################################
-->
<!DOCTYPE extension PUBLIC '-//QM/2.3/Extension//EN' 'http://www.codesourcery.com/qm/dtds/2.3/-//qm/2.3/extension//en.dtd'>
<extension class="GaudiTest.GaudiExeTest" kind="test">
<argument name="program"><text>lbexec</text></argument>
<argument name="args"><set>
<text>DaVinciTests.mc.option_davinci-issue-97_bkgcat_mc-truth:main</text>
</set></argument>
<argument name="options_yaml_fn"><text>$DAVINCITESTSROOT/python/DaVinciTests/mc/option_davinci-issue-97_bkgcat_mc-truth.yaml</text></argument>
<argument name="extra_options_yaml"><text>
ntuple_file: davinci-issue-97_bkgcat_mc-truth_ntuple.root
histo_file: davinci-issue-97_bkgcat_mc-truth_histos.root
evt_max: 10
</text></argument>
<argument name="reference"><text>../refs/test_davinci-issue-97_bkgcat_mc-truth.ref</text></argument>
<argument name="error_reference"><text>../refs/empty.ref</text></argument>
<argument name="validator"><text>
from DaVinciTests.QMTest.DaVinciExclusions import preprocessor, counter_preprocessor
validateWithReference(preproc = preprocessor, counter_preproc = counter_preprocessor)
countErrorLines({"FATAL": 0, "ERROR": 0}, stdout=stdout)
import os
from DaVinciTests.QMTest.check_helpers import get_pandas_dataframe, df_has_nan
filename = "./davinci-issue-97_bkgcat_mc-truth_ntuple.root"
ntuple = "Hlt2Lb2JpsiLambdaTuple/Tuple"
df = get_pandas_dataframe(filename, ntuple)
# Check ntuple structure
if df.empty:
causes.append(f"File {filename}: ntuple does not contain any branches")
if df.shape != (7, 116):
causes.append("Ntuple not with expected number of entries and/or branches")
# Check there are no NaN values in the ntuple except where expected
from DaVinciTests.QMTest.check_helpers import list_fields_with_nan
l_branches_with_nans = ['Lambda0_TRUEORIGINVX',
'Lambda0_TRUEORIGINVY',
'Lambda0_TRUEORIGINVZ',
'Lambda0_TRUEPT',
'Lb_TRUEORIGINVX',
'Lb_TRUEORIGINVY',
'Lb_TRUEORIGINVZ',
'Lb_TRUEPT'
]
l_test = list_fields_with_nan(filename, ntuple)
if sorted(l_test) != sorted(l_branches_with_nans):
causes.append("Unexpected list of branches with NaN values")
# Checks PIDs are correctly assigned
if not ( (df.filter(regex=("mu.*TRUEID")).abs() == 13).all().all() # all true muons
and ( (df["pi_minus_TRUEID"].abs() == 211).all().all() ) # all true pions
and ( (df["Jpsi_TRUEID"].abs() == 443).all().all() ) # all true J/psi
and ( df["Lambda0_TRUEID"].to_list() == [0, 310, 0, 310, 0, 0, 3122] )
and ( (df["Lb_TRUEID"] == 5122).sum() == 1 )
and ( (df["p_plus_TRUEID"].abs() == 2212).sum() == 3 )
):
causes.append("Ntuple contains unexpected TRUEID values")
# Check background categories
if not ( (df.filter(regex=("(p_|pi|mu).*BKGCAT"))==-1).all().all() # all entries are -1 for final-state particles
and (df["Jpsi_BKGCAT"]==0).all().all() # all signal
and ( (df["Lambda0_BKGCAT"]==0).sum() + (df["Lb_BKGCAT"]==0).sum() == 2 ) # single entry with both == 0
and ( df["Lb_BKGCAT"].to_list() == [100, 100, 100, 100, 100, 110, 0] )
and ( df["Lambda0_BKGCAT"].to_list() == [100, 30, 130, 30, 100, 120, 0] )
):
causes.append("Ntuple contains unexpected BKGCAT values")
print('Test successfully completed!')
os.system(f"rm {filename}")
</text></argument>
</extension>
This diff is collapsed.
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment