![]() |
Belle II Software light-2609-luna
|


Public Member Functions | |
| __init__ (self, particle_lists, payload_cat_model=None, payload_main_model=None, output_variable='BplusScore', cat_model_path=None, main_model_path=None, training_mode=False, skip_nn_evaluation=False, store_fei_calib_weight=False, debug=False, debug_max_events=10) | |
| initialize (self) | |
| beginRun (self) | |
| terminate (self) | |
| event (self) | |
Public Attributes | |
| particle_lists = particle_lists if isinstance(particle_lists, list) else [particle_lists] | |
| Input particle lists. | |
| cat_model_path = cat_model_path | |
| Path to category model. | |
| main_model_path = main_model_path | |
| Path to main model. | |
| output_variable = output_variable | |
| Output variable name. | |
| payload_cat_model_given = payload_cat_model is not None | |
| True if the category payload name was given explicitly instead of derived. | |
| payload_main_model_given = payload_main_model is not None | |
| True if the main payload name was given explicitly instead of derived. | |
| payload_cat_model = payload_cat_model if payload_cat_model is not None else config.DEFAULT_CAT_PAYLOAD | |
| Payload name for category model. | |
| payload_main_model = payload_main_model if payload_main_model is not None else config.DEFAULT_MAIN_PAYLOAD | |
| Payload name for main model. | |
| contract_version = config.MODEL_CONTRACT_VERSION | |
| Contract version the loaded models were built for. | |
| training_mode = training_mode | |
| Training mode (save features + MC truth, skip NN inference) | |
| skip_nn_evaluation = skip_nn_evaluation | |
| Skip NN inference and fill deterministic placeholder outputs. | |
| store_fei_calib_weight = store_fei_calib_weight | |
| Store modeSelector_feiCalibWeight in EventExtraInfo (MC only) | |
| debug = debug | |
| Debug mode. | |
| debug_max_events = debug_max_events | |
| Max events to debug. | |
| int | event_count = 0 |
| Event counter for debug. | |
| n_input_ids = config.N_INPUT_IDS | |
| Number of input_id slots (B+ sector + B0 sector, each split by particle/antiparticle) | |
| event_features = config.EVENT_FEATURES | |
| Event-level feature names. | |
| has_inputs = None | |
| Indices of non-zero features kept after sparsity filtering (None in training mode) | |
| cat_input_size = len(self.has_inputs) | |
| Category network input size (number of selected features) | |
| int | main_input_size = self.cat_input_size + 4 |
| Main network input size (cat features + cat output + charged flag) | |
| cat_expert = self._onnx_interface.getExpert() | |
| Category network expert. | |
| main_expert = self._onnx_interface.getExpert() | |
| Main network expert. | |
| cat_dataset = Belle2.MVA.SingleDataset(cat_opts, [0.0] * self.cat_input_size, 1.0) | |
| SingleDataset for category network inference. | |
| main_dataset = Belle2.MVA.SingleDataset(main_opts, [0.0] * self.main_input_size, 1.0) | |
| SingleDataset for main network inference. | |
| list | feature_blocks = [(name, transform) for name, _, transform in config.FEATURE_BLOCKS] |
| Feature block names and transformations (from config) | |
| deltaM_cut = config.DELTA_M_CUT | |
| Cut range for D* delta mass difference. | |
Static Public Attributes | |
| str | AUXILIARY_OUTPUT_PREFIX = 'modeSelector' |
| Prefix used for auxiliary ExtraInfo output variables. | |
Protected Member Functions | |
| _load_weightfile (self, path, label) | |
| _feature_selection_from_weightfile (self, variables, label) | |
| _check_contract_is_self_consistent (self) | |
| _check_contract (self, weightfile, identifier, label) | |
| _require_supported_contract (self, version, label) | |
| _select_contract_version (self, cat_version, main_version) | |
| _check_same_training (self, cat_training, main_training) | |
| _check_output_classes (self, options, expected, label) | |
| _warn_if_newer_contract_available (self) | |
| _load_payload (self, accessor, payload_name, label) | |
| _setup_models (self, cat_wf, main_wf) | |
| _build_feature_indices (self) | |
| _get_input_id (self, particle) | |
| _extract_particle_features (self, particle) | |
| _build_feature_array (self, candidates_data, event_features) | |
| _get_inference_experiment_feature_value (self) | |
| _get_event_features (self) | |
| _compute_fei_calib_weight (self, best_bp, best_b0) | |
| _get_placeholder_outputs (self, features) | |
| _extract_mc_truth_scalars (self, particle) | |
| _compute_training_event_scalars (self, bp_truth, b0_truth, best_bp_iid, best_bp_dp, best_b0_iid, best_b0_dp) | |
| _print_debug_info (self, candidates_data, event_features, all_features, max_input_id) | |
Static Protected Member Functions | |
| _assign_rank_extra_info (particle_score_input_id_triples, variable_name) | |
Protected Attributes | |
| bool | _gen_calib_weight_missing_warned = False |
| Flag to emit the gen-calib-weight missing warning at most once. | |
| bool | _event_shape_checked = False |
| Flag to check event shape prerequisite once. | |
| bool | _dstar_veto_checked = False |
| Flag to check D* veto prerequisite once. | |
| int | _empty_predicted_sector_count = 0 |
| Number of events where predicted sector had no candidate in the event. | |
| int | _inference_event_count = 0 |
| Number of inference events processed. | |
| int | _high_conf_event_count = 0 |
| Number of high-confidence inference events (abs(BplusScore) > threshold) | |
| int | _missing_top_mode_count = 0 |
| Number of events where sector top-output input_id has no candidate in the event. | |
| int | _missing_top_mode_high_conf_count = 0 |
| Number of high-confidence events where sector top-output input_id has no candidate. | |
| int | _empty_predicted_sector_high_conf_count = 0 |
| Number of high-confidence events where fallback was used. | |
| int | _presel_total_candidates = 0 |
| Number of candidates checked for preselection compliance. | |
| int | _presel_violation_count = 0 |
| Number of candidates failing at least one preselection cut. | |
| int | _unsupported_experiment_event_count = 0 |
| Number of inference events where unsupported experiment ids were replaced. | |
| dict | _unsupported_experiment_counts = {} |
| Counts of unsupported raw experiment ids seen during inference. | |
| _bp_calib_rest = config.get_fei_calibration_rest(521) | |
| FEI calibration fallback factor for the B+ sector. | |
| _bp_calib_lookup = np.full(config.N_BP_MODES, self._bp_calib_rest, dtype=np.float32) | |
| Per-decay-mode FEI calibration lookup for the B+ sector. | |
| _b0_calib_rest = config.get_fei_calibration_rest(511) | |
| FEI calibration fallback factor for the B0 sector. | |
| _b0_calib_lookup = np.full(config.N_B0_MODES, self._b0_calib_rest, dtype=np.float32) | |
| Per-decay-mode FEI calibration lookup for the B0 sector. | |
| _onnx_interface = Belle2.MVA.AbstractInterface.getSupportedInterfaces()["ONNX"] | |
| ONNX MVA interface used to create the experts. | |
| _cat_local_wf = None | |
| Category weightfile loaded from a local file (None if taken from the database) | |
| _main_local_wf = None | |
| Main weightfile loaded from a local file (None if taken from the database) | |
| _cat_accessor = None | |
| Database accessor for the category payload (None if a local file is used) | |
| _main_accessor = None | |
| Database accessor for the main payload (None if a local file is used) | |
| _loaded_checksums = None | |
| Checksums of the currently loaded payloads, used to detect a change between runs. | |
Event-level B meson classifier using neural networks.
This module processes FEI B meson candidates and computes a score using
information from all candidates in the event.
Args:
particle_lists (list): List of B meson particle list names
cat_model_path (str): Path to the basf2 MVA weightfile for the category network,
as produced by convert_to_onnx.py. Pass None to load from the conditions
database via payload_cat_model.
main_model_path (str): Path to the basf2 MVA weightfile for the main network,
as produced by convert_to_onnx.py. Pass None to load from the conditions
database via payload_main_model.
payload_cat_model (str): Conditions DB payload name for the category model. None
(default) uses the name derived from the contract version this release
implements, which is the normal case.
payload_main_model (str): Same for the main model.
output_variable (str): Name of ExtraInfo variable for output score
training_mode (bool): Expose features and MC truth for training instead of running
the networks.
skip_nn_evaluation (bool): Fill placeholder outputs instead of running the networks
(debugging and timing only).
store_fei_calib_weight (bool): Store modeSelector_feiCalibWeight (MC only).
debug (bool): Print the feature vector and network outputs for the first events.
debug_max_events (int): Number of events printed when debug is True.
See modeSelector.modeSelector() for the full description of each parameter.
Definition at line 30 of file ModeSelectorModule.py.
| __init__ | ( | self, | |
| particle_lists, | |||
| payload_cat_model = None, | |||
| payload_main_model = None, | |||
| output_variable = 'BplusScore', | |||
| cat_model_path = None, | |||
| main_model_path = None, | |||
| training_mode = False, | |||
| skip_nn_evaluation = False, | |||
| store_fei_calib_weight = False, | |||
| debug = False, | |||
| debug_max_events = 10 ) |
Initialise module parameters and training data buffers.
Definition at line 63 of file ModeSelectorModule.py.
|
staticprotected |
Assign deterministic descending ranks to the given particles.
Definition at line 549 of file ModeSelectorModule.py.
|
protected |
Build the feature array for neural network input.
Parameters:
candidates_data: List of (input_id, features) tuples
event_features: Dict of event-level features
Returns:
numpy.ndarray: Feature array ready for NN input
Definition at line 601 of file ModeSelectorModule.py.
|
protected |
Build the feature index mapping from config. The training used sparse matrices with specific non-zero columns. We need to match that structure.
Definition at line 530 of file ModeSelectorModule.py.
|
protected |
Check a model's contract version and return it together with the training name. The contract version is an extra element of the weightfile; the identifier holds the training name. The contract version must be one this release supports: the current one, or an older one whose code path the release still provides. A newer version, or an older one that is no longer supported, would run without error and give wrong results, so it is fatal. Weightfiles written before the contract version was recorded are treated as contract version 1 and are subject to the same support rule.
Definition at line 187 of file ModeSelectorModule.py.
|
protected |
Check that SUPPORTED_CONTRACT_VERSIONS is consistent with the current contract version. A mismatch is a developer error in the release, not a problem with the payload. The same rule is applied by convert_to_onnx.py before exporting.
Definition at line 177 of file ModeSelectorModule.py.
|
protected |
Check the model's output size against what the module's interpretation assumes. The outputs are read positionally -- for the main network the class index is the input_id and the last three entries are the background classes -- so a model with a different number of outputs would be misread rather than rejected.
Definition at line 282 of file ModeSelectorModule.py.
|
protected |
Check that the category and main models come from the same training. The main network takes the category network outputs as inputs, so a pair from different trainings runs without error but gives wrong results. Such a pair shares the contract version, so only the training name in the weightfile identifier, which convert_to_onnx.py writes identically into both weightfiles, can tell them apart.
Definition at line 265 of file ModeSelectorModule.py.
|
protected |
Compute event-level FEI calibration weight from the best candidates. Based on the reconstructed candidate, checking for truth-compatible tag PDG and DeltaP < DELTA_P_THRESH. Returns FEI_CALIB_CONT for continuum events. Returns NaN when reco conditions are not met (non-continuum events where tag PDG does not match or DeltaP is above threshold).
Definition at line 729 of file ModeSelectorModule.py.
|
protected |
Compute the per-event training scalar fields written to EventExtraInfo. best_bp_iid/best_bp_dp/best_b0_iid/best_b0_dp come from the truth-tag-matched particle_by_input_id scan already performed in event() and are only stored for the labels; everything else is derived from the best-B+/best-B0 MC truth scalars for this event. In particular, the FEI calibration weight is defined for the highest-sigProb candidate, so its DeltaP requirement uses that candidate's own DeltaP, as at inference in _compute_fei_calib_weight().
Definition at line 823 of file ModeSelectorModule.py.
|
protected |
Extract compact MC truth scalars used by training output.
Definition at line 794 of file ModeSelectorModule.py.
|
protected |
Extract features from a single particle.
Returns:
tuple: (input_id, feature_dict)
Definition at line 576 of file ModeSelectorModule.py.
|
protected |
Recover the raw feature indices a model was trained on from its weightfile. Returns None for legacy weightfiles that only carry placeholder variable names, in which case the caller falls back to config.HAS_INPUTS.
Definition at line 156 of file ModeSelectorModule.py.
|
protected |
Extract event-level features from EventShapeContainer.
Definition at line 692 of file ModeSelectorModule.py.
|
protected |
Return the raw experiment id.
Definition at line 678 of file ModeSelectorModule.py.
|
protected |
Compute input_id from decay mode ID, B type, and particle/antiparticle sign.
Encoding:
B+ sector (input_ids 0 to 2*N_BP_MODES-1):
anti-B+ (PDG=-521): dmID * 2 + 0
B+ (PDG=+521): dmID * 2 + 1
B0 sector (input_ids 2*N_BP_MODES to N_INPUT_IDS-1):
anti-B0 (PDG=-511): N_BP_MODES*2 + dmID * 2 + 0
B0 (PDG=+511): N_BP_MODES*2 + dmID * 2 + 1
Definition at line 558 of file ModeSelectorModule.py.
|
protected |
Return deterministic placeholder category and main-network outputs.
Definition at line 785 of file ModeSelectorModule.py.
|
protected |
Load a basf2 MVA weightfile from the conditions database payload of the current run.
Definition at line 453 of file ModeSelectorModule.py.
|
protected |
Load a basf2 MVA weightfile, raises error for unsupported extensions.
Definition at line 146 of file ModeSelectorModule.py.
|
protected |
Print debug information for preprocessing.
Definition at line 929 of file ModeSelectorModule.py.
|
protected |
Stop unless this release can run models built for the given contract version.
Definition at line 224 of file ModeSelectorModule.py.
|
protected |
Return the contract version to run with, and warn when it is older than the current one. Both networks are evaluated by one code path, so they must share a contract version.
Definition at line 242 of file ModeSelectorModule.py.
|
protected |
Create the experts for a pair of weightfiles and validate them. Called once for local weightfiles, and for each run in which a payload changes. Everything derived from the weightfiles (contract version, feature selection, input sizes) is set here, so a new pair of models can differ in all of them.
Definition at line 466 of file ModeSelectorModule.py.
|
protected |
Warn when the configured globaltags also serve payloads for a newer contract version. Payload names are derived from the contract version, so a single performance globaltag can hold models for several releases at once. Finding a newer one means a newer release would use a different, possibly improved model. Looking up a payload that does not exist only queries the metadata; the file of a newer payload is fetched only if it is actually present.
Definition at line 297 of file ModeSelectorModule.py.
| beginRun | ( | self | ) |
Load the models from the conditions database if the payloads changed.
Definition at line 421 of file ModeSelectorModule.py.
| event | ( | self | ) |
Called for each event.
Definition at line 1043 of file ModeSelectorModule.py.
| initialize | ( | self | ) |
Called at the beginning of processing.
Definition at line 325 of file ModeSelectorModule.py.
| terminate | ( | self | ) |
Called at the end of processing.
Definition at line 980 of file ModeSelectorModule.py.
|
protected |
Per-decay-mode FEI calibration lookup for the B0 sector.
Definition at line 350 of file ModeSelectorModule.py.
|
protected |
FEI calibration fallback factor for the B0 sector.
Definition at line 348 of file ModeSelectorModule.py.
|
protected |
Per-decay-mode FEI calibration lookup for the B+ sector.
Definition at line 341 of file ModeSelectorModule.py.
|
protected |
FEI calibration fallback factor for the B+ sector.
Definition at line 339 of file ModeSelectorModule.py.
|
protected |
Database accessor for the category payload (None if a local file is used)
Definition at line 399 of file ModeSelectorModule.py.
|
protected |
Category weightfile loaded from a local file (None if taken from the database)
Definition at line 395 of file ModeSelectorModule.py.
|
protected |
Flag to check D* veto prerequisite once.
Definition at line 118 of file ModeSelectorModule.py.
|
protected |
Number of events where predicted sector had no candidate in the event.
Definition at line 120 of file ModeSelectorModule.py.
|
protected |
Number of high-confidence events where fallback was used.
Definition at line 130 of file ModeSelectorModule.py.
|
protected |
Flag to check event shape prerequisite once.
Definition at line 116 of file ModeSelectorModule.py.
|
protected |
Flag to emit the gen-calib-weight missing warning at most once.
Definition at line 108 of file ModeSelectorModule.py.
|
protected |
Number of high-confidence inference events (abs(BplusScore) > threshold)
Definition at line 124 of file ModeSelectorModule.py.
|
protected |
Number of inference events processed.
Definition at line 122 of file ModeSelectorModule.py.
|
protected |
Checksums of the currently loaded payloads, used to detect a change between runs.
Definition at line 415 of file ModeSelectorModule.py.
|
protected |
Database accessor for the main payload (None if a local file is used)
Definition at line 401 of file ModeSelectorModule.py.
|
protected |
Main weightfile loaded from a local file (None if taken from the database)
Definition at line 397 of file ModeSelectorModule.py.
|
protected |
Number of events where sector top-output input_id has no candidate in the event.
Definition at line 126 of file ModeSelectorModule.py.
|
protected |
Number of high-confidence events where sector top-output input_id has no candidate.
Definition at line 128 of file ModeSelectorModule.py.
|
protected |
ONNX MVA interface used to create the experts.
Definition at line 390 of file ModeSelectorModule.py.
|
protected |
Number of candidates checked for preselection compliance.
Definition at line 132 of file ModeSelectorModule.py.
|
protected |
Number of candidates failing at least one preselection cut.
Definition at line 134 of file ModeSelectorModule.py.
|
protected |
Counts of unsupported raw experiment ids seen during inference.
Definition at line 138 of file ModeSelectorModule.py.
|
protected |
Number of inference events where unsupported experiment ids were replaced.
Definition at line 136 of file ModeSelectorModule.py.
|
static |
Prefix used for auxiliary ExtraInfo output variables.
Definition at line 61 of file ModeSelectorModule.py.
| cat_dataset = Belle2.MVA.SingleDataset(cat_opts, [0.0] * self.cat_input_size, 1.0) |
SingleDataset for category network inference.
Definition at line 521 of file ModeSelectorModule.py.
| cat_expert = self._onnx_interface.getExpert() |
Category network expert.
Definition at line 476 of file ModeSelectorModule.py.
| cat_input_size = len(self.has_inputs) |
Category network input size (number of selected features)
Definition at line 359 of file ModeSelectorModule.py.
| cat_model_path = cat_model_path |
Path to category model.
Definition at line 82 of file ModeSelectorModule.py.
| contract_version = config.MODEL_CONTRACT_VERSION |
Contract version the loaded models were built for.
Set from the weightfiles when the models are loaded. Everything that differs between supported contract versions (feature construction, expected output classes, output interpretation) branches on this, so a model for an older supported contract runs with that contract's behaviour.
Definition at line 100 of file ModeSelectorModule.py.
| debug = debug |
Debug mode.
Definition at line 110 of file ModeSelectorModule.py.
| debug_max_events = debug_max_events |
Max events to debug.
Definition at line 112 of file ModeSelectorModule.py.
| deltaM_cut = config.DELTA_M_CUT |
Cut range for D* delta mass difference.
Definition at line 546 of file ModeSelectorModule.py.
| int event_count = 0 |
Event counter for debug.
Definition at line 114 of file ModeSelectorModule.py.
| event_features = config.EVENT_FEATURES |
Event-level feature names.
Definition at line 144 of file ModeSelectorModule.py.
| feature_blocks = [(name, transform) for name, _, transform in config.FEATURE_BLOCKS] |
Feature block names and transformations (from config)
Definition at line 538 of file ModeSelectorModule.py.
| has_inputs = None |
Indices of non-zero features kept after sparsity filtering (None in training mode)
Definition at line 335 of file ModeSelectorModule.py.
| main_dataset = Belle2.MVA.SingleDataset(main_opts, [0.0] * self.main_input_size, 1.0) |
SingleDataset for main network inference.
Definition at line 523 of file ModeSelectorModule.py.
| main_expert = self._onnx_interface.getExpert() |
Main network expert.
Definition at line 479 of file ModeSelectorModule.py.
| int main_input_size = self.cat_input_size + 4 |
Main network input size (cat features + cat output + charged flag)
Definition at line 361 of file ModeSelectorModule.py.
| main_model_path = main_model_path |
Path to main model.
Definition at line 84 of file ModeSelectorModule.py.
| n_input_ids = config.N_INPUT_IDS |
Number of input_id slots (B+ sector + B0 sector, each split by particle/antiparticle)
Definition at line 142 of file ModeSelectorModule.py.
| output_variable = output_variable |
Output variable name.
Definition at line 86 of file ModeSelectorModule.py.
| particle_lists = particle_lists if isinstance(particle_lists, list) else [particle_lists] |
Input particle lists.
Definition at line 80 of file ModeSelectorModule.py.
| payload_cat_model = payload_cat_model if payload_cat_model is not None else config.DEFAULT_CAT_PAYLOAD |
Payload name for category model.
Definition at line 93 of file ModeSelectorModule.py.
| payload_cat_model_given = payload_cat_model is not None |
True if the category payload name was given explicitly instead of derived.
Definition at line 89 of file ModeSelectorModule.py.
| payload_main_model = payload_main_model if payload_main_model is not None else config.DEFAULT_MAIN_PAYLOAD |
Payload name for main model.
Definition at line 95 of file ModeSelectorModule.py.
| payload_main_model_given = payload_main_model is not None |
True if the main payload name was given explicitly instead of derived.
Definition at line 91 of file ModeSelectorModule.py.
| skip_nn_evaluation = skip_nn_evaluation |
Skip NN inference and fill deterministic placeholder outputs.
Definition at line 104 of file ModeSelectorModule.py.
| store_fei_calib_weight = store_fei_calib_weight |
Store modeSelector_feiCalibWeight in EventExtraInfo (MC only)
Definition at line 106 of file ModeSelectorModule.py.
| training_mode = training_mode |
Training mode (save features + MC truth, skip NN inference)
Definition at line 102 of file ModeSelectorModule.py.