Index: trunk/MagicSoft/Mars/mastro/MAstroCamera.cc
===================================================================
--- trunk/MagicSoft/Mars/mastro/MAstroCamera.cc	(revision 3927)
+++ trunk/MagicSoft/Mars/mastro/MAstroCamera.cc	(revision 3957)
@@ -70,9 +70,12 @@
 #include "MAstroCamera.h"
 
-#include <KeySymbols.h>       // kKey_*
-
-#include <TH2.h>              // TH2D
-#include <TMarker.h>          // TMarker
-#include <TVirtualPad.h>      // gPad
+#include <errno.h>        // strerror
+#include <fstream>        // ifstream
+
+#include <KeySymbols.h>   // kKey_*
+
+#include <TH2.h>          // TH2D
+#include <TMarker.h>      // TMarker
+#include <TVirtualPad.h>  // gPad
 
 #include "MLog.h"
@@ -144,4 +147,75 @@
         memcpy((*fMirrors)[i], arr[i], sizeof(MGeomMirror));
 
+}
+
+// --------------------------------------------------------------------------
+//
+// Read the mirror geometry from a MC .def file. The following
+// structure is expected:
+//
+// #*  TYPE=1  (MAGIC)
+// #*      i  f   sx   sy   x   y   z   thetan  phin 
+// #* 
+// #*       i : number of the mirror
+// #*       f : focal distance of that mirror
+// #*      sx : curvilinear coordinate of mirror's center in X[cm]
+// #*      sy : curvilinear coordinate of mirror's center in X[cm]
+// #*       x : x coordinate of the center of the mirror [cm]
+// #*       y : y coordinate of the center of the mirror [cm]
+// #*       z : z coordinate of the center of the mirror [cm]
+// #*  thetan : polar theta angle of the direction where the mirror points to
+// #*    phin : polar phi angle of the direction where the mirror points to
+// #*      xn : xn coordinate of the normal vector in the center (normalized)
+// #*      yn : yn coordinate of the normal vector in the center (normalized)
+// #*      zn : zn coordinate of the normal vector in the center (normalized)
+// #
+// define_mirrors
+//   1 1700.9200   25.0002   75.0061   25.0000   75.0000    0.9207 0.02328894 1.24904577 -0.00736394 -0.02209183 0.99972882
+//   2 ...
+//
+void MAstroCamera::SetMirrors(const char *fname)
+{
+    ifstream fin(fname);
+    if (!fin)
+    {
+        gLog << err << "Cannot open file " << fname << ": ";
+        gLog << strerror(errno) << endl;
+        return;
+    }
+
+    TString line;
+    while (1)
+    {
+        line.ReadLine(fin);
+        if (!fin)
+            return;
+
+        line = line.Strip(TString::kBoth);
+
+        if (line.BeginsWith("n_mirrors"))
+        {
+            Int_t n;
+            sscanf(line.Data(), "%*s %d", &n);
+
+            if (!fMirrors)
+                fMirrors = new TClonesArray(MGeomMirror::Class(), n);
+
+            fMirrors->ExpandCreate(n);
+            continue;
+        }
+
+
+        Int_t id;
+        Float_t f, sx, sy, x, y, z, thetan, phin, xn, yn, zn;
+
+        const Int_t n = sscanf(line.Data(), "%d %f %f %f %f %f %f %f %f %f %f %f",
+                               &id, &f, &sx, &sy, &x, &y, &z, &thetan,
+                               &phin, &xn, &yn, &zn);
+        if (n!=12)
+            continue;
+
+        new ((*fMirrors)[id-1]) MGeomMirror;
+        ((MGeomMirror*)(*fMirrors)[id-1])->SetMirrorContent(id, f, sx, sy, x, y, z, thetan, phin, xn, yn, zn);
+    }
 }
 
@@ -287,4 +361,6 @@
     {
         const Double_t mag = radec->Magnitude();
+        if (mag>GetLimMag())
+            continue;
 
         TVector3 star(*radec);
@@ -334,4 +410,5 @@
             const TVector3 spot = fMirror0->GetReflection(star, fGeom->GetCameraDist())*1000;
             DrawStar(spot(0), spot(1), *radec, !hasmean, Form("x=%.1fmm y=%.1fmm", mean(0), mean(1)));
+            //cout << TMath::Hypot(spot(0), spot(1)) << " " << TMath::Hypot(mean(0)-spot(0), mean(1)-spot(1)) << endl;
         }
     }
@@ -362,22 +439,9 @@
     }
 
-    // Get camera
-    MHCamera *camera=(MHCamera*)FindObjectInPad("MHCamera", gPad);
-    if (camera)
-    {
-        if (!camera->GetGeometry() || camera->GetGeometry()->IsA()!=fGeom->IsA())
-            camera->SetGeometry(*fGeom);
-    }
-    else
-    {
-        camera = new MHCamera(*fGeom);
-        camera->SetName("MHCamera");
-        camera->SetStats(0);
-        camera->SetInvDeepBlueSeaPalette();
-        camera->SetBit(kCanDelete);
-        camera->Draw();
-    }
-
-    const TRotation rot(GetGrid(kTRUE));
+    const MAstroSky2Local s2l(*fTime, *fObservatory);
+    const TRotation trans(AlignCoordinates(rot*fRaDec));
+
+    // Return the correct rotation matrix
+    const TRotation rot = trans*s2l;
 
     MVector3 *radec;
@@ -397,4 +461,8 @@
 	list->Add(starpos);
     }
+    // For MAGIC the distance of the mean of the light distribution
+    // to the Mirror0 reflection of the star (Abberation) can be
+    // expressed as:  dr = (0.0713 +/- 0.0002) * r = r/14.03
+    // with r = hypot(mean(0), mean(1))
 }
 */
@@ -402,8 +470,71 @@
 // ------------------------------------------------------------------------
 //
+// Uses fRaDec as a reference point.
+//
+// Return dZd and dAz corresponding to the distance from x,y to fRaDec
+//
+// Before calling this function you should correct for abberation. In
+// case of MAGIC you can do this by:
+//    x /= 1.0713;
+//    y /= 1.0713;
+//
+// x [mm]: x coordinate in the camera plane (assuming a perfect mirror)
+// y [mm]: y coordinate in the camera plane (assuming a perfect mirror)
+//
+// dzd [deg]: Delta Zd
+// daz [deg]: Delta Az
+//
+void MAstroCamera::GetDiffZdAz(Double_t x, Double_t y, Double_t &dzd, Double_t &daz)
+{
+    // Reflect the corrected pixel on a perfect mirror
+    TVector3 v(x, y, fGeom->GetCameraDist()*1000);
+    TVector3 spot = fMirror0->GetReflection(v);
+    
+    // Derotate into the local coordinate system
+    const MAstroSky2Local rot(*fTime, *fObservatory);
+    const TRotation align(AlignCoordinates(rot*fRaDec).Inverse());
+    spot *= align;
+
+    cout << "Zd="<<spot.Theta()*TMath::RadToDeg() << " ";
+    cout << "Az="<<spot.Phi()  *TMath::RadToDeg()+360 << endl;
+
+    // Derotatet the center of the camera
+    TVector3 c(0, 0, 1);
+    c *= align;
+
+    dzd = (spot.Theta()-c.Theta())*TMath::RadToDeg();
+    daz = (spot.Phi()  -c.Phi())  *TMath::RadToDeg();
+
+    if (daz> 180) daz -= 360;
+    if (daz<-180) daz += 360;
+}
+
+// ------------------------------------------------------------------------
+//
 // Execute a gui event on the camera
 //
 void MAstroCamera::ExecuteEvent(Int_t event, Int_t mp1, Int_t mp2)
 {
+    // if (mp1>0 && mp2>0)
+    // {
+    //     // Calculate World coordinates from pixel
+    //     Double_t x = gPad->AbsPixeltoX(mp1);
+    //     Double_t y = gPad->AbsPixeltoY(mp2);
+    //
+    //     // Correct for abberation
+    //     x /= 1.0713;
+    //     y /= 1.0713;
+    //
+    //     Double_t dzd, daz;
+    //     GetDiffZdAz(x, y, dzd, daz);
+    //
+    //     cout << "dZd="<< dzd << " " << "dAz="<< daz << endl;
+    // }
+    //
+    // For MAGIC the distance of the mean of the light distribution
+    // to the Mirror0 reflection of the star (Abberation) can be
+    // expressed as:  dr = 0.0713*r = r/14.03
+    //                   +-0.0002
+
     if (event==kKeyPress && fTime)
         switch (mp2)
Index: trunk/MagicSoft/Mars/mastro/MAstroCamera.h
===================================================================
--- trunk/MagicSoft/Mars/mastro/MAstroCamera.h	(revision 3927)
+++ trunk/MagicSoft/Mars/mastro/MAstroCamera.h	(revision 3957)
@@ -32,5 +32,8 @@
 
     void SetMirrors(TClonesArray &arr);
+    void SetMirrors(const char *fname);
     void SetGeom(const MGeomCam &cam);
+
+    void GetDiffZdAz(Double_t x, Double_t y, Double_t &dzd, Double_t &daz);
 
     ClassDef(MAstroCamera, 1) // Display class to display stars on the camera
Index: trunk/MagicSoft/Mars/mastro/MObservatory.h
===================================================================
--- trunk/MagicSoft/Mars/mastro/MObservatory.h	(revision 3927)
+++ trunk/MagicSoft/Mars/mastro/MObservatory.h	(revision 3957)
@@ -36,4 +36,15 @@
     MObservatory(LocationName_t key, const char *name=NULL, const char *title=NULL);
 
+    void Copy(TObject &obj) const
+    {
+        MObservatory &obs = (MObservatory&)obj;
+        obs.fObservatoryName = fObservatoryName;
+        obs.fLongitude = fLongitude;
+        obs.fLatitude = fLatitude;
+        obs.fSinLatitude = fSinLatitude;
+        obs.fCosLatitude = fCosLatitude;
+        obs.fHeight = fHeight;
+    }
+
     void SetLocation(LocationName_t name);
 
