Index: trunk/MagicSoft/Simulation/Detector/Camera/camera.cxx
===================================================================
--- trunk/MagicSoft/Simulation/Detector/Camera/camera.cxx	(revision 5268)
+++ trunk/MagicSoft/Simulation/Detector/Camera/camera.cxx	(revision 5269)
@@ -21,7 +21,7 @@
 //
 // $RCSfile: camera.cxx,v $
-// $Revision: 1.73 $
+// $Revision: 1.74 $
 // $Author: moralejo $ 
-// $Date: 2004-10-13 14:28:59 $
+// $Date: 2004-10-13 17:05:05 $
 //
 ////////////////////////////////////////////////////////////////////////
@@ -238,6 +238,4 @@
 static  float Spot_y=0.0;
 static  float Spotsigma=0.0;
-
-static  int CalibrationRun = 0;
 
   
@@ -717,6 +715,5 @@
   // get filenames
 
-  CalibrationRun = 1;
-  if (! CalibrationRun)
+  if (! is_calibration_run())
     for(int ict=0;ict<ct_Number;ict++)
       {
@@ -1682,5 +1679,5 @@
 
 
-  if (CalibrationRun)
+  if (is_calibration_run())
     {
       DoCalibration(Fadc_CT, Trigger_CT, camgeom, nsb_trigresp, nsb_fadcresp,
@@ -4455,5 +4452,6 @@
 {
 
-  int ntshow = 0;
+  int nevent = 0;
+  int maxevents;
   int lons_return;
 
@@ -4464,4 +4462,28 @@
   float timefirst = 0.;
   float timelast = 0.;
+  
+  float lambda, sigma_lambda, phot_per_pix, sigma_time;
+  UInt_t *first_pixel, *last_pixel;
+  int selected_pixel;
+
+  // Get parameters of the calibration:
+
+  get_calibration_properties( &lambda, &sigma_lambda, &phot_per_pix, 
+			      &sigma_time, &maxevents, &selected_pixel);
+
+  first_pixel = new UInt_t[ct_Number];
+  last_pixel  = new UInt_t[ct_Number];
+
+  for (int ict = 0; ict < ct_Number; ict++)
+    {
+      if (selected_pixel < 0)
+	{
+	  first_pixel[ict] = 0;
+	  last_pixel[ict]  = ((MGeomCam*)(camgeom.UncheckedAt(ict)))->GetNumPixels()-1;
+	}
+      else
+	first_pixel[ict] = last_pixel[ict] = selected_pixel;
+    }
+
 
   TArrayC *fadcValues;     //@< the analog Fadc High gain signal for pixels
@@ -4476,5 +4498,5 @@
   float *pheinpix = new float [ct_NPixels];
 
-  for (int calevent = 0; calevent < 1; calevent++) 
+  for (int calevent = 0; calevent < maxevents; calevent++) 
     { 
       //
@@ -4493,14 +4515,12 @@
 	}
 
-      ntshow++;
-
-      if((ntshow+1)%100 == 1)
-	log(SIGNATURE, "Event %d\n", ntshow);
+      nevent++;
+
+      if((nevent+1)%100 == 1)
+	log(SIGNATURE, "Event %d\n", nevent);
 	
       // Produce the photoelectrons
-      float phot_per_pix = 100.;
       for(int ict = 0; ict < ct_Number; ict++)
 	produce_calib_phes( (MGeomCam*)(camgeom.UncheckedAt(ict)),
-			    phot_per_pix,
 			    Trigger_CT[ict],
 			    Fadc_CT[ict],
@@ -4508,5 +4528,11 @@
 			    pheinpix,
 			    &(ntotphot[ict]),
-			    ict);
+			    ict,
+			    lambda,
+			    sigma_lambda,
+			    phot_per_pix,
+			    sigma_time,
+			    selected_pixel
+			    );
 
       // NSB simulation
@@ -4518,7 +4544,5 @@
 	  for (int ict = 0; ict < ct_Number; ict++)
 	    {
-	      for (UInt_t ui = 0;
-		  ui < ((MGeomCam*)(camgeom.UncheckedAt(ict)))->GetNumPixels();
-		  ui++)
+	      for (UInt_t ui = first_pixel[ict]; ui <= last_pixel[ict]; ui++)
 		{
 		  nphe_from_nsb[ict] += nsb_phepns[ict][ui];
@@ -4593,7 +4617,5 @@
 	  // Fill the FADC information
 	  //
-	  for(UInt_t ui=0;
-	      ui<((MGeomCam*)(camgeom.UncheckedAt(ict)))->GetNumPixels();
-	      ui++)
+	  for(UInt_t ui = first_pixel[ict]; ui <= last_pixel[ict]; ui++)
 	    {
 	      for (int jslice = 0; jslice < FADC_slices_written; jslice++)
@@ -4628,12 +4650,16 @@
 //------------------------------------------------------------
 
-int produce_calib_phes( MGeomCam *camgeom,      // The camera layout
-			float     phot_per_pix, // Average number of photons per inner pixel
+int produce_calib_phes( MGeomCam *camgeom,       // The camera layout
 			MTrigger *trigger,
 			MFadc    *fadc,
-			int      *itotnphe,     // total number of produced photoelectrons		
-			float    *nphe,         // number of photoelectrons in each pixel
-			int      *nphot,        // total number of photons in all pixels
-			int       ict           // Telescope that is being analised to get the right QE.
+			int      *itotnphe,      // total number of produced photoelectrons		
+			float    *nphe,          // number of photoelectrons in each pixel
+			int      *nphot,         // total number of photons in all pixels
+			int       ict,           // Telescope that is being analised to get the right QE.
+			float     lambda,        // Mean wavelength of light in nm
+			float     sigma_lambda,  // Sigma of wavelengtgaussian spread
+			float     phot_per_pix,  // Average # of photons per inner pixel
+			float     sigma_time,    // Sigma of time spread of photons
+			int       selected_pixel // if >= 0, only this pixel is used! 
 			)
 {
@@ -4643,4 +4669,5 @@
   float **qept;
   float radius_mm, focal_dist_mm;
+  int total_photons;
 
   // reset variables
@@ -4671,6 +4698,10 @@
 
   // Calculate total number of photons to be produced.
-  int total_photons = (int) (phot_per_pix * 3.14159265 * radius_mm * radius_mm / 
-			     (*camgeom)[0].GetA());
+  if (selected_pixel < 0)
+    total_photons = (int) (phot_per_pix * 3.14159265 * radius_mm * radius_mm / 
+			   (*camgeom)[0].GetA());
+  else
+    total_photons = (int) (phot_per_pix * 
+			   (*camgeom)[selected_pixel].GetA() / (*camgeom)[0].GetA());
 
   // loop over the photons
@@ -4679,39 +4710,55 @@
     {
 
-      time = 0.;
-
-      // Obtain photon coordinates on the camera. We assume a point source of light placed
-      // in the center of the mirror dish.
-
-      // polar angle 
-      float psi = RandomNumber * 2 * 3.14159265;
-      // angle between the telescope axis and the photon trajectory.
-      float epsilon = acos(1.-RandomNumber*(1.-cos_epsilon_max));
-      float tanepsilon = tan(epsilon);
-
-      cx = focal_dist_mm*tanepsilon*cos(psi); // mm
-      cy = focal_dist_mm*tanepsilon*sin(psi); // mm
-
-      // Angle between photon trajectory and camera plane:
-      phi = 3.14159265/2.-epsilon; // rad
+      // time
+      time = random.Gaus(4.*sigma_time, sigma_time);
+
+      cout << "### " << time << endl << endl;
 
       // wavelength
-	  
-      wl = 500.; // nm
-
-
-      if( (wl > WAVEBANDBOUND6) || (wl < WAVEBANDBOUND1) || 
-	  (sqrt(cx*cx + cy*cy) > radius_mm ) )
+      wl = lambda;
+
+      if( (wl > WAVEBANDBOUND6) || (wl < WAVEBANDBOUND1))
 	continue;
 
-      //
-      // Pixelization
-      //
-      ipixnum = bpoint_is_in_pix(cx, cy, camgeom);
-
-      // -1 = the photon is in none of the pixels
-      //  0 = the phton is in the central pixel, which is not used
-      if (ipixnum==-1 || ipixnum==0)
-        continue;
+      if (selected_pixel < 0)
+	{
+	  // Obtain photon coordinates on the camera. We assume a point source of light placed
+	  // in the center of the mirror dish.
+
+	  // polar angle 
+	  float psi = RandomNumber * 2 * 3.14159265;
+	  // angle between the telescope axis and the photon trajectory.
+	  float epsilon = acos(1.-RandomNumber*(1.-cos_epsilon_max));
+	  float tanepsilon = tan(epsilon);
+
+	  cx = focal_dist_mm*tanepsilon*cos(psi); // mm
+	  cy = focal_dist_mm*tanepsilon*sin(psi); // mm
+ 
+	  if (sqrt(cx*cx + cy*cy) > radius_mm )
+	    continue;
+
+	  // Angle between photon trajectory and camera plane:
+	  phi = 3.14159265/2.-epsilon; // rad
+
+	  //
+	  // Pixelization
+	  //
+	  ipixnum = bpoint_is_in_pix(cx, cy, camgeom);
+
+	  // -1 = the photon is in none of the pixels
+	  //  0 = the phton is in the central pixel, which is not used
+	  if (ipixnum==-1 || ipixnum==0)
+	    continue;
+	}
+      else
+	{
+	  // Angle between photon trajectory and camera plane:
+	  phi = atan2( focal_dist_mm, 
+		       sqrt( (*camgeom)[selected_pixel].GetX()*(*camgeom)[selected_pixel].GetX()+
+			     (*camgeom)[selected_pixel].GetY()*(*camgeom)[selected_pixel].GetY()));
+
+	  ipixnum = selected_pixel;
+	}
+
 
       // increase number of photons within pixels
@@ -4793,5 +4840,5 @@
       *itotnphe += 1;
     }
-
+  cout << "$$" << endl;
   return(0);
 
Index: trunk/MagicSoft/Simulation/Detector/Camera/camera.h
===================================================================
--- trunk/MagicSoft/Simulation/Detector/Camera/camera.h	(revision 5268)
+++ trunk/MagicSoft/Simulation/Detector/Camera/camera.h	(revision 5269)
@@ -19,7 +19,7 @@
 //=
 //= $RCSfile: camera.h,v $
-//= $Revision: 1.22 $
+//= $Revision: 1.23 $
 //= $Author: moralejo $ 
-//= $Date: 2004-09-17 14:48:21 $
+//= $Date: 2004-10-13 17:05:05 $
 //=
 //=//////////////////////////////////////////////////////////////////////
@@ -70,4 +70,5 @@
 #include "MParContainer.h"
 #include "MArray.h"
+
 
 // command line options available
@@ -341,4 +342,20 @@
 		   float ro);
 
+
+int DoCalibration( class MFadc **Fadc_CT, class MTrigger **Trigger_CT, 
+		   class TObjArray camgeom, 
+		   float* nsb_trigresp, float *nsb_fadcresp,
+		   class MLons *lons, class MLons *lons_outer,
+		   float **diffnsb_phepns, int addElecNoise,
+		   class TTree *EvtTree, class MRawEvtHeader **EvtHeader,
+		   class MMcEvt **McEvt, class MRawEvtData **EvtData);
+
+
+int produce_calib_phes(	class MGeomCam *camgeom, class MTrigger *trigger, 
+			class MFadc *fadc, int *itotnphe, float *nphe,
+			int *nphot, int ict, float lambda, float sigma_lambda,
+			float phot_per_pix, float sigma_time, int selected_pixel);
+
+
 //!@}
 
@@ -350,4 +367,8 @@
 /*
  *$Log: not supported by cvs2svn $
+ *Revision 1.22  2004/09/17 14:48:21  moralejo
+ *
+ *Changes to adapt headers to c++ style.
+ *
  *Revision 1.21  2004/09/16 15:24:53  moralejo
  *Updated on CVS after long time...
Index: trunk/MagicSoft/Simulation/Detector/Camera/creadparam.cxx
===================================================================
--- trunk/MagicSoft/Simulation/Detector/Camera/creadparam.cxx	(revision 5268)
+++ trunk/MagicSoft/Simulation/Detector/Camera/creadparam.cxx	(revision 5269)
@@ -19,7 +19,7 @@
 //=
 //= $RCSfile: creadparam.cxx,v $
-//= $Revision: 1.30 $
+//= $Revision: 1.31 $
 //= $Author: moralejo $ 
-//= $Date: 2004-10-12 13:32:02 $
+//= $Date: 2004-10-13 17:05:05 $
 //=
 //=//////////////////////////////////////////////////////////////////////
@@ -110,4 +110,13 @@
 static float m_fraction[MAX_NUMBER_OF_CTS];
 
+static int   CalibrationRun = 0;  // > 0 if a calibration run has been chosen
+static float lambda = 0.;         // Mean wavelength for photons of calibration pulses.
+static float sigma_lambda = 0.;   // Sigma of wavelength distribution for calibration.
+static float phot_per_pix = 0.;   // Average number of photons per inner pixel for calibration.
+static float sigma_time = 0.;     // Time spread (sigma of gaussian) of calibration photons.
+static int   nevents = 0;         // Number of events in calibration run.
+static int   selected_pixel = -1; // Selected pixel: if >= 0 ==> only this pixel is filled! 
+
+
 // Coordinates of CT locations, in centimeters, in the Corsika system.
 
@@ -599,8 +608,17 @@
     case mirror_fraction:
 
-	sscanf(line, "%s %d %f", token, &j, &aux);
-	m_fraction[j] = aux;
+      sscanf(line, "%s %d %f", token, &j, &aux);
+      m_fraction[j] = aux;
 	
-	break;
+      break;
+
+    case calibration_run:
+
+      sscanf(line, "%s %f %f %f %f %i %i", token, &lambda, &sigma_lambda, &phot_per_pix,
+	     &sigma_time, &nevents, &selected_pixel);
+      
+      CalibrationRun = 1;
+      
+      break;
 
     case end_file:            //@< end of the parameters file
@@ -1367,4 +1385,19 @@
 }
 
+int is_calibration_run(){
+  return CalibrationRun;
+}
+
+void  get_calibration_properties(float *a, float *b, float *c, float *d, int *e, int *f)
+{
+  *a = lambda;
+  *b = sigma_lambda;
+  *c = phot_per_pix;
+  *d = sigma_time;
+  *e = nevents;
+  *f = selected_pixel;
+}
+
+
 //=------------------------------------------------------------
 //!@subsection Log of this file.
@@ -1373,4 +1406,7 @@
 //
 // $Log: not supported by cvs2svn $
+// Revision 1.30  2004/10/12 13:32:02  moralejo
+// *** empty log message ***
+//
 // Revision 1.29  2004/09/17 14:48:21  moralejo
 //
Index: trunk/MagicSoft/Simulation/Detector/Camera/creadparam.h
===================================================================
--- trunk/MagicSoft/Simulation/Detector/Camera/creadparam.h	(revision 5268)
+++ trunk/MagicSoft/Simulation/Detector/Camera/creadparam.h	(revision 5269)
@@ -19,7 +19,7 @@
 //=
 //= $RCSfile: creadparam.h,v $
-//= $Revision: 1.27 $
+//= $Revision: 1.28 $
 //= $Author: moralejo $ 
-//= $Date: 2004-10-12 13:32:02 $
+//= $Date: 2004-10-13 17:05:05 $
 //=
 //=//////////////////////////////////////////////////////////////////////
@@ -110,9 +110,10 @@
 T(sigma_x_cm_spot), /* Additional gaussian spread of mirror in x axis spot on camera */ \
 T(sigma_y_cm_spot), /* Additional gaussian spread of mirror in y axis spot on camera */ \
-T(misspoint_deg), /* Additional misspointing of Telescope direction*/ \
-T(trigger_delay), /*  Delay in ns between beginning of FADC
-                      time window and the trigger instant.*/ \
+T(misspoint_deg),   /* Additional misspointing of Telescope direction*/ \
+T(trigger_delay),   /*  Delay in ns between beginning of FADC
+                        time window and the trigger instant.*/ \
 T(telescope_location_cm), /* Telescope coordinates in Corsika system */ \
 T(mirror_fraction), /* Fraction of dish mirror really operative */ \
+T(calibration_run), /* option to produce a calibration file */ \
 T(end_file)         /* end of the parameters file */
 
@@ -190,4 +191,8 @@
 float get_telescope_location_cm(int j, int icoor);
 float get_mirror_fraction(int j);
+
+int   is_calibration_run();
+void  get_calibration_properties(float *lambda, float *sigma_lambda, float *phot_per_pix,
+ 				 float *sigma_time, int *nevents, int *selected_pixel);
 //!@}
 
@@ -205,4 +210,7 @@
 /*
  * $Log: not supported by cvs2svn $
+ * Revision 1.27  2004/10/12 13:32:02  moralejo
+ * *** empty log message ***
+ *
  * Revision 1.26  2004/09/17 14:48:21  moralejo
  *
Index: trunk/MagicSoft/Simulation/Detector/Camera/input.card
===================================================================
--- trunk/MagicSoft/Simulation/Detector/Camera/input.card	(revision 5268)
+++ trunk/MagicSoft/Simulation/Detector/Camera/input.card	(revision 5269)
@@ -5,11 +5,15 @@
 ct_geom 1
 # Quantum efficiency file:
-qe_file 0  /home/magic/MagicProgs/Simulation/Detector/Data/qe-emi-coat.RFL.dat
+qe_file 0  /home/magic/CAMERA_NEW/MagicSoft/Simulation/Detector/Data/qe-emi-coat.RFL.dat
 # Input file (one per telescope):
-input_file 0 /data1/magic/reflex/Gamma_zbin0_0_7_1000to1009_w0.rfl
+# input_file 0 /data1/magic/reflex/Gamma_zbin0_0_7_1000to1009_w0.rfl
+# Perform calibration run: lambda sigma_lambda phot_per_pixel time_sigma n_events [selected_pixel]
+calibration_run 400. 30. 100. 5. 100 90
 # Ascii output file name:
-data_file ./Gamma_zbin0_0_7_1000to1009_w0.dat
+#data_file ./Gamma_zbin0_0_7_1000to1009_w0.dat
+data_file ./prueba.dat
 # root output file name:
-root_file ./Gamma_zbin0_0_7_1000to1009_w0.root
+#root_file ./Gamma_zbin0_0_7_1000to1009_w0.root
+root_file ./prueba.root
 # Trigger characteristics: gate length (ns), min. overlapping time (ns),
 # amplitude and FWHM of (gaussian) single phe response for trigger:
@@ -18,10 +22,11 @@
 trigger_single 0 4 4 2
 # Switch on NSB:
-nsb_on
+#nsb_on
+nsb_off
 # Number of photons from the diffuse NSB (nphe / ns 0.1*0.1 deg^2 239 m^2) and
 # minimum number of phe from shower required to simulate NSB:
 nsb_mean 0.13 10
 # Starfield (see Starfieldadder program)
-# starfield_file /home/magic/MagicProgs/Simulation/Detector/Starfield/starfield.rfl
+# starfield_file /home/magic/CAMERA_NEW/MagicSoft/Simulation/Detector/Starfield/starfield.rfl
 # Electronic noise in FADC (sigma in ADC counts): Inner pixels, outer pixels, digital noise: 
 fadc_noise 1.48 2.1 0.
@@ -35,20 +40,20 @@
 seeds 66767 45069
 # Directory where NSB database can be found for inner and outer pixels:
-nsb_directory  /home/magic/MagicProgs/Simulation/Detector/StarLight/inner/
-nsb_dir_outer  /home/magic/MagicProgs/Simulation/Detector/StarLight/outer/
+nsb_directory  /home/magic/CAMERA_NEW/MagicSoft/Simulation/Detector/StarLight/inner/
+nsb_dir_outer  /home/magic/CAMERA_NEW/MagicSoft/Simulation/Detector/StarLight/outer/
 #
 # FADC properties: shape of single phe response (1 means realistic one, from
 # Pulpo setup), integral in FADC counts for 1 phe. (3rd argument is ignored 
 # unless gaussian shape - 1st argument=0 - was chosen) :
-fadc_prop 1 7.8 5.0
+#fadc_prop 1 7.8 5.0
 # Same as above for outer pixels:
-fadc_outer 1 3.2 5.0
+#fadc_outer 1 3.2 5.0
 #
 # tentative values for future fast (2 GHz) FADC:
 #
-# fadc_prop  0 40. 3.
-# fadc_outer 0 15. 3.
+fadc_prop  0 40. 3.
+fadc_outer 0 15. 3.
 # FADC sampling frequency and number of FADC slices:
-# fadc_GHz 2. 100
+fadc_GHz 2. 100
 #
 # Write to output trigger information:
