Index: trunk/MagicSoft/Cosy/gui/MGAccuracy.cc
===================================================================
--- trunk/MagicSoft/Cosy/gui/MGAccuracy.cc	(revision 1804)
+++ trunk/MagicSoft/Cosy/gui/MGAccuracy.cc	(revision 1810)
@@ -195,8 +195,10 @@
     aaz *= d2r;
 
+    const double el = TMath::Pi()/2-pzd;
+
     const double dphi2 = aaz/2.;
     const double cos2  = cos(dphi2)*cos(dphi2);
     const double sin2  = sin(dphi2)*sin(dphi2);
-    const double d     = cos(azd)*cos2 - cos(2*pzd+azd)*sin2;
+    const double d     = cos(azd)*cos2 - cos(2*el+azd)*sin2;
 
     double dist = acos(d);
@@ -250,5 +252,5 @@
 
     UpdateCross(x, y);
-    UpdateText(pos.Zd(), acc.Az(), acc.Zd());
+    UpdateText(pos.Zd(), acc.Zd(), acc.Az());
 
     SetModified();
Index: trunk/MagicSoft/Cosy/gui/MGPngReader.cc
===================================================================
--- trunk/MagicSoft/Cosy/gui/MGPngReader.cc	(revision 1804)
+++ trunk/MagicSoft/Cosy/gui/MGPngReader.cc	(revision 1810)
@@ -3,8 +3,10 @@
 #include <fstream.h>    // ifstream
 #include <iostream.h>   // cout
+#include <iomanip.h>   // cout
 
 #include <TTimer.h>
 
 #include <TGMenu.h>
+#include <TGLabel.h>
 #include <TSystem.h>
 #include <TGSplitter.h>    // TGHorizontal3DLine
@@ -13,4 +15,5 @@
 #include "MGImage.h"
 #include "MGCoordinates.h"
+#include "MGDispCoordinates.h"
 
 #include "coord.h"
@@ -255,5 +258,5 @@
     fList->Add(fLimMag);
 
-    fSao->SetLimitMag(8.0);
+    fSao->SetLimitMag(7.0);
 
     fInterpol = new MGPopupMenu(p);
@@ -314,9 +317,32 @@
     fList->Add(fCRaDec);
 
-    fCZdAz = new MGCoordinates(this, kETypeZdAz);
+    fCZdAz = new MGCoordinates(this, kETypeZdAz, kFALSE);
     fCZdAz->Move(240+12+10, fMenu->GetDefaultHeight()+584);
     AddFrame(fCZdAz);
     fList->Add(fCZdAz);
 
+    fPZdAz = new MGCoordinates(this, kETypeZdAz, kFALSE);
+    fPZdAz->Move(240+12+10, fMenu->GetDefaultHeight()+630);
+    AddFrame(fPZdAz);
+    fList->Add(fPZdAz);
+
+    TGLabel *l = new TGLabel(this, "Arb.-Sky Pos");
+    l->SetTextJustify(kTextLeft);
+    l->Move(480+32, fMenu->GetDefaultHeight()+590);
+    AddFrame(l);
+    fList->Add(l);
+
+    l = new TGLabel(this, "arcsec/pix");
+    l->SetTextJustify(kTextLeft);
+    l->Move(605, fMenu->GetDefaultHeight()+619);
+    AddFrame(l);
+    fList->Add(l);
+
+    l = new TGLabel(this, "Pointing Pos");
+    l->SetTextJustify(kTextLeft);
+    l->Move(480+32, fMenu->GetDefaultHeight()+655);
+    AddFrame(l);
+    fList->Add(l);
+
     const Double_t pixsize = 23.4;
 
@@ -328,5 +354,5 @@
     fPixSize = new TGTextEntry(this, txt, IDM_kPixSize);
     fPixSize->SetAlignment(kTextCenterX);
-    fPixSize->Move(600, fMenu->GetDefaultHeight()+584);
+    fPixSize->Move(547, fMenu->GetDefaultHeight()+617);
     AddFrame(fPixSize);
     fList->Add(fPixSize);
@@ -357,8 +383,14 @@
     MapSubwindows();
     MapWindow();
+
+    //------------------------------------------------------------
+    //    XY xy(3.819444, 24.05333);
+    //    fCRaDec->SetCoordinates(xy);
+    //    fRaDec->Set(xy.X()*360/24, xy.Y());
+    //------------------------------------------------------------
 }
 
 MGPngReader::MGPngReader(MObservatory::LocationName_t obs)
-: TGMainFrame(gClient->GetRoot(), 768, 700), fFile(NULL), fDx((768-kZOOM)/2), fDy((512-kZOOM)/2)
+: TGMainFrame(gClient->GetRoot(), 768, 740), fFile(NULL), fDx((768-kZOOM)/2), fDy((512-kZOOM)/2)
 {
     fSao = new StarCatalog(obs);
@@ -811,6 +843,71 @@
 }
 
-void MGPngReader::CalcTrackingError(MStarList &spots, MStarList &stars)
-{
+ZdAz MGPngReader::TrackingError(TArrayF &x, TArrayF &y, TArrayF &mag) const
+{
+    //
+    // Viewable area (FIXME: AZ)
+    //
+    TH2F h("Hist", "dX/dY",  77, -768/2-.5,  768/2+.5, 58, -576/2-.5,  576/2+.5); // 3
+
+    /*
+    TH1F hmag("HistMag", "Mag", 19, 0, 100);
+    for (int i=0; i<mag.GetSize(); i++)
+        hmag.Fill(mag[i]);
+        */
+
+    //
+    // Search for matching Magnitudes
+    //
+    for (int i=0; i<mag.GetSize(); i++)
+    {
+        if (mag[i]>48-15 && mag[i]<48+15)
+            h.Fill(x[i], y[i]);
+    }
+
+    //
+    // Serach for an excess in the histogram
+    //
+    Int_t mx, my, dummy;
+    h.GetMaximumBin(mx, my, dummy);
+
+    const double xmax = h.GetXaxis()->GetBinCenter(mx);
+    const double dx   = h.GetXaxis()->GetBinWidth(mx);
+
+    const double ymax = h.GetYaxis()->GetBinCenter(my);
+    const double dy   = h.GetYaxis()->GetBinWidth(my);
+
+    cout << setprecision(3);
+    cout << "Cut-XY:       " << xmax << " +- " << dx << " / " << ymax << " +- " << dy << endl;
+
+    TGraph g;
+    for (int i=0; i<mag.GetSize(); i++)
+    {
+        if (!(x[i]>xmax-dx && x[i]<xmax+dx &&
+              y[i]>ymax-dy && y[i]<ymax+dy &&
+              mag[i]>48-15 && mag[i]<48+15))
+            continue;
+
+        g.SetPoint(g.GetN(), x[i], y[i]);
+    }
+
+    cout << "Offset-XY:    " << g.GetMean(1) << " +- " << g.GetRMS(1) << " / ";
+    cout << g.GetMean(2) << " +- " << g.GetRMS(2) << endl;
+
+    AltAz pos0 = fSao->CalcAltAzFromPix(768/2,              576/2)*kRad2Deg;
+    AltAz pos1 = fSao->CalcAltAzFromPix(768/2+g.GetMean(1), 576/2+g.GetMean(2))*kRad2Deg;
+
+    pos1 -= pos0;
+
+    ofstream fout("tracking_error.txt");
+    fout << setprecision(10) << fSao->GetMjd()-52000 << " " << -pos1.Alt() << " " << pos1.Az() << endl;
+    fout.close();
+
+    return ZdAz(-pos1.Alt(), pos1.Az());
+}
+
+void MGPngReader::CalcTrackingError(Leds &leds, MStarList &stars)
+{
+    const Int_t max = leds.GetEntries();
+
     if (stars.GetRealEntries() < 3)
     {
@@ -819,5 +916,5 @@
     }
 
-    if (spots.GetRealEntries() < 1)
+    if (max < 1)
     {
         cout << "Sorry, less than 1 detected spot in FOV!" << endl;
@@ -825,82 +922,59 @@
     }
 
-    Int_t idx = 0;
-
-    MStarList sortedspots;
+    stars.Sort(); // Sort by magnitude
+
+    TString str = "data/tracking_";
+    str += fSao->GetMjd()-52000;
+    str += ".txt";
+
+    ofstream fout(str);
+
+    TArrayF x, y, mag;
+
+    Int_t num = 0;
+
+    // FIXME: Is predifined value 3 a good idea?
 
     MStar *star;
-    MStar *spot;
     MStarListIter NextStar(&stars);
-    MStarListIter NextSpot(&spots);
-
-    while ((spot=NextSpot()))
-    {
-        AltAz aa = fSao->CalcAltAzFromPix(spot->GetX(), spot->GetY());
-        spot->Set(aa.Az(), aa.Alt());
-    }
-
-    while ((star=NextStar()))
-    {
-        AltAz aa = fSao->CalcAltAzFromPix(star->GetX(), star->GetY());
-        star->Set(aa.Az(), aa.Alt());
-
-        const double aaz   = star->GetX();
-        const double dphi2 = aaz/2.;
-        const double cos2  = cos(dphi2)*cos(dphi2);
-        const double sin2  = sin(dphi2)*sin(dphi2);
-
-        Double_t min = 800;
-
-        NextSpot.Reset();
-        while ((spot=NextSpot()))
+    while ((star=NextStar()) && num++<max+3)
+    {
+        TIter NextSp(&leds);
+        Led *spot=NULL;
+        while ((spot=(Led*)NextSp()))
         {
-            const double pzd = TMath::Pi()/2-spot->GetY();
-            const double azd = TMath::Pi()/2-star->GetY();
-
-            const double d = cos(azd)*cos2 - cos(2*pzd+azd)*sin2;
-
-            const Double_t dist = acos(d);
-
-            if (dist>=min)
-                continue;
-
-            min = dist;
-            sortedspots.AddAt(idx, spot->GetX(), spot->GetY(), spot->GetMag());
+            const XY dpos(spot->GetX()-star->GetX(), spot->GetY()-star->GetY());
+
+            const Int_t idx = x.GetSize();
+
+            x.Set(idx+1);
+            y.Set(idx+1);
+            mag.Set(idx+1);
+
+            x.AddAt(dpos.X(), idx);
+            y.AddAt(dpos.Y(), idx);
+            mag.AddAt(spot->GetMag()/star->GetMag(), idx);
+
+            if (fout)
+                fout << x[idx] << " " << y[idx] << " " << mag[idx] << endl;
         }
-        if (min>768)
-        {
-            cout << "ERROR!!!!!!!!" << endl;
-            return;
-        }
-        idx++;
-    }
-
-    //
-    // Now we have in sortedspots the entries with the shortest distances
-    // to the corresponding ones in stars.
-    // Now calculate the tracking error.
-    //
-    NextStar.Reset();
-    MStarListIter NextSpot2(&sortedspots);
-
-    Double_t meanx=0;
-    Double_t meany=0;
-
-    while ((star=NextStar()))
-    {
-        spot = NextSpot2();
-
-        meanx += star->GetX() - spot->GetX();
-        meany += star->GetY() - spot->GetY();
-    }
-
-    meanx /= idx;
-    meany /= idx;
-
-    cout << "Tracking Error:  dAlt=" << meany*180/TMath::Pi();
-    cout << "°  dAz=" << meanx*180/TMath::Pi() << "°    (calculated";
-    cout << " with " << idx << " stars/spots)" << endl;
-}
-
+    }
+
+    ZdAz d = TrackingError(x, y, mag);
+
+    //
+    // Calculated offsets
+    //
+
+    // round= floor(x+.5)
+    cout << "Offset-ZdAz: " << d.Zd()*60 << "' / " << d.Az()*60 << "'" << endl;
+    cout << "Offset-ZdAz: " << d.Zd()/360*16384 << " / " << d.Az()/360*16384 << " (SE) " << endl;
+
+    //
+    // Current Pointing position
+    //
+    ZdAz cpos = fSao->GetZdAz()-d;
+    fPZdAz->SetCoordinates(cpos);
+}
 
 
@@ -945,9 +1019,12 @@
 
     MStarList spots;
+
+    /*
     if (fDisplay->IsEntryChecked(IDM_kStarguider))
         Filter2::Execute(spots, c);
     else
-        if (fDisplay->IsEntryChecked(IDM_kFilter))
-            Filter::Execute(c);
+     */
+    if (fDisplay->IsEntryChecked(IDM_kFilter))
+        Filter::Execute(c);
 
     if (fDisplay->IsEntryChecked(IDM_kCaosFilter))
@@ -1031,25 +1108,36 @@
     if (fDisplay->IsEntryChecked(IDM_kCatalog))
     {
+        Timer time(tm);
+
+        GetCoordinates();
+
+        MStarList stars;
+        fSao->GetStars(stars, time.GetMjd(), *fRaDec);
+
+        if (fDisplay->IsEntryChecked(IDM_kStarguider))
+        {
+            Leds leds;
+            CaosFilter::Execute(c, leds, 1);
+
+            cout << "Found: " << leds.GetEntries() << " leds" << endl;
+
+            CaosFilter::RemoveTwins(leds, 3);
+            leds.Compress();
+
+            cout << "Rest: " << leds.GetEntries() << " leds" << endl;
+
+            CalcTrackingError(leds, stars);
+        }
+
+        byte cimg[768*576];
+        fSao->GetImg(c, cimg, stars);
+
         DrawCircle(c, 0.5);
         DrawCircle(c, 1.0);
         DrawCircle(c, 1.5);
 
-        byte cimg[768*576];
-
-        GetCoordinates();
-
-        Timer time(tm);
-
-        MStarList stars;
-        fSao->GetStars(stars, time.GetMjd(), *fRaDec);
-        fSao->GetImg(c, cimg, stars);
-        //fSao->GetImg(c, cimg, time.CalcMjd(), *fRaDec);
+        fCZdAz->SetCoordinates(fSao->GetZdAz());
 
         fImage->DrawColImg(c, cimg);
-
-        fCZdAz->SetCoordinates(fSao->GetZdAz());
-
-        if (fDisplay->IsEntryChecked(IDM_kStarguider))
-            CalcTrackingError(spots, stars);
     }
     else
@@ -1067,8 +1155,8 @@
     {
         const int dy = (int)sqrt(rpix*rpix-dx*dx);
-        img[cx+dx + (cy-dy)*768] = 0xd0;
-        img[cx+dx + (cy+dy)*768] = 0xd0;
-        img[cx-dy + (cy+dx)*768] = 0xd0;
-        img[cx+dy + (cy+dx)*768] = 0xd0;
+        img[cx+dx + (cy-dy)*768] = 0x40;
+        img[cx+dx + (cy+dy)*768] = 0x40;
+        img[cx-dy + (cy+dx)*768] = 0x40;
+        img[cx+dy + (cy+dx)*768] = 0x40;
     }
 }
Index: trunk/MagicSoft/Cosy/gui/MGPngReader.h
===================================================================
--- trunk/MagicSoft/Cosy/gui/MGPngReader.h	(revision 1804)
+++ trunk/MagicSoft/Cosy/gui/MGPngReader.h	(revision 1810)
@@ -13,11 +13,10 @@
 #include "MGImage.h"
 
-#include <TH1.h>
-#include <TH2.h>
-#include <TGraph.h>
-#include <TCanvas.h>
+#include "coord.h"
 
-class AltAz;
-class RaDec;
+class TArrayF;
+class TH1F;
+class TH2F;
+class TGraph;
 
 class TTimer;
@@ -87,4 +86,6 @@
     MGCoordinates *fCZdAz;
 
+    MGCoordinates *fPZdAz;
+
     TGTextEntry   *fPixSize;
 
@@ -104,5 +105,6 @@
     void Toggle(MGPopupMenu *p, UInt_t id);
     void GetCoordinates();
-    void CalcTrackingError(MStarList &, MStarList &);
+    void CalcTrackingError(Leds &, MStarList &);
+    ZdAz TrackingError(TArrayF &alt, TArrayF &az, TArrayF &mag) const;
 
     void InitHists();
