401def calibConfigToToolList(flags, calibSeqKey = None, **configDict):
402 """
403 Returns a list of instantiated tools for each of the calibration steps.
404 Tools are instantiated by calling functions declared in the calibStepDic dictionary.
405 The order of the steps is determined by the Sequence block of the config.
406
407 Parameters:
408 -----------
409 calibSeqKey: str
410 Specifies which sub-block of Sequence to take the calibration sequence from (e.g. Default/T0/Run3 etc.).
411 If not set, Default is used unless a "RunX" sub-block exists matching the metadata of the sample.
412 Default is also used if the requested sub-block has no entry for this sample type.
413 configDict: dict
414 A dictionary of config options (usually extracted from a YAML config file).
415 """
416
417 sampleKey, runKey = getSampleMetadata(flags)
418
419
420 sampleKeys = [sampleKey] if sampleKey=='Data' else [sampleKey, 'MC']
421
422
423 if 'Sequence' not in configDict:
424 raise JetCalibConfigError("Sequence block not found in YAML file.")
425 seqDict = configDict.pop('Sequence')
426
427
428 if calibSeqKey is None:
429 calibSeqKey = runKey if sequenceForSample(seqDict.get(runKey), sampleKeys)[0] else 'Default'
430
431 if calibSeqKey not in seqDict:
432 raise JetCalibConfigError(f"{calibSeqKey} key not found in YAML Sequence block")
433
434
435 sequence, matchedKey = sequenceForSample(seqDict[calibSeqKey], sampleKeys)
436
437
438 if sequence is None and calibSeqKey!='Default':
439 jcslog.warning(f'No {sampleKey} sequence in Sequence[{calibSeqKey}] - falling back on Default')
440 calibSeqKey = 'Default'
441 sequence, matchedKey = sequenceForSample(seqDict.get('Default'), sampleKeys)
442
443
444 if sequence is None:
445 raise JetCalibConfigError(f'No sequence for sample type "{sampleKey}" found in Sequence[{calibSeqKey}] YAML sub-block')
446
447 jcslog.info(f'Using {calibSeqKey} step sequence' + (f' for {matchedKey}' if matchedKey else ''))
448
449 toolList = []
450 jcslog.debug('Configuring jet calib steps:')
451 for step in sequence:
452
453 if step not in configDict:
454 raise JetCalibConfigError(f'Sequence includes step {step} but no YAML block is provided.')
455
456 calibConfig = configDict[step]
457 calibConfig.pop('prereqs',{})
458
459
460 if calibConfig.pop('noRun',False):
461 jcslog.warning(f'Expert option: Skipping calib step {step}')
462 continue
463
464
465 if step=="Insitu" and flags.Input.isMC and not calibConfig.get("CalibrateMC",False):
466 jcslog.warning('Insitu step included for MC but CalibrateMC is False - no calibration will be run')
467
468 if step=="MC2MC":
469
470 if not flags.Input.isMC:
471 jcslog.warning('Running MC2MC calibration for data')
472
473
474 generator = next(iter(flags.Input.GeneratorsInfo), '')
475 if 'Pythia' in generator:
476 jcslog.debug('Skipping MC2MC calibration for Pythia8')
477 continue
478
479
480 if step=="AF3":
481 if not flags.Input.isMC:
482 jcslog.warning('Running FastSimulation calibration for data')
483
484 if sampleKey=='FullSim':
485 jcslog.warning('Running FastSimulation calibration for full sim')
486
487 calibFunc = calibStepDic.get(step,None)
488
489 if calibFunc is None:
490 raise NotImplementedError(f'Calibration step {step} is not found in calibStepDic')
491
492
493
494 runOverrides = {key: calibConfig.pop(key) for key in ['Run2', 'Run3', 'Run4'] if key in calibConfig}
495 for key, value in runOverrides.get(runKey,{}).items():
496 jcslog.debug(f'{step}: Applying {runKey} override for {key}')
497 if key in calibConfig:
498 jcslog.warning(f'{key} will be overwritten by {runKey} settings')
499 calibConfig[key] = value
500
501
502 if len(toolList)==0:
503 inScale = 'JetConstitScaleMomentum'
504 else:
505 inScale = toolList[-1].OutScale
506
507 configInScale = calibConfig.setdefault('InScale', inScale)
508
509 if configInScale!=inScale:
510 jcslog.warning(f'InScale set to {configInScale} in YAML config, but expected {inScale} from Sequence ordering -- is this intentional?')
511
512
513
514 newToolList = calibFunc(flags, **calibConfig)
515 jcslog.debug(f'{step}: InScale = {newToolList[0].InScale}, OutScale = {newToolList[-1].OutScale}')
516
517 toolList += newToolList
518
519 return toolList
520