Belle II Software light-2609-luna
GeneratedDecayWeightModule Class Reference
Inheritance diagram for GeneratedDecayWeightModule:
Collaboration diagram for GeneratedDecayWeightModule:

Public Member Functions

 __init__ (self, bp_list, b0_list, gen_dmid_variable=DEFAULT_GEN_DMID_VARIABLE, weight_variable=DEFAULT_WEIGHT_VARIABLE, rest_dmid=REST_DMID, use_calibrated_dmids_only=True, store_btag_candidate_signature=False)
 
 initialize (self)
 
 event (self)
 

Public Attributes

 bp_list = bp_list
 B+ particle list name.
 
 b0_list = b0_list
 B0 particle list name.
 
 gen_dmid_variable = gen_dmid_variable
 ExtraInfo key name for the generated decay mode ID.
 
 weight_variable = weight_variable
 ExtraInfo key name for the calibration weight.
 
 rest_dmid = rest_dmid
 Decay mode ID used for the rest (non-calibrated) category.
 
 use_calibrated_dmids_only = use_calibrated_dmids_only
 Whether to restrict to calibrated decay mode IDs only.
 
 store_btag_candidate_signature = store_btag_candidate_signature
 Whether to store the full btag candidate signature as ExtraInfo.
 

Protected Member Functions

 _validate_configuration (self)
 
 _annotate_list (self, list_name, truth_cache)
 

Static Protected Member Functions

 _normalize_int (value)
 
 _set_extra_info (particle, key, value)
 
 _is_non_continuum_event (particle)
 
 _report_missing_truth_annotation (list_name, particle, reason)
 

Protected Attributes

bool _validated = False
 Internal flag: True after configuration has been validated.
 

Detailed Description

Assign generated-decay dmIDs and calibration weights to FEI candidates.

Definition at line 417 of file generatedDecayWeights.py.

Constructor & Destructor Documentation

◆ __init__()

__init__ ( self,
bp_list,
b0_list,
gen_dmid_variable = DEFAULT_GEN_DMID_VARIABLE,
weight_variable = DEFAULT_WEIGHT_VARIABLE,
rest_dmid = REST_DMID,
use_calibrated_dmids_only = True,
store_btag_candidate_signature = False )
Initialise module parameters.

Definition at line 422 of file generatedDecayWeights.py.

431 ):
432 """Initialise module parameters."""
433 super().__init__()
434
435 self.bp_list = bp_list
436
437 self.b0_list = b0_list
438
439 self.gen_dmid_variable = gen_dmid_variable
440
441 self.weight_variable = weight_variable
442
443 self.rest_dmid = rest_dmid
444
445 self.use_calibrated_dmids_only = use_calibrated_dmids_only
446
447 self.store_btag_candidate_signature = store_btag_candidate_signature
448
449 self._validated = False
450

Member Function Documentation

◆ _annotate_list()

_annotate_list ( self,
list_name,
truth_cache )
protected
Annotate all candidates in list_name using the pre-built truth_cache.

Definition at line 506 of file generatedDecayWeights.py.

506 def _annotate_list(self, list_name, truth_cache):
507 """Annotate all candidates in list_name using the pre-built truth_cache."""
508 plist = Belle2.PyStoreObj(list_name)
509 if not plist.isValid():
510 return
511
512 particle_list = plist.obj()
513 for i in range(plist.getListSize()):
514 particle = particle_list.getParticle(i)
515 btag_index = self._normalize_int(vm.evaluate('mostcommonBTagIndex', particle))
516 tag_pdg = self._normalize_int(vm.evaluate('mostcommonBTagPDG', particle))
517 is_non_continuum = self._is_non_continuum_event(particle)
518
519 gen_dm_id = INVALID_DMID
520 weight = config.FEI_CALIB_CONT
521 match = None
522 if btag_index is None:
523 if is_non_continuum:
524 self._report_missing_truth_annotation(list_name, particle, 'mostcommonBTagIndex is NaN')
525 elif tag_pdg is None:
526 if is_non_continuum:
527 self._report_missing_truth_annotation(list_name, particle, 'mostcommonBTagPDG is NaN')
528 elif abs(tag_pdg) not in (511, 521):
529 if is_non_continuum:
530 self._report_missing_truth_annotation(
531 list_name,
532 particle,
533 f'mostcommonBTagPDG={tag_pdg} is not a B0 or B+ PDG'
534 )
535 else:
536 match = truth_cache.get(btag_index)
537 if match is None:
538 if is_non_continuum:
539 self._report_missing_truth_annotation(
540 list_name,
541 particle,
542 f'mostcommonBTagIndex={btag_index} is not present in the event truth cache'
543 )
544 else:
545 gen_dm_id = match['gen_dm_id']
546 weight = match['weight']
547
548 self._set_extra_info(particle, self.gen_dmid_variable, float(gen_dm_id))
549 self._set_extra_info(particle, self.weight_variable, float(weight))
550 if self.store_btag_candidate_signature:
551 if match is None:
552 signature_values = (0,) * N_SIGNATURE_VALUES
553 else:
554 signature_values = match['signature']
555 for key, value in _signature_to_dict(signature_values).items():
556 self._set_extra_info(particle, key, float(value))
557
a (simplified) python wrapper for StoreObjPtr.
Definition PyStoreObj.h:67

◆ _is_non_continuum_event()

_is_non_continuum_event ( particle)
staticprotected
Return True if the event is not a continuum event.

Definition at line 493 of file generatedDecayWeights.py.

493 def _is_non_continuum_event(particle):
494 """Return True if the event is not a continuum event."""
495 is_cont = vm.evaluate('isContinuumEvent', particle)
496 return int(is_cont) != 1
497

◆ _normalize_int()

_normalize_int ( value)
staticprotected
Return int(value), or None if value is NaN.

Definition at line 480 of file generatedDecayWeights.py.

480 def _normalize_int(value):
481 """Return int(value), or None if value is NaN."""
482 return None if value != value else int(value)
483

◆ _report_missing_truth_annotation()

_report_missing_truth_annotation ( list_name,
particle,
reason )
staticprotected
Emit a B2ERROR for a non-continuum candidate missing its truth annotation.

Definition at line 499 of file generatedDecayWeights.py.

499 def _report_missing_truth_annotation(list_name, particle, reason):
500 """Emit a B2ERROR for a non-continuum candidate missing its truth annotation."""
501 b2.B2ERROR(
502 "ModeSelector generated weights: non-continuum candidate missing generated "
503 f"B truth annotation in {list_name} (array index {particle.getArrayIndex()}): {reason}"
504 )
505

◆ _set_extra_info()

_set_extra_info ( particle,
key,
value )
staticprotected
Set or add an ExtraInfo entry on a particle.

Definition at line 485 of file generatedDecayWeights.py.

485 def _set_extra_info(particle, key, value):
486 """Set or add an ExtraInfo entry on a particle."""
487 if particle.hasExtraInfo(key):
488 particle.setExtraInfo(key, value)
489 else:
490 particle.addExtraInfo(key, value)
491

◆ _validate_configuration()

_validate_configuration ( self)
protected
Raise a fatal error if any module parameter is invalid.

Definition at line 455 of file generatedDecayWeights.py.

455 def _validate_configuration(self):
456 """Raise a fatal error if any module parameter is invalid."""
457 if self._validated:
458 return
459 if not isinstance(self.bp_list, str) or not self.bp_list.startswith('B+:'):
460 b2.B2FATAL(f"ModeSelector generated weights: invalid bp_list '{self.bp_list}'")
461 if not isinstance(self.b0_list, str) or not self.b0_list.startswith('B0:'):
462 b2.B2FATAL(f"ModeSelector generated weights: invalid b0_list '{self.b0_list}'")
463 if not isinstance(self.gen_dmid_variable, str) or not self.gen_dmid_variable:
464 b2.B2FATAL("ModeSelector generated weights: gen_dmid_variable must be a non-empty string")
465 if not isinstance(self.weight_variable, str) or not self.weight_variable:
466 b2.B2FATAL("ModeSelector generated weights: weight_variable must be a non-empty string")
467 if int(self.rest_dmid) != REST_DMID:
468 b2.B2FATAL(f"ModeSelector generated weights: rest_dmid must be {REST_DMID}")
469 if not isinstance(self.use_calibrated_dmids_only, bool):
470 b2.B2FATAL(
471 "ModeSelector generated weights: use_calibrated_dmids_only must be bool"
472 )
473 if not isinstance(self.store_btag_candidate_signature, bool):
474 b2.B2FATAL(
475 "ModeSelector generated weights: store_btag_candidate_signature must be bool"
476 )
477 self._validated = True
478

◆ event()

event ( self)
Annotate all B candidates with generated decay mode IDs and calibration weights.

Definition at line 558 of file generatedDecayWeights.py.

558 def event(self):
559 """Annotate all B candidates with generated decay mode IDs and calibration weights."""
560 bp_plist = Belle2.PyStoreObj(self.bp_list)
561 b0_plist = Belle2.PyStoreObj(self.b0_list)
562 bp_empty = not bp_plist.isValid() or bp_plist.getListSize() == 0
563 b0_empty = not b0_plist.isValid() or b0_plist.getListSize() == 0
564 if bp_empty and b0_empty:
565 return
566
567 mc_particles = Belle2.PyStoreArray('MCParticles')
568 particles = [mc_particles[index] for index in range(mc_particles.getEntries())]
569 truth_cache = _build_event_truth_cache(
570 particles,
571 use_calibrated_dmids_only=self.use_calibrated_dmids_only,
572 )
573 self._annotate_list(self.bp_list, truth_cache)
574 self._annotate_list(self.b0_list, truth_cache)
575
576
A (simplified) python wrapper for StoreArray.

◆ initialize()

initialize ( self)
Validate configuration once at the start of processing.

Definition at line 451 of file generatedDecayWeights.py.

451 def initialize(self):
452 """Validate configuration once at the start of processing."""
453 self._validate_configuration()
454

Member Data Documentation

◆ _validated

bool _validated = False
protected

Internal flag: True after configuration has been validated.

Definition at line 449 of file generatedDecayWeights.py.

◆ b0_list

b0_list = b0_list

B0 particle list name.

Definition at line 437 of file generatedDecayWeights.py.

◆ bp_list

bp_list = bp_list

B+ particle list name.

Definition at line 435 of file generatedDecayWeights.py.

◆ gen_dmid_variable

gen_dmid_variable = gen_dmid_variable

ExtraInfo key name for the generated decay mode ID.

Definition at line 439 of file generatedDecayWeights.py.

◆ rest_dmid

rest_dmid = rest_dmid

Decay mode ID used for the rest (non-calibrated) category.

Definition at line 443 of file generatedDecayWeights.py.

◆ store_btag_candidate_signature

store_btag_candidate_signature = store_btag_candidate_signature

Whether to store the full btag candidate signature as ExtraInfo.

Definition at line 447 of file generatedDecayWeights.py.

◆ use_calibrated_dmids_only

use_calibrated_dmids_only = use_calibrated_dmids_only

Whether to restrict to calibrated decay mode IDs only.

Definition at line 445 of file generatedDecayWeights.py.

◆ weight_variable

weight_variable = weight_variable

ExtraInfo key name for the calibration weight.

Definition at line 441 of file generatedDecayWeights.py.


The documentation for this class was generated from the following file: