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

Constructor & Destructor Documentation

◆ __init__()

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

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

Member Function Documentation

◆ _build()

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

Definition at line 131 of file looper.py.

131  def _build(self, cfg):
132  theClass = cfg.class_object
133  obj = theClass( cfg, self.cfg_comp, self.outDir )
134  return obj
135 

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

◆ _prepareOutput()

def looper.Looper._prepareOutput (   self,
  name 
)
private

Definition at line 136 of file looper.py.

136  def _prepareOutput(self, name):
137  index = 0
138  tmpname = name
139  while True and index < 2000:
140  try:
141  # print 'mkdir', self.name
142  os.mkdir( tmpname )
143  break
144  except OSError:
145  index += 1
146  tmpname = '%s_%d' % (name, index)
147  if index == 2000:
148  raise ValueError( "More than 2000 output folder with same name or 2000 attempts failed, please clean-up, change name or check permissions")
149  return tmpname
150 
151 

◆ loop()

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

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

References looper.Looper.analyzers, Config.Process.analyzers, analyzer.Analyzer.cfg_comp, looper.Looper.cfg_comp, eventsfwlite.Events.events, generateEDF.LumiInfo.events, emtf::Forest.events, JanAlignmentAlgorithm.events, ME::Header.events, emtf::Node.events, looper.Looper.events, HitEff.events, CosmicRateAnalyzer.events, MatrixUtil.InputInfo.events, options.HLTProcessOptions.events, FastTimerService::ResourcesPerModule.events, FastTimerService::ResourcesPerJob.events, JsonOutputProducer::JsonConfiguration.events, Printer.Printer.firstEvent, lhef::LHEReader.firstEvent, lhef::LH5Reader.firstEvent, ApeEstimatorSummary.firstEvent, looper.Looper.firstEvent, LeptonRecoSkim.firstEvent, SiStripMonitorPedestals.firstEvent, SiPixelPhase1Analyzer.firstEvent, SiStripMonitorDigi.firstEvent, SiStripMonitorCluster.firstEvent, SiStripMonitorTrack.firstEvent, info(), createfilelist.int, crabFunctions.CrabController.logger, looper.Looper.logger, o2olib.O2OMgr.logger, conddb_version_mgr.conddb_tool.logger, o2olib.O2ORunMgr.logger, cmsHarvester.CMSHarvester.logger, HIPTrackMonitorVariables.nEvents, CTPPSPixelDQMSource.nEvents, QualityTester.nEvents, AlCaHcalNoiseProducer.nEvents, DTScalerInfoTask.nEvents, MCatNLOSource.nEvents, AlpgenHeader.nEvents, MyStubComparison.nEvents, DTLocalTriggerLutTask.nEvents, looper.Looper.nEvents, DTLocalTriggerBaseTask.nEvents, MuonIsolationDQM.nEvents, MatacqProducer::stats_t.nEvents, cscdqm::Configuration.nEvents, print(), FastTimerService_cff.range, event.Event.setup, looper.Looper.setup, options.HLTProcessOptions.setup, and str.

◆ process()

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

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

References TB06Reco.event, TB06RecoH2.event, ApeOverview.event, core.AutoHandle.AutoHandle.event, L1Analysis::L1AnalysisEventDataFormat.event, MuonPair.event, EcalPerEvtMatacqAnalyzer.event, LHAupLesHouches.event, WZInterestingEventSelector::event.event, EcalStatusAnalyzer.event, lhef::LHEProxy.event, MCatNLOSource.event, EcalMatacqAnalyzer.event, L1TCaloLayer1RawToDigi.event, looper.Looper.event, EcalTestPulseAnalyzer.event, EcalABAnalyzer.event, EcalPerEvtLaserAnalyzer.event, HitEff.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, JanAlignmentAlgorithm.events, ME::Header.events, emtf::Node.events, looper.Looper.events, HitEff.events, CosmicRateAnalyzer.events, MatrixUtil.InputInfo.events, options.HLTProcessOptions.events, FastTimerService::ResourcesPerModule.events, FastTimerService::ResourcesPerJob.events, JsonOutputProducer::JsonConfiguration.events, event.Event.setup, looper.Looper.setup, and options.HLTProcessOptions.setup.

◆ write()

def looper.Looper.write (   self)
Writes all analyzers.

See Analyzer.Write for more information.

Definition at line 259 of file looper.py.

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

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

Member Data Documentation

◆ analyzers

looper.Looper.analyzers

Definition at line 82 of file looper.py.

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

◆ cfg_comp

looper.Looper.cfg_comp

Definition at line 80 of file looper.py.

Referenced by objects.LeptonAnalyzer.LeptonAnalyzer.__init__(), core.JSONAnalyzer.JSONAnalyzer.__init__(), objects.JetAnalyzer.JetAnalyzer.__init__(), looper.Looper._build(), JetAnalyzer.JetAnalyzer.declareHandles(), objects.VertexAnalyzer.VertexAnalyzer.declareHandles(), core.AutoFillTreeProducer.AutoFillTreeProducer.declareVariables(), core.AutoFillTreeProducer.AutoFillTreeProducer.fillCoreVariables(), core.AutoFillTreeProducer.AutoFillTreeProducer.fillTree(), core.AutoFillTreeProducer.AutoFillTreeProducer.getPythonWrapper(), looper.Looper.loop(), gen.LHEAnalyzer.LHEAnalyzer.process(), SimpleJetAnalyzer.SimpleJetAnalyzer.process(), gen.LHEWeightAnalyzer.LHEWeightAnalyzer.process(), gen.HiggsDecayModeAnalyzer.HiggsDecayModeAnalyzer.process(), gen.GenHeavyFlavourAnalyzer.GenHeavyFlavourAnalyzer.process(), gen.PDFWeightsAnalyzer.PDFWeightsAnalyzer.process(), core.JSONAnalyzer.JSONAnalyzer.process(), core.SkimAnalyzerCount.SkimAnalyzerCount.process(), AlphaTAnalyzer.AlphaTAnalyzer.process(), JetAnalyzer.JetAnalyzer.process(), core.PileUpAnalyzer.PileUpAnalyzer.process(), objects.VertexAnalyzer.VertexAnalyzer.process(), objects.TauAnalyzer.TauAnalyzer.process(), gen.GeneratorAnalyzer.GeneratorAnalyzer.process(), objects.PhotonAnalyzer.PhotonAnalyzer.process(), objects.LeptonAnalyzer.LeptonAnalyzer.process(), core.Analyzer.Analyzer.readCollections(), DiLeptonAnalyzer.DiLeptonAnalyzer.selectionSequence(), JetAnalyzer.JetAnalyzer.testBJet(), and core.PileUpAnalyzer.PileUpAnalyzer.write().

◆ classes

looper.Looper.classes

Definition at line 81 of file looper.py.

◆ config

looper.Looper.config

Definition at line 70 of file looper.py.

◆ event

looper.Looper.event

Definition at line 114 of file looper.py.

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

◆ events

looper.Looper.events

Definition at line 99 of file looper.py.

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

◆ firstEvent

looper.Looper.firstEvent

Definition at line 84 of file looper.py.

Referenced by looper.Looper.loop().

◆ iEvent

looper.Looper.iEvent

Definition at line 233 of file looper.py.

◆ logger

looper.Looper.logger

Definition at line 73 of file looper.py.

Referenced by looper.Looper.loop().

◆ memLast

looper.Looper.memLast

Definition at line 88 of file looper.py.

◆ memReportFirstEvent

looper.Looper.memReportFirstEvent

Definition at line 87 of file looper.py.

◆ name

looper.Looper.name

◆ nEvents

looper.Looper.nEvents

Definition at line 83 of file looper.py.

Referenced by looper.Looper.loop().

◆ nPrint

looper.Looper.nPrint

Definition at line 85 of file looper.py.

◆ outDir

looper.Looper.outDir

Definition at line 72 of file looper.py.

Referenced by looper.Looper._build().

◆ setup

looper.Looper.setup

◆ start_time

looper.Looper.start_time

Definition at line 183 of file looper.py.

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

◆ start_time_event

looper.Looper.start_time_event

Definition at line 184 of file looper.py.

◆ timeReport

looper.Looper.timeReport

Definition at line 86 of file looper.py.

FastTimerService_cff.range
range
Definition: FastTimerService_cff.py:34
dqmMemoryStats.float
float
Definition: dqmMemoryStats.py:127
min
T min(T a, T b)
Definition: MathUtil.h:58
join
static std::string join(char **cmd)
Definition: RemoteFile.cc:17
if
if(0==first)
Definition: CAHitNtupletGeneratorKernelsImpl.h:48
bmtfDigis_cfi.Setup
Setup
Definition: bmtfDigis_cfi.py:5
info
static const TGPicture * info(bool iBackgroundIsBlack)
Definition: FWCollectionSummaryWidget.cc:153
MessageLogger_cfi.warning
warning
Definition: MessageLogger_cfi.py:18
reco::ceil
constexpr int32_t ceil(float num)
Definition: constexpr_cmath.h:7
str
#define str(s)
Definition: TestProcessor.cc:52
Event
print
void print(TMatrixD &m, const char *label=nullptr, bool mathematicaFormat=false)
Definition: Utilities.cc:46
SiStripPI::max
Definition: SiStripPayloadInspectorHelper.h:169
LaserDQM_cfg.process
process
Definition: LaserDQM_cfg.py:3
heppy_loop.loop
loop
Definition: heppy_loop.py:28
createfilelist.int
int
Definition: createfilelist.py:10
TriggerAnalyzer.__str__
def __str__(self)
Definition: TriggerAnalyzer.py:103
ComparisonHelper::zip
OutputIterator zip(InputIterator1 first1, InputIterator1 last1, InputIterator2 first2, InputIterator2 last2, OutputIterator result, Compare comp)
Definition: L1TStage2CaloLayer1.h:41
writeEcalDQMStatus.write
write
Definition: writeEcalDQMStatus.py:48
format
genParticles_cff.map
map
Definition: genParticles_cff.py:11