Belle II Software development
combiner_level.py
1# this script applies the combiner level classifier
2
3from flavorTagger.utils import get_Belle_or_Belle2
4import os
5
6
7def combiner_level(weightFiles='B2JpsiKs_mu', categories=None,
8 variablesCombinerLevel=None, categoriesCombinationCode=None,
9 TMVAfbdt=False, FANNmlp=False, downloadFlag=False,
10 useOnlyLocalFlag=False, signal_fraction=-2,
11 filesDirectory="./FlavorTagging/TrainedMethods", path=None):
12 """
13 Samples the input data or tests the combiner according to the selected categories.
14 """
15
16 # imports
17 import basf2
18 from basf2 import B2INFO, B2FATAL
19 import basf2_mva
20
21 # verbose
22 B2INFO('COMBINER LEVEL')
23
24 B2INFO("Flavor Tagger: Required Combiner for Categories:")
25 for category in categories:
26 B2INFO(category)
27
28 B2INFO(f"Flavor Tagger: which corresponds to a weight file with categories\
29 combination code {categoriesCombinationCode}")
30
31 # initialise configuration variables
32 if variablesCombinerLevel is None:
33 variablesCombinerLevel = []
34 exp_type = get_Belle_or_Belle2()
35
36 combiner_method_prefix = f"FlavorTagger_{exp_type}_{weightFiles}Combiner{categoriesCombinationCode}"
37
38 # Check if weight files are ready
39 if TMVAfbdt:
40 identifierFBDT = f"{combiner_method_prefix}FBDT"
41 if downloadFlag or useOnlyLocalFlag:
42 identifierFBDT = f"{filesDirectory}/{combiner_method_prefix}FBDT_1.root"
43
44 if downloadFlag:
45 if not os.path.isfile(identifierFBDT):
46 basf2_mva.download(f"{combiner_method_prefix}FBDT", identifierFBDT)
47 if not os.path.isfile(identifierFBDT):
48 B2FATAL(f"Flavor Tagger: Weight file {identifierFBDT} was\
49 not downloaded from Database. Please check the\
50 buildOrRevision name. Stopped")
51
52 if useOnlyLocalFlag:
53 if not os.path.isfile(identifierFBDT):
54 B2FATAL(f"flavorTagger: Combinerlevel FastBDT was not trained\
55 with this combination of categories. Weight file\
56 {identifierFBDT} not found. Stopped")
57
58 B2INFO(f"flavorTagger: Ready to be used with weightFile\
59 {combiner_method_prefix}FBDT_1.root")
60
61 if FANNmlp:
62 identifierFANN = f"{combiner_method_prefix}FANN"
63 if downloadFlag or useOnlyLocalFlag:
64 identifierFANN = f"{filesDirectory}/{combiner_method_prefix}FANN_1.root"
65
66 if downloadFlag:
67 if not os.path.isfile(identifierFANN):
68 basf2_mva.download(f"{combiner_method_prefix}FANN", identifierFANN)
69 if not os.path.isfile(identifierFANN):
70 B2FATAL(f"Flavor Tagger: Weight file {identifierFANN} was\
71 not downloaded from Database. Please check the\
72 build or revision name name. Stopped")
73 if useOnlyLocalFlag:
74 if not os.path.isfile(identifierFANN):
75 B2FATAL(f"flavorTagger: Combinerlevel FANNMLP was not trained\
76 with this combination of categories. Weight file\
77 {identifierFANN} not found. Stopped")
78
79 B2INFO(f"flavorTagger: Ready to be used with weightFile {combiner_method_prefix}FANN_1.root")
80
81 # At this stage, all necessary weight files should be ready.
82 # Call MVAExpert or MVAMultipleExperts module.
83 if TMVAfbdt and not FANNmlp:
84 B2INFO(f"flavorTagger: Apply FBDTMethod {combiner_method_prefix}FBDT")
85 path.add_module(
86 'MVAExpert',
87 listNames=[],
88 extraInfoName='qrCombinedFBDT',
89 signalFraction=signal_fraction,
90 identifier=identifierFBDT
91 )
92
93 if FANNmlp and not TMVAfbdt:
94 B2INFO('flavorTagger: Apply FANNMethod on combiner level')
95 path.add_module(
96 'MVAExpert',
97 listNames=[],
98 extraInfoName='qrCombinedFANN',
99 signalFraction=signal_fraction,
100 entifier=identifierFANN
101 )
102
103 if FANNmlp and TMVAfbdt:
104 B2INFO('flavorTagger: Apply FANNMethod and FBDTMethod on combiner level')
105 mvaMultipleExperts = basf2.register_module('MVAMultipleExperts')
106 mvaMultipleExperts.set_name('MVAMultipleExperts_Combiners')
107 mvaMultipleExperts.param('listNames', [])
108 mvaMultipleExperts.param('extraInfoNames', ['qrCombinedFBDT', 'qrCombinedFANN'])
109 mvaMultipleExperts.param('signalFraction', signal_fraction)
110 mvaMultipleExperts.param('identifiers', [identifierFBDT, identifierFANN])
111 path.add_module(mvaMultipleExperts)