APDGainPoints.py
Go to the documentation of this file.
1 import psycopg2
2 import csv
3 #import datetime
4 import time
5 import argparse
6 import os
7 import code
8 from ROOT import TH1F,gROOT,TCanvas
9 
10 gs=[]
11 # When ROOT objects get garbage-collected they disappear from the screen
12 # again. Need to leak them like in C++
13 def New(cons, *args):
14  ret = cons(*args)
15  gs.append(ret)
16  return ret
17 
18 c1 = New(TCanvas,"c1","c1")
19 c1.SetLogy(1)
20 c2 = New(TCanvas,"c2","c2")
21 c2.SetLogy(1)
22 m100Histo = New(TH1F, "gain100","gain100",600,0,300)
23 m150Histo = New(TH1F,"gain150","gain150",600,0,300)
24 pixelRatio = New(TH1F,"pixelRatio","pixelRatio",400,0,3)
25 FEBRatio = New(TH1F,"FEBRatio","FEBRatio",800,0,3)
26 
27 # try connecting to the database
28 # use the Fermilab (FL) database for testing information and the Ash River (AR)
29 # for the most up-to-date installation information
30 
31 #read in the password for the db
32 pswdloc = os.environ['NOVADBPWDFILE']
33 pswdfile = open(pswdloc, "r")
34 pswd = pswdfile.read()
35 
36 SQL = "dbname=nova_hardware host=ifdbprod.fnal.gov user=nova_reader password={} port=5432".format(pswd)
37 
38 try:
39  connFL = psycopg2.connect(SQL)
40 except:
41  print "Unable to connect to the database"
42  exit(0)
43 
44 
45 curFL = connFL.cursor()
46 SQL = "set search_path to ashriverprod_factory;"
47 curFL.execute(SQL)
48 
49 APDinstall_file = open("apd_installation.txt", "r")
50 APDinstall_list = csv.reader(APDinstall_file)
51 
52 for Location in APDinstall_list :
53  print "APD ",Location[2]," installed at ",Location[0],Location[1]
54  apdseqno = str(Location[2])
55 #for apdseqno in xrange(14216,14217):
56 
57  #SQL = "select * from public.apd_raw_data where apd_id like '%12345' and target_degc < 0 order by batch_no DESC "
58  SQL = "select * from public.apd_raw_data where apd_id like '%"+ apdseqno + "' and target_degc < 0 order by batch_no DESC "
59  curFL.execute(SQL)
60  rows = curFL.fetchall()
61  #print "retrieved ",len(rows),"\n"
62  if len(rows) >= 62 :
63  print len(rows)," rows returned for ",apdseqno
64  #break
65 
66  currentTable = [[0 for x in range(45)] for y in range(62)]
67  gainTable = []
68  #print currentTable
69  rownumber = 0
70  for row in rows:
71  #print(row)
72  #print len(row)
73  for rowindex in range(0,len(row)):
74  #print rownumber, rowindex, row[rowindex]
75  if rownumber < 62:
76  currentTable[rownumber][rowindex]=row[rowindex]
77  rownumber = rownumber +1
78  #print currentTable
79  # We now have the currentTable filled with LED off/on currents for all 32 pixels at all 31 voltages
80  photoCurrent = []
81  illumination = []
82  voltageArray = []
83  pixelGain = []
84  averageGain = []
85 
86  resultsFile = open("APD_pixel_gain_V.csv", "w+")
87 
88  for pixel in xrange(1,33):
89  illumination.append((currentTable[1+31][pixel+12]+currentTable[2+31][pixel+12]+
90  currentTable[3+31][pixel+12]-currentTable[1][pixel+12]-
91  currentTable[2][pixel+12]-currentTable[3][pixel+12])/3.0)
92  #print illumination
93  for voltageIndex in xrange(0,30):
94  voltageArray.append(currentTable[voltageIndex][11]-1.0e6*illumination[0])
95  gainSum = 0
96  for pixel in xrange(1,33):
97  #if len(pixelGain) < 32:
98  pixelGain.append((currentTable[voltageIndex+31][pixel+12]-
99  currentTable[voltageIndex][pixel+12])/illumination[pixel-1])
100  #else:
101  # pixelGain[pixel-1]=(currentTable[voltageIndex+31][pixel+12]-
102  # currentTable[voltageIndex][pixel+12])/illumination[pixel-1]
103  gainSum += pixelGain[voltageIndex*32+pixel-1]
104  #m100Histo.Fill(pixelGain[voltageIndex*32+pixel-1])
105  #print pixel,voltageArray[len(voltageArray)-1],pixelGain[voltageIndex*32+len(pixelGain)-1]
106  #print 'Average gain ' ,voltageArray[len(voltageArray)-1],gainSum/32.0
107  if len(averageGain)<31:
108  averageGain.append(gainSum/32.0)
109  else:
110  averageGain[voltageIndex]=gainSum/32.0
111  csvoutfmt = '{}, '*5 + '{:6.2f}, '*34 + '\n' # + '{:6.2f}\n'
112  #print csvoutfmt
113  csvout = csvoutfmt.format(row[0],row[1],row[2],row[3],row[4],voltageArray[len(voltageArray)-1],gainSum/32.0,
114  pixelGain[voltageIndex*32+0],
115  pixelGain[voltageIndex*32+1],
116  pixelGain[voltageIndex*32+2],
117  pixelGain[voltageIndex*32+3],
118  pixelGain[voltageIndex*32+4],
119  pixelGain[voltageIndex*32+5],
120  pixelGain[voltageIndex*32+6],
121  pixelGain[voltageIndex*32+7],
122  pixelGain[voltageIndex*32+8],
123  pixelGain[voltageIndex*32+9],
124  pixelGain[voltageIndex*32+10],
125  pixelGain[voltageIndex*32+11],
126  pixelGain[voltageIndex*32+12],
127  pixelGain[voltageIndex*32+13],
128  pixelGain[voltageIndex*32+14],
129  pixelGain[voltageIndex*32+15],
130  pixelGain[voltageIndex*32+16],
131  pixelGain[voltageIndex*32+17],
132  pixelGain[voltageIndex*32+18],
133  pixelGain[voltageIndex*32+19],
134  pixelGain[voltageIndex*32+20],
135  pixelGain[voltageIndex*32+21],
136  pixelGain[voltageIndex*32+22],
137  pixelGain[voltageIndex*32+23],
138  pixelGain[voltageIndex*32+24],
139  pixelGain[voltageIndex*32+25],
140  pixelGain[voltageIndex*32+26],
141  pixelGain[voltageIndex*32+27],
142  pixelGain[voltageIndex*32+28],
143  pixelGain[voltageIndex*32+29],
144  pixelGain[voltageIndex*32+30],
145  pixelGain[voltageIndex*32+31])
146  resultsFile.write(csvout)
147  #print "Average Gain of ",apdseqno, "at voltage ",voltageArray[len(voltageArray)-1],\
148  # " is ",gainSum/32.0," which should be the same as ",averageGain[len(averageGain)-1],"\n"
149  #print averageGain
150  #print "\n"
151 
152  #raw = (apd_raw_id,days_stamp,batch_no,apd_id,target_degc,elap_time,led,ref_diode_a,tec_degc,tec_pc,thmstr_ohms,source_v#,source_a,pix_a1,pix_a2,pix_a3,pix_a4,pix_a5,pix_a6,pix_a7,pix_a8,pix_b1,pix_b2,pix_b3,pix_b4,pix_b5,pix_b6,pix_b7,pix_#b8,pix_c1,pix_c2,pix_c3,pix_c4,pix_c5,pix_c6,pix_c7,pix_c8,pix_d1,pix_d2,pix_d3,pix_d4,pix_d5,pix_d6,pix_d7,pix_d8)
153 
154  resultsFile.close()
155 
156  # now have average gain for voltage voltageArray(index) in averageGain(index) array
157  # find linear interpolation of gain for finding gain 100 linearly interpolated from nearest points
158  voltageIndex = 0
159  voltage100 = 0
160  for voltage in voltageArray:
161  #print voltageIndex,voltage,averageGain[voltageIndex-1],averageGain[voltageIndex]
162  if (voltageIndex > 0 and averageGain[voltageIndex] >= 100.0 and averageGain[voltageIndex-1] < 100.0):
163  voltage100 = (voltageArray[voltageIndex-1]+
164  (100.0-averageGain[voltageIndex-1])*
165  (voltageArray[voltageIndex]-voltageArray[voltageIndex-1])/
166  (averageGain[voltageIndex]-averageGain[voltageIndex-1]))
167  #print "Voltage at gain 100 = ",voltage100," at ",voltageIndex,"\n"
168  print "Voltage at gain 100 = ",voltage100," for APD ",apdseqno
169  voltageIndex = voltageIndex + 1
170  voltage150=1.02*voltage100
171 
172  # Then find gain of each pixel at the two voltage point voltage100 and voltage150 from the gaintable.
173  voltageIndex = 0
174  for voltage in voltageArray:
175  #print voltageIndex,voltage,averageGain[voltageIndex-1],averageGain[voltageIndex]
176  if (voltageIndex > 0 and voltageArray[voltageIndex-1] < voltage100 and voltage > voltage100):
177  #compute gain for each pixel at V100
178  FEBGain100 = 0
179  FEBGain150 = 0
180  for pixel in range(0,32):
181  pixelgain100 = (pixelGain[pixel+32*(voltageIndex-1)]+
182  (pixelGain[32*voltageIndex+pixel]-pixelGain[32*(voltageIndex-1)+pixel])*
183  (voltage100-voltageArray[voltageIndex-1])/
184  (voltageArray[voltageIndex]-voltageArray[voltageIndex-1]))
185  m100Histo.Fill(pixelgain100)
186  FEBGain100=FEBGain100+pixelgain100/32.0
187  #print "Voltage at gain 100 = ",voltage100," at ",voltageIndex,"\n"
188  #print "Voltage at gain 100 = ",voltage100," for APD ",apdseqno
189  if (voltageIndex > 0 and voltageArray[voltageIndex-1] < voltage150 and voltage > voltage150):
190  #compute gain for each pixel at V150
191  for pixel in range(0,32):
192  pixelgain150 = (pixelGain[pixel+32*(voltageIndex-1)]+
193  (pixelGain[32*voltageIndex+pixel]-pixelGain[32*(voltageIndex-1)+pixel])*
194  (voltage150-voltageArray[voltageIndex-1])/
195  (voltageArray[voltageIndex]-voltageArray[voltageIndex-1]))
196  m150Histo.Fill(pixelgain150)
197  FEBGain150=FEBGain150+pixelgain150/32.0
198  pixelRatio.Fill(pixelgain150/pixelgain100)
199  if ((pixelgain150/pixelgain100) < 0.8):
200  print "APD ",Location[2], " installed at ",\
201  Location[0],Location[1], " ratio" , \
202  pixelgain150/pixelgain100, " for pixel ",pixel
203  #print "Voltage at gain 100 = ",voltage100," at ",voltageIndex,"\n"
204  #print "Voltage at gain 100 = ",voltage100," for APD ",apdseqno
205  voltageIndex = voltageIndex + 1
206  FEBRatio.Fill(FEBGain150/FEBGain100)
207  c1.cd()
208  m100Histo.Draw()
209  m150Histo.Draw("SAME")
210  c1.Update()
211  c2.cd()
212  pixelRatio.Draw()
213  FEBRatio.Draw("SAME")
214  c2.Update()
215 
216 APDinstall_file.close()
217 
218 
219 #time.sleep(10)
220 
221 # Just want the console to hang around so the user can look at the plots
222 # without them disappearing. Ctrl-D or "exit()" to quit.
223 print 'Ctrl-D to quit'
224 code.interact(local = locals(), banner='')
def New(cons, args)
std::string format(const int32_t &value, const int &ndigits=8)
Definition: HexUtils.cpp:14
procfile open("FD_BRL_v0.txt")
exit(0)