Index: trunk/MagicSoft/Cosy/caos/Led.h
===================================================================
--- trunk/MagicSoft/Cosy/caos/Led.h	(revision 4856)
+++ trunk/MagicSoft/Cosy/caos/Led.h	(revision 4865)
@@ -40,4 +40,7 @@
     }
 
+    void SetX(Double_t x)     { fX=x; }
+    void SetY(Double_t y)     { fY=y; }
+
     Double_t GetX() const    { return fX; }
     Double_t GetY() const    { return fY; }
Index: trunk/MagicSoft/Cosy/caos/Rings.cc
===================================================================
--- trunk/MagicSoft/Cosy/caos/Rings.cc	(revision 4856)
+++ trunk/MagicSoft/Cosy/caos/Rings.cc	(revision 4865)
@@ -14,5 +14,5 @@
     int nPoints = leds.GetEntries();
 
-    if (nPoints<5)
+    if (nPoints<fMinNumberLeds) 
         return;
 
Index: trunk/MagicSoft/Cosy/caos/Rings.h
===================================================================
--- trunk/MagicSoft/Cosy/caos/Rings.h	(revision 4856)
+++ trunk/MagicSoft/Cosy/caos/Rings.h	(revision 4865)
@@ -16,9 +16,15 @@
 private:
     Ring fCenter;
+    Short_t fMinNumberLeds;
 
     void CalcCenters(const Leds &leds, Float_t min, Float_t max);
 
 public:
-    Rings() : TClonesArray("Ring", 1) {}
+    Rings() : TClonesArray("Ring", 1) 
+	{
+	    fMinNumberLeds=5;
+	}
+
+    void SetMinNumberLeds(Short_t n) { fMinNumberLeds=n; }
 
     void CalcRings(Leds &leds, Float_t min=-1, Float_t max=-1);
Index: trunk/MagicSoft/Cosy/catalog/StarCatalog.cc
===================================================================
--- trunk/MagicSoft/Cosy/catalog/StarCatalog.cc	(revision 4856)
+++ trunk/MagicSoft/Cosy/catalog/StarCatalog.cc	(revision 4865)
@@ -16,5 +16,5 @@
 ClassImp(StarCatalog);
 
-StarCatalog::StarCatalog(MObservatory::LocationName_t key) : SlaStars(key), fAstro(0), /*fSao(NULL), fSrt(NULL), fEntries(0),*/ fSinAngle(0), fCosAngle(1)
+StarCatalog::StarCatalog(MObservatory::LocationName_t key) : SlaStars(key), fW(768), fH(576), fAstro(0), /*fSao(NULL), fSrt(NULL), fEntries(0),*/ fSinAngle(0), fCosAngle(1), fBox(768)
 {
     fAstro = new MAstroCatalog;
@@ -136,12 +136,32 @@
     {
         const int mag = (10 - (star->GetMag()>1 ? (int)star->GetMag() : 1))/2;
-
-        Double_t color = 0xf0; //0x0f;
-        DrawCircle(color, img, (int)star->GetX(), (int)star->GetY(), mag);
+        Double_t color = 0xf0; //0x0f;       
+	// DrawStars flips the picture in X defaultwise now
+        DrawCircle(color, img, 768-(int)star->GetX(), (int)star->GetY(), mag);
     }
 }
 
-void StarCatalog::CalcStars(MStarList &list) const
-{
+void StarCatalog::CalcStars(MStarList &list)
+{
+    // full FOV
+    fBox=768;
+    CalcStars(list, 0, 576, 0, 0);
+}
+
+void StarCatalog::CalcStars(MStarList &list, int xc, int yc, 
+			    int xo, int yo) const
+{
+
+    // CalcStars flips the picture in X defaultwise now
+    int x0 = (768-xc)-fBox;
+    int x1 = (768-xc)+fBox;
+    int y0 = yc-fBox;
+    int y1 = yc+fBox;
+
+    if (x0<0) x0=0;
+    if (y0<0) y0=0;
+    if (x1>fW) x1=fW;
+    if (y1>fH) y1=fH;
+
     // Align stars into telescope system
     // (Move the telescope to pointing position)
@@ -196,9 +216,11 @@
         Float_t yy = loc.X()*fSinAngle + loc.Y()*fCosAngle;
 
-        if (xx<0 || xx >=768 || yy<0 || yy>=576)
+        if (xx<(x0-768/2) || xx >=(x1-768/2) 
+	    || yy<(y0-(576/2+yo)) || yy>=(y1-(576/2+yo)))
             continue;
 
         // Store pixel coordinates of star in list
-        list.Add(xx+768/2, yy+576/2, -2.5*log10(mag));
+        list.Add(xx+768/2+xo, yy+576/2+yo, -2.5*log10(mag));
+
     }
 }
Index: trunk/MagicSoft/Cosy/catalog/StarCatalog.h
===================================================================
--- trunk/MagicSoft/Cosy/catalog/StarCatalog.h	(revision 4856)
+++ trunk/MagicSoft/Cosy/catalog/StarCatalog.h	(revision 4865)
@@ -25,4 +25,9 @@
 {
 private:
+
+    int fW;
+    int fH;
+
+ 
     MAstroCatalog *fAstro;
 
@@ -41,4 +46,6 @@
     void   SetRaDec(const RaDec &radec);
     void   SetAltAz(const AltAz &altaz);
+
+    int    fBox;
 
 public:
@@ -69,6 +76,9 @@
     }
 
-    void   CalcStars(MStarList &list) const;
+    void   CalcStars(MStarList &list, int xc, int yc, int xo, int yo) const;
+    void   CalcStars(MStarList &list);
     static void DrawStars(MStarList &list, byte *img);
+
+    void SetBox(int box)   { fBox = box; }
 
     ClassDef(StarCatalog, 0)
Index: trunk/MagicSoft/Cosy/gui/GuiLinkDef.h
===================================================================
--- trunk/MagicSoft/Cosy/gui/GuiLinkDef.h	(revision 4856)
+++ trunk/MagicSoft/Cosy/gui/GuiLinkDef.h	(revision 4865)
@@ -14,4 +14,5 @@
 #pragma link C++ class MGAccuracy+;
 #pragma link C++ class MGSkyPosition+;
+#pragma link C++ class MGStarg+;
 
 #pragma link C++ class MGCosy+;
Index: trunk/MagicSoft/Cosy/gui/MGStarg.cc
===================================================================
--- trunk/MagicSoft/Cosy/gui/MGStarg.cc	(revision 4865)
+++ trunk/MagicSoft/Cosy/gui/MGStarg.cc	(revision 4865)
@@ -0,0 +1,288 @@
+#include "MGStarg.h"
+
+#include <iostream.h>  // cout
+
+#include <TArc.h>
+#include <TLine.h>
+#include <TText.h>
+#include <TWbox.h>
+#include <TList.h>
+#include <TGaxis.h>
+#include <TGraph.h>
+#include <TCanvas.h>
+
+#include <MHexagon.h>
+
+#include "coord.h"
+#include "MTime.h"
+
+ClassImp(MGStarg);
+
+void MGStarg::DrawCoordinateSystem()
+{
+    TWbox box;
+    box.DrawWbox(-145*2, 145*2, -15*2, 120*2,  18,  2, 1);
+
+    TText text;
+    text.SetTextAlign(22);  // centered, centered (s.TAttText)
+    text.DrawText(-80*2, 132.5*2, "Misspointing [min]");
+
+    MHexagon hex;
+    hex.SetFillColor(39);
+    hex.SetFillStyle(4000);
+    hex.SetLineColor(17);
+    hex.DrawHexagon(0,0,177.5*2);
+
+    text.SetTextAlign(11);  // left, bottom (s.TAttText)
+    text.SetTextColor(3);
+    text.DrawText(160, -220, "0.0125 deg");
+
+    text.SetTextColor(5);
+    text.DrawText(160, -250, "0.025 deg");
+
+    text.SetTextColor(2);
+    text.DrawText(160, -280, "0.05 deg");
+
+
+    TLine line;
+    line.DrawLine(-65*4,     0, 65*4,    0);
+    line.DrawLine(    0, -65*4,    0, 65*4);
+
+    //
+    // Can be replaced by TGaxis axe; in a later root version
+    // than 3.01/06. I talked to Rene
+    //
+    TGaxis *axe;
+    axe = new TGaxis(-60*4,   0, 60*4,  0,  -4, 4, 30204, "+-N");
+    axe->SetTitle("Az"); // \xb0
+    axe->SetBit(kCanDelete);
+    axe->Draw();
+
+    axe = new TGaxis(  0, -60*4,  0, 60*4,  -4, 4,   304, "+-N");
+    axe->SetTitle("Zd"); // \xb0
+    axe->SetLabelOffset(-0.02);
+    axe->SetBit(kCanDelete);
+    axe->Draw();
+}
+
+void MGStarg::InitText()
+{
+    fTxt = new TText(280, 280, "0' / 0'");
+    fTxt->SetTextAlign(33); // right, top
+    fTxt->SetTextColor(10);
+    fTxt->Draw();
+
+    fList->Add(fTxt);
+}
+
+void MGStarg::InitBar()
+{
+    fBar = new TLine(0, 0, 0, 0);
+    fBar->SetLineColor(kBlack);
+    fBar->SetLineStyle(1);
+    fBar->SetLineWidth(5);
+    fBar->Draw();
+
+    fList->Add(fBar);
+}
+
+void MGStarg::InitCross()
+{
+    fLin1 = new TLine(0, 0, 0, 0);
+    fLin2 = new TLine(0, 0, 0, 0);
+
+    fLin1->SetLineColor(10); // white (s. TAttFill)
+    fLin2->SetLineColor(10); // white
+    fLin1->SetLineStyle(1);  // solid (s. TAttLine)
+    fLin2->SetLineStyle(1); 
+
+    fLin1->SetLineWidth(2);
+    fLin2->SetLineWidth(2);
+
+    fLin1->Draw();
+    fLin2->Draw();
+
+    fList->Add(fLin1);
+    fList->Add(fLin2);
+}
+
+MGStarg::MGStarg(const TGWindow* p, const UInt_t w)
+: MGEmbeddedCanvas("Starg", p, w, 300)
+{
+    // FIXME: Overload MapWindow in newer Root versions to remove
+    //        the contents of the graph!
+    fGraph = new TGraph;
+    fGraph->SetPoint(0, 0, 0);
+    fGraph->SetLineColor(kBlue);
+    fGraph->SetMarkerColor(kBlue);
+    fGraph->SetMarkerStyle(kFullDotMedium);
+    fGraph->Draw("LP");
+    fList->Add(fGraph);
+    //fGraph->SetNameTitle("AccVsT", "Accuracy vs Min of Time");
+    //fGraph->Draw("APL");
+    //fGraph->SetMarkerSize(2);
+
+    DrawCoordinateSystem();
+
+    InitText();
+    InitCross();
+    InitBar();
+
+    InitCanvas();
+
+    SetNoContextMenu();
+
+    MTime t(-1);
+    fTime = t.GetAxisTime();
+}
+
+MGStarg::~MGStarg()
+{
+    //    cout << "MGStarg destroyed." << endl;
+}
+
+// dist [deg]
+void MGStarg::UpdateText(Float_t dist)
+{
+    dist *= 3600.; // [sec]
+
+    int rs = (int)floor(fmod(dist, 60.));
+
+    dist /= 60.;   // [min]
+    int rm = (int)dist;
+
+    char txt[100];
+    rm ? sprintf(txt, "%d'%02d\"", rm, rs) : sprintf(txt, "%d\"", rs);
+
+    fTxt->SetText(fTxt->GetX(), fTxt->GetY(), txt);
+
+    fBar->SetY2(dist*60); // [sec]
+    if (dist<1.5)
+        fBar->SetLineColor(kGreen);
+    else
+        if (dist<3)
+            fBar->SetLineColor(kYellow);
+        else
+            fBar->SetLineColor(kRed);
+
+    SetModified();
+}
+
+// dist [deg]
+void MGStarg::UpdateGraph(Float_t dist)
+{
+    MTime t(-1);
+    const Double_t dtime = t.GetAxisTime()-fTime; // range [-0.5h, 0h]
+
+    dist *= 60; // min
+
+    static int odist = -1;
+    if (odist==(int)(dist*10*60) && dtime<10)
+        return;
+
+    odist = (int)(dist*10*60);
+
+    fGraph->SetPoint(fGraph->GetN(), dtime, dist*60);
+
+    const Double_t ntime = dtime;
+    for (int i=0; i<fGraph->GetN(); i++)
+    {
+        Double_t x, y;
+        fGraph->GetPoint(i, x,       y);
+        fGraph->SetPoint(i, x-ntime, y);
+        //cout << i << ":  " << x-ntime << " / " << y << endl;
+    }
+    while (fGraph->GetN()>0)
+    {
+        Double_t x, y;
+        fGraph->GetPoint(0, x, y);
+
+        if (x==-ntime && y==0)
+        {
+            fGraph->RemovePoint(0);
+            continue;
+        }
+
+        if (x>-4.75*60)
+            break;
+
+        fGraph->RemovePoint(0);
+    }
+
+    fTime = t.GetAxisTime();
+
+    SetModified();
+
+}
+
+void MGStarg::Update(Float_t pzd, Float_t azd, Float_t aaz)
+{
+    const Float_t d2r = TMath::Pi()/180.;
+
+    pzd *= d2r;
+    azd *= d2r;
+    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*el)*sin2;
+
+    double dist = acos(d)*TMath::RadToDeg();
+
+    UpdateText(dist);
+    UpdateGraph(dist);
+}
+
+void MGStarg::UpdateCross(Float_t x, Float_t y)
+{
+    //
+    // x["], y["]
+    //
+    static int X = ~0;
+    static int Y = ~0;
+
+    int pixx = (int)(x/fPix);  // [pix]
+    int pixy = (int)(y/fPix);  // [pix]
+
+    if (X==pixx && Y==pixy)
+        return;
+
+    X = pixx;
+    Y = pixy;
+
+    fLin1->SetX1(x-5.);
+    fLin1->SetX2(x+5.);
+
+    fLin2->SetX1(x-5.);
+    fLin2->SetX2(x+5.);
+
+    fLin1->SetY1(y-5.);
+    fLin1->SetY2(y+5.);
+
+    fLin2->SetY1(y+5.);
+    fLin2->SetY2(y-5.);
+
+    SetModified();
+}
+
+void MGStarg::Update(ZdAz &pos, ZdAz &acc)
+{
+
+    UpdateCross(acc.Az()*3600., acc.Zd()*3600.);
+    Update(pos.Zd(), acc.Zd(), acc.Az());
+
+    UpdateCanvas();
+}
+
+
+
+
+
+
+
+
+
+
Index: trunk/MagicSoft/Cosy/gui/MGStarg.h
===================================================================
--- trunk/MagicSoft/Cosy/gui/MGStarg.h	(revision 4865)
+++ trunk/MagicSoft/Cosy/gui/MGStarg.h	(revision 4865)
@@ -0,0 +1,47 @@
+#ifndef MGSTARG_H
+#define MGSTARG_H
+
+#ifndef COSY_MGEmbeddedCanvas
+#include "MGEmbeddedCanvas.h"
+#endif
+
+class TGraph;
+class TLine;
+class TText;
+
+class ZdAz;
+
+class MGStarg : public MGEmbeddedCanvas
+{
+    TLine  *fLin1;
+    TLine  *fLin2;
+
+    TText  *fTxt;
+
+    TLine  *fBar;
+
+    TGraph *fGraph;
+
+    Double_t fTime;
+
+    void DrawCoordinateSystem();
+
+    void InitText();
+    void InitCross();
+    void InitBar();
+
+    void UpdateCross(Float_t x, Float_t y);
+    void UpdateText(Float_t acc);
+    void UpdateGraph(Float_t acc);
+    void Update(Float_t zd, Float_t x, Float_t y);
+
+public:
+    MGStarg(const TGWindow* p, const UInt_t w);
+    ~MGStarg();
+
+    void Update(ZdAz &pos, ZdAz &acc);
+
+    ClassDef(MGStarg, 0)
+};
+
+#endif // MGSTARG_H
Index: trunk/MagicSoft/Cosy/gui/Makefile
===================================================================
--- trunk/MagicSoft/Cosy/gui/Makefile	(revision 4856)
+++ trunk/MagicSoft/Cosy/gui/Makefile	(revision 4865)
@@ -38,6 +38,7 @@
 	   MGEmbeddedCanvas.cc \
 	   MGAccuracy.cc \
+	   MGStarg.cc \
 	   MGSkyPosition.cc
-	   
+  
 SRCS        = $(SRCFILES)
 HEADERS     = $(SRCFILES:.cc=.h)
Index: trunk/MagicSoft/Cosy/main/MCaos.cc
===================================================================
--- trunk/MagicSoft/Cosy/main/MCaos.cc	(revision 4856)
+++ trunk/MagicSoft/Cosy/main/MCaos.cc	(revision 4865)
@@ -116,4 +116,5 @@
 
     Rings r;
+    r.SetMinNumberLeds(fMinNumberLeds);
     r.CalcRings(fPositions);
 
@@ -159,6 +160,6 @@
         fHistw[i] = new TH1F;
         fHistw[i]->SetNameTitle(name, title);
-        fHistw[i]->SetBins(721, -180.5, 180.5);
-        fHistw[i]->SetXTitle("\\Phi [deg]");
+        fHistw[i]->SetBins(101, -50.5, 50.5);
+        fHistw[i]->SetXTitle("\\Phi [arcmin]");
         fHistw[i]->SetYTitle("counts");
     }
@@ -259,4 +260,5 @@
     c2->Update();
 
+    /*
     c = new TCanvas("ctime", "Timedevelopement of Center", 800, 800);
     c->Divide(1,3);
@@ -266,6 +268,8 @@
     h->SetYTitle("x [pix]");
     h->DrawCopy();
-    ((TPad*)gPad)->SetSelected(NULL);
+    c->SetSelectedPad(NULL);
     fGraphprx->DrawClone("ALP*")->SetBit(kCanDelete);
+    gPad->Modified();
+    gPad->Update();
     c->cd(2);
     h = fGraphpry->GetHistogram();
@@ -273,6 +277,6 @@
     h->SetYTitle("y [pix]");
     h->DrawCopy();
-    ((TPad*)gPad)->SetSelected(NULL);
-    fGraphpry->DrawClone("ALP*")->SetBit(kCanDelete);
+    //((TPad*)gPad)->SetSelectedPad(NULL);
+    //fGraphpry->DrawClone("ALP*")->SetBit(kCanDelete);
     c->cd(3);
     h = fGraphr->GetHistogram();
@@ -280,8 +284,9 @@
     h->SetYTitle("r [pix]");
     h->DrawCopy();
-    ((TPad*)gPad)->SetSelected(NULL);
-    fGraphr->DrawClone("ALP*")->SetBit(kCanDelete);
+    //((TPad*)gPad)->SetSelectedPad(NULL);
+    //fGraphr->DrawClone("ALP*")->SetBit(kCanDelete);
     c->Modified();
     c->Update();
+    */
 
     c = new TCanvas("crot", "rotation angle", 800, 600);
@@ -291,4 +296,5 @@
     fHistallw->SetYTitle("counts");
     fHistallw->DrawCopy();
+    /*
     c->cd(2);
     h = fGraphw->GetHistogram();
@@ -298,5 +304,5 @@
     ((TPad*)gPad)->SetSelected(NULL);
     fGraphw->DrawClone("ALP*")->SetBit(kCanDelete);
-
+    */
 
     /* --------------------------------------------------------
@@ -325,5 +331,5 @@
 }
 
-Ring MCaos::Run(byte *img, bool printl, bool printr, const ZdAz &pos, const MTime &t)
+Ring MCaos::Run(byte *img, bool printl, bool printr, const ZdAz &pos, const MTime &t, Double_t box, Double_t cut)
 {
     Leds &leds = *fLeds;
@@ -331,5 +337,5 @@
 
     //          img  width height radius sigma
-    FilterLed f(img, 768, 576, 50, 3.0);
+    FilterLed f(img, 768, 576, box, cut);
 
     Int_t first=0;
@@ -365,7 +371,13 @@
 
     Rings rings;
-    rings.CalcRings(leds, 265, 268);
+    rings.SetMinNumberLeds(fMinNumberLeds);
+//    rings.CalcRings(leds, 265, 268); 
+// rwagner
+//    rings.CalcRings(leds, 158, 164);
+    rings.CalcRings(leds, fMinRadius, fMaxRadius);
 
     const Ring &center = rings.GetCenter();
+    
+    center.Print();
 
     // FIXME!
@@ -393,9 +405,9 @@
             {
                 0,
-                0,
-                0,
-                0,
-                0,
-                0
+		0,
+		0,
+		0,
+		0,
+		0
             };
 
Index: trunk/MagicSoft/Cosy/main/MCaos.h
===================================================================
--- trunk/MagicSoft/Cosy/main/MCaos.h	(revision 4856)
+++ trunk/MagicSoft/Cosy/main/MCaos.h	(revision 4865)
@@ -45,9 +45,14 @@
     TGraph        *fGraphr;
 
+    Short_t       fMinNumberLeds;
+    Double_t      fMinRadius;
+    Double_t      fMaxRadius;
+
 public:
-    MCaos() : fFile(NULL), fHistpr(NULL)
+    MCaos() : fFile(NULL), fHistpr(NULL), fMinNumberLeds(5), fMinRadius(265), fMaxRadius(268)
     {
         fLeds = new Leds;
     }
+    
     ~MCaos()
     {
@@ -66,4 +71,15 @@
     void OpenFile();
     void CloseFile();
+    
+    void SetMinNumberRings(Short_t n) 
+    {
+	fMinNumberLeds = n;
+    }
+
+    void SetRadii(Double_t min, Double_t max) 
+    {  
+	fMinRadius=min;
+	fMaxRadius=max;
+    }
 
     void InitHistograms();
@@ -72,5 +88,6 @@
     void ResetHistograms();
 
-    Ring Run(byte *img, bool printl, bool printr, const ZdAz &pos, const MTime &t);
+    Ring Run(byte *img, bool printl, bool printr, const ZdAz &pos, 
+	     const MTime &t, Double_t box, Double_t cut);
 };
 
Index: trunk/MagicSoft/Cosy/main/MCosy.h
===================================================================
--- trunk/MagicSoft/Cosy/main/MCosy.h	(revision 4856)
+++ trunk/MagicSoft/Cosy/main/MCosy.h	(revision 4865)
@@ -203,4 +203,7 @@
     MLog *GetOutRep() { return fOutRep; }
 
+    //rwagner
+    MDriveCom *GetDriveCom() { return fCom; }
+
     // static ZdAz CorrectTarget(const ZdAz &src, const ZdAz &dst);
     //    static ZdAz RaDec2ZdAz(const double mjd, const RaDec &pos, const RaDec &pm=RaDec(0,0));
Index: trunk/MagicSoft/Cosy/main/MStargHistograms.cc
===================================================================
--- trunk/MagicSoft/Cosy/main/MStargHistograms.cc	(revision 4865)
+++ trunk/MagicSoft/Cosy/main/MStargHistograms.cc	(revision 4865)
@@ -0,0 +1,171 @@
+#include "MStargHistograms.h"
+
+#include <iostream>
+#include <iomanip>
+
+#include <TSystem.h>
+#include <TFile.h>
+#include <TTree.h>
+#include <TH1.h>
+#include <TH2.h>
+#include <TGraph.h>
+#include <TCanvas.h>
+
+#include "MTime.h"
+
+#include "Led.h"
+#include "Leds.h"
+#include "Ring.h"
+#include "Rings.h"
+#include "FilterLed.h"
+#include "MStarList.h"
+
+#include "coord.h"
+
+void MStargHistograms::OpenFile()
+{
+        int i=0;
+        char name[100];
+        while (1)
+        {
+            sprintf(name, "data/starg%03d.root", i++);
+            if (gSystem->AccessPathName(name, kFileExists))
+                break;
+        }
+
+        if (fFile)
+            delete fFile;
+
+        fFile = new TFile(name, "RECREATE");
+
+        if (!fFile->IsOpen())
+        {
+            delete fFile;
+            fFile = NULL;
+
+            cout << "Error: Cannot open file '" << name << "'" << endl;
+        }
+
+        TTree *tree = new TTree("Data", "Real CaOs Data");
+
+
+        fEvtTime = 0;
+
+
+	// Tracking Position Zd, Az in deg
+        tree->Branch("PosZd.",      &fZenithDist, "fZenithDist/D");
+        tree->Branch("PosAz.",      &fAzimuth,    "fAzimuth/D");
+	// Event time, arbitrary start
+        tree->Branch("EvtTime.",    &fEvtTime,    "fEvtTime/D");
+	// Pointing Position Zd, Az in deg
+        tree->Branch("SaoZd.",      &fNomZd,      "fNomZd/D");
+        tree->Branch("SaoAz.",      &fNomAz,     "fNomAz/D");
+	// Misspointing from Starguider Zd, Az in deg
+        tree->Branch("MisZd.",      &fdZd,        "fdZd/D");
+        tree->Branch("MisAz.",      &fdAz,       "fdAz/D");
+	// LED Offset from StargLEDFinder, obsolete
+        tree->Branch("LEDOffsetX.", &fOffsetX,        "fOffsX/D");
+        tree->Branch("LEDOffsetY.", &fOffsetY,       "fOffsY/D");
+	// Center of Camera, offset from arb coords, in pix
+        tree->Branch("CenterX.",    &fCenterX,        "fCenterX/D");
+        tree->Branch("CenterY.",    &fCenterY,       "fCenterY/D");
+	// Center of Camera, offset from arb coords, in Zd, Az, deg
+        tree->Branch("CenterZd.",    &fCenterZd,        "fCenterZd/D");
+        tree->Branch("CenterAz.",    &fCenterAz,       "fCenterAz/D");
+	// Position of Star in Camera in Zd, Az
+        tree->Branch("StarZd.",    &fStarZd,        "fStarZd/D");
+        tree->Branch("StarAz.",    &fStarAz,       "fStarAz/D");
+	// number of spots found
+        tree->Branch("Spots.",      &fSpots,       "fSpots/D");
+	// number of stars expected
+        tree->Branch("Stars.",      &fStars,       "fStars/D");
+        tree->Branch("Bright.",      &fBright,       "fBright/D");
+
+
+        cout << "Root file '" << name << "' open." << endl;
+}
+
+void MStargHistograms::CloseFile()
+{
+        if (!fFile)
+            return;
+
+        const TString  name = fFile->GetName();
+        const Double_t n    = ((TTree*)fFile->Get("Data"))->GetEntries();
+
+        fFile->Write();
+        delete fFile;
+        fFile = NULL;
+
+        cout << "Root file closed (n=" << n << ")" << endl;
+
+        if (n<1)
+        {
+            gSystem->Unlink(name);
+            cout << "Root file deleted - no entries." << endl;
+        }
+}
+
+void MStargHistograms::InitHistograms()
+{
+    
+}
+
+void MStargHistograms::DeleteHistograms()
+{
+
+}
+
+void MStargHistograms::ShowHistograms()
+{
+
+}
+
+void MStargHistograms::ResetHistograms()
+{
+  
+}
+
+void MStargHistograms::Fill(Leds &spots, MStarList &stars, ZdAz &d, ZdAz sao, Ring &center,  ZdAz &centerzdaz, ZdAz &star, Double_t bright, const ZdAz &pos, const MTime &t)
+{
+      // FIXME!
+      static const MTime t0(t);
+      fEvtTime = t-t0;
+    
+      //    cout << "@ evttime " << fEvtTime << endl;
+
+      if (fFile && spots.GetEntries()>0)
+      {
+          fZenithDist = pos.Zd(); //fCosy ? fCosy->GetPointingPos().Zd() : 0
+          fAzimuth    = pos.Az(); //fCosy ? fCosy->GetPointingPos().Az() : 0;
+	  fNomZd      = sao.Zd();
+	  fNomAz      = sao.Az();
+	  fdZd        = d.Zd();
+	  fdAz        = d.Az();
+
+	  fCenterX    = center.GetX();
+	  fCenterY    = center.GetY();
+	  fCenterZd   = centerzdaz.Zd();
+	  fCenterAz   = centerzdaz.Az();
+	  fStarZd     = star.Zd();
+	  fStarAz     = star.Az();
+	  fStars      = stars.GetRealEntries();
+	  fSpots      = spots.GetEntries();
+	  fBright     = bright;
+
+  	//  cout << " Evttime=" << fEvtTime 
+//  	     << " ZD=" << fZenithDist 
+//  	     << " Az=" << fAzimuth 
+//  	     << " NomZd=" << fNomZd 
+//  	     << " NomAz=" << fNomAz 
+//  	     << " dZd=" << fdZd 
+//  	     << " dAz=" << fdAz 
+//  	     << " OffsX=" << fOffsetX
+//  	     << " OffsY=" << fOffsetY
+//  	     <<endl;
+	
+          TTree *t = (TTree*)fFile->Get("Data");
+          t->Fill();
+      }
+         
+}
Index: trunk/MagicSoft/Cosy/main/MStargHistograms.h
===================================================================
--- trunk/MagicSoft/Cosy/main/MStargHistograms.h	(revision 4865)
+++ trunk/MagicSoft/Cosy/main/MStargHistograms.h	(revision 4865)
@@ -0,0 +1,64 @@
+#ifndef STARG_MStargHistograms
+#define STARG_MStargHistograms
+
+#include <TFile.h>
+
+class TTree;
+class MTime;
+class ZdAz;
+class Ring;
+class Led;
+class Leds;
+class MStarList;
+class ZdAz;
+
+class MStargHistograms
+{
+private:
+    TFile*         fFile;
+    Double_t       fEvtTime;
+    Double_t       fZenithDist;
+    Double_t       fAzimuth;
+    Double_t       fNomZd;
+    Double_t       fNomAz;
+    Double_t       fdZd;
+    Double_t       fdAz;
+    Double_t       fOffsetX;
+    Double_t       fOffsetY;
+    Double_t       fCenterX;
+    Double_t       fCenterY;
+    Double_t       fStars;
+    Double_t       fSpots;
+    Double_t       fBright;
+    Double_t       fCenterZd;
+    Double_t       fCenterAz;
+    Double_t       fStarZd;
+    Double_t       fStarAz;
+
+
+public:
+    MStargHistograms() : fFile(NULL)
+    {
+  
+    }
+
+    ~MStargHistograms()
+    {
+    
+    }
+
+    void Fill(Leds &spots, MStarList &stars, ZdAz &d, ZdAz sao, 
+	      Ring &center, ZdAz &centerzdaz, ZdAz &star, Double_t bright,
+	      const ZdAz &pos, const MTime &t);
+    
+    void OpenFile();
+    void CloseFile();
+
+    void InitHistograms();
+    void DeleteHistograms();
+    void ShowHistograms();
+    void ResetHistograms();
+
+};
+
+#endif
Index: trunk/MagicSoft/Cosy/main/MStarguider.cc
===================================================================
--- trunk/MagicSoft/Cosy/main/MStarguider.cc	(revision 4856)
+++ trunk/MagicSoft/Cosy/main/MStarguider.cc	(revision 4865)
@@ -1,2 +1,4 @@
+#define EXPERT
+
 #include "MStarguider.h"
 
@@ -9,4 +11,7 @@
 #include <TTimer.h>
 #include <TSystem.h>
+#include <TFile.h> // temp writeout of histos
+#include "MAstro.h"
+#include "MString.h"
 
 #include <TGMenu.h>
@@ -15,9 +20,15 @@
 #include <TGSplitter.h>    // TGHorizontal3DLine
 #include <TGTextEntry.h>
-
+#include <TGLayout.h>
 #include "MString.h"
 
 #include "MCosy.h"
 #include "MCaos.h"
+// #include "MStargLeds.h"
+#include "MStargHistograms.h"
+#include "MDriveCom.h"
+
+#include "MGStarg.h" 
+#include "TGFrame.h"
 
 #include "MGImage.h"
@@ -91,5 +102,11 @@
     IDM_kCaosWriteStart,
     IDM_kCaosWriteStop,
-    IDM_kResetHistograms
+    IDM_kResetHistograms,
+    IDM_kStargHistograms,
+    IDM_kStargAnalysis,
+    IDM_kStargCaosFilter,
+    IDM_kStargLEDFilter,
+    IDM_kStargFindStar
+
 };
 
@@ -105,4 +122,6 @@
         fCosy->GetWin()->GetImage()->DoRedraw();
 
+    fGStarg->Update(fPos, fD);   
+ 
     return kTRUE;
 }
@@ -122,6 +141,6 @@
 
     fChannel = new MGPopupMenu(p);
-    fChannel->AddEntry("Channel #1", IDM_kChannel1);
-    fChannel->AddEntry("Channel #2", IDM_kChannel2);
+    fChannel->AddEntry("Starfield Camera", IDM_kChannel1);
+    fChannel->AddEntry("TPoint Camera", IDM_kChannel2);
     fChannel->CheckEntry(channel==0?IDM_kChannel1:IDM_kChannel2);
     fChannel->Associate(this);
@@ -134,11 +153,17 @@
     fDisplay->AddEntry("&Filter",      IDM_kFilter);
     fDisplay->AddEntry("Find &Star",   IDM_kFindStar);
-    fDisplay->AddEntry("C&aosFilter",  IDM_kCaosFilter);
-    fDisplay->AddEntry("Sao &Catalog", IDM_kCatalog);
+    fDisplay->AddEntry("C&aos Filter",  IDM_kCaosFilter);
+    fDisplay->AddEntry("SAO &Catalog", IDM_kCatalog);
+    fDisplay->AddEntry("Stretch",      IDM_kStretch);
+    fDisplay->AddSeparator();
     fDisplay->AddEntry("Starguider",   IDM_kStarguider);
-    fDisplay->AddEntry("Stretch",      IDM_kStretch);
+    fDisplay->AddEntry("Starguider LED Filter",  IDM_kStargCaosFilter);
+    fDisplay->AddEntry("Starguider Find Star",   IDM_kStargFindStar);
+    fDisplay->AddEntry("Starguider Analysis",   IDM_kStargAnalysis);
+    fDisplay->AddSeparator();    
     if (channel>=0)
         fDisplay->AddPopup("&Input",   fChannel);
-    fDisplay->DisableEntry(IDM_kStarguider);
+    fDisplay->DisableEntry(IDM_kStargAnalysis);
+    fDisplay->DisableEntry(IDM_kStargFindStar);
     fDisplay->CheckEntry(IDM_kStretch);
     fDisplay->Associate(this);
@@ -218,5 +243,5 @@
     {
         fInterpol->CheckEntry(IDM_kInterpol5);
-        fIntRate = 5;
+        fIntRate = 50;
     }
     else
@@ -247,6 +272,6 @@
 
     fCaosAnalyse = new MGPopupMenu(p);
-    fCaosAnalyse->AddEntry("S&tart Analyse", IDM_kCaosAnalStart);
-    fCaosAnalyse->AddEntry("St&opp Analyse", IDM_kCaosAnalStop);
+    fCaosAnalyse->AddEntry("S&tart Analysis", IDM_kCaosAnalStart);
+    fCaosAnalyse->AddEntry("St&op Analysis", IDM_kCaosAnalStop);
     fCaosAnalyse->DisableEntry(IDM_kCaosAnalStop);
     //    fCaosAnalyse->AddEntry("&Reset Histograms", IDM_kResetHistograms);
@@ -272,4 +297,13 @@
     fList->Add(fCaOs);
 
+    TGLayoutHints *hints2a = 
+	new TGLayoutHints(kLHintsCenterX|kLHintsCenterY|
+			  kLHintsExpandX|kLHintsExpandY,1,1);
+    fList->Add(hints2a);
+
+    fGStarg = new MGStarg(this, 235);
+    fGStarg->Move(530,596+5);
+    fList->Add(fGStarg);
+
     fCRaDec = new MGCoordinates(this, kETypeRaDec);
     fCRaDec->Move(4, fMenu->GetDefaultHeight()+584);
@@ -278,15 +312,25 @@
 
     fCZdAz = new MGCoordinates(this, kETypeZdAz, kFALSE);
-    fCZdAz->Move(240+12+10, fMenu->GetDefaultHeight()+584);
+    fCZdAz->Move(240+12+28, fMenu->GetDefaultHeight()+581+12);
     AddFrame(fCZdAz);
     fList->Add(fCZdAz);
 
     fPZdAz = new MGCoordinates(this, kETypeZdAz, kFALSE);
-    fPZdAz->Move(240+12+10, fMenu->GetDefaultHeight()+630);
+    fPZdAz->Move(240+12+28, fMenu->GetDefaultHeight()+627+2*12);
     AddFrame(fPZdAz);
     fList->Add(fPZdAz);
 
+    fDZdAz = new MGCoordinates(this, kETypeZdAz, kFALSE);
+    fDZdAz->Move(240+12+28, fMenu->GetDefaultHeight()+673+3*12);
+    AddFrame(fDZdAz);
+    fList->Add(fDZdAz);
+
+    fSZdAz = new MGCoordinates(this, kETypeZdAz, kFALSE);
+    fSZdAz->Move(240+12+28, fMenu->GetDefaultHeight()+719+4*12);
+    AddFrame(fSZdAz);
+    fList->Add(fSZdAz);
+
     fTPoint = new TGTextButton(this, "TPoint");
-    fTPoint->Move(4, fMenu->GetDefaultHeight()+665);
+    fTPoint->Move(4, fMenu->GetDefaultHeight()+722+3*12+25);
     fTPoint->AllowStayDown(kTRUE);
     AddFrame(fTPoint);
@@ -295,5 +339,5 @@
     fFps = new TGLabel(this, "---fps");
     fFps->SetTextJustify(kTextRight);
-    fFps->Move(633, fMenu->GetDefaultHeight()+578);
+    fFps->Move(650-440, fMenu->GetDefaultHeight()+619+13+60+20);
     AddFrame(fFps);
     fList->Add(fFps);
@@ -301,33 +345,53 @@
     fPosZoom = new TGLabel(this, "----.--d/----.--d (----, ----)");
     fPosZoom->SetTextJustify(kTextRight);
-    fPosZoom->Move(620, fMenu->GetDefaultHeight()+678);
+    fPosZoom->Move(4, fMenu->GetDefaultHeight()+765);
     AddFrame(fPosZoom);
     fList->Add(fPosZoom);
 
-    TGLabel *l = new TGLabel(this, "Arb.-Sky Pos");
+    fSkyBright = new TGLabel(this, "Sky Brightness: ---         ");
+    fSkyBright->SetTextJustify(kTextLeft);
+    fSkyBright->Move(4, fMenu->GetDefaultHeight()+785);
+    AddFrame(fSkyBright);
+    fList->Add(fSkyBright);
+
+    TGLabel *l = new TGLabel(this, "arcsec/pix");
     l->SetTextJustify(kTextLeft);
-    l->Move(480+32, fMenu->GetDefaultHeight()+590);
+    l->Move(605-400, fMenu->GetDefaultHeight()+619+13+60);
     AddFrame(l);
     fList->Add(l);
 
-    l = new TGLabel(this, "arcsec/pix");
+    l = new TGLabel(this, "deg");
     l->SetTextJustify(kTextLeft);
-    l->Move(605, fMenu->GetDefaultHeight()+619+13);
+    l->Move(605-410, fMenu->GetDefaultHeight()+619-10+60);
     AddFrame(l);
     fList->Add(l);
 
-    l = new TGLabel(this, "deg");
+    l = new TGLabel(this, "Telescope pointing at");
     l->SetTextJustify(kTextLeft);
-    l->Move(605, fMenu->GetDefaultHeight()+619-10);
+    l->Move(240+12+20, fMenu->GetDefaultHeight()+584-5);
     AddFrame(l);
     fList->Add(l);
 
-    l = new TGLabel(this, "Pointing Pos");
+    l = new TGLabel(this, "Starguider position");
     l->SetTextJustify(kTextLeft);
-    l->Move(480+32, fMenu->GetDefaultHeight()+655);
+    l->Move(240+12+20, fMenu->GetDefaultHeight()+630+12-5);
     AddFrame(l);
     fList->Add(l);
 
-    const Double_t pixsize = 23.4;
+    l = new TGLabel(this, "Misspointing");
+    l->SetTextJustify(kTextLeft);
+    l->Move(240+12+20, fMenu->GetDefaultHeight()+676+2*12-5);
+    AddFrame(l);
+    fList->Add(l);
+
+#ifdef EXPERT
+    l = new TGLabel(this, "Misspointing/FindStar (Experts Only!)");
+    l->SetTextJustify(kTextLeft);
+    l->Move(240+12+20, fMenu->GetDefaultHeight()+722+3*12-5);
+    AddFrame(l);
+    fList->Add(l);
+#endif
+    
+    const Double_t pixsize = 48.8; // used to be 23.4
 
     fSao->SetPixSize(pixsize);
@@ -339,5 +403,5 @@
     fPixSize = new TGTextEntry(this, txt, IDM_kPixSize);
     fPixSize->SetAlignment(kTextCenterX);
-    fPixSize->Move(547, fMenu->GetDefaultHeight()+617+13);
+    fPixSize->Move(547-410, fMenu->GetDefaultHeight()+617+13+60);
     AddFrame(fPixSize);
     fList->Add(fPixSize);
@@ -345,5 +409,5 @@
     fAngle = new TGTextEntry(this, "         0", IDM_kAngle);
     fAngle->SetAlignment(kTextCenterX);
-    fAngle->Move(547, fMenu->GetDefaultHeight()+617-10);
+    fAngle->Move(547-410, fMenu->GetDefaultHeight()+617-10+60);
     AddFrame(fAngle);
     fList->Add(fAngle);
@@ -357,5 +421,6 @@
     //
     fZoomImage = new MGImage(this, kZOOM, kZOOM);
-    fZoomImage->Move(768-kZOOM-2, 700-kZOOM-2);
+    // fZoomImage->Move(768-kZOOM-2, 700-kZOOM-2);
+    fZoomImage->Move(4, 700-kZOOM-2+85);
     AddFrame(fZoomImage);
     fList->Add(fZoomImage);
@@ -374,7 +439,13 @@
     MapSubwindows();
     fTPoint->UnmapWindow();
+    fGStarg->UnmapWindow();
+    fPZdAz->UnmapWindow();
+    fDZdAz->UnmapWindow();
+    fSZdAz->UnmapWindow();
+    fSkyBright->UnmapWindow();
     MapWindow();
 
-    IconifyWindow();
+
+    //IconifyWindow();
 
     //------------------------------------------------------------
@@ -386,6 +457,7 @@
 
 MStarguider::MStarguider(MObservatory::LocationName_t obs, Int_t channel)
-: TGMainFrame(gClient->GetRoot(), 768, 740), fCosy(NULL), fOutTp(0), fDx((768-kZOOM)/2), fDy((512-kZOOM)/2)
-{
+    : TGMainFrame(gClient->GetRoot(), 768, 840), fCosy(NULL), fOutTp(0), fDx((768-kZOOM)/2), fDy((512-kZOOM)/2), fStatus(MDriveCom::kStandby)
+{
+    
     cout << " #### FIXME: Make MCaos Thread safe!" << endl;
 
@@ -393,6 +465,16 @@
     fRaDec = new RaDec(180, 40);
 
+    //    fStargLeds = new MStargLeds;
+    //    fStargLeds->ReadResources();
+
     fCaos = new MCaos;
     fCaos->ReadResources();
+
+    fStargCaos = new MCaos;
+    fStargCaos->ReadResources("stargleds.txt");
+    fStargCaos->SetMinNumberRings(2);
+    fStargCaos->SetRadii(158,164);
+
+    fStargHistograms = new MStargHistograms();
 
     InitGui(channel);
@@ -427,6 +509,9 @@
 
     delete fCaos;
+    delete fStargCaos;
+    //    delete fStargLeds;
+    delete fStargHistograms;
     delete fSao;
-    delete fRaDec;
+    delete fRaDec;    
 
     if (fOutTp)
@@ -491,28 +576,109 @@
             case IDM_kCatalog:
                 Toggle(fDisplay, IDM_kCatalog);
-                if (fDisplay->IsEntryChecked(IDM_kCatalog))
-                    fDisplay->EnableEntry(IDM_kStarguider);
-                else
-                {
-                    fDisplay->UnCheckEntry(IDM_kStarguider);
-                    fDisplay->DisableEntry(IDM_kStarguider);
-                }
+//              if (!fDisplay->IsEntryChecked(IDM_kCatalog))
+//                 {
+//                     fDisplay->UnCheckEntry(IDM_kStarguider);
+//                     fDisplay->DisableEntry(IDM_kStarguider);
+//                 } else {
+// 		    fDisplay->EnableEntry(IDM_kStarguider);
+// 		}
+                return kTRUE;
+
+           case IDM_kStargFindStar:
+                Toggle(fDisplay, IDM_kStargFindStar);         
+		if (fDisplay->IsEntryChecked(IDM_kStargFindStar)) {
+		    fSZdAz->MapWindow();
+		} else {
+		    fSZdAz->UnmapWindow();
+		}
                 return kTRUE;
 
             case IDM_kStarguider:
                 Toggle(fDisplay, IDM_kStarguider);
+
+		if (fDisplay->IsEntryChecked(IDM_kStarguider)) {	
+
+  		    fLastBright = 0xff;
+
+		    fDisplay->DisableEntry(IDM_kFindStar);
+		    fDisplay->EnableEntry(IDM_kStargAnalysis);		  
+		    
+		    fPZdAz->MapWindow();
+		    fDZdAz->MapWindow();
+		    fSkyBright->MapWindow();
+
+		    fSao->SetLimitMag(8.3);
+		    fIntRate = 125;
+
+		    fDisplay->CheckEntry(IDM_kCatalog);
+		    fGStarg->MapWindow();
+
+		    const Int_t ch0 = 
+			fChannel->IsEntryChecked(IDM_kChannel1) ? 0 : 1;
+                    const Int_t ch1 = 0;
+
+                    if (ch0!=ch1)
+		    {
+
+			delete fGetter;
+			usleep(150000); // FIX: Device or resource busy.
+			fGetter = new Camera(*this, ch1);
+			((Camera*)fGetter)->Loop(0);
+		    }
+
+		    fChannel->CheckEntry(IDM_kChannel1);
+		    fChannel->UnCheckEntry(IDM_kChannel2);
+		    fChannel->DisableEntry(IDM_kChannel2);
+
+		} else {
+
+		    fStatus = MDriveCom::kStandby;
+		
+		    fPZdAz->UnmapWindow();
+		    fDZdAz->UnmapWindow();
+		    fSkyBright->UnmapWindow();
+		    fGStarg->UnmapWindow();		
+		    fChannel->EnableEntry(IDM_kChannel2);
+		    fDisplay->EnableEntry(IDM_kFindStar);
+		    fDisplay->DisableEntry(IDM_kStargAnalysis);		    
+		}
+
                 gSystem->Unlink("tracking_error.txt");
+
+                return kTRUE;
+
+            case IDM_kStargAnalysis:
+                Toggle(fDisplay, IDM_kStargAnalysis);
+		if (fDisplay->IsEntryChecked(IDM_kStargAnalysis)) {
+		    fStargHistograms->OpenFile();
+		} else {
+		    fStargHistograms->CloseFile();
+		}
+
+                return kTRUE;
+
+
+            case IDM_kStargHistograms:
+                Toggle(fDisplay, IDM_kStargHistograms);
+                return kTRUE;
+
+            case IDM_kStargLEDFilter:
+                Toggle(fDisplay, IDM_kStargLEDFilter);
                 return kTRUE;
 
             case IDM_kFilter:
-                Toggle(fDisplay, IDM_kFilter);
-                if (fDisplay->IsEntryChecked(IDM_kFilter))
-                    fDisplay->EnableEntry(IDM_kStarguider);
-                else
-                {
-                    fDisplay->UnCheckEntry(IDM_kStarguider);
-                    fDisplay->DisableEntry(IDM_kStarguider);
-                }
-                return kTRUE;
+                Toggle(fDisplay, IDM_kFilter);           
+                return kTRUE;
+
+//              case IDM_kFilterStarg:
+//                  Toggle(fDisplay, IDM_kFilterStarg);
+//                  if (fDisplay->IsEntryChecked(IDM_kFilterStarg))
+//                      fDisplay->EnableEntry(IDM_kStarguider);
+//                  else
+//                  {
+//                      fDisplay->UnCheckEntry(IDM_kStarguider);
+//                      fDisplay->DisableEntry(IDM_kStarguider);
+//                  }
+//                  return kTRUE;
 
             case IDM_kFindStar:
@@ -535,6 +701,7 @@
                 //    fCaos->OpenFile();
                 Toggle(fDisplay, IDM_kCaosFilter);
-                if (fDisplay->IsEntryChecked(IDM_kCaosFilter))
+                if (fDisplay->IsEntryChecked(IDM_kCaosFilter)) {
                     fMenu->AddPopup("&CaOs", fCaOs, NULL);
+		}
                 else
                 {
@@ -558,7 +725,22 @@
                 }
                 fMenu->Resize(fMenu->GetDefaultSize());
-                MapSubwindows();
+                MapSubwindows(); // maps everything, but we don't want that
+		fTPoint->UnmapWindow(); 
+		fGStarg->UnmapWindow();
+		fPZdAz->UnmapWindow();
+		fDZdAz->UnmapWindow();
+		fSZdAz->UnmapWindow();
+		fSkyBright->UnmapWindow();
                 MapWindow();
                 return kTRUE;
+
+            case IDM_kStargCaosFilter:             
+		Toggle(fDisplay, IDM_kStargCaosFilter);                
+		if (fDisplay->IsEntryEnabled(IDM_kStargCaosFilter)) {
+		    fDisplay->EnableEntry(IDM_kStargFindStar);
+		} else {
+		    fDisplay->DisableEntry(IDM_kStargFindStar);
+		}
+		return kTRUE;
 
             case IDM_kCaosPrintLeds:
@@ -665,5 +847,6 @@
                     const Int_t ch0 = fChannel->IsEntryChecked(IDM_kChannel1) ? 0 : 1;
                     const Int_t ch1 = mp1==IDM_kChannel1                      ? 0 : 1;
-                    if (ch0==ch1)
+
+		    if (ch0==ch1)
                         return kTRUE;
 
@@ -754,12 +937,13 @@
     // Viewable area (FIXME: AZ)
     //
-    TH2F h("Hist", "dX/dY", 77, -768/2-.5, 768/2+.5, 58, -576/2-.5, 576/2+.5); // 3
-
-    /*
+    // TH2F h("Hist", "dX/dY", 77, -768/2-.5, 768/2+.5, 58, -576/2-.5, 576/2+.5); // 3
+    // chose a bit coarser binning to enhance excess 
+    // important: chose binning symmetrical around (0|0)!
+    TH2F h("Hist", "dX/dY", 49, -768/2-8, 768/2+8, 37, -576/2-8, 576/2+8); // 3
+
     TH1F hmag("HistMag", "Mag", 19, 0, 100);
     for (int i=0; i<mag.GetSize(); i++)
         hmag.Fill(mag[i]);
-        */
-
+    
     //
     // Search for matching Magnitudes
@@ -772,5 +956,5 @@
 
     //
-    // Serach for an excess in the histogram
+    // Search for an excess in the histogram
     //
     Int_t mx, my, dummy;
@@ -783,6 +967,8 @@
     const double dy   = h.GetYaxis()->GetBinWidth(my);
 
+#ifdef EXPERT
     cout << setprecision(3);
     cout << "Cut-XY:       " << xmax << " +- " << dx << " / " << ymax << " +- " << dy << endl;
+#endif
 
     TGraph g;
@@ -790,13 +976,15 @@
     {
         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))
+              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]);
-    }
-
+        g.SetPoint(g.GetN(), x[i], y[i]);	
+    }
+  
+#ifdef EXPERT
     cout << "Offset-XY:    " << g.GetMean(1) << " +- " << g.GetRMS(1) << " / ";
     cout << g.GetMean(2) << " +- " << g.GetRMS(2) << endl;
+#endif
 
     AltAz pos0 = fSao->CalcAltAzFromPix(768/2,              576/2)*kRad2Deg;
@@ -817,27 +1005,41 @@
     fout2 << -pos1.Alt() << " " << pos1.Az() << endl;
 
+//    if (g.GetMean(1)>9 || g.GetMean(2)>9) {
+//      TFile f1("sguider-highoffset.root","UPDATE");
+//      h.Write();
+//      hmag.Write();
+//      g.Write();
+//      f1.Close(); 
+//    } else {
+//      TFile f1("sguider-niceoffset.root","UPDATE");
+//     h.Write();
+//      hmag.Write();
+//      g.Write();
+ //     f1.Close();
+	
+
+//	}
+
     return ZdAz(-pos1.Alt(), pos1.Az());
 }
 
-void MStarguider::CalcTrackingError(Leds &leds, MStarList &stars)
+bool MStarguider::CalcTrackingError(Leds &leds, MStarList &stars, ZdAz &d, MTime &t)
 {
     const Int_t max = leds.GetEntries();
-
-    if (stars.GetRealEntries() < 3)
-    {
-        cout << "Sorry, less than 3 stars in FOV!" << endl;
-        return;
-    }
-
-    if (max < 1)
-    {
-        cout << "Sorry, less than 1 detected spot in FOV!" << endl;
-        return;
-    }
+      if (stars.GetRealEntries() < 3)
+      {
+          cout << "Sorry, less than 3 stars in FOV!" << endl;
+          return kFALSE;
+      }
+      if (max < 3) //was 1
+      {
+          cout << "Sorry, less than 3 detected spot in FOV!" << endl;	 
+          return kFALSE;	 
+      }
 
     stars.Sort(); // Sort by magnitude
 
     TString str = "data/tracking_";
-    str += fSao->GetMjd()-52000;
+    str += fSao->GetMjd();
     str += ".txt";
 
@@ -848,40 +1050,50 @@
     Int_t num = 0;
 
-    // FIXME: Is predifined value 3 a good idea?
+    // FIXME: Is predefined value 3 a good idea?
 
     MStar *star;
     MStarListIter NextStar(&stars);
-    while ((star=NextStar()) && num++<max+3)
+    while ((star=NextStar()) && num++<max+3) 
     {
         TIter NextSp(&leds);
         Led *spot=NULL;
-        while ((spot=(Led*)NextSp()))
-        {
-            const XY dpos(spot->GetX()-star->GetX(), spot->GetY()-star->GetY());
-
+        while ((spot=(Led*)NextSp())) 
+	{
+            const XY dpos(spot->GetX()-(768-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)
+            if (fout) {
+		fout << spot->GetX() << " " 
+		     << spot->GetY()  << " " 
+		     << spot->GetMag()  << " " 
+		     << star->GetX() << " " 
+		     << star->GetY()  << " " 
+		     << star->GetMag()  << " ";
                 fout << x[idx] << " " << y[idx] << " " << mag[idx] << endl;
+	    }
         }
-    }
-
-    ZdAz d = TrackingError(x, y, mag);
-
+    }  
+
+    d = TrackingError(x, y, mag);
+    fDZdAz->SetCoordinates(d);
+    
     //
     // Calculated offsets
     //
 
+#ifdef EXPERT
     // 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;
+#endif
 
     //
@@ -890,11 +1102,14 @@
     ZdAz cpos = fSao->GetZdAz()-d;
     fPZdAz->SetCoordinates(cpos);
-}
-
-void MStarguider::FindStar(FilterLed &f, FilterLed &f2, Ring &center, MTime &t)
+
+    return kTRUE; 
+
+}
+
+ZdAz MStarguider::FindStar(FilterLed &f, FilterLed &f2, Ring &center, MTime &t, Double_t cut, Double_t box, Double_t scalefactor = 1.0)
 {
     // Set search Paremeters (FIXME: Get them from user input!)
-    f.SetCut(3.5);
-    f.SetBox(70);
+    f.SetCut(cut);  // 3.5
+    f.SetBox(box);  // 70
 
     // Try to find Led in this area
@@ -902,8 +1117,8 @@
     f.FindStar(leds, (Int_t)center.GetX(), (Int_t)center.GetY());
 
-    // Check wheter star found
+    // Check whether star found
     Led *star = (Led*)leds.At(0);
     if (!star || leds.GetEntries()<1)
-        return;
+        return ZdAz(.0,.0);
 
     cout << "Found star @ " << flush;
@@ -932,6 +1147,6 @@
 
     // Convert from Pixel to millimeter (1pix=2.9mm)
-    x *= 2.58427;
-    y *= 2.58427;
+    x *= (2.58427 * scalefactor);
+    y *= (2.58427 * scalefactor);
 
     // Correct for abberation.
@@ -945,7 +1160,9 @@
     cout << "STAR-Offset: " << MTime(-1) << " dZd=" << dzd << "d  dAz=" << daz << "d" << endl;
 
+    ZdAz zdaz(dzd,daz);
+
     // Check TPoint data set request
     if (!fTPoint->IsDown())
-        return;
+        return zdaz;
     fTPoint->SetDown(kFALSE);
 
@@ -1011,4 +1228,6 @@
         outrep.UnLock("MStarguider::FindStar");
     }
+    
+    return zdaz;
 }
 
@@ -1042,5 +1261,6 @@
 }
 
-void MStarguider::ProcessFrame(const unsigned long n, byte *img, struct timeval *tm)
+void MStarguider::ProcessFrame(const unsigned long n, byte *img, 
+			       struct timeval *tm)
 {
     static unsigned long n0 = n;
@@ -1066,8 +1286,8 @@
     FilterLed f(img,   768, 576, 2.5); // 2.5
     FilterLed f2(cimg, 768, 576); // former color 0xb0
-
+    
     if (fDisplay->IsEntryChecked(IDM_kStretch))
         f.Stretch();
-
+    
     if (!fWritePictures->IsEntryEnabled(IDM_kStart) &&
         (!(n%fWrtRate) || fWriteType->IsEntryChecked(IDM_kOnce)))
@@ -1075,16 +1295,26 @@
         if (fFileType->IsEntryChecked(IDM_kPNG))
             Writer::Png("pix/file", img, tm);
-
+	
         if (fFileType->IsEntryChecked(IDM_kPPM))
             Writer::Ppm("pix/file", img, tm);
-
+	
         if (fWriteType->IsEntryChecked(IDM_kOnce))
             ProcessMessage(MK_MSG(kC_COMMAND, kCM_MENU), IDM_kStop, 0);
     }
-
-    MStarList spots;
+    
+    // Visual Filter, whole FOV
     if (fDisplay->IsEntryChecked(IDM_kFilter))
         f.Execute();
-
+   
+    /* ---------------------------------- 
+    // Visual StarFilter for Starguider
+    if (fDisplay->IsEntryChecked(IDM_kFilterStarg) && !fDisplay->IsEntryChecked(IDM_kStarguider)) {
+      f.SetBox(230);
+      f.AddIgnoreRegion(295,112,384,204);
+      f.Execute(530, 292);
+    }
+    ------------------------------------- */
+
+    // Find Center of Camera for Caos and Tpoints
     Ring center(768/2, 576/2);
     if (fDisplay->IsEntryChecked(IDM_kCaosFilter))
@@ -1095,10 +1325,10 @@
         if (fCosy)
             pos = fCosy->GetPointingPos();
-
-        center = fCaos->Run(img, printl, printr, pos, t);
-    }
-
+        center = fCaos->Run(img, printl, printr, pos, t, 50, 3.0);
+    }
+
+    // Find Star at Center---for Tpoint Procedure
     if (fDisplay->IsEntryChecked(IDM_kFindStar))
-        FindStar(f, f2, center, t);
+        ZdAz zdaz = FindStar(f, f2, center, t, 3.5, 70);
 
     byte zimg[kZOOM*kZOOM];
@@ -1111,46 +1341,160 @@
     if (fCosy)
     {
-        byte simg[(768/2-1)*(576/2-1)];
-        for (int y=0; y<576/2-1; y++)
-            for (int x=0; x<768/2-1; x++)
-                simg[x+y*(768/2-1)] = ((unsigned int)img[2*x+2*y*768]+img[2*x+2*y*768+1]+img[2*x+2*(y+1)*768]+img[2*x+2*(y+1)*768+1])/4;
-
-        fCosy->GetWin()->GetImage()->DrawImg(simg);
-    }
-
+	byte simg[(768/2-1)*(576/2-1)];
+	for (int y=0; y<576/2-1; y++)
+	    for (int x=0; x<768/2-1; x++)
+		simg[x+y*(768/2-1)] = ((unsigned int)img[2*x+2*y*768]+img[2*x+2*y*768+1]+img[2*x+2*(y+1)*768]+img[2*x+2*(y+1)*768+1])/4;	
+	
+	fCosy->GetWin()->GetImage()->DrawImg(simg);
+    }
+
+    // Find Center of Camera in Starfield Camera picture 
+
+    Ring sgcenter(53.2, 293.6); // Center of camera in SG picture [px]
+    ZdAz sgcenterzdaz(0, 0);    // Center of camera in SG picture [deg] 
+                                // (0,0)_deg is at (53.2, 293.6)_px
+    ZdAz star(0, 0);            // Star on curtain in [deg]
+
+    if (fDisplay->IsEntryChecked(IDM_kStargCaosFilter))
+    {    
+        ZdAz pos;
+        if (fCosy)
+            pos = fCosy->GetPointingPos();
+        sgcenter = 
+	    fStargCaos->Run(img, kFALSE, kFALSE, pos, t, 30, 3.0); // [px]
+	const Float_t pixsize = atof(fPixSize->GetText()); // [arcsec/px]
+	sgcenterzdaz.Zd((sgcenter.GetY()-293.6) * pixsize /3600 );
+	sgcenterzdaz.Az((sgcenter.GetX()-53.2) * pixsize /3600 ); 
+#ifdef EXPERT
+	cout << "- LEDs imply offset of Zd=" 
+	     << sgcenter.GetX()-53.2 << "pix Az=" 
+	     << sgcenter.GetY()-293.6<< "pix" << endl; 	
+#endif
+	if (fDisplay->IsEntryChecked(IDM_kStargFindStar)) {
+	    star = FindStar(f, f2, sgcenter, t, 4.5, 30, 267/161.9); // [deg]
+#ifdef EXPERT
+	    cout << "- Star is found to be off Zd=" << star.Zd()*60 << "' Az=" 
+		 << star.Az()*60 << "'" << endl;	    
+#endif
+	    fSZdAz->SetCoordinates(star); // Misspointing found from Camera
+	}
+    }
+
+    // we calculate the offset given by the three ETH Leds visible to
+    // the guide camera
+    // This is an (inferior, obsolete) alternative to the StarCaosFilter
+    // Led offset;
+    // if (fDisplay->IsEntryChecked(IDM_kStargLEDFilter))	
+    //	fStargLeds->Run(img,offset);
+
+    // Position corresponding to the camera center (53.2, 293.6)
+    Ring skycenter(392, 318);
+    // MStarList spots;
+
+    // we obtain a list of stars in the FOV from the SAO catalog
     if (fDisplay->IsEntryChecked(IDM_kCatalog))
     {
-        MTime time(*tm);
-
-        XY xy = fCRaDec->GetCoordinates();
-        fRaDec->Set(xy.X()*360/24, xy.Y());
-
-        UpdatePosZoom();
-
-        /*
-        if (fDisplay->IsEntryChecked(IDM_kStarguider))
-        {
-            Leds leds;
-            f.Execute(leds);
-
-            cout << "Found: " << leds.GetEntries() << " stars." << endl;
-
-            CalcTrackingError(leds, stars);
-        }
-        */
-
-        // Always call SetMjd first!
-        fSao->SetPointing(time.GetMjd(), *fRaDec);
-        fCZdAz->SetCoordinates(fSao->GetZdAz());
-
-        MStarList stars;
-        fSao->CalcStars(stars);
-        fSao->DrawStars(stars, cimg);
-    }
-
+	MTime time(*tm);
+	  
+	XY xy = fCRaDec->GetCoordinates();
+	fRaDec->Set(xy.X()*360/24, xy.Y());
+
+	UpdatePosZoom();
+
+	// Always call SetMjd first!
+  	fSao->SetPointing(time.GetMjd(), *fRaDec);
+  	fCZdAz->SetCoordinates(fSao->GetZdAz());
+
+  	MStarList stars;
+    	fSao->SetBox(230); // Region of interest around center
+
+  	// very careful: If center of camera cannot be determined
+	// sgcenter jumps to (0,0)  
+	double sgcenteroffsetX = 0.0;
+	double sgcenteroffsetY = 0.0;
+	if ((double)sgcenter.GetX() > 0.0 && (double)sgcenter.GetY() > 0.0) {
+	    sgcenteroffsetX = sgcenter.GetX()-53.2;
+	    sgcenteroffsetY = sgcenter.GetY()-293.6;
+	}
+
+    	// we obtain stars in the effective star FOV and draw them.
+    	// coordinates are video frame coords.
+	// We determine the ideal starfield using camera sagging info
+	// from the LEDs
+
+	//cout << sgcenteroffsetX << " " << sgcenteroffsetY << endl;
+
+        fSao->CalcStars(stars, 530, 292, 
+    			-8+2-3+sgcenteroffsetX,  //x: star<spot =>subs
+			31.5-3+sgcenteroffsetY);   //y: star<spot =>subs?
+
+	fSao->DrawStars(stars, cimg);
+
+	// There are two corrections to the misspointing
+	// - Sagging of the camera as measured with the LEDs
+	// - Star not ideally centered on MAGIC Camera
+
+	// Next we evaluate the offset given by the LEDs. This we obtain
+	// in Zd/Az and add it to the tracking error.
+       
+	if (fDisplay->IsEntryChecked(IDM_kStarguider))
+	{
+  	    Leds spots;
+  	    f.SetBox(230);
+	    double bright;
+  	    f.ExecuteAndMark(spots, 530, 292, bright);
+ 	    MString txt;
+ 	    txt.Print("Sky Brightness: %.1f", bright);
+ 	    fSkyBright->SetText(txt);
+
+            ULong_t color;
+	    gClient->GetColorByName("Green", color);    
+	    if (bright> 60) gClient->GetColorByName("Yellow", color);    
+	    if (bright> 85) gClient->GetColorByName("Orange", color);    
+	    if (bright> 95) gClient->GetColorByName("Red", color);    
+            fSkyBright->SetBackgroundColor(color);
+	   
+  	    bool rc = CalcTrackingError(spots, stars, fD, t);
+
+	    if (rc && (bright <= 1.75* fLastBright) && (bright < 110)) {
+	      fStatus = MDriveCom::kMonitoring;
+	    } else {
+	      fStatus = MDriveCom::kError;
+	    }
+            
+	    if (fCosy)
+		fPos = fCosy->GetPointingPos();   
+	 
+  	    if (fDisplay->IsEntryChecked(IDM_kStargAnalysis)) 
+    		fStargHistograms->Fill(spots, stars, fD, 
+		   fSao->GetZdAz(), sgcenter, sgcenterzdaz,
+		   star, bright, fPos, t);
+
+	    fLastBright = bright;
+
+	} //kStarguider
+
+	f2.DrawCircle(skycenter, 2.0, 0x0a);
+	
+	f2.DrawCircle(skycenter, 7.4, 0x0a); //0.1deg
+
+	f2.DrawCircle(skycenter, 2.06*.5*74.0, 0x0a);
+	f2.DrawCircle(skycenter, 2.32*.5*74.0, 0x0a);
+        f2.DrawCircle(skycenter, 3.50*.5*74.0, 0x0a);
+        f2.DrawCircle(skycenter, 3.84*.5*74.0, 0x0a);
+
+    } //CalcStars
+
+    if (fCosy) {
+        MDriveCom &com = *fCosy->GetDriveCom();
+        com.SendStargReport(fStatus, fD);    // Report
+    }
+
+    // Draw Circles around center of Camera
     if (fDisplay->IsEntryChecked(IDM_kCaosFilter))
     {
         f2.DrawCircle(center, 0x0a);
-        f2.DrawCircle(center,   5.0, fDisplay->IsEntryChecked(IDM_kFindStar)?3:0xb0);
+        f2.DrawCircle(center,   5.0, 
+		      fDisplay->IsEntryChecked(IDM_kFindStar)?3:0xb0);
         f2.DrawCircle(center, 115.0, 0x0a);
         f2.DrawCircle(center, 230.0, 0x0a);
@@ -1158,14 +1502,19 @@
     }
 
-    if (fDisplay->IsEntryChecked(IDM_kCatalog))
-    {
-        fSao->PaintImg(cimg, 768, 576);
-
-        const float r = 60*60/fSao->GetPixSize();
-
-        f2.DrawCircle(0.5*r, 0x0a);
-        f2.DrawCircle(1.0*r, 0x0a);
-        f2.DrawCircle(1.5*r, 0x0a);
-    }
+    if (fDisplay->IsEntryChecked(IDM_kStargCaosFilter))
+    {
+        f2.DrawCircle(sgcenter, 0x0a);
+        f2.DrawCircle(sgcenter,   5.0, 
+		      fDisplay->IsEntryChecked(IDM_kFindStar)?3:0xb0);
+    }
+
+//      if (fDisplay->IsEntryChecked(IDM_kCatalog))
+//      {
+//          fSao->PaintImg(cimg, 768, 576);
+//          const float r = 60*60/fSao->GetPixSize();
+//  	f2.DrawCircle(0.5*r, 0x0a);
+//  	f2.DrawCircle(1.0*r, 0x0a);
+//  	f2.DrawCircle(1.5*r, 0x0a);
+//      }
 
     if (fDisplay->IsEntryChecked(IDM_kCaosFilter) ||
@@ -1183,15 +1532,12 @@
     {
         // FIXME: Necessary?
-        fSao->Now();
+	fSao->Now();
         AltAz aa = fSao->CalcAltAzFromPix(fDx, fDy)*kRad2Deg;
-
         if (aa.Az()<0)
             aa.Az(aa.Az()+360);
-
         txt.Print("%.1fd/%.1fd (%d, %d)", -aa.Alt(), aa.Az()-180, fDx, fDy);
     }
     else
         txt.Print("(%d, %d)", fDx, fDy);
-
     fPosZoom->SetText(txt);
 }
@@ -1221,2 +1567,13 @@
     return kTRUE;
 }
+
+void MStarguider::Print(TString &str, Double_t deg) const
+{
+    Char_t sgn;
+    UShort_t d, m, s;
+
+    MAstro::Deg2Dms(deg, sgn, d, m, s);
+
+    MString txt;
+    str += txt.Print("%c %03d %02d %03d ", sgn, d, m, s);
+}
Index: trunk/MagicSoft/Cosy/main/MStarguider.h
===================================================================
--- trunk/MagicSoft/Cosy/main/MStarguider.h	(revision 4856)
+++ trunk/MagicSoft/Cosy/main/MStarguider.h	(revision 4865)
@@ -1,8 +1,4 @@
 #ifndef COSY_MStarguider
 #define COSY_MStarguider
-
-#ifndef ROOT_TGFrame
-#include <TGFrame.h>
-#endif
 
 #include "PixClient.h"
@@ -23,4 +19,5 @@
 class TGLabel;
 class TGButton;
+class TString;
 
 class TTimer;
@@ -39,4 +36,7 @@
 class MCosy;
 class MCaos;
+// class MStargLeds;
+class MStargHistograms;
+class MGStarg;
 class FilterLed;
 class Ring;
@@ -54,4 +54,6 @@
     MGImage       *fImage;
     MGImage       *fZoomImage;
+
+    MGStarg       *fGStarg;
 
     MGPopupMenu   *fDisplay;
@@ -74,4 +76,6 @@
 
     MGCoordinates *fPZdAz;
+    MGCoordinates *fDZdAz;
+    MGCoordinates *fSZdAz;
 
     TGTextEntry   *fPixSize;
@@ -80,11 +84,18 @@
     TGLabel       *fFps;
     TGLabel       *fPosZoom;
+    TGLabel       *fSkyBright;
 
     TGButton      *fTPoint;
 
+    ZdAz          fPos;
+    ZdAz          fD;
+    
     StarCatalog   *fSao;
     MCosy         *fCosy;
     MCaos         *fCaos;
-
+    MCaos         *fStargCaos;
+    //    MStargLeds    *fStargLeds;
+    MStargHistograms *fStargHistograms;
+    
     RaDec *fRaDec;
 
@@ -100,11 +111,16 @@
     int  fWrtRate;
 
+    UInt_t fStatus;
+    Double_t fLastBright;
+
     void SetPixSize(const double pixsize);
     void Toggle(MGPopupMenu *p, UInt_t id);
     //void GetCoordinates();
-    void CalcTrackingError(Leds &, MStarList &);
+    bool CalcTrackingError(Leds &, MStarList &, ZdAz &, MTime &);
+    //void CalcTrackingError(Leds &, MStarList &);
     ZdAz TrackingError(TArrayF &alt, TArrayF &az, TArrayF &mag) const;
     bool Interpolate(const unsigned long n, byte *img) const;
-    void FindStar(FilterLed &f, FilterLed &f2, Ring &center, MTime &t);
+    ZdAz FindStar(FilterLed &f, FilterLed &f2, Ring &center, MTime &t, 
+		  Double_t cut, Double_t box, Double_t scalefactor);
 
     //void InitHists();
@@ -145,6 +161,10 @@
     void ProcessFrame(const unsigned long n, byte *img, struct timeval *tm);
 
+    void Print(TString &str, Double_t deg) const;
+
     ClassDef(MStarguider, 0)
 };
 
 #endif
+
+
Index: trunk/MagicSoft/Cosy/main/MainLinkDef.h
===================================================================
--- trunk/MagicSoft/Cosy/main/MainLinkDef.h	(revision 4856)
+++ trunk/MagicSoft/Cosy/main/MainLinkDef.h	(revision 4865)
@@ -7,4 +7,5 @@
 #pragma link C++ class MCosy;
 #pragma link C++ class MStarguider+;
+#pragma link C++ class MStargHistograms+;
 
 #pragma link C++ class MBending;
Index: trunk/MagicSoft/Cosy/main/Makefile
===================================================================
--- trunk/MagicSoft/Cosy/main/Makefile	(revision 4856)
+++ trunk/MagicSoft/Cosy/main/Makefile	(revision 4865)
@@ -34,4 +34,5 @@
 SRCFILES = MBending.cc \
            MStarguider.cc \
+           MStargHistograms.cc \
            MPointing.cc \
            MTracking.cc \
Index: trunk/MagicSoft/Cosy/prepos_magic.txt
===================================================================
--- trunk/MagicSoft/Cosy/prepos_magic.txt	(revision 4856)
+++ trunk/MagicSoft/Cosy/prepos_magic.txt	(revision 4865)
@@ -1,5 +1,5 @@
-100.045      -8.869999999 Camera Access
+98.30000000  -10.0133333  Camera Access
 61.23817667   0           Celestial Pole
-94.125       -9.23        Park
+93.91666667  -10.0133333  Park
 95.58527778 151.018055556 Roque Lamp
 4             0           Zenith
Index: trunk/MagicSoft/Cosy/starg.cc
===================================================================
--- trunk/MagicSoft/Cosy/starg.cc	(revision 4856)
+++ trunk/MagicSoft/Cosy/starg.cc	(revision 4865)
@@ -10,5 +10,5 @@
 //VoidFuncPtr_t initfuncs[] = { InitGui, 0 };
 
-TROOT root("GUI", "GUI test environement"); //, initfuncs);
+TROOT root("GUI", "GUI test environment"); //, initfuncs);
 
 int main(int argc, char **argv)
Index: trunk/MagicSoft/Cosy/stargleds.txt
===================================================================
--- trunk/MagicSoft/Cosy/stargleds.txt	(revision 4865)
+++ trunk/MagicSoft/Cosy/stargleds.txt	(revision 4865)
@@ -0,0 +1,3 @@
+125 148.9 0 0
+217.37 284.03 0 0
+141.9 429.3 0 0
Index: trunk/MagicSoft/Cosy/stars.txt
===================================================================
--- trunk/MagicSoft/Cosy/stars.txt	(revision 4856)
+++ trunk/MagicSoft/Cosy/stars.txt	(revision 4865)
@@ -4,4 +4,5 @@
 12 56 11  -05 47 22  3C279 (17.75)
 12 54 02   55 57 35  Alioth (1.77)
+19 30 43   27 57 34  Albiero (3.08)
 00 08 23   29 05 26  Alpha Andromedae (2.1)
 02 07 10   23 27 45  Alpha Aries (2.00)
@@ -33,5 +34,5 @@
 05 55 10   07 24 25  Betelgeuse (0.5)
 22 02 43   42 16 40  BL-Lac (14.5)
-05 15 41   45 59 53  Capella (0.08)
+05 16 41   45 59 53  Capella (0.08)
 00 09 11   59 08 59  Caph (2.27)
 12 59 49   27 58 50  Coma Cluster (10.70)
@@ -69,4 +70,5 @@
 06 33 54   17 46 48  Geminga
 19 30 42   51 43 47  Iota2 Cygni (3.8)
+22 07 00   25 20 42  Iota Pegasi (3.49)
 19 17 06   53 22 07  Kappa Cygni (3.9)
 18 24 10  -34 23 05  Kaus Australis (1.85)
@@ -88,6 +90,8 @@
 15 02 22  -41 53 48  SN-1006
 13 25 13  -11 09 41  Spica (0.98)
+19 46 15   10 36 48  Tarazed (2.72)
 20 11 18  -00 49 17  Theta Aquilae (3.2)
 17 37 19  -42 59 52  Theta Scorpionis (1.87)
+15 44 16    6 25 32  Unukalhay (2.65)
 18 36 56   38 47 01  Vega (0.03)
 19 05 25   13 51 48  Zeta Aquilae (3.0)
Index: trunk/MagicSoft/Cosy/tcpip/MCeCoCom.cc
===================================================================
--- trunk/MagicSoft/Cosy/tcpip/MCeCoCom.cc	(revision 4856)
+++ trunk/MagicSoft/Cosy/tcpip/MCeCoCom.cc	(revision 4865)
@@ -90,5 +90,5 @@
 }
 
-bool MCeCoCom::Send(const char *cmd, const char *str)
+bool MCeCoCom::Send(const char *cmd, const char *str, bool force=kFALSE)
 {
     MTime t;
@@ -113,5 +113,5 @@
              str);
 
-    const bool rc = MTcpIpIO::Send(msg);
+    const bool rc = MTcpIpIO::Send(msg, force);
     fComStat = rc ? kNoCmdReceived : kComProblem;
     return rc;
Index: trunk/MagicSoft/Cosy/tcpip/MCeCoCom.h
===================================================================
--- trunk/MagicSoft/Cosy/tcpip/MCeCoCom.h	(revision 4856)
+++ trunk/MagicSoft/Cosy/tcpip/MCeCoCom.h	(revision 4865)
@@ -49,5 +49,5 @@
     }
 
-    bool Send(const char *cmd, const char *str);
+    bool Send(const char *cmd, const char *str, bool force);
     void SetStatus(Byte_t s) { fStatus=s; }
 
Index: trunk/MagicSoft/Cosy/tcpip/MDriveCom.cc
===================================================================
--- trunk/MagicSoft/Cosy/tcpip/MDriveCom.cc	(revision 4856)
+++ trunk/MagicSoft/Cosy/tcpip/MDriveCom.cc	(revision 4865)
@@ -182,9 +182,42 @@
     str += txt.Print("%08.3f", er.Az());
 
-    return Send("DRIVE-REPORT", str);
+    return Send("DRIVE-REPORT", str, kFALSE);
 }
 
 bool MDriveCom::SendStatus(const char *stat)
 {
-    return Send("DRIVE-STATUS", stat);
-}
+    return Send("DRIVE-STATUS", stat, kFALSE);
+}
+
+bool MDriveCom::SendStargReport(UInt_t stat, ZdAz miss)
+{
+   
+    // miss [rad]
+    const MTime t(-1);
+
+    miss *= kRad2Deg;
+
+    // Set status flag
+    if (stat&kError)
+        SetStatus(0);
+    if (stat&kStopped)
+        SetStatus(1);
+    if (stat&kStandby)
+        SetStatus(2);
+    if (stat&kMonitoring)
+        SetStatus(4);
+    
+    MString txt;
+
+    TString str;
+    str += txt.Print("%05.3f ", miss.Zd());
+    str += txt.Print("%05.3f", miss.Az());
+    
+    return Send("STARG-REPORT", str, kTRUE);
+
+}
+
+
+
+
+
Index: trunk/MagicSoft/Cosy/tcpip/MDriveCom.h
===================================================================
--- trunk/MagicSoft/Cosy/tcpip/MDriveCom.h	(revision 4856)
+++ trunk/MagicSoft/Cosy/tcpip/MDriveCom.h	(revision 4865)
@@ -31,12 +31,26 @@
         kTracking = 0x04,
         kStopping = 0x08,
-        kStopped  = 0x10
+        kStopped  = 0x10,
+	kStandby  = 0x20,
+	kMonitoring = 0x40
     };
-
+  
     MDriveCom(MsgQueue *q, MLog &out=gLog) : MCeCoCom("DRIVE-REPORT", out), fQueue(q) {}
 
     bool SendReport(UInt_t stat, RaDec rd, ZdAz so, ZdAz is, ZdAz er);
+    bool SendStargReport(UInt_t stat, ZdAz miss);
     bool SendStatus(const char *stat);
 };
 
 #endif
+
+
+
+
+
+
+
+
+
+
+
Index: trunk/MagicSoft/Cosy/tcpip/MTcpIpIO.cc
===================================================================
--- trunk/MagicSoft/Cosy/tcpip/MTcpIpIO.cc	(revision 4856)
+++ trunk/MagicSoft/Cosy/tcpip/MTcpIpIO.cc	(revision 4865)
@@ -67,10 +67,12 @@
 }
 
-bool MTcpIpIO::Send(const char *msg)
-{
+bool MTcpIpIO::Send(const char *msg, bool force=kFALSE)
+{
+
     const MTime t(-1);
 
-    if ((double)t-(double)fTime<0.001*fSendInterval)
-        return true;
+    if ((double)t-(double)fTime<0.001*fSendInterval && !force) 
+          return true;
+    
 
     if (lout.Lock("MTcpIpIO::Send"))
@@ -103,4 +105,5 @@
     cout << "Tx: " << msg << flush;
 #endif
+
     return true;
 }
Index: trunk/MagicSoft/Cosy/tcpip/MTcpIpIO.h
===================================================================
--- trunk/MagicSoft/Cosy/tcpip/MTcpIpIO.h	(revision 4856)
+++ trunk/MagicSoft/Cosy/tcpip/MTcpIpIO.h	(revision 4865)
@@ -32,5 +32,5 @@
     ~MTcpIpIO();
 
-    virtual bool Send(const char *msg);
+    virtual bool Send(const char *msg, bool force);
     virtual bool InterpreteStr(TString str);
 
Index: trunk/MagicSoft/Cosy/videodev/FilterLed.cc
===================================================================
--- trunk/MagicSoft/Cosy/videodev/FilterLed.cc	(revision 4856)
+++ trunk/MagicSoft/Cosy/videodev/FilterLed.cc	(revision 4865)
@@ -227,5 +227,5 @@
 
     Execute(leds, xc, yc);
-
+ 
     // Mark Stars in image
     for (int i=first; i<leds.GetEntriesFast(); i++)
@@ -233,4 +233,16 @@
 }
 
+
+void FilterLed::ExecuteAndMark(Leds &leds, int xc, int yc, double &bright) const
+{
+    const Int_t first = leds.GetEntriesFast();
+
+    Execute(leds, xc, yc, bright);
+ 
+    // Mark Stars in image
+    for (int i=first; i<leds.GetEntriesFast(); i++)
+        MarkPoint(leds(i));
+}
+
 void FilterLed::Execute(int xc, int yc) const
 {
@@ -240,4 +252,11 @@
 
 void FilterLed::Execute(Leds &leds, int xc, int yc) const
+{
+    double bright;
+    Execute(leds, xc, yc, bright);
+}
+
+
+void FilterLed::Execute(Leds &leds, int xc, int yc, double &bright) const
 {
     int x0 = xc-fBox;
@@ -269,4 +288,7 @@
     sq  /= wx*hy;
 
+    bright=sum;
+
+    
     // 254 because b<=max and not b<max
     const double sdev = sqrt(sq-sum*sum);
@@ -345,4 +367,6 @@
 
     RemoveTwinsInterpol(leds, first, 5);
+    
+   
 }
 
Index: trunk/MagicSoft/Cosy/videodev/FilterLed.h
===================================================================
--- trunk/MagicSoft/Cosy/videodev/FilterLed.h	(revision 4856)
+++ trunk/MagicSoft/Cosy/videodev/FilterLed.h	(revision 4865)
@@ -22,5 +22,6 @@
     void GetMinMax(const int offset, byte *min, byte *max) const;
     int  GetMeanPosition(const int x, const int y, const int box) const;
-    int  GetMeanPosition(const int x, const int y, const int box, float &mx, float &my, unsigned int &sum) const;
+    int  GetMeanPosition(const int x, const int y, const int box, 
+			 float &mx, float &my, unsigned int &sum) const;
     void RemoveTwinsInterpol(Leds &leds, Int_t first, Double_t radius) const;
     void DrawBox(const int x1, const int y1,
@@ -30,21 +31,21 @@
 public:
     FilterLed(byte *img, int w, int h, double cut=2.5)
-        : fImg(img), fW(w), fH(h), fBox(w>h?w:h), fCut(cut)
+      : fImg(img), fW(w), fH(h), fBox(w>h?w:h), fCut(cut)
     {
     }
 
     FilterLed(byte *img, int w, int h, int box, double cut=2.5)
-        : fImg(img), fW(w), fH(h), fBox(box), fCut(cut)
+      : fImg(img), fW(w), fH(h), fBox(box), fCut(cut)
     {
     }
 
     void SetBox(int box)   { fBox = box; }
-    void SetCut(float cut) { fCut = cut; }
-
+    void SetCut(float cut) { fCut = cut; } 
     void FindStar(Leds &leds, int xc, int yc) const;
-
+    void Execute(Leds &leds, int xc, int yc, double &bright) const;
     void Execute(Leds &leds, int xc, int yc) const;
     void Execute(Leds &leds) const { Execute(leds, fW/2, fH/2); }
     void ExecuteAndMark(Leds &leds, int xc, int yc) const;
+    void ExecuteAndMark(Leds &leds, int xc, int yc, double &bright) const;
     void ExecuteAndMark(Leds &leds) const { ExecuteAndMark(leds, fW/2, fH/2); }
     void Execute(int xc, int yc) const;
