diff --git a/scripts/STLibrary/CreateDetectors.py b/scripts/STLibrary/CreateDetectors.py index bbeaff7..8fd6a87 100644 --- a/scripts/STLibrary/CreateDetectors.py +++ b/scripts/STLibrary/CreateDetectors.py @@ -13,54 +13,56 @@ """ - import pickle pickleDir = '../Pickle/' def create_TT(): - f = open(pickleDir+'NameList.pkl') - NameList = pickle.load(f) + f = open(pickleDir + 'NameList.pkl') + NameList = pickle.load(f) TT = {} - layers = ['TTaU','TTaX','TTbV','TTbX'] - region = ['RegionA','RegionB','RegionC'] + layers = ['TTaU', 'TTaX', 'TTbV', 'TTbX'] + region = ['RegionA', 'RegionB', 'RegionC'] for l in layers: - TT[l]={} + TT[l] = {} for r in region: - TT[l][r]={} + TT[l][r] = {} nSectors = 25 if ('a' in l) and ('B' in r): nSectors = 19 if ('b' in l) and ('B' in r): nSectors = 27 - for s in range(1,nSectors+1): - if l+r+'Sector'+str(s) in NameList['TTNames']: - TT[l][r][str(s)] = l+r+'Sector'+str(s) + for s in range(1, nSectors + 1): + if l + r + 'Sector' + str(s) in NameList['TTNames']: + TT[l][r][str(s)] = l + r + 'Sector' + str(s) return TT + def create_IT(): - f = open(pickleDir+'NameList.pkl') - NameList = pickle.load(f) + f = open(pickleDir + 'NameList.pkl') + NameList = pickle.load(f) IT = {} - ITs = ['IT1','IT2','IT3'] - side = ['ASide','CSide','Bottom','Top'] - xuw = ['X1','X2','U','V'] + ITs = ['IT1', 'IT2', 'IT3'] + side = ['ASide', 'CSide', 'Bottom', 'Top'] + xuw = ['X1', 'X2', 'U', 'V'] for i in ITs: - IT[i]={} + IT[i] = {} for s in side: - IT[i][s]= {} + IT[i][s] = {} for x in xuw: - IT[i][s][x]={} - for n in range(1,8): - if i+s+x+'Sector'+str(n) in NameList['ITNames']: - IT[i][s][x][str(n)]=i+s+x+'Sector'+str(n) + IT[i][s][x] = {} + for n in range(1, 8): + if i + s + x + 'Sector' + str(n) in NameList['ITNames']: + IT[i][s][x][str(n)] = i + s + x + 'Sector' + str(n) return IT + def get_IT_layers(IT): ITlayers = {} for station in IT.keys(): for box in IT[station].keys(): for striptype in IT[station][box]: - ITlayers[station+box+striptype] = IT[station][box][striptype] - return ITlayers \ No newline at end of file + ITlayers[station + box + + striptype] = IT[station][box][striptype] + return ITlayers diff --git a/scripts/STLibrary/STChannelID.py b/scripts/STLibrary/STChannelID.py index bbeebf5..1d4cf46 100644 --- a/scripts/STLibrary/STChannelID.py +++ b/scripts/STLibrary/STChannelID.py @@ -7,10 +7,10 @@ In [2]: STNames().uniqueSectorName(id) Out[2]: 'IT1ASideX1Sector2' """ -__author__ = "Elena Graverini" -__email__ = "elena.graverini@cern.ch" +__author__ = "Elena Graverini" +__email__ = "elena.graverini@cern.ch" __version__ = "1.0" -__date__ = "03/12/2014" +__date__ = "03/12/2014" class STChannelID(): @@ -18,38 +18,47 @@ STChannelID: retrieve detector type, station, region, layer, sector and strip from an LHCb::STChannelID integer. """ + def __init__(self, m_channelID): - self.channelID = long(m_channelID) - self.stripBits = 0 - self.sectorBits = 10 - self.detRegionBits = 15 - self.layerBits = 18 - self.stationBits = 21 - self.typeBits = 23 - self.stripMask = 0x3ffL - self.sectorMask = 0x7c00L - self.detRegionMask = 0x38000L - self.layerMask = 0x1c0000L - self.stationMask = 0x600000L - self.typeMask = 0x1800000L - self.typeTT = 0 - self.typeIT = 1 - self.uniqueLayerMask = self.layerMask + self.stationMask + self.channelID = long(m_channelID) + self.stripBits = 0 + self.sectorBits = 10 + self.detRegionBits = 15 + self.layerBits = 18 + self.stationBits = 21 + self.typeBits = 23 + self.stripMask = 0x3ffL + self.sectorMask = 0x7c00L + self.detRegionMask = 0x38000L + self.layerMask = 0x1c0000L + self.stationMask = 0x600000L + self.typeMask = 0x1800000L + self.typeTT = 0 + self.typeIT = 1 + self.uniqueLayerMask = self.layerMask + self.stationMask self.uniqueDetRegionMask = self.detRegionMask + self.layerMask + self.stationMask - self.uniqueSectorMask = self.sectorMask + self.detRegionMask + self.layerMask + self.stationMask + self.uniqueSectorMask = self.sectorMask + \ + self.detRegionMask + self.layerMask + self.stationMask self.type = ((self.channelID & self.typeMask) >> self.typeBits) self.isTT = (self.type == self.typeTT) self.isIT = (self.type == self.typeIT) - self.station = ((self.channelID & self.stationMask) >> self.stationBits) - self.detRegion = ((self.channelID & self.detRegionMask) >> self.detRegionBits) - self.uniqueDetRegion = ((self.channelID & self.uniqueDetRegionMask) >> self.detRegionBits) + self.station = ((self.channelID & self.stationMask) + >> self.stationBits) + self.detRegion = ((self.channelID & self.detRegionMask) + >> self.detRegionBits) + self.uniqueDetRegion = ( + (self.channelID & self.uniqueDetRegionMask) >> self.detRegionBits) self.layer = ((self.channelID & self.layerMask) >> self.layerBits) - self.uniqueLayer = ((self.channelID & self.uniqueLayerMask) >> self.layerBits) + self.uniqueLayer = ( + (self.channelID & self.uniqueLayerMask) >> self.layerBits) self.sector = ((self.channelID & self.sectorMask) >> self.sectorBits) - self.uniqueSector = ((self.channelID & self.uniqueSectorMask) >> self.sectorBits) - self.strip = ((self.channelID & self.stripMask) >> self.stripBits) # goes from 1 to 384 (IT) or 1 to 512 (TT) - #if self.detRegion == 3: - # print self.channelID, self.type, self.station, self.layer, self.detRegion, self.sector + self.uniqueSector = ( + (self.channelID & self.uniqueSectorMask) >> self.sectorBits) + # goes from 1 to 384 (IT) or 1 to 512 (TT) + self.strip = ((self.channelID & self.stripMask) >> self.stripBits) + # if self.detRegion == 3: + # print self.channelID, self.type, self.station, self.layer, + # self.detRegion, self.sector class STNames(): @@ -57,14 +66,15 @@ STNames: retrieve detector name, station name, region name, layer name and sector name from an STChannelID object. """ + def __init__(self): self.TTstations = ["a", "b"] self.TTlayers = [["X", "U"], ["V", "X"]] self.TTuniqueLayers = [] - for (ind,s) in enumerate(self.TTstations): + for (ind, s) in enumerate(self.TTstations): for l in self.TTlayers[ind]: - self.TTuniqueLayers.append('TT'+s+l) - self.TTregions = ["C", "B", "A"]#["A", "B", "C"] + self.TTuniqueLayers.append('TT' + s + l) + self.TTregions = ["C", "B", "A"] # ["A", "B", "C"] self.ITlayers = ["X1", "U", "V", "X2"] self.ITboxes = ["CSide", "ASide", "Bottom", "Top"] @@ -76,32 +86,32 @@ def stationName(self, id): if id.isTT: - return self.TTstations[id.station-1] + return self.TTstations[id.station - 1] else: return str(id.station) def regionName(self, id): if id.isTT: - return "Region" + self.TTregions[id.detRegion-1] + return "Region" + self.TTregions[id.detRegion - 1] else: - return self.ITboxes[id.detRegion-1] + return self.ITboxes[id.detRegion - 1] def layerName(self, id): if id.isTT: - return self.TTlayers[id.station-1][id.layer-1] + return self.TTlayers[id.station - 1][id.layer - 1] else: - return self.ITlayers[id.layer-1] + return self.ITlayers[id.layer - 1] def uniqueLayerName(self, id): if id.isTT: - name = ( self.detectorName(id) + name = (self.detectorName(id) + self.stationName(id) - + self.layerName(id) ) + + self.layerName(id)) else: - name = ( self.detectorName(id) + name = (self.detectorName(id) + self.stationName(id) + self.regionName(id) - + self.layerName(id) ) + + self.layerName(id)) return name def sectorName(self, id): @@ -109,12 +119,12 @@ def uniqueSectorName(self, id): if id.isTT: - name = ( self.uniqueLayerName(id) + name = (self.uniqueLayerName(id) + self.regionName(id) - + self.sectorName(id) ) + + self.sectorName(id)) else: - name = ( self.uniqueLayerName(id) - + self.sectorName(id) ) + name = (self.uniqueLayerName(id) + + self.sectorName(id)) return name def locateTTSector(self, uSectorName): @@ -122,4 +132,4 @@ region, sector = uSectorName.split('Region')[1].split('Sector') return uLayer, region, int(sector) -STNames_instance = STNames() \ No newline at end of file +STNames_instance = STNames() diff --git a/scripts/STLibrary/STPositions.py b/scripts/STLibrary/STPositions.py index 3233dc5..9931e2d 100644 --- a/scripts/STLibrary/STPositions.py +++ b/scripts/STLibrary/STPositions.py @@ -3,62 +3,70 @@ STPositions.py: retrieve TT and IT sector positions (previously saved in an ntuple). """ -__author__ = "Elena Graverini" -__email__ = "elena.graverini@cern.ch" +__author__ = "Elena Graverini" +__email__ = "elena.graverini@cern.ch" __version__ = "1.0" -__date__ = "09/01/2015" +__date__ = "09/01/2015" -import sys, os +import sys +import os import pickle import ROOT as r from STChannelID import * from TTModules import * + def retrieveSTPositions(tracker, filename, verbose=False): f = r.TFile(filename, 'read') - t = f.Get(tracker+'HitEfficiencyTuple/SectorPositions') + t = f.Get(tracker + 'HitEfficiencyTuple/SectorPositions') positions = {} if verbose: for sector in t: print STNames().uniqueSectorName(STChannelID(int(sector.sectorID))) - for (i,x) in enumerate(sector.sensor_x): + for (i, x) in enumerate(sector.sensor_x): print i, x, sector.sensor_y[i], sector.sensor_z[i] for sector in t: name = STNames().uniqueSectorName(STChannelID(int(sector.sectorID))) - positions[ name ] = {} + positions[name] = {} xm, ym, zm = 0., 0., 0. - for (i,x) in enumerate(sector.sensor_x): - positions[ name ][ i ] = ( x, sector.sensor_y[i], sector.sensor_z[i] ) + for (i, x) in enumerate(sector.sensor_x): + positions[name][i] = (x, sector.sensor_y[i], sector.sensor_z[i]) xm += x ym += sector.sensor_y[i] zm += sector.sensor_z[i] - positions[ name ][ 'average' ] = ( xm/len(sector.sensor_x), ym/len(sector.sensor_x), zm/len(sector.sensor_x) ) + positions[name]['average'] = ( + xm / len(sector.sensor_x), ym / len(sector.sensor_x), zm / len(sector.sensor_x)) return positions + def savePositionsToPickle(tracker, filename, verbose=False): positions = retrieveSTPositions(tracker, filename, verbose) filename = filename.split('/')[-1] path = os.path.abspath(__file__).split("Scripts")[0] + "Pickle/" - output = open(path+filename.replace('.root','-%spositions.pkl'%tracker), 'wb') + output = open(path + filename.replace('.root', + '-%spositions.pkl' % tracker), 'wb') pickle.dump(positions, output) output.close() + def loadPositionsFromPickle(picklefile): f = open(picklefile) positions = pickle.load(f) f.close() return positions + def TTHalfModuleAvgPosition(halfModuleId, positions): uLayer, region, moduleNum, position = locateTTHalfModule(halfModuleId) index = 0 if 'b' in position: index = 1 - sectors = TTModulesMap().dictOfHalfModules[uLayer]['Region'+region][moduleNum][index].sectors + sectors = TTModulesMap().dictOfHalfModules[uLayer][ + 'Region' + region][moduleNum][index].sectors xm, ym, zm = 0., 0., 0. for uName in sectors: xm += positions[uName]['average'][0] ym += positions[uName]['average'][1] zm += positions[uName]['average'][2] - return ( xm/len(sectors), ym/len(sectors), zm/len(sectors) ) \ No newline at end of file + return (xm / len(sectors), ym / len(sectors), zm / len(sectors)) diff --git a/scripts/STLibrary/TTModules.py b/scripts/STLibrary/TTModules.py index d0aeb2d..e900fce 100644 --- a/scripts/STLibrary/TTModules.py +++ b/scripts/STLibrary/TTModules.py @@ -5,55 +5,60 @@ Modules are numbered per unique layer and region, starting from 0 at the largest x (rightest). """ -__author__ = "Elena Graverini" -__email__ = "elena.graverini@cern.ch" +__author__ = "Elena Graverini" +__email__ = "elena.graverini@cern.ch" __version__ = "1.0" -__date__ = "18/12/2014" +__date__ = "18/12/2014" -import sys, os, pickle +import sys +import os +import pickle from STChannelID import * - class TTModulesMap(): """ Module-wise map of the TT detector. Members: - numberOfModules: number of modules per layer / region - - dictOfModules: module-wise dictionary of TTModules - - dictOfHalfModules: half-module-wise dictionary of TTModules + - dictOfModules: module-wise dictionary of TTModules + - dictOfHalfModules: half-module-wise dictionary of TTModules - findModule(id): find the module containing the given STChannelID - findSectorModule(name): find the module containing the given sector - findHalfModule(id): find the half module containing the given STChannelID - - findSectorHalfModule(name): find the half module containing the given sector + - findSectorHalfModule(name): find the half module containing the given sector """ + def __init__(self): - self.numberOfModules = { 'TTaX': { 'RegionA': 6, 'RegionB': 3, 'RegionC': 6 }, - 'TTaU': { 'RegionA': 6, 'RegionB': 3, 'RegionC': 6 }, - 'TTbV': { 'RegionA': 6, 'RegionB': 5, 'RegionC': 6 }, - 'TTbX': { 'RegionA': 6, 'RegionB': 5, 'RegionC': 6 } } + self.numberOfModules = {'TTaX': {'RegionA': 6, 'RegionB': 3, 'RegionC': 6}, + 'TTaU': {'RegionA': 6, 'RegionB': 3, 'RegionC': 6}, + 'TTbV': {'RegionA': 6, 'RegionB': 5, 'RegionC': 6}, + 'TTbX': {'RegionA': 6, 'RegionB': 5, 'RegionC': 6}} self.dictOfModules = {} self.dictOfHalfModules = {} for uniqueLayer in STNames_instance.TTuniqueLayers: self.dictOfModules[uniqueLayer] = {} self.dictOfHalfModules[uniqueLayer] = {} for region in STNames_instance.TTregions: - self.dictOfModules[uniqueLayer]['Region'+region] = [] - self.dictOfHalfModules[uniqueLayer]['Region'+region] = {} - nModules = self.numberOfModules[uniqueLayer]['Region'+region] + self.dictOfModules[uniqueLayer]['Region' + region] = [] + self.dictOfHalfModules[uniqueLayer]['Region' + region] = {} + nModules = self.numberOfModules[uniqueLayer]['Region' + region] for i in range(nModules): - nSectors = 4 # sectors per full module + nSectors = 4 # sectors per full module if 'B' in region: - nSectors = 6 # sectors per full module - if ('TTb' in uniqueLayer) and ( (i==0) or (i==nModules-1) ): - nSectors = 4 # sectors per full module - id = uniqueLayer+'Region'+region+'Module'+str(i) - newModule = TTModule( id, nSectors, True, uniqueLayer, 'Region'+region ) - self.dictOfModules[uniqueLayer]['Region'+region].append( newModule ) - self.dictOfHalfModules[uniqueLayer]['Region'+region][i] = [newModule.topHalf, newModule.bottomHalf] - #def find(inputData): + nSectors = 6 # sectors per full module + if ('TTb' in uniqueLayer) and ((i == 0) or (i == nModules - 1)): + nSectors = 4 # sectors per full module + id = uniqueLayer + 'Region' + region + 'Module' + str(i) + newModule = TTModule( + id, nSectors, True, uniqueLayer, 'Region' + region) + self.dictOfModules[uniqueLayer][ + 'Region' + region].append(newModule) + self.dictOfHalfModules[uniqueLayer][ + 'Region' + region][i] = [newModule.topHalf, newModule.bottomHalf] + # def find(inputData): # if type(inputData) == str: # return self.findByUniqueSector(inputData) # elif 'STChannelID' in str(inputData.__class__): @@ -63,6 +68,7 @@ # else: # print 'TTModulesMap ERROR: could not understand input!' # return False + def findModule(self, id): ul = STNames_instance.uniqueLayerName(id) reg = STNames_instance.regionName(id) @@ -71,93 +77,98 @@ print 'TTModulesMap ERROR: sector numbering starts from 0!!' sys.exit(0) if (reg != 'RegionB'): - iModule = (sector-1)/4 + iModule = (sector - 1) / 4 elif (reg == 'RegionB') and ('TTb' in ul): - if sector<5: - iModule = (sector-1)/4#%4 - if sector>22: + if sector < 5: + iModule = (sector - 1) / 4 # %4 + if sector > 22: iModule = 4 - if 5<=sector<=22: - iModule = (sector-1-4)/6+1 + if 5 <= sector <= 22: + iModule = (sector - 1 - 4) / 6 + 1 elif (reg == 'RegionB') and ('TTa' in ul): - iModule = (sector-1)/6#%6 - #print iModule, sector, ul, reg + iModule = (sector - 1) / 6 # %6 + # print iModule, sector, ul, reg return self.dictOfModules[ul][reg][iModule] + def findSectorModule(self, uSectorName): - uniqueLayer, region, sector = STNames_instance.locateTTSector(uSectorName) - reg = 'Region'+region + uniqueLayer, region, sector = STNames_instance.locateTTSector( + uSectorName) + reg = 'Region' + region if sector == 0: print 'TTModulesMap ERROR: sector numbering starts from 0!!' sys.exit(0) if (reg != 'RegionB'): - iModule = (sector-1)/4 + iModule = (sector - 1) / 4 elif (reg == 'RegionB') and ('TTb' in uniqueLayer): - if sector<5: - iModule = (sector-1)/4#%4 - if sector>22: + if sector < 5: + iModule = (sector - 1) / 4 # %4 + if sector > 22: iModule = 4 - if 5<=sector<=22: - iModule = (sector-1-4)/6+1 + if 5 <= sector <= 22: + iModule = (sector - 1 - 4) / 6 + 1 elif (reg == 'RegionB') and ('TTa' in uniqueLayer): - iModule = (sector-1)/6#%6 - #print iModule, sector, ul, reg + iModule = (sector - 1) / 6 # %6 + # print iModule, sector, ul, reg return self.dictOfModules[uniqueLayer][reg][iModule] + def findHalfModule(self, id): module = self.findModule(id) sector = int(id.sector) if 'B'in STNames_instance.regionName(id) and 'TTb' in STNames_instance.uniqueLayerName(id): - if 4= 3: + if 4 < sector < 23: + if (sector - 1 - 4) % 6 >= 3: return module.topHalf else: return module.bottomHalf - elif sector<=4: - if sector in [3,4]: + elif sector <= 4: + if sector in [3, 4]: return module.topHalf else: return module.bottomHalf - elif sector>=23: - if sector in [25,26]: + elif sector >= 23: + if sector in [25, 26]: return module.topHalf else: return module.bottomHalf elif 'B' in STNames_instance.regionName(id) and 'TTa' in STNames_instance.uniqueLayerName(id): - if (sector-1)%6 >= 3: + if (sector - 1) % 6 >= 3: return module.topHalf else: return module.bottomHalf - if (sector-1)%4 >= 2: + if (sector - 1) % 4 >= 2: return module.topHalf else: return module.bottomHalf + def findSectorHalfModule(self, uSectorName): module = self.findSectorModule(uSectorName) - uniqueLayer, region, sector = STNames_instance.locateTTSector(uSectorName) + uniqueLayer, region, sector = STNames_instance.locateTTSector( + uSectorName) if 'B'in region and 'TTb' in uniqueLayer: - if 4= 3: + if 4 < sector < 23: + if (sector - 1 - 4) % 6 >= 3: return module.topHalf else: return module.bottomHalf - elif sector<=4: - if sector in [3,4]: + elif sector <= 4: + if sector in [3, 4]: return module.topHalf else: return module.bottomHalf - elif sector>=23: - if sector in [25,26]: + elif sector >= 23: + if sector in [25, 26]: return module.topHalf else: - return module.bottomHalf + return module.bottomHalf elif 'B' in region and 'TTa' in uniqueLayer: - if (sector-1)%6 >= 3: + if (sector - 1) % 6 >= 3: return module.topHalf else: return module.bottomHalf - if (sector-1)%4 >= 2: + if (sector - 1) % 4 >= 2: return module.topHalf else: - return module.bottomHalf + return module.bottomHalf class TTModule(): @@ -175,36 +186,46 @@ - topHalf, bottomHalf (half modules if this is a full module) - sectors (list of unique identifiers for sectors contained in this full or half module) """ + def __init__(self, id, nSectors=4, parent=True, uniqueLayer='TTaX', region='RegionB', position='Top'): - self.id = str(id) - self.moduleNumber = int( self.id.split('Module')[-1].replace('b','').replace('t','') ) - self.nSectors = nSectors - self.parent = parent - self.uniqueLayer = uniqueLayer - self.region = region - self.position = position + self.id = str(id) + self.moduleNumber = int(self.id.split( + 'Module')[-1].replace('b', '').replace('t', '')) + self.nSectors = nSectors + self.parent = parent + self.uniqueLayer = uniqueLayer + self.region = region + self.position = position # Assigning half modules if parent: - self.topHalf = TTModule(id=self.id+'t', nSectors=self.nSectors/2, parent=False, uniqueLayer=self.uniqueLayer, region=self.region, position='Top') - self.bottomHalf = TTModule(id=self.id+'b', nSectors=self.nSectors/2, parent=False, uniqueLayer=self.uniqueLayer, region=self.region, position='Bottom') - self.position = None + self.topHalf = TTModule(id=self.id + 't', nSectors=self.nSectors / 2, parent=False, + uniqueLayer=self.uniqueLayer, region=self.region, position='Top') + self.bottomHalf = TTModule(id=self.id + 'b', nSectors=self.nSectors / 2, parent=False, + uniqueLayer=self.uniqueLayer, region=self.region, position='Bottom') + self.position = None # Assigning sectors - if not parent: nSectors = self.nSectors*2 + if not parent: + nSectors = self.nSectors * 2 if 'B'in self.region and 'TTb' in self.uniqueLayer: if self.moduleNumber == 0: - secNums = range(1,5) + secNums = range(1, 5) elif self.moduleNumber == 4: - secNums = range(23,27) - else: - secNums = [((self.moduleNumber-1)*nSectors + (i + 1 + 4)) for i in xrange(nSectors)] + secNums = range(23, 27) + else: + secNums = [((self.moduleNumber - 1) * nSectors + (i + 1 + 4)) + for i in xrange(nSectors)] else: - secNums = [((self.moduleNumber)*nSectors + (i + 1)) for i in xrange(nSectors)] + secNums = [((self.moduleNumber) * nSectors + (i + 1)) + for i in xrange(nSectors)] if self.position == 'Top': - self.sectors = [(self.uniqueLayer + self.region + 'Sector' + str(i)) for i in secNums[len(secNums)/2:]] + self.sectors = [(self.uniqueLayer + self.region + 'Sector' + str(i)) + for i in secNums[len(secNums) / 2:]] elif self.position == 'Bottom': - self.sectors = [(self.uniqueLayer + self.region + 'Sector' + str(i)) for i in secNums[:len(secNums)/2]] + self.sectors = [(self.uniqueLayer + self.region + 'Sector' + str(i)) + for i in secNums[:len(secNums) / 2]] elif self.position == None: - self.sectors = [(self.uniqueLayer + self.region + 'Sector' + str(i)) for i in secNums] + self.sectors = [(self.uniqueLayer + self.region + + 'Sector' + str(i)) for i in secNums] TTModulesMap_instance = TTModulesMap() @@ -213,31 +234,34 @@ def dictOfTTChannelIDs(save=True): bits = STChannelID(0) d = {} - for itype,typ in enumerate(('TT',)): - for istation,station in enumerate(('a','b'), start=1): - for ilayer,layer in enumerate( (("X", "U"), ("V", "X"))[istation-1], start=1 ): - for iregion,region in enumerate(("C","B","A"), start=1): - for module in TTModulesMap_instance.dictOfModules['TT'+station+layer]['Region'+region]: - for halfmodule in TTModulesMap_instance.dictOfHalfModules['TT'+station+layer]['Region'+region][module.moduleNumber]: - secNums = [ int(it) for it in sectorsInHalfModule(halfmodule.id)[-1] ] + for itype, typ in enumerate(('TT',)): + for istation, station in enumerate(('a', 'b'), start=1): + for ilayer, layer in enumerate((("X", "U"), ("V", "X"))[istation - 1], start=1): + for iregion, region in enumerate(("C", "B", "A"), start=1): + for module in TTModulesMap_instance.dictOfModules['TT' + station + layer]['Region' + region]: + for halfmodule in TTModulesMap_instance.dictOfHalfModules['TT' + station + layer]['Region' + region][module.moduleNumber]: + secNums = [int(it) for it in sectorsInHalfModule( + halfmodule.id)[-1]] for sector in secNums: - for i in xrange(1,513): - chanID = ( i - + (sector << bits.sectorBits) - + (iregion << bits.detRegionBits) - + (ilayer << bits.layerBits) - + (istation << bits.stationBits) - + (itype << bits.typeBits) ) - d[chanID] = { 'type':typ, 'station':station, 'layer':layer, 'region':region, - 'sector':sector, 'uniqueSector': 'TT%s%s'%(station,layer)+'Region%s'%region+'Sector%s'%sector, - 'module':halfmodule.id.replace('t','').replace('b',''), 'hm':halfmodule.id } + for i in xrange(1, 513): + chanID = (i + + (sector << bits.sectorBits) + + (iregion << bits.detRegionBits) + + (ilayer << bits.layerBits) + + (istation << bits.stationBits) + + (itype << bits.typeBits)) + d[chanID] = {'type': typ, 'station': station, 'layer': layer, 'region': region, + 'sector': sector, 'uniqueSector': 'TT%s%s' % (station, layer) + 'Region%s' % region + 'Sector%s' % sector, + 'module': halfmodule.id.replace('t', '').replace('b', ''), 'hm': halfmodule.id} + def write(d): pickle.dump(d, open('../Pickle/TTchIDlookupTable.pkl', 'wb')) - # + # if save: if os.path.isfile('../Pickle/TTchIDlookupTable.pkl'): - yes = raw_input("dictOfTTChannelIDs WARNING: file ../Pickle/TTchIDlookupTable.pkl already exists! Overwrite? (yes or no)\n\t") - if yes=='yes': + yes = raw_input( + "dictOfTTChannelIDs WARNING: file ../Pickle/TTchIDlookupTable.pkl already exists! Overwrite? (yes or no)\n\t") + if yes == 'yes': write(d) else: print "Skipping." @@ -246,8 +270,6 @@ return d - - def listOfTTHalfModules(): """ Flat list of unique half modules indentifiers. @@ -269,44 +291,51 @@ listOfHalfModules = listOfTTHalfModules() ttmap = TTModulesMap_instance pairs = [] - regions = {'A':1, 'B':2, 'C':3} + regions = {'A': 1, 'B': 2, 'C': 3} print "Overlapping TT half modules:" for hm1 in listOfHalfModules: for hm2 in listOfHalfModules: # they must be different - if hm1 == hm2: continue + if hm1 == hm2: + continue # they must be both on top or both on bottom - if locateTTHalfModule(hm1)[3] != locateTTHalfModule(hm2)[3]: continue + if locateTTHalfModule(hm1)[3] != locateTTHalfModule(hm2)[3]: + continue # they must be on the same layer - if locateTTHalfModule(hm1)[0] != locateTTHalfModule(hm2)[0]: continue + if locateTTHalfModule(hm1)[0] != locateTTHalfModule(hm2)[0]: + continue # avoid duplicates - if (hm1, hm2) in pairs: continue - if (hm2, hm1) in pairs: continue + if (hm1, hm2) in pairs: + continue + if (hm2, hm1) in pairs: + continue # they must be contiguous: if (locateTTHalfModule(hm1)[1] == locateTTHalfModule(hm2)[1]): if (abs(locateTTHalfModule(hm1)[2] - locateTTHalfModule(hm2)[2]) == 1): - pairs.append( (hm1, hm2) ) + pairs.append((hm1, hm2)) else: num1 = locateTTHalfModule(hm1)[2] num2 = locateTTHalfModule(hm2)[2] - max1 = ttmap.numberOfModules[locateTTHalfModule(hm1)[0]]['Region'+locateTTHalfModule(hm1)[1]] - 1 - max2 = ttmap.numberOfModules[locateTTHalfModule(hm2)[0]]['Region'+locateTTHalfModule(hm2)[1]] - 1 + max1 = ttmap.numberOfModules[locateTTHalfModule( + hm1)[0]]['Region' + locateTTHalfModule(hm1)[1]] - 1 + max2 = ttmap.numberOfModules[locateTTHalfModule( + hm2)[0]]['Region' + locateTTHalfModule(hm2)[1]] - 1 nreg1 = regions[locateTTHalfModule(hm1)[1]] nreg2 = regions[locateTTHalfModule(hm2)[1]] - if ( (num1==max1 and num2==0 and nreg2-nreg1==1) or (num2==max2 and num1==0 and nreg1-nreg2==1) ): - pairs.append( (hm1, hm2) ) + if ((num1 == max1 and num2 == 0 and nreg2 - nreg1 == 1) or (num2 == max2 and num1 == 0 and nreg1 - nreg2 == 1)): + pairs.append((hm1, hm2)) print '\t', hm1, hm2 - ## - same region - #if ((abs(locateTTHalfModule(hm1)[2] - locateTTHalfModule(hm2)[2]) != 1) + # - same region + # if ((abs(locateTTHalfModule(hm1)[2] - locateTTHalfModule(hm2)[2]) != 1) # and (locateTTHalfModule(hm1)[0] != locateTTHalfModule(hm2)[0])): continue - ## - or neighbouring region - #elif not ((locateTTHalfModule(hm1)[0] != locateTTHalfModule(hm2)[0]) + # - or neighbouring region + # elif not ((locateTTHalfModule(hm1)[0] != locateTTHalfModule(hm2)[0]) # and ( ( (ttmap.numberOfModules[locateTTHalfModule(hm1)[0]] == locateTTHalfModule(hm1)[2]+1 ) # and (locateTTHalfModule(hm2)[2] == 0) ) # or ( (ttmap.numberOfModules[locateTTHalfModule(hm2)[0]] == locateTTHalfModule(hm2)[2]+1 ) # and (locateTTHalfModule(hm1)[2] == 0) ) ) ): continue - ## append to list of pairs - #pairs.append( (hm1, hm2) ) + # append to list of pairs + # pairs.append( (hm1, hm2) ) print return pairs @@ -337,7 +366,8 @@ List of sectors and list of sector numbers belonging to a TT module. """ uLayer, region, moduleNum = locateTTModule(moduleId) - sectors = TTModulesMap_instance.dictOfModules[uLayer]['Region'+region][moduleNum].sectors + sectors = TTModulesMap_instance.dictOfModules[ + uLayer]['Region' + region][moduleNum].sectors sectorNumbers = [secName.split('Sector')[-1] for secName in sectors] return sectors, sectorNumbers @@ -350,7 +380,8 @@ index = 0 if 'b' in position: index = 1 - sectors = TTModulesMap_instance.dictOfHalfModules[uLayer]['Region'+region][moduleNum][index].sectors + sectors = TTModulesMap_instance.dictOfHalfModules[ + uLayer]['Region' + region][moduleNum][index].sectors sectorNumbers = [secName.split('Sector')[-1] for secName in sectors] return sectors, sectorNumbers @@ -364,18 +395,19 @@ """ layer, region, num, position = locateTTHalfModule(name) - reg = {'A': 'R3', 'B': 'R2', 'C':'R1'} - return layer+'Layer'+reg[region]+'Module'+str(num+1)+position.capitalize() + reg = {'A': 'R3', 'B': 'R2', 'C': 'R1'} + return layer + 'Layer' + reg[region] + 'Module' + str(num + 1) + position.capitalize() if __name__ == '__main__': import ROOT as r - f = r.TFile('../../RootFiles/EveryHit/runs131973-133785-end2012.root','read') + f = r.TFile( + '../../RootFiles/EveryHit/runs131973-133785-end2012.root', 'read') t = f.Get('TTHitEfficiencyTuple/TrackMonTuple') for (i, track) in enumerate(t): ids = track.clusterSTchanID chids = [long(id) for id in ids] - if len(chids)>=4: + if len(chids) >= 4: id = STChannelID(chids[3]) if not id.isTT(): print 'Not TT.'