Belle II Software development
charmFlavorTagger.py
1#!/usr/bin/env python3
2
3
10
11import basf2
12import variables as va
13import modularAnalysis as ma
14import math
15
16
17def charmFlavorTagger(particle_list, uniqueIdentifier='CFT_ragdoll',
18 path=None):
19 """
20 Interfacing for the Charm Flavor Tagger.
21
22 This function requires a reconstructed D meson signal particle list with a built RestOfEvent.
23
24 :param particle_list: string, particle list of the reconstructed signal D meson
25 :param uniqueIdentifier: string, database identifier for the method
26 :param path: basf2 path obj
27 :return: None
28 """
29
30 # create roe specific paths
31 roe_path = basf2.create_path()
32 dead_end_path = basf2.create_path()
33
34 # define cft specific lists to enable multiple calls, if someone really wants to do that
35 extension = particle_list.replace(':', '_to_')
36 roe_particle_list_cut = 'isInRestOfEvent == 1 and dr < 1 and abs(dz) < 3'
37 roe_particle_list = 'pi+:cft' + '_' + extension
38
39 # filter rest of events only for specific particle list
40 ma.signalSideParticleFilter(particle_list, '', roe_path, dead_end_path)
41
42 # create final state particle lists
43 ma.fillParticleList(roe_particle_list, roe_particle_list_cut, path=roe_path)
44
45 # compute ranking variable and additional CFT input variables, PID_diff=pionID-kaonID, deltaR=sqrt(deltaPhi**2+deltaEta**2)
46 rank_variable = 'opang_shift'
47 va.variables.addAlias(rank_variable, f"abs(formula(angleToClosestInList({particle_list}) - {math.pi}/2))")
48 va.variables.addAlias("eta", "formula(-1*log(tan(formula(theta/2))))")
49 va.variables.addAlias("phi_sig", "particleRelatedToCurrentROE(phi)")
50 va.variables.addAlias("eta_sig", "particleRelatedToCurrentROE(eta)")
51 va.variables.addAlias("deltaPhi_temp", "abs(formula(phi-phi_sig))")
52 va.variables.addAlias(
53 "deltaPhi",
54 f"conditionalVariableSelector(deltaPhi_temp>{math.pi},formula(deltaPhi_temp-2*{math.pi}),deltaPhi_temp)")
55 va.variables.addAlias("deltaR", "formula(((deltaPhi)**2+(eta-eta_sig)**2)**0.5)")
56 va.variables.addAlias("PID_diff", "formula(pionID-kaonID)")
57
58 # split tracks by charge, rank them (keep only the three highest ranking) and write CFT input to extraInfo of signal particle
59 var_list = ['mRecoil', 'PID_diff', 'deltaR']
60 cft_particle_dict = {'pi+:pos_charge': ['charge > 0 and p < infinity', 'p'],
61 'pi+:neg_charge': ['charge < 0 and p < infinity', 'n']}
62
63 for listName, config in cft_particle_dict.items():
64 ma.cutAndCopyList(listName, roe_particle_list, config[0], writeOut=True, path=roe_path)
65 ma.rankByHighest(listName, rank_variable, numBest=3, path=roe_path)
66 roe_dict = {}
67 suffix = config[1]
68 for var in var_list:
69 for i_num in range(1, 3 + 1):
70 roe_dict[f'eventCached(getVariableByRank({listName}, {rank_variable}, {var}, {i_num}))'] = (
71 f'pi_{i_num}_{suffix}_{var}')
72 va.variables.addAlias(f'pi_{i_num}_{suffix}_{var}', f'extraInfo(pi_{i_num}_{suffix}_{var})')
73
74 ma.variableToSignalSideExtraInfo(listName, roe_dict, path=roe_path)
75
76 # apply CFT with MVAExpert module and write output to extraInfo
77 expert_module = basf2.register_module('MVAExpert')
78 expert_module.param('listNames', [particle_list])
79 expert_module.param('identifier', uniqueIdentifier)
80
81 expert_module.param('extraInfoName', 'CFT_out')
82
83 roe_path.add_module(expert_module)
84
85 # The CFT output probability should be 0.5 when no track is reconstructed in the ROE
86 va.variables.addAlias(
87 'CFT_prob',
88 'conditionalVariableSelector(isNAN(pi_1_p_deltaR) and isNAN(pi_1_n_deltaR),0.5,formula(1-extraInfo(CFT_out)))')
89 va.variables.addAlias('CFT_qr', 'formula(2*CFT_prob-1)')
90
91 path.for_each('RestOfEvent', 'RestOfEvents', roe_path)