getGoodRuns4SAM.py
Go to the documentation of this file.
1 from ConDB import ConDB
2 from datetime import datetime, timedelta
3 from timelib import text2datetime, epoch
4 import sys, getopt, os
5 import psycopg2, psycopg2.extras
6 
7 Usage = """
8 python getGoodRuns4SAM.py [options]
9 options:
10  -U <user>
11  -w <password>
12  -T <tag> [REQUIRED]
13 """
14 
15 data_type = 'data'
16 tag = None
17 dbcon = []
18 dbcon.append("host=ifdbrep.fnal.gov port=5433 dbname=nova_prod ")
19 t0 = text2datetime("0")
20 t1 = text2datetime("100000000")
21 channel_range = None
22 username = "nova_reader"
23 
24 opts, args = getopt.getopt(sys.argv[1:], 'U:w:T:')
25 
26 if len(args) > 0 :
27  if args[0] == 'help':
28  print Usage
29  sys.exit(0)
30 
31 for opt, val in opts:
32  if opt == '-U': username = val.strip()
33  elif opt == '-w': dbcon.append("password=%s " % (val,))
34  elif opt == '-T': tag = val
35  else:
36  print "Unknown argument."
37  print Usage
38  sys.exit(0)
39 
40 if not tag:
41  print "Tag is required!"
42  sys.exit(1)
43 
44 dbcon.append("user=%s" % (username,))
45 
46 dbcon = ' '.join(dbcon)
47 tname = "fardet.dq_subrun_info"
48 columns = ["dbmask","flag"]
49 db = ConDB(dbcon)
50 table = db.table(tname, columns)
51 
52 sampwd = None
53 sampwdFile = None
54 try:
55  sampwdFile = os.environ["SAM4NOVAPWDFILE"]
56 except KeyError:
57  print "Please set the SAM4NOVAPWDFILE environment variable to point to the password file for access to the SAM database"
58  exit(1)
59 
60 with open(sampwdFile,"r") as fin:
61  sampwd = fin.readline().strip()
62 
63 
64 """ Establish connection to SAM database
65 """
66 try:
67  conn = psycopg2.connect("dbname=samdev host=cspgsdev.fnal.gov "\
68  "user=jpaley "\
69  "port=5433")
70  cur = conn.cursor(cursor_factory=psycopg2.extras.DictCursor)
71 except:
72  print "Unable to connect to the SAM database"
73  exit(1)
74 
75 
76 data = table.getDataIntervalIter(t0, t1, data_type=data_type, tag=tag,
77  channel_range = channel_range)
78 
79 n = 0
80 nwrote = 0
81 detname='fardet'
82 
83 fout = open("/tmp/goodRuns4SAM.csv","w")
84 
85 prev_diblock_mask = -1
86 prev_isGood = True
87 for c, tv, values in data:
88 
89  run = int(epoch(tv))/100
90  subrun = int(epoch(tv))%100
91  isGood=True
92  if (values[1] > 0):
93  isGood = False
94  diblock_mask = int(values[0])
95 
96  n += 1
97 
98  if ((prev_diblock_mask != diblock_mask) or (prev_isGood != isGood)):
99  line = '{0},{1},{2},{3},{4},{5}\n'.format(run,subrun,tag,diblock_mask,isGood,detname)
100 
101  fout.write(line)
102  prev_diblock_mask = diblock_mask
103  prev_isGood = isGood
104  nwrote += 1
105 
106 fout.close()
107 
108 print "read %d rows from the NOvA good runs database" % (n,)
109 
110 csvFile = open("/tmp/goodRuns4SAM.csv","r")
111 
112 cur.copy_from(csvFile,'nova.good_runs',columns=('run_number','subrun_number','tag','diblock_mask','good','detector'),sep=',')
113 
114 conn.commit()
115 cur.close()
116 conn.close()
117 
118 print "wrote %d rows to the SAM good runs database table" % (nwrote,)
std::string format(const int32_t &value, const int &ndigits=8)
Definition: HexUtils.cpp:14
procfile open("FD_BRL_v0.txt")
exit(0)