Belle II Software development
isSignalAcceptFlags.py
1#!/usr/bin/env/python3
2
3
10
11# isSignalAcceptFlags or isSignalAcceptSomething, means a special isSignal variable that accepts certain
12# bits in the mcErrors to be set. For example, the pure isSignal equals to 1 only when mcErrors == 0, while
13# isSignalAcceptMissingGamma equals to 1 when mcErrors == 0 or mcErrors == 16, which could be interpreted
14# as "After setting the MissGamma bit (the 5th bit, or 16 = 0b00010000) to 0, if mcErrors == 0, then
15# isSignalAcceptMissingGamma equals to 1". A new isSignalAcceptSomething variables could be added conveniently
16# by create_isSignal_alias() in variable.utils:
17# >>> c_MissGamma = 16
18# >>> c_DecayInFlight = 4
19# >>> vu.create_isSignal_alias("isSignalAcceptMissingGammaAndDecayInFlight", [c_MissGamma, c_DecayInFlight])
20#
21#
22# if you want to know the detail of create_isSignal_alias(), you could read the paragraph below then check the code
23# in analysis/scripts/variables/utils.py
24# The operation, setting certain bits in a variable to 0, is called "unmask", in contrast to "mask".
25# The function create_isSignal_alias() adds new isSignalAcceptSomething variable by unmasking the mcErrors
26# code using the meta function unmask() like:
27# >>> c_MissGamma = 16
28# >>> c_DecayInFlight = 4
29# >>> c_Correct = 0
30# >>> vm.addAlias("isSignalAcceptMissingGammaAndDecayInFlight", "passesCut(unmask(mcErrors," +
31# ... "%d) == %d)" % (c_MissGamma | c_DecayInFlight, c_Correct))
32# The full definition of mc match error flags could be found in:
33# https://software.belle2.org/development/sphinx/analysis/doc/MCMatching.html#error-flags
34#
35# For full documentation please refer to https://software.belle2.org
36# Anything unclear? Ask questions at https://questions.belle2.org
37
38import basf2 as b2
39import modularAnalysis as ma
40from variables import variables as vm
41import variables.collections as vc
42import variables.utils as vu
43import stdV0s
44
45# adjust log level, which would be helpful when debugging
46# b2.logging.log_level = b2.LogLevel.DEBUG
47
48# Create a new path
49mypath = b2.Path()
50
51# Add input file and ParticleLoader modules to the path
52ma.inputMdstList([b2.find_file('analysis/tests/mdst.root')], path=mypath)
53
54# Add ParticleLoader and ParticleCombiner modules to the path, reconstructing the process
55# Ds*+ -> pi+ D0
56# D0 -> pi- pi+ K_S0
57# K_S0 -> pi+ pi-
58ma.fillParticleList('pi+:all', '', path=mypath)
59ma.fillParticleList('K+:all', '', path=mypath)
60stdV0s.stdKshorts(path=mypath)
61ma.reconstructDecay('D0:Example -> pi-:all pi+:all K_S0:merged', '1.7 < M < 2.0', path=mypath)
62ma.reconstructDecay('D*+:Example -> pi+:all D0:Example', '0 < Q < 0.022 and useCMSFrame(p)>2.3', path=mypath)
63
64# add MC matching process, during which the isSignal and mcErrors are calculated
65ma.matchMCTruth(list_name='D*+:Example', path=mypath)
66
67# declare what variables are needed
68basic_vars = vc.inv_mass + vc.kinematics + vc.mc_truth + vc.mc_variables
69
70# The following isSignalSomething variables are predefined in Variable Manager,
71isSignalSomething_vars = ["isSignalAcceptWrongFSPs"]
72isSignalSomething_vars += ["isSignalAcceptMissingNeutrino", "isSignalAcceptMissingMassive"]
73isSignalSomething_vars += ["isSignalAcceptMissingGamma", "isSignalAcceptMissing", "mcParticleStatus"]
74
75# Try to define your own isSignalAcceptSomething
76c_Correct = 0 # This Particle and all its daughters are perfectly reconstructed.
77c_MissFSR = 1 # A Final State Radiation (FSR) photon is not reconstructed (based
78# on MCParticle::c_IsFSRPhoton).
79c_MissingResonance = 2 # The associated MCParticle decay contained additional non-final-state
80# particles (e.g. a rho) that weren't reconstructed. This is probably O.K. in most cases
81c_DecayInFlight = 4 # A Particle was reconstructed from the secondary decay product of the actual particle.
82# This means that a wrong hypothesis was used to reconstruct it, which e.g. for tracks might
83# mean a pion hypothesis was used for a secondary electron.
84c_MissNeutrino = 8 # A neutrino is missing (not reconstructed).
85c_MissGamma = 16 # A photon (not FSR) is missing (not reconstructed).
86c_MissMassiveParticle = 32 # A generated massive FSP is missing (not reconstructed).
87c_MissKlong = 64 # A Klong is missing (not reconstructed).
88c_MisID = 128 # One of the charged final state particles is mis-identified.
89c_AddedWrongParticle = 256 # A non-FSP Particle has wrong PDG code, meaning one of the daughters (or their daughters)
90# belongs to another Particle.
91c_InternalError = 512 # There was an error in MC matching. Not a valid match. Might indicate fake/background track or cluster.
92c_MissPHOTOS = 1024 # A photon created by PHOTOS was not reconstructed (based on MCParticle::c_IsPHOTOSPhoton)
93c_AddedRecoBremsPhoton = 2048 # A photon added with the bremsstrahlung recovery tools (correctBrems or correctBremsBelle)
94# has no MC particle assigned, or it doesn't belong to the decay chain
95
96# the indicated flags should be provided as an integer list
97vu.create_isSignal_alias("isSignalAcceptMissingGammaAndMissingNeutrino", [16, 8])
98isSignalSomething_vars += ["isSignalAcceptMissingGammaAndMissingNeutrino"]
99
100vu.create_isSignal_alias("isSignalAcceptMissingGammaAndDecayInFlight", [c_MissGamma, c_DecayInFlight])
101isSignalSomething_vars += ["isSignalAcceptMissingGammaAndDecayInFlight"]
102
103# this line show how create_isSignal_alias() work, and is equivalent to the lines above
104vm.addAlias("isSignalAcceptMissingNeutrinoAndWrongFSP", "passesCut(unmask(mcErrors," +
105 f"{int(c_MissGamma | c_MissMassiveParticle | c_MissKlong | c_MissKlong)}) == {int(c_Correct)})")
106isSignalSomething_vars += ["isSignalAcceptMissingNeutrinoAndWrongFSP"]
107
108basic_vars += isSignalSomething_vars
109
110vm.printAliases()
111
112
113# save the variables using variablesToNtuple
114ma.variablesToNtuple('D*+:Example', basic_vars, filename="isSignalAcceptFlags.root",
115 treename='Dst', path=mypath)
116
117b2.process(mypath)
118print(b2.statistics)
def stdKshorts(prioritiseV0=True, fitter='TreeFit', path=None, updateAllDaughters=False, writeOut=False)
Definition: stdV0s.py:17