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