Belle II Software  release-08-01-10
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 
38 import basf2 as b2
39 import modularAnalysis as ma
40 from variables import variables as vm
41 import variables.collections as vc
42 import variables.utils as vu
43 import 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
49 mypath = b2.Path()
50 
51 # Add input file and ParticleLoader modules to the path
52 ma.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-
58 ma.fillParticleList('pi+:all', '', path=mypath)
59 ma.fillParticleList('K+:all', '', path=mypath)
60 stdV0s.stdKshorts(path=mypath)
61 ma.reconstructDecay('D0:Example -> pi-:all pi+:all K_S0:merged', '1.7 < M < 2.0', path=mypath)
62 ma.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
65 ma.matchMCTruth(list_name='D*+:Example', path=mypath)
66 
67 # declare what variables are needed
68 basic_vars = vc.inv_mass + vc.kinematics + vc.mc_truth + vc.mc_variables
69 
70 # The following isSignalSomething variables are predefined in Variable Manager,
71 isSignalSomething_vars = ["isSignalAcceptWrongFSPs"]
72 isSignalSomething_vars += ["isSignalAcceptMissingNeutrino", "isSignalAcceptMissingMassive"]
73 isSignalSomething_vars += ["isSignalAcceptMissingGamma", "isSignalAcceptMissing", "mcParticleStatus"]
74 
75 # Try to define your own isSignalAcceptSomething
76 c_Correct = 0 # This Particle and all its daughters are perfectly reconstructed.
77 c_MissFSR = 1 # A Final State Radiation (FSR) photon is not reconstructed (based
78 # on MCParticle::c_IsFSRPhoton).
79 c_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
81 c_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.
84 c_MissNeutrino = 8 # A neutrino is missing (not reconstructed).
85 c_MissGamma = 16 # A photon (not FSR) is missing (not reconstructed).
86 c_MissMassiveParticle = 32 # A generated massive FSP is missing (not reconstructed).
87 c_MissKlong = 64 # A Klong is missing (not reconstructed).
88 c_MisID = 128 # One of the charged final state particles is mis-identified.
89 c_AddedWrongParticle = 256 # A non-FSP Particle has wrong PDG code, meaning one of the daughters (or their daughters)
90 # belongs to another Particle.
91 c_InternalError = 512 # There was an error in MC matching. Not a valid match. Might indicate fake/background track or cluster.
92 c_MissPHOTOS = 1024 # A photon created by PHOTOS was not reconstructed (based on MCParticle::c_IsPHOTOSPhoton)
93 c_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
97 vu.create_isSignal_alias("isSignalAcceptMissingGammaAndMissingNeutrino", [16, 8])
98 isSignalSomething_vars += ["isSignalAcceptMissingGammaAndMissingNeutrino"]
99 
100 vu.create_isSignal_alias("isSignalAcceptMissingGammaAndDecayInFlight", [c_MissGamma, c_DecayInFlight])
101 isSignalSomething_vars += ["isSignalAcceptMissingGammaAndDecayInFlight"]
102 
103 # this line show how create_isSignal_alias() work, and is equivalent to the lines above
104 vm.addAlias("isSignalAcceptMissingNeutrinoAndWrongFSP", "passesCut(unmask(mcErrors," +
105  "%d) == %d)" % (c_MissGamma | c_MissMassiveParticle | c_MissKlong | c_MissKlong, c_Correct))
106 isSignalSomething_vars += ["isSignalAcceptMissingNeutrinoAndWrongFSP"]
107 
108 basic_vars += isSignalSomething_vars
109 
110 vm.printAliases()
111 
112 
113 # save the variables using variablesToNtuple
114 ma.variablesToNtuple('D*+:Example', basic_vars, filename="isSignalAcceptFlags.root",
115  treename='Dst', path=mypath)
116 
117 b2.process(mypath)
118 print(b2.statistics)
def stdKshorts(prioritiseV0=True, fitter='TreeFit', path=None, updateAllDaughters=False, writeOut=False)
Definition: stdV0s.py:17