7This module defines various components to be used in AnalysisBase releases.
8These components (classes, submodules...) can be used in place of some Athena components
9they are replacing, so athena-style configuration can be used unchanged in AnalysisBase
11This module is thus designed ONLY for AnalysisBase and to run EventLoop jobs.
13IMPORTANT : this module is only designed so the jet configuration works. There's no guarantee it won't mess up
14when used with other part of Athena configuration.
16Internally, the athena-style configurations of algorithms are translated to AnaReentrantAlgorithmConfig or
17AnaAlgorithmConfig. The configuration of AsgTools are translated to call to AnaAlgorithmConfig.addPrivateTool()
18or AnaAlgorithmConfig.setPropertyFromString()
20This module allows to write python config for EventLoop exactly as one would do for Athena :
22alg1 = CompFactory.MyAlg("algname",
23 InputContainer = "SomeJets".
25 AprivateTool = CompFactory.SomeTool("tool", AToolparam =0.21), )
26alg2 = CompFactory.AnOtherAlg( ... )
30job.addManyAlgs([alg1, alg2]) # specific call allowed by this module
32driver = ROOT.EL.DirectDriver()
33driver.submit( job, "out")
38from types
import ModuleType
46logging.setLoggerClass( logging.Logger )
50 """Helper function producing a string property value"""
52 stringValue = str( value )
53 if isinstance( value, bool ):
54 stringValue = str( int( value ) )
61 """A simplistic array of Configured (see below) to replace the ToolHandleArray of Athena """
83 tool = anaAlg.addPrivateToolInArray(conf.fullname(), conf.type)
84 conf._name = tool._prefix.split(
'.')[-1]
85 conf.assignAllProperties(anaAlg)
90 """A replacement for Athena auto-generated Configured python class.
91 Configured has the same interface as its Athena counterpart and can describe both Tools and Algorithms
93 This is a base class. The system replacing CompFactory will generate a derived class for each c++ Tool/Algorithm
94 to hold the configuration of such tool/alg (see generateConfigured())
102 _allowed = [
'_properties',
'_name',
'_parent',
'_anaAlg']
108 for k,v
in props.items():
115 raise AttributeError(
"Configuration of Tool {} / {} . can't set attribute : {}".format(self.
type, self.
_name, k) )
123 if isinstance(v, Configured):
125 raise RuntimeError(
"Configuring {} / {} : Tool for property {} already exists".format(self.
type, self.
_name, k) )
129 elif isinstance(v, (list, tuple) ):
130 if isinstance(v[0], Configured):
145 if self.
_parent is None:
return k
149 if self.
_parent is None :
return [self]
154 return '.'.join([p._name
for p
in parents])
157 return [ (k, getattr(self,k))
for k
in self.
_properties]
164 """Returns this configured alg as an instance of Ana(Reentrant)AlgorithmConfig
166 if issubclass(self.
_cppclass, ROOT.EL.AnaReentrantAlgorithm):
167 alg=ROOT.EL.AnaReentrantAlgorithmConfig()
169 alg=ROOT.EL.AnaAlgorithmConfig()
177 """If self represents a configured AlgTool,
178 this call will configure the AnaAlgorithmConfig 'anaAlg' so
179 its ToolHandle property 'handlename' is configured with self
181 if handlename
in anaAlg._props:
183 props = {handlename:self, }
184 klass=
type(
'TmpConf', (Configured,), dict(_allowed=self.
_allowed+[handlename], _propTypes={},
185 type=anaAlg.getType(),_cppclass=
'none') )
186 c=klass(anaAlg.name(), **props)
187 c.assignAllProperties(anaAlg)
190 """ Transfer all the configuration in self to anaAlg
191 where anaAlg is an AnaAlgorithmConfig."""
198 if isinstance(v , Configured):
201 alg.addPrivateTool(v.fullname(), v.type)
205 v.assignAllProperties(alg)
206 elif isinstance(v, ConfArray ):
208 v.assignAllProperties(alg)
212 alg.setProperty[cpptype](self.
prefixed(k) , v)
222 if issubclass(cppclass, cppyy.gbl.asg.IAsgTool):
223 dummy = cppclass(
'dummy')
225 dummy = cppclass(
'dummy', 0)
228 pm = dummy.getPropertyMgr()
229 propkeys = [str(k)
for k,p
in pm.getProperties() ]
230 propTypes = dict( (k,
propertyType(pm.getProperty(k)) )
for k
in propkeys)
231 allowedProp = Configured._allowed + [k
for k
in propkeys]
233 klass=
type(classname+
'Conf', (Configured,), dict(_allowed=allowedProp, _propTypes=propTypes,
234 type=prefix+classname,_cppclass=cppclass) )
239 """Guess the type of the TProperty p.
240 p is a C++ instance of a TProperty.
242 This simply interpret the cpp name as set by cppyy...
244 clsname = p.__class__.__cpp_name__
246 typ = clsname[10:-1].
strip()
253 """A namespace able to automatically generate Configured when quering attributes :
254 Used to replace CompFactory so that expressions like :
255 tool = CompFactory.Trk.SomeTrkTool("tname", Prop=2.345)
256 tool = CompFactory.getComp("Trk::SomeTrkTool")("tname", Prop=2.345)
258 In the above 2 examples both CompFactory and Trk are ConfNameSpace
261 self.
prefix=name+
"::" if name !=
"" else ""
267 """generates a new Configured class for the C++ class ROOT.classname .
268 This implies there must be a dictionnary for classname.
272 c = self.__dict__.
get(classname,
None)
277 c=getattr(ROOT, self.
prefix+classname,
None)
280 print(
"JetAnalysisCommon ERROR : ",classname,
" is not a known C++ tool, alg, or namespace ")
283 if hasattr(c,
'getPropertyMgr'):
290 setattr(self, classname, conf)
303CompFactory.addNameSpaces(
'Analysis',
'Trk',
'Jet',
'Sim',)
306ComponentFactory = ModuleType(
"ComponentFactory")
307ComponentFactory.CompFactory = CompFactory
309ComponentFactory.isComponentAccumulatorCfg =
lambda :
True
316CFElements = ModuleType(
"CFElements")
317CFElements.parOR = parOR
323 """Provdide similar interface than AthenaConfiguration.ComponentAccumulator and also very simplistic
324 merging of list of algorithms
331 return iter(self.
algs)
337 setattr(self, alg._name, alg)
339 def merge(self, ca, sequenceName=""):
340 myTNs =
set( alg.typeAndName()
for alg
in self.
algs)
342 tn = alg.typeAndName()
344 self.
algs.append(alg)
345 setattr(self, alg._name, alg)
348ComponentAccumulator = ModuleType(
"ComponentAccumulator")
357 """a little configuration function added from the python module JetAnalysisCommon.py to easily schedule
358 # a list of Configured algs as defined by this module."""
360 job.algsAdd( alg.asAnaAlg() )
362ROOT.EL.Job.addManyAlgs = addManyAlgs
369JetAnalysisCommon = sys.modules[__name__]
372sys.modules[
'AthenaConfiguration.ComponentFactory'] = JetAnalysisCommon.ComponentFactory
373sys.modules[
'AthenaConfiguration.ComponentAccumulator'] = JetAnalysisCommon.ComponentAccumulator
374sys.modules[
'AthenaCommon.CFElements'] = JetAnalysisCommon.CFElements
378 """Allows to ignore JetRecTools in case this package is not checked out on top of AnalysisBase"""
379 sys.modules[
'JetRecTools'] = ModuleType(
'JetRecTools')
380 sys.modules[
'JetRecTools.JetRecToolsConfig'] = ModuleType(
'JetRecToolsConfig')
387import JetRecConfig.JetRecConfig
as JetRecConfig
396JetRecConfig.JetRecCfg_original = JetRecConfig.JetRecCfg
398 """Builds the algs with JetRecConfig.JetRecCfg and then make sure
399 they are in proper order.
400 Re-ordering is done manually, according to various input alg type.
402 res = JetRecConfig.JetRecCfg_original(flags, jetdef , returnFinalJetDef)
404 acc , _ = res
if returnFinalJetDef
else (res,
None)
409 if not hasattr(ROOT,
'EventDensityAthAlg'):
411 evtDensityAlgs = [ (i,alg)
for (i,alg)
in enumerate(algs)
if alg._cppclass == ROOT.EventDensityAthAlg ]
412 pjAlgs = [ (i,alg)
for (i,alg)
in enumerate(algs)
if alg._cppclass == ROOT.PseudoJetAlgorithm ]
414 for i,edalg
in evtDensityAlgs:
415 edInput = edalg.EventDensityTool.InputContainer
416 for j,pjalg
in pjAlgs:
418 if edInput == pjalg.OutputContainer:
419 pairsToswap.append( (i,j) )
420 for (i,j)
in pairsToswap:
421 algs[i], algs[j] = algs[j], algs[i]
429JetRecConfig.JetRecCfg = JetRecCfg_reorder
void print(char *figname, TCanvas *c1)
__init__(self, name="ca")
addSequence(self, seqName)
addEventAlgo(self, alg, primary=False, sequenceName="")
assignAllProperties(self, anaAlg)
__init__(self, key, conflist, parent)
addNameSpaces(self, *nsList)
bool add(const std::string &hname, TKey *tobj)
T * get(TKey *tobj)
get a TObject* from a TKey* (why can't a TObject be a TKey?)
JetRecCfg_reorder(jetdef, flags, returnFinalJetDef=False)
generateConfigured(classname, cppclass, prefix="")