Index: trunk/MagicSoft/Simulation/Detector/include-MFadc/MFadc.cxx
===================================================================
--- trunk/MagicSoft/Simulation/Detector/include-MFadc/MFadc.cxx	(revision 5078)
+++ trunk/MagicSoft/Simulation/Detector/include-MFadc/MFadc.cxx	(revision 5079)
@@ -18,5 +18,5 @@
 #include "MGFadcSignal.hxx"
 
-MFadc::MFadc(Int_t pix, Float_t integral, Float_t fwhm, Float_t integralout, Float_t fwhmout, Float_t trigger_delay) {
+MFadc::MFadc(Int_t pix, Int_t shape, Float_t integral, Float_t fwhm, Int_t shapeout, Float_t integralout, Float_t fwhmout, Float_t trigger_delay) {
   //
   //  Constructor overloaded II 
@@ -43,12 +43,37 @@
   fwhm_resp_outer = fwhmout; 
   integ_resp_outer = integralout; 
-
-  cout<< "[MFadc]  Setting up the MFadc with this values "<< endl ; 
-  cout<< "[MFadc]    - Inner pixels :  "<< endl ; 
-  cout<< "[MFadc]       Response Area : "<<integral<<" adc counts"<< endl ; 
-  cout<< "[MFadc]       Response FWHM : "<<fwhm<<" ns"<< endl ; 
-  cout<< "[MFadc]    - Inner pixels :  "<< endl ; 
-  cout<< "[MFadc]       Response Area : "<<integralout<<" adc counts"<< endl ; 
-  cout<< "[MFadc]       Response FWHM : "<<fwhmout<<" ns"<< endl ; 
+  shape_resp=shape;
+  shape_resp_outer=shapeout;
+
+    cout<< "[MFadc]  Setting up the MFadc with this values "<< endl ; 
+    cout<< "[MFadc]    - Inner pixels :  "<< endl ; 
+  switch(shape_resp){
+  case 0:
+    cout<< "[MFadc]       Pulse shape   : Gaussian ("<<shape_resp<<")"<< endl ; 
+    cout<< "[MFadc]       Response Area : "<<integ_resp<<" adc counts"<< endl ; 
+    cout<< "[MFadc]       Response FWHM : "<<fwhm_resp<<" ns"<< endl ; 
+    break;
+  case 1:
+    cout<< "[MFadc]       Pulse shape   : From Pulpo ("<<shape_resp<<")"<< endl ; 
+    cout<< "[MFadc]       Response Area : "<<integ_resp<<" adc counts"<< endl ; 
+    break;
+  default:
+    cout<< "[MFadc]       Pulse shape unknown"<<endl;
+  }
+  cout<< "[MFadc]    - Outer pixels :  "<< endl ; 
+  switch(shape_resp_outer){
+  case 0:
+    cout<< "[MFadc]       Pulse shape   : Gaussian ("<<shape_resp_outer<<")"<<endl; 
+    cout<< "[MFadc]       Response Area : "<<integ_resp_outer<<" adc counts"<<endl; 
+    cout<< "[MFadc]       Response FWHM : "<<fwhm_resp_outer<<" ns"<< endl ; 
+    break;
+  case 1:
+    cout<< "[MFadc]       Pulse shape   : From Pulpo ("<<shape_resp_outer<<")"<<endl;
+    cout<< "[MFadc]       Response Area : "<<integ_resp_outer<<" adc counts"<< endl ;
+    break;
+  default:
+    cout<< "[MFadc]       Pulse shape unknown ("<<shape_resp_outer<<")"<<endl;
+  }
+
   
   //
@@ -59,75 +84,142 @@
   Float_t   sigma ; 
   Float_t   x, x0 ; 
-
-  sigma = fwhm_resp / 2.35 ; 
-  x0 = 3*sigma;
-  
-  fadc_time_offset = trigger_delay-x0; // ns
-
-
   Float_t   dX, dX2 ; 
 
-  dX  = WIDTH_FADC_TIMESLICE / SUBBINS ; 
+  Float_t   response_sum_inner, response_sum_outer;
+  response_sum_inner = 0.;
+  response_sum_outer = 0.;
+
+  dX  = WIDTH_FADC_TIMESLICE / SUBBINS ;   // Units: ns
   dX2 = dX/2. ; 
 
-
-  
+  switch(shape_resp){
+
+  case 0:
+    sigma = fwhm_resp / 2.35 ; 
+    x0 = 3*sigma;
+    fadc_time_offset = trigger_delay-x0; // ns
+  
+    for (i=0; i< RESPONSE_SLICES_MFADC ; i++ ) {  
+
+      x = i * dX + dX2 ; 
+      
+      sing_resp[i] = expf(-0.5 * (x-x0)*(x-x0) / (sigma*sigma) ) ; 
+
+      response_sum_inner += sing_resp[i];
+    }
+
+    break;
+  case 1:
+    float p1,p2,p3,p4,p5,p6,p7;
+    float d;
+    float zed_slices;
+    // Parameters values extracted from fitting a real FADC response
+    // gaussian electronic pulse passed through the whole chain from 
+    // transmitter boards to FADC.
+    p1 = 2.066; 
+    p2 = 1.568; 
+    p3 = 3; // This sets the peak of the pulse at x ~ 3 ADC slices
+            // It is just a safe value so that the pulse is well contained.
+    p4 = 0.00282; 
+    p5 = 0.04093; 
+    p6 = 0.2411; 
+    p7 = -0.009442;
+    // Define the time before trigger to read FADC signal when it
+    //  has to be written
+    fadc_time_offset = trigger_delay-p3*WIDTH_FADC_TIMESLICE; // ns
+
+    for (i=0; i< RESPONSE_SLICES_MFADC ; i++ ) {  
+      x = i * dX + dX2;
+
+      // x has to be converted from ns to units FADC slices:
+      zed_slices=x/WIDTH_FADC_TIMESLICE-p3;
+      d=(zed_slices>0)?0.5:-0.5;
+
+      sing_resp[i] =  (p1*exp(-p2*(exp(-p2*zed_slices)+zed_slices))+p4+
+		       p5*exp(-p2*(exp(-p2*zed_slices)+p6*zed_slices))+p7*d);
+
+      response_sum_inner += sing_resp[i];
+    }
+
+    break;
+  default:
+    cout<<"[MFadc] MFadc::MFadc : Shape of FADC pulse for inner pixel unknown."
+	<<endl;
+    cout<<"[MFadc] MFadc::MFadc : Exiting Camera ..."
+	<<endl;
+    exit(1);
+  } 
+
+  // Response for outer pixels
+
+  switch(shape_resp_outer){
+
+  case 0:
+    sigma = fwhm_resp_outer / 2.35 ; 
+    x0 = 3*sigma ; 
+    fadc_time_offset = trigger_delay-x0; // ns
+    
+    for (i=0; i< RESPONSE_SLICES_MFADC ; i++ ) {  
+      
+      x = i * dX + dX2 ; 
+      
+      //
+      //   the value 1/sqrt(2*Pi*sigma^2) was introduced to normalize 
+      //   the area at the input value After this, the integral
+      //   of the response will be integ_resp.
+      //
+      sing_resp_outer[i] = expf(-0.5 * (x-x0)*(x-x0) / (sigma*sigma) ) ; 
+      response_sum_outer += sing_resp_outer[i];
+    } 
+    break;
+  case 1:
+    float p1,p2,p3,p4,p5,p6,p7;
+    float d;
+    float zed_slices;
+    // Parameters values extracted from fitting a real FADC response
+    // gaussian electronic pulse passed through the whole chain from 
+    // transmitter boards to FADC.
+    p1 = 2.066; 
+    p2 = 1.568; 
+    p3 = 3; // This sets the peak of the pulse at x ~ 3 ADC slices
+               // It is just a safe value so that the pulse is well contained.
+    p4 = 0.00282; 
+    p5 = 0.04093; 
+    p6 = 0.2411; 
+    p7 = -0.009442;
+    // Define the time before trigger to read FADC signal when it
+    //  has to be written
+    fadc_time_offset = trigger_delay-p3*WIDTH_FADC_TIMESLICE; // ns
+
+    for (i=0; i< RESPONSE_SLICES_MFADC ; i++ ) {  
+      x = i * dX + dX2;
+
+      zed_slices=x/WIDTH_FADC_TIMESLICE-p3;
+      d=(zed_slices>0)?0.5:-0.5;
+
+      sing_resp_outer[i] = (p1*exp(-p2*(exp(-p2*zed_slices)+zed_slices))+p4+
+			    p5*exp(-p2*(exp(-p2*zed_slices)+p6*zed_slices))+p7*d);
+      response_sum_outer += sing_resp_outer[i];
+      }
+    break;
+  default:
+    cout<<"[MFadc] MFadc::MFadc : Shape of FADC pulse for inner pixel unknown."
+	<<endl;
+    cout<<"[MFadc] MFadc::MFadc : Exiting Camera ..."
+	<<endl;
+    exit(1);
+  }
+
+  //   
+  // Normalize responses to values set trhough input card: (= set gain of electronic chain)
+  // Take into account that only 1 of every SUBBINS bins of sing_resp[] will be "sampled" by 
+  // the FADC, so we have to correct for this to get the right "FADC integral" (=integ_resp) 
+  // per photoelectron:
+  //
+
   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. After this, the integral
-    //   of the response will be integ_resp.
-    //
-    sing_resp[i] = integ_resp / sqrt(2*3.1415926*sigma*sigma)*  
-       expf(-0.5 * (x-x0)*(x-x0) / (sigma*sigma) ) ; 
-
-    //   
-    //   The integral of the response above would be the sum of all
-    //   sing_resp[i] values times the bin width WIDTH_RESPONSE_MFADC,
-    //   and it would now equal "integ_resp". 
-    //   We want however that our actual measurement, the sum of FADC 
-    //   slices contents, is equal to integ_resp. Since in each FADC 
-    //   slice we will put the content of just one response bin, and 
-    //   there are a number SUBBINS of such response bins within 1 FADC 
-    //   slice, the needed factor is then:
-    //
-    sing_resp[i] *= (WIDTH_RESPONSE_MFADC*SUBBINS);
-
-  } 
-
-
-  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 After this, the integral
-    //   of the response will be integ_resp.
-    //
-    sing_resp_outer[i] = integ_resp_outer / sqrt(2*3.1415926*sigma*sigma)*  
-       expf(-0.5 * (x-x0)*(x-x0) / (sigma*sigma) ) ; 
-
-    //   
-    //   The integral of the response above would be the sum of all
-    //   sing_resp[i] values times the bin width WIDTH_RESPONSE_MFADC,
-    //   and it would now equal "integ_resp". 
-    //   We want however that our actual measurement, the sum of FADC 
-    //   slices contents, is equal to integ_resp. Since in each FADC 
-    //   slice we will put the content of just one response bin, and 
-    //   there are a number SUBBINS of such response bins within 1 FADC 
-    //   slice, the needed factor is then:
-    //  
-    sing_resp_outer[i] *= (WIDTH_RESPONSE_MFADC*SUBBINS);
-  } 
+      sing_resp[i] *= integ_resp / response_sum_inner * SUBBINS;
+      sing_resp_outer[i] *= integ_resp_outer / response_sum_outer * SUBBINS;
+  }
 
   //
@@ -156,6 +248,6 @@
     //
     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));
+    memset(output, 0, CAMERA_PIXELS*FADC_SLICES*sizeof(Float_t));
+    memset(output_lowgain, 0, CAMERA_PIXELS*FADC_SLICES*sizeof(Float_t));
     // 
     // Added 15 01 2004, AM:
@@ -380,5 +472,5 @@
   // Adds signals to the fadc reponse from a given array
   //
-  // parameter is the number of the pixel and the values to be added
+  // parameters are the number of the pixel and the values to be added
   //
   //
@@ -529,14 +621,17 @@
 }
 
-void MFadc::SetElecNoise(Float_t value){
+void MFadc::SetElecNoise(Float_t value1, Float_t value2, UInt_t n_in_pix){
 
   UInt_t i;
  
+  fInnerPixelsNum = n_in_pix;
+
   cout<<"MFadc::SetElecNoise ... generating database for electronic noise."
       <<endl;
 
   for (i=0;i<(UInt_t (SLICES_MFADC))*1001;i++){
-    noise[i]=GenElec->Gaus(0., value  );
-      }
+    noise[i]=GenElec->Gaus(0., value1 );
+    noise_outer[i]=GenElec->Gaus(0., value2 );
+  }
 
   cout<<"MFadc::SetElecNoise ... done"<<endl;
@@ -544,41 +639,47 @@
 }
 
-void MFadc::ElecNoise(Float_t value) {
+void MFadc::ElecNoise() {
   // ============================================================
   //
-  //    adds the noise due to optronic and electronic 
-  //    to the signal
+  //    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.
   //
   UInt_t startslice;
 
-    for ( Int_t i = 0 ; i < numpix; i++) {
-	//
-	//  but at the beginning we must check if this pixel is
-	//  hitted the first time
-	//
-
-      startslice=GenElec->Integer(((Int_t)SLICES_MFADC)*1000);
-
-	if ( used[i] == FALSE ) {
-	    used [i] = TRUE ; 
-	    
-      	    memcpy( (Float_t*)&sig[i][0],
+  for ( Int_t i = 0 ; i < numpix; i++) {
+    //
+    //  but at the beginning we must check if this pixel is
+    //  hitted the first time
+    //
+
+    startslice=GenElec->Integer(((Int_t)SLICES_MFADC)*1000);
+
+    if ( used[i] == FALSE )
+    {
+	used [i] = TRUE ; 
+	if (i < fInnerPixelsNum)
+	    memcpy( (Float_t*)&sig[i][0],
 		    (Float_t*)&noise[startslice], 
 		    ((Int_t) SLICES_MFADC)*sizeof(Float_t));
-      
-	  for ( Int_t is=0 ; is< (Int_t)SLICES_MFADC ; is++ ) {
-
-	  }
-	}
-	//      
-	//  Then the noise is introduced for each time slice
-	//
 	else
-	  for ( Int_t is=0 ; is< (Int_t)SLICES_MFADC ; is++ ) {
-	    
-	    sig[i][is] += noise[startslice+is] ; 
-	    
-	  }
-    }
+	    memcpy( (Float_t*)&sig[i][0],
+		    (Float_t*)&noise_outer[startslice], 
+		    ((Int_t) SLICES_MFADC)*sizeof(Float_t));
+    }
+
+    //      
+    //  If pixel already in use, the noise is added each time slice
+    //
+    else
+    {
+	if (i < fInnerPixelsNum)
+	    for ( Int_t is=0 ; is< (Int_t)SLICES_MFADC ; is++ ) 
+		sig[i][is] += noise[startslice+is];
+	else
+	    for ( Int_t is=0 ; is< (Int_t)SLICES_MFADC ; is++ ) 
+		sig[i][is] += noise_outer[startslice+is];
+    }
+  }
 }
 
@@ -603,39 +704,26 @@
   // ============================================================
   //
-  //    adds the noise due to optronic and electronic 
-  //    to the signal
+  //    adds the noise due to FADC electronics to the signal. This
+  //    noise affects equally the high and low gain branches, that is, 
+  //    it is not scaled down in the low gain branch.
   //
   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])));
+  for ( Int_t i = 0 ; i < numpix; i++) 
+    {
+      if ( used[i] == FALSE )
+	continue;
+
+      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++ ) 
+	{ 
+	  output[i][is] += digital_noise[startslice+is];
+	  output_lowgain[i][is] += digital_noise[startslice+FADC_SLICES+is];
 	}
-      }
-    }
-}
-
+    }
+}
 
 void MFadc::Scan() {
@@ -726,10 +814,14 @@
 }
 
-Float_t MFadc::GetPedestalNoise( Int_t pix, Int_t ishigh) {
-  // ============================================================
-  //
-  //    computes the pedestal sigma for channel pix
-
-  Float_t sigma=0;
+//===========================================================================
+//
+// 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.
+//
+Float_t MFadc::AddNoiseInSlices( Int_t pix, Int_t ishigh, Int_t n_slices) {
+
+  Float_t sum=0;
+  Float_t fvalue = 0.;
   UChar_t value=0;
   
@@ -737,38 +829,35 @@
   UInt_t startslice;
 
+  //
+  // 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)
+  //
   factor=(ishigh?1.0:high2low_gain);
 
+  //
+  // Get at random a point in the FADC presimulated digital noise:
+  //
   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]));
-    }
-    sigma+=((Float_t)value-pedestal[pix])*((Float_t)value-pedestal[pix]);
-
-  }
-
-  sigma=sqrt(sigma/(SLICES_MFADC-1));
-
-  return sigma;
-}
+  for ( Int_t is=0; is < n_slices ; is++ ) 
+    {
+	fvalue = pedestal[pix]+(sig[pix][is]-pedestal[pix])/factor;
+	fvalue += digital_noise[startslice+is];
+
+	fvalue = fvalue < 0? fvalue-0.5 : fvalue+0.5;
+
+	value = fvalue < 0.? (UChar_t) 0 :
+	  (fvalue > 255.? 255 : (UChar_t) fvalue);
+
+
+      // Add up slices:
+      sum += value - pedestal[pix];
+    }
+
+  return sum;
+}
+
+//=======================================================================
 
 void MFadc::TriggeredFadc(Float_t time) {
@@ -786,50 +875,42 @@
   iFirstSlice = (Int_t) ( 0.5 + time /  WIDTH_FADC_TIMESLICE ) ; 
 
-  for ( Int_t ip=0; ip<numpix; ip++ ) {
-    
-      if ( used[ip] == kTRUE ) { 
-	  i=0;
-	  for ( Int_t is=iFirstSlice ; is < (iFirstSlice+FADC_SLICES) ; is++ ) 
-	  {
-	      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])/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)
-	      {
-		  output[ip][i]= (UChar_t)(pedestal[ip]+0.5);
-		  output_lowgain[ip][i]= (UChar_t)(pedestal[ip]+0.5);
-		  i++;
-	      }
-	      else 
-	      {
-		  output[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++;
-	      }
-	  }
-      }
-      else  
-	  // Pixels with no C-photons in the case that camera is run with
-	  // no noise (nor NSB neither electronic)
-      {
+
+  for ( Int_t ip=0; ip<numpix; ip++ )
+    {
+
+      if ( used[ip] == kFALSE)
+	// Pixels with no C-photons, in the case that camera is being run with
+	// no noise (nor NSB neither electronic). We then set the mean pedestal as
+	// signal, since when analyzing the camera output file, MARS will subtract 
+	// it anyway!
+	{
 	  for ( Int_t i=0 ; i < FADC_SLICES ; i++ ) 
-	  {
-	      output[ip][i]= (UChar_t)(pedestal[ip]+0.5);
-	      output_lowgain[ip][i]= (UChar_t)(pedestal[ip]+0.5);
-	  } 
-      }
-  }
-
-} 
+	    {
+	      output[ip][i]= pedestal[ip];
+	      output_lowgain[ip][i]= pedestal[ip];
+	    }
+	  continue;
+	}
+
+      i=0;
+      for ( Int_t is=iFirstSlice ; is < (iFirstSlice+FADC_SLICES) ; is++ ) 
+	{
+	  if (is < (Int_t)SLICES_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;
+	    }
+	  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++;
+	}
+    }
+}
+
 
 void MFadc::ShowSignal (MMcEvt *McEvt, Float_t trigTime) { 
@@ -856,5 +937,5 @@
   // the list of analog signal histograms
   // at the beginning we initalise 10 elements
-  // but this array expand automaticly if neccessay
+  // but this array expand automatically if neccessay
   
   Int_t ic = 0 ; 
@@ -865,5 +946,5 @@
       sprintf (name, "fadc signal %d", i ) ; 
       
-      hist = new TH1F(dumm, name, SLICES_MFADC, fadc_time_offset, TOTAL_TRIGGER_TIME+fadc_time_offset); 
+      hist = new TH1F(dumm, name, (Int_t)SLICES_MFADC, fadc_time_offset, TOTAL_TRIGGER_TIME+fadc_time_offset); 
       //
       //  fill the histogram
@@ -911,5 +992,14 @@
   //  time slice which would be read.
 
-  return (output[pixel][slice]);
+  //  Since May 1 2004, we do the rounding and the truncation to the range 
+  //  0-255 counts here. (A. Moralejo)
+
+  Float_t out = output[pixel][slice] > 0. ? 
+    output[pixel][slice]+0.5 : output[pixel][slice]-0.5; 
+  // (add or subtract 0.5 for correct rounding)
+
+  return (out < 0.? (UChar_t) 0 :
+	  (out > 255.? (UChar_t) 255 : 
+	   (UChar_t) out));
 }
 
@@ -918,9 +1008,16 @@
  
    //  It returns the analog signal for a given pixel and a given FADC
-   //  time slice which would be read.
+   //  time slice which would be read. Same comment as above.
  
-   return (output_lowgain[pixel][slice]);
-}
-
-
-
+  Float_t outlow = output_lowgain[pixel][slice] > 0. ? 
+    output_lowgain[pixel][slice]+0.5 : 
+    output_lowgain[pixel][slice]-0.5; 
+  // (add or subtract 0.5 for correct rounding)
+
+  return (outlow < 0.? (UChar_t) 0 :
+	  (outlow > 255.? (UChar_t) 255 : 
+	   (UChar_t) outlow));
+}
+
+
+
Index: trunk/MagicSoft/Simulation/Detector/include-MFadc/MFadc.hxx
===================================================================
--- trunk/MagicSoft/Simulation/Detector/include-MFadc/MFadc.hxx	(revision 5078)
+++ trunk/MagicSoft/Simulation/Detector/include-MFadc/MFadc.hxx	(revision 5079)
@@ -49,4 +49,5 @@
  private:
   Int_t numpix;
+  Int_t fInnerPixelsNum; // number of inner (small) pixels.
   //
   //    then for all pixels the shape of all the analog signals 
@@ -55,15 +56,23 @@
   Float_t  pedestal[CAMERA_PIXELS] ;  //  Pedestal of FADCs
 
-  Float_t  sig[CAMERA_PIXELS][(Int_t) SLICES_MFADC] ; //  the analog signal for pixels
+  Float_t  sig[CAMERA_PIXELS][(Int_t) SLICES_MFADC] ; //  the analog signal for pixels, in bins of width
+                                                      //  equal to the FADC slice: 50/15 ns, but with a
+                                                      //  total of SLICES_MFADC (=48 now) bins.
   Float_t noise[((Int_t) SLICES_MFADC)*1001];
+  Float_t noise_outer[((Int_t) SLICES_MFADC)*1001];
   Int_t digital_noise[((Int_t) SLICES_MFADC)*1001];
 
-  UChar_t  output[CAMERA_PIXELS][FADC_SLICES];  //  the analog signal for pixels that is read after a trigger occurs (high gain).
-
-  UChar_t  output_lowgain[CAMERA_PIXELS][FADC_SLICES]; //  analog signal, low gain.
+  Float_t  output[CAMERA_PIXELS][FADC_SLICES];  //  the analog signal for pixels that is read after a trigger 
+                                                //  occurs (high gain). Only 15 (=FADC_SLICES) bins
+
+  Float_t  output_lowgain[CAMERA_PIXELS][FADC_SLICES]; //  analog signal, low gain.
+
   Float_t high2low_gain;
   //
   //    first the data for the response function
   //
+  Int_t shape_resp ;                      // index shape of the phe_response function
+                                          // = 0 --> Gaussian
+                                          // = 1 --> Pulpo set-up
   Float_t fwhm_resp ;                      // fwhm of the phe_response function (in ns)
   Float_t integ_resp ;                      // area below curve of the phe_response function (in counts * ns)
@@ -73,4 +82,7 @@
   //    We may end up with a different reponse for the outer pixels
   //
+  Int_t shape_resp_outer ;                      // index shape of the phe_response function
+                                          // = 0 --> Gaussian
+                                          // = 1 --> Pulpo set-up
   Float_t fwhm_resp_outer ;                      // fwhm of the phe_response function (in ns)
   Float_t integ_resp_outer ;                      // area below curve of the phe_response function (in counts * ns)
@@ -90,6 +102,8 @@
 
   MFadc(Int_t pix=577,
+	Int_t shape=0,
 	Float_t ampl=MFADC_RESPONSE_INTEGRAL, 
 	Float_t fwhm=MFADC_RESPONSE_FWHM,
+	Int_t shapeout=0,
 	Float_t amplout=MFADC_RESPONSE_INTEGRAL, 
 	Float_t fwhmout=MFADC_RESPONSE_FWHM,
@@ -114,4 +128,9 @@
   void SetPedestals( Float_t *ped);
 
+  void SetShape( Int_t inner, Int_t outer){
+    shape_resp=inner;
+    shape_resp_outer=outer;
+  }
+
   void SetFwhm( Float_t fwhm){
     fwhm_resp=fwhm;
@@ -136,7 +155,7 @@
   void Offset( Float_t, Int_t );
 
-  void SetElecNoise(Float_t value=2.0);
-
-  void ElecNoise(Float_t value=2.0) ; 
+  void SetElecNoise(Float_t value1=2.0, Float_t value2=2.0, UInt_t ninpix=CAMERA_PIXELS);
+
+  void ElecNoise(); 
 
   void SetDigitalNoise(Float_t value=2.0);
@@ -154,4 +173,6 @@
   Float_t GetPedestalNoise (Int_t pix, Int_t ishigh);
 
+  Float_t AddNoiseInSlices( Int_t pix, Int_t ishigh, Int_t n_slices);
+
   void TriggeredFadc(Float_t time);
 
@@ -163,4 +184,8 @@
 
   void SetHigh2LowGain(Float_t h2l) {high2low_gain=h2l;}
+
+  Float_t GetShape() {
+    return shape_resp;
+  }
 
   Float_t GetIntegral() {
