239 def makeAlgs (self, config) :
240
241 from Campaigns.Utils import Campaign
242
243 log = logging.getLogger('makePileupAnalysisSequence')
244
245 eventInfoVar = [('runNumber','unsigned'),
246 ('eventNumber','unsigned_long'),
247 ('actualInteractionsPerCrossing','float'),
248 ('averageInteractionsPerCrossing','float')]
249 if config.dataType() is not DataType.Data:
250 eventInfoVar += [('mcChannelNumber','unsigned')]
251 if self.writeColumnarToolVariables:
252
253
254
255 eventInfoVar += [('eventTypeBitmask','int')]
256
257 if config.isPhyslite() and not self.alternativeConfig:
258
259 log.info(f'Physlite does not need pileup reweighting. Variables will be copied from input instead. {config.isPhyslite}')
260 for var_name,var_type in eventInfoVar:
261 config.addOutputVar ('EventInfo', var_name, var_name, noSys=True, auxType=var_type)
262
263 if config.dataType() is not DataType.Data:
264 config.addOutputVar ('EventInfo', 'PileupWeight_%SYS%', 'weight_pileup', auxType='float')
265 if config.geometry() is LHCPeriod.Run2:
266 config.addOutputVar ('EventInfo', 'beamSpotWeight', 'weight_beamspot', noSys=True, auxType='float')
267 return
268
269
270 if self.files is None and config.flags is not None:
271 self.files = config.flags.Input.Files
272
273 campaign = self.campaign
274
275
276 if config.dataType() is not DataType.Data and self.campaign is None:
277
278 if config.campaign() is not None and config.campaign() is not Campaign.Unknown:
279 campaign = config.campaign()
280 log.info(f'Auto-configuring campaign for PRW from flags: {campaign.value}')
281 else:
282
283 if self.files is not None:
284 from Campaigns.Utils import getMCCampaign
285 campaign = getMCCampaign(self.files)
286 if campaign and campaign is not Campaign.Unknown:
287 log.info(f'Auto-configuring campaign for PRW from files: {campaign.value}')
288 else:
289 log.info('Campaign could not be determined.')
290
291
292 toolConfigFiles = []
293 toolLumicalcFiles = []
294
295
296
297 if (config.dataType() is not DataType.Data and
298 config.geometry() is not LHCPeriod.Run4):
299
300 if self.userPileupConfigs is not None and self.userPileupConfigsPerCampaign is not None:
301 raise ValueError('Both userPileupConfigs and userPileupConfigsPerCampaign specified, '
302 'use only one of the options!')
303 if self.userPileupConfigsPerCampaign is not None:
304 if not campaign:
305 raise Exception('userPileupConfigsPerCampaign requires campaign to be configured!')
306 if campaign is Campaign.Unknown:
307 raise Exception('userPileupConfigsPerCampaign used, but campaign = Unknown!')
308 try:
309 toolConfigFiles = self.userPileupConfigsPerCampaign[campaign.value][:]
310 log.info('Using user provided per-campaign PRW configuration')
311 except KeyError as e:
312 raise KeyError(f'Unconfigured campaign {e} for userPileupConfigsPerCampaign!')
313
314 elif self.userPileupConfigs is not None:
315 toolConfigFiles = self.userPileupConfigs[:]
316 log.info('Using user provided PRW configuration')
317
318 else:
319 if self.useDefaultConfig and self.files is None:
320 raise ValueError('useDefaultConfig requires files to be configured! '
321 'Either pass them as an option or use flags.')
322
323 from PileupReweighting.AutoconfigurePRW import getConfigurationFiles
324 if campaign and campaign is not Campaign.Unknown:
325 toolConfigFiles = getConfigurationFiles(campaign=campaign,
326 files=self.files,
327 useDefaultConfig=self.useDefaultConfig,
328 data_type=config.dataType())
329 if self.useDefaultConfig:
330 log.info('Auto-configuring universal/default PRW config')
331 else:
332 log.info('Auto-configuring per-sample PRW config files based on input files')
333 else:
334 log.info('No campaign specified, no PRW config files configured')
335
336
337 if self.userLumicalcFilesPerCampaign is not None and self.userLumicalcFiles is not None:
338 raise ValueError('Both userLumicalcFiles and userLumicalcFilesYear specified, '
339 'use only one of the options!')
340 if self.userLumicalcFilesPerCampaign is not None:
341 try:
342 toolLumicalcFiles = self.userLumicalcFilesPerCampaign[campaign.value][:]
343 log.info('Using user-provided per-campaign lumicalc files')
344 except KeyError as e:
345 raise KeyError(f'Unconfigured campaign {e} for userLumicalcFilesPerCampaign!')
346 elif self.userLumicalcFiles is not None:
347 toolLumicalcFiles = self.userLumicalcFiles[:]
348 log.info('Using user-provided lumicalc files')
349 else:
350 if campaign and campaign is not Campaign.Unknown:
351 from PileupReweighting.AutoconfigurePRW import getLumicalcFiles
352 toolLumicalcFiles = getLumicalcFiles(campaign)
353 log.info('Using auto-configured lumicalc files')
354 else:
355 log.info('No campaign specified, no lumicalc files configured for PRW')
356 else:
357 log.info('Data needs no lumicalc and PRW configuration files')
358
359
360 if config.geometry() is LHCPeriod.Run4:
361 warnings.warn_explicit(
362 'Pileup reweighting is not yet supported for Run 4 geometry',
363 Run4FallbackWarning, filename='', lineno=0)
364 alg = config.createAlgorithm( 'CP::EventDecoratorAlg', 'EventDecoratorAlg' )
365 alg.uint32Decorations = { 'RandomRunNumber' :
366 config.flags.Input.RunNumbers[0] }
367
368 else:
369 alg = config.createAlgorithm( 'CP::PileupReweightingAlg',
370 'PileupReweightingAlg' )
371 config.addPrivateTool( 'pileupReweightingTool', 'CP::PileupReweightingTool' )
372 alg.pileupReweightingTool.ConfigFiles = toolConfigFiles
373 if not toolConfigFiles and config.dataType() is not DataType.Data:
374 log.info("No PRW config files provided. Disabling reweighting")
375
376 alg.pileupWeightDecoration = ""
377 else:
378 alg.pileupWeightDecoration = "PileupWeight" + self.postfix + "_%SYS%"
379 alg.pileupReweightingTool.LumiCalcFiles = toolLumicalcFiles
380 alg.pileupReweightingTool.UnrepresentedDataWarningThreshold = (
381 self.unrepresentedDataWarningThreshold)
382
383 if not self.alternativeConfig:
384 for var_name,var_type in eventInfoVar:
385 config.addOutputVar ('EventInfo', var_name, var_name, noSys=True, auxType=var_type)
386
387 if config.dataType() is not DataType.Data and config.geometry() is LHCPeriod.Run2:
388 config.addOutputVar ('EventInfo', 'beamSpotWeight', 'weight_beamspot', noSys=True, auxType='float')
389
390 if config.dataType() is not DataType.Data and toolConfigFiles:
391 config.addOutputVar ('EventInfo', 'PileupWeight' + self.postfix + '_%SYS%',
392 'weight_pileup'+self.postfix, auxType='float')
393
394