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 51 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 61 of file looper.py.

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

Member Function Documentation

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

Definition at line 129 of file looper.py.

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

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

Definition at line 134 of file looper.py.

134  def _prepareOutput(self, name):
135  index = 0
136  tmpname = name
137  while True and index < 2000:
138  try:
139  # print 'mkdir', self.name
140  os.mkdir( tmpname )
141  break
142  except OSError:
143  index += 1
144  tmpname = '%s_%d' % (name, index)
145  if index == 2000:
146  raise ValueError( "More than 2000 output folder with same name or 2000 attempts failed, please clean-up, change name or check permissions")
147  return tmpname
148 
149 
def _prepareOutput(self, name)
Definition: looper.py:134
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 150 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, ME::Header.events, emtf::Node.events, HitEff.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, SiStripMonitorDigi.firstEvent, SiStripMonitorCluster.firstEvent, SiStripMonitorTrack.firstEvent, createfilelist.int, HIPTrackMonitorVariables.nEvents, HiggsTo2GammaSkim.nEvents, HiggsToZZ4LeptonsSkim.nEvents, JGJFilter.nEvents, CTPPSPixelDQMSource.nEvents, HiggsToZZ4LeptonsSkimEff.nEvents, MCDijetResonance.nEvents, EgammaProbeSelector.nEvents, QualityTester.nEvents, AlCaHcalNoiseProducer.nEvents, DTScalerInfoTask.nEvents, MCatNLOSource.nEvents, HeavyChHiggsToTauNuSkim.nEvents, AlpgenHeader.nEvents, DTLocalTriggerLutTask.nEvents, looper.Looper.nEvents, DTLocalTriggerBaseTask.nEvents, MuonIsolationDQM.nEvents, MatacqProducer::stats_t.nEvents, cscdqm::Configuration.nEvents, edm.print(), event.Event.setup, looper.Looper.setup, options.HLTProcessOptions.setup, and str.

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

References TB06Reco.event, TB06RecoH2.event, ApeOverview.event, core.AutoHandle.AutoHandle.event, L1Analysis::L1AnalysisEventDataFormat.event, MuonPair.event, LHAupLesHouches.event, EcalPerEvtMatacqAnalyzer.event, WZInterestingEventSelector::event.event, EcalStatusAnalyzer.event, lhef::LHEProxy.event, MCatNLOSource.event, EcalMatacqAnalyzer.event, L1TCaloLayer1RawToDigi.event, HitEff.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, emtf::Node.events, HitEff.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.

223  def process(self, iEv ):
224  """Run event processing for all analyzers in the sequence.
225 
226  This function is called by self.loop,
227  but can also be called directly from
228  the python interpreter, to jump to a given event.
229  """
230  self.event = Event(iEv, self.events[iEv], self.setup)
231  self.iEvent = iEv
232  for i,analyzer in enumerate(self.analyzers):
233  if not analyzer.beginLoopCalled:
234  analyzer.beginLoop(self.setup)
235  start = timeit.default_timer()
236  if self.memReportFirstEvent >=0 and iEv >= self.memReportFirstEvent:
237  memNow=resource.getrusage(resource.RUSAGE_SELF).ru_maxrss
238  if memNow > self.memLast :
239  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))
240  self.memLast=memNow
241  ret = analyzer.process( self.event )
242  if self.memReportFirstEvent >=0 and iEv >= self.memReportFirstEvent:
243  memNow=resource.getrusage(resource.RUSAGE_SELF).ru_maxrss
244  if memNow > self.memLast :
245  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))
246  self.memLast=memNow
247  if self.timeReport:
248  self.timeReport[i]['events'] += 1
249  if self.timeReport[i]['events'] > 0:
250  self.timeReport[i]['time'] += timeit.default_timer() - start
251  if ret == False:
252  return (False, analyzer.name)
253  if iEv<self.nPrint:
254  self.logger.info( self.event.__str__() )
255  return (True, analyzer.name)
256 
memReportFirstEvent
Definition: looper.py:92
S & print(S &os, JobReport::InputFile const &f)
Definition: JobReport.cc:65
def process(self, iEv)
Definition: looper.py:223
def looper.Looper.write (   self)
Writes all analyzers.

See Analyzer.Write for more information.

Definition at line 257 of file looper.py.

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

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

Member Data Documentation

looper.Looper.analyzers

Definition at line 87 of file looper.py.

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

looper.Looper.cfg_comp
looper.Looper.classes

Definition at line 86 of file looper.py.

looper.Looper.config

Definition at line 75 of file looper.py.

looper.Looper.event

Definition at line 119 of file looper.py.

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

looper.Looper.events

Definition at line 104 of file looper.py.

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

looper.Looper.firstEvent

Definition at line 89 of file looper.py.

Referenced by looper.Looper.loop().

looper.Looper.iEvent

Definition at line 231 of file looper.py.

looper.Looper.logger

Definition at line 78 of file looper.py.

looper.Looper.memLast

Definition at line 93 of file looper.py.

looper.Looper.memReportFirstEvent

Definition at line 92 of file looper.py.

looper.Looper.name
looper.Looper.nEvents

Definition at line 88 of file looper.py.

Referenced by looper.Looper.loop().

looper.Looper.nPrint

Definition at line 90 of file looper.py.

looper.Looper.outDir

Definition at line 77 of file looper.py.

Referenced by looper.Looper._build().

looper.Looper.setup
looper.Looper.start_time

Definition at line 181 of file looper.py.

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

looper.Looper.start_time_event

Definition at line 182 of file looper.py.

looper.Looper.timeReport

Definition at line 91 of file looper.py.