From ce6aed65f4974fa012de777d7502219d6e28a578 Mon Sep 17 00:00:00 2001
From: Abhijit Mathad <abhijit.mathad@cern.ch>
Date: Tue, 23 May 2023 12:10:35 +0200
Subject: [PATCH] Follow
 https://gitlab.cern.ch/lhcb/Analysis/-/merge_requests/980

---
 .../DaVinciTests/multiplecand_branches.py     | 49 +++++++++++++++
 .../test_multiplecand_branches.qmt            | 61 +++++++++++++++++++
 2 files changed, 110 insertions(+)
 create mode 100644 DaVinciTests/python/DaVinciTests/multiplecand_branches.py
 create mode 100755 DaVinciTests/tests/qmtest/davinci.qms/test_multiplecand_branches.qmt

diff --git a/DaVinciTests/python/DaVinciTests/multiplecand_branches.py b/DaVinciTests/python/DaVinciTests/multiplecand_branches.py
new file mode 100644
index 000000000..a9fc5ea2b
--- /dev/null
+++ b/DaVinciTests/python/DaVinciTests/multiplecand_branches.py
@@ -0,0 +1,49 @@
+###############################################################################
+# (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.                                       #
+###############################################################################
+"""
+Test for branches related to multiple candidates in an event.
+ - test that the branches related to totCandidates and nCandidates are filled correctly
+"""
+import Functors as F
+from Hlt2Conf.standard_particles import make_detached_mumu
+from RecoConf.reconstruction_objects import upfront_reconstruction
+from FunTuple import FunctorCollection
+from FunTuple import FunTuple_Particles as Funtuple
+from DaVinci import Options, make_config
+
+
+def main(options: Options):
+    # Prepare the node with the selection
+    dimuons = make_detached_mumu()
+
+    #FunTuple: Jpsi info
+    fields = {}
+    fields['Jpsi'] = 'J/psi(1S) -> mu+ mu-'
+
+    #make collection of functors for Jpsi
+    variables_jpsi = FunctorCollection({
+        'THOR_MASS': F.MASS,
+    })
+
+    #associate FunctorCollection to field (branch) name
+    variables = {}
+    variables['Jpsi'] = variables_jpsi
+
+    #Configure Funtuple algorithm
+    tuple_dimuons = Funtuple(
+        name="DimuonsTuple",
+        tuple_name="DecayTree",
+        fields=fields,
+        variables=variables,
+        inputs=dimuons,
+        store_multiple_cand_info=True)
+
+    return make_config(options, upfront_reconstruction() + [tuple_dimuons])
diff --git a/DaVinciTests/tests/qmtest/davinci.qms/test_multiplecand_branches.qmt b/DaVinciTests/tests/qmtest/davinci.qms/test_multiplecand_branches.qmt
new file mode 100755
index 000000000..657f92c60
--- /dev/null
+++ b/DaVinciTests/tests/qmtest/davinci.qms/test_multiplecand_branches.qmt
@@ -0,0 +1,61 @@
+<?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.                                       #
+###############################################################################
+-->
+
+<!--
+This is a test that checks that the multiplecand_branches example runs
+-->
+
+<!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.multiplecand_branches:main</text>
+  </set></argument>
+  <argument name="options_yaml_fn"><text>$DAVINCIEXAMPLESROOT/example_data/Upgrade_Bd2KstarMuMu_ldst.yaml</text></argument>
+  <argument name="extra_options_yaml"><text>
+    evt_max: 10
+    histo_file: DV-example-tupling-DTF-his.root
+    input_raw_format: 4.3
+    ntuple_file: DV-example-tupling-DTF-ntp.root
+    input_process: Brunel
+  </text></argument>
+<argument name="validator"><text>
+import sys, os, glob
+from ROOT import TFile
+from PyConf.components import findRootObjByDir
+
+B_vars_stored=['totCandidates', 'nCandidate', 'EVENTNUMBER', 'RUNNUMBER', 'Jpsi_THOR_MASS']
+#sort the expected vars
+B_vars_stored = sorted(B_vars_stored)
+
+#open the TFile and TTree
+ntuple = './DV-example-tupling-DTF-ntp.root'
+if not os.path.isfile(ntuple): raise Exception(f"File: {ntuple} does not exist!")
+f      = TFile.Open(ntuple)
+t_B    = findRootObjByDir(f, 'DimuonsTuple', 'DecayTree')
+
+#sort the stores vars
+b_names = sorted([b.GetName() for b in t_B.GetListOfLeaves()])
+
+B_excluded_1 = set(B_vars_stored) - set(b_names)
+B_excluded_2 = set(b_names) - set(B_vars_stored)
+if len(B_excluded_1) != 0: raise Exception('Number of stored variables is less than what is expected. The extra variables expected are: ' , B_excluded_1)
+if len(B_excluded_2) != 0: raise Exception('Number of stored variables is greater than what is expected. The extra variables stored are: ', B_excluded_2)
+
+f.Close()
+print('Test successfully completed!')
+os.system(f"rm {ntuple}")
+
+</text></argument>
+</extension>
-- 
GitLab