-
Notifications
You must be signed in to change notification settings - Fork 0
/
core.py
307 lines (270 loc) · 11.2 KB
/
core.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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
"""
NAME
pyframe.core
DESCRIPTION
These are the core base-classes of the new re-vamped
pyframe framework.
AUTHORS
Ryan Reece <[email protected]>
Alex Tuna <[email protected]>
Will Davey <[email protected]>
COPYRIGHT
Copyright 2010 The authors
License: GPL <http://www.gnu.org/licenses/gpl.html>
SEE ALSO
ROOT <http://root.cern.ch>
2015-05-26
"""
## std imports
import json
import time
import logging
log = logging.getLogger(__name__)
log.setLevel(logging.INFO)
## my imports
import pyrootutils
#------------------------------------------------------------------------------
# Algorithm class
#------------------------------------------------------------------------------
class Algorithm(object):
"""
A process to execute event-by-event in an analysis. A user should write
classes that inherit from Algorithm, implementing the initialize(),
finalize(), and execute() methods as needed.
"""
#_________________________________________________________________________
def __init__(self, name=None, is_filter=False):
# initialized here
self.name = name or self.__class__
self.is_filter = is_filter
# initialized in +=
self.parent = None
self.config = None
self.store = None
self.chain = None
#_________________________________________________________________________
def initialize(self):
"""
Override this method in your derived class as you need.
"""
return None
#_________________________________________________________________________
def finalize(self):
"""
Override this method in your derived class as you need.
"""
return None
#_________________________________________________________________________
def execute(self):
"""
Override this method in your derived class as you need.
"""
return True
#_________________________________________________________________________
def hist(self, name, decl, dir=''):
"""
TODO: write a docstring."
"""
return self.parent._hist_manager.hist(name, decl, dir)
#------------------------------------------------------------------------------
# EventLoop class
#------------------------------------------------------------------------------
class EventLoop(object):
"""
TODO: write a docstring.
"""
#__________________________________________________________________________
def __init__(self, name='myloop', config=None, tree='tree'):
self.name = name
self.config = config or dict() ## information persists
self.store = dict() ## information cleared event-by-event
self._algorithms = list()
self.chain = pyrootutils.TreeReader(tree)
self._hist_manager = pyrootutils.HistManager()
self._progress_interval = 100
self._n_events_processed = 0
# self.quiet = False
## configure logging
timestamp = time.strftime("%Y-%m-%d-%Hh%M")
logging.basicConfig(
filename="%s.%s.log" % (name, timestamp),
filemode="w",
level=logging.INFO,
format="[%(asctime)s %(name)-16s %(levelname)-7s] %(message)s",
datefmt="%Y-%m-%d %H:%M:%S",
)
#_________________________________________________________________________
def __iadd__(self, alg):
"""
The user should use this operator to schedule Algorithms to the
EventLoop.
"""
algs_to_add = []
try:
iterator = iter(alg)
except TypeError:
# not iterable
algs_to_add.append(alg)
else:
# iterable
algs_to_add.extend(alg)
for a in algs_to_add:
a.parent = self # set a reference to this event loop
a.config = self.config
a.store = self.store
a.chain = self.chain
self._algorithms.append(a)
return self
#_________________________________________________________________________
def add_input_files(self, input_files):
"""
TODO: write a docstring."
"""
self.chain.add_files(input_files)
#_________________________________________________________________________
def hist(self, name, decl, dir=''):
"""
TODO: write a docstring."
"""
return self._hist_manager.hist(name, decl, dir)
#_________________________________________________________________________
def run(self, min_entry=0, max_entry=-1):
"""
This is the CPU-consuming function call that runs the event-loop.
The user can optionally specify the event range to run over.
"""
log.info("EventLoop.run: %s" % self.name)
n_entries = self.chain.get_entries()
if max_entry < 0:
max_entry = n_entries
else:
max_entry = min(max_entry, n_entries)
## initialize
self.initialize()
## do the event-loop
log_line = 'EventLoop.run: running on event %i to %i' % (min_entry, max_entry)
log.info(log_line)
print log_line
# if not self.quiet:
# progbar = progressbar.ProgressBar(width=24, block="=", empty=" ", min=min_entry, max=max_entry)
rate = 0.0
minutes_remaining = -1.0
progress_time = time.clock()
for i_entry in xrange(min_entry, max_entry):
## progress bar and log
if i_entry % self._progress_interval == 0 or i_entry == max_entry-1:
temp_progress_time = time.clock()
if temp_progress_time-progress_time > 0.0:
rate = float(self._progress_interval)/(temp_progress_time-progress_time) if self._n_events_processed else 0.0
else:
rate = 0.0
minutes_remaining = float(max_entry-i_entry)/float(rate)/60.0 if rate else -1.0
# if not self.quiet:
# digits = len(str(max_entry))
# progbar.update(i_entry+1, "[ %*s / %*s ] @ %.1f Hz (time remaining: %.1fm)" % (digits, i_entry+1, digits, max_entry, rate, minutes_remaining))
progress_time = temp_progress_time
if i_entry != 0 and (i_entry % 1000 == 0 or i_entry == max_entry-1):
log_line = 'EventLoop.run: event: %12i @ %10.1f Hz (time remaining: %5.1fm)' % (i_entry, rate, minutes_remaining)
log.info(log_line)
print log_line
## GetEntry and execute algs
self.chain.get_entry(i_entry)
self.execute()
# finalize
self.finalize()
#-------------------------------------------------------------------------
# The user should not have to use the EventLoop functions below.
#-------------------------------------------------------------------------
#_________________________________________________________________________
def initialize(self):
log.debug('EventLoop.initialize: %s' % self.name)
## begin timers
self._timing = {}
self._ncalls = {}
## initialize algs
for alg in self._algorithms:
_time = time.time()
alg.initialize()
_time = time.time()-_time
self._timing['initialize_%s' % alg.name] = _time
self._ncalls['initialize_%s' % alg.name] = 1
self._timing['execute_%s' % alg.name] = _time
self._ncalls['execute_%s' % alg.name] = 0
log.debug('initialized %s' % alg.name)
## reset branches
self.chain.reset_branches()
## print config
s_config = {}
for key, val in self.config.iteritems():
if isinstance(val, (int, long, float, complex, str, list)):
s_config[key] = val
else:
s_config[key] = str(val)
log.info('config =\n%s' % json.dumps(s_config, sort_keys=True, indent=4) )
#_________________________________________________________________________
def execute(self):
for alg in self._algorithms:
_time = time.time()
result = alg.execute()
_time = time.time()-_time
## bookkeep runtimes
if 'execute_%s' % alg.name in self._timing:
self._timing['execute_%s' % alg.name] += _time
self._ncalls['execute_%s' % alg.name] += 1
else:
self._timing['execute_%s' % alg.name] = _time
self._ncalls['execute_%s' % alg.name] = 1
## treat filter
if alg.is_filter:
if not result:
self.store.clear()
self._n_events_processed += 1
return False
self.store.clear()
self._n_events_processed += 1
return True
#_________________________________________________________________________
def finalize(self):
log.debug('EventLoop.finalize: %s' % self.name)
## finalize algs
for alg in self._algorithms:
_time = time.time()
alg.finalize()
_time = time.time()-_time
self._timing['finalize_%s' % alg.name] = _time
self._ncalls['finalize_%s' % alg.name] = 1
log.debug('finalized %s' % alg.name)
## time summary
log.info('ALGORITHM TIME SUMMARY\n' + self.get_time_summary())
## write histograms
if len(self._hist_manager._hists) > 0:
outfile = '%s.hists.root' % self.name
log.info('Writing histograms to %s ...' % outfile)
n_hists = self._hist_manager.write_hists(outfile)
log.info(' %i histograms written.' % n_hists)
#_________________________________________________________________________
def get_time_summary(self):
s = '\n'
s += '%3s %-40s %8s %8s %10s %8s\n' % ('#', 'ALGORITHM', 'TIME [s]', 'CALLS', 'RATE [Hz]', 'FRACTION')
## timing per method
for method in ['initialize', 'execute', 'finalize']:
s += ' %s %s %s\n' % ('-'*25, method, '-'*(79-25-len(method)))
## timing method summary
timingSum = sum([self._timing['%s_%s' % (method,alg.name)] for alg in self._algorithms])
# ncalls = self._n_events_processed if method == 'execute' else 1
ncalls = self._ncalls['%s_%s' % (method,alg.name)] if method == 'execute' else 1
rate = ncalls / timingSum if timingSum else 0.0
s += '%3s %-40s %8.2f %8i %10.1f %8.3f\n' % ('', 'Sum', timingSum, ncalls, rate, 1.00)
if not timingSum:
continue
## timing per alg
for i_alg, alg in enumerate(self._algorithms):
if '%s_%s' % (method, alg.name) in self._timing:
timing = self._timing['%s_%s' % (method, alg.name)]
ncalls = self._ncalls['%s_%s' % (method, alg.name)]
rate = ncalls / timing if timing else -1
fraction = timing / timingSum
s += '%3i %-40s %8.2f %8i %10.1f %8.3f\n' % (i_alg, alg.name, timing, ncalls, rate, fraction)
else:
log.warning(""" %s does not have runtime statistics. Oops!""" % (alg.name) )
return s