Index: trunk/MagicSoft/Simulation/Detector/include-MFadc/MFadc.cxx
===================================================================
--- trunk/MagicSoft/Simulation/Detector/include-MFadc/MFadc.cxx	(revision 2289)
+++ trunk/MagicSoft/Simulation/Detector/include-MFadc/MFadc.cxx	(revision 2290)
@@ -18,8 +18,13 @@
 #include "MGFadcSignal.hxx"
 
-MFadc::MFadc(Float_t ampl, Float_t fwhm) {
+MFadc::MFadc(Float_t ampl, Float_t fwhm, Float_t amplout, Float_t fwhmout) {
   //
   //  Constructor overloaded II 
   //  
+  //  Input variables:
+  //  1. ampl(out) = integration of the single phe response (outer pixels)
+  //  2. fwhm(out) = width at half high of the single phe 
+  //  response(outer pixels)
+  //
   //  The procedure is the following: 
   //  1. some parameters of the trigger are set to default.   
@@ -27,6 +32,8 @@
   //  3. Then the all signals are set to zero
   
-  fwhm_resp = fwhm       ; 
-  ampl_resp = ampl  ; 
+  fwhm_resp = fwhm; 
+  ampl_resp = ampl; 
+  fwhm_resp_outer = fwhmout; 
+  ampl_resp_outer = amplout; 
   
   //
@@ -42,5 +49,5 @@
   
   Float_t   dX, dX2 ; 
-  
+
   dX  = WIDTH_FADC_TIMESLICE / SUBBINS ; 
   dX2 = dX/2. ; 
@@ -51,8 +58,28 @@
     
     //
-    //   the value 0.125 was introduced to normalize the things
-    //
-    sing_resp[i] = 0.125 *  
-      ampl_resp * expf(-0.5 * (x-x0)*(x-x0) / (sigma*sigma) ) ; 
+    //   the value 1/(2*Pi*sigma^2) was introduced to normalize 
+    //   the area at the input value
+    //
+    sing_resp[i] = ampl_resp / sqrt(2*3.1415926*sigma*sigma)*  
+       expf(-0.5 * (x-x0)*(x-x0) / (sigma*sigma) ) ; 
+
+  } 
+
+  sigma = fwhm_resp_outer / 2.35 ; 
+  x0 = 3*sigma ; 
+  
+  dX  = WIDTH_FADC_TIMESLICE / SUBBINS ; 
+  dX2 = dX/2. ; 
+  
+  for (i=0; i< RESPONSE_SLICES_MFADC ; i++ ) {  
+
+    x = i * dX + dX2 ; 
+    
+    //
+    //   the value 1/(2*Pi*sigma^2) was introduced to normalize 
+    //   the area at the input value
+    //
+    sing_resp_outer[i] = ampl_resp_outer / sqrt(2*3.1415926*sigma*sigma)*  
+       expf(-0.5 * (x-x0)*(x-x0) / (sigma*sigma) ) ; 
 
   } 
@@ -82,6 +109,15 @@
     memset(used, 0, CAMERA_PIXELS*sizeof(Bool_t));
     memset(output, 0, CAMERA_PIXELS*FADC_SLICES*sizeof(UChar_t));
-}
-
+    memset(output_lowgain, 0, CAMERA_PIXELS*FADC_SLICES*sizeof(UChar_t));
+}
+void MFadc::Fill( Int_t iPix, Float_t time, 
+		  Float_t amplitude, Int_t isinner ) { 
+
+  if(isinner)
+    Fill(iPix, time, amplitude);
+  else
+    FillOuter(iPix, time, amplitude);
+
+}
 void MFadc::Fill( Int_t iPix, Float_t time, Float_t amplitude ) { 
   
@@ -143,4 +179,69 @@
   else {
     cout << "  WARNING!  Fadc::Fill " << time << "  out of TriggerTimeRange " 
+	 << TOTAL_TRIGGER_TIME << endl ; 
+  }
+
+}
+
+void MFadc::FillOuter( Int_t iPix, Float_t time, Float_t amplitude ) { 
+  
+  //
+  // fills the information about one single Phe in the Trigger class
+  // for an outer pixel
+  //
+  // parameter is the number of the pixel and the time-difference to the
+  // first particle
+  //
+  //
+
+  Int_t i, ichan, ichanfadc ; 
+  
+  //
+  //   first we have to check if the pixel iPix is used or not until now
+  //   if this is the first use, reset all signal for that pixels
+  // 
+  if ( iPix > CAMERA_PIXELS ) {
+    cout << " WARNING:  MFadc::FillOuter() :  iPix greater than CAMERA_PIXELS"
+	 << endl ;
+    exit(987) ; 
+  }
+
+  if ( used[iPix] == FALSE ) {
+    used [iPix] = TRUE ; 
+    
+    for (i=0; i < (Int_t) SLICES_MFADC; i++ ) {
+      sig[iPix][i] = 0. ; 
+    }
+  }
+
+  //
+  //   then select the time slice to use (ican) 
+  // 
+
+  
+  if ( time < 0. ) {
+    cout << "  WARNING! Fadc::FillOuter  " << time << "  below ZERO!! Very strange!!" 
+	 << endl ; 
+  }
+  else if ( time < TOTAL_TRIGGER_TIME ) { 
+    //
+    //   determine the slices number assuming the WIDTH_RESPONSE_MFADC
+    //
+    ichan = (Int_t) ( time / ((Float_t) WIDTH_RESPONSE_MFADC )); 
+
+    //
+    //   putting the response slices in the right sig slices.
+    //   Be carefull, because both slices have different widths. 
+    //
+
+    for ( i = 0 ; i<RESPONSE_SLICES; i++ ) {
+      ichanfadc = (Int_t) ((ichan+i)/SUBBINS) ; 
+      if ( (ichanfadc) < (Int_t)SLICES_MFADC ) {  
+	sig[iPix][ichanfadc] += (amplitude * sing_resp_outer[i] )  ; 
+      } 
+    }
+  }
+  else {
+    cout << "  WARNING!  Fadc::FillOuter " << time << "  out of TriggerTimeRange " 
 	 << TOTAL_TRIGGER_TIME << endl ; 
   }
@@ -328,4 +429,19 @@
 }
 
+void MFadc::SetElecNoise(Float_t value){
+
+  UInt_t i;
+ 
+  cout<<"MFadc::SetElecNoise ... generating database for electroni noise."
+      <<endl;
+
+  for (i=0;i<CAMERA_PIXELS*(Int_t) SLICES_MFADC*101;i++){
+    noise[i]=GenElec->Gaus(0., value  );
+      }
+
+  cout<<"MFadc::SetElecNoise ... done"<<endl;
+
+}
+
 void MFadc::ElecNoise(Float_t value) {
   // ============================================================
@@ -334,4 +450,7 @@
   //    to the signal
   //
+  UInt_t startslice;
+
+  startslice=GenElec->Integer(CAMERA_PIXELS*(Int_t) SLICES_MFADC*100);
 
     for ( Int_t i = 0 ; i < CAMERA_PIXELS; i++) {
@@ -343,16 +462,18 @@
 	    used [i] = TRUE ; 
 	    
-	    for (Int_t ii=0; ii < (Int_t)SLICES_MFADC; ii++ ) {
-		sig[i][ii] = 0. ; 
-	    }
+	    memcpy( (Float_t*)&sig[0][0],
+		    (Float_t*)&noise[startslice+i*(Int_t) SLICES_MFADC], 
+		    (Int_t) SLICES_MFADC*sizeof(Float_t));
+    
 	}
 	//      
 	//  Then the noise is introduced for each time slice
 	//
-	for ( Int_t is=0 ; is< (Int_t)SLICES_MFADC ; is++ ) {
+	else
+	  for ( Int_t is=0 ; is< (Int_t)SLICES_MFADC ; is++ ) {
 	    
-	    sig[i][is] += GenElec->Gaus(0., value) ; 
+	    sig[i][is] += noise[startslice+i*(Int_t) SLICES_MFADC+is] ; 
 	    
-	}
+	  }
 	
     }
@@ -478,7 +599,16 @@
       for ( Int_t is=iFirstSlice ; is < (iFirstSlice+FADC_SLICES) ; is++ ) {
 	if (is< (Int_t)SLICES_MFADC && sig[ip][is]>0.0)
-	  output[ip][i++]=(UChar_t) (sig[ip][is]+0.5);
+	  {
+	  output[ip][i++]=(sig[ip][is] > 255. ? 255 :(UChar_t) (sig[ip][is]+0.5));
+	  output_lowgain[ip][i++]= 
+	    (Int_t)(pedestal[ip]+(sig[ip][is]-pedestal[ip])/HIGH2LOWGAIN) > 255. ? 255 :
+	    (UChar_t)(pedestal[ip]+(sig[ip][is]-pedestal[ip])/HIGH2LOWGAIN);
+	  
+	  }
 	else 
-	  output[ip][i++]= 0;
+ 	  {
+ 	    output[ip][i]= 0;
+ 	    output_lowgain[ip][i++]= 0;
+ 	  }
       }
 
@@ -490,5 +620,5 @@
   // ============================================================
   //
-  //  This method is used to book the histogramm to show the signal in 
+  //  This method is used to book the histogram to show the signal in 
   //  a special gui frame (class MGTriggerSignal). After the look onto the
   //  signals for a better understanding of the things we will expect 
@@ -568,3 +698,13 @@
 }
 
-
+ 
+UChar_t MFadc::GetFadcLowGainSignal(Int_t pixel, Int_t slice){
+ 
+   //  It returns the analog signal for a given pixel and a given FADC
+   //  time slice which would be read.
+ 
+   return (output_lowgain[pixel][slice]);
+}
+
+
+
