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