Index: trunk/MagicSoft/Simulation/Detector/Camera/camera.cxx
===================================================================
--- trunk/MagicSoft/Simulation/Detector/Camera/camera.cxx	(revision 5317)
+++ trunk/MagicSoft/Simulation/Detector/Camera/camera.cxx	(revision 5318)
@@ -21,7 +21,7 @@
 //
 // $RCSfile: camera.cxx,v $
-// $Revision: 1.78 $
+// $Revision: 1.79 $
 // $Author: moralejo $ 
-// $Date: 2004-10-21 17:44:07 $
+// $Date: 2004-10-26 14:02:32 $
 //
 ////////////////////////////////////////////////////////////////////////
@@ -827,14 +827,19 @@
   
   log(SIGNATURE,
-      "%s:\n\t%20s: %s\n\t%20s: %s\n\t%20s: %s\n\t%20s: %s %3.2f, %s %3.2f (%s), %3.2f (%s) + %3.2f (%s) %s\n",
+      "%s:\n\t%20s: %s\n\t%20s: %s\n\t%20s: %s\n\t%20s: %s\n\t\t %s %3.2f,\n\t\t %s %3.2f (%s), %3.2f (%s) \n\t\t + %3.2f (%s)\n\n",
       "Flags",
       "Data_From_STDIN",   ONoff(Data_From_STDIN),  
       "Write_All_Events",  ONoff(Write_All_Images), 
       "Rotate Starfield",  ONoff(Starfield_rotate),
-      "Electronic Noise",  "trigger (rel. noise): ", Trigger_noise, 
-      "FADC (ADC counts): ", FADC_noise_inner, "inner pixels",
-      FADC_noise_outer, "outer pixels", 
-      DIGITAL_noise, "added digital noise at FADC", ONoff(addElecNoise)
+      "Electronic Noise",  ONoff(addElecNoise), 
+      "Trigger noise (amplitude relative to that of single phe): ", 
+      Trigger_noise, 
+      "FADC noise (ADC counts): ", FADC_noise_inner, "inner p.",
+      FADC_noise_outer, "outer p.", 
+      DIGITAL_noise, "added digital noise at FADC"
       );
+
+  if (! apply_gain_fluctuations())
+    log(SIGNATURE, "PMT gain fluctuations have been switched OFF in the input card.\n\n"); 
 
   // log flags information
@@ -931,4 +936,7 @@
 		    Trigger_response_fwhm, i);  //@< A instance of the Class MTrigger 
     Trigger_CT[i]->SetSeed(UInt_t(i+get_seeds(0)));
+
+    if ( ! apply_gain_fluctuations())
+      Trigger_CT[i]->SetGainFluctuations(kFALSE);
   }
 
@@ -4033,5 +4041,5 @@
   // totally equivalent to having made  +- 1 FADC slice
 
-  float fadc_jitter = (1./fadc->GetFadcSlicesPerNanosec()) * random.Uniform(0., 1.);
+  float fadc_jitter = (1./fadc->GetFadcSlicesPerNanosec()) * random.Uniform(0., 1.); //ns
 
   //- - - - - - - - - - - - - - - - - - - - - - - - - 
@@ -4471,11 +4479,15 @@
   int *ntotphot = new int[ct_Number];
   float *nphe_from_nsb = new float[ct_Number];
-
-  float timefirst = 0.;
-  float timelast = 0.;
+  float *pulsetime = new float[ct_Number];
   
   float lambda, sigma_lambda, phot_per_pix, sigma_time;
   UInt_t *first_pixel, *last_pixel;
   int selected_pixel;
+
+  TRandom random;
+  random.SetSeed((UInt_t)(get_seeds(0)*get_seeds(0)));
+
+  for (int ict = 0; ict < ct_Number; ict++)
+    pulsetime[ict] = 0.;
 
   // Get parameters of the calibration:
@@ -4534,17 +4546,30 @@
       // Produce the photoelectrons
       for(int ict = 0; ict < ct_Number; ict++)
-	produce_calib_phes( (MGeomCam*)(camgeom.UncheckedAt(ict)),
-			    Trigger_CT[ict],
-			    Fadc_CT[ict],
-			    &(ntotphe[ict]),
-			    pheinpix,
-			    &(ntotphot[ict]),
-			    ict,
-			    lambda,
-			    sigma_lambda,
-			    phot_per_pix,
-			    sigma_time,
-			    selected_pixel
-			    );
+	{
+	  // Obtain the FADC jitter of 1 FADC slice. This is a time to be added to the 
+	  // time of all photons in an event, before digitalization of the signal. It is
+	  // therefore the same time shift for all pixels in a CT. 
+
+	  float fadc_jitter = 
+	    (1./Fadc_CT[ict]->GetFadcSlicesPerNanosec()) * random.Uniform(0., 1.); //ns
+
+
+	  produce_calib_phes( (MGeomCam*)(camgeom.UncheckedAt(ict)),
+			      Trigger_CT[ict],
+			      Fadc_CT[ict],
+			      &(ntotphe[ict]),
+			      pheinpix,
+			      &(ntotphot[ict]),
+			      ict,
+			      lambda,
+			      sigma_lambda,
+			      phot_per_pix,
+			      sigma_time,
+			      selected_pixel,
+			      fadc_jitter
+			      );
+
+	  pulsetime[ict] = fadc_jitter; // Keep value for writing it to output.
+	}
 
       // NSB simulation
@@ -4612,8 +4637,13 @@
 	  // The additional value 3*sigma_time makes that the pulse maximum is, in 
 	  // average, in the same position no matter of the time width of the pulse
-	  // (see in produce_calib_phes(...) that in order to avoid negative times 
+	  // (see that, in produce_calib_phes(...), in order to avoid negative times 
 	  // we shift the time of the photons by this same amount!)
 
 	  Fadc_CT[ict]->TriggeredFadc(3.+3*sigma_time);
+
+	  // Add the "digital noise": electronic noise intrinsic to the FADC and which
+	  // therefore is not scaled down in the low gain slices!
+
+	  Fadc_CT[ict]->DigitalNoise();
 
 	  //
@@ -4623,5 +4653,5 @@
 
 	  McEvt[ict]->Fill( 0, 0, 0., -1.0, -1.0, -1.0, 0., 0., 0., 0., 0.,
-			    0., 0., 0., timefirst, timelast, 0., 0., 0.,
+			    0., 0., 0., pulsetime[ict], 0., 0., 0., 0.,
 			    0., 0., 0., 0., 0, 0, 0,
 			    (UInt_t) ntotphot[ict], 
@@ -4677,5 +4707,7 @@
 			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! 
+			int       selected_pixel,// if >= 0, only this pixel is used! 
+			float     fadc_jitter    // Random shift (max 1 slice) of pulses in 
+			                         // FADC, to simulate FADC clock noise.
 			)
 {
@@ -4698,11 +4730,4 @@
   TRandom random;
   random.SetSeed(seed);
-
-  // Introduce the FADC jitter of 1 FADC slice. This is a time to be added to the 
-  // time of all photons in an event, before digitalization of the signal. We use 
-  // a positive value between 0 and 1 slice for technical reasons, but it is 
-  // totally equivalent to having made  +- 1 FADC slice
-
-  float fadc_jitter = (1./fadc->GetFadcSlicesPerNanosec()) * random.Uniform(0., 1.);
 
   //
@@ -4733,6 +4758,8 @@
   for (int iph = 0; iph < total_photons; iph++) 
     {
-
-      // time
+      //
+      // Simulate arrival times of photons to camera plane. We do not simulate the small
+      // delays between pixels due to the different distances to the pulser.
+      //
       // We do not want negative times, so center the gaussian at 3 sigma 
       // and reject negative values:
@@ -4883,4 +4910,12 @@
 //
 // $Log: not supported by cvs2svn $
+// Revision 1.78  2004/10/21 17:44:07  moralejo
+//
+//  Fixed error recently introduced in MLons::GetResponse. The NSB database
+//  was not correclt ycopied to the FADC when the randomly selected time was
+//  too close to the end of the database. This happened about 2% of times and
+//  produced some repetitive noise peaks in the FADC.
+//  Changed in camera.cxx the arguments of lons.SetSeed y lons_outer.SetSeed
+//
 // Revision 1.77  2004/10/19 10:35:05  moralejo
 // *** empty log message ***
