Index: trunk/MagicSoft/Simulation/Detector/ReflectorII/Makefile
===================================================================
--- trunk/MagicSoft/Simulation/Detector/ReflectorII/Makefile	(revision 1523)
+++ trunk/MagicSoft/Simulation/Detector/ReflectorII/Makefile	(revision 1535)
@@ -19,7 +19,7 @@
 #
 # $RCSfile: Makefile,v $
-# $Revision: 1.3 $
+# $Revision: 1.4 $
 # $Author: bigongia $ 
-# $Date: 2002-07-24 14:35:45 $
+# $Date: 2002-10-09 18:15:27 $
 #
 ##################################################################
@@ -55,4 +55,6 @@
 #CXXFLAGS  = -D__${SYSTEM}__ ${INCLUDES} ${OPTIM} ${DEBUG}
 CXXFLAGS  = -D__${SYSTEM}__ ${INCLUDES} ${OPTIM}
+#CXXFLAGS  = -D__${SYSTEM}__ ${INCLUDES} ${DEBUG}
+
 CFLAGS    = ${CXXFLAGS}
 FFLAGS    = ${CXXFLAGS}
@@ -74,4 +76,5 @@
 	ph2cph.c \
 	header.c \
+	attach.c \
 	reflector.c
 
@@ -95,4 +98,5 @@
 	ph2cph.o \
 	header.o \
+	attach.o \
 	reflector.o
 
@@ -173,67 +177,66 @@
 # DO NOT DELETE THIS LINE -- make depend depends on it.
 
-diag.o: /usr/include/stdio.h /usr/include/features.h /usr/include/sys/cdefs.h
-diag.o: /usr/include/gnu/stubs.h
-diag.o: /usr/lib/gcc-lib/i386-redhat-linux/egcs-2.91.66/include/stddef.h
-diag.o: /usr/lib/gcc-lib/i386-redhat-linux/egcs-2.91.66/include/stdarg.h
-diag.o: /usr/include/bits/types.h /usr/include/libio.h
-diag.o: /usr/include/_G_config.h /usr/include/bits/stdio_lim.h
-diag.o: /usr/include/stdlib.h version.h diag.h
-init.o: /usr/include/stdio.h /usr/include/features.h /usr/include/sys/cdefs.h
-init.o: /usr/include/gnu/stubs.h
-init.o: /usr/lib/gcc-lib/i386-redhat-linux/egcs-2.91.66/include/stddef.h
-init.o: /usr/lib/gcc-lib/i386-redhat-linux/egcs-2.91.66/include/stdarg.h
-init.o: /usr/include/bits/types.h /usr/include/libio.h
-init.o: /usr/include/_G_config.h /usr/include/bits/stdio_lim.h
-init.o: /usr/include/string.h /usr/include/stdlib.h /usr/include/math.h
-init.o: /usr/include/bits/huge_val.h /usr/include/bits/mathdef.h
-init.o: /usr/include/bits/mathcalls.h version.h diag.h init.h
-parms.o: /usr/include/stdio.h /usr/include/features.h
-parms.o: /usr/include/sys/cdefs.h /usr/include/gnu/stubs.h
-parms.o: /usr/lib/gcc-lib/i386-redhat-linux/egcs-2.91.66/include/stddef.h
-parms.o: /usr/lib/gcc-lib/i386-redhat-linux/egcs-2.91.66/include/stdarg.h
-parms.o: /usr/include/bits/types.h /usr/include/libio.h
-parms.o: /usr/include/_G_config.h /usr/include/bits/stdio_lim.h
-parms.o: /usr/include/string.h /usr/include/stdlib.h /usr/include/math.h
-parms.o: /usr/include/bits/huge_val.h /usr/include/bits/mathdef.h
-parms.o: /usr/include/bits/mathcalls.h diag.h parms.h init.h
-geometry.o: /usr/include/stdio.h /usr/include/features.h
-geometry.o: /usr/include/sys/cdefs.h /usr/include/gnu/stubs.h
-geometry.o: /usr/lib/gcc-lib/i386-redhat-linux/egcs-2.91.66/include/stddef.h
-geometry.o: /usr/lib/gcc-lib/i386-redhat-linux/egcs-2.91.66/include/stdarg.h
-geometry.o: /usr/include/bits/types.h /usr/include/libio.h
-geometry.o: /usr/include/_G_config.h /usr/include/bits/stdio_lim.h
-geometry.o: /usr/include/string.h /usr/include/stdlib.h /usr/include/math.h
-geometry.o: /usr/include/bits/huge_val.h /usr/include/bits/mathdef.h
-geometry.o: /usr/include/bits/mathcalls.h diag.h geometry.h init.h
-atm.o: /usr/include/stdio.h /usr/include/features.h /usr/include/sys/cdefs.h
-atm.o: /usr/include/gnu/stubs.h
-atm.o: /usr/lib/gcc-lib/i386-redhat-linux/egcs-2.91.66/include/stddef.h
-atm.o: /usr/lib/gcc-lib/i386-redhat-linux/egcs-2.91.66/include/stdarg.h
-atm.o: /usr/include/bits/types.h /usr/include/libio.h
-atm.o: /usr/include/_G_config.h /usr/include/bits/stdio_lim.h
-atm.o: /usr/include/string.h /usr/include/math.h /usr/include/bits/huge_val.h
-atm.o: /usr/include/bits/mathdef.h /usr/include/bits/mathcalls.h diag.h atm.h
-atm.o: init.h
-ph2cph.o: /usr/include/stdio.h /usr/include/features.h
-ph2cph.o: /usr/include/sys/cdefs.h /usr/include/gnu/stubs.h
-ph2cph.o: /usr/lib/gcc-lib/i386-redhat-linux/egcs-2.91.66/include/stddef.h
-ph2cph.o: /usr/lib/gcc-lib/i386-redhat-linux/egcs-2.91.66/include/stdarg.h
-ph2cph.o: /usr/include/bits/types.h /usr/include/libio.h
-ph2cph.o: /usr/include/_G_config.h /usr/include/bits/stdio_lim.h
-ph2cph.o: /usr/include/math.h /usr/include/bits/huge_val.h
-ph2cph.o: /usr/include/bits/mathdef.h /usr/include/bits/mathcalls.h diag.h
-ph2cph.o: init.h lagrange.h
-header.o: /usr/include/string.h /usr/include/features.h
-header.o: /usr/include/sys/cdefs.h /usr/include/gnu/stubs.h
-header.o: /usr/lib/gcc-lib/i386-redhat-linux/egcs-2.91.66/include/stddef.h
-header.o: header.h
-reflector.o: /usr/include/stdio.h /usr/include/features.h
-reflector.o: /usr/include/sys/cdefs.h /usr/include/gnu/stubs.h
-reflector.o: /usr/lib/gcc-lib/i386-redhat-linux/egcs-2.91.66/include/stddef.h
-reflector.o: /usr/lib/gcc-lib/i386-redhat-linux/egcs-2.91.66/include/stdarg.h
-reflector.o: /usr/include/bits/types.h /usr/include/libio.h
-reflector.o: /usr/include/_G_config.h /usr/include/bits/stdio_lim.h
-reflector.o: /usr/include/stdlib.h /usr/include/string.h /usr/include/math.h
-reflector.o: /usr/include/bits/huge_val.h /usr/include/bits/mathdef.h
-reflector.o: /usr/include/bits/mathcalls.h version.h diag.h init.h header.h
+diag.o: /usr/include/stdio.h /usr/include/standards.h /usr/include/sys/seek.h
+diag.o: /usr/include/va_list.h /usr/include/sys/types.h
+diag.o: /usr/include/mach/machine/vm_types.h /usr/include/sys/select.h
+diag.o: /usr/include/getopt.h /usr/include/sys/limits.h
+diag.o: /usr/include/sys/machine/machlimits.h /usr/include/sys/syslimits.h
+diag.o: /usr/include/sys/machine/machtime.h /usr/include/sys/rt_limits.h
+diag.o: /usr/include/stdlib.h /usr/include/stdarg.h version.h diag.h
+init.o: /usr/include/stdio.h /usr/include/standards.h /usr/include/sys/seek.h
+init.o: /usr/include/va_list.h /usr/include/sys/types.h
+init.o: /usr/include/mach/machine/vm_types.h /usr/include/sys/select.h
+init.o: /usr/include/getopt.h /usr/include/sys/limits.h
+init.o: /usr/include/sys/machine/machlimits.h /usr/include/sys/syslimits.h
+init.o: /usr/include/sys/machine/machtime.h /usr/include/sys/rt_limits.h
+init.o: /usr/include/string.h /usr/include/strings.h /usr/include/stdlib.h
+init.o: /usr/include/math.h version.h diag.h init.h
+parms.o: /usr/include/stdio.h /usr/include/standards.h
+parms.o: /usr/include/sys/seek.h /usr/include/va_list.h
+parms.o: /usr/include/sys/types.h /usr/include/mach/machine/vm_types.h
+parms.o: /usr/include/sys/select.h /usr/include/getopt.h
+parms.o: /usr/include/sys/limits.h /usr/include/sys/machine/machlimits.h
+parms.o: /usr/include/sys/syslimits.h /usr/include/sys/machine/machtime.h
+parms.o: /usr/include/sys/rt_limits.h /usr/include/string.h
+parms.o: /usr/include/strings.h /usr/include/stdlib.h /usr/include/math.h
+parms.o: diag.h parms.h init.h
+geometry.o: /usr/include/stdio.h /usr/include/standards.h
+geometry.o: /usr/include/sys/seek.h /usr/include/va_list.h
+geometry.o: /usr/include/sys/types.h /usr/include/mach/machine/vm_types.h
+geometry.o: /usr/include/sys/select.h /usr/include/getopt.h
+geometry.o: /usr/include/sys/limits.h /usr/include/sys/machine/machlimits.h
+geometry.o: /usr/include/sys/syslimits.h /usr/include/sys/machine/machtime.h
+geometry.o: /usr/include/sys/rt_limits.h /usr/include/string.h
+geometry.o: /usr/include/strings.h /usr/include/stdlib.h /usr/include/math.h
+geometry.o: diag.h geometry.h init.h
+atm.o: /usr/include/stdio.h /usr/include/standards.h /usr/include/sys/seek.h
+atm.o: /usr/include/va_list.h /usr/include/sys/types.h
+atm.o: /usr/include/mach/machine/vm_types.h /usr/include/sys/select.h
+atm.o: /usr/include/getopt.h /usr/include/sys/limits.h
+atm.o: /usr/include/sys/machine/machlimits.h /usr/include/sys/syslimits.h
+atm.o: /usr/include/sys/machine/machtime.h /usr/include/sys/rt_limits.h
+atm.o: /usr/include/string.h /usr/include/strings.h /usr/include/math.h
+atm.o: /usr/include/stdlib.h diag.h atm.h init.h
+ph2cph.o: /usr/include/stdio.h /usr/include/standards.h
+ph2cph.o: /usr/include/sys/seek.h /usr/include/va_list.h
+ph2cph.o: /usr/include/sys/types.h /usr/include/mach/machine/vm_types.h
+ph2cph.o: /usr/include/sys/select.h /usr/include/getopt.h
+ph2cph.o: /usr/include/sys/limits.h /usr/include/sys/machine/machlimits.h
+ph2cph.o: /usr/include/sys/syslimits.h /usr/include/sys/machine/machtime.h
+ph2cph.o: /usr/include/sys/rt_limits.h /usr/include/math.h
+ph2cph.o: /usr/include/stdlib.h diag.h init.h lagrange.h
+header.o: /usr/include/string.h /usr/include/standards.h
+header.o: /usr/include/sys/types.h /usr/include/mach/machine/vm_types.h
+header.o: /usr/include/sys/select.h /usr/include/strings.h header.h
+attach.o: /usr/include/string.h /usr/include/standards.h
+attach.o: /usr/include/sys/types.h /usr/include/mach/machine/vm_types.h
+attach.o: /usr/include/sys/select.h /usr/include/strings.h
+reflector.o: /usr/include/stdio.h /usr/include/standards.h
+reflector.o: /usr/include/sys/seek.h /usr/include/va_list.h
+reflector.o: /usr/include/sys/types.h /usr/include/mach/machine/vm_types.h
+reflector.o: /usr/include/sys/select.h /usr/include/getopt.h
+reflector.o: /usr/include/sys/limits.h /usr/include/sys/machine/machlimits.h
+reflector.o: /usr/include/sys/syslimits.h /usr/include/sys/machine/machtime.h
+reflector.o: /usr/include/sys/rt_limits.h /usr/include/stdlib.h
+reflector.o: /usr/include/string.h /usr/include/strings.h /usr/include/math.h
+reflector.o: version.h diag.h init.h header.h
Index: trunk/MagicSoft/Simulation/Detector/ReflectorII/config.mk.linux
===================================================================
--- trunk/MagicSoft/Simulation/Detector/ReflectorII/config.mk.linux	(revision 1523)
+++ trunk/MagicSoft/Simulation/Detector/ReflectorII/config.mk.linux	(revision 1535)
@@ -18,7 +18,7 @@
 #---------------------------------------------------------------
 # $RCSfile: config.mk.linux,v $
-# $Revision: 1.1 $
+# $Revision: 1.2 $
 # $Author: bigongia $ 
-# $Date: 2002-07-24 14:43:34 $
+# $Date: 2002-10-09 18:15:27 $
 ##################################################################
 # @maintitle
Index: trunk/MagicSoft/Simulation/Detector/ReflectorII/geometry.c
===================================================================
--- trunk/MagicSoft/Simulation/Detector/ReflectorII/geometry.c	(revision 1523)
+++ trunk/MagicSoft/Simulation/Detector/ReflectorII/geometry.c	(revision 1535)
@@ -9,8 +9,4 @@
 extern  char line[];		/*  parsing buf. (init)	*/
 extern  char axisdev_filename[256], reflectivity_filename[256];
-
-float   mean_refl;              /*  Mirror mean reflectivity 270-610 nm. 
-				 *  AM June 2002.
-				 */
 
 float	ct_Focal_mean;		/*  focal dist. (mean) (cm)	*/
@@ -24,6 +20,4 @@
 
 mirror *ct_data=NULL;		/*  ptr to mirror data		*/
-FILE   *ct_BinaryData=NULL;	/*  binary data for mirrors     */
-char	ct_BinaryName[128];	/*  binary data filename        */
 
 int	nReflectivity=0;	/*  elements in refl. table	*/
@@ -66,6 +60,4 @@
     FILE *datfile = fopen(datname, "r");
     int current = 0;
-
-    mean_refl = 0.;
 
     if (datfile == NULL)
@@ -93,6 +85,4 @@
 			     &Reflectivity[1][current]))
       { 
-	// Added June 2002, AM:
-	mean_refl += Reflectivity[1][current];
 	current++;
 	if (current >= nReflectivity) break; 
@@ -102,6 +92,4 @@
 
     nReflectivity = current;
-    if (current > 0)
-      mean_refl /= (float) current;
 
 }   /*  end of ReadReflectivity  */
Index: trunk/MagicSoft/Simulation/Detector/ReflectorII/header.c
===================================================================
--- trunk/MagicSoft/Simulation/Detector/ReflectorII/header.c	(revision 1523)
+++ trunk/MagicSoft/Simulation/Detector/ReflectorII/header.c	(revision 1535)
@@ -2,68 +2,26 @@
 #include "header.h"
 
-/*  Need to be defined the procedures to set the values in
-    float TimeFirst;
-    float TimeLast;	*/
-
-static RflHeader rhead;  RflHeader *rheadp = &rhead;
-static CerHeader chead;  CerHeader *cheadp = &chead;
+static RflEventHeader rhead;  RflEventHeader *rheadp = &rhead;
+static CerEventHeader chead;  CerEventHeader *cheadp = &chead;
 
 extern float fixed_Phi, fixed_Theta;
-extern int   ct_NMirrors;
-extern float mean_refl;
+extern int   is_Fixed_Target;
 
-void TranslateHeader(RflHeader *r, CerHeader *c)
+void TranslateHeader(RflEventHeader *r, CerEventHeader *c)
 {
-  r->EvtNumber      = c->EvtNumber;
-  r->PrimaryID      = c->PrimaryID;
-  r->Etotal         = c->Etotal;
-  r->Thick0         = c->Thick0;
-  r->FirstTarget    = c->FirstTarget;
-  r->zFirstInt      = c->zFirstInt;
-  r->Theta          = c->Theta;
-  r->Phi            = c->Phi;
-  r->NumRndSeq      = c->NumRndSeq;
-  r->RunNumber      = c->RunNumber;
-  r->DateRun        = c->DateRun;
-  r->Corsika_version= c->Corsika_version;
-  r->NumObsLev      = c->NumObsLev;
-  r->HeightLev      = c->HeightLev[0];
+  memcpy(r,c,154*sizeof(float));
 
-
-  r->SlopeSpec      = c->SlopeSpec;
-  r->ELowLim        = c->ELowLim;
-  r->EUppLim        = c->EUppLim;
-  r->ThetaMin       = c->ThetaMin;
-  r->ThetaMax       = c->ThetaMax;
-  r->PhiMin         = c->PhiMin;
-  r->PhiMax         = c->PhiMax;
-  r->CWaveLower     = c->CWaveLower;
-  r->CWaveUpper     = c->CWaveUpper;
-
-  memcpy(r->p,         c->p,          3*sizeof(float));
-  memcpy(r->RndData,   c->RndData,   30*sizeof(float));
-  memcpy(r->CorePos,   c->CorePos,   40*sizeof(float));
-
-  /* Next 4 variables added in June 2002, AM */
-  r->telescopePhi      = fixed_Phi;
-  r->telescopeTheta    = fixed_Theta; 
-  r->num_mirrors       = ct_NMirrors;
-  r->mean_reflectivity = mean_refl;
-
-  if (c->Corsika_version >= 6.)  /* Viewcone option implemented only in c6xx */
+  if (is_Fixed_Target)  // <- Added in September 2002, AM
     {
-      if (c->viewcone_angles[0] > 0.)
-	{
-	  printf ("ERROR: Input cer file was run using Corsika option\n"); 
-	  printf ("       VIEWCONE VUECON(1) VUECON(2)   with VUECON(1) > 0\n");
-	  printf ("  The reflector program only supports VUECON(1) = 0\n\n");
-	  exit(-1);
-	}
-      else
-	r->ViewConeRadius = c->viewcone_angles[1]; /* degrees */
+      /* Next 2 variables added in June 2002, AM */
+      r->telescopePhi      = fixed_Phi;
+      r->telescopeTheta    = fixed_Theta; 
     }
+  // If no fixed target, telescope always points towards shower. AM Sep 2002
   else
-    r->ViewConeRadius = 0.;
-
+    {
+      r->telescopePhi      = cheadp->Phi;
+      r->telescopeTheta    = cheadp->Theta; 
+    }
 } /*	end of TranslateHeader  */
 
Index: trunk/MagicSoft/Simulation/Detector/ReflectorII/header.h
===================================================================
--- trunk/MagicSoft/Simulation/Detector/ReflectorII/header.h	(revision 1523)
+++ trunk/MagicSoft/Simulation/Detector/ReflectorII/header.h	(revision 1535)
@@ -2,5 +2,37 @@
 #define __RFL_HEADER__
 
-typedef struct			/*  EVTH from cerfile  */ 
+/*  RUNH from cerfile. See CORSIKA manual for explanations */ 
+typedef struct
+{   char RUNH[4];
+    float RunNumber;
+    float date;
+    float Corsika_version;
+    float NumObsLev;
+    float HeightLev[10];
+    float SlopeSpec;  /* Slope of primaries' energy spectrum */
+    float ELowLim;  
+    float EUppLim;    /* Limits of energy range for generation */
+    float EGS4_flag;
+    float NKG_flag;
+    float Ecutoffh;  
+    float Ecutoffm;  
+    float Ecutoffe;  
+    float Ecutoffg;  
+    /* Physical constants and interaction flags (see CORSIKA manual): */
+    float C[50];
+    float dummy1[20]; /* not used */
+    float CKA[40];
+    float CETA[5];
+    float CSTRBA[11];
+    float dummy2[104]; /* not used */
+    float AATM[5];
+    float BATM[5];
+    float CATM[5];
+    float NFL[4];
+}   CerRunHeader;
+
+
+/*  EVTH from cerfile. See CORSIKA manual for explanations */ 
+typedef struct
 {   char	EVTH[4];
     float	EvtNumber;
@@ -71,14 +103,26 @@
     float	Multipl;       
     float	CorePos[2][20];   
-
-    float	dmmy1[14]; 
+    float       SIBYLL[2];
+    float       QGSJET[2];
+    float       DPMJET[2];
+    float       VENUS_cross;
+    float       mu_mult_scat;
+    float       NKG_range;
+    float       EFRCTHN[2];
+    float       WMAX[2];
+    float       rthin_rmax;
+
     float       viewcone_angles[2]; /* (degrees) Inner and outer angles in 
 				      * Corsika's VIEWCONE option
 				      */
     float	dmmy2[119]; 
-}   CerHeader;
-
-typedef struct			/*  EVTH from rflfile  */ 
-{   float	EvtNumber;
+}   CerEventHeader;
+
+/*  EVTH from rflfile. We include first  all the information from the 
+    Corsika event header (except empty bytes) */ 
+
+typedef struct			
+{   char	EVTH[4];
+    float	EvtNumber;
     float	PrimaryID;
     float	Etotal;   
@@ -97,10 +141,86 @@
     float	Corsika_version;
 
-    float	NumObsLev;  /* Should be 1 for MAGIC simulation */
-    float	HeightLev;  /* Observation Level */
-
-    /* Changed meaning of next 9 variables. June 2002, A.Moralejo: */
-    float       num_mirrors;
-    float       mean_reflectivity;
+    float	NumObsLev;
+    float	HeightLev[10]; 
+
+    float	SlopeSpec;
+    float	ELowLim;   
+    float	EUppLim;   
+
+    float	Ecutoffh;  
+    float	Ecutoffm;  
+    float	Ecutoffe;  
+    float	Ecutoffg;  
+
+    float	NFLAIN;
+    float	NFLDIF;
+    float	NFLPI0;
+    float	NFLPIF;
+    float	NFLCHE;
+    float	NFRAGM; 
+ 
+    float	Bx;
+    float	By;
+  
+    float	EGS4yn;
+    float	NKGyn;
+    float	GHEISHAyn;
+    float	VENUSyn;
+    float	CERENKOVyn;
+    float	NEUTRINOyn;
+    float	HORIZONTyn;
+    float	COMPUTER;
+
+    float	ThetaMin;
+    float	ThetaMax;
+    float	PhiMin;
+    float	PhiMax;
+
+    float	CBunchSize;
+    float	CDetInX,CDetInY;
+    float	CSpacInX,CSpacInY;
+    float	CLenInX,CLenInY;
+    float	COutput;
+
+    float	AngleNorthX;
+    float	MuonInfo;
+
+    float	StepLength;
+    float	CWaveLower;       
+    float	CWaveUpper;       
+    float	Multipl;       
+    float	CorePos[2][20];   
+    float       SIBYLL[2];
+    float       QGSJET[2];
+    float       DPMJET[2];
+    float       VENUS_cross;
+    float       mu_mult_scat;
+    float       NKG_range;
+    float       EFRCTHN[2];
+    float       WMAX[2];
+    float       rthin_rmax;
+
+    float       viewcone_angles[2]; 
+    /* (degrees) Inner and outer angles in 
+     * Corsika's VIEWCONE option. This is
+     * only possible with Corsika>6 versions. In
+     * that case, PhiMin=PhiMax  and 
+     * ThetaMin=ThetaMax (also in this header) 
+     * indicate the axis of this cone.   
+     */
+
+    /* ^^^ Up to here, the info from the CORSIKA event header. */
+
+    /* Telescope orientation: */
+    float	telescopePhi;    /* rad */
+    float	telescopeTheta;  /* rad */
+
+    /* Time of first and last photon: */
+    float	TimeFirst;
+    float	TimeLast;
+
+    /* 6 parameters and chi2 of the NKG fit to the longitudinal 
+     * particle distribution (see CORSIKA manual for explanation): 
+     */
     float       longi_Nmax;
     float       longi_t0;
@@ -111,38 +231,5 @@
     float       longi_chi2;
 
-    float	SlopeSpec;
-    float	ELowLim;   
-    float	EUppLim;   
-
-    float	ThetaMin;
-    float	ThetaMax;
-    float	PhiMin;
-    float	PhiMax;
-
-    float	CWaveLower;       
-    float	CWaveUpper;       
-    float	CorePos[2][20];   
-    float	TimeFirst;
-    float	TimeLast;
-
-    /* AM, 23/05/2002: Changed meaning of following
-     * three variables (which were unused before): 
-     */
-    float	telescopePhi;    /* rad */
-    float	telescopeTheta;  /* rad */
-  
-    float	ViewConeRadius; /* Degrees.
-				 * Radius of "view cone" when the primaries'
-				 * directions generated by Corsika lie within
-				 * a cone around a fixed direction. This is
-				 * only possible with Corsika>6 versions. In
-				 * that case, PhiMin=PhiMax  and 
-				 * ThetaMin=ThetaMax (also in this header) 
-				 * indicate the axis of this cone.   
-				 * If ViewConeRadius==0, it means that
-				 * the VIEWCONE option was not used.
-				 */
-
-
+    /* Now the photon statistics of the event: */
     float	CORSIKAPhs;	/*  Original photons written by Corsika	*/
     float	AtmAbsPhs;	/*  Photons absorbed by the atmosphere	*/
@@ -152,5 +239,7 @@
     float	OutOfChamPhs;	/*  Photons outside the camera		*/
     float	CPhotons;	/*  Photons reaching the camera         */
-}   RflHeader;
+
+    float       dummy[10];      /* not used */
+}   RflEventHeader;
 
 
@@ -168,5 +257,5 @@
 } Event_end;
 
-void TranslateHeader(RflHeader *r, CerHeader *c);
+void TranslateHeader(RflEventHeader *r, CerEventHeader *c);
 
 #endif
Index: trunk/MagicSoft/Simulation/Detector/ReflectorII/init.h
===================================================================
--- trunk/MagicSoft/Simulation/Detector/ReflectorII/init.h	(revision 1523)
+++ trunk/MagicSoft/Simulation/Detector/ReflectorII/init.h	(revision 1535)
@@ -48,6 +48,4 @@
 extern long  Seeds[2];			/*  random seeds	*/
 
-extern FILE *ct_BinaryData;		/*  binary data for mirrors	*/
-extern char  ct_BinaryName[];		/*  binary data filename	*/
 extern float ct_Focal_mean;		/*  focal dist. (mean) (cm)	*/
 extern float ct_PSpread_mean;		/*  pt. spread fn. (mean) (cm)	*/
@@ -102,4 +100,6 @@
     "    Single file version\n" \
     "        - D. Bastieri & C. Bigongiari - Jan 2000\n" \
+    "    Version 0.6\n" \
+    "        - A. Moralejo - October 2002\n" \
     "  ################################################\n\n"
 #define SIGN_ERROR_FTL		/*  program, version	*/ \
Index: trunk/MagicSoft/Simulation/Detector/ReflectorII/parms.c
===================================================================
--- trunk/MagicSoft/Simulation/Detector/ReflectorII/parms.c	(revision 1523)
+++ trunk/MagicSoft/Simulation/Detector/ReflectorII/parms.c	(revision 1535)
@@ -11,4 +11,5 @@
 
 char axisdev_filename[256], reflectivity_filename[256];
+char geo_filename[256];
 
 /*  Prototypes  */
@@ -82,7 +83,7 @@
 		if ((geofile=fopen(value_ptr, "r")) == NULL)
 		  FatalError(GEOF_ERROR_FTL, value_ptr);
+		else
+		  strcpy(geo_filename, value_ptr); 
 		Message(GEOF_OPEN__MSG, value_ptr);
-		strcat(strcpy(ct_BinaryName, value_ptr), ".mirr");
-		ct_BinaryData = fopen(ct_BinaryName, "r");
 		break;
 	      case axisdev_file:
Index: trunk/MagicSoft/Simulation/Detector/ReflectorII/ph2cph.c
===================================================================
--- trunk/MagicSoft/Simulation/Detector/ReflectorII/ph2cph.c	(revision 1523)
+++ trunk/MagicSoft/Simulation/Detector/ReflectorII/ph2cph.c	(revision 1535)
@@ -94,4 +94,6 @@
   float sx, sy;
   float t1, t2;
+  float dummy = 0.;
+
 
   void makeOmega(float theta, float phi);
@@ -255,7 +257,14 @@
 
   } else {
-          
-    d = (float) sqrt( b*b - 4.0*a*c );
-          
+
+    /* Introduce positiveness check, AM 3/7/2002 */
+
+    dummy = b*b - 4.0*a*c;
+
+    if (dummy < 0.)           /* No intersection */
+      return 2;
+
+    d = (float) sqrt(dummy);
+
     /*  two possible values for z */ 
           
@@ -276,4 +285,5 @@
   xcut[0] = xCT[0] + rCT[0]/rCT[2]*(xcut[2]-xCT[2]);
   xcut[1] = xCT[1] + rCT[1]/rCT[2]*(xcut[2]-xCT[2]);
+      
 
   /* CBC */
@@ -285,5 +295,5 @@
   sx = Lin2Curv( xcut[0] );
   sy = Lin2Curv( xcut[1] );
-          
+
   /* CBC */
   Debug("@4 sx sy %f %f\n", sx, sy);
@@ -310,5 +320,5 @@
     if (distmirr2 < distmirr) {
       i_mirror = i;
-      distmirr = distmirr2;
+      distmirr  = distmirr2;
     }
   }
Index: trunk/MagicSoft/Simulation/Detector/ReflectorII/reflector.c
===================================================================
--- trunk/MagicSoft/Simulation/Detector/ReflectorII/reflector.c	(revision 1523)
+++ trunk/MagicSoft/Simulation/Detector/ReflectorII/reflector.c	(revision 1535)
@@ -13,4 +13,13 @@
  *
  * ----------------------------------------------------------------------
+ *
+ *  Modified: October 2002
+ *  Author:   Abelardo Moralejo
+ *  Version 0.6: introduced a run header and changed event header to keep
+ *  all information from CORSIKA. In addition, now the magic.def, axisdev.dat
+ *  and reflectivity.dat are attached at the end of the reflector output file 
+ *  to keep also all the parameters with which the program is run.
+ *
+ * ----------------------------------------------------------------------
  */
 
@@ -36,6 +45,6 @@
 static long writep = 0L;	/*  write ptr (on rflfile)	*/
 
-extern CerHeader *cheadp;	/*  var inited in header.c	*/
-extern RflHeader *rheadp;	/*  var inited in header.c	*/
+extern CerEventHeader *cheadp;	/*  var inited in header.c	*/
+extern RflEventHeader *rheadp;	/*  var inited in header.c	*/
 
 /*  Prototypes  */
@@ -43,5 +52,7 @@
 FILE *GetNextFile(char *cername);
 static int GetEvent(void);
-static int ProcessEvent(CerHeader *cheadp, FILE *cerfile, FILE *rflfile);
+static int ProcessEvent(CerEventHeader *cheadp, FILE *cerfile, FILE *rflfile);
+void attach(FILE *f1, FILE *f2);
+
 
 FILE *chkf = NULL;
@@ -51,5 +62,9 @@
 
 main(void)
-{   long event = 0L;		/*  event counter	*/
+{
+    extern char axisdev_filename[256], reflectivity_filename[256];
+    extern char geo_filename[256];
+    FILE *dummy;
+    long event = 0L;		/*  event counter	*/
 
     /*  Read init & geometry parms, init files and vars */
@@ -71,5 +86,18 @@
     fwrite(FLAG_END_OF_FILE, SIZE_OF_FLAGS, 1, rflfile);
 
-    /*  Close file  */
+    /* Attach parameter files used for running the program: */
+    dummy = fopen (geo_filename,"r");
+    attach (rflfile, dummy);
+    fclose(dummy);
+
+    dummy = fopen (axisdev_filename,"r");
+    attach (rflfile, dummy);
+    fclose(dummy);
+
+    dummy = fopen (reflectivity_filename,"r");
+    attach (rflfile, dummy);
+    fclose(dummy);
+
+    /*  Close reflector output file  */
     Log(RFLF_CLOSE_LOG);
     fclose(rflfile);
@@ -82,5 +110,5 @@
   } /*  end of main  */
 
-static int ProcessEvent(CerHeader *cheadp, FILE *cerfile, FILE *rflfile)
+static int ProcessEvent(CerEventHeader *cheadp, FILE *cerfile, FILE *rflfile)
 {   extern int absorption(float wlen, float height, float theta);
     extern int ph2cph(photon *ph, cphoton *cph);
@@ -92,5 +120,4 @@
     extern void makeOmegaI(float theta, float phi);
 
-    char pp[256];
 
     /*  Various counters: phs = absphs + refphs[0..3] + cphs  */
@@ -172,17 +199,35 @@
 	for (ph=0; ph<PH_IN_DATABLOCK; ph++)
 	  {
-	    if (Photons[ph].w <= 0.) break;
+	    /* Added July 2002, AM: check integrity of photon info:
+	       Sometimes we found NaN values inside cerXXXXX file.
+	     */
+	    if (isnan(Photons[ph].w) || isnan(Photons[ph].x) ||
+		isnan(Photons[ph].y) || isnan(Photons[ph].u) ||
+		isnan(Photons[ph].v) || isnan(Photons[ph].t) ||
+		isnan(Photons[ph].h))
+	      {
+		printf("Warning: skipped one photon because its data contained Not-a-Number!\n");
+		continue;
+	      }
+
+	    if (! (Photons[ph].w > 0.) ) break;
 
 	    CPhotons[cphs].w = Photons[ph].w;
 	    Photons[ph].w = wlen = (float) fmod(Photons[ph].w, 1000.);
 
-	    /* TEMPORARY FIX, AM Nov 2001: we found that sometimes the value
+	    /* AM Nov 2001: we found that sometimes the value
 	       stored in Photons[ph].w is not correct, and can result in a 
 	       wavelength beyond 600 nm, which makes the program crash later.
-	       Now we force wlen to its expected range:
-	       */
-
-	    wlen = MIN(MAX(290.,wlen),600.);
-
+
+	       AM July 2002: we now simply skip the photon if the wavelength
+	       is not in the expected range, which we now take from the corsika
+	       event header (just in case we would change it in the future).
+	    */
+
+	       if (wlen < cheadp->CWaveLower || wlen > cheadp->CWaveUpper)
+		 {
+		   printf("Warning: skipped one photon with strange wavelength: %f nm\n", wlen);
+		   continue;
+		 }
 
 	    /* ADDED AM May 2002: now we take into account the telescope
@@ -215,4 +260,5 @@
 			   ph2cph(&Photons[ph], &CPhotons[cphs])))
 	      refphs[ref_type-1]++; 
+
 	    else		/*	Photon passed	*/
 	      {
@@ -260,5 +306,5 @@
 	rheadp->TimeLast  = last;
 
-	/*  Update RflHeader with info on ph/cph nrs and write it  */
+	/*  Update RflEventHeader with info on ph/cph nrs and write it  */
 	rheadp->CORSIKAPhs   = phs;
 	rheadp->AtmAbsPhs    = absphs;
@@ -268,7 +314,7 @@
 	rheadp->OutOfChamPhs = refphs[3];
 	rheadp->CPhotons     = (long) overflow * NR_OF_CPHOTONS + cphs;
-	fwrite(rheadp, sizeof(RflHeader), 1, rflfile);
-
-/*        for (myloop=0; myloop<sizeof(RflHeader)/4; myloop++)
+	fwrite(rheadp, sizeof(RflEventHeader), 1, rflfile);
+
+/*        for (myloop=0; myloop<sizeof(RflEventHeader)/4; myloop++)
  *	  fprintf(chkf, "%e ", *((float *)rheadp+myloop)); 
  *        fputc('\n', chkf);
@@ -284,6 +330,8 @@
 	    fseek(tmpf, 0L, SEEK_SET); /*  Start from the beginning  */
 	    while (overflow--)
-	      {	fread (CPhotons, sizeof(cphoton), NR_OF_CPHOTONS, tmpf);
-		fwrite(CPhotons, sizeof(cphoton), NR_OF_CPHOTONS, rflfile); }
+	      {	
+		fread (CPhotons, sizeof(cphoton), NR_OF_CPHOTONS, tmpf);
+		fwrite(CPhotons, sizeof(cphoton), NR_OF_CPHOTONS, rflfile); 
+	      }
 
 	    /*  Reload data in CPhotons    */
@@ -291,7 +339,9 @@
 
 	    /*	Close (and delete) temp file  */
-	    fclose(tmpf);  }
+	    fclose(tmpf);  
+	  }
 
 	/*  Write (remaining) cphotons  */
+
 	fwrite(CPhotons, sizeof(cphoton), cphs, rflfile);
 
@@ -308,40 +358,52 @@
 
 static int GetEvent(void)
-{   int found = FALSE,		/*  event found		*/
-      isWrong = FALSE;		/*  cerfile is wrong	*/
-      static int newFile = TRUE; /*  if TRUE, check if cerfile is valid	*/
-
-      do
-	{ /* In case the just-opened file is a valid cerfile,
-	     starting with a RUNH, loop until a valid event is found:
-	     id est with: first_Event <= EvtNumber <= last_Event
-	     and             low_Ecut <=  Etotal   <= high_Ecut
-	     If the search was successful, "found" is set to TRUE.
-	     If there are reading errors, "isWrong" is set to TRUE. */
+{   
+  int found = FALSE,	      /*  event found		*/
+  isWrong = FALSE;	      /*  cerfile is wrong	*/
+  static int newFile = TRUE;  /*  if TRUE, check if cerfile is valid	*/
+
+  CerRunHeader cRunHead;
+  
+  do
+    {
+      /* In case the just-opened file is a valid cerfile,
+	 starting with a RUNH, loop until a valid event is found:
+	 id est with: first_Event <= EvtNumber <= last_Event
+	 and             low_Ecut <=  Etotal   <= high_Ecut
+	 If the search was successful, "found" is set to TRUE.
+	 If there are reading errors, "isWrong" is set to TRUE. */
 
 	if (newFile
-	 && (1 != fread(cheadp, sizeof(CerHeader), 1, cerfile)
+	 && (1 != fread(cheadp, sizeof(CerEventHeader), 1, cerfile)
 	     || strncmp(cheadp->EVTH, "RUNH", 4)))
 	{   isWrong = TRUE;   }
 
 	else do
-	{   /*  Push fileptr in case it is a "EVTH"  */
-	    readp = ftell(cerfile);
-
-	    /*  Error: exit loop  */
-	    if (1 != fread(cheadp, sizeof(CerHeader), 1, cerfile))
+	{
+	  if (newFile)
+	    {
+	      /* Write Reflector "run header" (one per cer file!): */
+	      memcpy(&cRunHead, cheadp, sizeof(CerEventHeader));
+	      fwrite(&cRunHead, sizeof(CerRunHeader), 1, rflfile);
+	    }
+
+	  /*  Push fileptr in case it is a "EVTH"  */
+	  readp = ftell(cerfile);
+
+	  /*  Error: exit loop  */
+	  if (1 != fread(cheadp, sizeof(CerEventHeader), 1, cerfile))
 	    {	isWrong = TRUE;    break;   }
 
-	    /*  Ok: set found at TRUE and exit loop  */
-	    if (strncmp(cheadp->EVTH, "EVTH", 4) == 0
-	     && first_Event <= (long)cheadp->EvtNumber
-	     &&		       (long)cheadp->EvtNumber <= last_Event
-	     &&    low_Ecut <= cheadp->Etotal
-	     &&		       cheadp->Etotal <= high_Ecut)
+	  /*  Ok: set found at TRUE and exit loop  */
+	  if (strncmp(cheadp->EVTH, "EVTH", 4) == 0
+	      && first_Event <= (long)cheadp->EvtNumber
+	      &&		       (long)cheadp->EvtNumber <= last_Event
+	      &&    low_Ecut <= cheadp->Etotal
+	      &&		       cheadp->Etotal <= high_Ecut)
 	    {	found = TRUE;   }
 
-	    /*  File is finished: exit loop  */
-	    else if (strncmp(cheadp->EVTH, "RUNE", 4) == 0)
-		break;
+	  /*  File is finished: exit loop  */
+	  else if (strncmp(cheadp->EVTH, "RUNE", 4) == 0)
+	    break;
 
 	} while(found == FALSE);
Index: trunk/MagicSoft/Simulation/Detector/ReflectorII/version.h
===================================================================
--- trunk/MagicSoft/Simulation/Detector/ReflectorII/version.h	(revision 1523)
+++ trunk/MagicSoft/Simulation/Detector/ReflectorII/version.h	(revision 1535)
@@ -6,5 +6,5 @@
 
 #define PROGRAM reflector
-#define VERSION 0.5
+#define VERSION 0.6
 
 #endif
