diff --git a/Phys/FunctorCore/python/Functors/__init__.py b/Phys/FunctorCore/python/Functors/__init__.py index 9a6f0ea1bac8c1718b91bdbbd93255c05ee670e9..83eae7fd3a8ab43775e9dadb91756798fa5fc8b8 100644 --- a/Phys/FunctorCore/python/Functors/__init__.py +++ b/Phys/FunctorCore/python/Functors/__init__.py @@ -2476,6 +2476,40 @@ def BPVDLS(Vertices: DataHandle = None): return VTX_DLS.bind(BPV(Vertices), FORWARDARGS) +# See test_constants.py:test_DataTypesToType for the origin of these constants +DataTypesToType = { + "nPVs": 0, + "nLongTracks": 10, + "nDownstreamTracks": 11, + "nUpstreamTracks": 12, + "nVeloTracks": 13, + "nTTracks": 14, + "nBackTracks": 15, + "nTracks": 16, + "nGhosts": 17, + "nRich1Hits": 20, + "nRich2Hits": 21, + "nVeloClusters": 30, + "nVPClusters": 31, + "nITClusters": 40, + "nTTClusters": 50, + "nUTClusters": 51, + "nOTClusters": 60, + "nFTClusters": 41, + "nSPDhits": 70, + "eCalTot": 71, + "hCalTot": 72, + "nEcalClusters": 73, + "nMuonCoordsS0": 80, + "nMuonCoordsS1": 91, + "nMuonCoordsS2": 92, + "nMuonCoordsS3": 93, + "nMuonCoordsS4": 94, + "nMuonTracks": 95, + "TypeUnknown": 1000, +} + + def RECSUMMARY_INFO(rec_summary: DataHandle, DataType: Union[str, int]): """Extracts DataType information from LHCb::RecSummary.DataType @@ -2483,24 +2517,14 @@ def RECSUMMARY_INFO(rec_summary: DataHandle, DataType: Union[str, int]): rec_summary: DataHandle of the LHCb::RecSummary DataType: Name or enum of datatype as defined in https://gitlab.cern.ch/lhcb/LHCb/-/blob/master/Event/RecEvent/include/Event/RecSummary.h """ - # Workaround for cppyy warning with ROOT-10769 - # (see https://gitlab.cern.ch:8443/lhcb/LHCb/-/merge_requests/2637) - import warnings - - with warnings.catch_warnings(): - warnings.simplefilter("ignore") - from cppyy.gbl import LHCb - if isinstance(DataType, int): # or maybe check directly for enum type? - if DataType == LHCb.RecSummary.TypeUnknown: + if DataType == DataTypesToType["TypeUnknown"]: raise ValueError("RecSummary type unknown") return RECSUMMARY_INFO._F(DataType) @ TES(rec_summary) elif isinstance(DataType, str): - if LHCb.RecSummary.DataTypesToType(DataType) == LHCb.RecSummary.TypeUnknown: + if DataTypesToType[DataType] == DataTypesToType["TypeUnknown"]: raise ValueError("RecSummary type unknown") - return RECSUMMARY_INFO._F(LHCb.RecSummary.DataTypesToType(DataType)) @ TES( - rec_summary - ) + return RECSUMMARY_INFO._F(DataTypesToType[DataType]) @ TES(rec_summary) else: raise TypeError("RECSUMMARY_INFO expects string or enum value") diff --git a/Phys/FunctorCore/python/Functors/tests/test_constants.py b/Phys/FunctorCore/python/Functors/tests/test_constants.py new file mode 100644 index 0000000000000000000000000000000000000000..cb7aa842730d14ec5f7b532279f272173b1946c6 --- /dev/null +++ b/Phys/FunctorCore/python/Functors/tests/test_constants.py @@ -0,0 +1,39 @@ +############################################################################### +# (c) Copyright 2025 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. # +############################################################################### +"""Ensure that constants in the Functors module are consistent with the C++. + +In principle these constants could be loaded at runtime using cppyy (as is done +for the tests in this module) however this is extremely slow to load and has +significant memory overhead. Instead we just hard-code the values and use these +tests to ensure they stay in sync with the C++ code. +""" + +import warnings + + +def test_DataTypesToType(): + """Ensure that the DataTypesToType mapping is consistent with the C++ code.""" + from Functors import DataTypesToType + + # Workaround for cppyy warning with ROOT-10769 + # (see https://gitlab.cern.ch:8443/lhcb/LHCb/-/merge_requests/2637) + with warnings.catch_warnings(): + warnings.simplefilter("ignore") + from cppyy.gbl import LHCb + + expected = { + k: int(v) + for k, v in LHCb.RecSummary.DataTypes.__dict__.items() + if not k.startswith("_") + } + assert DataTypesToType == expected, ( + "DataTypesToType mapping is not consistent with the C++ code, please update it!" + )