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

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

Member Function Documentation

◆ _build()

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

Definition at line 130 of file looper.py.

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

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

◆ _prepareOutput()

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

Definition at line 135 of file looper.py.

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

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

References looper.Looper.analyzers, Config.Process.analyzers, analyzer.Analyzer.cfg_comp, looper.Looper.cfg_comp, CSCFileDumper.events, eventsfwlite.Events.events, generateEDF.LumiInfo.events, emtf::Forest.events, JanAlignmentAlgorithm.events, ME::Header.events, emtf::Node.events, HitEff.events, HitResol.events, looper.Looper.events, MatrixUtil.InputInfo.events, CosmicRateAnalyzer.events, BeamSpotDipServer.events, options.HLTProcessOptions.events, FastTimerService::ResourcesPerModule.events, FastTimerService::ResourcesPerJob.events, JsonOutputProducer::JsonConfiguration.events, Printer.Printer.firstEvent, CSCFileReader.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.O2OJobMgr.logger, conddb_version_mgr.conddb_tool.logger, o2olib.O2ORunMgr.logger, cmsHarvester.CMSHarvester.logger, CSCFileReader.nEvents, HIPTrackMonitorVariables.nEvents, WorkFlowRunner.WorkFlowRunner.nEvents, CTPPSPixelDQMSource.nEvents, QualityTester.nEvents, DTScalerInfoTask.nEvents, AlpgenHeader.nEvents, AlCaHcalNoiseProducer.nEvents, DTLocalTriggerLutTask.nEvents, looper.Looper.nEvents, hcalCalib.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.

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

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

References TB06Reco.event, TB06RecoH2.event, hgcal::econd::HGCalModuleTreeReader::HGCModuleTreeEvent.event, core.AutoHandle.AutoHandle.event, L1Analysis::L1AnalysisEventDataFormat.event, MuonPair.event, EcalPerEvtMatacqAnalyzer.event, LHAupLesHouches.event, WZInterestingEventSelector::event.event, lhef::LHEProxy.event, EcalStatusAnalyzer.event, EcalMatacqAnalyzer.event, L1TCaloLayer1RawToDigi.event, looper.Looper.event, EcalABAnalyzer.event, EcalTestPulseAnalyzer.event, EcalPerEvtLaserAnalyzer.event, HitEff.event, edm::service::SimpleMemoryCheck::SignificantEvent.event, EcalLaserAnalyzer.event, EcalLaserAnalyzer2.event, FastTimerService::ResourcesPerJob.event, cms::alpakatools::CachingAllocator< TDev, TQueue >::BlockDescriptor.event, l1ct::Event.event, JsonOutputProducer::JsonEvent.event, SummaryOutputProducer::Event.event, CSCFileDumper.events, eventsfwlite.Events.events, generateEDF.LumiInfo.events, emtf::Forest.events, JanAlignmentAlgorithm.events, ME::Header.events, emtf::Node.events, HitEff.events, HitResol.events, looper.Looper.events, MatrixUtil.InputInfo.events, CosmicRateAnalyzer.events, BeamSpotDipServer.events, options.HLTProcessOptions.events, FastTimerService::ResourcesPerModule.events, FastTimerService::ResourcesPerJob.events, JsonOutputProducer::JsonConfiguration.events, event.Event.setup, looper.Looper.setup, and options.HLTProcessOptions.setup.

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

◆ write()

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

See Analyzer.Write for more information.

Definition at line 258 of file looper.py.

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

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

Member Data Documentation

◆ analyzers

looper.Looper.analyzers

Definition at line 88 of file looper.py.

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

◆ cfg_comp

looper.Looper.cfg_comp

Definition at line 86 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 87 of file looper.py.

◆ config

looper.Looper.config

Definition at line 76 of file looper.py.

◆ event

looper.Looper.event

Definition at line 120 of file looper.py.

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

◆ events

looper.Looper.events

Definition at line 105 of file looper.py.

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

◆ firstEvent

looper.Looper.firstEvent

Definition at line 90 of file looper.py.

Referenced by looper.Looper.loop().

◆ iEvent

looper.Looper.iEvent

Definition at line 232 of file looper.py.

◆ logger

looper.Looper.logger

Definition at line 79 of file looper.py.

Referenced by looper.Looper.loop().

◆ memLast

looper.Looper.memLast

Definition at line 94 of file looper.py.

◆ memReportFirstEvent

looper.Looper.memReportFirstEvent

Definition at line 93 of file looper.py.

◆ name

looper.Looper.name

◆ nEvents

looper.Looper.nEvents

Definition at line 89 of file looper.py.

Referenced by looper.Looper.loop().

◆ nPrint

looper.Looper.nPrint

Definition at line 91 of file looper.py.

◆ outDir

looper.Looper.outDir

Definition at line 78 of file looper.py.

Referenced by looper.Looper._build().

◆ setup

looper.Looper.setup

◆ start_time

looper.Looper.start_time

Definition at line 182 of file looper.py.

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

◆ start_time_event

looper.Looper.start_time_event

Definition at line 183 of file looper.py.

◆ timeReport

looper.Looper.timeReport

Definition at line 92 of file looper.py.