Index: trunk/MagicSoft/Simulation/Detector/Camera/camera.cxx
===================================================================
--- trunk/MagicSoft/Simulation/Detector/Camera/camera.cxx	(revision 387)
+++ trunk/MagicSoft/Simulation/Detector/Camera/camera.cxx	(revision 388)
@@ -21,7 +21,7 @@
 //
 // $RCSfile: camera.cxx,v $
-// $Revision: 1.7 $
+// $Revision: 1.8 $
 // $Author: blanch $ 
-// $Date: 2000-03-24 18:10:46 $
+// $Date: 2000-05-11 13:57:27 $
 //
 ////////////////////////////////////////////////////////////////////////
@@ -217,4 +217,15 @@
 //@: flag: TRUE: show all trigger singnal in the screen; FALSE: don't
 static int Trigger_Scan = FALSE;
+
+//@: flag: TRUE: loop trigger analysis over several thresholds, multiplicities and topologies; FALSE: a single trigger configuration
+static int Trigger_Loop = FALSE;
+
+//@: Upper and lower edges of the trigger loop
+static int Trigger_loop_lthres = 0;
+static int Trigger_loop_uthres = 10;
+static int Trigger_loop_lmult = 2;
+static int Trigger_loop_umult = 10;
+static int Trigger_loop_ltop = 0;
+static int Trigger_loop_utop = 2;
 
 //!@}
@@ -373,5 +384,4 @@
   MCCphoton cphoton;          //@< Cherenkov Photon class (MC)
 
-  Photoelectron *photoe = NULL; //@< array of the photoelectrons of one event
   int inumphe;                //@< number of photoelectrons in an event
 
@@ -421,4 +431,10 @@
   int itrigger;               //@< index of pixel fired
   int ntrigger = 0;           //@< number of triggers in the whole file
+  int ithrescount;            //@< counter for loop over threshold trigger
+  int imulticount;            //@< counter for loop over multiplicity trigger
+  int itopocount;             //@< counter for loop over topology trigger
+  float fpixelthres[TRIGGER_PIXELS];
+
+  float fadcValues[(Int_t) SLICES_MFADC];  //@< the analog Fadc siganl for pixels
 
   float plateScale_cm2deg;    //@< plate scale (deg/cm)
@@ -511,4 +527,6 @@
   FADC_Scan = get_FADC_Scan();
   Trigger_Scan = get_Trigger_Scan();
+  Trigger_Loop = get_Trigger_Loop(&Trigger_loop_lthres, &Trigger_loop_uthres, &Trigger_loop_lmult, &Trigger_loop_umult, &Trigger_loop_ltop, &Trigger_loop_utop);
+
 
   // get filenames
@@ -581,4 +599,16 @@
       "Energy", ONoff(Select_Energy), Select_Energy_le, Select_Energy_ue);
   
+  //  Definition and initialization of array to save trigger statistics
+ 
+  int ntriggerloop[(int) (Trigger_loop_uthres+1)][Trigger_loop_umult+1][Trigger_loop_utop+1];
+
+  for (ithrescount=Trigger_loop_lthres;ithrescount<=Trigger_loop_uthres;ithrescount++){
+    for (imulticount=Trigger_loop_lmult;imulticount<=Trigger_loop_umult;imulticount++){
+      for(itopocount=Trigger_loop_ltop;itopocount<=Trigger_loop_utop;itopocount++){
+	ntriggerloop[ithrescount][imulticount][itopocount]=0;
+      }
+    }   
+  }
+
   // set all random numbers seeds
 
@@ -605,8 +635,4 @@
 
   read_pixels(&cam);
-
-  // allocate memory for the photoelectrons
-
-  photoe = new Photoelectron[iMAXNUMPHE];
 
   Int_t Lev0, Lev1, Lev2 ; 
@@ -737,5 +763,4 @@
     k = produce_nsbrates( starfieldname,
 			  &cam,
-			  photoe, // only a dummy here
 			  nsbrate_phepns );
     if (k != 0){
@@ -835,4 +860,6 @@
 	//
       	Trigger.Reset() ; 
+	Trigger.ClearFirst();
+	Trigger.ClearZero();
 	fadc.Reset() ; 
 
@@ -928,46 +955,4 @@
 	}
 
-	// read the photons and produce the photoelectrons
-
-	k = produce_phes( inputfile,
-			  &cam,
-			  WAVEBANDBOUND1,
-			  WAVEBANDBOUND6,
-			  photoe,   // will be changed by the function!
-			  &inumphe, // important for later: the size of photoe[]
-			  fnpix,    // will be changed by the function!
-			  &ncph,    // will be changed by the function!
-			  &arrtmin_ns, // will be changed by the function!
-			  &arrtmax_ns // will be changed by the function!
-			  );
-
-	if( k != 0 ){ // non-zero returnvalue means error
-	  cout << "Exiting.\n";
-	  exit(1);
-	}
-	  
-	log(SIGNATURE, "End of this event: %d cphs(+%d). . .\n",
-	    ncph, ntcph);
-
-	ntcph += ncph;
-
-	// skip it ?
-	
-	for ( i=0; i<nSkip; ++i ) {
-	  if (Skip[i] == (nshow+ntshow)) {
-	    i = -1;
-	    break;
-	  }
-	}
-	
-	// if after the previous loop, the exit value of i is -1
-	// then the shower number is in the list of showers to be
-	// skipped
-	
-	if (i == -1) {
-	  log(SIGNATURE, "\t\tskipped!\n");
-	  continue;
-	}
-	
 	// energy cut
 	
@@ -979,4 +964,9 @@
 	  }
 	}
+
+	// Read first and last time and put inumphe to 0
+
+	mcevth.get_times(&arrtmin_ns,&arrtmax_ns);
+	inumphe=0;
 
 	// NSB simulation
@@ -992,5 +982,6 @@
 				ext,
 				fnpix,   // will be changed by the function!
-				photoe,  // will be changed by the function!
+				&Trigger,  // will be changed by the function!
+				&fadc,      // will be changed by the function!
 				&inumphe, // important for later: the size of photoe[]
 				baseline_mv // will be generated by the function
@@ -1001,126 +992,52 @@
 	    exit(1);
 	  }
-
+	  
 	}// end if(simulateNSB) ...
 
-
-//	cout << arrtmin_ns << " " << arrtmax_ns << "\n";
-//	for(i=0; i<cam.inumpixels; i++){
-//        cout << i << " " << baseline_mv[i] <<"\n";
-//      }
-
-	  cout << "Total number of phes: " << inumphe <<endl;
-	  
-	// TRIGGER HERE
-
-	//   
-	//   Put  values of the analog signal for
-	//   each pixel from photoe array
-	//
-	
-	for(i=0;i<inumphe;i++){
-	  Trigger.FillShow(photoe[i].ipixnum,float((photoe[i].iarrtime_ns-arrtmin_ns)));
-	  fadc.Fill( photoe[i].ipixnum,(photoe[i].iarrtime_ns-arrtmin_ns) , Trigger.FillShow(photoe[i].ipixnum,float((photoe[i].iarrtime_ns-arrtmin_ns)))); 
-}
-
-	//
-	//   now the noise of the electronic 
-	//   (preamps, optical transmission,..)  is introduced. 
-	//   This is done inside the class MTrigger by the method ElecNoise. 
-	//   
-	Trigger.ElecNoise() ;
-	fadc.ElecNoise() ;
-	
-
-	Trigger.Diskriminate() ; 
-
-	//
-	//   look if in all the signals in the trigger signal branch
-	//   is a possible Trigger. Therefore we habe to diskriminate all
-	//   the simulated analog signals (Method Diskriminate in class
-	//   MTrigger). We look simultanously for the moments at which
-	//   there are more than TRIGGER_MULTI pixels above the 
-	//   CHANNEL_THRESHOLD. 
-	//
-
-	McTrig->SetZeroLevel( Lev0 = (Short_t) Trigger.ZeroLevel() ) ; 
-
-	Lev1 = Lev2 = 0 ; 
-	
-	//
-	//   Start the First Level Trigger simulation
-	//
-
-	if ( Lev0 > 0 ) {
-	  McTrig->SetFirstLevel ( Lev1 = Trigger.FirstLevel() )  ;
+	// read the photons and produce the photoelectrons
+
+	k = produce_phes( inputfile,
+			  &cam,
+			  WAVEBANDBOUND1,
+			  WAVEBANDBOUND6,
+			  &Trigger,   // will be changed by the function!
+			  &fadc,      // will be changed by the function!
+			  &inumphe, // important for later: the size of photoe[]
+			  fnpix,    // will be changed by the function!
+			  &ncph,    // will be changed by the function!
+			  &arrtmin_ns, // will be changed by the function!
+			  &arrtmax_ns // will be changed by the function!
+			  );
+
+	if( k != 0 ){ // non-zero returnvalue means error
+	  cout << "Exiting.\n";
+	  exit(1);
 	}
-
-// 	for( i=0; i<inumphe; i++){
-// 	  cout << "phe " << photoe[i].ipixnum << " " << photoe[i].iarrtime_ns << "\n";
-// 	}
-	
-	//if ( trigger>0 ) {
-	if (Lev1>0){
-	
-	  itrigger = i;
-	  ++ntrigger;
-	  
-	  memcpy( fnpixclean, fnpix, sizeof(float) * ct_NPixels );
-	}
-	//
-	//  Fill the header of this event 
-	//
-	
-	Evt->FillHeader ( (UShort_t) (ntshow + nshow) ,  20 ) ; 
-	
-	//
-	//   fill the MMcEvt with all information  
-	//
-	
-	McEvt->Fill( (UShort_t) mcevth.get_primary() , 
-		     mcevth.get_energy(), 
-		     mcevth.get_theta(), 
-		     mcevth.get_phi(), 
-		     mcevth.get_core(),
-		     mcevth.get_coreX(),
-		     mcevth.get_coreY(),
-		     impactD,
-		     ulli, ulli, 
-		     (UShort_t) ncph, 
-		     ulli, 
-		     (UShort_t) ncph) ; 
-
-	//   We don not count phtons out of the camera.	
-
-	//
-	//    write it out to the file outfile
-	// 
-	
-	EvtTree.Fill() ; 
-	
-
-	//
-	//    if a first level trigger occurred, then 
-	//       1. do some other stuff (not implemented) 
-	//       2. start the gui tool
-
-	if(FADC_Scan){
-	  if ( Lev0 > 0 ) {
-	    fadc.ShowSignal( McEvt, (Float_t) 60. ) ; 
+	  
+	log(SIGNATURE, "End of this event: %d cphs(+%d). . .\n",
+	    ncph, ntcph);
+
+	ntcph += ncph;
+
+	// skip it ?
+	
+	for ( i=0; i<nSkip; ++i ) {
+	  if (Skip[i] == (nshow+ntshow)) {
+	    i = -1;
+	    break;
 	  }
 	}
-
-	if(Trigger_Scan){
-	  if ( Lev0 > 0 ) {
-	    Trigger.ShowSignal(McEvt) ;
-	  }
+	
+	// if after the previous loop, the exit value of i is -1
+	// then the shower number is in the list of showers to be
+	// skipped
+	
+	if (i == -1) {
+	  log(SIGNATURE, "\t\tskipped!\n");
+	  continue;
 	}
-
-	//    clear all
-	Evt->Clear() ; 
-	McEvt->Clear() ; 
-	McTrig->Clear() ; 
-      
-	
+	
+	cout << "Total number of phes: " << inumphe <<endl;
+
 	//++++++++++++++++++++++++++++++++++++++++++++++++++
 	// at this point we have a camera full of
@@ -1132,5 +1049,232 @@
 	// and so on).
 	//--------------------------------------------------
-	
+	  
+	// TRIGGER HERE
+
+
+	//
+	//   now the noise of the electronic 
+	//   (preamps, optical transmission,..)  is introduced. 
+	//   This is done inside the class MTrigger by the method ElecNoise. 
+	//   
+	Trigger.ElecNoise() ;
+	fadc.ElecNoise() ;
+	
+	//   We study several trigger conditons
+	if(Trigger_Loop)
+
+	  //  Loop over trigger threshold
+	  for (ithrescount=Trigger_loop_lthres;ithrescount<=Trigger_loop_uthres;ithrescount++){
+	    for (i=0;i<TRIGGER_PIXELS;i++)
+	      fpixelthres[i]=(float) ithrescount;
+	    Trigger.SetThreshold(fpixelthres);
+
+	    Trigger.Diskriminate();
+	    //
+	    //   look if in all the signals in the trigger signal branch
+	    //   is a possible Trigger. Therefore we habe to diskriminate all
+	    //   the simulated analog signals (Method Diskriminate in class
+	    //   MTrigger). We look simultanously for the moments at which
+	    //   there are more than TRIGGER_MULTI pixels above the 
+	    //   CHANNEL_THRESHOLD. 
+	    //
+
+	    //  loop over multiplicity of trigger configuration
+	    for (imulticount=Trigger_loop_lmult;imulticount<=Trigger_loop_umult;imulticount++){
+	      Trigger.SetMultiplicity(imulticount);
+	      Trigger.ClearZero();
+
+	      Lev0=(Short_t) Trigger.ZeroLevel();
+	      if (Lev0>0){
+		Lev1=Lev2=0;
+
+		//  loop over topologies
+		for(itopocount=Trigger_loop_ltop;itopocount<=Trigger_loop_utop;itopocount++){
+		  if(itopocount==0 && imulticount>7) continue;
+		  if(itopocount==2 && imulticount<3) continue;
+		  Trigger.SetTopology(itopocount);
+		  Trigger.ClearFirst();
+
+		  //
+		  //   Start the First Level Trigger simulation
+		  //
+
+		  McTrig->SetFirstLevel (Lev1=Trigger.FirstLevel());
+		  if(Lev1>0) {
+		    ntriggerloop[ithrescount][imulticount][itopocount]++;
+		    McTrig->SetTopology(itopocount);
+		    McTrig->SetMultiplicity(imulticount);
+		    McTrig->SetThreshold(fpixelthres);
+		  }
+		  if(Lev1==0 && Write_All_Images){
+		    McTrig->SetTopology(itopocount);
+		    McTrig->SetMultiplicity(imulticount);
+		    McTrig->SetThreshold(fpixelthres);
+		    Lev1=1;
+		  }
+		  for (Int_t ii=0;ii<Lev1;ii++){
+		    McTrig->SetTime(Trigger.GetFirstLevelTime(ii));
+		    McTrig->SetPixel(Trigger.GetFirstLevelPixel(ii));
+		    fadc.TriggeredFadc(Trigger.GetFirstLevelTime(ii));
+		    //
+		    //  Fill the header of this event 
+		    //
+		    
+		    Evt->FillHeader ( (UShort_t) (ntshow + nshow) ,  20 ) ; 
+		    
+		    //   fill pixel information
+		    
+		    for(i=0;i<iMAXNUMPIX;i++){
+		      for (j=0;j<SLICES_MFADC;j++){
+			fadcValues[j]=fadc.GetFadcSignal(i,j);
+		      }
+		      Evt->FillPixel(i,fadcValues);
+		    }
+		    //
+		    //   fill the MMcEvt with all information  
+		    //
+		    
+		    McEvt->Fill( (UShort_t) mcevth.get_primary() , 
+				 mcevth.get_energy(), 
+				 mcevth.get_theta(), 
+				 mcevth.get_phi(), 
+				 mcevth.get_core(),
+				 mcevth.get_coreX(),
+				 mcevth.get_coreY(),
+				 impactD,
+				 ulli, ulli, 
+				 (UShort_t) ncph, 
+				 ulli, 
+				 (UShort_t) ncph) ; 
+		    
+		    //   We don not count phtons out of the camera.	
+
+		    //
+		    //    write it out to the file outfile
+		    // 
+		    
+		    EvtTree.Fill() ; 
+		    //    clear all
+		    Evt->Clear() ; 
+		    McEvt->Clear() ;  
+		    
+		  }
+		  McTrig->Clear() ;
+		}
+	      }
+	      else break;
+	    }
+	  }
+
+	//  We study a single trigger condition
+	else {
+
+	  Trigger.Diskriminate() ; 
+
+	  //
+	  //   look if in all the signals in the trigger signal branch
+	  //   is a possible Trigger. Therefore we habe to diskriminate all
+	  //   the simulated analog signals (Method Diskriminate in class
+	  //   MTrigger). We look simultanously for the moments at which
+	  //   there are more than TRIGGER_MULTI pixels above the 
+	  //   CHANNEL_THRESHOLD. 
+	  //
+	  
+	  Lev0 = (Short_t) Trigger.ZeroLevel() ; 
+	  
+	  Lev1 = Lev2 = 0 ; 
+	  
+	  //
+	  //   Start the First Level Trigger simulation
+	  //
+	  
+	  if ( Lev0 > 0 ) {
+	    McTrig->SetFirstLevel (Lev1 = Trigger.FirstLevel());
+	  }
+	  if (Lev1>0){
+	    ++ntrigger;
+	  }
+	  if (Lev1==0 && Write_All_Images){ 
+	    Lev1=1;
+	  }
+	  McTrig->SetTopology(Trigger.GetTopology());
+	  McTrig->SetMultiplicity(Trigger.GetMultiplicity());
+	  for(i=0;i<TRIGGER_PIXELS;i++){
+	    fpixelthres[i]=Trigger.GetThreshold(i);
+	  }
+	  McTrig->SetThreshold(fpixelthres);
+
+	  for(Int_t ii=0;ii<Lev1;ii++){
+	    //  Loop over different level one triggers
+	    fadc.TriggeredFadc(Trigger.GetFirstLevelTime(ii));
+	    McTrig->SetTime(Trigger.GetFirstLevelTime(ii));
+	    McTrig->SetPixel(Trigger.GetFirstLevelPixel(ii));
+	    
+	    //
+	    //  Fill the header of this event 
+	    //
+       
+	    Evt->FillHeader ( (UShort_t) (ntshow + nshow) ,  20 ) ; 
+	    
+	    //   fill pixel information
+	    
+	    for(i=0;i<iMAXNUMPIX;i++){
+	      for (j=0;j<SLICES_MFADC;j++){
+		fadcValues[j]=fadc.GetFadcSignal(i,j);
+	      }
+	      Evt->FillPixel(i,fadcValues);
+	    }
+	    
+	    //
+	    //   fill the MMcEvt with all information  
+	    //
+	    
+	    McEvt->Fill( (UShort_t) mcevth.get_primary() , 
+			 mcevth.get_energy(), 
+			 mcevth.get_theta(), 
+			 mcevth.get_phi(), 
+			 mcevth.get_core(),
+			 mcevth.get_coreX(),
+			 mcevth.get_coreY(),
+			 impactD,
+			 ulli, ulli, 
+			 (UShort_t) ncph, 
+			 ulli, 
+			 (UShort_t) ncph) ; 
+	    
+	    //   We don not count photons out of the camera.	
+	    	    
+	    
+	    //
+	    //    write it out to the file outfile
+	    // 
+	  
+	    EvtTree.Fill() ; 
+	    
+	    
+	    //
+	    //    if a first level trigger occurred, then 
+	    //       1. do some other stuff (not implemented) 
+	    //       2. start the gui tool
+	    
+	    if(FADC_Scan){
+	      if ( Lev0 > 0 ) {
+		fadc.ShowSignal( McEvt, (Float_t) 60. ) ; 
+	      }
+	    }
+	    
+	    if(Trigger_Scan){
+	      if ( Lev0 > 0 ) {
+		Trigger.ShowSignal(McEvt) ;
+	      }
+	    }
+	    
+	    //    clear all
+	    Evt->Clear() ; 
+	    McEvt->Clear() ; 
+	  } 
+	  McTrig->Clear() ;
+	}
+		
 #ifdef __DEBUG__  
 	printf("\n");
@@ -1165,19 +1309,4 @@
 	  	  	  
 	
-	//!@' @#### Save data.
-	//@'
-	
-	//++++++++++++++++++++++++++++++++++++++++++++++++++
-	// we now have all information we want
-	// the only thing we must do now is writing it to 
-	// the output file
-	//--------------------------------------------------
-	
-	//++ 
-	// save the image to the file
-	//--
-
-
-
 	// look for the next event
 	
@@ -1209,5 +1338,5 @@
 	    
 	  }
-	  
+
 	} // end if found end of file
       } // end if found end of run
@@ -1226,12 +1355,25 @@
   outfile.Close() ; 
 
-  
+ 
   // close input file
   
   log( SIGNATURE, "%d event(s), with a total of %d C.photons\n", 
        ntshow, ntcph );
-  log( SIGNATURE, "Fraction of triggers: %5.1f%% (%d out of %d)\n", 
-       ((float)ntrigger) / ((float)ntshow) * 100.0, ntrigger, ntshow);
-  
+  if (Trigger_Loop){
+    log( SIGNATURE, "Fraction of triggers: \n");
+    for (ithrescount=Trigger_loop_lthres;ithrescount<=Trigger_loop_uthres;ithrescount++){
+      for (imulticount=Trigger_loop_lmult;imulticount<=Trigger_loop_umult;imulticount++){
+	for(itopocount=Trigger_loop_ltop;itopocount<=Trigger_loop_utop;itopocount++){
+	  log( SIGNATURE, "Thres %d, Multi %d, Topo %d: %5.1f%% (%d out of %d)\n", 
+	       ithrescount,imulticount,itopocount,((float)ntriggerloop[ithrescount][imulticount][itopocount] / ((float)ntshow) * 100.0), ntriggerloop[ithrescount][imulticount][itopocount], ntshow);
+	}
+      }   
+    }
+  }
+  else{
+    log( SIGNATURE, "Fraction of triggers: %5.1f%% (%d out of %d)\n", 
+	 ((float)ntrigger) / ((float)ntshow) * 100.0, ntrigger, ntshow);
+  }
+
   // close files
   
@@ -2234,18 +2376,4 @@
 
 //------------------------------------------------------------
-// @name Photoelectron                          
-//                                     
-// @desc constructor for class Photoelectron
-//
-// @date Mon Feb 15 16:44:21 CET 2000
-// @function @code 
-//------------------------------------------------------------
-
-Photoelectron::Photoelectron(void){
-  iarrtime_ns = NOTIME;
-  ipixnum = -1;
-}
-
-//------------------------------------------------------------
 // @name produce_phes                          
 //                                     
@@ -2261,5 +2389,6 @@
 		  float minwl_nm, // the minimum accepted wavelength
 		  float maxwl_nm, // the maximum accepted wavelength
-		  class Photoelectron phe[iMAXNUMPHE], // the generated phes
+		  class MTrigger *trigger, // the generated phes
+		  class MFadc *fadc,
 		  int *itotnphe, // total number of produced photoelectrons
 		  float nphe[iMAXNUMPIX], // number of photoelectrons in each pixel
@@ -2276,4 +2405,5 @@
   static float radius;
 
+
   // reset variables
 
@@ -2284,8 +2414,5 @@
   }
 
-  *itotnphe = 0;
   *incph = 0;
-  *tmin_ns = NOTIME; // very big 
-  *tmax_ns = -NOTIME; // very small
 
   radius = cam->dxc[cam->inumpixels-1]
@@ -2315,5 +2442,19 @@
 	  
     (*incph)++;
-	  	  
+
+    //  Chceck if photon is inside trigger time range
+
+    t = photon.get_t() ; 
+
+    if (t-*tmin_ns>TOTAL_TRIGGER_TIME) {
+ 
+     //  read next Photon
+	  
+      fread( flag, SIZE_OF_FLAGS, 1, sp );
+      
+      // go to beginning of loop, the photon is lost
+      continue;
+    }
+  	  
     //
     // Pixelization
@@ -2423,31 +2564,11 @@
 	  
     nphe[ipixnum] += 1.0;
-
-    t = photon.get_t() ; 
-
-    //    cout << " t " << t;
-	  
-    // find minimum and maximum arrival time
-
-    if(t < *tmin_ns){ 
-      *tmin_ns = t; // memorize time
-    }
-    if(t > *tmax_ns){ 
-      *tmax_ns = t; // memorize time
-    }
-
+	  
     // store the new photoelectron
 
-    if(*itotnphe >= iMAXNUMPHE){
-      cout << "Error: Memory overflow. Event produces more than maximum\n";
-      cout << "       allowed number of photoelectrons (" << iMAXNUMPHE << ").\n";
-      return(1);
-    }
-
-    phe[*itotnphe].iarrtime_ns = (int)t;
-    phe[*itotnphe].ipixnum = ipixnum;
-	  
+    fadc->Fill(ipixnum,(t-*tmin_ns) , trigger->FillShow(ipixnum,t-*tmin_ns));
+    
     *itotnphe += 1;
-
+    
     // read next Photon
 	  
@@ -2473,5 +2594,4 @@
 int produce_nsbrates( char *iname, // the starfield input file name
 		      struct camera *cam, // camera layout
-		      class Photoelectron phe[iMAXNUMPHE], // array for photoelectrons
 		      float rate_phepns[iMAXNUMPIX][iNUMWAVEBANDS] // the product of this function:
 		                               // the NSB rates in phe/ns for each pixel 
@@ -2479,4 +2599,7 @@
 
   int i, j, k, ii; // counters
+
+  MTrigger trigger;
+  MFadc flashadc;
 		      
   static float wl_nm[iNUMWAVEBANDS + 1] = { WAVEBANDBOUND1,
@@ -2574,5 +2697,6 @@
 			      wl_nm[i],
 			      wl_nm[i+1],
-			      phe, // this is a dummy here
+			      &trigger,  // this is a dummy here
+			      &flashadc, // this is a dummy here
 			      &itnphe,
 			      nphe, // we want this!
@@ -2635,5 +2759,6 @@
 		      float extinction[iNUMWAVEBANDS],
 		      float fnpx[iMAXNUMPIX],
-		      Photoelectron photo[iMAXNUMPHE],
+		      MTrigger *trigger,
+		      MFadc *fadc,
 		      int *inphe,
 		      float base_mv[iMAXNUMPIX]){
@@ -2644,12 +2769,13 @@
   int inumnsbphe;   //  number of photoelectrons caused by NSB
 
+  float t;
+
   ii = *inphe; // avoid dereferencing
 		
   // check if the arrival times are set; if not generate them
 
-  if(*atmin_ns == NOTIME){
-
+  if(*atmin_ns <SIMTIMEOFFSET_NS || *atmin_ns > *atmax_ns){
     *atmin_ns = 0.;
-    *atmax_ns = simtime_ns = SLICES*WIDTH_TIMESLICE;
+    *atmax_ns = simtime_ns = TOTAL_TRIGGER_TIME;
 
   }
@@ -2661,11 +2787,18 @@
     simtime_ns = *atmax_ns - *atmin_ns;
 
-    // make sure the simulated time is long enough for the FADC simulation
-
-    if(simtime_ns< SLICES*WIDTH_TIMESLICE){
-      *atmax_ns = *atmin_ns + SLICES*WIDTH_TIMESLICE;
-      simtime_ns = SLICES*WIDTH_TIMESLICE;
+    // make sure the simulated time is long enough for the FADC 
+    // simulation and not too long
+
+    if(simtime_ns< TOTAL_TRIGGER_TIME){
+      *atmin_ns = *atmin_ns -(TOTAL_TRIGGER_TIME-simtime_ns)/2;
+      *atmax_ns = *atmin_ns + TOTAL_TRIGGER_TIME;
+      simtime_ns = TOTAL_TRIGGER_TIME;
     }
-      
+
+    if(simtime_ns> TOTAL_TRIGGER_TIME){
+      *atmax_ns =*atmin_ns + TOTAL_TRIGGER_TIME;
+      simtime_ns = TOTAL_TRIGGER_TIME;
+    }
+ 
   }
 
@@ -2698,16 +2831,8 @@
       
       for(k=0; k < inumnsbphe; k++){
-
-	if(ii >= iMAXNUMPHE){
-	  cout << "Error: Memory overflow. NSB simulation produces more than maximum\n";
-	  cout << "       allowed number of photoelectrons (" << iMAXNUMPHE << ").\n";
-	  return(1);
-	}
-
-	photo[ii].iarrtime_ns = (int)(RandomNumber * simtime_ns + *atmin_ns );
-	photo[ii].ipixnum = j;
-	
-	// cout << "Created phe " << photo[ii].iarrtime_ns << " " 
-	//     << photo[ii].ipixnum << "\n";
+	
+	t=(RandomNumber * simtime_ns);
+
+	(*fadc).Fill(j,t ,(*trigger).FillNSB(j,t));
 	
 	ii++; // increment total number of photoelectons
@@ -2741,4 +2866,9 @@
 //
 // $Log: not supported by cvs2svn $
+// Revision 1.7  2000/03/24 18:10:46  blanch
+// A first FADC simulation and a trigger simulation are already implemented.
+// The calculation of the Hillas Parameters have been removed, since it was decided that it should be in the analysis software.
+// A loop over trigger threshold and some corretcions in the time range where it looks for a trigger will be implemented as soon as possible.
+//
 // Revision 1.6  2000/03/20 18:35:11  blanch
 // The trigger is already implemented but it does not save the trigger information in any file as it is implemented in timecam. In the next days there will be a version which also creates the files with the trigger information. It is going to be a mixing of the current camera and timecam programs.
