Index: trunk/MagicSoft/Simulation/Detector/include-MFadc/MFadc.cxx
===================================================================
--- trunk/MagicSoft/Simulation/Detector/include-MFadc/MFadc.cxx	(revision 5099)
+++ trunk/MagicSoft/Simulation/Detector/include-MFadc/MFadc.cxx	(revision 5248)
@@ -20,5 +20,8 @@
 using namespace std;
 
-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) {
+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, Float_t fadc_slices_per_ns,
+	     Int_t fadc_slices_written) {
   //
   //  Constructor overloaded II 
@@ -45,9 +48,28 @@
   fwhm_resp_outer = fwhmout; 
   integ_resp_outer = integralout; 
-  shape_resp=shape;
-  shape_resp_outer=shapeout;
-
-    cout<< "[MFadc]  Setting up the MFadc with this values "<< endl ; 
-    cout<< "[MFadc]    - Inner pixels :  "<< endl ; 
+  shape_resp = shape;
+  shape_resp_outer = shapeout;
+  fFadcSlicesPerNanosec = fadc_slices_per_ns;
+  fFadcSlices = fadc_slices_written;
+
+  fSlices_mFadc = (Int_t)(TOTAL_TRIGGER_TIME*fFadcSlicesPerNanosec);
+
+  for (Int_t i = 0; i < CAMERA_PIXELS; i++)
+    sig[i] = new Float_t[fSlices_mFadc];
+
+  noise = new Float_t[fSlices_mFadc*1001];
+  noise_outer = new Float_t[fSlices_mFadc*1001];
+  digital_noise = new Float_t[fSlices_mFadc*1001];
+
+  for (Int_t i = 0; i < CAMERA_PIXELS; i++)
+    {
+      output[i] = new Float_t[fFadcSlices];
+      output_lowgain[i] = new Float_t[fFadcSlices];
+    }
+
+  cout<< "[MFadc]  Setting up the MFadc with this values "<< endl ; 
+  cout<< "[MFadc]  FADC sampling frequency: " << fFadcSlicesPerNanosec << " GHz" << endl ; 
+  cout<< "[MFadc]    - Inner pixels :  "<< endl ; 
+
   switch(shape_resp){
   case 0:
@@ -82,4 +104,21 @@
   //    set up the response shape
   // 
+
+  //
+  // First select number of bins for the histogram which will contain the single 
+  // photoelectron response of the FADC. The width of these bins is smaller than that 
+  // of the real FADC slices by a factor SUBBINS (see MFadcDefine.h):
+  //
+  if (shape_resp == 1)
+    fResponseSlicesFadc = (Int_t)(50.*fFadcSlicesPerNanosec*SUBBINS); 
+  // 50 ns range
+
+  else
+    fResponseSlicesFadc = (Int_t)(7*fwhm_resp/2.35*fFadcSlicesPerNanosec*SUBBINS); 
+  // 7 sigma range
+
+  sing_resp = new Float_t[fResponseSlicesFadc];
+  sing_resp_outer = new Float_t[fResponseSlicesFadc];
+
   Int_t  i ;
   
@@ -92,5 +131,5 @@
   response_sum_outer = 0.;
 
-  dX  = WIDTH_FADC_TIMESLICE / SUBBINS ;   // Units: ns
+  dX  = 1. / fFadcSlicesPerNanosec / SUBBINS ;   // Units: ns
   dX2 = dX/2. ; 
 
@@ -102,12 +141,12 @@
     fadc_time_offset = trigger_delay-x0; // ns
   
-    for (i=0; i< RESPONSE_SLICES_MFADC ; i++ ) {  
-
-      x = i * dX + dX2 ; 
+    for (i = 0; i < fResponseSlicesFadc ; i++ ) 
+      {  
+	x = i * dX + dX2 ; 
       
-      sing_resp[i] = expf(-0.5 * (x-x0)*(x-x0) / (sigma*sigma) ) ; 
-
-      response_sum_inner += sing_resp[i];
-    }
+	sing_resp[i] = (Float_t)(expf(-0.5*(x-x0)*(x-x0)/(sigma*sigma))); 
+
+	response_sum_inner += sing_resp[i];
+      }
 
     break;
@@ -121,5 +160,5 @@
     p1 = 2.066; 
     p2 = 1.568; 
-    p3 = 3; // This sets the peak of the pulse at x ~ 3 ADC slices
+    p3 = 3; // This will set the peak of the pulse at x ~ 3*3.3 = 10 ns
             // It is just a safe value so that the pulse is well contained.
     p4 = 0.00282; 
@@ -127,20 +166,34 @@
     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];
-    }
+
+    // Now define the time before trigger to read FADC signal when it
+    // has to be written. Here FADC_SLICES_PER_NSEC (=0.3) is the value
+    // for the 300 MHz MAGIC FADCs and must NOT be changed, even if you
+    // use a faster sampling in the simulation (through the input card 
+    // command "fadc_GHz"), because this is just a conversion of units. The
+    // parameters of the "pulpo" pulse shape were obtained with the 300 MHz
+    // FADC and so we convert the time parameter to units of 3.3 ns slices 
+    // just to use the provided parametrization, and no matter what sampling 
+    // frequency we are simulating!
+
+    fadc_time_offset = trigger_delay - p3 / FADC_SLICES_PER_NSEC; // ns
+
+    for (i=0; i< fResponseSlicesFadc ; i++ )
+      {
+	x = i * dX + dX2;
+
+	// x has to be converted from ns to units FADC slices of the default 
+	// FADC of 300 MHz (these are just units, and must be these even if you
+	// are using another sampling frequency!):
+	//
+	zed_slices = x * FADC_SLICES_PER_NSEC - p3;
+	d=(zed_slices>0)?0.5:-0.5;
+
+	sing_resp[i] =  (Float_t) (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;
@@ -162,16 +215,17 @@
     fadc_time_offset = trigger_delay-x0; // ns
     
-    for (i=0; i< RESPONSE_SLICES_MFADC ; i++ ) {  
+    for (i = 0; i < fResponseSlicesFadc ; i++ ) 
+      {  
+	x = i * dX + dX2 ; 
       
-      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];
-    } 
+	//
+	//   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] = (Float_t) (expf(-0.5 * (x-x0)*(x-x0) / 
+					     (sigma*sigma)) ) ; 
+	response_sum_outer += sing_resp_outer[i];
+      } 
     break;
   case 1:
@@ -184,23 +238,39 @@
     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.
+    p3 = 3; // This sets the peak of the pulse at x ~ 3*3.3 = 10 nanosec
+            // 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];
+
+    // Now define the time before trigger to read FADC signal when it
+    // has to be written. Here FADC_SLICES_PER_NSEC (=0.3) is the value
+    // for the 300 MHz MAGIC FADCs and must NOT be changed, even if you
+    // use a faster sampling in the simulation (through the input card 
+    // command "fadc_GHz"), because this is just a conversion of units. The
+    // parameters of the "pulpo" pulse shape were obtained with the 300 MHz
+    // FADC and so we convert the time parameter to units of 3.3 ns slices 
+    // just to use the provided parametrization, and no matter what sampling 
+    // frequency we are simulating!
+
+    fadc_time_offset = trigger_delay - p3 / FADC_SLICES_PER_NSEC; // ns
+
+    for (i=0; i< fResponseSlicesFadc ; i++ )
+      {
+	x = i * dX + dX2;
+
+	// x has to be converted from ns to units FADC slices of the default 
+	// FADC of 300 MHz (these are just units, and must be these even if you
+	// are using another sampling frequency!):
+	//
+	zed_slices = x * FADC_SLICES_PER_NSEC - p3;
+	d=(zed_slices>0)?0.5:-0.5;
+
+	sing_resp_outer[i] = (Float_t) (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;
@@ -220,8 +290,9 @@
   //
 
-  for (i=0; i< RESPONSE_SLICES_MFADC ; i++ ) {  
+  for (i=0; i< fResponseSlicesFadc ; i++ )
+    {  
       sing_resp[i] *= integ_resp / response_sum_inner * SUBBINS;
       sing_resp_outer[i] *= integ_resp_outer / response_sum_outer * SUBBINS;
-  }
+    }
 
   //
@@ -250,10 +321,16 @@
     //
     memset(used, 0, CAMERA_PIXELS*sizeof(Bool_t));
-    memset(output, 0, CAMERA_PIXELS*FADC_SLICES*sizeof(Float_t));
-    memset(output_lowgain, 0, CAMERA_PIXELS*FADC_SLICES*sizeof(Float_t));
+
+    for (Int_t i = 0; i < CAMERA_PIXELS; i++)
+      {
+	memset(output[i], 0, fFadcSlices*sizeof(Float_t));
+	memset(output_lowgain[i], 0, fFadcSlices*sizeof(Float_t));
+      }
+
     // 
     // Added 15 01 2004, AM:
     //
-    memset(sig, 0, (Int_t)(CAMERA_PIXELS*SLICES_MFADC*sizeof(Float_t)));
+    for (Int_t i = 0; i < CAMERA_PIXELS; i++)
+      memset(sig[i], 0, (Int_t)(fSlices_mFadc*sizeof(Float_t)));
 }
 void MFadc::Fill( Int_t iPix, Float_t time, 
@@ -277,8 +354,12 @@
   // fills the information about one single Phe in the Trigger class
   //
-  // parameter is the number of the pixel and the time-difference to the
-  // first particle
-  //
-  //
+  // Parameters are the number of the pixel and the time-difference to the
+  // first photon.
+  //
+  //
+  // AM, Jan 2004: Replaced former FADC simulation (integration of signal)
+  // with a more realistic one (measuring signal height at discrete points).
+  //
+
 
   Int_t i, ichan, ichanfadc ; 
@@ -286,44 +367,43 @@
   //
   //   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 this is the first use, reset all signal for that pixel
   // 
-  if ( iPix > numpix ) {
-    cout << " WARNING:  MFadc::Fill() :  iPix greater than Pixels in Camera = "
-	 << numpix
-	 << endl ;
-    exit(987) ; 
-  }
-
-  if ( used[iPix] == FALSE ) {
-    used [iPix] = TRUE ; 
+  if ( iPix > numpix ) 
+    {
+      cout << " WARNING: MFadc::Fill() : iPix greater than Pixels in Camera = "
+	   << numpix
+	   << 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) 
+      for (i=0; i < (Int_t) fSlices_mFadc; i++ ) 
+	sig[iPix][i] = 0.; 
+    }
+
+  //
+  //   then select the time slice to use (ichan) 
   // 
 
   if ( time < TOTAL_TRIGGER_TIME+fadc_time_offset ) { 
     //
-    //   determine the slices number assuming the WIDTH_RESPONSE_MFADC
-    //   ichan marks the start of the pulse, in number of bins of width  
-    //   WIDTH_RESPONSE_MFADC (2/3 of a ns), measured from the start of the 
-    //   FADC.
-    //
-
-    ichan = (Int_t) ( time / ((Float_t) WIDTH_RESPONSE_MFADC )); 
+    // Convert time into units of the width of the analog 
+    // signal histogram, sing_resp:
+    //
+    ichan = (Int_t) ( time * fFadcSlicesPerNanosec * SUBBINS); 
 
     //
     //   putting the response slices in the right sig slices.
-    //   Be carefull, because both slices have different widths. 
-    //
-
-    //
-    // AM, Jan 2004: Replaced former FADC simulation (integration of signal)
-    // with a more realistic one (measuring signal height at discrete points).
-    //
+    //   Be careful, because both slices have different widths. 
+    //
+
+    // We want to put the single phe response given by sing_resp into the
+    // array sig[][], but only one of each SUBBINS bins, since the binning 
+    // of sing_resp is finer than that of sig[][]. We want that the start of
+    // sing_resp coincides with the time "time" with respect to the begining
+    // of sig[][]
 
     // We take the pulse height in the middle of FADC slices, we start in the
@@ -332,26 +412,28 @@
 
     Int_t first_i =  Int_t(SUBBINS/2) - ichan%(Int_t)SUBBINS;
-    first_i = first_i < 0 ? (Int_t)SUBBINS+first_i : first_i;
-
-
-    for ( i = first_i ; i < (Int_t)RESPONSE_SLICES; i += (Int_t)SUBBINS) {	
-      ichanfadc = (Int_t) ((ichan+i)/SUBBINS) ; 
-      if ( ichanfadc < 0 )
+    first_i = first_i < 0 ? (Int_t)SUBBINS+first_i : first_i;    //
+    //
+    // first_i is the first bin of sing_resp which matches the center of one
+    // bin of sig[][]
+    //
+
+    for ( i = first_i ; i < (Int_t)fResponseSlicesFadc; i += (Int_t)SUBBINS)
+      {	
+	ichanfadc = (Int_t) ((ichan+i)/SUBBINS) ; 
+	if ( ichanfadc < 0 )
 	  continue;
 
-      //
-      // SLICES_MFADC is by default 48. sig[][] is not the true FADC, which
-      // is filled from sig[][] in MFadc::TriggeredFadc()
-      //
-      if ( (ichanfadc) < (Int_t)SLICES_MFADC ) {  
-	sig[iPix][ichanfadc] += (amplitude * sing_resp[i] )  ; 
-      } 
-    }
-
-  }
-  else {
+	//
+	// fSlices_mFadc is by default 48. sig[][] is not the true FADC, which
+	// is filled (from sig[][]) in MFadc::TriggeredFadc()
+	//
+	if ( (ichanfadc) < (Int_t) fSlices_mFadc )  
+	  sig[iPix][ichanfadc] += (amplitude * sing_resp[i] ); 
+      }
+
+  }
+  else
     cout << "  WARNING!  Fadc::Fill " << time << "  out of TriggerTimeRange " 
 	 << TOTAL_TRIGGER_TIME+fadc_time_offset << endl ; 
-  }
 
 }
@@ -363,68 +445,43 @@
   // for an outer pixel
   //
-  // parameter is the number of the pixel and the time-difference to the
-  // first particle
-  //
+  // See explanations of the code in function Fill() above 
   //
 
   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 > numpix ) {
-    cout << " WARNING:  MFadc::FillOuter() :  iPix greater than CAMERA_PIXELS"
-	 << endl ;
-    exit(987) ; 
-  }
-
-  if ( used[iPix] == FALSE ) {
-    used [iPix] = TRUE ; 
+  if ( iPix > numpix ) 
+    {
+      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) 
-  // 
+      for (i=0; i < (Int_t) fSlices_mFadc; i++)
+	sig[iPix][i] = 0.;
+    }
 
 
   if ( time < TOTAL_TRIGGER_TIME+fadc_time_offset ) { 
-    //
-    //   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. 
-    //
-
-    //
-    // AM, Jan 2004: Replaced former FADC simulation (integration of signal)
-    // with a more realistic one (measuring signal height at discrete points).
-    //
-
-    // We take the pulse height in the middle of FADC slices, we start in the
-    // first such point after the time "time" (=ichan in response bins). Each
-    // FADC slice corresponds to SUBBINS response bins (SUBBINS=5 by default).
+
+    ichan = (Int_t) ( time * fFadcSlicesPerNanosec * SUBBINS); 
 
     Int_t first_i =  Int_t(SUBBINS/2) - ichan%(Int_t)SUBBINS;
     first_i = first_i < 0 ? (Int_t)SUBBINS+first_i : first_i;
 
-    for ( i = first_i ; i < (Int_t)RESPONSE_SLICES; i += (Int_t)SUBBINS) {
-      ichanfadc = (Int_t) ((ichan+i)/SUBBINS) ; 
-
-      if ( ichanfadc < 0 )
+    for ( i = first_i ; i < (Int_t)fResponseSlicesFadc; i += (Int_t)SUBBINS)
+      {
+	ichanfadc = (Int_t) ((ichan+i)/SUBBINS); 
+
+	if ( ichanfadc < 0 )
 	  continue;
 
-      if ( (ichanfadc) < (Int_t)SLICES_MFADC ) {  
-	sig[iPix][ichanfadc] += (amplitude * sing_resp_outer[i] )  ; 
-      } 
-    }
-
+	if ( (ichanfadc) < (Int_t)fSlices_mFadc )
+	  sig[iPix][ichanfadc] += (amplitude * sing_resp_outer[i] ); 
+      }
+    
   }
   else {
@@ -432,8 +489,8 @@
 	 << TOTAL_TRIGGER_TIME+fadc_time_offset << endl ; 
   }
-
-}
-
-void MFadc::Set( Int_t iPix, Float_t resp[(Int_t) SLICES_MFADC]) { 
+  
+}
+
+void MFadc::Set( Int_t iPix, Float_t *resp) { 
   
   //
@@ -459,9 +516,9 @@
     used [iPix] = TRUE ; 
     
-    for (i=0; i < (Int_t)SLICES_MFADC; i++ ) {
+    for (i=0; i < (Int_t)fSlices_mFadc; i++ ) {
       sig[iPix][i] = 0. ; 
     }
   }
-  for ( i = 0 ; i<(Int_t)SLICES_MFADC; i++ ) {
+  for ( i = 0 ; i<(Int_t)fSlices_mFadc; i++ ) {
     sig[iPix][i] = resp[i] ; 
   }
@@ -469,5 +526,5 @@
 }
 
-void MFadc::AddSignal( Int_t iPix, Float_t resp[(Int_t) SLICES_MFADC]) { 
+void MFadc::AddSignal( Int_t iPix, Float_t *resp) { 
   
   //
@@ -493,9 +550,9 @@
     used [iPix] = TRUE ; 
     
-    for (i=0; i < (Int_t)SLICES_MFADC; i++ ) {
+    for (i=0; i < (Int_t)fSlices_mFadc; i++ ) {
       sig[iPix][i] = 0. ; 
     }
   }
-  for ( i = 0 ; i<(Int_t)SLICES_MFADC; i++ ) {
+  for ( i = 0 ; i<(Int_t)fSlices_mFadc; i++ ) {
     sig[iPix][i] += resp[i] ; 
   }
@@ -534,9 +591,9 @@
   for(j=0;j<numpix;j++){
     baseline=0.0;
-    for(i=0;i<(Int_t) SLICES_MFADC;i++){
+    for(i=0;i<(Int_t) fSlices_mFadc;i++){
       baseline+=sig[j][i];
     }
-    baseline=baseline/SLICES_MFADC;
-    for(i=0;i<(Int_t) SLICES_MFADC;i++){
+    baseline=baseline/fSlices_mFadc;
+    for(i=0;i<(Int_t) fSlices_mFadc;i++){
       sig[j][i]=-baseline;
     }
@@ -554,5 +611,5 @@
 
   for(i=0;i<numpix;i++)
-    for(j=0;j<(Int_t)SLICES_MFADC;j++)
+    for(j=0;j<(Int_t)fSlices_mFadc;j++)
         sig[i][j]+=pedestal[i];
   //
@@ -562,6 +619,4 @@
   // to do it for all pixels.
   //
-
-
 }
 
@@ -585,5 +640,5 @@
 	if (used[i]){
 	  fdum=(10*GenOff->Rndm());
-	  for(j=0;j<(Int_t) SLICES_MFADC;j++)
+	  for(j=0;j<(Int_t) fSlices_mFadc;j++)
 	    sig[i][j]+=fdum;
 	}
@@ -594,5 +649,5 @@
       if (used[pixel]){
 	fdum=(10*GenOff->Rndm());
-	for(j=0;j<(Int_t) SLICES_MFADC;j++)
+	for(j=0;j<(Int_t) fSlices_mFadc;j++)
 	  sig[pixel][j]+=fdum;
 	}
@@ -607,5 +662,5 @@
       for(i=0;i<numpix;i++){
 	if (used[i]){
-	  for(j=0;j<(Int_t) SLICES_MFADC;j++)
+	  for(j=0;j<(Int_t) fSlices_mFadc;j++)
 	    sig[i][j]+=offset;
 	}
@@ -615,5 +670,5 @@
 
       if (used[pixel]){
-	for(j=0;j<(Int_t) SLICES_MFADC;j++)
+	for(j=0;j<(Int_t) fSlices_mFadc;j++)
 	  sig[pixel][j]+=offset;
       }
@@ -632,5 +687,5 @@
       <<endl;
 
-  for (i=0;i<(UInt_t (SLICES_MFADC))*1001;i++){
+  for (i=0;i<(UInt_t (fSlices_mFadc))*1001;i++){
     noise[i]=GenElec->Gaus(0., value1 );
     noise_outer[i]=GenElec->Gaus(0., value2 );
@@ -656,5 +711,5 @@
     //
 
-    startslice=GenElec->Integer(((Int_t)SLICES_MFADC)*1000);
+    startslice=GenElec->Integer(((Int_t)fSlices_mFadc)*1000);
 
     if ( used[i] == FALSE )
@@ -664,9 +719,9 @@
 	    memcpy( (Float_t*)&sig[i][0],
 		    (Float_t*)&noise[startslice], 
-		    ((Int_t) SLICES_MFADC)*sizeof(Float_t));
+		    ((Int_t) fSlices_mFadc)*sizeof(Float_t));
 	else
 	    memcpy( (Float_t*)&sig[i][0],
 		    (Float_t*)&noise_outer[startslice], 
-		    ((Int_t) SLICES_MFADC)*sizeof(Float_t));
+		    ((Int_t) fSlices_mFadc)*sizeof(Float_t));
     }
 
@@ -677,8 +732,8 @@
     {
 	if (i < fInnerPixelsNum)
-	    for ( Int_t is=0 ; is< (Int_t)SLICES_MFADC ; is++ ) 
+	    for ( Int_t is=0 ; is< (Int_t)fSlices_mFadc ; is++ ) 
 		sig[i][is] += noise[startslice+is];
 	else
-	    for ( Int_t is=0 ; is< (Int_t)SLICES_MFADC ; is++ ) 
+	    for ( Int_t is=0 ; is< (Int_t)fSlices_mFadc ; is++ ) 
 		sig[i][is] += noise_outer[startslice+is];
     }
@@ -694,5 +749,5 @@
       <<endl;
 
-  for (i=0;i<UInt_t(SLICES_MFADC*1001);i++){
+  for (i=0;i<UInt_t(fSlices_mFadc*1001);i++){
     xrdm=GenElec->Gaus(0., value);
     digital_noise[i]=(xrdm>0?Int_t(xrdm+0.5):Int_t(xrdm-0.5));    
@@ -717,12 +772,12 @@
 	continue;
 
-      startslice=GenElec->Integer((Int_t) SLICES_MFADC*999);
+      startslice=GenElec->Integer((Int_t) fSlices_mFadc*999);
       //      
       //  Then the noise is introduced for each time slice
       //
-      for ( Int_t is=0 ; is< FADC_SLICES; is++ ) 
+      for ( Int_t is = 0 ; is < fFadcSlices; is++ ) 
 	{ 
 	  output[i][is] += digital_noise[startslice+is];
-	  output_lowgain[i][is] += digital_noise[startslice+FADC_SLICES+is];
+	  output_lowgain[i][is] += digital_noise[startslice+fFadcSlices+is];
 	}
     }
@@ -738,5 +793,5 @@
       printf ("Pid %3d",  ip ) ; 
 
-      for ( Int_t is=0 ; is < (Int_t)SLICES_MFADC; is++ ) {
+      for ( Int_t is=0 ; is < (Int_t)fSlices_mFadc; is++ ) {
 
 	if ( sig[ip][is] > 0. ) {
@@ -776,5 +831,5 @@
   Int_t  iFirstSlice ; 
 
-  iFirstSlice = (Int_t) ( t /  WIDTH_FADC_TIMESLICE ) ; 
+  iFirstSlice = (Int_t) ( t * fFadcSlicesPerNanosec ) ; 
 
   for ( Int_t ip=0; ip<numpix; ip++ ) {
@@ -799,8 +854,7 @@
   //    puts the standard response function into the array resp
   
-  for ( Int_t i=0; i< RESPONSE_SLICES; i++ ) {
-
-    resp[i] = sing_resp[i] ; 
-  }
+  for ( Int_t i=0; i< fResponseSlicesFadc; i++ )
+    resp[i] = sing_resp[i];
+
 }
 
@@ -841,5 +895,5 @@
   // Get at random a point in the FADC presimulated digital noise:
   //
-  startslice=GenElec->Integer((Int_t) SLICES_MFADC*999);
+  startslice=GenElec->Integer((Int_t) fSlices_mFadc*999);
 
   for ( Int_t is=0; is < n_slices ; is++ ) 
@@ -875,8 +929,7 @@
   // We had 0.5 for the correct rounding:
   //
-  iFirstSlice = (Int_t) ( 0.5 + time /  WIDTH_FADC_TIMESLICE ) ; 
-
-
-  for ( Int_t ip=0; ip<numpix; ip++ )
+  iFirstSlice = (Int_t) ( 0.5 + time * fFadcSlicesPerNanosec ) ; 
+
+  for ( Int_t ip = 0; ip < numpix; ip++ )
     {
 
@@ -887,5 +940,5 @@
 	// it anyway!
 	{
-	  for ( Int_t i=0 ; i < FADC_SLICES ; i++ ) 
+	  for ( Int_t i=0 ; i < fFadcSlices ; i++ ) 
 	    {
 	      output[ip][i]= pedestal[ip];
@@ -896,7 +949,7 @@
 
       i=0;
-      for ( Int_t is=iFirstSlice ; is < (iFirstSlice+FADC_SLICES) ; is++ ) 
+      for ( Int_t is = iFirstSlice; is < (iFirstSlice+fFadcSlices); is++ ) 
 	{
-	  if (is < (Int_t)SLICES_MFADC)
+	  if (is < (Int_t)fSlices_mFadc)
 	    {
 	      output[ip][i] = sig[ip][is];
@@ -948,12 +1001,12 @@
       sprintf (name, "fadc signal %d", i ) ; 
       
-      hist = new TH1F(dumm, name, (Int_t)SLICES_MFADC, fadc_time_offset, TOTAL_TRIGGER_TIME+fadc_time_offset); 
+      hist = new TH1F(dumm, name, (Int_t)fSlices_mFadc, fadc_time_offset, TOTAL_TRIGGER_TIME+fadc_time_offset); 
       //
       //  fill the histogram
       //
       
-      for (Int_t ibin=1; ibin <=(Int_t)SLICES_MFADC; ibin++) {
-	hist->SetBinContent (ibin, sig[i][ibin-1]) ;
-      }
+      for (Int_t ibin = 1; ibin <= (Int_t)fSlices_mFadc; ibin++)
+	hist->SetBinContent (ibin, sig[i][ibin-1]);
+
 
       //      hist->SetMaximum( 5.);
Index: trunk/MagicSoft/Simulation/Detector/include-MFadc/MFadc.hxx
===================================================================
--- trunk/MagicSoft/Simulation/Detector/include-MFadc/MFadc.hxx	(revision 5099)
+++ trunk/MagicSoft/Simulation/Detector/include-MFadc/MFadc.hxx	(revision 5248)
@@ -56,36 +56,56 @@
   Float_t  pedestal[CAMERA_PIXELS] ;  //  Pedestal of FADCs
 
-  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];
-
-  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.
+  Int_t    fSlices_mFadc;  // Number of simulated FADC slices. Larger than the actual number of slices
+                           // that will be written, since we have to account for possible delays in the 
+                           // trigger
+
+  Int_t fResponseSlicesFadc;
+
+  //
+  //  Float_t sig[CAMERA_PIXELS][] ; 
+  //  the analog signal for pixels, in bins of width
+  //  equal to the FADC slice: (default 50/15 ns), but with a
+  //  total of fSlices_mFadc bins (default 48).
+  //
+  Float_t *sig[CAMERA_PIXELS];
+
+  Float_t *noise;
+  Float_t *noise_outer;
+  Float_t *digital_noise;
+
+  Float_t  *output[CAMERA_PIXELS];  //  the analog signal for pixels that is read after a trigger 
+                                    //  occurs (high gain).
+
+  Float_t  *output_lowgain[CAMERA_PIXELS]; //  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)
-  Float_t sing_resp[ RESPONSE_SLICES_MFADC ] ;   // the shape of the phe_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)
+  Float_t *sing_resp;    // the shape of the phe_response function 
+
+  Float_t fFadcSlicesPerNanosec; // Number of FADC slices per nanosecond, that is, the
+                                 // sampling frequency of the FADC.
+
+  Int_t fFadcSlices;     // Number of FADC slices that will be written on the output 
+                         // file (same number for high and low gain)
 
   //
   //    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)
-  Float_t sing_resp_outer[ RESPONSE_SLICES_MFADC ] ;   // the shape of the phe_response function 
+  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)
+  Float_t *sing_resp_outer; // the shape of the phe_response function 
+
+
   //
   //   RandomGenerator for the Electonic Noise
@@ -98,15 +118,16 @@
                             // in the trigger pixels.
 
-
 public:
 
-  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,
-	Float_t trig_delay=0.) ;
+  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,
+	Float_t trig_delay = 0.,
+	Float_t fadc_slices_per_ns = FADC_SLICES_PER_NSEC,
+	Int_t   fadc_slices_written = FADC_SLICES);
   
   void SetSeed(UInt_t seed)  {GenElec->SetSeed(seed);}
@@ -120,7 +141,6 @@
   void FillOuter( Int_t, Float_t, Float_t  ) ;  
 
-  void Set( Int_t iPix, Float_t res[(Int_t) SLICES_MFADC]);
-
-  void AddSignal( Int_t iPix, Float_t res[(Int_t) SLICES_MFADC]);
+  void Set( Int_t iPix, Float_t *res);
+  void AddSignal( Int_t iPix, Float_t *res);
 
   void SetPedestals( Int_t ped);
@@ -185,5 +205,5 @@
   void SetHigh2LowGain(Float_t h2l) {high2low_gain=h2l;}
 
-  Float_t GetShape() {
+  Int_t GetShape() {
     return shape_resp;
   }
@@ -205,4 +225,12 @@
   }
 
+  Float_t GetFadcSlicesPerNanosec() {
+    return fFadcSlicesPerNanosec;
+  }
+
+  Int_t GetResponseSlicesFadc() {
+    return fResponseSlicesFadc;
+  }
+
   Bool_t IsPixelUsed(UInt_t p){
     return used[p];
