-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathReadCMSPixSheet.py
146 lines (109 loc) · 4.25 KB
/
ReadCMSPixSheet.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
#!/usr/bin/env python
#import gspread
import glob, os, sys
from oauth2client.service_account import ServiceAccountCredentials
import datetime
sys.path.insert(0, '../gspread')
import gspread
today = datetime.date.today()
def getSheet():
scope = ['https://spreadsheets.google.com/feeds']
credentials = ServiceAccountCredentials.from_json_keyfile_name('gspread-test.json', scope)
gc = gspread.authorize(credentials)
sheet = gc.open_by_url('https://docs.google.com/spreadsheets/d/1xc7pZBfNwF1VRmnEr-IGaT7nDnJNE03y77gmejgzxKU/edit#gid=0').sheet1
return sheet
def getGlobalSettings(setups):
Settings = []
for setup in setups:
cells = rl.range(rl.get_addr_int(setup.row, setup.col + 2)+":"+rl.get_addr_int(setup.row+5, setup.col + 2))
cells = list(x.value for x in cells)
cells.append(int(setup.row))
print cells
s = globalSettings(cells)
Settings.append(s)
return Settings
def getLocalSettings(setups):
Settings = []
for setup in setups:
cells = rl.range(rl.get_addr_int(setup.row, setup.col + 1)+":"+rl.get_addr_int(setup.row, setup.col + 2))
cells = list(x.value for x in cells)
cells.append(int(setup.row))
print cells
s = localSettings(cells)
Settings.append(s)
return Settings
def getAllRuns(runs):
AllRuns = []
for run in runs:
cells = rl.range(rl.get_addr_int(run.row, run.col + 3)+":"+rl.get_addr_int(run.row, run.col + 4))
cells = list(x.value for x in cells)
cells.append(int(run.row))
r = cmsrun(cells)
AllRuns.append(r)
return AllRuns
class globalSettings:
def __init__(self, (chip, refchip, geo, gain, refgain, gev, row)):
self.chip = chip
self.refchip = refchip
self.geo = self.createGeoFile(geo)
self.gain = 'calibrations/chip'+chip+'/'+gain
self.refgain = 'calibrations/chip'+refchip+'/'+refgain
self.gev = gev
self.row = row
def __repr__(self):
return "\nchip %s\nrefchip %s\ngeo %s\ngain %s\nrefgain %s\nGeV %s\n" % ( self.chip, self.refchip, self.geo, self.gain, self.refgain, self.gev)
def createGeoFile(self,geo):
geoPositions = [x for x in geo.split( ) if x.replace(".", "", 1).isdigit()]
replacements = dict()
for i in xrange(0,6):
replacements['ZPOS'+str(i)] = geoPositions[i]
inputFileName = 'geo_tmp.dat'
outputFileName = 'geo'
for elem in list(geo.split( )):
outputFileName += '_'+elem
outputFileName += '.dat'
infile = open(inputFileName).read()
if not os.path.isfile(outputFileName):
outfile = open(outputFileName, 'w')
outfile.write('# geometry ' + outputFileName.replace('.dat',''))
for i in replacements.keys():
infile = infile.replace(i, replacements[i])
outfile.write(infile)
outfile.close
return outputFileName
class localSettings:
def __init__(self, (setting, value, row)):
self.setting = setting
self.value = value
self.row = row
def __repr__(self):
if 'gain' in self.setting:
return "\n%s calibrations/chip%s/%s" % ( self.setting, self.value[1:4], self.value)
else:
return "\n%s %s" % ( self.setting, self.value)
class cmsrun:
def __init__(self, (tilt, runnumber, row)):
self.runnumber = runnumber
self.tilt = tilt
self.row = row
def __repr__(self):
if self.tilt is not "":
return "\ntilt %s\nrun %s" % ( self.tilt, self.runnumber)
else:
return "run %s" % ( self.runnumber)
if __name__ == "__main__":
rl = getSheet()
GlobalSettings = getGlobalSettings( rl.findall("Global") )
LocalSettings = getLocalSettings( rl.findall("Local") )
allRuns = getAllRuns(rl.findall("*"))
all = GlobalSettings + LocalSettings + allRuns
dictAll = {}
for obj in all:
dictAll[obj.row] = obj
filename = "runs_"+"{:%d_%b_%Y}".format(today)+".dat"
f = open(filename,"w")
print >> f, 'weib 3 # gain file format for 2015'
for key in sorted(dictAll):
print >> f, dictAll[key]
print "created output file: ",filename
f.close()