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