1 | #!/usr/bin/env -S mysql -h ihp-pc45.ethz.ch
|
---|
2 |
|
---|
3 | -- You might want to change the table name
|
---|
4 | CREATE TABLE Calibration
|
---|
5 | (
|
---|
6 | WITH Table1 AS
|
---|
7 | (
|
---|
8 | SELECT
|
---|
9 | -- NIGHT and RUNID of the data file
|
---|
10 | R1.Telescope AS Telescope,
|
---|
11 | R1.NIGHT AS NIGHT,
|
---|
12 | R1.RUNID AS RUNID,
|
---|
13 | -- NIGHT and RUNID of the DRS file
|
---|
14 | R2.NIGHT AS DrsNight,
|
---|
15 | R2.RUNID AS DrsRunID,
|
---|
16 | -- Time difference between the two
|
---|
17 | LEAST(
|
---|
18 | ABS(UNIX_TIMESTAMP(R2.`DATE-OBS`)-UNIX_TIMESTAMP(R1.`DATE-OBS`)),
|
---|
19 | ABS(UNIX_TIMESTAMP(R2.`DATE-OBS`)-UNIX_TIMESTAMP(R1.`DATE-END`)),
|
---|
20 | ABS(UNIX_TIMESTAMP(R2.`DATE-END`)-UNIX_TIMESTAMP(R1.`DATE-OBS`)),
|
---|
21 | ABS(UNIX_TIMESTAMP(R2.`DATE-END`)-UNIX_TIMESTAMP(R1.`DATE-END`))
|
---|
22 | ) AS DeltaT,
|
---|
23 | -- Assign a row number to each combination of data-file and drs-file
|
---|
24 | -- after sorting them by time difference
|
---|
25 | ROW_NUMBER() OVER(PARTITION BY R1.NIGHT, R1.RUNID ORDER BY
|
---|
26 | LEAST(
|
---|
27 | ABS(UNIX_TIMESTAMP(R2.`DATE-OBS`)-UNIX_TIMESTAMP(R1.`DATE-OBS`)),
|
---|
28 | ABS(UNIX_TIMESTAMP(R2.`DATE-OBS`)-UNIX_TIMESTAMP(R1.`DATE-END`)),
|
---|
29 | ABS(UNIX_TIMESTAMP(R2.`DATE-END`)-UNIX_TIMESTAMP(R1.`DATE-OBS`)),
|
---|
30 | ABS(UNIX_TIMESTAMP(R2.`DATE-END`)-UNIX_TIMESTAMP(R1.`DATE-END`))
|
---|
31 | )
|
---|
32 | ) AS RowNumber
|
---|
33 | FROM
|
---|
34 | RawData R1
|
---|
35 | -- Join the table with itself to get all possible combinations of runs
|
---|
36 | CROSS JOIN
|
---|
37 | RawData R2
|
---|
38 | -- Join the DRS files with the table that tells you whether they can be used
|
---|
39 | -- if the table does not (yet) exists, this can be omitted
|
---|
40 | LEFT JOIN
|
---|
41 | DoNotUse ON (R2.NIGHT=DoNotUse.NIGHT AND R2.RUNID=DoNotUse.RUNID)
|
---|
42 | WHERE
|
---|
43 | -- Of course claibration must be from the same telescope
|
---|
44 | R1.Telescope=R2.Telescope
|
---|
45 | AND
|
---|
46 | -- Select all combinations that have a data file in R1 and a drs file (at step 1) in R2
|
---|
47 | ISNULL(R1.DRSSTEP) AND R2.DRSSTEP=1
|
---|
48 | AND
|
---|
49 | -- Only DRS files with 100 events and the correct ROI are considered
|
---|
50 | R2.ZNAXIS2=1000 AND R1.NROI=R2.NROI
|
---|
51 | AND
|
---|
52 | -- Check if they are valid
|
---|
53 | IFNULL(DoNotUse.WrongVoltage,false)!=true
|
---|
54 | -- This can be used to restrict the number of combinations
|
---|
55 | -- AND ABS(UNIX_TIMESTAMP(R2.`DATE-OBS`)-UNIX_TIMESTAMP(R1.`DATE-OBS`))<78*3600
|
---|
56 | ORDER BY
|
---|
57 | R1.Telescope,
|
---|
58 | R1.NIGHT,
|
---|
59 | R1.RUNID,
|
---|
60 | DeltaT
|
---|
61 | )
|
---|
62 | SELECT
|
---|
63 | Telescope,
|
---|
64 | NIGHT,
|
---|
65 | RUNID,
|
---|
66 | DrsNight,
|
---|
67 | DrsRunId,
|
---|
68 | DeltaT/3600e0 AS DeltaT
|
---|
69 | FROM
|
---|
70 | Table1
|
---|
71 | WHERE
|
---|
72 | -- Select only the rows with the smallest DeltaT
|
---|
73 | RowNumber=1
|
---|
74 | ) |
---|