358 def makeAlgs (self, config) :
360 log = logging.getLogger(
'ElectronWorkingPointConfig')
362 if self.forceFullSimConfig:
363 log.warning(
"You are running ElectronWorkingPointConfig forcing full sim config")
364 log.warning(
"This is only intended to be used for testing purposes")
366 selectionPostfix = self.selectionName
367 if selectionPostfix !=
'' and selectionPostfix[0] !=
'_' :
368 selectionPostfix =
'_' + selectionPostfix
371 if config.geometry()
is LHCPeriod.Run1:
372 raise ValueError (
"Can't set up the ElectronWorkingPointConfig with %s, there must be something wrong!" % config.geometry().value)
374 postfix = self.postfix
376 postfix = self.selectionName
377 if postfix !=
'' and postfix[0] !=
'_' :
378 postfix =
'_' + postfix
381 if self.trackSelection :
382 alg = config.createAlgorithm(
'CP::AsgLeptonTrackSelectionAlg',
383 'ElectronTrackSelectionAlg',
385 alg.selectionDecoration =
'trackSelection' + postfix +
',as_bits'
386 alg.maxD0Significance = self.maxD0Significance
387 alg.maxDeltaZ0SinTheta = self.maxDeltaZ0SinTheta
388 alg.particles = config.readName (self.containerName)
389 alg.preselection = config.getPreselection (self.containerName,
'')
390 if self.trackSelection :
391 config.addSelection (self.containerName, self.selectionName, alg.selectionDecoration,
392 preselection=self.addSelectionToPreselection)
394 if 'LH' in self.identificationWP:
397 alg = config.createAlgorithm(
'CP::AsgSelectionAlg',
'ElectronLikelihoodAlg' )
398 alg.selectionDecoration =
'selectLikelihood' + selectionPostfix +
',as_char'
401 config.addPrivateTool(
'selectionTool',
'AsgElectronLikelihoodTool' )
402 alg.selectionTool.primaryVertexContainer =
'PrimaryVertices'
405 if config.geometry() >= LHCPeriod.Run3:
406 if 'HI' not in self.identificationWP:
407 alg.selectionTool.WorkingPoint = self.identificationWP.
replace(
"BLayer",
"BL") +
'Electron'
409 alg.selectionTool.WorkingPoint = self.identificationWP.
replace(
'_HI',
'Electron_HI')
410 elif config.geometry()
is LHCPeriod.Run2:
411 alg.selectionTool.WorkingPoint = self.identificationWP.
replace(
"BLayer",
"BL") +
'Electron_Run2'
414 config.addPrivateTool(
'selectionTool',
'CP::AsgFlagSelectionTool' )
415 dfFlag =
"DFCommonElectronsLH" + self.identificationWP.
split(
'LH')[0]
416 dfFlag = dfFlag.replace(
"BLayer",
"BL")
417 alg.selectionTool.selectionFlags = [dfFlag]
418 elif 'SiHit' in self.identificationWP:
420 algVeto = config.createAlgorithm(
'CP::AsgSelectionAlg',
'ElectronLikelihoodAlgVeto')
421 algVeto.selectionDecoration =
'selectLikelihoodVeto' + postfix +
',as_char'
422 config.addPrivateTool(
'selectionTool',
'CP::AsgFlagSelectionTool' )
423 algVeto.selectionTool.selectionFlags = [
"DFCommonElectronsLHLoose"]
424 algVeto.selectionTool.invertFlags = [
True]
425 algVeto.particles = config.readName (self.containerName)
426 algVeto.preselection = config.getPreselection (self.containerName, self.selectionName)
428 config.addSelection (self.containerName, self.selectionName, algVeto.selectionDecoration,
429 preselection=self.addSelectionToPreselection)
432 alg = config.createAlgorithm(
'CP::AsgSelectionAlg',
'ElectronLikelihoodAlg' )
433 alg.selectionDecoration =
'selectSiHit' + selectionPostfix +
',as_char'
435 config.addPrivateTool(
'selectionTool',
'CP::AsgMaskSelectionTool' )
436 dfVar =
"DFCommonElectronsLHLooseBLIsEMValue"
437 alg.selectionTool.selectionVars = [dfVar]
438 mask =
int( 0 | 0x1 << 1 | 0x1 << 2)
439 alg.selectionTool.selectionMasks = [mask]
440 elif 'DNN' in self.identificationWP:
441 if self.chargeIDSelectionRun2:
442 raise ValueError(
'DNN is not intended to be used with '
443 '`chargeIDSelectionRun2` option as there are '
444 'DNN WPs containing charge flip rejection.')
446 alg = config.createAlgorithm(
'CP::AsgSelectionAlg',
'ElectronDNNAlg' )
447 alg.selectionDecoration =
'selectDNN' + selectionPostfix +
',as_char'
450 config.addPrivateTool(
'selectionTool',
'AsgElectronSelectorTool' )
452 if config.geometry()
is LHCPeriod.Run3:
453 raise ValueError (
"DNN working points are not available for Run 3 yet.")
455 alg.selectionTool.WorkingPoint = self.identificationWP +
'Electron'
458 config.addPrivateTool(
'selectionTool',
'CP::AsgFlagSelectionTool' )
459 dfFlag =
"DFCommonElectronsDNN" + self.identificationWP.
split(
'DNN')[0]
460 alg.selectionTool.selectionFlags = [dfFlag]
462 alg.particles = config.readName (self.containerName)
463 alg.preselection = config.getPreselection (self.containerName, self.selectionName)
464 config.addSelection (self.containerName, self.selectionName, alg.selectionDecoration,
465 preselection=self.addSelectionToPreselection)
468 if 'SiHit' in self.identificationWP:
470 algDec = config.createAlgorithm(
'CP::ElectronSiHitDecAlg',
'ElectronSiHitDecAlg' )
471 selDec =
'siHitEvtHasLeptonPair' + selectionPostfix +
',as_char'
472 algDec.selectionName = selDec.split(
",")[0]
473 algDec.ElectronContainer = config.readName (self.containerName)
475 algDec.RequireTwoLeptons =
True
476 config.addSelection (self.containerName, self.selectionName, selDec,
477 preselection=self.addSelectionToPreselection)
480 if self.convSelection
is not None:
482 if self.identificationWP !=
'TightLH':
483 raise ValueError(f
"convSelection can only be used with TightLH ID, "
484 f
"whereas {self.identificationWP} has been selected. convSelection option will be ignored.")
486 allowedValues = [
"Veto",
"GammaStar",
"MatConv"]
487 if self.convSelection
not in allowedValues:
488 raise ValueError(f
"convSelection has been set to {self.convSelection}, which is not a valid option. "
489 f
"convSelection option must be one of {allowedValues}.")
492 alg = config.createAlgorithm(
'CP::AsgSelectionAlg',
'ElectronAmbiguityTypeAlg' )
493 alg.selectionDecoration =
'selectAmbiguityType' + selectionPostfix +
',as_char'
494 config.addPrivateTool(
'selectionTool',
'CP::AsgNumDecorationSelectionToolUInt8' )
495 alg.selectionTool.decorationName =
"ambiguityType"
496 alg.selectionTool.doEqual =
True
497 alg.selectionTool.equal = 0
498 alg.particles = config.readName (self.containerName)
499 alg.preselection = config.getPreselection (self.containerName, self.selectionName)
500 config.addSelection (self.containerName, self.selectionName, alg.selectionDecoration,
501 preselection=self.addSelectionToPreselection)
504 alg = config.createAlgorithm(
'CP::AsgSelectionAlg',
'ElectronDFCommonAddAmbiguityAlg' )
505 alg.selectionDecoration =
'selectDFCommonAddAmbiguity' + selectionPostfix +
',as_char'
506 config.addPrivateTool(
'selectionTool',
'CP::AsgNumDecorationSelectionToolInt' )
507 alg.selectionTool.decorationName =
"DFCommonAddAmbiguity"
508 if self.convSelection ==
"Veto":
509 alg.selectionTool.doMax =
True
510 alg.selectionTool.max = 1
511 elif self.convSelection ==
"GammaStar":
512 alg.selectionTool.doEqual =
True
513 alg.selectionTool.equal = 1
514 elif self.convSelection ==
"MatConv":
515 alg.selectionTool.doEqual =
True
516 alg.selectionTool.equal = 2
517 alg.particles = config.readName (self.containerName)
518 alg.preselection = config.getPreselection (self.containerName, self.selectionName)
519 config.addSelection (self.containerName, self.selectionName, alg.selectionDecoration,
520 preselection=self.addSelectionToPreselection)
523 if self.doFSRSelection :
525 wpFlag = alg.selectionDecoration.split(
",")[0]
526 alg = config.createAlgorithm(
'CP::EgammaFSRForMuonsCollectorAlg',
'EgammaFSRForMuonsCollectorAlg' )
527 alg.selectionDecoration = wpFlag
528 alg.ElectronOrPhotonContKey = config.readName (self.containerName)
532 if 'SiHit' in self.identificationWP:
536 if self.isolationWP !=
'NonIso' :
537 alg = config.createAlgorithm(
'CP::EgammaIsolationSelectionAlg',
538 'ElectronIsolationSelectionAlg' )
539 alg.selectionDecoration =
'isolated' + selectionPostfix +
',as_char'
540 config.addPrivateTool(
'selectionTool',
'CP::IsolationSelectionTool' )
541 alg.selectionTool.ElectronWP = self.isolationWP
542 if self.closeByCorrection:
543 alg.selectionTool.IsoDecSuffix =
"CloseByCorr"
544 alg.egammas = config.readName (self.containerName)
545 alg.preselection = config.getPreselection (self.containerName, self.selectionName)
546 config.addSelection (self.containerName, self.selectionName, alg.selectionDecoration,
547 preselection=self.addSelectionToPreselection)
549 if self.chargeIDSelectionRun2
and config.geometry() >= LHCPeriod.Run3:
550 log.warning(
"ECIDS is only available for Run 2 and will not have any effect in Run 3.")
553 if self.chargeIDSelectionRun2
and config.geometry() < LHCPeriod.Run3:
554 alg = config.createAlgorithm(
'CP::AsgSelectionAlg',
555 'ElectronChargeIDSelectionAlg' )
556 alg.selectionDecoration =
'chargeID' + selectionPostfix +
',as_char'
557 if self.recomputeChargeID:
559 config.addPrivateTool(
'selectionTool',
560 'AsgElectronChargeIDSelectorTool' )
561 alg.selectionTool.TrainingFile = \
562 'ElectronPhotonSelectorTools/ChargeID/ECIDS_20180731rel21Summer2018.root'
563 alg.selectionTool.WorkingPoint =
'Loose'
564 alg.selectionTool.CutOnBDT = -0.337671
567 config.addPrivateTool(
'selectionTool',
'CP::AsgFlagSelectionTool' )
568 alg.selectionTool.selectionFlags = [
"DFCommonElectronsECIDS"]
570 alg.particles = config.readName (self.containerName)
571 alg.preselection = config.getPreselection (self.containerName, self.selectionName)
572 config.addSelection (self.containerName, self.selectionName, alg.selectionDecoration,
573 preselection=self.addSelectionToPreselection)
575 correlationModels = [
"SIMPLIFIED",
"FULL",
"TOTAL",
"TOYS"]
576 map_file =
'ElectronEfficiencyCorrection/2015_2025/rel22.2/2025_Run2Rel22_Recommendation_v3/map0.txt' \
577 if config.geometry()
is LHCPeriod.Run2
else \
578 'ElectronEfficiencyCorrection/2015_2025/rel22.2/2025_Run3_Consolidated_Prerecom_v3/map1.txt'
581 if config.dataType()
is not DataType.Data
and not self.noEffSF:
582 if 'DNN' in self.identificationWP:
583 raise ValueError(
'DNN does not yet have efficiency correction, '
584 'please disable it by setting `noEffSF` to True.')
586 alg = config.createAlgorithm(
'CP::ElectronEfficiencyCorrectionAlg',
587 'ElectronEfficiencyCorrectionAlgReco' )
588 config.addPrivateTool(
'efficiencyCorrectionTool',
589 'AsgElectronEfficiencyCorrectionTool' )
590 alg.scaleFactorDecoration =
'el_reco_effSF' + selectionPostfix +
'_%SYS%'
591 alg.efficiencyCorrectionTool.MapFilePath = map_file
592 alg.efficiencyCorrectionTool.RecoKey =
"Reconstruction"
593 if self.correlationModelReco
not in correlationModels:
594 raise ValueError(
'Invalid correlation model for reconstruction efficiency, '
595 f
'has to be one of: {", ".join(correlationModels)}')
596 if config.geometry() >= LHCPeriod.Run3
and self.correlationModelReco !=
"TOTAL":
597 log.warning(
"Only TOTAL correlation model is currently supported "
598 "for reconstruction efficiency correction in Run 3.")
599 alg.efficiencyCorrectionTool.CorrelationModel =
"TOTAL"
601 alg.efficiencyCorrectionTool.CorrelationModel = self.correlationModelReco
602 if config.dataType()
is DataType.FastSim:
603 alg.efficiencyCorrectionTool.ForceDataType = (
604 PATCore.ParticleDataType.Full
if self.forceFullSimConfig
605 else PATCore.ParticleDataType.Fast)
606 elif config.dataType()
is DataType.FullSim:
607 alg.efficiencyCorrectionTool.ForceDataType = \
608 PATCore.ParticleDataType.Full
609 alg.outOfValidity = 2
610 alg.outOfValidityDeco =
'el_reco_bad_eff' + selectionPostfix
611 alg.electrons = config.readName (self.containerName)
612 alg.preselection = config.getPreselection (self.containerName, self.selectionName)
613 if self.saveDetailedSF:
614 config.addOutputVar (self.containerName, alg.scaleFactorDecoration,
615 'reco_effSF' + postfix)
616 sfList += [alg.scaleFactorDecoration]
619 if config.dataType()
is not DataType.Data
and not self.noEffSF:
620 alg = config.createAlgorithm(
'CP::ElectronEfficiencyCorrectionAlg',
621 'ElectronEfficiencyCorrectionAlgID' )
622 config.addPrivateTool(
'efficiencyCorrectionTool',
623 'AsgElectronEfficiencyCorrectionTool' )
624 alg.scaleFactorDecoration =
'el_id_effSF' + selectionPostfix +
'_%SYS%'
625 alg.efficiencyCorrectionTool.MapFilePath = map_file
626 alg.efficiencyCorrectionTool.IdKey = self.identificationWP.
replace(
"LH",
"")
627 if self.correlationModelId
not in correlationModels:
628 raise ValueError(
'Invalid correlation model for identification efficiency, '
629 f
'has to be one of: {", ".join(correlationModels)}')
630 alg.efficiencyCorrectionTool.CorrelationModel = self.correlationModelId
631 if config.dataType()
is DataType.FastSim:
632 alg.efficiencyCorrectionTool.ForceDataType = (
633 PATCore.ParticleDataType.Full
if self.forceFullSimConfig
634 else PATCore.ParticleDataType.Fast)
635 elif config.dataType()
is DataType.FullSim:
636 alg.efficiencyCorrectionTool.ForceDataType = \
637 PATCore.ParticleDataType.Full
638 alg.outOfValidity = 2
639 alg.outOfValidityDeco =
'el_id_bad_eff' + selectionPostfix
640 alg.electrons = config.readName (self.containerName)
641 alg.preselection = config.getPreselection (self.containerName, self.selectionName)
642 if self.saveDetailedSF:
643 config.addOutputVar (self.containerName, alg.scaleFactorDecoration,
644 'id_effSF' + postfix)
645 sfList += [alg.scaleFactorDecoration]
648 if config.dataType()
is not DataType.Data
and self.isolationWP !=
'NonIso' and not self.noEffSF:
649 alg = config.createAlgorithm(
'CP::ElectronEfficiencyCorrectionAlg',
650 'ElectronEfficiencyCorrectionAlgIsol' )
651 config.addPrivateTool(
'efficiencyCorrectionTool',
652 'AsgElectronEfficiencyCorrectionTool' )
653 alg.scaleFactorDecoration =
'el_isol_effSF' + selectionPostfix +
'_%SYS%'
654 alg.efficiencyCorrectionTool.MapFilePath = map_file
655 alg.efficiencyCorrectionTool.IdKey = self.identificationWP.
replace(
"LH",
"")
656 alg.efficiencyCorrectionTool.IsoKey = self.isolationWP
657 if self.correlationModelIso
not in correlationModels:
658 raise ValueError(
'Invalid correlation model for isolation efficiency, '
659 f
'has to be one of: {", ".join(correlationModels)}')
660 if self.correlationModelIso !=
'TOTAL':
661 log.warning(
"Only TOTAL correlation model is currently supported "
662 "for isolation efficiency correction in Run 3.")
663 alg.efficiencyCorrectionTool.CorrelationModel =
"TOTAL"
664 if config.dataType()
is DataType.FastSim:
665 alg.efficiencyCorrectionTool.ForceDataType = (
666 PATCore.ParticleDataType.Full
if self.forceFullSimConfig
667 else PATCore.ParticleDataType.Fast)
668 elif config.dataType()
is DataType.FullSim:
669 alg.efficiencyCorrectionTool.ForceDataType = \
670 PATCore.ParticleDataType.Full
671 alg.outOfValidity = 2
672 alg.outOfValidityDeco =
'el_isol_bad_eff' + selectionPostfix
673 alg.electrons = config.readName (self.containerName)
674 alg.preselection = config.getPreselection (self.containerName, self.selectionName)
675 if self.saveDetailedSF:
676 config.addOutputVar (self.containerName, alg.scaleFactorDecoration,
677 'isol_effSF' + postfix)
678 sfList += [alg.scaleFactorDecoration]
680 if (self.chargeIDSelectionRun2
and config.geometry() < LHCPeriod.Run3
and
681 config.dataType()
is not DataType.Data
and not self.noEffSF):
682 alg = config.createAlgorithm(
'CP::ElectronEfficiencyCorrectionAlg',
683 'ElectronEfficiencyCorrectionAlgEcids' )
684 config.addPrivateTool(
'efficiencyCorrectionTool',
685 'AsgElectronEfficiencyCorrectionTool' )
686 alg.scaleFactorDecoration =
'el_ecids_effSF' + selectionPostfix +
'_%SYS%'
687 if self.isolationWP !=
'Tight_VarRad':
688 raise ValueError(
'ECIDS SFs are supported only for Tight_VarRad isolation.')
689 if self.identificationWP ==
'LooseBLayerLH':
691 elif self.identificationWP ==
'MediumLH':
693 elif self.identificationWP ==
'TightLH':
696 raise ValueError(
'ECIDS SFs are supported only for ID LooseBLayerLH, MediumLH, or TightLH')
698 alg.efficiencyCorrectionTool.CorrelationModel =
"TOTAL"
699 alg.efficiencyCorrectionTool.CorrectionFileNameList = \
700 [f
'ElectronEfficiencyCorrection/2015_2025/rel22.2/2025_Run2Rel22_Recommendation_v2/ecids/efficiencySF.ChargeID.{ecids_lh}_ECIDS_Tight_VarRad.root']
701 if config.dataType()
is DataType.FastSim:
702 alg.efficiencyCorrectionTool.ForceDataType = (
703 PATCore.ParticleDataType.Full
if self.forceFullSimConfig
704 else PATCore.ParticleDataType.Fast)
705 elif config.dataType()
is DataType.FullSim:
706 alg.efficiencyCorrectionTool.ForceDataType = \
707 PATCore.ParticleDataType.Full
708 alg.outOfValidity = 2
709 alg.outOfValidityDeco =
'el_ecids_bad_eff' + selectionPostfix
710 alg.electrons = config.readName (self.containerName)
711 alg.preselection = config.getPreselection (self.containerName, self.selectionName)
712 if self.saveDetailedSF:
713 config.addOutputVar (self.containerName, alg.scaleFactorDecoration,
714 'ecids_effSF' + postfix)
715 sfList += [alg.scaleFactorDecoration]
717 if self.addChargeMisIDSF
and config.dataType()
is not DataType.Data
and not self.noEffSF:
718 if config.geometry() >= LHCPeriod.Run3:
719 raise ValueError(
'Run 3 does not yet have charge mis-ID correction, '
720 'please disable it by setting `noEffSF` to False.')
722 alg = config.createAlgorithm(
'CP::ElectronEfficiencyCorrectionAlg',
723 'ElectronEfficiencyCorrectionAlgMisid' )
724 config.addPrivateTool(
'efficiencyCorrectionTool',
725 'CP::ElectronChargeEfficiencyCorrectionTool' )
726 alg.scaleFactorDecoration =
'el_charge_misid_effSF' + selectionPostfix +
'_%SYS%'
727 if self.isolationWP !=
'Tight_VarRad':
728 raise ValueError(
'Charge mis-ID SFs are supported only for Tight_VarRad isolation.')
729 if self.identificationWP ==
'LooseBLayerLH':
730 misid_lh =
'LooseAndBLayerLLH'
731 elif self.identificationWP ==
'MediumLH':
732 misid_lh =
'MediumLLH'
733 elif self.identificationWP ==
'TightLH':
734 misid_lh =
'TightLLH'
736 raise ValueError(
'Charge mis-ID SFs are supported only for ID LooseBLayerLH, MediumLH, or TightLH')
737 misid_suffix =
'_ECIDSloose' if self.chargeIDSelectionRun2
else ''
739 alg.efficiencyCorrectionTool.CorrectionFileName = \
740 f
'ElectronEfficiencyCorrection/2015_2025/rel22.2/2025_Run2Rel22_Recommendation_v2/charge_misID/chargeEfficiencySF.{misid_lh}_d0z0_TightVarRad{misid_suffix}.root'
741 if config.dataType()
is DataType.FastSim:
742 alg.efficiencyCorrectionTool.ForceDataType = (
743 PATCore.ParticleDataType.Full
if self.forceFullSimConfig
744 else PATCore.ParticleDataType.Fast)
745 elif config.dataType()
is DataType.FullSim:
746 alg.efficiencyCorrectionTool.ForceDataType = \
747 PATCore.ParticleDataType.Full
748 alg.outOfValidity = 2
749 alg.outOfValidityDeco =
'el_misid_bad_eff' + selectionPostfix
750 alg.electrons = config.readName (self.containerName)
751 alg.preselection = config.getPreselection (self.containerName, self.selectionName)
752 if self.saveDetailedSF:
753 config.addOutputVar (self.containerName, alg.scaleFactorDecoration,
754 'charge_misid_effSF' + postfix)
755 sfList += [alg.scaleFactorDecoration]
757 if config.dataType()
is not DataType.Data
and not self.noEffSF
and self.saveCombinedSF:
758 alg = config.createAlgorithm(
'CP::AsgObjectScaleFactorAlg',
759 'ElectronCombinedEfficiencyScaleFactorAlg' )
760 alg.particles = config.readName (self.containerName)
761 alg.inScaleFactors = sfList
762 alg.outScaleFactor =
'effSF' + postfix +
'_%SYS%'
763 config.addOutputVar (self.containerName, alg.outScaleFactor,
'effSF' + postfix)