Index: trunk/MagicSoft/Simulation/Detector/include-MFadc/MFadc.cxx
===================================================================
--- trunk/MagicSoft/Simulation/Detector/include-MFadc/MFadc.cxx	(revision 5319)
+++ 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++;
 	}
+
     }
 }
