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] = [x for x in ndiscriminator[structure] if x != 0]
47 
48  return [len(set(ndiscriminator[structure]))
49  for structure in discriminator]
50 
51  def create_list(self, MillePedeUser):
52  # loop over output TTree
53  for entry in MillePedeUser:
54  # check which structures were aligned
55  if (entry.ObjId != 1 and 999999 not in map(abs, entry.Par)):
56  # check if structure is not yet in the list
57  if not any(x.name == str(entry.Name) for x in self.structures):
58  # create new structure object
59  name = str(entry.Name)
60  subdetid = self.get_subdetid(entry.ObjId)
61  discriminator = self.get_discriminator(entry.ObjId)
62  ndiscriminator = self.get_ndiscriminator(entry.ObjId)
63  # create structure
64  self.structures.append(
65  Structure(name, subdetid, discriminator, ndiscriminator))
66  # add detids which belong to this structure
67  self.structures[-1].detids = self.get_detids(subdetid)
68 
70  for struct in self.structures:
71  # loop over discriminators -> create patterns
72  # pattern {"half": 2, "side": 2, "layer": 6, ...}
73  ranges = struct.ndiscriminator
74  pranges = [range(1, x+1) for x in ranges]
75  # loop over all possible combinations of the values of the
76  # discriminators
77  for number in itertools.product(*pranges):
78  # create pattern dict
79  pattern = dict(zip(map(lambda x: x.lower(), struct.discriminator), number))
80  # name out of pattern
81  name = " ".join("{0} {1}".format(key.lower(), value)
82  for (key, value) in pattern.items())
83  # get detids of child
84  detids = self.get_detids(struct.subdetid, pattern)
85  # create child and add it to parent
86  child = Structure(name, struct.subdetid, detids=detids)
87  struct.children.append(child)
88 
89 
90  def get_detids(self, subdetid, pattern={}):
91  # list of all detids in the structure
92  detids = []
93  # open TrackerTree.root file
94  treeFile = ROOT.TFile(os.path.join(self.config.jobDataPath,
95  ".TrackerTree.root"))
96  tree = treeFile.Get("TrackerTreeGenerator/TrackerTree/TrackerTree")
97 
98  for entry in tree:
99  # check if entry is part of the structure
100  if (entry.SubdetId == subdetid):
101  # to create a child also check the pattern
102  structure_found = False
103  for structure in ("Half", "Side", "Layer", "Rod", "Ring",
104  "Petal", "Blade", "Panel", "OuterInner",
105  "Module"):
106  if structure.lower() in pattern:
107  if getattr(entry, structure) != pattern[structure.lower()]:
108  structure_found = True
109  break
110  if structure_found: continue
111 
112  detids.append(entry.RawId)
113  return detids
114 
115 
116 class Structure:
117  """ A object represents a physical strucutre
118  """
119 
120  def __init__(self, name, subdetid, discriminator=[], ndiscriminator=[], detids=[]):
121  # name of the structure
122  self.name = name
123  # fields to identify the DetIds which belong to the structure
124  self.subdetid = subdetid
125  # fields which allow to discriminate the parts of the structure
126  self.discriminator = discriminator
127  # number per discriminator
128  self.ndiscriminator = ndiscriminator
129  # all DetIds which belong to this structure
130  self.detids = detids
131  # fieldss of all parts of the structure
132  self.children = []
133 
134  def get_name(self):
135  return self.name
136 
137  def get_children(self):
138  return self.children
139 
140  def contains_detid(self, detid):
141  if detid in self.detids:
142  return True
143  return False
def get_children(self)
Definition: geometry.py:137
bool any(const std::vector< T > &v, const T &what)
Definition: ECalSD.cc:37
def get_detids(self, subdetid, pattern={})
Definition: geometry.py:90
def get_name(self)
Definition: geometry.py:134
def get_subdetid(self, objid)
Definition: geometry.py:25
def contains_detid(self, detid)
Definition: geometry.py:140
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:69
def __init__(self, name, subdetid, discriminator=[], ndiscriminator=[], detids=[])
Definition: geometry.py:120
def get_discriminator(self, objid)
Definition: geometry.py:28
def create_list(self, MillePedeUser)
Definition: geometry.py:51
def get_ndiscriminator(self, objid)
Definition: geometry.py:31
#define str(s)