1 | WITH Theta AS -- Get Total Observation Time
|
---|
2 | (
|
---|
3 | SELECT SUM(OnTime) AS ObsTime FROM ThetaDist
|
---|
4 | ),
|
---|
5 | Area AS -- Get total simulated area
|
---|
6 | (
|
---|
7 | SELECT POW(MinImpactHi,2)*PI() AS Area FROM MonteCarloArea
|
---|
8 | ),
|
---|
9 | Binning AS -- Get binning and calculate weights for each bin
|
---|
10 | (
|
---|
11 | SELECT
|
---|
12 | *,
|
---|
13 | (POW(10,hi)-POW(10,lo))/1000 AS Width,
|
---|
14 | Area*ObsTime AS AreaTime
|
---|
15 | FROM
|
---|
16 | BinningEnergy_%100:binning
|
---|
17 | CROSS JOIN
|
---|
18 | Theta, Area
|
---|
19 | ),
|
---|
20 | Analyzed AS -- Summarizy data after cuts (analyzed) in bins
|
---|
21 | (
|
---|
22 | SELECT
|
---|
23 | `.%100:binning:_est`,
|
---|
24 |
|
---|
25 | SUM(SignalN) AS SignalN,
|
---|
26 | SUM(SignalW) AS SignalW,
|
---|
27 | SUM(SignalW2) AS SignalW2,
|
---|
28 |
|
---|
29 | SUM(BackgroundN) AS BackgroundN,
|
---|
30 | SUM(BackgroundW) AS BackgroundW,
|
---|
31 | SUM(BackgroundW2) AS BackgroundW2,
|
---|
32 |
|
---|
33 | SUM(ResidualW) AS ResidualW,
|
---|
34 | SUM(ResidualW2) AS ResidualW2,
|
---|
35 |
|
---|
36 | SUM(SumEnergySimW) AS SumEnergySimW,
|
---|
37 | SUM(SumEnergyEstW) AS SumEnergyEstW
|
---|
38 | FROM
|
---|
39 | AnalysisMC
|
---|
40 | GROUP BY
|
---|
41 | `.%100:binning:_est`
|
---|
42 | ),
|
---|
43 | Integrated AS
|
---|
44 | (
|
---|
45 | SELECT -- Return Result
|
---|
46 | *,
|
---|
47 |
|
---|
48 | SignalN - BackgroundN/5 AS ExcessN,
|
---|
49 |
|
---|
50 | SQRT(SignalN + BackgroundN/25) AS ErrExcessN,
|
---|
51 | SQRT(SignalN) AS ErrSignalN,
|
---|
52 | SQRT(BackgroundN) AS ErrBackgroundN,
|
---|
53 |
|
---|
54 | SignalW /Width/AreaTime AS SignalFluxW,
|
---|
55 | BackgroundW/5 /Width/AreaTime AS BackgroundFluxW,
|
---|
56 | (SignalW - BackgroundW/5) /Width/AreaTime AS ExcessFluxW,
|
---|
57 | SQRT(SignalW2) /Width/AreaTime AS ErrSignalFluxW,
|
---|
58 | SQRT(BackgroundW2)/5 /Width/AreaTime AS ErrBackgroundFluxW,
|
---|
59 | SQRT(SignalW2 + BackgroundW2/25) /Width/AreaTime AS ErrExcessFluxW,
|
---|
60 |
|
---|
61 | SumEnergyEstW/SignalW AS AvgEnergyEstW,
|
---|
62 | SumEnergySimW/SignalW AS AvgEnergySimW,
|
---|
63 |
|
---|
64 | IF(SignalW>0, ResidualW/SignalW, NULL) AS BiasW,
|
---|
65 | IF(SignalW>0, ResidualW/SignalW*SQRT(ResidualW2/POW(ResidualW,2) + SignalW2/POW(SignalW,2)), NULL) AS ErrBiasW,
|
---|
66 | IF(SignalW>0, SQRT(ResidualW2/SignalW - POW(ResidualW/SignalW, 2)), NULL) AS ResolutionW,
|
---|
67 |
|
---|
68 | (SUM(SignalW) OVER Integral) AS IntegralSignalW,
|
---|
69 | (SUM(SignalW) OVER Integral) / AreaTime AS IntegralSignalFluxW,
|
---|
70 | (SUM(BackgroundW) OVER Integral) / AreaTime AS IntegralBackgroundFluxW,
|
---|
71 | (SUM(SignalW2) OVER Integral) / POW(AreaTime, 2) AS IntegralSignalFluxW2,
|
---|
72 | (SUM(BackgroundW2) OVER Integral) / POW(AreaTime, 2) AS IntegralBackgroundFluxW2,
|
---|
73 |
|
---|
74 | (SUM(SumEnergySimW) OVER Integral) AS IntegralEnergySimW,
|
---|
75 | (SUM(SumEnergyEstW) OVER Integral) AS IntegralEnergyEstW
|
---|
76 | FROM
|
---|
77 | Analyzed
|
---|
78 | INNER JOIN
|
---|
79 | Binning ON `.%100:binning:_est`=bin
|
---|
80 | WINDOW
|
---|
81 | Integral AS (ORDER BY `.%100:binning:_est` DESC)
|
---|
82 | )
|
---|
83 | SELECT
|
---|
84 | *,
|
---|
85 |
|
---|
86 | IntegralSignalFluxW-IntegralBackgroundFluxW/5 AS IntegralExcessFluxW,
|
---|
87 | SQRT(IntegralSignalFluxW2+IntegralBackgroundFluxW2/25) AS ErrIntegralExcessFluxW,
|
---|
88 | SQRT(IntegralSignalFluxW2) AS ErrIntegralSignalFluxW,
|
---|
89 | SQRT(IntegralBackgroundFluxW2) AS ErrIntegralBackgroundFluxW,
|
---|
90 |
|
---|
91 | IntegralEnergyEstW/IntegralSignalW AS AvgIntegralEnergyEstW,
|
---|
92 | IntegralEnergySimW/IntegralSignalW AS AvgIntegralEnergySimW
|
---|
93 |
|
---|
94 | FROM
|
---|
95 | Integrated
|
---|