Belle II Software development
event_level.py
1# this script applies the event level classifier
2
3from flavorTagger.utils import get_available_categories
4import os
5
6
7def event_level(weightFiles='B2JpsiKs_mu', categories=None, files_dir="",
8 downloadFlag=False, useOnlyLocalFlag=False, signal_fraction=-2,
9 exp_type="Belle2", path=None):
10 """
11 Samples data for training or tests all categories all categories at event level.
12 """
13
14 # imports
15 import basf2
16 from basf2 import create_path, register_module
17 from basf2 import B2INFO, B2FATAL
18 import basf2_mva
19
20 # verbose
21 B2INFO('EVENT LEVEL')
22
23 # initialise configuration variables
24 ready_methods = 0
25 all_categories = get_available_categories()
26
27 # Each category has its own Path in order to be skipped if the
28 # corresponding particle list is empty
29 identifiers_extraInfo_dict = dict()
30 identifiers_extraInfo_KaonPion = []
31
32 # process each category
33 for category in categories:
34 particleList = all_categories[category].particleList
35
36 # initialise names
37 event_method_prefix = (f"FlavorTagger_{exp_type}_{weightFiles}"
38 f"EventLevel{category}FBDT")
39 event_identifier = event_method_prefix
40 target_variable = f"isRightCategory({category})"
41 extraInfo_name = target_variable
42
43 # setup flag specific configuration
44 if downloadFlag or useOnlyLocalFlag:
45 event_identifier = f"{files_dir}/{event_method_prefix}_1.root"
46
47 if downloadFlag:
48 if not os.path.isfile(event_identifier):
49 basf2_mva.download(event_method_prefix, event_identifier)
50 if not os.path.isfile(event_identifier):
51 B2FATAL(f"Flavor Tagger: Weight file {event_identifier}"
52 "was not downloaded from database. Please check the"
53 " build or revision name. Stopped.")
54
55 if useOnlyLocalFlag:
56 if not os.path.isfile(event_identifier):
57 B2FATAL(f"Flavor Tagger: {particleList} event level was not "
58 "trained. Weight file {event_identifier} was not "
59 "found. Stopped.")
60
61 # verbose
62 B2INFO(f"Flavor Tagger: MVAExpert {event_method_prefix} ready.")
63
64 # start application
65 B2INFO(f"Flavor Tagger: Applying MVAExpert {event_method_prefix}.")
66
67 if category == 'KaonPion':
68 identifiers_extraInfo_KaonPion.append((extraInfo_name, event_identifier))
69 elif particleList not in identifiers_extraInfo_dict:
70 identifiers_extraInfo_dict[particleList] = [(extraInfo_name, event_identifier)]
71 else:
72 identifiers_extraInfo_dict[particleList].append((extraInfo_name, event_identifier))
73
74 ready_methods += 1
75
76 # Each category has its own Path in order to be skipped if the corresponding particle list is empty
77 for particleList in identifiers_extraInfo_dict:
78 eventLevelPath = create_path()
79 SkipEmptyParticleList = register_module("SkimFilter")
80 SkipEmptyParticleList.set_name('SkimFilter_EventLevel_' + particleList)
81 SkipEmptyParticleList.param('particleLists', particleList)
82 SkipEmptyParticleList.if_true(eventLevelPath, basf2.AfterConditionPath.CONTINUE)
83 path.add_module(SkipEmptyParticleList)
84
85 mvaMultipleExperts = register_module('MVAMultipleExperts')
86 mvaMultipleExperts.set_name('MVAMultipleExperts_EventLevel_' + particleList)
87 mvaMultipleExperts.param('listNames', [particleList])
88 mvaMultipleExperts.param('extraInfoNames', [row[0] for row in identifiers_extraInfo_dict[particleList]])
89 mvaMultipleExperts.param('signalFraction', signal_fraction)
90 mvaMultipleExperts.param('identifiers', [row[1] for row in identifiers_extraInfo_dict[particleList]])
91 eventLevelPath.add_module(mvaMultipleExperts)
92
93 if 'KaonPion' in categories and len(identifiers_extraInfo_KaonPion) != 0:
94 eventLevelKaonPionPath = create_path()
95 SkipEmptyParticleList = register_module("SkimFilter")
96 SkipEmptyParticleList.set_name('SkimFilter_' + 'K+:inRoe')
97 SkipEmptyParticleList.param('particleLists', 'K+:inRoe')
98 SkipEmptyParticleList.if_true(eventLevelKaonPionPath, basf2.AfterConditionPath.CONTINUE)
99 path.add_module(SkipEmptyParticleList)
100
101 mvaExpertKaonPion = register_module("MVAExpert")
102 mvaExpertKaonPion.set_name('MVAExpert_KaonPion_' + 'K+:inRoe')
103 mvaExpertKaonPion.param('listNames', ['K+:inRoe'])
104 mvaExpertKaonPion.param('extraInfoName', identifiers_extraInfo_KaonPion[0][0])
105 mvaExpertKaonPion.param('signalFraction', signal_fraction)
106 mvaExpertKaonPion.param('identifier', identifiers_extraInfo_KaonPion[0][1])
107
108 eventLevelKaonPionPath.add_module(mvaExpertKaonPion)
109
110 if ready_methods != len(categories):
111 return False
112 else:
113 return True