25 from __future__
import print_function
26 import sys, os, datetime
32 calibs = [
'crt_channelstatus_data',
36 'dqdx_xshape_plane0_data',
37 'dqdx_xshape_plane0_mc',
38 'dqdx_xshape_plane1_data',
39 'dqdx_xshape_plane1_mc',
40 'dqdx_xshape_plane2_data',
41 'dqdx_xshape_plane2_mc',
42 'dqdx_yz_plane0_data',
44 'dqdx_yz_plane1_data',
46 'dqdx_yz_plane2_data',
48 'electronicscalib_data',
50 'lightyieldscale_data']
52 calibs_dev = [
'crt_channelstatus_data',
56 'dqdx_xshape_plane0_data',
57 'dqdx_xshape_plane1_data',
58 'dqdx_xshape_plane2_data',
59 'dqdx_yz_plane0_data',
61 'dqdx_yz_plane1_data',
63 'dqdx_yz_plane2_data',
65 'electronicscalib_data',
67 'lightyieldscale_data']
71 suffixes = [
'tags',
'iovs',
'tag_iovs',
'data']
79 if args[0] ==
'-d' or args[0] ==
'--devel' :
84 print(
'Unknown option %s' % args[0])
92 host = os.environ[
'PUB_PSQL_READER_HOST']
93 port = int(os.environ[
'PUB_PSQL_READER_PORT'])
94 user = os.environ[
'PUB_PSQL_READER_USER']
95 db = os.environ[
'PUB_PSQL_READER_DB']
96 pw = os.environ[
'PUB_PSQL_READER_PASS']
98 print(
"Source ~/.sqlaccess/prod_access.sh first.")
102 host =
'ifdb04.fnal.gov'
103 db =
'microboone_dev'
106 conn = psycopg2.connect(host=host, port=port, dbname=db, user=user, password=pw)
113 print(
'Calibration database %s.' % calib)
117 sqlite_database =
'%s.db' % calib
118 if os.path.exists(sqlite_database):
119 os.remove(sqlite_database)
120 sqlite_conn = sqlite3.connect(sqlite_database)
121 sqlite_cur = sqlite_conn.cursor()
129 for suffix
in suffixes:
133 table_name =
'%s_%s' % (calib, suffix)
134 schema[table_name] = []
135 print(
'Processing table %s.' % table_name)
139 qtbl =
'CREATE TABLE IF NOT EXISTS %s (' % table_name
143 q =
'select column_name, data_type, is_nullable from information_schema.columns where table_name=%s'
144 cur.execute(q, (table_name,))
145 rows = cur.fetchall()
152 schema[table_name].append(column_name)
160 if data_type ==
'integer':
161 sqlite_type =
'integer'
162 elif data_type ==
'bigint':
163 sqlite_type =
'integer'
164 elif data_type ==
'text':
166 elif data_type ==
'timestamp without time zone':
167 sqlite_type =
'integer'
168 elif data_type ==
'boolean':
169 sqlite_type =
'integer'
170 elif data_type ==
'real':
172 if sqlite_type ==
'':
173 print(
'Unknown type %s' % data_type)
178 qtbl +=
'%s %s' % (column_name, sqlite_type)
182 if first
and (suffix ==
'tags' or suffix ==
'iovs'):
183 qtbl +=
' PRIMARY KEY'
190 if suffix ==
'tag_iovs':
191 qtbl +=
', FOREIGN KEY (iov_id) REFERENCES %s_iovs(iov_id)' % calib
192 qtbl +=
', FOREIGN KEY (tag) REFERENCES %s_tags(tag)' % calib
193 elif suffix ==
'data':
194 qtbl +=
', FOREIGN KEY (__iov_id) REFERENCES %s_iovs(iov_id)' % calib
200 sqlite_cur.execute(qtbl)
208 q =
'SELECT MAX(__iov_id) FROM %s' % table_name
212 print(
'Maximum iov_id = %d' % max_iov_id)
216 for iov_id
in range(max_iov_id + 1):
222 for column
in schema[table_name]:
228 q +=
' FROM %s' % table_name
233 print(
'iov id = %d' % iov_id)
234 q +=
' WHERE __iov_id = %d' % iov_id
239 rows = cur.fetchall()
240 print(
'%d rows fetched.' % len(rows))
241 now = datetime.datetime.now()
246 q =
'INSERT INTO %s (' % table_name
250 for column
in schema[table_name]:
258 values.append(int(element.strftime(
'%s')))
260 values.append(element)
266 sqlite_cur.execute(q, tuple(values))
do one_file $F done echo for F in find $TOP name CMakeLists txt print