Skip to content
Snippets Groups Projects
Commit 6f372662 authored by Jiahui Zhuo's avatar Jiahui Zhuo :penguin: Committed by Sebastien Ponce
Browse files

Functor to replace MCTupleToolPrompt

parent 2e37cccb
No related branches found
No related tags found
2 merge requests!1103Draft: Add AnalysisHelpers to DaVinci Stack,!800Functor to replace MCTupleToolPrompt
Showing with 61 additions and 27 deletions
...@@ -38,7 +38,8 @@ def main(options: Options): ...@@ -38,7 +38,8 @@ def main(options: Options):
#Get variables related to reconstructible information. #Get variables related to reconstructible information.
mcrtible = MCReconstructible(mc_track_info=get_mc_track_info()) mcrtible = MCReconstructible(mc_track_info=get_mc_track_info())
#The option extra_info is set to False by default and can be set to True to get more information #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) vars_rtible = MCReconstructible_Collection(
mcreconstructible_alg=mcrtible, extra_info=True)
print('Reconstructible functors:', vars_rtible.functor_dict.keys()) print('Reconstructible functors:', vars_rtible.functor_dict.keys())
#Note instead of importing functorcollections (MCReconstructed_Collection), one #Note instead of importing functorcollections (MCReconstructed_Collection), one
# can directly add track related information using the helper class (MCReconstructible) # can directly add track related information using the helper class (MCReconstructible)
...@@ -58,7 +59,8 @@ def main(options: Options): ...@@ -58,7 +59,8 @@ def main(options: Options):
relations_charged=relations_charged, relations_charged=relations_charged,
relations_neutral=relations_neutral) relations_neutral=relations_neutral)
#The option extra_info below is set to False by default in the functor collection. #The option extra_info below is set to False by default in the functor collection.
vars_rted = MCReconstructed_Collection(mcrted_all, extra_info=False) vars_rted = MCReconstructed_Collection(
mcreconstructed_alg=mcrted_all, extra_info=False)
#Note: #Note:
# - Instead of importing functorcollections (MCReconstructed_Collection), one # - Instead of importing functorcollections (MCReconstructed_Collection), one
# can directly add track related information using the helper class (MCReconstructed). # can directly add track related information using the helper class (MCReconstructed).
......
...@@ -10,11 +10,12 @@ ...@@ -10,11 +10,12 @@
############################################################################### ###############################################################################
""" """
Read an HLT2 file and create an ntuple using pre-defined Functor collections. Read an HLT2 file and create an ntuple using pre-defined Functor collections.
This example is also used to test all available functor collections.
""" """
import Functors as F import Functors as F
from DaVinciMCTools import MCReconstructible, MCReconstructed from DaVinciMCTools import MCReconstructible, MCReconstructed
from FunTuple import FunctorCollection, functorcollections from FunTuple import FunctorCollection, functorcollections as functorcollections_original
from FunTuple import FunTuple_Particles as Funtuple, FunTuple_MCParticles as MCFuntuple from FunTuple import FunTuple_Particles as Funtuple, FunTuple_MCParticles as MCFuntuple
from DaVinci.algorithms import add_filter from DaVinci.algorithms import add_filter
from DaVinci import Options, make_config from DaVinci import Options, make_config
...@@ -24,54 +25,86 @@ from Gaudi.Configuration import INFO ...@@ -24,54 +25,86 @@ from Gaudi.Configuration import INFO
from PyConf.reading import get_particles, get_mc_particles, get_mc_track_info from PyConf.reading import get_particles, get_mc_particles, get_mc_track_info
class check_wrapper:
def __init__(self, functorcollection):
self.functorcollection = functorcollection
self.called = set()
def __getattr__(self, name: str):
func = getattr(self.functorcollection, name)
self.called.add(func.__name__)
return func
def number_of_functor_collection(self):
return len(self.functorcollection.__all__)
def number_of_called_functor_collection(self):
return len(self.called)
def check(self):
if self.number_of_functor_collection(
) != self.number_of_called_functor_collection():
return False
sorted_set_all = sorted(set(self.functorcollection.__all__))
sorted_set_called = sorted(self.called)
if sorted_set_all != sorted_set_called:
return False
return True
def main(options: Options): def main(options: Options):
line_name = 'Hlt2Charm_D0ToKmPip_Line' line_name = 'Hlt2Charm_D0ToKmPip_Line'
d02kpi_data = get_particles(f"/Event/HLT2/{line_name}/Particles") d02kpi_data = get_particles(f"/Event/HLT2/{line_name}/Particles")
MC_data = get_mc_particles("/Event/HLT2/MC/Particles") MC_data = get_mc_particles("/Event/HLT2/MC/Particles")
#Get variables related to reconstructible information. # Get variables related to reconstructible information.
mcrtible = MCReconstructible(mc_track_info=get_mc_track_info()) mcrtible = MCReconstructible(mc_track_info=get_mc_track_info())
#Get variables related to reconstructed information. # Get variables related to reconstructed information.
mcrted_all = MCReconstructed(MC_data, use_best_mcmatch=True) mcrted_all = MCReconstructed(MC_data, use_best_mcmatch=True)
#get configured "MCTruthAndBkgCatAlg" algorithm for HLT2 output # get configured "MCTruthAndBkgCatAlg" algorithm for HLT2 output
mctruth = configured_MCTruthAndBkgCatAlg(inputs=d02kpi_data) mctruth = configured_MCTruthAndBkgCatAlg(inputs=d02kpi_data)
#configure "WeightedRelTableAlg" algorithm for HLT2 output # configure "WeightedRelTableAlg" algorithm for HLT2 output
iso_rel_table = WeightedRelTableAlg( iso_rel_table = WeightedRelTableAlg(
ReferenceParticles=d02kpi_data, ReferenceParticles=d02kpi_data,
InputCandidates=d02kpi_data, InputCandidates=d02kpi_data,
Cut=(F.DR2() < 0.4), Cut=(F.DR2() < 0.4),
OutputLevel=INFO) OutputLevel=INFO)
# initialize the checker
functorcollections = check_wrapper(functorcollections_original)
# use functorcollections to add variables, please expand when there's new collections :) # use functorcollections to add variables, please expand when there's new collections :)
collections = [ collections = [
functorcollections.Kinematics(), functorcollections.Kinematics(),
functorcollections.MCHierarchy(mctruth), functorcollections.MCHierarchy(mctruth=mctruth),
functorcollections.MCKinematics(mctruth), functorcollections.MCKinematics(mctruth=mctruth),
functorcollections.MCVertexInfo(mctruth), functorcollections.MCVertexInfo(mctruth=mctruth),
functorcollections.TrackIsolation(iso_rel_table), functorcollections.MCPromptDecay(mctruth=mctruth),
functorcollections.TrackIsolation(iso_rel_table=iso_rel_table),
] ]
MC_collections = [ MC_collections = [
functorcollections.MCReconstructible_Collection( functorcollections.MCReconstructible_Collection(
mcrtible, extra_info=True), mcreconstructible_alg=mcrtible, extra_info=True),
functorcollections.MCReconstructed_Collection( functorcollections.MCReconstructed_Collection(
mcrted_all, extra_info=False), mcreconstructed_alg=mcrted_all, extra_info=False),
functorcollections.MCHierarchy(), functorcollections.MCHierarchy(),
functorcollections.MCVertexInfo(),
functorcollections.MCPromptDecay(),
] ]
evt_collections = [ evt_collections = [
functorcollections.EventInfo(), functorcollections.EventInfo(),
functorcollections.SelectionInfo("Hlt2", [line_name]) functorcollections.SelectionInfo(
sel_type="Hlt2", line_names=[line_name])
] ]
assert len(collections) + len(evt_collections) + len( assert functorcollections.check(
MC_collections) - 1 == len( # -1 because MCHierarchy is tested twice. ), "Oh no! Did you forget to add a new collection to this test?"
functorcollections.__all__
), "Oh no! Did you forget to add a new collection to this test?"
field_vars = FunctorCollection() field_vars = FunctorCollection()
for coll in collections: for coll in collections:
......
...@@ -101,7 +101,7 @@ def main(options: Options): ...@@ -101,7 +101,7 @@ def main(options: Options):
"RUNNUMBER": F.RUNNUMBER(odin), "RUNNUMBER": F.RUNNUMBER(odin),
"EVENTNUMBER": F.EVENTNUMBER(odin) "EVENTNUMBER": F.EVENTNUMBER(odin)
}) })
evt_variables += SelectionInfo("Hlt2", [line_name]) evt_variables += SelectionInfo(sel_type="Hlt2", line_names=[line_name])
#For now remove: The 'Hlt2' line decision tuples fine but breaks unit test with an error. (Why?) #For now remove: The 'Hlt2' line decision tuples fine but breaks unit test with an error. (Why?)
#see linked issue here: https://gitlab.cern.ch/lhcb/DaVinci/-/merge_requests/654#note_5320732 #see linked issue here: https://gitlab.cern.ch/lhcb/DaVinci/-/merge_requests/654#note_5320732
evt_variables.pop('Hlt2') evt_variables.pop('Hlt2')
......
...@@ -21,7 +21,7 @@ from Gaudi.Configuration import INFO ...@@ -21,7 +21,7 @@ from Gaudi.Configuration import INFO
from FunTuple import FunTuple_Particles as Funtuple from FunTuple import FunTuple_Particles as Funtuple
from FunTuple import FunctorCollection as FC from FunTuple import FunctorCollection as FC
from DaVinci.algorithms import add_filter from DaVinci.algorithms import add_filter
from PyConf.reading import get_particles, get_odin from PyConf.reading import get_particles
from DaVinciTools import SubstitutePID from DaVinciTools import SubstitutePID
from PyConf.Algorithms import ParticleContainerMerger from PyConf.Algorithms import ParticleContainerMerger
from FunTuple.functorcollections import EventInfo from FunTuple.functorcollections import EventInfo
...@@ -74,8 +74,7 @@ def main(options: Options): ...@@ -74,8 +74,7 @@ def main(options: Options):
variables = {'ALL': variables_all} variables = {'ALL': variables_all}
# Get event information # Get event information
odin = get_odin(options) evt_vars = EventInfo(extra_info=True)
evt_vars = EventInfo(odin)
# #
# Configure Funtuple algorithms # Configure Funtuple algorithms
......
...@@ -77,7 +77,7 @@ def main(options: Options): ...@@ -77,7 +77,7 @@ def main(options: Options):
variables_all = FunctorCollection({'THOR_P': F.P, 'THOR_PT': F.PT}) variables_all = FunctorCollection({'THOR_P': F.P, 'THOR_PT': F.PT})
track_iso_variables = TrackIsolation(ftAlg) track_iso_variables = TrackIsolation(iso_rel_table=ftAlg)
variables_jpsik = { variables_jpsik = {
'B': variables_all + extra_variables, 'B': variables_all + extra_variables,
......
...@@ -40,7 +40,7 @@ def main(options: Options): ...@@ -40,7 +40,7 @@ def main(options: Options):
] ]
#define event level variables #define event level variables
evt_variables = SelectionInfo("Hlt1", Hlt1_decisions) evt_variables = SelectionInfo(sel_type="Hlt1", line_names=Hlt1_decisions)
#define FunTuple instance #define FunTuple instance
my_tuple = Funtuple( my_tuple = Funtuple(
......
...@@ -79,7 +79,7 @@ def main(options: Options): ...@@ -79,7 +79,7 @@ def main(options: Options):
turbo_line = "Hlt2BsToJpsiPhi_JPsi2MuMu_PhiToKK_Line" turbo_line = "Hlt2BsToJpsiPhi_JPsi2MuMu_PhiToKK_Line"
turbo_line2 = "Hlt2BsToJpsiPhi_JPsi2ee_PhiToKK_Line" turbo_line2 = "Hlt2BsToJpsiPhi_JPsi2ee_PhiToKK_Line"
line_names = [f'{turbo_line}Decision', f'{turbo_line2}'] line_names = [f'{turbo_line}Decision', f'{turbo_line2}']
selinfo = SelectionInfo(sel_type, line_names) selinfo = SelectionInfo(sel_type=sel_type, line_names=line_names)
print(selinfo) print(selinfo)
#Define variables dictionary "field name" -> Collections of functor #Define variables dictionary "field name" -> Collections of functor
......
...@@ -50,8 +50,8 @@ def main(options: Options): ...@@ -50,8 +50,8 @@ def main(options: Options):
#Pass it to collections #Pass it to collections
kin = Kinematics() kin = Kinematics()
mckin = MCKinematics(mctruth) mckin = MCKinematics(mctruth=mctruth)
mchierarchy = MCHierarchy(mctruth) mchierarchy = MCHierarchy(mctruth=mctruth)
#print(mckin) #print(mckin)
#print(mchierarchy) #print(mchierarchy)
......
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