CMS 3D CMS Logo

List of all members | Public Member Functions | Public Attributes | Private Member Functions
looper.Looper Class Reference
Inheritance diagram for looper.Looper:

Public Member Functions

def __init__ (self, name, config, nEvents=None, firstEvent=0, nPrint=0, timeReport=False, quiet=False, memCheckFromEvent=-1)
 
def loop (self)
 
def process (self, iEv)
 
def write (self)
 

Public Attributes

 analyzers
 
 cfg_comp
 
 classes
 
 config
 
 event
 
 events
 
 firstEvent
 
 iEvent
 
 logger
 
 memLast
 
 memReportFirstEvent
 
 name
 
 nEvents
 
 nPrint
 
 outDir
 
 setup
 
 start_time
 
 start_time_event
 
 timeReport
 

Private Member Functions

def _build (self, cfg)
 
def _prepareOutput (self, name)
 

Detailed Description

Creates a set of analyzers, and schedules the event processing.

Definition at line 49 of file looper.py.

Constructor & Destructor Documentation

def looper.Looper.__init__ (   self,
  name,
  config,
  nEvents = None,
  firstEvent = 0,
  nPrint = 0,
  timeReport = False,
  quiet = False,
  memCheckFromEvent = -1 
)
Handles the processing of an event sample.
An Analyzer is built for each Config.Analyzer present
in sequence. The Looper can then be used to process an event,
or a collection of events.

Parameters:
name    : name of the Looper, will be used as the output directory name
config  : process configuration information, see Config
nEvents : number of events to process. Defaults to all.
firstEvent : first event to process. Defaults to the first one.
nPrint  : number of events to print at the beginning

Definition at line 59 of file looper.py.

59  memCheckFromEvent=-1):
60  """Handles the processing of an event sample.
61  An Analyzer is built for each Config.Analyzer present
62  in sequence. The Looper can then be used to process an event,
63  or a collection of events.
64 
65  Parameters:
66  name : name of the Looper, will be used as the output directory name
67  config : process configuration information, see Config
68  nEvents : number of events to process. Defaults to all.
69  firstEvent : first event to process. Defaults to the first one.
70  nPrint : number of events to print at the beginning
71  """
72 
73  self.config = config
74  self.name = self._prepareOutput(name)
75  self.outDir = self.name
76  self.logger = logging.getLogger( self.name )
77  self.logger.addHandler(logging.FileHandler('/'.join([self.name,
78  'log.txt'])))
79  self.logger.propagate = False
80  if not quiet:
81  self.logger.addHandler( logging.StreamHandler(sys.stdout) )
82 
83  self.cfg_comp = config.components[0]
84  self.classes = {}
85  self.analyzers = map( self._build, config.sequence )
86  self.nEvents = nEvents
87  self.firstEvent = firstEvent
88  self.nPrint = int(nPrint)
89  self.timeReport = [ {'time':0.0,'events':0} for a in self.analyzers ] if timeReport else False
90  self.memReportFirstEvent = memCheckFromEvent
91  self.memLast=0
92  tree_name = None
93  if( hasattr(self.cfg_comp, 'tree_name') ):
94  tree_name = self.cfg_comp.tree_name
95  if len(self.cfg_comp.files)==0:
96  errmsg = 'please provide at least an input file in the files attribute of this component\n' + str(self.cfg_comp)
97  raise ValueError( errmsg )
98  if hasattr(config,"preprocessor") and config.preprocessor is not None :
99  self.cfg_comp = config.preprocessor.run(self.cfg_comp,self.outDir,firstEvent,nEvents)
100  if hasattr(self.cfg_comp,"options"):
101  print self.cfg_comp.files,self.cfg_comp.options
102  self.events = config.events_class(self.cfg_comp.files, tree_name,options=self.cfg_comp.options)
103  else :
104  self.events = config.events_class(self.cfg_comp.files, tree_name)
105  if hasattr(self.cfg_comp, 'fineSplit'):
106  fineSplitIndex, fineSplitFactor = self.cfg_comp.fineSplit
107  if fineSplitFactor > 1:
108  if len(self.cfg_comp.files) != 1:
109  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))
110  totevents = min(len(self.events),int(nEvents)) if (nEvents and int(nEvents) not in [-1,0]) else len(self.events)
111  self.nEvents = int(ceil(totevents/float(fineSplitFactor)))
112  self.firstEvent = firstEvent + fineSplitIndex * self.nEvents
113  if self.firstEvent + self.nEvents >= totevents:
114  self.nEvents = totevents - self.firstEvent
115  #print "For component %s will process %d events starting from the %d one, ending at %d excluded" % (self.cfg_comp.name, self.nEvents, self.firstEvent, self.nEvents + self.firstEvent)
116  # self.event is set in self.process
117  self.event = None
118  services = dict()
119  for cfg_serv in config.services:
120  service = self._build(cfg_serv)
121  services[cfg_serv.name] = service
122  # would like to provide a copy of the config to the setup,
123  # so that analyzers cannot modify the config of other analyzers.
124  # but cannot copy the autofill config.
125  self.setup = Setup(config, services)
126 
memReportFirstEvent
Definition: looper.py:90
T min(T a, T b)
Definition: MathUtil.h:58
def _build(self, cfg)
Definition: looper.py:127
static std::string join(char **cmd)
Definition: RemoteFile.cc:18
if(dp >Float(M_PI)) dp-
def _prepareOutput(self, name)
Definition: looper.py:132

Member Function Documentation

def looper.Looper._build (   self,
  cfg 
)
private

Definition at line 127 of file looper.py.

References analyzer.Analyzer.cfg_comp, looper.Looper.cfg_comp, and looper.Looper.outDir.

127  def _build(self, cfg):
128  theClass = cfg.class_object
129  obj = theClass( cfg, self.cfg_comp, self.outDir )
130  return obj
131 
def _build(self, cfg)
Definition: looper.py:127
def looper.Looper._prepareOutput (   self,
  name 
)
private

Definition at line 132 of file looper.py.

132  def _prepareOutput(self, name):
133  index = 0
134  tmpname = name
135  while True and index < 2000:
136  try:
137  # print 'mkdir', self.name
138  os.mkdir( tmpname )
139  break
140  except OSError:
141  index += 1
142  tmpname = '%s_%d' % (name, index)
143  if index == 2000:
144  raise ValueError( "More than 2000 output folder with same name or 2000 attempts failed, please clean-up, change name or check permissions")
145  return tmpname
146 
147 
def _prepareOutput(self, name)
Definition: looper.py:132
def looper.Looper.loop (   self)
Loop on a given number of events.

At the beginning of the loop, 
Analyzer.beginLoop is called for each Analyzer.
At each event, self.process is called.
At the end of the loop, Analyzer.endLoop is called.

Definition at line 148 of file looper.py.

References looper.Looper.analyzers, Config.Process.analyzers, analyzer.Analyzer.cfg_comp, looper.Looper.cfg_comp, eventsfwlite.Events.events, generateEDF.LumiInfo.events, emtf::Forest.events, HitEff.events, ME::Header.events, emtf::Node.events, looper.Looper.events, MatrixUtil.InputInfo.events, CosmicRateAnalyzer.events, options.HLTProcessOptions.events, FastTimerService::ResourcesPerModule.events, FastTimerService::ResourcesPerJob.events, JsonOutputProducer::JsonConfiguration.events, Printer.Printer.firstEvent, lhef::LHEReader.firstEvent, ApeEstimatorSummary.firstEvent, looper.Looper.firstEvent, LeptonRecoSkim.firstEvent, SiStripMonitorPedestals.firstEvent, IsoTrackCalib.firstEvent, SiStripMonitorDigi.firstEvent, SiStripMonitorCluster.firstEvent, SiStripMonitorTrack.firstEvent, createfilelist.int, HiggsTo2GammaSkim.nEvents, HiggsToZZ4LeptonsSkim.nEvents, JGJFilter.nEvents, CTPPSPixelDQMSource.nEvents, HiggsToZZ4LeptonsSkimEff.nEvents, MCDijetResonance.nEvents, EgammaProbeSelector.nEvents, QualityTester.nEvents, AlCaHcalNoiseProducer.nEvents, DTScalerInfoTask.nEvents, MCatNLOSource.nEvents, AlpgenHeader.nEvents, HeavyChHiggsToTauNuSkim.nEvents, looper.Looper.nEvents, DTLocalTriggerLutTask.nEvents, DTLocalTriggerBaseTask.nEvents, MuonIsolationDQM.nEvents, MatacqProducer::stats_t.nEvents, cscdqm::Configuration.nEvents, event.Event.setup, looper.Looper.setup, options.HLTProcessOptions.setup, and harvestTrackValidationPlots.str.

148  def loop(self):
149  """Loop on a given number of events.
150 
151  At the beginning of the loop,
152  Analyzer.beginLoop is called for each Analyzer.
153  At each event, self.process is called.
154  At the end of the loop, Analyzer.endLoop is called.
155  """
156  nEvents = self.nEvents
157  firstEvent = self.firstEvent
158  iEv = firstEvent
159  if nEvents is None or int(nEvents) > len(self.events) :
160  nEvents = len(self.events)
161  else:
162  nEvents = int(nEvents)
163  eventSize = nEvents
164  self.logger.info(
165  'starting loop at event {firstEvent} '\
166  'to process {eventSize} events.'.format(firstEvent=firstEvent,
167  eventSize=eventSize))
168  self.logger.info( str( self.cfg_comp ) )
169  for analyzer in self.analyzers:
170  analyzer.beginLoop(self.setup)
171  try:
172  for iEv in range(firstEvent, firstEvent+eventSize):
173  # if iEv == nEvents:
174  # break
175  if iEv%100 ==0:
176  # print 'event', iEv
177  if not hasattr(self,'start_time'):
178  print 'event', iEv
179  self.start_time = timeit.default_timer()
180  self.start_time_event = iEv
181  else:
182  print 'event %d (%.1f ev/s)' % (iEv, (iEv-self.start_time_event)/float(timeit.default_timer() - self.start_time))
183 
184  self.process( iEv )
185  if iEv<self.nPrint:
186  print self.event
187 
188  except UserWarning:
189  print 'Stopped loop following a UserWarning exception'
190 
191  info = self.logger.info
192  warning = self.logger.warning
193  warning('number of events processed: {nEv}'.format(nEv=iEv+1))
194  warning('')
195  info( self.cfg_comp )
196  info('')
197  for analyzer in self.analyzers:
198  analyzer.endLoop(self.setup)
199  if self.timeReport:
200  allev = max([x['events'] for x in self.timeReport])
201  warning("\n ---- TimeReport (all times in ms; first evt is skipped) ---- ")
202  warning("%9s %9s %9s %9s %6s %s" % ("processed","all evts","time/proc", " time/all", " [%] ", "analyer"))
203  warning("%9s %9s %9s %9s %6s %s" % ("---------","--------","---------", "---------", " -----", "-------------"))
204  sumtime = sum(rep['time'] for rep in self.timeReport)
205  passev = self.timeReport[-1]['events']
206  for ana,rep in zip(self.analyzers,self.timeReport):
207  timePerProcEv = rep['time']/(rep['events']-1) if rep['events'] > 1 else 0
208  timePerAllEv = rep['time']/(allev-1) if allev > 1 else 0
209  fracAllEv = rep['time']/sumtime
210  warning( "%9d %9d %10.2f %10.2f %5.1f%% %s" % ( rep['events'], allev, 1000*timePerProcEv, 1000*timePerAllEv, 100.0*fracAllEv, ana.name))
211  totPerProcEv = sumtime/(passev-1) if passev > 1 else 0
212  totPerAllEv = sumtime/(allev-1) if allev > 1 else 0
213  warning("%9s %9s %9s %9s %s" % ("---------","--------","---------", "---------", "-------------"))
214  warning("%9d %9d %10.2f %10.2f %5.1f%% %s" % ( passev, allev, 1000*totPerProcEv, 1000*totPerAllEv, 100.0, "TOTAL"))
215  warning("")
216  if hasattr(self.events, 'endLoop'): self.events.endLoop()
217  if hasattr(self.config,"preprocessor") and self.config.preprocessor is not None:
218  if hasattr(self.config.preprocessor,"endLoop"):
219  self.config.preprocessor.endLoop(self.cfg_comp)
220 
def loop(self)
Definition: looper.py:148
static const TGPicture * info(bool iBackgroundIsBlack)
OutputIterator zip(InputIterator1 first1, InputIterator1 last1, InputIterator2 first2, InputIterator2 last2, OutputIterator result, Compare comp)
def process(self, iEv)
Definition: looper.py:221
def looper.Looper.process (   self,
  iEv 
)
Run event processing for all analyzers in the sequence.

This function is called by self.loop,
but can also be called directly from
the python interpreter, to jump to a given event.

Definition at line 221 of file looper.py.

References TB06Reco.event, TB06RecoH2.event, core.AutoHandle.AutoHandle.event, ApeOverview.event, L1Analysis::L1AnalysisEventDataFormat.event, MuonPair.event, LHAupLesHouches.event, EcalPerEvtMatacqAnalyzer.event, WZInterestingEventSelector::event.event, EcalStatusAnalyzer.event, lhef::LHEProxy.event, MCatNLOSource.event, EcalMatacqAnalyzer.event, HitEff.event, L1TCaloLayer1RawToDigi.event, looper.Looper.event, EcalTestPulseAnalyzer.event, EcalABAnalyzer.event, EcalPerEvtLaserAnalyzer.event, edm::service::SimpleMemoryCheck::SignificantEvent.event, EcalLaserAnalyzer.event, EcalLaserAnalyzer2.event, FastTimerService::ResourcesPerJob.event, JsonOutputProducer::JsonEvent.event, SummaryOutputProducer::Event.event, eventsfwlite.Events.events, generateEDF.LumiInfo.events, emtf::Forest.events, ME::Header.events, HitEff.events, emtf::Node.events, looper.Looper.events, MatrixUtil.InputInfo.events, CosmicRateAnalyzer.events, options.HLTProcessOptions.events, FastTimerService::ResourcesPerModule.events, FastTimerService::ResourcesPerJob.events, JsonOutputProducer::JsonConfiguration.events, event.Event.setup, looper.Looper.setup, and options.HLTProcessOptions.setup.

Referenced by ConfigBuilder.ConfigBuilder.addExtraStream(), ConfigBuilder.ConfigBuilder.completeInputCommand(), ConfigBuilder.ConfigBuilder.doNotInlineEventContent(), ConfigBuilder.ConfigBuilder.PrintAllModules.leave(), ConfigBuilder.ConfigBuilder.prepare(), ConfigBuilder.ConfigBuilder.prepare_ALCA(), ConfigBuilder.ConfigBuilder.prepare_DQM(), ConfigBuilder.ConfigBuilder.prepare_HLT(), ConfigBuilder.ConfigBuilder.prepare_LHE(), ConfigBuilder.ConfigBuilder.prepare_PATFILTER(), ConfigBuilder.ConfigBuilder.prepare_VALIDATION(), ConfigBuilder.ConfigBuilder.renameHLTprocessInSequence(), ConfigBuilder.ConfigBuilder.renameInputTagsInSequence(), and ConfigBuilder.ConfigBuilder.scheduleSequence().

221  def process(self, iEv ):
222  """Run event processing for all analyzers in the sequence.
223 
224  This function is called by self.loop,
225  but can also be called directly from
226  the python interpreter, to jump to a given event.
227  """
228  self.event = Event(iEv, self.events[iEv], self.setup)
229  self.iEvent = iEv
230  for i,analyzer in enumerate(self.analyzers):
231  if not analyzer.beginLoopCalled:
232  analyzer.beginLoop(self.setup)
233  start = timeit.default_timer()
234  if self.memReportFirstEvent >=0 and iEv >= self.memReportFirstEvent:
235  memNow=resource.getrusage(resource.RUSAGE_SELF).ru_maxrss
236  if memNow > self.memLast :
237  print "Mem Jump detected before analyzer %s at event %s. RSS(before,after,difference) %s %s %s "%( analyzer.name, iEv, self.memLast, memNow, memNow-self.memLast)
238  self.memLast=memNow
239  ret = analyzer.process( self.event )
240  if self.memReportFirstEvent >=0 and iEv >= self.memReportFirstEvent:
241  memNow=resource.getrusage(resource.RUSAGE_SELF).ru_maxrss
242  if memNow > self.memLast :
243  print "Mem Jump detected in analyzer %s at event %s. RSS(before,after,difference) %s %s %s "%( analyzer.name, iEv, self.memLast, memNow, memNow-self.memLast)
244  self.memLast=memNow
245  if self.timeReport:
246  self.timeReport[i]['events'] += 1
247  if self.timeReport[i]['events'] > 0:
248  self.timeReport[i]['time'] += timeit.default_timer() - start
249  if ret == False:
250  return (False, analyzer.name)
251  if iEv<self.nPrint:
252  self.logger.info( self.event.__str__() )
253  return (True, analyzer.name)
254 
memReportFirstEvent
Definition: looper.py:90
def process(self, iEv)
Definition: looper.py:221
def looper.Looper.write (   self)
Writes all analyzers.

See Analyzer.Write for more information.

Definition at line 255 of file looper.py.

References looper.Looper.analyzers, Config.Process.analyzers, event.Event.setup, looper.Looper.setup, and options.HLTProcessOptions.setup.

255  def write(self):
256  """Writes all analyzers.
257 
258  See Analyzer.Write for more information.
259  """
260  for analyzer in self.analyzers:
261  analyzer.write(self.setup)
262  self.setup.close()
263 
264 
def write(self)
Definition: looper.py:255

Member Data Documentation

looper.Looper.analyzers

Definition at line 85 of file looper.py.

Referenced by looper.Looper.loop(), and looper.Looper.write().

looper.Looper.cfg_comp
looper.Looper.classes
looper.Looper.config

Definition at line 73 of file looper.py.

looper.Looper.event

Definition at line 117 of file looper.py.

Referenced by Types.EventID.cppID(), and looper.Looper.process().

looper.Looper.events

Definition at line 102 of file looper.py.

Referenced by looper.Looper.loop(), and looper.Looper.process().

looper.Looper.firstEvent

Definition at line 87 of file looper.py.

Referenced by looper.Looper.loop().

looper.Looper.iEvent

Definition at line 229 of file looper.py.

looper.Looper.logger

Definition at line 76 of file looper.py.

looper.Looper.memLast

Definition at line 91 of file looper.py.

looper.Looper.memReportFirstEvent

Definition at line 90 of file looper.py.

looper.Looper.name
looper.Looper.nEvents

Definition at line 86 of file looper.py.

Referenced by looper.Looper.loop().

looper.Looper.nPrint

Definition at line 88 of file looper.py.

looper.Looper.outDir

Definition at line 75 of file looper.py.

Referenced by looper.Looper._build().

looper.Looper.setup
looper.Looper.start_time

Definition at line 179 of file looper.py.

Referenced by progressbar.ProgressBar.__next__(), and progressbar.ProgressBar.update().

looper.Looper.start_time_event

Definition at line 180 of file looper.py.

looper.Looper.timeReport

Definition at line 89 of file looper.py.