10 from platform
import platform
12 from event
import Event
16 '''The Looper creates a Setup object to hold information relevant during
17 the whole process, such as the process configuration obtained from
18 the configuration file, or services that can be used by several analyzers.
20 The user may freely attach new information to the setup object,
21 as long as this information is relevant during the whole process.
22 If the information is event specific, it should be attached to the event
27 Create a Setup object.
31 config: configuration object from the configuration file
33 services: dictionary of services indexed by service name.
34 The service name has the form classObject_instanceLabel
36 <base_heppy_path>.framework.services.tfile.TFileService_myhists
37 To find out about the service name of a given service,
38 load your configuration file in python, and print the service.
44 '''Stop all services'''
45 for service
in self.services.values():
50 """Creates a set of analyzers, and schedules the event processing."""
55 firstEvent=0, nPrint=0, timeReport=
False ):
56 """Handles the processing of an event sample.
57 An Analyzer is built for each Config.Analyzer present
58 in sequence. The Looper can then be used to process an event,
59 or a collection of events.
62 name : name of the Looper, will be used as the output directory name
63 config : process configuration information, see Config
64 nEvents : number of events to process. Defaults to all.
65 firstEvent : first event to process. Defaults to the first one.
66 nPrint : number of events to print at the beginning
72 self.logger.addHandler(logging.FileHandler(
'/'.
join([self.
name,
74 self.logger.addHandler( logging.StreamHandler(sys.stdout) )
85 tree_name = self.cfg_comp.tree_name
86 if len(self.cfg_comp.files)==0:
87 errmsg =
'please provide at least an input file in the files attribute of this component\n' + str(self.
cfg_comp)
88 raise ValueError( errmsg )
89 self.
events = config.events_class(self.cfg_comp.files, tree_name)
90 if hasattr(self.
cfg_comp,
'fineSplit'):
91 fineSplitIndex, fineSplitFactor = self.cfg_comp.fineSplit
92 if fineSplitFactor > 1:
93 if len(self.cfg_comp.files) != 1:
94 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)
95 totevents =
min(len(self.
events),int(nEvents))
if (nEvents
and int(nEvents)
not in [-1,0])
else len(self.
events)
96 self.
nEvents = int(ceil(totevents/float(fineSplitFactor)))
102 for cfg_serv
in config.services:
103 service = self.
_build(cfg_serv)
104 services[cfg_serv.name] = service
111 theClass = cfg.class_object
118 while True and index < 2000:
125 tmpname =
'%s_%d' % (name, index)
127 raise ValueError(
"More than 2000 output folder with same name or 2000 attempts failed, please clean-up, change name or check permissions")
132 """Loop on a given number of events.
134 At the beginning of the loop,
135 Analyzer.beginLoop is called for each Analyzer.
136 At each event, self.process is called.
137 At the end of the loop, Analyzer.endLoop is called.
142 if nEvents
is None or int(nEvents) > len(self.
events) :
143 nEvents = len(self.
events)
145 nEvents = int(nEvents)
148 'starting loop at event {firstEvent} '\
149 'to process {eventSize} events.'.
format(firstEvent=firstEvent,
150 eventSize=eventSize))
151 self.logger.warning( str( self.
cfg_comp ) )
153 analyzer.beginLoop(self.
setup)
155 for iEv
in range(firstEvent, firstEvent+eventSize):
160 if not hasattr(self,
'start_time'):
172 print 'Stopped loop following a UserWarning exception'
174 analyzer.endLoop(self.
setup)
175 warn = self.logger.warning
179 warn(
'number of events processed: {nEv}'.
format(nEv=iEv+1))
182 """Run event processing for all analyzers in the sequence.
184 This function is called by self.loop,
185 but can also be called directly from
186 the python interpreter, to jump to a given event.
190 for i,analyzer
in enumerate(self.
analyzers):
191 if not analyzer.beginLoopCalled:
193 start = timeit.default_timer()
194 ret = analyzer.process( self.
event )
198 self.
timeReport[i][
'time'] += timeit.default_timer() - start
200 return (
False, analyzer.name)
201 return (
True, analyzer.name)
204 """Writes all analyzers.
206 See Analyzer.Write for more information.
209 analyzer.write(self.
setup)
214 print "\n ---- TimeReport (all times in ms; first evt is skipped) ---- "
215 print "%9s %9s %9s %9s %s" % (
"processed",
"all evts",
"time/proc",
" time/all",
"analyer")
216 print "%9s %9s %9s %9s %s" % (
"---------",
"--------",
"---------",
"---------",
"-------------")
218 print "%9d %9d %10.2f %10.2f %s" % ( rep[
'events'], allev, 1000*rep[
'time']/(rep[
'events']-1)
if rep[
'events']>1
else 0, 1000*rep[
'time']/(allev-1)
if allev > 1
else 0, ana.name)
223 if __name__ ==
'__main__':
228 if len(sys.argv) == 2 :
229 cfgFileName = sys.argv[1]
230 pckfile = open( cfgFileName,
'r' )
231 config = pickle.load( pckfile )
232 comp = config.components[0]
233 events_class = config.events_class
234 elif len(sys.argv) == 3 :
235 cfgFileName = sys.argv[1]
236 file = open( cfgFileName,
'r' )
237 cfg = imp.load_source( 'cfg', cfgFileName, file)
238 compFileName = sys.argv[2]
239 pckfile = open( compFileName,
'r' )
240 comp = pickle.load( pckfile )
241 cfg.config.components=[comp]
242 events_class = cfg.config.events_class
244 looper = Looper( 'Loop', cfg.config,nPrint = 5)
static std::string join(char **cmd)
if(conf.exists("allCellsPositionCalc"))