Belle II Software  release-05-01-25
B2A905-ApplyWeightsToTracks.py
1 #!/usr/bin/env python3
2 
3 
14 
15 import basf2 as b2
16 import modularAnalysis as ma
17 import variables as va
18 import os
19 
20 # create path
21 my_path = b2.create_path()
22 
23 # load input ROOT file
24 ma.inputMdst(environmentType='default',
25  filename=b2.find_file('B2pi0D_D2hh_D2hhh_B2munu.root', 'examples', False),
26  path=my_path)
27 
28 # use standard final state particle lists
29 # creates "pi+:all" ParticleList (and c.c.)
30 ma.fillParticleListFromMC(decayString='pi+:gen', cut='', path=my_path)
31 
32 # ID of weight table is taked from B2A904
33 weight_table_id = "ParticleReweighting:TestMomentum"
34 
35 if not os.getenv('BELLE2_EXAMPLES_DATA_DIR'):
36  b2.B2FATAL("You need the example data installed. Run `b2install-data example` in terminal for it.")
37 
38 db_location = os.getenv('BELLE2_EXAMPLES_DATA_DIR') + '/database/'
39 b2.use_local_database(db_location + 'database.txt',
40  directory=db_location,
41  readonly=True)
42 
43 # We know what weight info will be added (see B2A904),
44 # so we add aliases and add it ot tools
45 va.variables.addAlias('Weight', 'extraInfo(' + weight_table_id + '_Weight)')
46 va.variables.addAlias('StatErr', 'extraInfo(' + weight_table_id + '_StatErr)')
47 va.variables.addAlias('SystErr', 'extraInfo(' + weight_table_id + '_SystErr)')
48 va.variables.addAlias('binID', 'extraInfo(' + weight_table_id + '_binID)')
49 
50 
51 # We configure weighing module
52 reweighter = b2.register_module('ParticleWeighting')
53 reweighter.param('tableName', weight_table_id)
54 reweighter.param('particleList', 'pi+:gen')
55 my_path.add_module(reweighter)
56 
57 
58 pivars = ['p', 'pz', 'Weight', 'StatErr', 'SystErr', 'binID']
59 
60 # Saving variables to ntuple
61 output_file = 'B2A905-ApplyWeightsToTracks.root'
62 ma.variablesToNtuple(decayString='pi+:gen',
63  variables=pivars,
64  treename='pion',
65  filename=output_file,
66  path=my_path)
67 
68 # Process the events
69 b2.process(my_path)
70 
71 # print out the summary
72 print(b2.statistics)