1 | WITH BinnedData AS -- Bin data and create sums per bin
|
---|
2 | (
|
---|
3 | SELECT
|
---|
4 | %101:bin,
|
---|
5 |
|
---|
6 | SUM(`Signal`) AS `Signal`,
|
---|
7 | SUM(`Background`)/5 AS `Background`,
|
---|
8 | SUM(SumEnergyEst) AS SumEnergyEst,
|
---|
9 | SUM(SumW) AS SumW
|
---|
10 | FROM
|
---|
11 | SummaryData
|
---|
12 | GROUP BY
|
---|
13 | %101:bin
|
---|
14 | ),
|
---|
15 | Data AS -- Integral is a placeholder for the bin and all following bins
|
---|
16 | (
|
---|
17 | SELECT
|
---|
18 | *,
|
---|
19 | SUM(`Signal`) OVER Integral AS SignalI,
|
---|
20 | SUM(`Background`) OVER Integral /5 AS BackgroundI,
|
---|
21 | SUM(SumEnergyEst) OVER Integral AS EnergyEstI,
|
---|
22 | SUM(SumW) OVER Integral AS SumWI
|
---|
23 | FROM
|
---|
24 | BinnedData
|
---|
25 | WINDOW
|
---|
26 | Integral AS (ORDER BY %101:bin DESC)
|
---|
27 | ),
|
---|
28 | CombinedData AS -- Joind data together and for conveninece (easier reading of the query) rename the columns
|
---|
29 | (
|
---|
30 | SELECT
|
---|
31 | %102:id.%101:bin,
|
---|
32 | %102:id.lo,
|
---|
33 | %102:id.hi,
|
---|
34 | (%102:id.lo+%102:id.hi)/2 AS center,
|
---|
35 | %102:id.Width,
|
---|
36 | %102:id.AreaTime,
|
---|
37 |
|
---|
38 | `Signal`,
|
---|
39 | `Background`,
|
---|
40 |
|
---|
41 | SignalI,
|
---|
42 | BackgroundI,
|
---|
43 |
|
---|
44 | `Signal` - `Background` AS Excess,
|
---|
45 | SQRT(`Signal`) AS ErrSignal,
|
---|
46 | SQRT(`Background`) AS ErrBackground,
|
---|
47 | ExcErr(`Signal`, `Background`) AS ErrExcess,
|
---|
48 | LiMa( `Signal`, `Background`) AS Significance,
|
---|
49 |
|
---|
50 | SignalI - BackgroundI AS ExcessI,
|
---|
51 | SQRT(SignalI) AS ErrSignalI,
|
---|
52 | SQRT(BackgroundI) AS ErrBackgroundI,
|
---|
53 | ExcErr(SignalI, BackgroundI) AS ErrExcessI,
|
---|
54 | LiMa( SignalI, BackgroundI) AS SignificanceI,
|
---|
55 |
|
---|
56 | SumEnergyEst/SumW AS AvgEnergyEst,
|
---|
57 | EnergyEstI/SumWI AS AvgEnergyEstI,
|
---|
58 |
|
---|
59 | %102:id.ExcessFluxW AS SimExcess,
|
---|
60 | %102:id.ErrExcessFluxW AS ErrSimExcess,
|
---|
61 | %102:id.IntegralExcessFluxW AS SimExcessI,
|
---|
62 | %102:id.ErrIntegralExcessFluxW AS ErrSimExcessI,
|
---|
63 |
|
---|
64 | -- For flux-vs-theta: Correction for already applied ZdWeights
|
---|
65 | Sim.SimFluxW/%103:weight AS SimFluxW,
|
---|
66 | Sim.ErrSimFluxW/%104:errweight AS ErrSimFluxW,
|
---|
67 | Sim.IntegralSimFluxW/%103:weight AS SimFluxI,
|
---|
68 | Sim.ErrIntegralSimFluxW/%104:errweight AS ErrSimFluxI
|
---|
69 | FROM
|
---|
70 | Data
|
---|
71 | INNER JOIN
|
---|
72 | %105:join1
|
---|
73 | INNER JOIN
|
---|
74 | %106:join2
|
---|
75 | ),
|
---|
76 | Spectrum AS
|
---|
77 | (
|
---|
78 | SELECT -- Return final result
|
---|
79 | *,
|
---|
80 |
|
---|
81 | -- Differetial Spectrum
|
---|
82 |
|
---|
83 | SimExcess/SimFluxW AS Efficiency,
|
---|
84 |
|
---|
85 | Excess/SimExcess/Width/AreaTime AS ExcessRatio,
|
---|
86 | Excess/SimExcess/Width/AreaTime
|
---|
87 | * SQRT(
|
---|
88 | + POW(ErrExcess / Excess, 2)
|
---|
89 | + POW(ErrSimExcess / SimExcess, 2)
|
---|
90 | ) AS ErrExcessRatio,
|
---|
91 |
|
---|
92 |
|
---|
93 | Excess/SimExcess*SimFluxW/Width/AreaTime AS Flux,
|
---|
94 | Excess/SimExcess*SimFluxW/Width/AreaTime
|
---|
95 | * SQRT(
|
---|
96 | + POW(ErrExcess / Excess, 2)
|
---|
97 | + POW(ErrSimExcess / SimExcess, 2)
|
---|
98 | + POW(ErrSimFluxW / SimFluxW, 2)
|
---|
99 | ) AS ErrFlux,
|
---|
100 |
|
---|
101 | -- Integral Spectrum
|
---|
102 |
|
---|
103 | SimExcessI/SimFluxI AS EfficiencyI,
|
---|
104 |
|
---|
105 | ExcessI/SimExcessI/AreaTime AS ExcessRatioI,
|
---|
106 | ExcessI/SimExcessI/AreaTime
|
---|
107 | * SQRT(
|
---|
108 | + POW(ErrExcessI / ExcessI, 2)
|
---|
109 | + POW(ErrSimExcessI / SimExcessI, 2)
|
---|
110 | ) AS ErrExcessRatioI,
|
---|
111 |
|
---|
112 |
|
---|
113 | ExcessI/SimExcessI*SimFluxI/AreaTime AS FluxI,
|
---|
114 | ExcessI/SimExcessI*SimFluxI/AreaTime
|
---|
115 | * SQRT(
|
---|
116 | + POW(ErrExcessI / ExcessI, 2)
|
---|
117 | + POW(ErrSimExcessI / SimExcessI, 2)
|
---|
118 | + POW(ErrSimFluxI / SimFluxI, 2)
|
---|
119 | ) AS ErrFluxI
|
---|
120 |
|
---|
121 | FROM
|
---|
122 | CombinedData
|
---|
123 | WHERE
|
---|
124 | Excess>0
|
---|
125 | ORDER BY
|
---|
126 | %101:bin
|
---|
127 | )
|
---|
128 | SELECT
|
---|
129 | *,
|
---|
130 |
|
---|
131 | -- Integrated Spectrum
|
---|
132 |
|
---|
133 | SUM(Flux*Width) OVER Integral AS IntegratedFlux,
|
---|
134 | SQRT(SUM(POW(ErrFlux*Width,2)) OVER Integral) AS ErrIntegratedFlux
|
---|
135 |
|
---|
136 | FROM
|
---|
137 | Spectrum
|
---|
138 |
|
---|
139 | WINDOW
|
---|
140 | Integral AS (ORDER BY %101:bin DESC)
|
---|