CMS 3D CMS Logo

geometry.py
Go to the documentation of this file.
1 ##########################################################################
2 # Classes which provide the geometry information.
3 
4 
5 import itertools
6 import os
7 
8 import ROOT
9 ROOT.PyConfig.IgnoreCommandLineOptions = True
10 ROOT.gROOT.SetBatch()
11 
12 import Alignment.MillePedeAlignmentAlgorithm.mpsvalidate.geometrydata as mpsv_geometrydata
13 
14 
15 class Alignables:
16  """ Creates a list of the aligned strucutres. Get the fields out of the
17  TrackerTree.root file.
18  """
19 
20  def __init__(self, config):
21  # list of Structure objects, contains structures which were aligned
22  self.structures = []
23  self.config = config
24 
25  def get_subdetid(self, objid):
26  return mpsv_geometrydata.data[objid].subdetid
27 
28  def get_discriminator(self, objid):
29  return mpsv_geometrydata.data[objid].discriminator
30 
31  def get_ndiscriminator(self, objid):
32  subdetid = self.get_subdetid(objid)
33  discriminator = self.get_discriminator(objid)
34  ndiscriminator = {key: [] for key in discriminator}
35  # open TrackerTree.root file
36  treeFile = ROOT.TFile(os.path.join(self.config.jobDataPath,
37  ".TrackerTree.root"))
38  tree = treeFile.Get("TrackerTreeGenerator/TrackerTree/TrackerTree")
39 
40  for entry in tree:
41  # check if entry is part of the structure
42  if (entry.SubdetId == subdetid):
43  for structure in discriminator:
44  ndiscriminator[structure].append(getattr(entry, structure))
45  for structure in discriminator:
46  ndiscriminator[structure] = filter(lambda x: x != 0,
47  ndiscriminator[structure])
48 
49  return [len(set(ndiscriminator[structure]))
50  for structure in discriminator]
51 
52  def create_list(self, MillePedeUser):
53  # loop over output TTree
54  for entry in MillePedeUser:
55  # check which structures were aligned
56  if (entry.ObjId != 1 and 999999 not in map(abs, entry.Par)):
57  # check if structure is not yet in the list
58  if not any(x.name == str(entry.Name) for x in self.structures):
59  # create new structure object
60  name = str(entry.Name)
61  subdetid = self.get_subdetid(entry.ObjId)
62  discriminator = self.get_discriminator(entry.ObjId)
63  ndiscriminator = self.get_ndiscriminator(entry.ObjId)
64  # create structure
65  self.structures.append(
66  Structure(name, subdetid, discriminator, ndiscriminator))
67  # add detids which belong to this structure
68  self.structures[-1].detids = self.get_detids(subdetid)
69 
71  for struct in self.structures:
72  # loop over discriminators -> create patterns
73  # pattern {"half": 2, "side": 2, "layer": 6, ...}
74  ranges = struct.ndiscriminator
75  pranges = [range(1, x+1) for x in ranges]
76  # loop over all possible combinations of the values of the
77  # discriminators
78  for number in itertools.product(*pranges):
79  # create pattern dict
80  pattern = dict(zip(map(lambda x: x.lower(), struct.discriminator), number))
81  # name out of pattern
82  name = " ".join("{0} {1}".format(key.lower(), value)
83  for (key, value) in pattern.items())
84  # get detids of child
85  detids = self.get_detids(struct.subdetid, pattern)
86  # create child and add it to parent
87  child = Structure(name, struct.subdetid, detids=detids)
88  struct.children.append(child)
89 
90 
91  def get_detids(self, subdetid, pattern={}):
92  # list of all detids in the structure
93  detids = []
94  # open TrackerTree.root file
95  treeFile = ROOT.TFile(os.path.join(self.config.jobDataPath,
96  ".TrackerTree.root"))
97  tree = treeFile.Get("TrackerTreeGenerator/TrackerTree/TrackerTree")
98 
99  for entry in tree:
100  # check if entry is part of the structure
101  if (entry.SubdetId == subdetid):
102  # to create a child also check the pattern
103  structure_found = False
104  for structure in ("Half", "Side", "Layer", "Rod", "Ring",
105  "Petal", "Blade", "Panel", "OuterInner",
106  "Module"):
107  if structure.lower() in pattern:
108  if getattr(entry, structure) != pattern[structure.lower()]:
109  structure_found = True
110  break
111  if structure_found: continue
112 
113  detids.append(entry.RawId)
114  return detids
115 
116 
117 class Structure:
118  """ A object represents a physical strucutre
119  """
120 
121  def __init__(self, name, subdetid, discriminator=[], ndiscriminator=[], detids=[]):
122  # name of the structure
123  self.name = name
124  # fields to identify the DetIds which belong to the structure
125  self.subdetid = subdetid
126  # fields which allow to discriminate the parts of the structure
127  self.discriminator = discriminator
128  # number per discriminator
129  self.ndiscriminator = ndiscriminator
130  # all DetIds which belong to this structure
131  self.detids = detids
132  # fieldss of all parts of the structure
133  self.children = []
134 
135  def get_name(self):
136  return self.name
137 
138  def get_children(self):
139  return self.children
140 
141  def contains_detid(self, detid):
142  if detid in self.detids:
143  return True
144  return False
def get_children(self)
Definition: geometry.py:138
bool any(const std::vector< T > &v, const T &what)
Definition: ECalSD.cc:37
def get_detids(self, subdetid, pattern={})
Definition: geometry.py:91
def get_name(self)
Definition: geometry.py:135
def get_subdetid(self, objid)
Definition: geometry.py:25
def contains_detid(self, detid)
Definition: geometry.py:141
OutputIterator zip(InputIterator1 first1, InputIterator1 last1, InputIterator2 first2, InputIterator2 last2, OutputIterator result, Compare comp)
def __init__(self, config)
Definition: geometry.py:20
static std::string join(char **cmd)
Definition: RemoteFile.cc:18
def create_children_list(self)
Definition: geometry.py:70
def __init__(self, name, subdetid, discriminator=[], ndiscriminator=[], detids=[])
Definition: geometry.py:121
def get_discriminator(self, objid)
Definition: geometry.py:28
def create_list(self, MillePedeUser)
Definition: geometry.py:52
def get_ndiscriminator(self, objid)
Definition: geometry.py:31