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

Merge branch 'jzhuo_AddMCTrack' into 'master'

Thor functors for MCTrackInfo

See merge request !716
parents e58766a0 e4106d8c
No related branches found
No related tags found
2 merge requests!1103Draft: Add AnalysisHelpers to DaVinci Stack,!716Thor functors for MCTrackInfo
Pipeline #4479849 passed
......@@ -158,13 +158,13 @@ def all_variables(pvs, DTFR, mctruth, ptype):
if basic:
all_vars['TRACKPT'] = F.PT @ F.TRACK
all_vars['TRACKHISTORY'] = F.TRACKHISTORY @ F.TRACK
all_vars['TRACKHISTORY'] = F.VALUE_OR(-1) @ F.TRACKHISTORY @ F.TRACK
all_vars['QOVERP'] = F.QOVERP @ F.TRACK
all_vars['NDOF'] = F.NDOF @ F.TRACK
all_vars['NFTHITS'] = F.NFTHITS @ F.TRACK
all_vars['NHITS'] = F.NHITS @ F.TRACK
all_vars['NUTHITS'] = F.NUTHITS @ F.TRACK
all_vars['NVPHITS'] = F.NVPHITS @ F.TRACK
all_vars['NDOF'] = F.VALUE_OR(-1) @ F.NDOF @ F.TRACK
all_vars['NFTHITS'] = F.VALUE_OR(-1) @ F.NFTHITS @ F.TRACK
all_vars['NHITS'] = F.VALUE_OR(-1) @ F.NHITS @ F.TRACK
all_vars['NUTHITS'] = F.VALUE_OR(-1) @ F.NUTHITS @ F.TRACK
all_vars['NVPHITS'] = F.VALUE_OR(-1) @ F.NVPHITS @ F.TRACK
all_vars['OBJECT_KEY'] = F.OBJECT_KEY
......
###############################################################################
# (c) Copyright 2022 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. #
###############################################################################
"""
Example options to show the usage of the new DaVinciMCTools: MCReconstructible
and MCReconstructed.
"""
from FunTuple import FunTuple_MCParticles as MCFuntuple
from PyConf.components import force_location
from DaVinciMCTools import MCReconstructible, MCReconstructed
from DaVinci import Options, make_config
from RecoConf.data_from_file import make_mc_track_info
from FunTuple.functorcollections import MCReconstructed_Collection, MCReconstructible_Collection
def main(options: Options):
# Input
MC_data = force_location("/Event/HLT2/MC/Particles")
#Get variables related to reconstructible information.
mcrtible = MCReconstructible(
process='Hlt2', mc_track_info=make_mc_track_info())
#The option extra_info is set to False by default and can be set to True to get more information
vars_rtible = MCReconstructible_Collection(mcrtible, extra_info=True)
print('Reconstructible functors:', vars_rtible.functor_dict.keys())
#Note instead of importing functorcollections (MCReconstructed_Collection), one
# can directly add track related information using the helper class (MCReconstructible)
vars_rtible["EXTRA_MC_HASUT"] = mcrtible.HasUT
vars_rtible["EXTRA_MC_HASVELO"] = mcrtible.HasVelo
print('Reconstructible functors:', vars_rtible.functor_dict.keys())
#Get variables related to reconstructed information.
# - If "use_best_mcmatch = True" (default), the best associated reconstructed
# track to the mc particle is used (tupling scalars).
# - If "use_best_mcmatch = False", all associated reconstructed
# tracks to the mc particle are used (tupling arrays).
# Here we set it to false for testing purposes.
mcrted_all = MCReconstructed(
MC_data, use_best_mcmatch=False, process='Hlt2')
#The option extra_info below is set to False by default in the functor collection.
vars_rted = MCReconstructed_Collection(mcrted_all, extra_info=False)
#Note:
# - Instead of importing functorcollections (MCReconstructed_Collection), one
# can directly add track related information using the helper class (MCReconstructed).
# - A new functor can be added for e.g. vars_rtible["MC_HASUT"] = mcrtible.get_info(F.NEW_TRACK_FUNCTOR)
vars_rted['TRACK_TYPE[TRACK_INDX]'] = mcrted_all.TrackType
vars_rted['TRACK_HASUT[TRACK_INDX]'] = mcrted_all.HasUT
print('Reconstructed functors:', vars_rted.functor_dict.keys())
# Variables
variables = {
'ALL': vars_rted,
# Apply reconstructible to charged particles
'Kplus': vars_rtible,
'pip': vars_rtible,
'pim1': vars_rtible,
'pim2': vars_rtible,
}
# Define fields
fields = {
'B0': '[[B0]CC -> (D- -> K+ pi- pi-) pi+]CC',
'Dm': '[[B0]CC -> ^(D- -> K+ pi- pi-) pi+]CC',
'Kplus': '[[B0]CC -> (D- -> ^K+ pi- pi-) pi+]CC',
'pip': '[[B0]CC -> (D- -> K+ pi- pi-) ^pi+]CC',
'pim1': '[[B0]CC -> (D- -> K+ ^pi- pi-) pi+]CC',
'pim2': '[[B0]CC -> (D- -> K+ pi- ^pi-) pi+]CC'
}
# Make tuple algorithm
tuple_Dpi = MCFuntuple(
name="DpiMC",
tuple_name="DecayTree",
fields=fields,
variables=variables,
inputs=MC_data)
# Run
return make_config(options, [tuple_Dpi])
......@@ -14,10 +14,12 @@ Read an HLT2 file and create an ntuple using pre-defined Functor collections.
import Functors as F
from PyConf.components import force_location
from DaVinciMCTools import MCReconstructible, MCReconstructed
from FunTuple import FunctorCollection, functorcollections
from FunTuple import FunTuple_Particles as Funtuple
from FunTuple import FunTuple_Particles as Funtuple, FunTuple_MCParticles as MCFuntuple
from DaVinci.algorithms import add_filter
from DaVinci import Options, make_config
from RecoConf.data_from_file import make_mc_track_info
from DaVinci.truth_matching import configured_MCTruthAndBkgCatAlg
from DaVinci.algorithms import get_odin, get_decreports
from PyConf.Algorithms import WeightedRelTableAlg
......@@ -27,6 +29,15 @@ from Gaudi.Configuration import INFO
def main(options: Options):
line_name = 'Hlt2CharmD0ToKmPipLine'
d02kpi_data = force_location(f"/Event/HLT2/{line_name}/Particles")
MC_data = force_location("/Event/HLT2/MC/Particles")
#Get variables related to reconstructible information.
mcrtible = MCReconstructible(
process='Hlt2', mc_track_info=make_mc_track_info())
#Get variables related to reconstructed information.
mcrted_all = MCReconstructed(
MC_data, use_best_mcmatch=True, process='Hlt2')
#get configured "MCTruthAndBkgCatAlg" algorithm for HLT2 output
mctruth = configured_MCTruthAndBkgCatAlg(
......@@ -51,7 +62,14 @@ def main(options: Options):
functorcollections.MCHierarchy(mctruth),
functorcollections.MCKinematics(mctruth),
functorcollections.MCVertexInfo(mctruth),
functorcollections.TrackIsolation(iso_rel_table)
functorcollections.TrackIsolation(iso_rel_table),
]
MC_collections = [
functorcollections.MCReconstructible_Collection(
mcrtible, extra_info=True),
functorcollections.MCReconstructed_Collection(
mcrted_all, extra_info=False),
]
evt_collections = [
......@@ -59,9 +77,10 @@ def main(options: Options):
functorcollections.SelectionInfo("Hlt2", dec, [line_name])
]
assert len(collections) + len(evt_collections) == len(
functorcollections.
__all__), "Oh no! Did you forget to add a new collection to this test?"
assert len(collections) + len(evt_collections) + len(
MC_collections) == len(
functorcollections.__all__
), "Oh no! Did you forget to add a new collection to this test?"
field_vars = FunctorCollection()
for coll in collections:
......@@ -71,6 +90,10 @@ def main(options: Options):
for coll in evt_collections:
evt_vars += coll
MC_field_vars = FunctorCollection()
for coll in MC_collections:
MC_field_vars += coll
fields = {
"D0": "[D0 -> K- pi+]CC",
"Kminus": "[D0 -> ^K- pi+]CC",
......@@ -87,6 +110,12 @@ def main(options: Options):
"piplus": field_vars,
}
MC_variables = {
"D0": MC_field_vars,
"Kminus": MC_field_vars,
"piplus": MC_field_vars,
}
my_tuple = Funtuple(
name="Tuple",
tuple_name="DecayTree",
......@@ -98,4 +127,11 @@ def main(options: Options):
my_filter = add_filter(options, "HDRFilter_D0Kpi",
f"HLT_PASS('{line_name}')")
return make_config(options, [my_filter, my_tuple])
mc_tuple = MCFuntuple(
name="MCTuple",
tuple_name="DecayTree",
fields=fields,
variables=MC_variables,
inputs=MC_data)
return make_config(options, [mc_tuple, my_filter, my_tuple])
......@@ -23,4 +23,9 @@
ntuple_file: tuple_D0_Kpi_10evts_collections.root
print_freq: 1
</text></argument>
<argument name="validator"><text>
from DaVinciTests.QMTest.DaVinciExclusions import remove_known_warnings
countErrorLines({"FATAL": 0, "WARNING": 0, "ERROR": 0},
stdout=remove_known_warnings(stdout))
</text></argument>
</extension>
<?xml version="1.0" ?>
<!--
###############################################################################
# (c) Copyright 2022 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>DaVinciExamples.tupling.example-tupling-mc-reconstructible-reconstructed:main</text>
</set></argument>
<argument name="options_yaml_fn"><text>$DAVINCIEXAMPLESROOT/example_data/test_hlt1_trigger_decisions.yaml</text></argument>
<argument name="extra_options_yaml"><text>
print_freq: 1
</text></argument>
<argument name="validator"><text>
from DaVinciTests.QMTest.DaVinciExclusions import remove_known_warnings
countErrorLines({"FATAL": 0, "WARNING": 0, "ERROR": 0},
stdout=remove_known_warnings(stdout))
import sys, os
from ROOT import TFile
B_vars_stored = ['B0_N_RECO_TRACKS', 'B0_RECONSTRUCTED', 'B0_TRACK_HASUT', 'B0_TRACK_INDX', 'B0_TRACK_MCASSOCWEIGHTS', 'B0_TRACK_TYPE', 'Dm_N_RECO_TRACKS', 'Dm_RECONSTRUCTED', 'Dm_TRACK_HASUT', 'Dm_TRACK_INDX', 'Dm_TRACK_MCASSOCWEIGHTS', 'Dm_TRACK_TYPE', 'Kplus_EXTRA_MC_HASUT', 'Kplus_EXTRA_MC_HASVELO', 'Kplus_MC_ACCT', 'Kplus_MC_ACCT1', 'Kplus_MC_ACCT1S', 'Kplus_MC_ACCT1X', 'Kplus_MC_ACCT2', 'Kplus_MC_ACCT2S', 'Kplus_MC_ACCT2X', 'Kplus_MC_ACCT3', 'Kplus_MC_ACCT3S', 'Kplus_MC_ACCT3X', 'Kplus_MC_ACCUT', 'Kplus_MC_ACCUT1', 'Kplus_MC_ACCUT2', 'Kplus_MC_ACCVELO', 'Kplus_MC_ACCVELO_AND_T', 'Kplus_MC_HAST', 'Kplus_MC_HAST1', 'Kplus_MC_HAST1S', 'Kplus_MC_HAST1X', 'Kplus_MC_HAST2', 'Kplus_MC_HAST2S', 'Kplus_MC_HAST2X', 'Kplus_MC_HAST3', 'Kplus_MC_HAST3S', 'Kplus_MC_HAST3X', 'Kplus_MC_HASUT', 'Kplus_MC_HASUT1', 'Kplus_MC_HASUT2', 'Kplus_MC_HASVELO', 'Kplus_MC_HASVELO_AND_T', 'Kplus_MC_RECONSTRUCTIBLE', 'Kplus_N_RECO_TRACKS', 'Kplus_RECONSTRUCTED', 'Kplus_TRACK_HASUT', 'Kplus_TRACK_INDX', 'Kplus_TRACK_MCASSOCWEIGHTS', 'Kplus_TRACK_TYPE', 'pim1_EXTRA_MC_HASUT', 'pim1_EXTRA_MC_HASVELO', 'pim1_MC_ACCT', 'pim1_MC_ACCT1', 'pim1_MC_ACCT1S', 'pim1_MC_ACCT1X', 'pim1_MC_ACCT2', 'pim1_MC_ACCT2S', 'pim1_MC_ACCT2X', 'pim1_MC_ACCT3', 'pim1_MC_ACCT3S', 'pim1_MC_ACCT3X', 'pim1_MC_ACCUT', 'pim1_MC_ACCUT1', 'pim1_MC_ACCUT2', 'pim1_MC_ACCVELO', 'pim1_MC_ACCVELO_AND_T', 'pim1_MC_HAST', 'pim1_MC_HAST1', 'pim1_MC_HAST1S', 'pim1_MC_HAST1X', 'pim1_MC_HAST2', 'pim1_MC_HAST2S', 'pim1_MC_HAST2X', 'pim1_MC_HAST3', 'pim1_MC_HAST3S', 'pim1_MC_HAST3X', 'pim1_MC_HASUT', 'pim1_MC_HASUT1', 'pim1_MC_HASUT2', 'pim1_MC_HASVELO', 'pim1_MC_HASVELO_AND_T', 'pim1_MC_RECONSTRUCTIBLE', 'pim1_N_RECO_TRACKS', 'pim1_RECONSTRUCTED', 'pim1_TRACK_HASUT', 'pim1_TRACK_INDX', 'pim1_TRACK_MCASSOCWEIGHTS', 'pim1_TRACK_TYPE', 'pim2_EXTRA_MC_HASUT', 'pim2_EXTRA_MC_HASVELO', 'pim2_MC_ACCT', 'pim2_MC_ACCT1', 'pim2_MC_ACCT1S', 'pim2_MC_ACCT1X', 'pim2_MC_ACCT2', 'pim2_MC_ACCT2S', 'pim2_MC_ACCT2X', 'pim2_MC_ACCT3', 'pim2_MC_ACCT3S', 'pim2_MC_ACCT3X', 'pim2_MC_ACCUT', 'pim2_MC_ACCUT1', 'pim2_MC_ACCUT2', 'pim2_MC_ACCVELO', 'pim2_MC_ACCVELO_AND_T', 'pim2_MC_HAST', 'pim2_MC_HAST1', 'pim2_MC_HAST1S', 'pim2_MC_HAST1X', 'pim2_MC_HAST2', 'pim2_MC_HAST2S', 'pim2_MC_HAST2X', 'pim2_MC_HAST3', 'pim2_MC_HAST3S', 'pim2_MC_HAST3X', 'pim2_MC_HASUT', 'pim2_MC_HASUT1', 'pim2_MC_HASUT2', 'pim2_MC_HASVELO', 'pim2_MC_HASVELO_AND_T', 'pim2_MC_RECONSTRUCTIBLE', 'pim2_N_RECO_TRACKS', 'pim2_RECONSTRUCTED', 'pim2_TRACK_HASUT', 'pim2_TRACK_INDX', 'pim2_TRACK_MCASSOCWEIGHTS', 'pim2_TRACK_TYPE', 'pip_EXTRA_MC_HASUT', 'pip_EXTRA_MC_HASVELO', 'pip_MC_ACCT', 'pip_MC_ACCT1', 'pip_MC_ACCT1S', 'pip_MC_ACCT1X', 'pip_MC_ACCT2', 'pip_MC_ACCT2S', 'pip_MC_ACCT2X', 'pip_MC_ACCT3', 'pip_MC_ACCT3S', 'pip_MC_ACCT3X', 'pip_MC_ACCUT', 'pip_MC_ACCUT1', 'pip_MC_ACCUT2', 'pip_MC_ACCVELO', 'pip_MC_ACCVELO_AND_T', 'pip_MC_HAST', 'pip_MC_HAST1', 'pip_MC_HAST1S', 'pip_MC_HAST1X', 'pip_MC_HAST2', 'pip_MC_HAST2S', 'pip_MC_HAST2X', 'pip_MC_HAST3', 'pip_MC_HAST3S', 'pip_MC_HAST3X', 'pip_MC_HASUT', 'pip_MC_HASUT1', 'pip_MC_HASUT2', 'pip_MC_HASVELO', 'pip_MC_HASVELO_AND_T', 'pip_MC_RECONSTRUCTIBLE', 'pip_N_RECO_TRACKS', 'pip_RECONSTRUCTED', 'pip_TRACK_HASUT', 'pip_TRACK_INDX', 'pip_TRACK_MCASSOCWEIGHTS', 'pip_TRACK_TYPE']
#sort the expected vars
B_vars_stored = sorted(B_vars_stored)
#open the TFile and TTree
ntuple = './hlt2_B0_test_ntuple.root'
if not os.path.isfile(ntuple): raise Exception(f"File: {ntuple} does not exist!")
f = TFile.Open(ntuple)
t_B = f.Get('DpiMC/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>
......@@ -23,6 +23,7 @@ def configured_MCTruthAndBkgCatAlg(
process='Spruce',
root_in_tes=None,
redo_neutral_assoc=False,
filter_MCP=True,
output_level=INFO):
"""
Function to help configure the tools instantiated by the `MCTruthAndBkgCatAlg` algorithm.
......@@ -95,6 +96,7 @@ def configured_MCTruthAndBkgCatAlg(
mctruth = MCTruthAndBkgCatAlg(
Input=inputs,
filter_MCP=filter_MCP,
DaVinciSmartAssociator=dv_assc,
MCMatchObjP2MCRelator=mcrel_assc,
BackgroundCategory=bkg_cat,
......
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