10 from event
import Event
14 '''The Looper creates a Setup object to hold information relevant during
15 the whole process, such as the process configuration obtained from
16 the configuration file, or services that can be used by several analyzers.
18 The user may freely attach new information to the setup object,
19 as long as this information is relevant during the whole process.
20 If the information is event specific, it should be attached to the event
25 Create a Setup object.
29 config: configuration object from the configuration file
31 services: dictionary of services indexed by service name.
32 The service name has the form classObject_instanceLabel
34 <base_heppy_path>.framework.services.tfile.TFileService_myhists
35 To find out about the service name of a given service,
36 load your configuration file in python, and print the service.
42 '''Stop all services'''
43 for service
in self.services.values():
48 """Creates a set of analyzers, and schedules the event processing."""
57 """Handles the processing of an event sample.
58 An Analyzer is built for each Config.Analyzer present
59 in sequence. The Looper can then be used to process an event,
60 or a collection of events.
63 name : name of the Looper, will be used as the output directory name
64 config : process configuration information, see Config
65 nEvents : number of events to process. Defaults to all.
66 firstEvent : first event to process. Defaults to the first one.
67 nPrint : number of events to print at the beginning
73 self.logger.addHandler(logging.FileHandler(
'/'.
join([self.
name,
75 self.logger.propagate =
False
77 self.logger.addHandler( logging.StreamHandler(sys.stdout) )
88 tree_name = self.cfg_comp.tree_name
89 if len(self.cfg_comp.files)==0:
90 errmsg =
'please provide at least an input file in the files attribute of this component\n' + str(self.
cfg_comp)
91 raise ValueError( errmsg )
92 if hasattr(config,
"preprocessor")
and config.preprocessor
is not None :
95 print self.cfg_comp.files,self.cfg_comp.options
96 self.
events = config.events_class(self.cfg_comp.files, tree_name,options=self.cfg_comp.options)
98 self.
events = config.events_class(self.cfg_comp.files, tree_name)
99 if hasattr(self.
cfg_comp,
'fineSplit'):
100 fineSplitIndex, fineSplitFactor = self.cfg_comp.fineSplit
101 if fineSplitFactor > 1:
102 if len(self.cfg_comp.files) != 1:
103 raise RuntimeError,
"Any component with fineSplit > 1 is supposed to have just a single file, while %s has %s" % (self.cfg_comp.name, self.cfg_comp.files)
104 totevents =
min(len(self.
events),int(nEvents))
if (nEvents
and int(nEvents)
not in [-1,0])
else len(self.
events)
105 self.
nEvents = int(ceil(totevents/float(fineSplitFactor)))
113 for cfg_serv
in config.services:
114 service = self.
_build(cfg_serv)
115 services[cfg_serv.name] = service
122 theClass = cfg.class_object
129 while True and index < 2000:
136 tmpname =
'%s_%d' % (name, index)
138 raise ValueError(
"More than 2000 output folder with same name or 2000 attempts failed, please clean-up, change name or check permissions")
143 """Loop on a given number of events.
145 At the beginning of the loop,
146 Analyzer.beginLoop is called for each Analyzer.
147 At each event, self.process is called.
148 At the end of the loop, Analyzer.endLoop is called.
153 if nEvents
is None or int(nEvents) > len(self.
events) :
154 nEvents = len(self.
events)
156 nEvents = int(nEvents)
159 'starting loop at event {firstEvent} '\
160 'to process {eventSize} events.'.
format(firstEvent=firstEvent,
161 eventSize=eventSize))
162 self.logger.info( str( self.
cfg_comp ) )
164 analyzer.beginLoop(self.
setup)
166 for iEv
in range(firstEvent, firstEvent+eventSize):
171 if not hasattr(self,
'start_time'):
183 print 'Stopped loop following a UserWarning exception'
185 info = self.logger.info
186 info(
'number of events processed: {nEv}'.
format(nEv=iEv+1))
191 analyzer.endLoop(self.
setup)
194 warning = self.logger.warning
195 warning(
"\n ---- TimeReport (all times in ms; first evt is skipped) ---- ")
196 warning(
"%9s %9s %9s %9s %6s %s" % (
"processed",
"all evts",
"time/proc",
" time/all",
" [%] ",
"analyer"))
197 warning(
"%9s %9s %9s %9s %6s %s" % (
"---------",
"--------",
"---------",
"---------",
" -----",
"-------------"))
198 sumtime = sum(rep[
'time']
for rep
in self.
timeReport)
201 timePerProcEv = rep[
'time']/(rep[
'events']-1)
if rep[
'events'] > 1
else 0
202 timePerAllEv = rep[
'time']/(allev-1)
if allev > 1
else 0
203 fracAllEv = rep[
'time']/sumtime
204 warning(
"%9d %9d %10.2f %10.2f %5.1f%% %s" % ( rep[
'events'], allev, 1000*timePerProcEv, 1000*timePerAllEv, 100.0*fracAllEv, ana.name))
205 totPerProcEv = sumtime/(passev-1)
if passev > 1
else 0
206 totPerAllEv = sumtime/(allev-1)
if allev > 1
else 0
207 warning(
"%9s %9s %9s %9s %s" % (
"---------",
"--------",
"---------",
"---------",
"-------------"))
208 warning(
"%9d %9d %10.2f %10.2f %5.1f%% %s" % ( passev, allev, 1000*totPerProcEv, 1000*totPerAllEv, 100.0,
"TOTAL"))
212 """Run event processing for all analyzers in the sequence.
214 This function is called by self.loop,
215 but can also be called directly from
216 the python interpreter, to jump to a given event.
220 for i,analyzer
in enumerate(self.
analyzers):
221 if not analyzer.beginLoopCalled:
222 analyzer.beginLoop(self.
setup)
223 start = timeit.default_timer()
224 ret = analyzer.process( self.
event )
228 self.
timeReport[i][
'time'] += timeit.default_timer() - start
230 return (
False, analyzer.name)
232 self.logger.info( self.event.__str__() )
233 return (
True, analyzer.name)
236 """Writes all analyzers.
238 See Analyzer.Write for more information.
241 analyzer.write(self.
setup)
245 if __name__ ==
'__main__':
250 if len(sys.argv) == 2 :
251 cfgFileName = sys.argv[1]
252 pckfile = open( cfgFileName,
'r' )
253 config = pickle.load( pckfile )
254 comp = config.components[0]
255 events_class = config.events_class
256 elif len(sys.argv) == 3 :
257 cfgFileName = sys.argv[1]
258 file = open( cfgFileName,
'r' )
259 cfg = imp.load_source( 'cfg', cfgFileName, file)
260 compFileName = sys.argv[2]
261 pckfile = open( compFileName,
'r' )
262 comp = pickle.load( pckfile )
263 cfg.config.components=[comp]
264 events_class = cfg.config.events_class
266 looper = Looper( 'Loop', cfg.config,nPrint = 5)
static std::string join(char **cmd)
if(conf.exists("allCellsPositionCalc"))