Index: trunk/MagicSoft/Mars/mgeom/MGeomCam.cc
===================================================================
--- trunk/MagicSoft/Mars/mgeom/MGeomCam.cc	(revision 3547)
+++ trunk/MagicSoft/Mars/mgeom/MGeomCam.cc	(revision 3666)
@@ -53,4 +53,5 @@
 
 #include <TClass.h>     // IsA()->New()
+#include <TVector2.h>   // TVector2
 
 #include "MLog.h"
@@ -92,5 +93,5 @@
 
     for (UInt_t i=0; i<npix; i++)
-      fPixels[i] = new MGeomPix;
+        fPixels[i] = new MGeomPix;
 
     SetReadyToSave();
@@ -195,44 +196,42 @@
 void MGeomCam::CalcMaxRadius()
 {
-
-  fMaxRadius.Set(fNumAreas+1);
-  fMinRadius.Set(fNumAreas+1);  
-
-  for (Int_t i=0; i<fNumAreas+1; i++)
-    {
-      fMaxRadius[i] = 0.;
-      fMinRadius[i] = FLT_MAX;
-    }
-  
-  for (UInt_t i=0; i<fNumPixels; i++)
-    {
-
-      const MGeomPix &pix = (*this)[i];
-
-      const UInt_t  s = pix.GetAidx();      
-      const Float_t x = pix.GetX();
-      const Float_t y = pix.GetY();
-      const Float_t d = pix.GetD();
-
-      const Float_t r = TMath::Hypot(x, y);
-
-      const Float_t maxr = r + d;
-      const Float_t minr = r>d ? r-d : 0;
-      
-      if (maxr>fMaxRadius[s+1])
-        fMaxRadius[s+1] = maxr;
-
-      if (minr<fMinRadius[s+1])
-        fMinRadius[s+1] = minr;
-
-      if (minr<fMinRadius[0])
-        fMinRadius[0] = minr;
-
-      if (maxr>fMaxRadius[0])
-        fMaxRadius[0] = maxr;
-
-    }
-}
-
+    fMaxRadius.Set(fNumAreas+1);
+    fMinRadius.Set(fNumAreas+1);
+
+    for (Int_t i=0; i<fNumAreas+1; i++)
+    {
+        fMaxRadius[i] = 0.;
+        fMinRadius[i] = FLT_MAX;
+    }
+
+    for (UInt_t i=0; i<fNumPixels; i++)
+    {
+        const MGeomPix &pix = (*this)[i];
+
+        const UInt_t  s = pix.GetAidx();
+        const Float_t x = pix.GetX();
+        const Float_t y = pix.GetY();
+        const Float_t d = pix.GetD();
+
+        const Float_t r = TMath::Hypot(x, y);
+
+        const Float_t maxr = r + d;
+        const Float_t minr = r>d ? r-d : 0;
+
+        if (maxr>fMaxRadius[s+1])
+            fMaxRadius[s+1] = maxr;
+
+        if (minr<fMinRadius[s+1])
+            fMinRadius[s+1] = minr;
+
+        if (minr<fMinRadius[0])
+            fMinRadius[0] = minr;
+
+        if (maxr>fMaxRadius[0])
+            fMaxRadius[0] = maxr;
+    }
+}
+
+// --------------------------------------------------------------------------
 //
 // Have to call the radii of the subcameras starting to count from 1
@@ -240,8 +239,8 @@
 Float_t MGeomCam::GetMaxRadius(const Int_t i) const
 {
-  if (i==-1) return fMaxRadius[0];
-  return i>fNumAreas ? -1 : fMaxRadius[i+1];
-}
-
+    return i<-1 || i>fNumAreas ? -1 : fMaxRadius[i+1];
+}
+
+// --------------------------------------------------------------------------
 //
 // Have to call the radii of the subcameras starting to count from 1
@@ -249,6 +248,5 @@
 Float_t MGeomCam::GetMinRadius(const Int_t i) const
 {
-  if (i==-1) return fMinRadius[0];
-  return i>fNumAreas ? -1 : fMinRadius[i+1];
+    return i<-1 || i>fNumAreas ? -1 : fMinRadius[i+1];
 }
 
@@ -302,4 +300,30 @@
     return (TObject*)IsA()->New();
 }
+
+// --------------------------------------------------------------------------
+//
+//  Return the pixel index corresponding to the coordinates given in x, y.
+//  The coordinates are given in pixel units (millimeters)
+//  If no pixel exists return -1;
+//
+Int_t MGeomCam::GetPixelIdxXY(Float_t x, Float_t y) const
+{
+    for (unsigned int i=0; i<fNumPixels; i++)
+        if ((*this)[i].IsInside(x, y))
+            return i;
+
+    return -1;
+}
+
+Int_t MGeomCam::GetPixelIdx(const TVector2 &v) const
+{
+    return GetPixelIdxXY(v.X(), v.Y());
+}
+
+Int_t MGeomCam::GetPixelIdxDeg(const TVector2 &v) const
+{
+    return GetPixelIdxXYdeg(v.X(), v.Y());
+}
+
 /*
 void MGeomCam::Streamer(TBuffer &R__b)
Index: trunk/MagicSoft/Mars/mgeom/MGeomCam.h
===================================================================
--- trunk/MagicSoft/Mars/mgeom/MGeomCam.h	(revision 3547)
+++ trunk/MagicSoft/Mars/mgeom/MGeomCam.h	(revision 3666)
@@ -12,4 +12,5 @@
 #endif
 
+class TVector2;
 class MGeomPix;
 
@@ -73,5 +74,13 @@
 
     MGeomPix &operator[](Int_t i);
-    MGeomPix &operator[](Int_t i)          const;
+    MGeomPix &operator[](Int_t i) const;
+
+    Int_t GetPixelIdx(const TVector2 &v) const;
+    Int_t GetPixelIdxDeg(const TVector2 &v) const;
+    Int_t GetPixelIdxXY(Float_t x, Float_t y) const;
+    Int_t GetPixelIdxXYdeg(Float_t x, Float_t y) const
+    {
+        return GetPixelIdxXY(x/fConvMm2Deg, y/fConvMm2Deg);
+    }
 
     virtual void Print(Option_t *opt=NULL)   const;
Index: trunk/MagicSoft/Mars/mgeom/MGeomPix.cc
===================================================================
--- trunk/MagicSoft/Mars/mgeom/MGeomPix.cc	(revision 3547)
+++ trunk/MagicSoft/Mars/mgeom/MGeomPix.cc	(revision 3666)
@@ -133,2 +133,39 @@
     *fLog << "d= " << fD << "mm  A= " << fA << "mm²" << endl;
 }
+
+// ------------------------------------------------------------------------
+//
+// compute the distance of a point (px,py) to the Hexagon center in
+// MGeomPix coordinates. Return kTRUE if inside.
+//
+Bool_t MGeomPix::IsInside(Float_t px, Float_t py) const
+{
+    //
+    //  compute the distance of the Point to the center of the Hexagon
+    //
+    const Double_t dx = px-fX;
+
+    //
+    // Now check if point is outside of hexagon; just check x coordinate
+    // in three coordinate systems: the default one, in which two sides of
+    // the hexagon are paralel to the y axis (see camera displays) and two 
+    // more, rotated with respect to that one by +- 60 degrees.
+    //
+    if (TMath::Abs(dx)*2>fD)
+        return kFALSE;
+
+    const Double_t dy = py-fY;
+
+    const static Double_t cos60 = TMath::Cos(60/kRad2Deg);
+    const static Double_t sin60 = TMath::Sin(60/kRad2Deg);
+
+    const Double_t dx2 = dx*cos60 + dy*sin60;
+    if  (TMath::Abs(dx2)*2>fD)
+        return kFALSE;
+
+    const Double_t dx3 = dx*cos60 - dy*sin60;
+    if (TMath::Abs(dx3)*2>fD)
+        return kFALSE;
+
+    return kTRUE;
+}
Index: trunk/MagicSoft/Mars/mgeom/MGeomPix.h
===================================================================
--- trunk/MagicSoft/Mars/mgeom/MGeomPix.h	(revision 3547)
+++ trunk/MagicSoft/Mars/mgeom/MGeomPix.h	(revision 3666)
@@ -56,4 +56,6 @@
     Bool_t IsInOuterRing() const     { return TestBit(kIsInOuterRing); }
 
+    Bool_t IsInside(Float_t px, Float_t py) const;
+
     /*
      //
