ATLAS Offline Software
Loading...
Searching...
No Matches
JetAttributeHistoManager.py
Go to the documentation of this file.
1# Copyright (C) 2002-2021 CERN for the benefit of the ATLAS collaboration
2
3from JetMonitoring.HistoDefinitionHelpers import createHistoDefTool as hdef
4from JetMonitoring.HistoDefinitionHelpers import mergeHistoDefinition
5from JetMonitoring.JetMonitoringConf import JetAttributeHisto, JetSelectorAttributeRunII
6from JetMonitoring.JetHistoManager import jetHistoManager as jhm
7
9
10 def __init__(self, jhm ):
11 self.jhm = jhm
12
13 def buildKnownTools(self, compactSpecification):
14 for name, spec in compactSpecification.items():
15 if len(spec) == 2 :
16 binning, attributeInfo = spec
17 self.add1DHistoTool(name, binning, attributeInfo)
18 elif len(spec) == 3 :
19 binning, attributeInfo1, attributeInfo2 = spec
20 doTProfile = name.beginswith("Prof_")
21 self.add2DHistoTool(name, binning, attributeInfo1, attributeInfo2, DoTProfile=doTProfile)
22
23 def add1DHistoTool(self, name, binning, attributeInfo,**otherArgs):
24
25 if self.jhm.hasTool(name):
26 print ("ERROR JetAttributeHisto with name ", name ," already exists. Can't add a new one")
27 return None
28
29 tool = create1DHistoTool(name, binning, attributeInfo, **otherArgs)
30 return self.jhm.addTool( tool)
31
32
33
34 def add2DHistoTool(self, name, binning=None, attributeInfo1=None, attributeInfo2=None,**otherArgs):
35 if self.jhm.hasTool(name):
36 print ("ERROR JetAttributeHisto with name ", name ," already exists. Can't add a new one")
37 return None
38
39 tool = create2DHistoTool(name, binning, attributeInfo1, attributeInfo2, **otherArgs)
40 return self.jhm.addTool( tool )
41
42
43 def create2DHistoToolFrom1D(self, name, **otherArgs):
44 tool = self.jhm.tool(name, build2Difmissing=False)
45 if tool is not None : return tool
46
47 # else try to build it
48 # We are trying to build a 2D histo tool from existing 1D histo tools.
49 n1, n2 = name.split(':')
50 t1 = self.jhm.tool(n1)
51 t2 = self.jhm.tool(n2)
52
53 if None in (t1, t2):
54 missing = n1 if t1 is None else n2
55 print ("ERROR : can't build 2D histo", name, " : ",missing, " is unknonw")
56 return None
57
58 binning = mergeHistoDefinition( t1.HistoDef, t2.HistoDef)
59 def rebuildSuffix(index):
60 if index==-1: return ''
61 return '['+str(index)+']'
62 attInfo1 = (t1.AttributeNames[0]+rebuildSuffix(t1.SelectIndex), t1.AttributeTypes[0])
63 attInfo2 = (t2.AttributeNames[0]+rebuildSuffix(t2.SelectIndex), t2.AttributeTypes[0])
64
65 tool = create2DHistoTool(name, binning, attInfo1, attInfo2, **otherArgs)
66
67 return self.jhm.addTool( tool)
68
69 def addSelector(self, selectString, name="", typ="float"):
70 if name != "" and self.jhm.hasTool(name) :
71 print ("ERROR JetSelectorAttributeRunII with name ", name ," already exists. Can't add a new one")
72 return None
73 if self.jhm.hasTool(selectString) :
74 print ("ERROR JetSelectorAttributeRunII ", selectString ," already exists. Can't add a new one")
75 return None
76 tool = createAttSelector( selectString, name=name, typ=typ)
77 # selectors are public tools :
78 from AthenaCommon.AppMgr import ToolSvc
79
80 ToolSvc += tool
81 return self.jhm.addTool( tool , alias=selectString)
82
83
84
85
86def create1DHistoTool( name, binning, attributeInfo,**otherArgs):
87
88 attName, attType, attGeV = unpackto3(attributeInfo)
89 attName, selectIndex = findSelectIndex(attName) # 'JVF[1]' --> 'JVF', 1
90
91 #hname = name if selectIndex==-1 else (name+'_'+str(selectIndex))
92 hname = sanitizeName(name) # remove [ and ] which can be problematic in histo names
93
94 return JetAttributeHisto( name, HistoDef = hdef(hname, *binning),
95 AttributeTypes = [ attType ],
96 AttributeNames = [ attName ],
97 AttributeInGeV = [ bool(attGeV) ],
98 SelectIndex = selectIndex , **otherArgs)
99
100
101def create2DHistoTool( name, binning=None, attributeInfo1=None, attributeInfo2=None,**otherArgs):
102 attName1, attType1, attGeV1 = unpackto3(attributeInfo1)
103 attName1, selectIndex1 = findSelectIndex(attName1)
104
105 attName2, attType2, attGeV2 = unpackto3(attributeInfo2)
106 attName2, selectIndex2 = findSelectIndex(attName2)
107
108 # currently support only vector<float> vs float, so there can be only one selected index.
109 selectIndex = max ( selectIndex1, selectIndex2)
110
111 #hname = name if selectIndex==-1 else (name+'_'+str(selectIndex))
112 hname = sanitizeName(name) # remove [ and ] which can be problematic in histo names
113
114 return JetAttributeHisto( name, HistoDef = hdef(hname, *binning),
115 AttributeTypes = [ attType1, attType2 ],
116 AttributeNames = [ attName1, attName2 ],
117 AttributeInGeV = [ bool(attGeV1), bool(attGeV2) ],
118 SelectIndex = selectIndex , **otherArgs)
119
120
121def createAttSelector(selectString, name="", typ="float"):
122 """A short cut to create JetSelectorAttributeRunII out of a simple string """
123 cmin, att, cmax = interpretSelStr(selectString)
124 att, ind = findSelectIndex(att)
125 if ind>-1 and 'vector' not in typ :
126 typ = 'vector<'+typ+'>'
127
128 if name == "":
129 # try to build a unique name
130 name = selectString.replace('<','_inf_')
131 name = name.replace('[','_')
132 name = name.replace(']','_')
133 name = name.replace('.','_')
134 name = 'sel_'+name
135 tool = JetSelectorAttributeRunII(name, Attribute=att, AttributeType=typ, VectorIndex=ind)
136 if cmin is not None: tool.CutMin = cmin
137 if cmax is not None: tool.CutMax = cmax
138 return tool
139
140
141def sanitizeName(name):
142 return name.replace('[','_').replace(']','_')
143
145 if len(t)==2:
146 return t+(False,)
147 return t
148
150 try:
151 name, index = name.split('[')
152 except Exception:
153 name, index = name, ''
154 if not index.endswith(']'):
155 return name, -1
156 index = int(index[:-1])
157 return name, index
158
159
160def interpretSelStr(selStr):
161 """Interpret a selection string in the form '12.3<var<42.0'
162 and returns a tuple.
163 '12.3<var<42.0' -> returns (12.3, 'var', 42.)
164 'var<42.0' -> returns (None, 'var', 42.)
165 '12.3<var' -> returns (12.3, 'var', None)
166 """
167 parts = selStr.split('<')
168 cmin, cmax = None, None
169 var = selStr
170 if len(parts)==2:
171 ismin = False
172 try :
173 var, cut = parts[0] , float(parts[1])
174 except Exception:
175 cut, var = float(parts[0]) ,parts[1]
176 ismin=True
177 if ismin : cmin = cut
178 else: cmax = cut
179 elif len(parts)==3:
180 cmin, var, cmax = parts
181 cmin = float(cmin)
182 cmax = float(cmax)
183
184 return cmin, var, cmax
185
186
187attributeHistoManager = AttributeHistoManager(jhm)
addSelector(self, selectString, name="", typ="float")
add2DHistoTool(self, name, binning=None, attributeInfo1=None, attributeInfo2=None, **otherArgs)
add1DHistoTool(self, name, binning, attributeInfo, **otherArgs)
A histo building tool (JetHistoBase) using attributes to fill histograms.
std::string replace(std::string s, const std::string &s2, const std::string &s3)
Definition hcg.cxx:310
create1DHistoTool(name, binning, attributeInfo, **otherArgs)
create2DHistoTool(name, binning=None, attributeInfo1=None, attributeInfo2=None, **otherArgs)
createAttSelector(selectString, name="", typ="float")