ATLAS Offline Software
Loading...
Searching...
No Matches
EventCleaningConfig.py
Go to the documentation of this file.
1# Copyright (C) 2002-2024 CERN for the benefit of the ATLAS collaboration
2
3# AnaAlgorithm import(s):
4from AnalysisAlgorithmsConfig.ConfigBlock import ConfigBlock
5from AnalysisAlgorithmsConfig.ConfigAccumulator import DataType
6
7
8class EventCleaningBlock (ConfigBlock):
9 """the ConfigBlock for event cleaning"""
10
11 def __init__ (self) :
12 super (EventCleaningBlock, self).__init__ ()
13 self.addOption ('runPrimaryVertexSelection', True, type=bool,
14 info="whether to run primary vertex selection.")
15 self.addOption ('runEventCleaning', False, type=bool,
16 info="whether to run event cleaning (sets up an instance of "
17 "`CP::EventFlagSelectionAlg`).")
18 self.addOption ('runGRL', True, type=bool,
19 info="whether to run GRL decoration/selection.")
20 self.addOption ('userGRLFiles', [], type=list,
21 info="a list of GRL files (list of strings) to select data from.")
22 self.addOption ('minTracksPerVertex', 2, type=int,
23 info="minimum number of tracks per vertex.")
24 self.addOption ('selectionFlags', ['DFCommonJets_eventClean_LooseBad'], type=list,
25 info="flags (list of strings) to use for jet cleaning.")
26 # This is a vector<bool>, so parsing True/False is not handled
27 # in AnalysisBase, but we can evade this with numerical values
28 self.addOption ('invertFlags', [0], type=list,
29 info="list of booleans determining whether to invert the cut of the "
30 "above selectionFlags. In AnalysisBase, use 0/1 values instead.")
31 self.addOption ('GRLDict', {}, type=dict, info="a custom GRL dictionary with key some name and value a GRL file. Leaving it empty will use the recommended values.")
32 self.addOption ('GRLSuffixDict', {}, type=dict, info="a year-suffix dictionary to help with autoconfiguration of GRL selection, e.g. selecting 'BjetHLT'.")
33 self.addOption ('noFilter', False, type=bool,
34 info="whether to toggle off event filtering.")
35 self.addOption ('useRandomRunNumber', False, type=bool,
36 info="use `RandomRunNumber` to compute GRL info. Only supported for MC.")
37
38 if self.runGRL and self.userGRLFiles:
39 raise ValueError("No userGRLFiles should be specified if runGRL=False")
40
41 def instanceName (self) :
42 """Return the instance name for this block"""
43 return '' # no instance name needed for singleton block
44
45 def getDefaultGRLs (self, data_year) :
46 """ returns a reasonable set of GRLs that should be suited for most analyses """
47 from GoodRunsLists.GoodRunsListsDictionary import getGoodRunsLists
48 GRLDict = getGoodRunsLists()
49
50 GRLKey = 'GRL' + str(data_year)
51 if data_year in self.GRLSuffixDict:
52 GRLKey = GRLKey + '_' + self.GRLSuffixDict[data_year]
53 elif data_year in [2017, 2018]:
54 GRLKey = GRLKey + '_Triggerno17e33prim'
55 return GRLDict[GRLKey]
56
57 def makeAlgs (self, config) :
58
59 # Apply GRL
60 if self.runGRL and (config.dataType() is DataType.Data or self.useRandomRunNumber):
61 if config.dataType() is DataType.Data and self.useRandomRunNumber:
62 raise ValueError ("UseRandomRunNumber is only supported for MC!")
63
64 if self.noFilter:
65 # here we only decorate the PHYSLITE events with a boolean and don't do any cleaning
66 # Set up the GRL Decoration
67 if not self.GRLDict:
68 raise ValueError ("No GRLDict specified for GRL decoration, please specify a GRLDict")
69
70 for GRLDecoratorName, GRLFileList in self.GRLDict.items():
71 if isinstance(GRLFileList, str):
72 GRLFileList = [GRLFileList]
73
74 alg = config.createAlgorithm("GRLSelectorAlg", GRLDecoratorName)
75 config.addPrivateTool("Tool", "GoodRunsListSelectionTool")
76 alg.Tool.UseRandomRunNumber = self.useRandomRunNumber
77 alg.Tool.GoodRunsListVec = GRLFileList
78 alg.noFilter = True
79 alg.grlKey = f"EventInfo.{GRLDecoratorName}"
80
81 config.addOutputVar("EventInfo", GRLDecoratorName, GRLDecoratorName, noSys=True, auxType="char")
82 else:
83 # Set up the GRL selection:
84 alg = config.createAlgorithm( 'GRLSelectorAlg', 'GRLSelectorAlg' )
85 config.addPrivateTool( 'Tool', 'GoodRunsListSelectionTool' )
86 alg.Tool.UseRandomRunNumber = self.useRandomRunNumber
87 if self.userGRLFiles:
88 alg.Tool.GoodRunsListVec = self.userGRLFiles
89 else:
90 alg.Tool.GoodRunsListVec = self.getDefaultGRLs( config.dataYear() )
91
92 # Skip events with no primary vertex:
94 alg = config.createAlgorithm( 'CP::VertexSelectionAlg',
95 'PrimaryVertexSelectorAlg',
96 reentrant=True )
97 alg.VertexContainer = 'PrimaryVertices'
98 alg.MinVertices = 1
99 alg.MinTracks = self.minTracksPerVertex
100
101 # Set up the event cleaning selection:
103 if config.dataType() is DataType.Data:
104 alg = config.createAlgorithm( 'CP::EventStatusSelectionAlg', 'EventStatusSelectionAlg' )
105 alg.FilterKey = 'EventErrorState'
106 alg.FilterDescription = 'selecting events without any error state set'
107
108 alg = config.createAlgorithm( 'CP::EventFlagSelectionAlg', 'EventFlagSelectionAlg' )
109 alg.FilterKey = 'JetCleaning'
110 alg.selectionFlags = [f'{sel},as_char' for sel in self.selectionFlags]
111 alg.invertFlags = self.invertFlags
112 alg.FilterDescription = f"selecting events passing: {','.join(alg.selectionFlags)}"
113
114
115