Belle II Software  release-06-02-00
ConvertAndReconstruct.py
1 #!/usr/bin/env python3
2 # -*- coding: utf-8 -*-
3 
4 
11 
12 import os
13 import sys
14 
15 import basf2
16 import modularAnalysis as ma
17 import variables as va
18 import variables.collections as vc
19 import variables.utils as vu
20 from vertex import kFit
21 from b2biiConversion import convertBelleMdstToBelleIIMdst
22 from b2biiMonitors import addBeamParamsConversionMonitors
23 from b2biiMonitors import addTrackConversionMonitors
24 from b2biiMonitors import addNeutralsConversionMonitors
25 
26 os.environ['USE_GRAND_REPROCESS_DATA'] = '1'
27 os.environ['PGUSER'] = 'g0db'
28 
29 # If you want to create the monitoring histograms (recommended in the beginning of your analysis), just provide any argument
30 monitoring = False
31 if len(sys.argv) == 1:
32  basf2.B2WARNING("In the beginning of an analysis it is recommended to study the monitoring histograms.\n"
33  "These tell you if the conversion works as expected.\n"
34  "If you want to create them, just provide any argument to this script.")
35 else:
36  monitoring = True
37 
38 print('BELLE2_EXTERNALS_DIR = ' + str(os.getenv('BELLE2_EXTERNALS_DIR')))
39 print('BELLE2_EXTERNALS_SUBDIR = ' + str(os.getenv('BELLE2_EXTERNALS_SUBDIR')))
40 print('BELLE2_EXTERNALS_OPTION = ' + str(os.getenv('BELLE2_EXTERNALS_OPTION')))
41 print('BELLE2_EXTERNALS_VERSION = ' + str(os.getenv('BELLE2_EXTERNALS_VERSION')))
42 print('BELLE2_LOCAL_DIR = ' + str(os.getenv('BELLE2_LOCAL_DIR')))
43 print('BELLE2_RELEASE = ' + str(os.getenv('BELLE2_RELEASE')))
44 print('BELLE2_OPTION = ' + str(os.getenv('BELLE2_OPTION')))
45 print('BELLE_POSTGRES_SERVER = ' + str(os.getenv('BELLE_POSTGRES_SERVER')))
46 print('USE_GRAND_REPROCESS_DATA = ' + str(os.getenv('USE_GRAND_REPROCESS_DATA')))
47 print('PANTHER_TABLE_DIR = ' + str(os.getenv('PANTHER_TABLE_DIR')))
48 print('PGUSER = ' + str(os.getenv('PGUSER')))
49 
50 # Convert
51 mypath = basf2.create_path()
52 inputfile = basf2.find_file('b2bii_input_evtgen_exp_07_BptoD0pip-D0toKpipi0-0.mdst', 'examples', False)
53 convertBelleMdstToBelleIIMdst(inputfile, path=mypath)
54 
55 # Reconstruct
56 if monitoring:
57  # Create monitoring histograms if requested
58  addBeamParamsConversionMonitors(path=mypath)
59  addTrackConversionMonitors(path=mypath)
60  addNeutralsConversionMonitors(path=mypath)
61 
62 # Only charged final state particles need to be loaded. The neutral particles
63 # gamma, pi0, K_S0, K_L0, and Lambda0 are already loaded to the 'gamma:mdst',
64 # 'pi0:mdst', 'K_S0:mdst', 'K_L0:mdst', and 'Lambda0:mdst' particle lists,
65 # respectively.
66 ma.fillParticleList('pi+:all', '', path=mypath)
67 ma.fillParticleList('K+:all', '', path=mypath)
68 ma.fillParticleList('mu+:all', '', path=mypath)
69 ma.fillParticleList('e+:all', '', path=mypath)
70 
71 # Let's have a look at the pi0 candidates in 'pi0:mdst' and print the values of some variables
72 # In order to access the MC information we need to run the MC matching first
73 ma.matchMCTruth('pi0:mdst', path=mypath)
74 ma.printVariableValues('gamma:mdst', ['mcPDG', 'E', 'clusterE9E25'], path=mypath)
75 ma.printVariableValues('pi0:mdst', ['mcPDG', 'p', 'M', 'InvM'], path=mypath)
76 
77 # The advantage of the pre-loaded V0s (which are the only ones that you should
78 # use in B2BII analyses) is that the momenta and the position of the daughter
79 # tracks are determined wrt. a pivot at the decay vertex of the V0. In
80 # addition, K_S0:mdst (Lambda0:mdst) has goodKs (goodLambda) and nisKsFinder
81 # outputs attached as extra info. It can be used to select good candidates.
82 ma.cutAndCopyList('K_S0:good', 'K_S0:mdst', cut='goodBelleKshort', path=mypath)
83 
84 # It makes sense to perform a vertex fit of the K_S0 candidates and accept
85 # only those candidates where the vertex fit converged
86 kFit('K_S0:good', 0, path=mypath)
87 
88 # Again, let's print a few variable values:
89 ma.matchMCTruth('K_S0:good', path=mypath)
90 ma.printVariableValues('K_S0:good', ['mcPDG', 'M', 'InvM', 'p', 'px', 'py', 'pz',
91  'extraInfo(goodKs)', 'extraInfo(ksnbVLike)', 'extraInfo(ksnbNoLam)'], path=mypath)
92 
93 # The Belle PID variables are: atcPIDBelle(sigHyp, bkgHyp), muIDBelle, and eIDBelle
94 va.variables.addAlias('Lkpi', 'atcPIDBelle(3,2)')
95 va.variables.addAlias('Lppi', 'atcPIDBelle(4,2)')
96 va.variables.addAlias('Lpk', 'atcPIDBelle(4,3)')
97 
98 # Since we did not apply any PID requirement the 'pi+:all' particle list
99 # contains all type of charged final state particles. Have a look at the
100 # printOut and notice how the true identity correlates with the corresponding
101 # PID values.
102 ma.printVariableValues('pi+:all', ['mcPDG', 'p', 'Lkpi', 'Lppi', 'muIDBelle',
103  'muIDBelleQuality', 'eIDBelle', 'nSVDHits'], path=mypath)
104 
105 # Now, let's really reconstruct a B decay with an intermediate D meson:
106 ma.reconstructDecay('D0:Kpipi0 -> K-:all pi+:all pi0:mdst', '1.7 < M < 2.0', path=mypath)
107 ma.reconstructDecay('B+:D0pi -> anti-D0:Kpipi0 pi+:all', '4.8 < M < 5.5', path=mypath)
108 
109 ma.matchMCTruth('B+:D0pi', path=mypath)
110 
111 # create and fill flat Ntuple with MCTruth and kinematic information
112 kinematics_and_truth = vc.kinematics + vc.mc_truth
113 variables = vu.create_aliases_for_selected(kinematics_and_truth, '^B+ -> [ ^D0 -> ^K- ^pi+ ^pi0] ^pi+')
114 
115 belle1pid = ['eIDBelle', 'muIDBelleQuality', 'muIDBelle', 'Lkpi', 'Lppi', 'Lpk']
116 variables += vu.create_aliases_for_selected(belle1pid, 'B+ -> [ D0 -> ^K- ^pi+ pi0] ^pi+')
117 
118 ma.variablesToNtuple('B+:D0pi', variables, filename='B2BII_ConvertAndReconstruct_Example.root', path=mypath)
119 
120 # progress
121 mypath.add_module('Progress')
122 
123 basf2.process(mypath)
124 
125 # Print call statistics
126 print(basf2.statistics)