Belle II Software  release-05-01-25
builtin_reweighting.py
1 #!/usr/bin/env python3
2 # -*- coding: utf-8 -*-
3 
4 # Thomas Keck 2016
5 
6 # The mva package has a builtin reweighting mechanism.
7 # If your data and mc do not match, you can use the MetaOptions to do a meta-training.
8 # Using 'm_use_reweighting = True' the mva package will train
9 # - the provided data files against the provided mc files, and calculate the probability p for each event in the mc files
10 # - the provided signal against background in the mc files weighted with the p / (1 - p)
11 # You can access the first boost training, it is saved with the postfix _boost.xml
12 
13 # In general you want to write out
14 # - train_mc.root containing the reconstructed candidates from MC (Y4S + Continuum)
15 # - train_data.root containing the reconstructed candidates from data
16 
17 # In this example we investigate the decay D -> K pi pi0 in MC and data (e.g. using b2bii)
18 # It turns out that mostly the continuum description is wrong, so we only correct for this
19 # - train_mc_continuum contains only candidates from charm and uds
20 # - train_data_continuum contains off resonance data with only continuum
21 # In the end we also only want to apply the reweighting to continuum events, so we set the
22 # m_reweighting_variable to 'isContinuumEvent'
23 
24 import basf2_mva
25 from basf2 import *
26 from modularAnalysis import *
27 
28 if __name__ == "__main__":
29  variables = ['p', 'pt', 'pz', 'phi',
30  'chiProb', 'dr', 'dz', 'dphi',
31  'daughter(0, dr)', 'daughter(1, dr)', 'daughter(0, dz)', 'daughter(1, dz)',
32  'daughter(0, dphi)', 'daughter(1, dphi)',
33  'daughter(0, chiProb)', 'daughter(1, chiProb)', 'daughter(2, chiProb)', 'daughter(2, M)',
34  'daughter(0, atcPIDBelle(3,2))', 'daughter(1, atcPIDBelle(3,2))',
35  'daughter(2, daughter(0, E))', 'daughter(2, daughter(1, E))',
36  'daughter(2, daughter(0, clusterLAT))', 'daughter(2, daughter(1, clusterLAT))',
37  'daughter(2, daughter(0, clusterHighestE))', 'daughter(2, daughter(1, clusterHighestE))',
38  'daughter(2, daughter(0, clusterNHits))', 'daughter(2, daughter(1, clusterNHits))',
39  'daughter(2, daughter(0, clusterE9E25))', 'daughter(2, daughter(1, clusterE9E25))',
40  'daughter(2, daughter(0, minC2HDist))', 'daughter(2, daughter(1, minC2HDist))',
41  'daughterInvariantMass(1, 2)', 'daughterInvariantMass(0, 1)', 'daughterInvariantMass(0, 2)'
42  'daughterAngle(0, 1)', 'daughterAngle(0, 2)', 'daughterAngle(1, 2)',
43  'daughter(0, p)', 'daughter(0, pz)', 'daughter(0, pt)', 'daughter(0, phi)',
44  'daughter(1, p)', 'daughter(1, pz)', 'daughter(1, pt)', 'daughter(1, phi)',
45  'daughter(2, p)', 'daughter(2, pz)', 'daughter(2, pt)', 'daughter(2, phi)',
46  ]
47 
48  general_options = basf2_mva.GeneralOptions()
49  general_options.m_datafiles = basf2_mva.vector("train_mc.root")
50  general_options.m_identifier = "MVAReweighted"
51  general_options.m_treename = "tree"
52  general_options.m_variables = basf2_mva.vector(*variables)
53  general_options.m_target_variable = "isSignal"
54 
55  fastbdt_options = basf2_mva.FastBDTOptions()
56 
57  meta_options = basf2_mva.MetaOptions()
58  meta_options.m_use_reweighting = True
59  meta_options.m_reweighting_variable = 'isContinuumEvent'
60  meta_options.m_reweighting_identifier = "Reweighter"
61  meta_options.m_reweighting_mc_files = basf2_mva.vector("train_mc_continuum.root")
62  meta_options.m_reweighting_data_files = basf2_mva.vector("train_data_continuum.root")
63 
64  basf2_mva.teacher(general_options, fastbdt_options, meta_options)