Skip to content
GitLab
Menu
Projects
Groups
Snippets
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Sign in
Toggle navigation
Menu
Open sidebar
Kazuya Mochizuki
athena
Commits
35fe7f37
Commit
35fe7f37
authored
Jun 02, 2020
by
Christos Anastopoulos
Browse files
Cleanup a bit the egamma config Part 1
parent
07d2aec7
Changes
5
Hide whitespace changes
Inline
Side-by-side
Reconstruction/egamma/egammaAlgs/python/EMBremCollectionBuilder.py
View file @
35fe7f37
...
...
@@ -3,20 +3,20 @@
__doc__
=
"ToolFactory to instantiate egammaBremCollectionBuilder with default configuration"
__author__
=
"Christos"
# default configuration of the EMBremCollectionBuilder
from
egammaTrackTools.egammaTrackToolsFactories
import
EMExtrapolationTools
from
InDetRecExample.InDetKeys
import
InDetKeys
from
AthenaCommon.Logging
import
logging
import
InDetRecExample.TrackingCommon
as
TrackingCommon
from
AthenaCommon.DetFlags
import
DetFlags
from
RecExConfig.RecFlags
import
rec
from
InDetRecExample.InDetJobProperties
import
InDetFlags
from
AthenaCommon.Logging
import
logging
# import base class
from
egammaAlgs
import
egammaAlgsConf
from
egammaTools.egammaExtrapolators
import
egammaExtrapolator
from
egammaRec.Factories
import
AlgFactory
from
egammaRec
import
egammaKeys
import
InDetRecExample.TrackingCommon
as
TrackingCommon
from
egammaRec.Factories
import
AlgFactory
from
egammaTools.egammaExtrapolators
import
(
AtlasPublicExtrapolator
,
egammaExtrapolator
)
# default configuration of the EMBremCollectionBuilder
from
egammaTrackTools.egammaTrackToolsFactories
import
EMExtrapolationTools
from
InDetRecExample.InDetJobProperties
import
InDetFlags
from
InDetRecExample.InDetKeys
import
InDetKeys
from
RecExConfig.RecFlags
import
rec
class
egammaBremCollectionBuilder
(
egammaAlgsConf
.
EMBremCollectionBuilder
):
...
...
@@ -28,15 +28,10 @@ class egammaBremCollectionBuilder (egammaAlgsConf.EMBremCollectionBuilder):
super
(
egammaBremCollectionBuilder
,
self
).
__init__
(
name
,
**
kw
)
# Extrapolator to be used for GSF
import
egammaRec.EMCommonRefitter
GSFBuildInDetExtrapolator
=
egammaExtrapolator
()
# Some of the InDet tool need the extrapolator to be passed
# to be public. We can remove this part if this becomes
# not needed
from
AthenaCommon.AppMgr
import
ToolSvc
ToolSvc
+=
GSFBuildInDetExtrapolator
# Extrapolator to be used for GSF this is private
GSFBuildInDetExtrapolator
=
egammaExtrapolator
()
from
egammaTrackTools.egammaTrackToolsConf
import
egammaTrkRefitterTool
GSFRefitterTool
=
egammaTrkRefitterTool
(
...
...
@@ -52,7 +47,8 @@ class egammaBremCollectionBuilder (egammaAlgsConf.EMBremCollectionBuilder):
GSFBuildTestBLayerTool
=
None
GSFBuildPixelToTPIDTool
=
None
if
DetFlags
.
haveRIO
.
pixel_on
():
GSFPixelConditionsSummaryTool
=
TrackingCommon
.
getInDetPixelConditionsSummaryTool
()
GSFPixelConditionsSummaryTool
=
(
TrackingCommon
.
getInDetPixelConditionsSummaryTool
())
if
InDetFlags
.
usePixelDCS
():
GSFPixelConditionsSummaryTool
.
IsActiveStates
=
[
'READY'
,
'ON'
,
'UNKNOWN'
,
'TRANSITION'
,
'UNDEFINED'
]
...
...
@@ -72,44 +68,55 @@ class egammaBremCollectionBuilder (egammaAlgsConf.EMBremCollectionBuilder):
GSFBuildTRT_ElectronPidTool
=
None
if
DetFlags
.
haveRIO
.
TRT_on
()
and
not
InDetFlags
.
doSLHC
(
)
and
not
InDetFlags
.
doHighPileup
():
GSFBuildTRT_ElectronPidTool
=
TrackingCommon
.
getInDetTRT_ElectronPidTool
(
name
=
"GSFBuildTRT_ElectronPidTool"
)
GSFBuildTRT_ElectronPidTool
=
(
TrackingCommon
.
getInDetTRT_ElectronPidTool
(
name
=
"GSFBuildTRT_ElectronPidTool"
,
private
=
True
))
#
# InDet Track Summary Helper
# InDet Track Summary Helper, no Association and no hole
# as we do not redo them
#
GSFBuildTrackSummaryHelperTool
=
TrackingCommon
.
getInDetSummaryHelper
(
name
=
"GSFBuildTrackSummaryHelperTool"
,
AssoTool
=
None
,
HoleSearch
=
None
,
PixelToTPIDTool
=
GSFBuildPixelToTPIDTool
,
TestBLayerTool
=
GSFBuildTestBLayerTool
,
DoSharedHits
=
False
)
DoSharedHits
=
False
,
private
=
True
)
#
# TrkTrackSummaryTool: no shared hits
and avoid repeat of
hole search
# TrkTrackSummaryTool: no shared hits
no
hole search
#
GSFBuildInDetTrackSummaryTool
=
TrackingCommon
.
getInDetTrackSummaryTool
(
name
=
"GSFBuildInDetTrackSummaryTool"
,
InDetSummaryHelperTool
=
GSFBuildTrackSummaryHelperTool
,
doSharedHits
=
False
,
doHolesInDet
=
False
,
TRT_ElectronPidTool
=
GSFBuildTRT_ElectronPidTool
,
PixelToTPIDTool
=
GSFBuildPixelToTPIDTool
GSFBuildInDetTrackSummaryTool
=
(
TrackingCommon
.
getInDetTrackSummaryTool
(
name
=
"GSFBuildInDetTrackSummaryTool"
,
InDetSummaryHelperTool
=
GSFBuildTrackSummaryHelperTool
,
doSharedHits
=
False
,
doHolesInDet
=
False
,
TRT_ElectronPidTool
=
GSFBuildTRT_ElectronPidTool
,
PixelToTPIDTool
=
GSFBuildPixelToTPIDTool
)
)
#
# Track Particle Creator tool
#
from
TrkParticleCreator.TrkParticleCreatorConf
import
Trk__TrackParticleCreatorTool
# The TrackSummary needs a public extrapolator still...
from
TrkParticleCreator.TrkParticleCreatorConf
import
(
Trk__TrackParticleCreatorTool
)
GSFBuildInDetParticleCreatorTool
=
Trk__TrackParticleCreatorTool
(
name
=
"GSFBuildInDetParticleCreatorTool"
,
KeepParameters
=
True
,
Extrapolator
=
GSFBuildInDet
Extrapolator
,
Extrapolator
=
AtlasPublic
Extrapolator
()
,
TrackSummaryTool
=
GSFBuildInDetTrackSummaryTool
,
UseTrackSummaryTool
=
False
)
#
# do track slimming
#
from
TrkTrackSlimmingTool.TrkTrackSlimmingToolConf
import
Trk__TrackSlimmingTool
as
ConfigurableTrackSlimmingTool
from
TrkTrackSlimmingTool.TrkTrackSlimmingToolConf
import
(
Trk__TrackSlimmingTool
as
ConfigurableTrackSlimmingTool
)
GSFBuildInDetTrkSlimmingTool
=
ConfigurableTrackSlimmingTool
(
name
=
"GSFBuildInDetTrackSlimmingTool"
,
KeepParameters
=
False
,
...
...
@@ -123,13 +130,14 @@ class egammaBremCollectionBuilder (egammaAlgsConf.EMBremCollectionBuilder):
self
.
TrackSummaryTool
=
GSFBuildInDetTrackSummaryTool
EMBremCollectionBuilder
=
AlgFactory
(
egammaBremCollectionBuilder
,
name
=
'EMBremCollectionBuilder'
,
ExtrapolationTool
=
EMExtrapolationTools
,
TrackParticleContainerName
=
InDetKeys
.
xAODTrackParticleContainer
(),
OutputTrkPartContainerName
=
egammaKeys
.
outputTrackParticleKey
(),
OutputTrackContainerName
=
egammaKeys
.
outputTrackKey
(),
DoTruth
=
rec
.
doTruth
(),
usePixel
=
DetFlags
.
haveRIO
.
pixel_on
(),
useSCT
=
DetFlags
.
haveRIO
.
SCT_on
()
)
EMBremCollectionBuilder
=
AlgFactory
(
egammaBremCollectionBuilder
,
name
=
'EMBremCollectionBuilder'
,
ExtrapolationTool
=
EMExtrapolationTools
,
TrackParticleContainerName
=
InDetKeys
.
xAODTrackParticleContainer
(),
OutputTrkPartContainerName
=
egammaKeys
.
outputTrackParticleKey
(),
OutputTrackContainerName
=
egammaKeys
.
outputTrackKey
(),
DoTruth
=
rec
.
doTruth
(),
usePixel
=
DetFlags
.
haveRIO
.
pixel_on
(),
useSCT
=
DetFlags
.
haveRIO
.
SCT_on
()
)
Reconstruction/egamma/egammaAlgs/python/EMVertexBuilder.py
View file @
35fe7f37
# Copyright (C) 2002-20
19
CERN for the benefit of the ATLAS collaboration
# Copyright (C) 2002-20
20
CERN for the benefit of the ATLAS collaboration
__doc__
=
"ToolFactory to instantiate EMVertexBuilder with default configuration"
__author__
=
"Bruno Lenzi"
import
InDetRecExample.TrackingCommon
as
TrackingCommon
from
egammaAlgs
import
egammaAlgsConf
from
egammaRec.Factories
import
FcnWrapper
,
AlgFactory
,
PublicToolFactory
from
egammaRec.Factories
import
FcnWrapper
,
AlgFactory
from
egammaRec
import
egammaKeys
from
egammaTrackTools.egammaTrackToolsFactories
import
EMExtrapolationTools
from
egammaTools.egammaExtrapolators
import
egamma
Extrapolator
from
egammaTools.egammaExtrapolators
import
AtlasPublic
Extrapolator
class
VertexFinderToolInstance
(
FcnWrapper
):
def
__call__
(
self
):
#################################################################
# egamma InDet summary tool to be used conversion finding
#
# load association tool from Inner Detector to handle pixel ganged
# ambiguities
from
InDetAssociationTools.InDetAssociationToolsConf
import
InDet__InDetPRD_AssociationToolGangedPixels
egammaInDetPrdAssociationTool
=
PublicToolFactory
(
InDet__InDetPRD_AssociationToolGangedPixels
,
name
=
"egammaInDetPrdAssociationTool"
,
PixelClusterAmbiguitiesMapName
=
'PixelClusterAmbiguitiesMap'
)
# Loading Configurable HoleSearchTool
#
from
InDetTrackHoleSearch.InDetTrackHoleSearchConf
import
InDet__InDetTrackHoleSearchTool
egammaInDetHoleSearchTool
=
PublicToolFactory
(
InDet__InDetTrackHoleSearchTool
,
name
=
"egammaInDetHoleSearchTool"
,
Extrapolator
=
egammaExtrapolator
)
#
egammaInDetHoleSearchTool
=
TrackingCommon
.
getInDetHoleSearchTool
(
name
=
"egammaInDetHoleSearchTool"
,
Extrapolator
=
AtlasPublicExtrapolator
()
)
# Load the InDetTrackSummaryHelperTool
#
from
AthenaCommon.DetFlags
import
DetFlags
from
InDetTrackSummaryHelperTool.InDetTrackSummaryHelperToolConf
import
InDet__InDetTrackSummaryHelperTool
egammaInDetTrackSummaryHelperTool
=
PublicToolFactory
(
InDet__InDetTrackSummaryHelperTool
,
name
=
"egammaInDetSummaryHelper"
,
AssoTool
=
egammaInDetPrdAssociationTool
,
DoSharedHits
=
False
,
HoleSearch
=
egammaInDetHoleSearchTool
,
usePixel
=
DetFlags
.
haveRIO
.
pixel_on
(),
useSCT
=
DetFlags
.
haveRIO
.
SCT_on
(),
useTRT
=
DetFlags
.
haveRIO
.
TRT_on
())
egammaInDetTrackSummaryHelperTool
=
(
TrackingCommon
.
getInDetSummaryHelper
(
name
=
"egammaInDetSummaryHelper"
,
AssoTool
=
None
,
DoSharedHits
=
False
,
HoleSearch
=
egammaInDetHoleSearchTool
,
private
=
True
))
#
from
TrkTrackSummaryTool.TrkTrackSummaryToolConf
import
Trk__
TrackSummaryTool
egammaInDetTrackSummaryTool
=
PublicToolFactory
(
Trk__
TrackSummaryTool
,
name
=
"egammaInDetTrackSummaryTool"
,
InDetSummaryHelperTool
=
egammaInDetTrackSummaryHelperTool
,
doSharedHits
=
False
,
doHolesInDet
=
True
)
egammaInDet
TrackSummaryTool
=
(
TrackingCommon
.
getInDet
TrackSummaryTool
(
name
=
"egammaInDetTrackSummaryTool"
,
InDetSummaryHelperTool
=
egammaInDetTrackSummaryHelperTool
,
doSharedHits
=
False
,
doHolesInDet
=
True
)
)
#
# Configured conversion vertex reconstruction cuts
#
from
InDetRecExample.ConfiguredSecondaryVertexCuts
import
ConfiguredSecondaryVertexCuts
from
InDetRecExample.ConfiguredSecondaryVertexCuts
import
(
ConfiguredSecondaryVertexCuts
)
egammaConversionVertexCuts
=
ConfiguredSecondaryVertexCuts
(
mode
=
"EGammaPileUp"
)
from
InDetRecExample.ConfiguredSecVertexFinding
import
ConfiguredSecVertexFinding
theemvertexfindertool
=
ConfiguredSecVertexFinding
(
prefix
=
"egammaConversion"
,
VertexCuts
=
egammaConversionVertexCuts
,
TrackParticles
=
egammaKeys
.
outputTrackParticleKey
(),
SecVertices
=
egammaKeys
.
outputConversionKey
(),
Extrapolator
=
egammaExtrapolator
(),
TrackSummaryTool
=
egammaInDetTrackSummaryTool
(),
printConfig
=
False
)
from
InDetRecExample.ConfiguredSecVertexFinding
import
(
ConfiguredSecVertexFinding
)
theemvertexfindertool
=
ConfiguredSecVertexFinding
(
prefix
=
"egammaConversion"
,
VertexCuts
=
egammaConversionVertexCuts
,
TrackParticles
=
egammaKeys
.
outputTrackParticleKey
(),
SecVertices
=
egammaKeys
.
outputConversionKey
(),
Extrapolator
=
AtlasPublicExtrapolator
(),
TrackSummaryTool
=
egammaInDetTrackSummaryTool
,
printConfig
=
False
)
return
theemvertexfindertool
.
toolInstance
()
EMVertexBuilder
=
AlgFactory
(
egammaAlgsConf
.
EMVertexBuilder
,
InputTrackParticleContainerName
=
egammaKeys
.
outputTrackParticleKey
(),
OutputConversionContainerName
=
egammaKeys
.
outputConversionKey
(),
VertexFinderTool
=
VertexFinderToolInstance
(),
ExtrapolationTool
=
EMExtrapolationTools
)
EMVertexBuilder
=
AlgFactory
(
egammaAlgsConf
.
EMVertexBuilder
,
InputTrackParticleContainerName
=
egammaKeys
.
outputTrackParticleKey
(),
OutputConversionContainerName
=
egammaKeys
.
outputConversionKey
(),
VertexFinderTool
=
VertexFinderToolInstance
(),
ExtrapolationTool
=
EMExtrapolationTools
)
Reconstruction/egamma/egammaRec/python/Factories.py
View file @
35fe7f37
# Copyright (C) 2002-20
19
CERN for the benefit of the ATLAS collaboration
# Copyright (C) 2002-20
20
CERN for the benefit of the ATLAS collaboration
from
__future__
import
print_function
...
...
@@ -7,265 +7,303 @@ __authors__ = "Bruno Lenzi, Christos Anastopoulos, Jovan Mitrevski"
from
AthenaCommon.Logging
import
logging
from
AthenaCommon.Resilience
import
treatException
def
factoriesInfo
(
log
):
logFactories
=
logging
.
getLogger
(
'Factories'
)
logFactories
.
info
(
log
)
def
factoriesInfo
(
log
):
logFactories
=
logging
.
getLogger
(
'Factories'
)
logFactories
.
info
(
log
)
# tools
def
isAlreadyInToolSvc
(
name
):
"isAlreadyInToolSvc ( name of the tool ) --> check if the tool with name is already in the service"
from
AthenaCommon.AppMgr
import
ToolSvc
if
hasattr
(
ToolSvc
,
name
):
return
True
else
:
return
False
def
getFromToolSvc
(
name
):
"getFromToolSvc ( name of the tool ) --> Get the tool from toolSvc by name "
from
AthenaCommon.AppMgr
import
ToolSvc
return
getattr
(
ToolSvc
,
name
)
def
addToToolSvc
(
tool
):
"addToToolSvc( tool ) --> add tool to ToolSvc"
from
AthenaCommon.AppMgr
import
ToolSvc
ToolSvc
+=
tool
return
tool
def
isAlreadyInToolSvc
(
name
):
"""isAlreadyInToolSvc (name of the tool)
--> check if the tool with name is already in the service"""
from
AthenaCommon.AppMgr
import
ToolSvc
if
hasattr
(
ToolSvc
,
name
):
return
True
else
:
return
False
def
getFromToolSvc
(
name
):
"""getFromToolSvc (name of the tool)
--> Get the tool from toolSvc by name """
from
AthenaCommon.AppMgr
import
ToolSvc
return
getattr
(
ToolSvc
,
name
)
def
addToToolSvc
(
tool
):
"""addToToolSvc(tool)
--> add tool to ToolSvc"""
from
AthenaCommon.AppMgr
import
ToolSvc
ToolSvc
+=
tool
return
tool
# services
def
isAlreadyInServiceMgr
(
name
):
"isAlreadyInServiceMgr ( mane of the service ) --> check if the service with name is already in the manager"
from
AthenaCommon.AppMgr
import
ServiceMgr
if
hasattr
(
ServiceMgr
,
name
):
return
True
else
:
return
False
def
getFromServiceMgr
(
name
):
"getFromServiceMgr ( name of the service ) --> Get the service from manager by name "
from
AthenaCommon.AppMgr
import
ServiceMgr
return
getattr
(
ServiceMgr
,
name
)
def
addToServiceMgr
(
service
):
"addToServiceMgr( service ) --> add service to ServiceMgr"
from
AthenaCommon.AppMgr
import
ServiceMgr
ServiceMgr
+=
service
return
service
def
isAlreadyInServiceMgr
(
name
):
"""isAlreadyInServiceMgr (name of the service)
--> check if the service with name is already in the manager"""
from
AthenaCommon.AppMgr
import
ServiceMgr
if
hasattr
(
ServiceMgr
,
name
):
return
True
else
:
return
False
def
getFromServiceMgr
(
name
):
"""getFromServiceMgr (name of the service)
--> Get the service from manager by name """
from
AthenaCommon.AppMgr
import
ServiceMgr
return
getattr
(
ServiceMgr
,
name
)
def
addToServiceMgr
(
service
):
"""addToServiceMgr( service )
--> add service to ServiceMgr"""
from
AthenaCommon.AppMgr
import
ServiceMgr
ServiceMgr
+=
service
return
service
# algs
def
isAlreadyInTopSequence
(
name
):
"isAlreadyInTopSequence ( mane of the alg ) --> check if the alg with name is already in the Alg sequence"
from
AthenaCommon.AlgSequence
import
AlgSequence
topSequence
=
AlgSequence
()
if
hasattr
(
topSequence
,
name
):
return
True
else
:
return
False
def
getFromTopSequence
(
name
):
"getFromTopSequence ( name of the alg ) --> Get the alg from TopSequence by name "
from
AthenaCommon.AlgSequence
import
AlgSequence
topSequence
=
AlgSequence
()
return
getattr
(
topSequence
,
name
)
def
addToTopSequence
(
alg
):
"addToTopSequence( alg ) --> add alg to TopSequence"
from
AthenaCommon.AlgSequence
import
AlgSequence
topSequence
=
AlgSequence
()
topSequence
+=
alg
return
alg
def
isAlreadyInTopSequence
(
name
):
"""isAlreadyInTopSequence ( mane of the alg )
--> check if the alg with name is already in the Alg sequence"""
from
AthenaCommon.AlgSequence
import
AlgSequence
topSequence
=
AlgSequence
()
if
hasattr
(
topSequence
,
name
):
return
True
else
:
return
False
def
getFromTopSequence
(
name
):
"""getFromTopSequence (name of the alg)
--> Get the alg from TopSequence by name """
from
AthenaCommon.AlgSequence
import
AlgSequence
topSequence
=
AlgSequence
()
return
getattr
(
topSequence
,
name
)
def
addToTopSequence
(
alg
):
"addToTopSequence(alg) --> add alg to TopSequence"
from
AthenaCommon.AlgSequence
import
AlgSequence
topSequence
=
AlgSequence
()
topSequence
+=
alg
return
alg
def
getPropertyValue
(
tool
,
property
):
"""getPropertyValue( tool, property) --> return the set value or the default value
of the given property for the given tool or alg"""
try
:
return
getattr
(
tool
,
property
)
except
AttributeError
:
return
tool
.
getDefaultProperty
(
property
)
"""getPropertyValue(tool, property)
--> return the set value or the default value
of the given property for the given tool or alg"""
try
:
return
getattr
(
tool
,
property
)
except
AttributeError
:
return
tool
.
getDefaultProperty
(
property
)
class
FcnWrapper
:
"""A simple wrapper to call a function with no arguments"""
def
__init__
(
self
,
fcn
=
None
):
self
.
fcn
=
fcn
def
__call__
(
self
):
return
self
.
fcn
()
"""A simple wrapper to call a function with no arguments"""
def
__init__
(
self
,
fcn
=
None
):
self
.
fcn
=
fcn
def
__call__
(
self
):
return
self
.
fcn
()
class
Factory
:
"""Factory: base class of ToolFactory and AlgFactory.
Allows to "lazy" instantiate tools/algs/services. Properties are set in the c-tor ,
and can include other factories,tools. One can also define preInit/postInit methods.
The end result is that the configuration can be defined/formed/composed before the actual instantiation
which happens when needed, i.e at demand via () (__call__()) method.
# Definition
from MyPkg import MyPkgConf
MyTool1 = ToolFactory(MyPkgConf.MyTool1) # does not set any default
# set default via value and pass a method that will be called after instantiation
def setPropertyX(tool): tool.propertyX = 'x'
MyTool2 = ToolFactory(MyPkgConf.MyTool2, postInit=[setPropertyX], name='MyTool2a', PropertyY = 'y')
# default via FcnWrapper or ToolFactory instance, called when the tool is instantiated
def getNameOfTool2(): return MyTool2().getFullName()
MyTool3 = ToolFactory(MyPkgConf.MyTool3,
Tool1 = MyTool1, # ToolFactory instance, not actual tool instance
NameOfTool2 = FcnWrapper(getNameOfTool2), # could have used FullNameWrapper( MyTool2 )
)
# Actual tool instantiation at demand
MyTool1() # create tool instance with default values
MyTool2(propertyY = 'xx') # create a tool instance overriding default propertyY. At this point setPropertyX will be called
MyTool3() # create default instance, will all try to instantiate MyTool1 (effect depends on Private/Public usage)
tool2a = MyTool2('myTool2a') # create another factory instance with the same properties as MyTool2 above ('x')
tool3a = MyTool3('myTool3a', Tool1 = MyTool1('myTool1a', propertyA = 'B'),NameOfTool2 = 'myTool2a')
# create another factory instance overriding both properties set above for MyTool3
"""
def
__init__
(
self
,
iclass
,
**
defaults
):
"""
@param iclass Tool / Alg class
@param defaults default values for configurables, can be overridden at instantiation.
Special parameters:
- preInit: list of functions to be called before tool/alg instantiation, take no arguments
- preInit: list of functions to be called after tool/alg instantiation, take tool/alg as argument
- doAdd: add tool (alg) to ToolSvc (TopSequence) (default: True)
- doPrint: print tool/alg after instantiation (default: False)
"""Factory: base class of ToolFactory and AlgFactory.
Allows to "lazy" instantiate the configuration of tools/algs/services.
Properties are set in the c-tor,
and can include other factories,tools.
One can also define preInit/postInit methods.
The end result is that the configuration
can be defined/formed/composed before
before its actual instantiation which happens at demand
via the () (__call__()) method.
"""
self
.
iclass
=
iclass
self
.
defaults
=
defaults
def
copy
(
self
,
name
,
**
kw
):
"copy(self, name, **kw) --> return a new instance of the factory with new name and defaults"
kw
[
'name'
]
=
name
deflt
=
self
.
defaults
.
copy
()
deflt
.
update
(
kw
)
return
self
.
__class__
(
self
.
iclass
,
**
deflt
)
def
__call__
(
self
,
name
=
''
,
**
kw
):
"""Call preInit functions, instantiate tool (alg), call postInit functions and add
to ToolSvc (TopSequence)"""
params
=
self
.
defaults
.
copy
()
params
.
update
(
kw
)
params
[
'name'
]
=
name
or
params
.
get
(
'name'
,
self
.
iclass
.
__name__
)
del
name
,
kw
# to avoid silly mistakes
# Get special parameters (or default values) and remove them from dictionary
preInit
=
params
.
pop
(
'preInit'
,
[])
postInit
=
params
.
pop
(
'postInit'
,
[])
doAdd
=
params
.
pop
(
'doAdd'
,
True
)
doPrint
=
params
.
pop
(
'doPrint'
,
False
)
# Call preInit functions
for
fcn
in
preInit
:
try
:
fcn
()
except
:
treatException
(
'calling preInit function %s on %s instantiation
\n
'
%
(
fcn
.
__name__
,
params
[
'name'
]))
raise
# Instantiate the Factory parameters or call the Function wrappers.
# (if they are inside a list -->ToolHandleArray loop over them)
classes
=
(
FcnWrapper
,
ToolFactory
,
PublicToolFactory
,
ServiceFactory
)
for
paramName
,
value
in
params
.
items
():
if
isinstance
(
value
,
classes
)
or
\
(
isinstance
(
value
,
list
)
and
any
(
isinstance
(
v
,
classes
)
for
v
in
value
)
):
def
__init__
(
self
,
iclass
,
**
defaults
):
"""
@param iclass Tool / Alg class
@param defaults default values for configurables,
can be overridden at instantiation.
Special parameters:
- preInit: list of functions to be called before tool/alg
instantiation, take no arguments
- preInit: list of functions to be called after tool/alg
instantiation, take tool/alg as argument
- doAdd: add tool (alg) to ToolSvc (TopSequence) (default: True)
- doPrint: print tool/alg after instantiation (default: False)
"""
self
.
iclass
=
iclass
self
.
defaults
=
defaults
def
copy
(
self
,
name
,
**
kw
):
"""copy(self, name, **kw)
--> return a new instance of the factory with new name and defaults"""
kw
[
'name'
]
=
name
deflt
=
self
.
defaults
.
copy
()
deflt
.
update
(
kw
)
return
self
.
__class__
(
self
.
iclass
,
**
deflt
)
def
__call__
(
self
,
name
=
''
,
**
kw
):
"""Call preInit functions, instantiate tool (alg),
call postInit functions and add to ToolSvc (TopSequence)"""
params
=
self
.
defaults
.
copy
()
params
.
update
(
kw
)
params
[
'name'
]
=
name
or
params
.
get
(
'name'
,
self
.
iclass
.
__name__
)
del
name
,
kw
# to avoid silly mistakes
# Get special parameters (or default values)
# and remove them from dictionary
preInit
=
params
.
pop
(
'preInit'
,
[])
postInit
=
params
.
pop
(
'postInit'
,
[])
doAdd
=
params
.
pop
(
'doAdd'
,
True
)
doPrint
=
params
.
pop
(
'doPrint'
,
False
)
# Call preInit functions
for
fcn
in
preInit
:
try
:
fcn
()
except
Exception
: