11from basf2
import B2FATAL, B2RESULT, Path, process, conditions, register_module
13import modularAnalysis
as ma
14from variables
import variables
27def make_1D_bin(name, min_val, max_val):
28 return {name: [min_val, max_val]}
31def make_2D_bin(bin_x, bin_y):
37def make_3D_bin(bin_x, bin_y, bin_z):
39 bin_3d.update(bin_y).update(bin_z)
43def check(ntupleName, treeName):
45 Verify results make sense.
47 ntuplefile = TFile(ntupleName)
48 ntuple = ntuplefile.Get(treeName)
50 if ntuple.GetEntries() == 0:
51 B2FATAL(
"No pions saved")
53 if not(ntuple.GetEntries(
"binID > 0 ") > 0):
54 B2FATAL(
"Binning was applied incorrectly: no pions in physical bins")
56 B2RESULT(
"Bins are defined")
58 if not(ntuple.GetEntries(
"Weight > 0") > 0):
59 B2FATAL(
"Weights are not applied")
61 B2RESULT(
"Weights are applied")
73bins_x = [make_1D_bin(
"p", 0, 2),
74 make_1D_bin(
"p", 2, 3),
75 make_1D_bin(
"p", 3, 4)]
77bins_y = [make_1D_bin(
"pz", 0, 1),
78 make_1D_bin(
"pz", 1, 5)]
99 weightInfo[
"Weight"] = float(random.randint(0, 100)) / 100
100 weightInfo[
"StatErr"] = float(random.randint(100, 200)) / 100
101 weightInfo[
"SystErr"] = float(10)
102 tableIDNotSpec.append([weightInfo, make_2D_bin(xbin, ybin)])
127 weightInfo[
"Weight"] = float(random.randint(0, 100)) / 100
128 weightInfo[
"StatErr"] = float(random.randint(100, 200)) / 100
129 weightInfo[
"SystErr"] = float(10)
130 tableIDSpec.append([[weightInfo, make_2D_bin(xbin, ybin)], binID])
133outOfRangeWeightInfo = {}
134outOfRangeWeightInfo[
"Weight"] = -1
135outOfRangeWeightInfo[
"StatErr"] = -1
136outOfRangeWeightInfo[
"SystErr"] = -1
140 conditions.testing_payloads = [
"localdb/database.txt"]
143 addtable = register_module(
'ParticleWeightingLookUpCreator')
144 addtable.param(
'tableIDNotSpec', tableIDNotSpec)
145 addtable.param(
'outOfRangeWeight', outOfRangeWeightInfo)
146 addtable.param(
'experimentHigh', 1003)
147 addtable.param(
'experimentLow', 1003)
148 addtable.param(
'runHigh', 1000)
149 addtable.param(
'runLow', 0)
150 addtable.param(
'tableName',
"ParticleReweighting:TestMomentum")
152 addtable2 = register_module(
'ParticleWeightingLookUpCreator')
153 addtable2.param(
'tableIDSpec', tableIDSpec)
154 addtable2.param(
'outOfRangeWeight', outOfRangeWeightInfo)
155 addtable2.param(
'experimentHigh', 1003)
156 addtable2.param(
'experimentLow', 1003)
157 addtable2.param(
'runHigh', 1000)
158 addtable2.param(
'runLow', 0)
159 addtable2.param(
'tableName',
"ParticleReweighting:TestMomentum2")
162 testpath.add_module(addtable)
163 testpath.add_module(addtable2)
164 testpath.add_module(
'EventInfoSetter', evtNumList=[100], runList=[0], expList=[1003])
168 B2RESULT(
"Weights are created and loaded to DB")
172 ntupleName =
'particleWeighting.root'
175 ma.inputMdst(inputfile, path=main)
178 ma.fillParticleListFromMC(
'pi+:gen',
'', path=main)
181 weight_table_id =
"ParticleReweighting:TestMomentum"
185 variables.addAlias(
'Weight',
'extraInfo(' + weight_table_id +
'_Weight)')
186 variables.addAlias(
'StatErr',
'extraInfo(' + weight_table_id +
'_StatErr)')
187 variables.addAlias(
'SystErr',
'extraInfo(' + weight_table_id +
'_SystErr)')
188 variables.addAlias(
'binID',
'extraInfo(' + weight_table_id +
'_binID)')
189 varsPi = [
'p',
'pz',
'Weight',
'StatErr',
'SystErr',
'binID']
192 reweighter = register_module(
'ParticleWeighting')
193 reweighter.param(
'tableName', weight_table_id)
194 reweighter.param(
'particleList',
'pi+:gen')
195 main.add_module(reweighter)
198 ma.variablesToNtuple(
'pi+:gen', varsPi, filename=ntupleName, treename=treeName, path=main)
203 check(ntupleName, treeName)
205B2RESULT(
"Weights were applied correctly")
def require_file(filename, data_type="", py_case=None)
def clean_working_directory()
def safe_process(*args, **kwargs)