TopoAlgorithms.py 3.99 KB
Newer Older
1
2
3
4
5
6
7
# Copyright (C) 2002-2019 CERN for the benefit of the ATLAS collaboration

from collections import OrderedDict as odict
from operator import attrgetter
from enum import Enum

from AthenaCommon.Logging import logging
8
log = logging.getLogger(__name__)
9
10
11
12
13
14
15
16
17
18
19
20

from .TopoAlgos import DecisionAlgo, MultiplicityAlgo, SortingAlgo

class AlgType(Enum):
    SORT = ('sortingAlgorithms')
    DEC = ('decisionAlgorithms') 
    MULT = ('multiplicityAlgorithms')

    def __init__(self, key):
        self.key = key
    
class AlgCategory(Enum):
Joerg Stelzer's avatar
Joerg Stelzer committed
21
22
23
24
    TOPO = (1, 'TOPO', 'new topo', 'TopoAlgoDef')
    MUCTPI = (2, 'MUTOPO', 'muctpi topo', 'TopoAlgoDefMuctpi')
    LEGACY = (3, 'R2TOPO', 'legacy topo', 'TopoAlgoDefLegacy')
    MULTI = (4, 'MULTTOPO', 'multiplicity topo', 'TopoAlgoDefMultiplicity')
25

Joerg Stelzer's avatar
Joerg Stelzer committed
26
    def __init__(self, _, key, desc, defFile ):
27
28
29
        self.key = key
        self.prefix = key + '_' if key else ''
        self.desc = desc
Joerg Stelzer's avatar
Joerg Stelzer committed
30
        self.defFile = defFile
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59

    def __str__(self):
        return self.desc

    @staticmethod
    def getAllCategories():
        return [ AlgCategory.TOPO, AlgCategory.MUCTPI, AlgCategory.MULTI, AlgCategory.LEGACY ]
            
    @staticmethod
    def getCategoryFromBoardName(boardName):
        if 'muctpi' in boardName.lower():
            currentTopoCategory = AlgCategory.MUCTPI
        elif 'topo' in boardName.lower():
            if 'legacy' in boardName.lower():
                currentTopoCategory = AlgCategory.LEGACY
            else:
                currentTopoCategory = AlgCategory.TOPO
        else:
            raise RuntimeError("Board %s is not a topo board" % boardName )
        return currentTopoCategory


class MenuTopoAlgorithmsCollection(object):

    def __init__(self):
        # all algos that are in menu (new and legacy)
        self.topoAlgos = odict()
        for cat in AlgCategory:
            self.topoAlgos[cat] = odict()
60
61
62
63
64
            if cat in [AlgCategory.TOPO, AlgCategory.MUCTPI, AlgCategory.LEGACY]:
                self.topoAlgos[cat][AlgType.DEC] = odict()
                self.topoAlgos[cat][AlgType.SORT] = odict()
            elif cat in [AlgCategory.MULTI]:
                self.topoAlgos[cat][AlgType.MULT] = odict()
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88

    def addAlgo(self, algo, category):
        if type(category) != AlgCategory:
            raise RuntimeError( "No category is provided when adding topo algo %s to menu" % algo.name)

        if isinstance(algo,DecisionAlgo):
            algType = AlgType.DEC
        elif isinstance(algo, SortingAlgo):
            algType = AlgType.SORT
        elif isinstance(algo, MultiplicityAlgo):
            algType = AlgType.MULT
        else:
            raise RuntimeError("Trying to add topo algorithm %s of unknown type %s to the menu" % (algo.name, type(algo)))

        if algType not in self.topoAlgos[category]:
            self.topoAlgos[category][algType] = odict()

        if algo.name in self.topoAlgos[category][algType]:
            raise RuntimeError("Trying to add topo algorithm %s a second time" % algo.name)

        self.topoAlgos[category][algType][algo.name] = algo


    def json(self):
89
90
91
92
93
94
95
        def idGenerator(usedIds, start):
            while True:
                while start in usedIds:
                    start += 1
                yield start
                start += 1

96
97
98
99
100
101
102
103
        confObj = odict()
        for cat in self.topoAlgos:
            confObj[cat.key] = odict()
            for typ in self.topoAlgos[cat]:
                confObj[cat.key][typ.key] = odict()
                for alg in sorted(self.topoAlgos[cat][typ].values(), key=attrgetter('name')):
                    confObj[cat.key][typ.key][alg.name] = alg.json()

104
105
106
107
108
109
110
                # set unspecified algoIds to a unique value
                usedAlgIds = set([x["algId"] for x in confObj[cat.key][typ.key].values() if x["algId"]>=0])
                autoId = idGenerator(usedAlgIds, 0)
                for algJsonEntry in confObj[cat.key][typ.key].values():
                    if algJsonEntry["algId"] < 0:
                        algJsonEntry["algId"] = next(autoId)

111
        return confObj