Index: trunk/MagicSoft/Simulation/Detector/ReflectorII/writemagicdef/writemagicdef.c
===================================================================
--- trunk/MagicSoft/Simulation/Detector/ReflectorII/writemagicdef/writemagicdef.c	(revision 5439)
+++ trunk/MagicSoft/Simulation/Detector/ReflectorII/writemagicdef/writemagicdef.c	(revision 5440)
@@ -11,5 +11,5 @@
   double x_min, y_min, x_max, y_max, a;
   double norm;
-  double step; /* (cm) separation of mirror centers = length of mirror side */
+  double step; // (cm) separation of mirror centers = length of mirror side
 
   long x_index, y_index;
@@ -45,4 +45,5 @@
     for (y = y_min ; y < y_max+1; y += step)    
       {
+	// Skip non existent mirrors:
 
 	if ( (fabs(x)+fabs(y)) > diameter*0.72)
@@ -55,8 +56,7 @@
 	  continue;
 
+
 	x_index = x > 0? (int)(x+50)/100 : (int)(x-50)/100 ;
 	y_index = y > 0? (int)(y+50)/100 : (int)(y-50)/100 ;
-
-//	printf("%d %d  %.0f %.0f\n", x_index, y_index, x, y);
 
 	i_mirror++;
@@ -73,14 +73,7 @@
 	  phin += 2*PI;
 
-//
-// OLD: (before chessboarding) Valid only if mirror is ON the parabola
-//	xn = - x / sqrt(x*x+y*y+4*ct_f*ct_f);
-//	yn = - y / sqrt(x*x+y*y+4*ct_f*ct_f);
-//	zn = 2*ct_f / sqrt(x*x+y*y+4*ct_f*ct_f);
-//	printf("%.6f %.6f %.6f\n", xn, yn, zn);
-
-//
-//	Shift z for chessboarding:
-//
+	//
+	// Shift z for chessboarding:
+	//
 	if (!((x_index+y_index)%2 != 0  || 
 	      (x_index+y_index) ==  12  || 
@@ -88,14 +81,25 @@
 	      (x_index-y_index) ==  12  || 
 	      (x_index-y_index) == -12 ) )
-	  z += 8.;
+	  z += 8.; // cm
 
-//
-// NEW: Valid also (correct focusing) if we shift mirrors from the parabola,
-//      since we use explicitely the z coordinate:
-//
+	//
+	// Calculate orientation of vector normal to the mirror:
+	//
+	// Add the versor pointing from the mirror element center (x,y,z)
+	// to the camera center (0,0,ct_f), and versor (0,0,1). The 
+        // direction of the resulting vector is that of the bisector of 
+	// the two versors. If the normal of the mirror element is 
+	// oriented along that direction, light rays paralel to the
+	// telescope axis and impinging on the center of the mirror
+	// element will be reflected to the center of the camera.
+	// 
+	//
 	xn = - x / sqrt(x*x+y*y+(ct_f-z)*(ct_f-z));
 	yn = - y / sqrt(x*x+y*y+(ct_f-z)*(ct_f-z));
 	zn = 1 + (ct_f - z) / sqrt(x*x+y*y+(ct_f-z)*(ct_f-z));
 
+	//
+	// Normalize the vector to obtain the versor:
+	//
 	norm = sqrt(xn*xn+yn*yn+zn*zn);
 	xn /= norm;
@@ -103,14 +107,15 @@
 	zn /= norm;
 
-//	printf("%.6f %.6f %.6f\n\n", xn, yn, zn);
-
-
+	//
+	// Get angle between the normal to the mirror and the 
+	// telescope axis:
+	//
 	thetan = acos(zn);
 
 	a = 2*ct_f;
 
-//
-// Focal distance of the mirror:
-//
+	//
+	// Focal distance of the mirror:
+	//
 	f= 0.5 * (a*sqrt((1.+(x*x+y*y)/(a*a))*
 			 (1.+(x*x+y*y)/(a*a))*
@@ -121,5 +126,5 @@
 	printf("%3d %9.4f %9.4f %9.4f %9.4f %9.4f %9.4f %9.8f %9.8f %9.8f %9.8f %9.8f\n", i_mirror, f, sx, sy, x, y, z, thetan, phin, xn, yn, zn);
       }
-
+  
   return 0;
 }
