Index: trunk/MagicSoft/Simulation/Detector/Camera/TOBEDONE
===================================================================
--- trunk/MagicSoft/Simulation/Detector/Camera/TOBEDONE	(revision 6567)
+++ trunk/MagicSoft/Simulation/Detector/Camera/TOBEDONE	(revision 6584)
@@ -2,5 +2,9 @@
 - Change shape of low gain pulse, to look like in data.
 
-- Change default delay of low gain pulse w.r.t. high gain
+- Introduce correlations in the electronic noise
+
+- Default intensity of UV calibration 40/0.35 photons
+
+- Change default delay of low gain pulse w.r.t. high gain (add option in input card)
 
 - Make no switch to low gain of high gain signal is small, so that
Index: trunk/MagicSoft/Simulation/Detector/Camera/camera.cxx
===================================================================
--- trunk/MagicSoft/Simulation/Detector/Camera/camera.cxx	(revision 6567)
+++ trunk/MagicSoft/Simulation/Detector/Camera/camera.cxx	(revision 6584)
@@ -7,6 +7,8 @@
 // @subtitle    Code for the simulation of the camera phase
 // @desc        Code for the simulation of the camera of CT1 and MAGIC
-// @author      J C Gonzalez
+// @author      J C Gonzalez, O Blanch, A Moralejo
+// @email       moralejo@pd.infn.it
 // @email       gonzalez@mppmu.mpg.de
+// @email       blanch@ifae.es
 // @date        Thu May  7 16:24:22 1998
 //
@@ -5059,4 +5061,11 @@
 //
 // $Log: not supported by cvs2svn $
+// Revision 1.90  2005/02/17 15:37:12  moralejo
+//
+// Corrected bug in the setting of the trigger patterns. It turns out that
+// in the current data format, MRawRunHeader.fFormatVersion=5, as of
+// February 2005, the trigger bits are "inverted" ( 0 <-> 1 ) in their meaning
+// (see Mars/mtrigger/MTriggerPattern).
+//
 // Revision 1.89  2005/02/17 09:15:28  moralejo
 //
Index: trunk/MagicSoft/Simulation/Detector/include-MFadc/MFadc.cxx
===================================================================
--- trunk/MagicSoft/Simulation/Detector/include-MFadc/MFadc.cxx	(revision 6567)
+++ trunk/MagicSoft/Simulation/Detector/include-MFadc/MFadc.cxx	(revision 6584)
@@ -23,5 +23,6 @@
 	     Int_t shapeout, Float_t integralout, Float_t fwhmout, 
 	     Float_t trigger_delay, Float_t fadc_slices_per_ns,
-	     Int_t fadc_slices_written) {
+	     Int_t fadc_slices_written, Int_t gainswitchamp,
+	     Int_t shiftfromswitch2lowgain) {
   //
   //  Constructor overloaded II 
@@ -52,4 +53,6 @@
   fFadcSlicesPerNanosec = fadc_slices_per_ns;
   fFadcSlices = fadc_slices_written;
+  fGainSwitchAmp = gainswitchamp;
+  fShiftFromSwitch2LowGain = shiftfromswitch2lowgain;
 
   fSlices_mFadc = (Int_t)(TOTAL_TRIGGER_TIME*fFadcSlicesPerNanosec);
@@ -102,5 +105,5 @@
   
   //
-  //    set up the response shape
+  // set up the response shape
   // 
 
@@ -701,5 +704,6 @@
   //    adds the noise due to optronics and electronics 
   //    to the signal. This is noise which comes before the FADC,
-  //    so it will be later scaled down in the low gain branch.
+  //    so it will be later scaled down in the low gain branch, if 
+  //    the switch to low gain occurs.
   //
   UInt_t startslice;
@@ -872,7 +876,12 @@
 //===========================================================================
 //
-// Next function generates one pure noise event for pixel "pix", then adds 
-// up the readouts of a number n_slices of its FADC slices, this being the 
-// return value.
+// Next function adds up the noise in pixel "pix", scaling down the part
+// of it which comes from before the receivers in the case we are dealing with
+// low gain (ishigh=0). The output is the sum of the readouts of a number 
+// n_slices of FADC slices. For the case of low gain, the FADC contents we add
+// are not what we would have in a real pedestal event, but nevertheless this 
+// is useful in the camera simulation to obtain what the pedestal fluctuations 
+// are for the low gain. This will be written to the camera output, in the 
+// MMcFadcHeader.
 //
 Float_t MFadc::AddNoiseInSlices( Int_t pix, Int_t ishigh, Int_t n_slices) {
@@ -888,5 +897,5 @@
   // If we deal with low gain, we have to scale the values in sig[][] by
   // the gain ratio (high2low_gain), since "sig" contains here the noise 
-  // produce before the receiver boards (for instance NSB noise)
+  // produced before the receiver boards (for instance NSB noise)
   //
   factor=(ishigh?1.0:high2low_gain);
@@ -949,10 +958,34 @@
 	  for ( Int_t i=0 ; i < fFadcSlices ; i++ ) 
 	    {
-	      output[ip][i]= pedestal[ip];
-	      output_lowgain[ip][i]= pedestal[ip];
+	      output[ip][i] = pedestal[ip];
+	      output_lowgain[ip][i] = pedestal[ip];
 	    }
 	  continue;
 	}
 
+
+      // First put the high gain in the output slices:
+      i = 0;
+      Int_t switch_i = 0;
+      for ( Int_t is = iFirstSlice; is < (iFirstSlice+fFadcSlices); is++ ) 
+	{
+	  if (is < (Int_t)fSlices_mFadc)
+	    {
+	      output[ip][i] = sig[ip][is];
+	      
+	      if (switch_i == 0) // Hi gain limit not yet surpassed before.
+		{
+		  if (output[ip][i] > fGainSwitchAmp)
+		    switch_i = i + fShiftFromSwitch2LowGain;
+		}
+	    }
+
+	  else // We are beyond the simulated signal history in sig[][]! Put just mean pedestal!
+	    output[ip][i] = pedestal[ip];
+
+	  i++;
+	}
+
+      // Now put the low gain:
       i=0;
       for ( Int_t is = iFirstSlice; is < (iFirstSlice+fFadcSlices); is++ ) 
@@ -960,16 +993,23 @@
 	  if (is < (Int_t)fSlices_mFadc)
 	    {
-	      output[ip][i] = sig[ip][is];
-
-	      // Low gain is scaled down by the factor high2low_gain:
-	      output_lowgain[ip][i]= pedestal[ip] + (sig[ip][is]-pedestal[ip])/high2low_gain;
+	      if (switch_i > 0 && (i+fFadcSlices) >= switch_i)
+		output_lowgain[ip][i] = pedestal[ip] + 
+		  (sig[ip][is]-pedestal[ip])/high2low_gain;
+	      // Once the shift occurs, low gain is filled with the high
+	      // gain signal scaled down by the factor high2low_gain
+
+	      else
+		output_lowgain[ip][i] = sig[ip][is+fFadcSlices];
+	      // Write out high gain into low gain slices if there was no 
+	      // switch, or before the switch occurs.
 	    }
+
 	  else // We are beyond the simulated signal history in sig[][]! Put just mean pedestal!
 	    {
-	      output[ip][i] = pedestal[ip];
 	      output_lowgain[ip][i]= pedestal[ip];
 	    }
 	  i++;
 	}
+
     }
 }
Index: trunk/MagicSoft/Simulation/Detector/include-MFadc/MFadc.hxx
===================================================================
--- trunk/MagicSoft/Simulation/Detector/include-MFadc/MFadc.hxx	(revision 6567)
+++ trunk/MagicSoft/Simulation/Detector/include-MFadc/MFadc.hxx	(revision 6584)
@@ -22,5 +22,5 @@
 #include "MFadcDefine.h"
 
-class MMcEvt  ; 
+class MMcEvt; 
 
 //==========
@@ -107,7 +107,14 @@
   Float_t *sing_resp_outer; // the shape of the phe_response function 
 
-
-  //
-  //   RandomGenerator for the Electonic Noise
+  Int_t fGainSwitchAmp; // Height of the high gain signal (in ADC counts) at which we decide
+                        // to fill the low gain with a scaled down version of the pulse in the
+                        // high gain. Else we put in the continuation of the high gain. 
+
+  Int_t fShiftFromSwitch2LowGain; 
+  // Distance in FADC slices from the slice in which the amplitude
+  // fGainSwitchAmp is reached to were the switch to low gain will happen.
+
+  //
+  //   RandomGenerator for the Electronic Noise
   //
 
@@ -129,6 +136,8 @@
 	Float_t trig_delay = 0.,
 	Float_t fadc_slices_per_ns = FADC_SLICES_PER_NSEC,
-	Int_t   fadc_slices_written = FADC_SLICES);
-  
+	Int_t   fadc_slices_written = FADC_SLICES,
+	Int_t   gainswitchamp = 120,
+	Int_t   shiftfromswitch2lowgain = 13);
+
   void SetSeed(UInt_t seed)  {GenElec->SetSeed(seed);}
 
