febshutoff_auto.py
Go to the documentation of this file.
1 #This script will be used to report on FEBs that were active and went inactive during a run
2 #can also be applied in reverse, inactive to active due to some reset
3 
4 from ROOT import *
5 import sys
6 import os
7 from datetime import datetime, timedelta
8 from time import strftime, strptime
9 
10 #User specifies run at runtime
11 run = sys.argv[1]
12 #Change with new release
13 softrel = 'S13-02-16'
14 
15 #specify data directory.
16 datapath = '/scratch/nearline/NDOS/'+softrel+'/000'+run[0:3]+'/'+run+'/cosmic/'
17 
18 #Get a sorted list of subruns involved in run
19 if os.path.exists(datapath):
20  dcfiles = [f for f in sorted(os.listdir(datapath)) if (f.startswith('reco_') and f.endswith('_hist.root'))]
21 
22 else:
23  print run, "has not been processed"
24  sys.exit(1)
25 
26 if len(dcfiles)==0:
27  print run, "has no reco files"
28  sys.exit(2)
29 
30 #Read in password
31 dbpswfile = os.environ.get('NOVADBPWDFILE')
32 dbpsw =''
33 pfile = open(dbpswfile,'r')
34 for line in pfile:
35  dbpsw = line.rstrip()
36  break
37 pfile.close()
38 
39 #Get start and end time of run from database
40 query = 'SELECT MIN(firstnonemptyeventtime), MAX(lastnonemptyeventtime) FROM ndos.nearline_metrics'
41 query+=" WHERE triggertype = 2 AND softwarerelease='"+softrel+"' AND run ="+run+" AND firstnonemptyeventtime > TIMESTAMP'2010-01-01' "
42 query+=" GROUP BY run;"
43 
44 db= TSQLServer.Connect("pgsql://ifdbprod.fnal.gov:5433/nova_prod", "nova_reader", dbpsw)
45 if not db:
46  print "Cannot connect to database"
47  sys.exit(3)
48 
49 stmt = db.Statement(query,1000)
50 if stmt.Process():
51  stmt.StoreResult()
52  while stmt.NextResultRow():
53  start = stmt.GetString(0)
54  end = stmt.GetString(1)
55 else:
56  print "Problem with Statement"
57  sys.exit(4)
58 
59 #Clean up connection info
60 del stmt
61 
62 #Indeces are subrun, dcm, feb
63 dcm=[]
64 
65 #Create an error buffer to be written to the file later
66 errors=''
67 
68 #Step through files
69 for file in dcfiles:
70  #Check file size before opening file
71  if os.path.getsize(datapath+file)<1000:
72 # print file, "Is broken, filesize too small"
73  errors+=file+' '+'Is broken, filesize too small\n'
74  continue
75 
76  #Open the Rootfile and pull out histgrams after checking if DataCheck was run on file
77  rootfile = TFile(datapath+file,'READ')
78  if rootfile.TestBit(TFile.kRecovered):
79 # print "Warning bad write to", file
80  errors+=file+' '+'has recovered keys\n'
81  rootfile.Close()
82  continue
83 
84  if not rootfile.cd("DataCheck"):
85 # print file, "Does not have DataCheck subdirectory"
86  errors+=file+' '+'Does not have DataCheck subdirectory\n'
87  continue
88 
89  dcm.append([])
90  hists=[]
91  hists.append(rootfile.Get("DataCheck/fDCM1_1_FebVEvent"))
92  hists.append(rootfile.Get("DataCheck/fDCM1_2_FebVEvent"))
93  hists.append(rootfile.Get("DataCheck/fDCM1_3_FebVEvent"))
94  hists.append(rootfile.Get("DataCheck/fDCM2_1_FebVEvent"))
95  hists.append(rootfile.Get("DataCheck/fDCM2_2_FebVEvent"))
96  hists.append(rootfile.Get("DataCheck/fDCM2_3_FebVEvent"))
97  hists.append(rootfile.Get("DataCheck/fDCM3_1_FebVEvent"))
98  hists.append(rootfile.Get("DataCheck/fDCM3_2_FebVEvent"))
99  hists.append(rootfile.Get("DataCheck/fDCM3_3_FebVEvent"))
100  hists.append(rootfile.Get("DataCheck/fDCM4_1_FebVEvent"))
101  hists.append(rootfile.Get("DataCheck/fDCM4_2_FebVEvent"))
102 
103  #Step through each FEB in the histogram and note whether it was active
104  for hist in hists:
105  dcm[-1].append([])
106  for feb in range(1,65):
107  integral=hist.Integral(1,hist.GetNbinsX(),feb,feb)
108  if integral>0.0:
109  dcm[-1][-1].append(1)
110  else:
111  dcm[-1][-1].append(0)
112 
113  del hists
114  rootfile.Close()
115 
116 #Things in production database are in UTC so need to convert and be smart about transition.
117 day = timedelta(days=1)
118 hour = timedelta(hours=1)
119 minute = timedelta(minutes=1)
120 
121 startdt = datetime.strptime(start,"%Y-%m-%d %H:%M:%S")
122 
123 dstoff = datetime(startdt.year,11,1,1,30,0)
124 while dstoff.weekday()<6:
125  dstoff=dstoff+day
126 
127 dston = datetime(startdt.year,3,8,3,0,0)
128 while dston.weekday()<6:
129  dston=dston+day
130 
131 if startdt < dstoff and startdt > dston:
132  hoffset = 5*hour
133 else:
134  hoffset = 6*hour
135 
136 startutc = (startdt+hoffset).strftime("%Y-%m-%d %H:%M:%S")
137 partutc = (startdt+6*minute+hoffset).strftime("%Y-%m-%d %H:%M:%S")
138 endutc = (datetime.strptime(end,"%Y-%m-%d %H:%M:%S")+hoffset).strftime("%Y-%m-%d %H:%M:%S")
139 
140 #///////////////////////////////////////////////////////////////////////////////////////////////////////////
141 
142 
143 #Look up type of coating in the hardware db
144 hdb = TSQLServer.Connect("pgsql://ifdbprod.fnal.gov/nova_hardware", "nova_reader", dbpsw)
145 if not hdb:
146  print "Cannot connect to hardware database"
147  sys.exit(5)
148 
149 #Query to select installed APD at the time of run
150 query1= "select hw_id, coating, dcm_name, dcm_port "
151 query1+="from hardware_position where hw_type = 'APD' and (remove_date is null or remove_date > TIMESTAMP'"+end+"') "
152 query1+="order by dcm_name, dcm_port, install_date desc;"
153 
154 hstmt = hdb.Statement(query1,1000)
155 if hstmt.Process():
156  id=[]
157  coat=[]
158  hdcm=[]
159  hfeb=[]
160  hstmt.StoreResult()
161  while hstmt.NextResultRow():
162  id.append(hstmt.GetString(0))
163  coat.append(hstmt.GetString(1))
164  hdcm.append(hstmt.GetString(2))
165  hfeb.append(hstmt.GetString(3))
166 
167 else:
168  print "Problem with Hardware Statement"
169  sys.exit(6)
170 
171 #Clean up this connection
172 hdb.Close()
173 del hdb
174 del hstmt
175 
176 apd=[]
177 #Let's make things easier to work with
178 for i in range(len(hdcm)):
179  if '01-01' in hdcm[i]:
180  hdcm[i] = '1-1'
181  elif '01-02' in hdcm[i]:
182  hdcm[i] = '1-2'
183  elif '01-03' in hdcm[i]:
184  hdcm[i] = '1-3'
185  elif '02-01' in hdcm[i]:
186  hdcm[i] = '2-1'
187  elif '02-02' in hdcm[i]:
188  hdcm[i] = '2-2'
189  elif '02-03' in hdcm[i]:
190  hdcm[i] = '2-3'
191  elif '03-01' in hdcm[i]:
192  hdcm[i] = '3-1'
193  elif '03-02' in hdcm[i]:
194  hdcm[i] = '3-2'
195  elif '03-03' in hdcm[i]:
196  hdcm[i] = '3-3'
197  elif '04-01' in hdcm[i]:
198  hdcm[i] = '4-1'
199  elif '04-02' in hdcm[i]:
200  hdcm[i] = '4-2'
201  else:
202  print "DCM name in Hardware Database is out of range", hdcm[i]
203  sys.exit(7)
204 
205  #Distinguish coatings
206  if coat[i]=='P':
207  if '(X3)' in id[i]:
208  coat[i]='New Parylene'
209  else:
210  coat[i]='Used Parylene'
211  elif coat[i]=='U':
212  coat[i]='Un'
213  elif coat[i]=='S':
214  coat[i]='Silicon'
215  else:
216  print "Coating type in Hardware Database is out of range", coat[i]
217  sys.exit(8)
218 
219  #Concatenate dcm name and port
220  apd.append(hdcm[i]+':'+hfeb[i])
221 
222 #///////////////////////////////////////////////////////////////////////////////////
223 
224 #Get cooling information
225 query2 = 'SELECT channel_name, value FROM ndos.dcs_report'
226 query2+=" WHERE channel_name LIKE '%_tecc.RVAL' AND time_stamp in"
227 query2+=" (SELECT MIN(time_stamp) FROM ndos.dcs_report "
228 query2+=" WHERE channel_name LIKE '%_tecc.RVAL' AND time_stamp > TIMESTAMP'"+startutc+"'"
229 query2+=" AND time_stamp < TIMESTAMP'"+partutc+"'"
230 query2+=" GROUP BY channel_name)"
231 query2+=" ORDER BY channel_name;"
232 
233 #cdb= TSQLServer.Connect("pgsql://novadbprod.fnal.gov:5433/nova_prod", "nova_reader", dbpsw)
234 #if not cdb:
235 # print "Cannot connect to production database"
236 # sys.exit(9)
237 
238 stmt2 = db.Statement(query2,1000)
239 if stmt2.Process():
240  cooled=[]
241  port=[]
242  cdcm=[]
243  capd=[]
244  stmt2.StoreResult()
245  while stmt2.NextResultRow():
246  chan = stmt2.GetString(0)[:-10]
247  port.append(chan[12:])
248  if '01-01' in chan:
249  cdcm.append('1-1')
250  elif '01-02' in chan:
251  cdcm.append('1-2')
252  elif '01-03' in chan:
253  cdcm.append('1-3')
254  elif '02-01' in chan:
255  cdcm.append('2-1')
256  elif '02-02' in chan:
257  cdcm.append('2-2')
258  elif '02-03' in chan:
259  cdcm.append('2-3')
260  elif '03-01' in chan:
261  cdcm.append('3-1')
262  elif '03-02' in chan:
263  cdcm.append('3-2')
264  elif '03-03' in chan:
265  cdcm.append('3-3')
266  elif '04-01' in chan:
267  cdcm.append('4-1')
268  elif '04-02' in chan:
269  cdcm.append('4-2')
270  else:
271  cdcm.append('error')
272  print "DCM name in Production Database is out of range", chan
273  sys.exit(10)
274 
275  capd.append(cdcm[-1]+':'+port[-1])
276 
277  val=stmt2.GetInt(1)
278  if val == 0:
279  cooled.append('Uninstrumented')
280  elif val<2000:
281  cooled.append('Not Cooled')
282  elif val>2000 and val != 4095:
283  cooled.append('Cooled')
284  else:
285  cooled.append('Error')
286 else:
287  print "Problem with Cooling Statement"
288  sys.exit(11)
289 
290 #Cleanup this connection
291 del stmt2
292 db.Close()
293 del db
294 
295 #Make an array to hold previous status
296 prevstat=[]
297 for i in range(len(dcm[0])):
298  prevstat.append([])
299  for j in range(len(dcm[0][i])):
300  prevstat[i].append(dcm[0][i][j])
301 
302 dcmlist=['1-1','1-2','1-3','2-1','2-2','2-3','3-1','3-2','3-3','4-1','4-2']
303 
304 #This will be the textfile to hold information about the run processed
305 outfile = open('/home/nearline/jancode/results/'+run+'febshutoff_auto.txt','w')
306 
307 #Have some information about the run in the file
308 outfile.write("Run: "+run+'\n')
309 outfile.write("From: "+start+'\n')
310 outfile.write("To: "+end+'\n')
311 outfile.write("Subruns: "+str(len(dcm))+'\n')
312 
313 #Now time to analyze the arrays for FEBs turning off
314 changes=0
315 for i in range(1,len(dcm)):
316 
317  #Check for run meltdown
318  afebs=0
319  for a in range(len(dcm[i])):
320  afebs+=sum(dcm[i][a])
321  if afebs == 0:
322  errors+='The run died in subrun '+str(i-1)+'\n'
323  break
324  for j in range(len(dcm[i])):
325  for k in range(len(dcm[i][j])):
326  prev = prevstat[j][k]
327  curr = dcm[i][j][k]
328  if prev != curr:
329  changes+=1
330 
331  #Find coating information
332  coating = coat[apd.index(dcmlist[j]+':'+str(k))]
333  coolstat= cooled[capd.index(dcmlist[j]+':'+str(k))]
334  if prev > curr:
335  outfile.write('FEB'+dcmlist[j]+':'+str(k)+' shutoff in subrun '+str(i-1)+' is '+coating+' coated and was '+coolstat+'\n')
336  else:
337  outfile.write('FEB'+dcmlist[j]+':'+str(k)+' came back in subrun '+str(i)+' is '+coating+' coated and was '+coolstat+'\n')
338  prevstat[j][k]=dcm[i][j][k]
339 
340 #Write errors to file
341 if changes == 0:
342  outfile.write("There were no shutoffs in this run\n")
343 outfile.write(errors)
344 outfile.close()
procfile open("FD_BRL_v0.txt")
Double_t sum
Definition: plot.C:31