Belle II Software development
b2biiConversion.py
1#!/usr/bin/env python3
2
3
10
11import basf2 as b2
12import b2bii
13from modularAnalysis import setAnalysisConfigParams
14import os
15import re
16import requests
17from ctypes import cdll
18
19
20def setupBelleDatabaseServer():
21 """
22 Sets the Belle DB server to the one recommended in /sw/belle/local/var/belle_postgres_server.
23
24 If the user does not have the access rights to the above file, the can01 server is set by default.
25 """
26 belleDBServerFile = '/sw/belle/local/var/belle_postgres_server'
27 belleDBServer = 'can01'
28
29 try:
30 with open(belleDBServerFile) as f:
31 belleDBServer = (f.read()).strip()
32 except OSError:
33 pass
34
35 os.environ['PGUSER'] = 'g0db'
36 os.environ['BELLE_POSTGRES_SERVER'] = belleDBServer
37
38
39def convertBelleMdstToBelleIIMdst(inputBelleMDSTFile, applySkim=True,
40 saveResultExtraInfo=False,
41 useBelleDBServer=None,
42 convertBeamParameters=True,
43 generatorLevelReconstruction=False,
44 generatorLevelMCMatching=False,
45 evtgenProcessing=False,
46 path=None, entrySequences=None,
47 matchType2E9oE25Threshold=-1.1,
48 enableNisKsFinder=True,
49 HadronA=True, HadronB=True,
50 enableRecTrg=False, enableEvtcls=True,
51 SmearTrack=2, enableLocalDB=True,
52 convertNbar=False):
53 """
54 Loads Belle MDST file and converts in each event the Belle MDST dataobjects to Belle II MDST
55 data objects and loads them to the StoreArray.
56
57 Args:
58 inputBelleMDSTFile (str): Name of the file(s) to be loaded.
59 applySkim (bool): Apply skim conditions in B2BIIFixMdst.
60 saveResultExtraInfo (bool): Save B2BIIFixMdst module return value as EventExtraInfo.
61 useBelleDBServer (str): None to use the recommended BelleDB server.
62 convertBeamParameters (bool): Convert beam parameters or use information stored in Belle II database.
63 generatorLevelReconstruction (bool): Enables to bypass skims and corrections applied in B2BIIFixMdst.
64 generatorLevelMCMatching (bool): Enables to switch MCTruth matching to generator-level particles.
65 This is recommended for analyses with gammas in the final state.
66 evtgenProcessing (bool): Enables to switch on only evtgen processing.
67 path (basf2.Path): Path to add modules in.
68 entrySequences (list(str)): The number sequences (e.g. 23:42,101) defining
69 the entries which are processed for each inputFileName.
70 matchType2E9oE25Threshold (float): Clusters with a E9/E25 value above this threshold are classified as neutral
71 even if tracks are matched to their connected region (matchType == 2 in basf).
72 enableNisKsFinder (bool): Enables to convert nisKsFinder information.
73 HadronA (bool): Enables to switch on HadronA skim in B2BIIFixMdst module.
74 HadronB (bool): Enables to switch on HadronB skim in B2BIIFixMdst module.
75 enableRecTrg (bool): Enables to convert RecTrg_summary3 table.
76 enableEvtcls (bool): Enables to convert Evtcls and Evtcls_hadronic tables.
77 SmearTrack (float): Smear the MC tracks to match real data.
78 Apart from the recommended default value of 2 it can also be set to 1.
79 Details about the difference between those two options can be found
80 `here <https://belle.kek.jp/secured/wiki/doku.php?id=physics:charm:tracksmearing>`_.
81 Set to 0 to skip smearing (automatically set to 0 internally for real data).
82 enableLocalDB (bool): Enables to use local payloads.
83 convertNbar (bool): Enables conversion of anti-n0:mdst.
84 """
85
86 # If we are on KEKCC make sure we load the correct NeuroBayes library
87 try:
88 cdll.LoadLibrary(
89 '/sw/belle/local/neurobayes/lib/libNeuroBayesCore_shared.so')
90 except BaseException:
91 pass
92
93 if useBelleDBServer is None:
94 setupBelleDatabaseServer()
95 else:
96 os.environ['BELLE_POSTGRES_SERVER'] = useBelleDBServer
97
98 b2.B2INFO('Belle DB server is set to: ' + os.environ['BELLE_POSTGRES_SERVER'])
99
100 setAnalysisConfigParams({'mcMatchingVersion': 'Belle'}, path)
101
103
104 if enableLocalDB is True:
105 b2.conditions.metadata_providers = ["/sw/belle/b2bii/database/conditions/b2bii.sqlite"]
106 b2.conditions.payload_locations = ["/sw/belle/b2bii/database/conditions/"]
107 else:
108 b2.B2WARNING(
109 "B2BII is accessing the payloads from the conditions database.\n"
110 "The recommended procedure is to use the offline database and it is significantly\n"
111 "faster than using the global database.\n"
112 "If you need the payloads which are not included in the current offline database,\n"
113 "please contact b2bii librarian.")
114
115 input = b2.register_module('B2BIIMdstInput')
116 if inputBelleMDSTFile is not None:
117 input.param('inputFileNames', parse_process_url(inputBelleMDSTFile))
118 if entrySequences is not None:
119 input.param('entrySequences', entrySequences)
120 input.param('evtgenProcessing', evtgenProcessing)
121 # input.logging.set_log_level(LogLevel.DEBUG)
122 # input.logging.set_info(LogLevel.DEBUG, LogInfo.LEVEL | LogInfo.MESSAGE)
123 path.add_module(input)
124
125 # we need magnetic field which is different than default.
126 # shamelessly copied from analysis/scripts/modularAnalysis.py:inputMdst
127 from ROOT import Belle2 # reduced scope of potentially-misbehaving import
128 from ROOT.Math import XYZVector
129 field = Belle2.MagneticField()
130 field.addComponent(
132 XYZVector(0, 0, 1.5 * Belle2.Unit.T)))
133 Belle2.DBStore.Instance().addConstantOverride("MagneticField", field, False)
134
135 if (not generatorLevelReconstruction):
136 # Fix MSDT Module
137 fix = b2.register_module('B2BIIFixMdst')
138 # fix.logging.set_log_level(LogLevel.DEBUG)
139 # fix.logging.set_info(LogLevel.DEBUG, LogInfo.LEVEL | LogInfo.MESSAGE)
140 fix.param('SaveResultExtraInfo', saveResultExtraInfo)
141 # Hadron skim settings
142 fix.param('HadronA', HadronA)
143 fix.param('HadronB', HadronB)
144 fix.param('Smear_trk', SmearTrack)
145 if (HadronA is not True and HadronB is True):
146 b2.B2WARNING(
147 'The Hadron A skim is turned off.'
148 'However, its requirements are still applied since the HadronB(J) skim, which includes them, is turned on.')
149 path.add_module(fix)
150
151 if (applySkim):
152 emptypath = b2.create_path()
153 # discard 'bad events' marked by fixmdst
154 fix.if_value('<=0', emptypath)
155 else:
156 b2.B2INFO('applySkim is set to be False.'
157 'No bad events marked by fixmdst will be discarded.'
158 'Corrections will still be applied.')
159 else:
160 b2.B2INFO('Perform generator level reconstruction, no corrections or skims in fix_mdst will be applied.')
161 # Convert MDST Module
162 convert = b2.register_module('B2BIIConvertMdst')
163 convert.param('convertBeamParameters', convertBeamParameters)
164 if (generatorLevelMCMatching):
165 convert.param('mcMatchingMode', 'GeneratorLevel')
166 convert.param("matchType2E9oE25Threshold", matchType2E9oE25Threshold)
167 convert.param("nisKsInfo", enableNisKsFinder)
168 convert.param("RecTrg", enableRecTrg)
169 convert.param("convertEvtcls", enableEvtcls)
170 convert.param("convertNbar", convertNbar)
171 convert.param('evtgenProcessing', evtgenProcessing)
172 # convert.logging.set_log_level(LogLevel.DEBUG)
173 # convert.logging.set_info(LogLevel.DEBUG, LogInfo.LEVEL | LogInfo.MESSAGE)
174 path.add_module(convert)
175 if convertNbar:
176 b2.conditions.append_globaltag('BellePID')
177 path.add_module('BelleNbarMVA', particleList='anti-n0:mdst', identifier='nbarMVA')
178
179
180def parse_process_url(url):
181 """
182 Handle process_url style requests.
183 If url is a list just return it without looking at it. If the url starts
184 with http(s):// return the list of files provided by that url, otherwise
185 check if a file with the name given as url exists and return this as a list
186 with one item. If not such file exists return an empty list
187
188 >>> parse_process_url(["foo", "bar"])
189 ['foo', 'bar']
190 >>> parse_process_url("/dev/null")
191 ['/dev/null']
192 >>> parse_process_url("missing_file.mdst")
193 []
194 >>> parse_process_url("http://bweb3.cc.kek.jp/mdst.php?ex=55&rs=1&re=5&skm=HadronBorJ&dt=on_resonance&bl=caseB") \
195 # doctest: +NORMALIZE_WHITESPACE
196 ['/group/belle/bdata_b/dstprod/dat/e000055/HadronBJ/0127/on_resonance/00/HadronBJ-e000055r000003-b20090127_0910.mdst',
197 '/group/belle/bdata_b/dstprod/dat/e000055/HadronBJ/0127/on_resonance/00/HadronBJ-e000055r000004-b20090127_0910.mdst',
198 '/group/belle/bdata_b/dstprod/dat/e000055/HadronBJ/0127/on_resonance/00/HadronBJ-e000055r000005-b20090127_0910.mdst']
199
200 Some files are missing and thus marked with "### LOST ###", make sure we don't pick them up
201
202 >>> parse_process_url("http://bweb3.cc.kek.jp/mdst.php?ex=65&rs=1&re=30&skm=ypipi")
203 []
204
205 If the URL cannot be found an error is raised and an empty list is returned.
206 Also for other http errors or non existing urls
207
208 >>> parse_process_url("http://httpbin.org/status/404")
209 []
210 >>> parse_process_url("http://httpbin.org/status/500")
211 []
212 >>> parse_process_url("http://nosuchurl")
213 []
214 """
215
216 if isinstance(url, list) or isinstance(url, tuple):
217 return url
218
219 if not url.startswith("http://") and not url.startswith("https://"):
220 if os.path.exists(url):
221 return [url]
222 else:
223 b2.B2ERROR(
224 f"Could not parse url '{url}': no such file or directory")
225 return []
226
227 # regular expression to find process_event lines in html response
228 process_event = re.compile(br"^\s*process_event\s+(.*)\s+0$", re.MULTILINE)
229 # get the url
230 try:
231 request = requests.get(url)
232 request.raise_for_status()
233 return [e.decode("ASCII")
234 for e in process_event.findall(request.content)]
235 except (requests.ConnectionError, requests.HTTPError) as e:
236 b2.B2ERROR(
237 f"Failed to connect to '{url}': {str(e)}")
238
239 return []
setB2BII()
Definition b2bii.py:21
Describe one component of the Geometry.
Magnetic field map.
static DBStore & Instance()
Instance of a singleton DBStore.
Definition DBStore.cc:26