Index: trunk/MagicSoft/Simulation/Detector/include-MFadc/MFadc.cxx
===================================================================
--- trunk/MagicSoft/Simulation/Detector/include-MFadc/MFadc.cxx	(revision 2339)
+++ trunk/MagicSoft/Simulation/Detector/include-MFadc/MFadc.cxx	(revision 2393)
@@ -138,5 +138,6 @@
   // 
   if ( iPix > numpix ) {
-    cout << " WARNING:  MFadc::Fill() :  iPix greater than CAMERA_PIXELS"
+    cout << " WARNING:  MFadc::Fill() :  iPix greater than Pixels in Camera = "
+	 << numpix
 	 << endl ;
     exit(987) ; 
@@ -371,4 +372,5 @@
     for(j=0;j<(Int_t)SLICES_MFADC;j++)
       sig[i][j]+=pedestal[i];
+
 }
 
@@ -437,5 +439,5 @@
       <<endl;
 
-  for (i=0;i<UInt_t(SLICES_MFADC*1001);i++){
+  for (i=0;i<(UInt_t (SLICES_MFADC))*1001;i++){
     noise[i]=GenElec->Gaus(0., value  );
       }
@@ -459,5 +461,5 @@
 	//
 
-      startslice=GenElec->Integer((Int_t) SLICES_MFADC*1000);
+      startslice=GenElec->Integer(((Int_t)SLICES_MFADC)*1000);
 
 	if ( used[i] == FALSE ) {
@@ -466,6 +468,9 @@
       	    memcpy( (Float_t*)&sig[i][0],
 		    (Float_t*)&noise[startslice], 
-		    (Int_t) SLICES_MFADC*sizeof(Float_t));
+		    ((Int_t) SLICES_MFADC)*sizeof(Float_t));
       
+	  for ( Int_t is=0 ; is< (Int_t)SLICES_MFADC ; is++ ) {
+
+	  }
 	}
 	//      
@@ -481,4 +486,58 @@
 }
 
+void MFadc::SetDigitalNoise(Float_t value){
+
+  UInt_t i;
+  Float_t xrdm;
+ 
+  cout<<"MFadc::SetDigitalNoise ... generating database for electronic noise."
+      <<endl;
+
+  for (i=0;i<UInt_t(SLICES_MFADC*1001);i++){
+    xrdm=GenElec->Gaus(0., value);
+    digital_noise[i]=(xrdm>0?Int_t(xrdm+0.5):Int_t(xrdm-0.5));    
+  }
+  
+  cout<<"MFadc::SetDigitalNoise ... done"<<endl;
+
+}
+
+void MFadc::DigitalNoise() {
+  // ============================================================
+  //
+  //    adds the noise due to optronic and electronic 
+  //    to the signal
+  //
+  UInt_t startslice;
+
+    for ( Int_t i = 0 ; i < numpix; i++) {
+
+      if ( used[i] == TRUE ) {
+	startslice=GenElec->Integer((Int_t) SLICES_MFADC*999);
+	//      
+	//  Then the noise is introduced for each time slice
+	//
+	for ( Int_t is=0 ; is< FADC_SLICES; is++ ) {
+	  
+	  if(digital_noise[startslice+is]+Int_t(output[i][is])<0) 
+	     output[i][is] = 0;
+	  else
+	    output[i][is] = 
+	      (digital_noise[startslice+is]+Int_t(output[i][is])>255 ?
+	       255 :
+	       UChar_t(digital_noise[startslice+is]+Int_t(output[i][is])));
+	  if(digital_noise[startslice+FADC_SLICES+is]+Int_t(output_lowgain[i][is])<0) 
+	    output_lowgain[i][is] = 0;
+	  else
+	    output_lowgain[i][is] = 
+	      (digital_noise[startslice+FADC_SLICES+is]
+	       +Int_t(output_lowgain[i][is])>255?
+	       255:
+	       UChar_t(digital_noise[startslice+FADC_SLICES+is]
+		       +Int_t(output_lowgain[i][is])));
+	}
+      }
+    }
+}
 
 
@@ -570,4 +629,48 @@
 }
 
+Float_t MFadc::GetPedestalNoise( Int_t pix, Int_t ishigh) {
+  // ============================================================
+  //
+  //    computes the pedestal sigma for channel pix
+
+  Float_t rms=0;
+  UChar_t value=0;
+  
+  Float_t factor;
+  UInt_t startslice;
+
+  factor=(ishigh?1.0:high2low_gain);
+
+  startslice=GenElec->Integer((Int_t) SLICES_MFADC*999);
+
+  for ( Int_t is=0; is <  (Int_t)SLICES_MFADC ; is++ ) {
+    if (pedestal[pix]+(sig[pix][is]-pedestal[pix])/factor>0.0){
+      value=(pedestal[pix]+(sig[pix][is]-pedestal[pix])/factor > 255. 
+	     ? 255 
+	     :UChar_t(pedestal[pix]+(sig[pix][is]-pedestal[pix])/factor+0.5));
+      if(Int_t(value)+digital_noise[startslice+is]<0.0)
+	 value=0;
+      else
+	value=(Int_t(value)+digital_noise[startslice+is]>255
+	       ?255
+	       :UChar_t(Int_t(value)+digital_noise[startslice+is]));
+    }
+    else {
+      value= 0;
+      if(Int_t(value)+digital_noise[startslice+is]<0.0)
+	 value=0;
+      else
+	value=(Int_t(value)+digital_noise[startslice+is]>255
+	       ?255
+	       :UChar_t(Int_t(value)+digital_noise[startslice+is]));
+    }
+    rms+=((Float_t)value-pedestal[pix])*((Float_t)value-pedestal[pix]);
+  }
+
+  rms=sqrt(rms/(SLICES_MFADC-1));
+
+  return rms;
+}
+
 void MFadc::TriggeredFadc(Float_t time) {
   
@@ -601,11 +704,13 @@
 	if (is< (Int_t)SLICES_MFADC && sig[ip][is]>0.0)
 	  {
+	    
 	    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);
+	      (Int_t)(pedestal[ip]+(sig[ip][is]-pedestal[ip])/high2low_gain+0.5) > 255. ? 255 :
+	      (UChar_t)(pedestal[ip]+(sig[ip][is]-pedestal[ip])/high2low_gain+0.5);
 	    i++;
+	    
 	  }
-	else if(sig[ip][is]>0.0)
+	else if(sig[ip][is]>=0.0)
 	  {
  	    output[ip][i]= (UChar_t)(pedestal[ip]+0.5);
@@ -616,5 +721,8 @@
  	  {
  	    output[ip][i]= 0;
- 	    output_lowgain[ip][i]= 0;
+	    if((pedestal[ip]+(sig[ip][is]-pedestal[ip])/high2low_gain)<0)
+	      output_lowgain[ip][i]= 0;
+	    else
+	      output_lowgain[ip][i]=(UChar_t)(pedestal[ip]+(sig[ip][is]-pedestal[ip])/high2low_gain+0.5);
 	    i++;
  	  }
@@ -714,2 +822,5 @@
    return (output_lowgain[pixel][slice]);
 }
+
+
+
