1 | #!/bin/bash
|
---|
2 |
|
---|
3 | # setup
|
---|
4 | sqlpw=/home/$USER/.mysql.pw
|
---|
5 | dbname=factdata20170804
|
---|
6 |
|
---|
7 | # DB files
|
---|
8 | dbfile=/home/dorner/fact_db/factdata_2017-08-04.sql
|
---|
9 | dbfileold=/home/dorner/fact_db/factdata_cp20150811.sql
|
---|
10 |
|
---|
11 | # cu-function (QLA)
|
---|
12 | cuqla="CREATE FUNCTION CUQLA(n int) RETURNS double DETERMINISTIC RETURN
|
---|
13 | IF (n<20140520, 16.4, IF (n<20150131, 25.2, IF (n<20150716, 12.5,
|
---|
14 | IF (n<20160218, 20.0, IF (n<20160901, 15.1, 16.8)))))"
|
---|
15 | # define CUISDC accordingly
|
---|
16 |
|
---|
17 | # mjd-function
|
---|
18 | mjd="CREATE FUNCTION MJD(t datetime) RETURNS double DETERMINISTIC RETURN
|
---|
19 | ROUND(UNIX_TIMESTAMP(CONVERT_TZ(t, '+00:00', 'SYSTEM'))/86400.+40587,9) "
|
---|
20 |
|
---|
21 | lima="CREATE FUNCTION LiMa(sig double, bg double) RETURNS double DETERMINISTIC
|
---|
22 | RETURN IF (sig>0 AND bg*5>0 AND (sig*Log(sig/(sig+bg*5)*1.2/0.2)+bg*5*Log(bg*5/(sig+bg*5)*1.2))> 0,
|
---|
23 | Sqrt((sig*Log(sig/(sig+bg*5)*1.2/0.2)+bg*5*Log(bg*5/(sig+bg*5)*1.2))*2), 0) "
|
---|
24 |
|
---|
25 | excerr="CREATE FUNCTION ExcErr(sig double, bg double) RETURNS double DETERMINISTIC
|
---|
26 | RETURN IF ((sig + 0.2*0.2*bg*5)<0, 0, Sqrt(sig + 0.2*0.2*bg*5)) "
|
---|
27 |
|
---|
28 | combinedtable="create table AnalysisResultsAllQLA like AnalysisResultsRunLP"
|
---|
29 |
|
---|
30 | # set up DB
|
---|
31 | echo "read-in DB-copy $dbfile to DB $dbname..."
|
---|
32 | cat $dbfile | sed "s/factdata/$dbname/g" | mysql --defaults-file=$sqlpw -u root
|
---|
33 | # create functions
|
---|
34 | # (not needed if same computer and DB only updated)
|
---|
35 | echo "Creating funciton CUQLA..."
|
---|
36 | mysql --defaults-file=$sqlpw -u root $dbname -s -e "$cuqla"
|
---|
37 | echo "Creating funciton Mjd..."
|
---|
38 | mysql --defaults-file=$sqlpw -u root $dbname -s -e "$mjd"
|
---|
39 | echo "Creating funciton LiMa..."
|
---|
40 | mysql --defaults-file=$sqlpw -u root $dbname -s -e "$lima"
|
---|
41 | echo "Creating funciton ExcErr..."
|
---|
42 | mysql --defaults-file=$sqlpw -u root $dbname -s -e "$excerr"
|
---|
43 |
|
---|
44 | # create table with full QLA content
|
---|
45 | echo "create and populate table with full QLA results..."
|
---|
46 | drop="drop table if exists AnalysisResultsAllQLA"
|
---|
47 | create="create table AnalysisResultsAllQLA like AnalysisResultsRunLP"
|
---|
48 | copyqla="insert into AnalysisResultsAllQLA select * from AnalysisResultsRunLP"
|
---|
49 | copyold="insert into AnalysisResultsAllQLA select fRunID, fNight, fNumEvtsAfterCleaning, "
|
---|
50 | copyold=$copyold"fNumEvtsAfterQualCuts, fNumEvtsAfterBgCuts, fNumBgEvts, fNumSigEvts, fNumExcEvts, "
|
---|
51 | copyold=$copyold"fNumIslandsMean, fOnTimeAfterCuts, Now() from factdata_cp20150811.AnalysisResultsRunISDC fcp "
|
---|
52 | copyold=$copyold"where fNight<20121213"
|
---|
53 | mysql --defaults-file=$sqlpw -u root $dbname -s -e "$drop"
|
---|
54 | mysql --defaults-file=$sqlpw -u root $dbname -s -e "$create"
|
---|
55 | mysql --defaults-file=$sqlpw -u root $dbname -s -e "$copyqla"
|
---|
56 | mysql --defaults-file=$sqlpw -u root $dbname -s -e "$copyold"
|
---|
57 |
|
---|
58 | echo "done. Please check if table AnalysisResultsAllQLA is complete."
|
---|
59 |
|
---|
60 | checkdb="SELECT MIN(fNight), MAX(fNight), COUNT(*), COUNT(DISTINCT fNight) FROM "$dbname".AnalysisResultsAllQLA"
|
---|
61 | mysql --defaults-file=$sqlpw -u root $dbname -e "$checkdb"
|
---|
62 |
|
---|
63 | # create functions
|
---|
64 | exit
|
---|
65 |
|
---|
66 | # alternative way for CU-Function
|
---|
67 |
|
---|
68 | # creat table CU:
|
---|
69 | createcu="CREATE TABLE CU (fNight int(10) unsigned DEFAULT NULL,
|
---|
70 | fCU double(3,1) DEFAULT NULL, fLastUpdate timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
|
---|
71 | fValid tinyint(3) unsigned DEFAULT NULL, fAnalysis tinyint(3) unsigned DEFAULT '1')"
|
---|
72 |
|
---|
73 | # from 9.6.2017
|
---|
74 | #mysql> select * from CU;
|
---|
75 | #+----------+------+---------------------+--------+-----------+
|
---|
76 | #| fNight | fCU | fLastUpdate | fValid | fAnalysis |
|
---|
77 | #+----------+------+---------------------+--------+-----------+
|
---|
78 | # --- first iteration of CU:
|
---|
79 | #| 20111115 | 16.5 | 2017-05-30 20:50:15 | NULL | 1 |
|
---|
80 | #| 20140520 | 24.0 | 2017-05-30 20:50:15 | NULL | 1 |
|
---|
81 | #| 20150131 | 16.0 | 2017-05-30 20:50:15 | NULL | 1 |
|
---|
82 | #| 20150716 | 20.0 | 2017-05-30 20:50:15 | NULL | 1 |
|
---|
83 | #| 20160218 | 15.0 | 2017-05-30 20:50:15 | NULL | 1 |
|
---|
84 | #| 20160901 | 16.5 | 2017-05-30 20:50:15 | NULL | 1 |
|
---|
85 | # --- next iteration of CU:
|
---|
86 | #| 20121212 | 16.4 | 2017-05-30 21:07:26 | 1 | 1 |
|
---|
87 | #| 20140526 | 25.2 | 2017-05-30 21:08:28 | 1 | 1 |
|
---|
88 | #| 20150131 | 12.5 | 2017-05-30 21:08:51 | 1 | 1 |
|
---|
89 | #| 20150716 | 20.0 | 2017-05-30 21:09:09 | 1 | 1 |
|
---|
90 | #| 20160218 | 15.1 | 2017-05-30 21:09:42 | 1 | 1 |
|
---|
91 | #| 20160901 | 16.8 | 2017-05-30 21:10:15 | 1 | 1 |
|
---|
92 | #+----------+------+---------------------+--------+-----------+
|
---|
93 | #12 rows in set (0,00 sec)
|
---|
94 |
|
---|
95 | cuinsert="INSERT CU VALUES
|
---|
96 | (20121212, 16.4, '2017-05-30 21:07:26', 1, 1)
|
---|
97 | (20140526, 25.2, '2017-05-30 21:07:26', 1, 1)
|
---|
98 | (20150131, 12.5, '2017-05-30 21:07:26', 1, 1)
|
---|
99 | (20150716, 20.0, '2017-05-30 21:07:26', 1, 1)
|
---|
100 | (20160218, 15.1, '2017-05-30 21:07:26', 1, 1)
|
---|
101 | (20160901, 16.8, '2017-05-30 21:07:26', 1, 1) "
|
---|
102 |
|
---|
103 | delstart=" delimiter $$ "
|
---|
104 | delend=" delimiter ; "
|
---|
105 | cufkt="create function CU(night int, ana tinyint) returns double(4,1) deterministic begin declare cu double; set cu=0; select fCU into cu from CU where fValid=1 and fAnalysis=ana and fNight<night order by fNight desc limit 0,1; return cu; end $$ "
|
---|
106 |
|
---|