ATLAS Offline Software
detector_hierarchy.py
Go to the documentation of this file.
1 #! /usr/bin/env python
2 
3 # Copyright (C) 2002-2019 CERN for the benefit of the ATLAS collaboration
4 
5 from __future__ import print_function
6 
7 HIERARCHY = (
8  ("Inner_Detector",(
9  ("Pixel", ("PIXB", "PIX0", "PIXEA", "PIXEC")),
10  ("SCT", ("SCTB", "SCTEA", "SCTEC")),
11  ("TRT", ("TRTB", "TRTEA", "TRTEC")),
12  ("IDGlobal", ("IDGL", "IDAL", "IDBS", "IDPF", "IDBCM")),
13  )),
14  ("Calorimeters",(
15  ("LAr", ("EMBA", "EMBC", "EMECA", "EMECC", "HECA",
16  "HECC", "FCALA", "FCALC")),
17  ("Tile", ("TIGB", "TILBA", "TILBC", "TIEBA", "TIEBC")),
18  ("MBTS", ("MBTSA", "MBTSC")),
19  ("CaloGlobal", ("CALBA", "CALEA", "CALEC")),
20  )),
21  ("Muons",(
22  ("MDT", ("MDTBA", "MDTBC", "MDTEA", "MDTEC")),
23  ("RPC", ("RPCBA", "RPCBC")),
24  ("TGC", ("TGCEA", "TGCEC")),
25  ("CSC", ("CSCEA", "CSCEC")),
26  )),
27  ("Luminosity",(
28  ("LUCID", ("LCD", "LCDA", "LCDC")),
29  ("Other", ("ALFA", "ZDC", "LUMI")),
30  ("RC", ("RUNCLT", "RCOPS")),
31  ("GL", ("ATLGL",))
32  )),
33  ("Trigger",(
34  ("L1", ("L1CAL", "L1MUB", "L1MUE", "L1CTP")),
35  ("HLT", ("TRCAL", "TRBJT", "TRBPH", "TRCOS", "TRGAM",
36  "TRMET", "TRMBI", "TRMUO", "TRTAU", "TRIDT")),
37  )),
38  ("Physics_Objects",(
39  ("egammaID", ("EIDCR", "EIDE", "PIDB", "PIDCR", "PIDE",
40  "EIDF", "EIDSOFT")),
41  ("muonID", ("MSTACO", "MMUIDCB", "MMUIDVX", "MMUGIRL",
42  "MMUBOY", "MMUIDSA", "MMUTAG", "MMTIMO",
43  "MCMUTAG", "MCALLHR")),
44  ("Jets", ("JETB", "JETEA", "JETEC")),
45  ("MET", ("MET", "METCALO", "METMUON")),
46  ("b_tag", ("BTGB", "BTGEA", "BTGEC")),
47  ("tauID", ("TAUE", "TAUB", "TAUCR")),
48  )),
49 )
50 
52  def __init__(self, hierarchy, depth=0, parent=None):
53  """
54  Create a DetectorHierarchy object
55  """
56 
57  self.depth = depth
58  self.parent = parent
59 
60  if isinstance(hierarchy, str):
61  self.name, sub_hierarchy = hierarchy, []
62  else:
63  self.name, sub_hierarchy = hierarchy
64 
65  self.sub_hierarchy = [DetectorHierarchy(sub_part, depth+1, self)
66  for sub_part in sub_hierarchy]
67 
68  if self.sub_hierarchy:
69  self.max_sub_width = max(len(x.name) for x in self.sub_hierarchy)
70  else:
71  self.max_sub_width = 7
72 
73  def get_channels(self, name, selected=False, parts=()):
74  """
75  Accumulate the lowest parts of the hierarchy which contain "name"
76  """
77  if self.name == name:
78  selected = True
79 
80  if not self.sub_hierarchy:
81  return [(self.name, parts)] if selected else []
82 
83  result = []
84  for sub_part in self.sub_hierarchy:
85  breadcrumbs = parts + (self.name,)
86  result.extend(sub_part.get_channels(name, selected, breadcrumbs))
87  return result
88 
89  def __repr__(self):
90  """
91  String representation of the hierarchy
92  """
93  sub_part = ()
94  if self.sub_hierarchy:
95  jstr = "\n"
96  indent = "\n"
97  if self.depth == 2:
98  jstr = " "
99  indent = " : "
100  sub_part = indent, jstr.join(repr(x) for x in self.sub_hierarchy)
101 
102  width = self.parent.max_sub_width if self.parent else len(self.name)
103  indent = " "*self.depth if self.depth < 3 else ""
104  name = self.name.ljust(width)
105 
106  return "".join((indent, name) + sub_part)
107 
108 detector_hierarchy = DetectorHierarchy(("All", HIERARCHY))
109 
110 def test():
111  print("Detector hierarchy:")
112  print(detector_hierarchy)
113  print()
114 
115  accumulated = detector_hierarchy.get_channels("Inner_Detector")
116 
117  from pprint import pprint
118  pprint(accumulated)
119 
120  from itertools import groupby
121  pprint(list(groupby(accumulated, key=lambda x: x[1])))
122 
123 if __name__ == "__main__":
124  test()
max
#define max(a, b)
Definition: cfImp.cxx:41
python.detector_hierarchy.DetectorHierarchy.get_channels
def get_channels(self, name, selected=False, parts=())
Definition: detector_hierarchy.py:73
python.detector_hierarchy.DetectorHierarchy.parent
parent
Definition: detector_hierarchy.py:58
python.detector_hierarchy.DetectorHierarchy.sub_hierarchy
sub_hierarchy
Definition: detector_hierarchy.py:65
python.detector_hierarchy.DetectorHierarchy.__init__
def __init__(self, hierarchy, depth=0, parent=None)
Definition: detector_hierarchy.py:52
python.detector_hierarchy.DetectorHierarchy
Definition: detector_hierarchy.py:51
python.detector_hierarchy.DetectorHierarchy.depth
depth
Definition: detector_hierarchy.py:57
PyAthena::repr
std::string repr(PyObject *o)
returns the string representation of a python object equivalent of calling repr(o) in python
Definition: PyAthenaUtils.cxx:106
histSizes.list
def list(name, path='/')
Definition: histSizes.py:38
TCS::join
std::string join(const std::vector< std::string > &v, const char c=',')
Definition: Trigger/TrigT1/L1Topo/L1TopoCommon/Root/StringUtils.cxx:10
python.detector_hierarchy.test
def test()
Definition: detector_hierarchy.py:110
python.detector_hierarchy.DetectorHierarchy.name
name
Definition: detector_hierarchy.py:77
pickleTool.object
object
Definition: pickleTool.py:30
python.detector_hierarchy.DetectorHierarchy.max_sub_width
max_sub_width
Definition: detector_hierarchy.py:69
dbg::print
void print(std::FILE *stream, std::format_string< Args... > fmt, Args &&... args)
Definition: SGImplSvc.cxx:70
python.detector_hierarchy.DetectorHierarchy.__repr__
def __repr__(self)
Definition: detector_hierarchy.py:89