/
SpecificationBuilder_cfi.py
152 lines (132 loc) · 4.27 KB
/
SpecificationBuilder_cfi.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
import FWCore.ParameterSet.Config as cms
from copy import deepcopy
# this is a pure Python module to build the deeply nested PSets that describe a
# SummationSpecification.
# The C++ code assumes the form is fully correct, so you should always use this,
# which outputs a valid form.
# these need to stay in sync with the C++ enums. TODO: Can we use Python3 enum or sth.?
NO_TYPE = cms.int32(0)
GROUPBY = cms.int32(1)
EXTEND_X = cms.int32(2)
EXTEND_Y = cms.int32(3)
COUNT = cms.int32(4)
REDUCE = cms.int32(5)
SAVE = cms.int32(6)
CUSTOM = cms.int32(7)
NO_STAGE = cms.int32(0)
FIRST = cms.int32(1)
STAGE1 = cms.int32(2)
STAGE1_2 = cms.int32(3)
STAGE2 = cms.int32(4)
def val(maybecms):
if hasattr(maybecms, "value"):
return maybecms.value()
else:
return maybecms
def parent(path):
parts = val(path).split("/")
return "/".join(parts[0:len(parts)-1])
# do not change values here, Pass in a PSet instead
DefaultConf = cms.PSet(enabled = cms.bool(True))
class Specification(cms.PSet):
def __init__(self, conf = DefaultConf):
super(Specification, self).__init__()
self.spec = cms.VPSet()
self.conf = conf
self._activeColumns = set()
self._state = FIRST
def __deepcopy__(self, memo):
# override deepcopy to not copy .conf: it should remain a reference
# w/o this it is not cleanly possible to build a per-module switch.
t = Specification(self.conf)
t.spec = deepcopy(self.spec, memo)
return t
def groupBy(self, cols, mode = "SUM"):
cnames = val(cols).split("/")
if mode == "SUM":
if self._state == STAGE1:
if not "Event" in self.spec[0].columns:
raise Exception("Only per-event counting supported for step1.")
self.spec[0].columns.remove("Event"); # per-Event groupng is done automatically
self._state = STAGE1_2
t = GROUPBY
cstrings = cms.vstring(cnames)
elif mode == "EXTEND_X" or mode == "EXTEND_Y":
if self._state == FIRST:
raise Exception("First grouping must be SUM")
cname = self._activeColumns.difference(cnames)
if len(cname) != 1:
raise Exception("EXTEND must drop exactly one column.")
cstrings = cms.vstring(cname)
if mode == "EXTEND_X":
t = EXTEND_X
else:
t = EXTEND_Y
else:
raise Exception("Summation mode %s unknown" % mode)
self._activeColumns = set(cnames)
self._lastColumns = cnames
self._lastMode = mode
self.spec.append(cms.PSet(
type = t,
stage = self._state,
columns = cstrings,
arg = cms.string(mode)
))
if self._state == FIRST:
self._state = STAGE1
return self
def save(self):
if self._state == FIRST:
raise Exception("First statement must be groupBy.")
self.spec.append(cms.PSet(
type = SAVE,
stage = self._state,
columns = cms.vstring(),
arg = cms.string("")
))
self._state = STAGE2
return self
def custom(self, arg = ""):
if self.spec[-1].type != SAVE:
# this is not obvious but needed for now, to avoid memory management
# trouble with bare TH1's. After save all data is in MEs which are save.
self.save()
if self._state != STAGE2:
raise Exception("Custom processing exists only in Harvesting.")
self.spec.append(cms.PSet(
type = CUSTOM,
stage = self._state,
columns = cms.vstring(),
arg = cms.string(arg)
))
return self
def reduce(self, sort):
if self._state == FIRST:
raise Exception("First statement must be groupBy.")
if sort != "MEAN" and sort != "COUNT":
raise Exception("reduction type %s not known" % sort)
if self._state == STAGE1:
if sort != "COUNT":
raise Exception("reduction type %s not allowed in step1" % sort)
t = COUNT
else:
t = REDUCE
self.spec.append(cms.PSet(
type = t,
stage = self._state,
columns = cms.vstring(),
arg = cms.string(sort)
))
return self
def saveAll(self):
self.save()
columns = self._lastColumns
for i in range(len(columns)-1, 0, -1):
cols = columns[0:i]
self.groupBy("/".join(cols), self._lastMode)
self.save()
return self
# this is used for serialization, and for that this is just a PSet.
def pythonTypeName(self):
return 'cms.PSet';