Index: trunk/FACT++/spectrum/analysis.sql
===================================================================
--- trunk/FACT++/spectrum/analysis.sql	(revision 19906)
+++ trunk/FACT++/spectrum/analysis.sql	(revision 19907)
@@ -11,10 +11,15 @@
    returned. (Note that you must not add a comma behind it)
 
-   100| %100:columns
-   101| %101:files:: table containing the `FileId`s to analyze.
-   102| %102:runinfo:: table with the run info data
-   103| %103:events:: table with the image parameters
-   104| %104:positions:: table with the source positions in the camera
-   105| %105:estimator:: estimator for log10 energy
+   100| %100:files:: table containing the `FileId`s to analyze.
+   101| %101:runinfo:: table with the run info data
+   102| %102:events:: table with the image parameters
+   103| %103:positions:: table with the source positions in the camera
+   104| %105:zenith:: zenith angle in degrees
+   105| %104:columns
+   106| %105:estimator:: estimator for log10 energy
+
+   WARNING:
+     Right now, we correlate the mean zenith angle of the data
+     file with the particle direction in the simulation!
 
 *************************************************************************** */
@@ -23,5 +28,6 @@
 (
    SELECT
-      %100:columns  -- this could be removed if we can join events via the same columns (without CorsikaNumResuse)
+      %105:columns  -- this could be removed if we can join events via the same columns (without CorsikaNumResuse)
+      %104:zenith AS Theta,
       Weight,
       Size,
@@ -40,11 +46,11 @@
       cosa*Y + sina*X   AS PY
    FROM
-      %101:files
+      %100:files
    LEFT JOIN
-      %102:runinfo USING (FileId)
+      %101:runinfo USING (FileId)
    LEFT JOIN 
-      %103:events USING (FileId)  -- This could be replaced by a user uploaded temporary table
+      %102:events USING (FileId)  -- This could be replaced by a user uploaded temporary table
    LEFT JOIN 
-      %104:positions USING (FileId, EvtNumber)
+      %103:positions USING (FileId, EvtNumber)
    CROSS JOIN 
       Wobble
@@ -60,6 +66,6 @@
 (
    SELECT
-      %100:columns
-      Weight,
+      %105:columns
+      Theta, Weight,
       Size, CosDelta, SinDelta, M3Long, SlopeLong, Leakage1, WdivL,
       MeanX - PX/1.02e0 AS DX,
@@ -74,6 +80,6 @@
 (
    SELECT
-      %100:columns
-      Weight,
+      %105:columns
+      Theta, Weight,
       Size, CosDelta, SinDelta, DX, DY, M3Long, SlopeLong, Leakage1, WdivL,
       SQRT(DX*DX + DY*DY) AS Norm
@@ -85,6 +91,6 @@
 (
    SELECT
-      %100:columns
-      Weight,
+      %105:columns
+      Theta, Weight,
       Size, M3Long, SlopeLong, Leakage1, WdivL, Norm,
       LEAST(GREATEST((CosDelta*DY - SinDelta*DX)/Norm, -1), 1) AS LX,
@@ -97,6 +103,6 @@
 (
    SELECT
-      %100:columns
-      Weight,
+      %105:columns
+      Theta, Weight,
       Size, Leakage1, WdivL, LX,
       Norm          *0.0117193246260285378e0 AS Dist,
@@ -110,6 +116,6 @@
 (
    SELECT
-      %100:columns
-      Weight,
+      %105:columns
+      Theta, Weight,
       Size, WdivL, Dist, LX, M3L, Slope,
       1.39252e0 + 0.154247e0*Slope + 1.67972e0*(1-1/(1+4.86232e0*Leakage1)) AS Xi
@@ -121,6 +127,6 @@
 (
    SELECT
-      %100:columns
-      Weight,
+      %105:columns
+      Theta, Weight,
       Size, Dist, LX,
       IF (M3L<-0.07 OR (Dist-0.5e0)*7.2e0-Slope<0, -Xi, Xi) * (1-WdivL) AS Disp
@@ -130,8 +136,8 @@
 
 SELECT
-   %100:columns
-   Weight,
+   %105:columns
+   Theta, Weight,
    (Disp*Disp + Dist*Dist - 2*Disp*Dist*SQRT(1-LX*LX)) AS ThetaSq,
-   %105:estimator AS LogEnergyEst
+   %106:estimator AS LogEnergyEst
 FROM
    Table7
Index: trunk/FACT++/spectrum/data.sql
===================================================================
--- trunk/FACT++/spectrum/data.sql	(revision 19906)
+++ trunk/FACT++/spectrum/data.sql	(revision 19907)
@@ -1,5 +1,5 @@
 SELECT
-   INTERVAL(fZenithDistanceMean, %106:theta)  AS `.theta`,
-   INTERVAL(LogEnergyEst, %107:sparse)  AS  `.sparse_est`,
+   INTERVAL(Theta, %107:theta)  AS `.theta`,
+   INTERVAL(LogEnergyEst, %108:sparse)  AS  `.sparse_est`,
 
    -- Signal and Background counts
Index: trunk/FACT++/spectrum/simulation.sql
===================================================================
--- trunk/FACT++/spectrum/simulation.sql	(revision 19906)
+++ trunk/FACT++/spectrum/simulation.sql	(revision 19907)
@@ -3,11 +3,11 @@
    SELECT
       Weight, Energy, LogEnergyEst,
-      INTERVAL(Zd, %106:theta)  AS `.theta`,
-      INTERVAL(LogEnergyEst, %107:sparse)  AS `.sparse_est`,
-      INTERVAL(LogEnergyEst, %108:dense)  AS `.dense_est`,
-      INTERVAL(LOG10(Energy), %107:sparse)  AS `.sparse_sim`,
-      INTERVAL(LOG10(Energy), %108:dense)  AS `.dense_sim`,
+      INTERVAL(Theta, %107:theta)  AS `.theta`,
+      INTERVAL(LogEnergyEst, %108:sparse)  AS `.sparse_est`,
+      INTERVAL(LogEnergyEst, %109:dense)  AS `.dense_est`,
+      INTERVAL(LOG10(Energy), %108:sparse)  AS `.sparse_sim`,
+      INTERVAL(LOG10(Energy), %109:dense)  AS `.dense_sim`,
 
-      (%109:spectrum)/POW(Energy, SpectralIndex) AS SpectralWeight,  -- FIXME: Is this correct for files with different Slopes?
+      (%110:spectrum)/POW(Energy, SpectralIndex) AS SpectralWeight,  -- FIXME: Is this correct for files with different Slopes?
       LogEnergyEst - log10(Energy) AS Residual
    FROM
Index: trunk/FACT++/spectrum/spectrum.sql
===================================================================
--- trunk/FACT++/spectrum/spectrum.sql	(revision 19906)
+++ trunk/FACT++/spectrum/spectrum.sql	(revision 19907)
@@ -76,24 +76,24 @@
 Flux AS
 (
-   SELECT -- Return final result
+   SELECT -- Calculate Flux and Relative Errors
       *,
 
-      -- Differetial Spectrum
+      -- Differential Spectrum
 
       SimExcess/SimFluxW  AS  Efficiency,
 
       Excess/SimExcess/Width/AreaTime  AS  ExcessRatio,
-         1/SQRT(
+         SQRT(
              + POW(ErrExcess    / Excess,    2)
              + POW(ErrSimExcess / SimExcess, 2)
-         )  AS  SigmaExcessRatio,
+         )  AS  RelErrExcessRatio,
 
 
       Excess/SimExcess*SimFluxW/Width/AreaTime  AS  Flux,
-         1/SQRT(
+         SQRT(
              + POW(ErrExcess    / Excess,    2)
              + POW(ErrSimExcess / SimExcess, 2)
              + POW(ErrSimFluxW  / SimFluxW,  2)
-         )  AS  SigmaFlux,
+         )  AS  RelErrFlux,
 
       -- Integral Spectrum
@@ -102,29 +102,34 @@
 
       ExcessI/SimExcessI/AreaTime  AS  ExcessRatioI,
-         1/SQRT(
+         SQRT(
              + POW(ErrExcessI    / ExcessI,    2)
              + POW(ErrSimExcessI / SimExcessI, 2)
-         )  AS  SigmaExcessRatioI,
+         )  AS  RelErrExcessRatioI,
 
 
       ExcessI/SimExcessI*SimFluxI/AreaTime  AS  FluxI,
-         1/SQRT(
+         SQRT(
              + POW(ErrExcessI    / ExcessI,    2)
              + POW(ErrSimExcessI / SimExcessI, 2)
              + POW(ErrSimFluxI   / SimFluxI,   2)
-         )  AS  SigmaFluxI
+         )  AS  RelErrFluxI
 
    FROM
       CombinedData
 ),
-Spectrum AS
+Errors AS
 (
-   SELECT -- Return final result
+   SELECT -- Calculate Inverse of Relative Errors (Sigma) and Absolute Errors
       *,
 
-      ExcessRatio /SigmaExcessRatio   AS  ErrExcessRatio,
-      ExcessRatioI/SigmaExcessRatioI  AS  ErrExcessRatioI,
-      ABS(Flux)   /SigmaFlux          AS  ErrFlux,
-      ABS(FluxI)  /SigmaFluxI         AS  ErrFluxI
+      IF(RelErrExcessRatio =0, NULL, 1/RelErrExcessRatio )  AS  SigmaExcessRatio,
+      IF(RelErrExcessRatioI=0, NULL, 1/RelErrExcessRatioI)  AS  SigmaExcessRatioI,
+      IF(RelErrFlux        =0, NULL, 1/RelErrFlux        )  AS  SigmaFlux,
+      IF(RelErrFluxI       =0, NULL, 1/RelErrFluxI       )  AS  SigmaFluxI,
+
+      IF(Excess =0, ErrExcess /SimExcess   /AreaTime/Width, ExcessRatio *RelErrExcessRatio )  AS  ErrExcessRatio,
+      IF(ExcessI=0, ErrExcessI/SimExcessI  /AreaTime,       ExcessRatioI*RelErrExcessRatioI)  AS  ErrExcessRatioI,
+      IF(Excess =0, ErrExcess /Efficiency  /AreaTime/Width, ABS(Flux)   *RelErrFlux        )  AS  ErrFlux,
+      IF(ExcessI=0, ErrExcessI/EfficiencyI /AreaTime,       ABS(FluxI)  *RelErrFluxI       )  AS  ErrFluxI
    FROM
       Flux
@@ -133,5 +138,5 @@
    *,
 
-   -- Integrated Spectrum
+   -- Integrate Differential Spectrum
 
    SUM(Flux*Width) OVER Integral AS IntegratedFlux,
@@ -139,5 +144,5 @@
 
 FROM
-   Spectrum
+   Errors
 WINDOW
    Integral AS (ORDER BY %101:bin DESC)
Index: trunk/FACT++/spectrum/summary-sim.sql
===================================================================
--- trunk/FACT++/spectrum/summary-sim.sql	(revision 19906)
+++ trunk/FACT++/spectrum/summary-sim.sql	(revision 19907)
@@ -30,5 +30,5 @@
       %102:bin
 ),
-Triggered AS -- Summarizy triggered (ceres) data in bins
+Triggered AS -- Summarize triggered (ceres) data in bins
 (
    SELECT
