Index: trunk/MagicSoft/Simulation/Detector/Camera/camera.cxx
===================================================================
--- trunk/MagicSoft/Simulation/Detector/Camera/camera.cxx	(revision 5245)
+++ trunk/MagicSoft/Simulation/Detector/Camera/camera.cxx	(revision 5247)
@@ -21,7 +21,7 @@
 //
 // $RCSfile: camera.cxx,v $
-// $Revision: 1.71 $
+// $Revision: 1.72 $
 // $Author: moralejo $ 
-// $Date: 2004-09-17 09:20:52 $
+// $Date: 2004-10-12 13:39:34 $
 //
 ////////////////////////////////////////////////////////////////////////
@@ -181,5 +181,5 @@
 static int FADC_Scan = FALSE;
 
-//@: flag: TRUE: show all trigger singnal in the screen; FALSE: don't
+//@: flag: TRUE: show all trigger signal in the screen; FALSE: don't
 static int Trigger_Scan = FALSE;
 
@@ -198,13 +198,15 @@
 
 //@: Properties of the FADC
-static int FADC_shape = 0;
+static Int_t FADC_shape = 0;
 static float FADC_response_integ = MFADC_RESPONSE_INTEGRAL;
 static float FADC_response_fwhm = MFADC_RESPONSE_FWHM;
-static int FADC_shape_out = 0;
+static Int_t FADC_shape_out = 0;
 static float FADC_resp_integ_out = MFADC_RESPONSE_INTEGRAL;
 static float FADC_resp_fwhm_out = MFADC_RESPONSE_FWHM;
+static float FADC_slices_per_ns = FADC_SLICES_PER_NSEC;
+static Int_t FADC_slices_written = FADC_SLICES;
 static float FADC_noise_inner = 2.0;
 static float FADC_noise_outer = 2.0;
-static float DIGITAL_noise = 2.0;
+static float DIGITAL_noise = 0.0;
 static float FADC_high2low = HIGH2LOWGAIN;
 
@@ -411,5 +413,5 @@
   Float_t nsb_trigresp[TRIGGER_TIME_SLICES];    //@< array to write the trigger
                                                 //@< response from the database
-  Float_t nsb_fadcresp[(Int_t) SLICES_MFADC];   //@< array to write the fadc
+  Float_t *nsb_fadcresp;                        //@< array to write the fadc
                                                 //@< response from the database
   Byte_t trigger_map[((Int_t)(CAMERA_PIXELS/8))+1]; //@< Pixels on when the
@@ -674,5 +676,9 @@
   get_FADC_properties
     (&FADC_shape, &FADC_response_integ, &FADC_response_fwhm,
-     &FADC_shape_out, &FADC_resp_integ_out, &FADC_resp_fwhm_out);
+     &FADC_shape_out, &FADC_resp_integ_out, &FADC_resp_fwhm_out,
+     &FADC_slices_per_ns, &FADC_slices_written);
+
+  // Allocate memory for the NSB contribution to the FADC signal:
+  nsb_fadcresp = new Float_t[(Int_t)(FADC_slices_per_ns*TOTAL_TRIGGER_TIME)];
 
   FADC_high2low=get_High_to_Low();
@@ -895,6 +901,6 @@
   Int_t Lev0MT[MAX_NUMBER_OF_CTS], Lev1MT[MAX_NUMBER_OF_CTS]; 
   
-  fadcValues    =  new TArrayC(FADC_SLICES);
-  fadcValuesLow =  new TArrayC(FADC_SLICES);
+  fadcValues    =  new TArrayC(FADC_slices_written);
+  fadcValuesLow =  new TArrayC(FADC_slices_written);
 
   // number of pixels for parameters
@@ -988,6 +994,7 @@
 		 FADC_shape_out,
 		 FADC_resp_integ_out,FADC_resp_fwhm_out,
-		 get_trig_delay()
-	  ) ; //@< A instance of the Class MFadc
+		 get_trig_delay(),
+		 FADC_slices_per_ns,
+		 FADC_slices_written); //@< A instance of the Class MFadc
 
 
@@ -1187,5 +1194,5 @@
   RunHeader->SetRunType(256);
   RunHeader->SetRunNumber(0);
-  RunHeader->SetNumSamples(FADC_SLICES, FADC_SLICES);
+  RunHeader->SetNumSamples(FADC_slices_written, FADC_slices_written);
   RunHeader->SetNumCrates(1);
   RunHeader->SetNumPixInCrate(ct_NPixels);
@@ -1255,6 +1262,6 @@
       }
 
-    HeaderFadc[0]->SetShape(FADC_shape);
-    HeaderFadc[0]->SetShapeOuter(FADC_shape_out);
+    HeaderFadc[0]->SetShape((Float_t)FADC_shape);
+    HeaderFadc[0]->SetShapeOuter((Float_t)FADC_shape_out);
     HeaderFadc[0]->SetAmplitud(FADC_response_integ,
 			       FADC_resp_integ_out);
@@ -1280,6 +1287,6 @@
 
       Fadc_CT[i]->GetPedestals(&fadc_pedestals[0]);
-      HeaderFadc[i]->SetShape(FADC_shape);
-      HeaderFadc[i]->SetShapeOuter(FADC_shape_out);
+      HeaderFadc[i]->SetShape((Float_t)FADC_shape);
+      HeaderFadc[i]->SetShapeOuter((Float_t)FADC_shape_out);
       HeaderFadc[i]->SetAmplitud(FADC_response_integ,
 				 FADC_resp_integ_out);
@@ -1308,6 +1315,6 @@
 	for(itopocount=0;itopocount<=Trigger_loop_utop-Trigger_loop_ltop;itopocount++){
 	  Fadc_CT[0]->GetPedestals(&fadc_pedestals[0]);
-	  HeaderFadc[iconcount]->SetShape(FADC_shape);
-	  HeaderFadc[iconcount]->SetShapeOuter(FADC_shape_out);
+	  HeaderFadc[iconcount]->SetShape((Float_t)FADC_shape);
+	  HeaderFadc[iconcount]->SetShapeOuter((Float_t)FADC_shape_out);
 	  HeaderFadc[iconcount]->SetAmplitud(FADC_response_integ,
 					     FADC_resp_integ_out);
@@ -1423,23 +1430,26 @@
   }
   
-  // prepare the NSB simulation
+  //  prepare the NSB simulation
 
   //  Instance of the Mlons class
-  MLons lons(0.0, Trigger_response_ampl, Trigger_response_fwhm,
-	     float(FADC_shape),FADC_response_integ,FADC_response_fwhm);
-
-  lons.SetSeed(Int_t(get_seeds(1)/get_seeds(0))+1);
+
+  MLons lons(0, Trigger_response_ampl, Trigger_response_fwhm,
+	     FADC_shape, FADC_response_integ, FADC_response_fwhm,
+	     FADC_slices_per_ns);
+
+  lons.SetSeed(Int_t(get_seeds(1)%get_seeds(0))+1);
 
   lons.SetPath(nsbpathname);
 
   //  Instance of the Mlons class
-  MLons lons_outer(0.0, Trigger_response_ampl, Trigger_response_fwhm,
-		   float(FADC_shape_out),FADC_resp_integ_out,FADC_resp_fwhm_out);
-
-  lons_outer.SetSeed(Int_t(get_seeds(1)/get_seeds(0))+2);
+  MLons lons_outer(0, Trigger_response_ampl, Trigger_response_fwhm,
+		   FADC_shape_out,FADC_resp_integ_out,FADC_resp_fwhm_out,
+		   FADC_slices_per_ns);
+
+  lons_outer.SetSeed(Int_t(get_seeds(1)%get_seeds(0))+2);
 
   lons_outer.SetPath(nsbpath_outer);
 
-  if( simulateNSB ){
+  if( simulateNSB){
 
     //
@@ -1451,5 +1461,5 @@
     // Then we will have to use mirror_frac[ict]
     //
-    log(SIGNATURE,"Produce NSB rates from Star Field");
+    log(SIGNATURE,"Produce NSB rates from Star Field...\n");
 
     k = produce_nsbrates( starfieldname,
@@ -1458,5 +1468,4 @@
 			  0,
 			  mirror_frac[0]);
-
 
     //
@@ -1597,26 +1606,35 @@
 	Fadc_CT[ict]->ElecNoise() ;
       }
-      if(simulateNSB){
-	for(UInt_t ui=0;
-	    ui<((MGeomCam*)(camgeom.UncheckedAt(ict)))->GetNumPixels();
-	    ui++){
-	  if(nsb_phepns[ict][ui]>0.0){
-	    if((*((MGeomCam*)(camgeom.UncheckedAt(ict))))[ui].GetD()>(*((MGeomCam*)(camgeom.UncheckedAt(ict))))[0].GetD()){
-	      k=lons_outer.GetResponse(nsb_phepns[ict][ui],0.01,
-				       & nsb_trigresp[0],
-				       & nsb_fadcresp[0]);
+      if(simulateNSB)
+	{
+	  for(UInt_t ui=0;
+	      ui<((MGeomCam*)(camgeom.UncheckedAt(ict)))->GetNumPixels(); 
+	      ui++)
+	    {
+	      if(nsb_phepns[ict][ui]>0.0)
+		{
+		  if((*((MGeomCam*)(camgeom.UncheckedAt(ict))))[ui].GetD() > 
+		     (*((MGeomCam*)(camgeom.UncheckedAt(ict))))[0].GetD())
+		    {
+
+		      k = lons_outer.GetResponse(nsb_phepns[ict][ui],0.01,
+						 & nsb_trigresp[0],
+						 & nsb_fadcresp[0]);
+		    }
+		  else
+		    {
+		      k=lons.GetResponse(nsb_phepns[ict][ui],0.01,
+					 & nsb_trigresp[0],& nsb_fadcresp[0]);
+		    }
+
+		  if(k==0)
+		    {
+		      cout << "Exiting.\n";
+		      exit(1);
+		    }
+		  Fadc_CT[ict]->AddSignal(ui,nsb_fadcresp);
+		}
 	    }
-	    else{
-	      k=lons.GetResponse(nsb_phepns[ict][ui],0.01,
-				 & nsb_trigresp[0],& nsb_fadcresp[0]);
-	    }
-	    if(k==0){
-	      cout << "Exiting.\n";
-	      exit(1);
-	    }
-	    Fadc_CT[ict]->AddSignal(ui,nsb_fadcresp);
-	  }
 	}
-      }
       Fadc_CT[ict]->Pedestals();
 
@@ -1669,7 +1687,6 @@
   }
   else{
-
     for(int ict=0;ict<ct_Number;ict++){
-      
+
       log( SIGNATURE, "Opening input \"rfl\" file %s\n", inname_CT[ict] );
       inputfile[ict] = fopen( inname_CT[ict], "r" );
@@ -1917,5 +1934,5 @@
 	  inumphe=(inumphe<inumphe_CT[ict])?inumphe_CT[ict]:inumphe;
 
-	  if( k != 0 ){ // non-zero returnvalue means error
+	  if( k != 0 ){ // non-zero return value means error
 	    cout << "Exiting.\n";
 	    exit(1);
@@ -1925,79 +1942,88 @@
 	// NSB simulation
 
-	if(simulateNSB && nphe2NSB<=inumphe){
-
-	  if(Starfield_rotate){
+	if(simulateNSB && nphe2NSB<=inumphe)
+	  {
+
+	    if(Starfield_rotate){
 	    
-	    // Introduction rho angle
+	      // Introduction rho angle
 	    
-	    zenith = thetashw;
-	    azimutal = phishw;
-	    C1 = 0.48 * sin(zenith) - 0.87 * cos(zenith) * cos(azimutal);
-	    C3 = (0.87 * cos(zenith) - 0.48 * sin(zenith) * cos(azimutal));
-	    C2 = sqrt( sin(zenith) * sin(zenith) * sin(azimutal) * sin(azimutal) + C3 * C3 );
-	    rho = acos( C1/C2 );
+	      zenith = thetashw;
+	      azimutal = phishw;
+	      C1 = 0.48 * sin(zenith) - 0.87 * cos(zenith) * cos(azimutal);
+	      C3 = (0.87 * cos(zenith) - 0.48 * sin(zenith) * cos(azimutal));
+	      C2 = sqrt( sin(zenith) * sin(zenith) * sin(azimutal) * sin(azimutal) + 
+			 C3 * C3 );
+	      rho = acos( C1/C2 );
 	   
-	    if ( sin(azimutal) < 0)
-	      {
+	      if ( sin(azimutal) < 0)
 		rho = 2 * 3.14159 - rho;
-	      }   
-	    else
-	      {
+	      else
 		rho = rho;
-	      }
-	    rho = rho*180/3.14159;
-
-	    // Rotation of the NSB
-	    // FIXME --- We should rotate for all cameras. Is it always the same rho? 	
-	    for(int ict=0;ict<ct_Number;ict++){      
-	      k = size_rotated(
-			       &nsb_phepns_rotated[ict][0],
-			       nsb_phepns[ict],
-			       rho);
+
+	      rho = rho*180/3.14159;
+
+	      // Rotation of the NSB
+	      // FIXME --- We should rotate for all cameras. Is it always the same rho? 	
+	      for(int ict=0;ict<ct_Number;ict++)
+		k = size_rotated(&nsb_phepns_rotated[ict][0],
+				 nsb_phepns[ict],
+				 rho);
+	    }
+
+	  //  Fill trigger and fadc response in the trigger class from the database
+	  for(int ict=0;ict<ct_Number;ict++)
+	    {
+	      for(UInt_t ui=0;
+		  ui<((MGeomCam*)(camgeom.UncheckedAt(ict)))->GetNumPixels();
+		  ui++)
+		{
+		  if(nsb_phepns_rotated[ict][ui]>0.0)
+		    {
+		      if((*((MGeomCam*)(camgeom.UncheckedAt(ict))))[ui].GetD() > 
+			 (*((MGeomCam*)(camgeom.UncheckedAt(ict))))[0].GetD())
+			{
+			  k=lons_outer.GetResponse(nsb_phepns_rotated[ict][ui],0.01,
+						   & nsb_trigresp[0],
+						   & nsb_fadcresp[0]);
+			}
+		      else
+			{
+			  k=lons.GetResponse(nsb_phepns_rotated[ict][ui],0.01,
+					     & nsb_trigresp[0],& nsb_fadcresp[0]);
+			}
+		      if(k==0)
+			{
+			  cout << "Exiting.\n";
+			  exit(1);
+			}
+		      Trigger_CT[ict]->AddNSB(ui,nsb_trigresp);
+		      Fadc_CT[ict]->AddSignal(ui,nsb_fadcresp);
+		    }
+		}
+	    }
+	  
+	  }// end if(simulateNSB && nphe2NSB<=inumphe_CT[0]) ...
+
+
+	for(int ict=0;ict<ct_Number;ict++)
+	  {
+	    inumphensb[ict]=0;
+
+	    for (UInt_t ui=0;
+		 ui < ((MGeomCam*)(camgeom.UncheckedAt(ict)))->GetNumPixels();
+		 ui++)
+	      inumphensb[ict]+=nsb_phepns[ict][ui]*TOTAL_TRIGGER_TIME;
+
+	    ntcph[ict]+=ncph[ict];
+	    if ((nshow+ntshow+1)%100 == 1){
+	      log(SIGNATURE, "End of this event: %d cphs(+%d). . .\n",
+		  ncph[ict], ntcph[ict]);
+
+	      cout << "Total number of phes in CT "<<ict<<": " 
+		   << inumphe_CT[ict]<<" (+ ";
+	      cout<<inumphensb[ict]<<" mean expected number from NSB)"<<endl;
 	    }
 	  }
-
-	  //  Fill trigger and fadc response in the trigger class from the database
-	  for(int ict=0;ict<ct_Number;ict++){
-	    
-	    for(UInt_t ui=0;ui<((MGeomCam*)(camgeom.UncheckedAt(ict)))->GetNumPixels();ui++){
-	      if(nsb_phepns_rotated[ict][ui]>0.0){
-		if((*((MGeomCam*)(camgeom.UncheckedAt(ict))))[ui].GetD()>(*((MGeomCam*)(camgeom.UncheckedAt(ict))))[0].GetD()){
-		  k=lons_outer.GetResponse(nsb_phepns_rotated[ict][ui],0.01,
-					   & nsb_trigresp[0],
-					   & nsb_fadcresp[0]);
-	      }
-		else{
-		  k=lons.GetResponse(nsb_phepns_rotated[ict][ui],0.01,
-				     & nsb_trigresp[0],& nsb_fadcresp[0]);
-		}
-		if(k==0){
-		  cout << "Exiting.\n";
-		  exit(1);
-		}
-		Trigger_CT[ict]->AddNSB(ui,nsb_trigresp);
-		Fadc_CT[ict]->AddSignal(ui,nsb_fadcresp);
-	      }
-	    }
-	  }
-	
-	}// end if(simulateNSB && nphe2NSB<=inumphe_CT[0]) ...
-
-	for(int ict=0;ict<ct_Number;ict++){
-	  inumphensb[ict]=0;
-	  for (UInt_t ui=0;ui<
-		 ((MGeomCam*)(camgeom.UncheckedAt(ict)))->GetNumPixels();ui++){
-	    inumphensb[ict]+=nsb_phepns[ict][ui]*TOTAL_TRIGGER_TIME;
-	  }
-	  ntcph[ict]+=ncph[ict];
-	  if ((nshow+ntshow+1)%100 == 1){
-	    log(SIGNATURE, "End of this event: %d cphs(+%d). . .\n",
-		ncph[ict], ntcph[ict]);
-
-	    cout << "Total number of phes in CT "<<ict<<": " 
-		 << inumphe_CT[ict]<<" (+ ";
-	    cout<<inumphensb[ict]<<" mean expected number from NSB)"<<endl;
-	  }
-	}
 
 	// skip it ?
@@ -2033,8 +2059,8 @@
 
 	//  We should simulate the AC coupling behaviour:
-        //  For the FADC it is only done for the NSB while producinr
+        //  For the FADC it is only done for the NSB while producing
         //  the StarResponse database.
 	//  For the trigger is done in the Trigger.Diskriminate(), which
-	//  is called later (it should be seperated to speed up the program
+	//  is called later (it should be separated to speed up the program)
 	//
 
@@ -2044,295 +2070,328 @@
 	//   
 
-	for(int ict=0;ict<ct_Number;ict++) {
-	  if (addElecNoise && nphe2NSB<=inumphe){	
-
-	    Trigger_CT[ict]->ElecNoise(Trigger_noise) ;
-	    
-	    Fadc_CT[ict]->ElecNoise() ;
+	for(int ict=0;ict<ct_Number;ict++) 
+	  {
+	    if (addElecNoise && nphe2NSB<=inumphe)
+	      {	
+		Trigger_CT[ict]->ElecNoise(Trigger_noise) ;
+		Fadc_CT[ict]->ElecNoise() ;
+	      }
 	  }
-	}
 	
-	//  now a shift in the fadc signal due to the pedestlas is 
-	//  intorduced
+	//  now a shift in the fadc signal due to the pedestals is 
+	//  introduced
 	//  This is done inside the class MFadc by the method Pedestals
-	for(int ict=0;ict<ct_Number;ict++) {
+
+	for(int ict=0;ict<ct_Number;ict++)
 	  Fadc_CT[ict]->Pedestals();
-	}
+
 	
 	//   We study several trigger conditons
-	if(Trigger_Loop){
-
-	  // Set to zero the flag to know if some conditon has triggered
-	  btrigger=0;
-	  flagstoring = 0;
-
-	  //  Loop over trigger threshold
-	  int iconcount;
-	  for (iconcount=0, ithrescount=0, fthrescount=Trigger_loop_lthres;fthrescount<=Trigger_loop_uthres;ithrescount++, fthrescount+=Trigger_loop_sthres){
-	    for (int i=0;i<ct_NPixels;i++){
-	      fpixelthres[i]=
-		((Float_t)(fthrescount)>=qThreshold[0][i])?
-		(Float_t)(fthrescount):qThreshold[0][i];
-
-	      // Rise the discrimnator threshold to avoid huge rates
-
-	      if(riseDiskThres>0.0 && simulateNSB && nphe2NSB<=inumphe)
-		for(int ii=0;ii<ct_NPixels;ii++)
-		  if( nsb_phepns_rotated[0][ii]>riseDiskThres)
-		    fpixelthres[ii]=secureDiskThres;
-	    }
-	    Trigger_CT[0]->SetThreshold(fpixelthres);
+	if(Trigger_Loop)
+	  {
+	    // Set to zero the flag to know if some conditon has triggered
+	    btrigger=0;
+	    flagstoring = 0;
+
+	    //  Loop over trigger threshold
+	    int iconcount;
+	    for (iconcount=0, ithrescount=0, fthrescount=Trigger_loop_lthres;
+		 fthrescount <= Trigger_loop_uthres;
+		 ithrescount++, fthrescount += Trigger_loop_sthres)
+	      {
+		for (int i=0;i<ct_NPixels;i++)
+		  {
+		    fpixelthres[i] =
+		      ((Float_t)(fthrescount)>=qThreshold[0][i])?
+		      (Float_t)(fthrescount):qThreshold[0][i];
+
+		    // Rise the discrimnator threshold to avoid huge rates
+
+		    if(riseDiskThres>0.0 && simulateNSB && nphe2NSB<=inumphe)
+		      for(int ii=0;ii<ct_NPixels;ii++)
+			if( nsb_phepns_rotated[0][ii]>riseDiskThres)
+			  fpixelthres[ii]=secureDiskThres;
+		  }
+		Trigger_CT[0]->SetThreshold(fpixelthres);
 	    
-	    Trigger_CT[0]->Diskriminate();
-
+		Trigger_CT[0]->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. 
+		//
 	    
-	    //
-	    //   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. 
-	    //
+		//  Set trigger flags to zero
+		Lev0=0;
+		Lev1=0;
+
+		//  loop over multiplicity of trigger configuration
+		for (imulticount = Trigger_loop_lmult;
+		     imulticount <= Trigger_loop_umult;
+		     imulticount++)
+		  {
+		    Trigger_CT[0]->SetMultiplicity(imulticount);
+		    Trigger_CT[0]->ClearZero();
+
+		    Lev0=(Short_t) Trigger_CT[0]->ZeroLevel();
+		    if (Lev0>0 || Write_All_Images || btrigger)
+		      {
+		
+			//  loop over topologies
+			for(itopocount=Trigger_loop_ltop;
+			    itopocount<=Trigger_loop_utop;
+			    itopocount++)
+			  {
+			    Lev1=0;
+		
+			    if(itopocount==0 && imulticount>7) 
+			      continue;
+
+			    //COBB if(itopocount==2 && imulticount<3) continue;
+			    // It only makes to look for a different topology
+			    // if there are 3 or more N pixels. 
+			    if(imulticount<3)
+			      Trigger_CT[0]->SetTopology(1);
+			    else
+			      {
+				// We should be careful that topologies are sort from 
+				// the less to the more restrictive one. 
+				Trigger_CT[0]->SetTopology(isorttopo[itopocount]);
+			      }
+			    Trigger_CT[0]->ClearFirst();
+		  
+			    //
+			    //   Start the First Level Trigger simulation
+			    //
+			    if(Lev0!=0)
+			      Lev1=Trigger_CT[0]->FirstLevel();
+			    if(Lev1>0) {
+			      btrigger= 1;
+			      ntriggerloop[ithrescount]
+				[imulticount-Trigger_loop_lmult]
+				[itopocount-Trigger_loop_ltop]++;
+			    }
+		  
+			    Lev0=1;
+			    Int_t NumImages = Lev1;
+			    if(Lev1==0 && (Write_All_Images || btrigger))
+			      {
+				btrigger= 1;
+				NumImages=1;
+				Lev0=0;
+			      }
+
+			    for (Int_t ii=0;ii<NumImages;ii++)
+			      {
+				if (Write_McTrig)
+				  {
+				    McTrig[iconcount]->SetFirstLevel ((ii+1)*Lev0);
+				    McTrig[iconcount]->
+				      SetTime(Trigger_CT[0]->GetFirstLevelTime(ii),ii+1);
+				    Trigger_CT[0]->GetMapDiskriminator(trigger_map);
+				    McTrig[iconcount]->SetMapPixels(trigger_map,ii);
+				  }
+				//
+				//  fill inside the class fadc the member output
+				//
+
+				Fadc_CT[0]->TriggeredFadc(Trigger_CT[0]->
+							  GetFirstLevelTime(ii));
 	    
-	    //  Set trigger flags to zero
-	    Lev0=0;
-	    Lev1=0;
-
-	    //  loop over multiplicity of trigger configuration
-	    for (imulticount=Trigger_loop_lmult;imulticount<=Trigger_loop_umult;imulticount++){
-	      Trigger_CT[0]->SetMultiplicity(imulticount);
-	      Trigger_CT[0]->ClearZero();
-
-	      Lev0=(Short_t) Trigger_CT[0]->ZeroLevel();
-	      if (Lev0>0 || Write_All_Images || btrigger){
-		
-		//  loop over topologies
-		for(itopocount=Trigger_loop_ltop;itopocount<=Trigger_loop_utop;itopocount++){
-		  Lev1=0;
-		
-		  if(itopocount==0 && imulticount>7) continue;
-		  //COBB if(itopocount==2 && imulticount<3) continue;
-		  // It only makes to look for a different topology
-		  // if there are 3 or more N pixels. 
-		  if(imulticount<3)
-		    Trigger_CT[0]->SetTopology(1);
-		  else
-		    {
-		      // We should be careful that topologies are sort from 
-		      // the less to the more restrictive one. 
-		      Trigger_CT[0]->SetTopology(isorttopo[itopocount]);
-		    }
-		  Trigger_CT[0]->ClearFirst();
-		  
-		  //
-		  //   Start the First Level Trigger simulation
-		  //
-		  if(Lev0!=0)
-		      Lev1=Trigger_CT[0]->FirstLevel();
-		  if(Lev1>0) {
-		    btrigger= 1;
-		    ntriggerloop[ithrescount][imulticount-Trigger_loop_lmult][itopocount-Trigger_loop_ltop]++;
-		  }
-		  
-		  Lev0=1;
-		  Int_t NumImages = Lev1;
-		  if(Lev1==0 && (Write_All_Images || btrigger)){
-		    btrigger= 1;
-		    NumImages=1;
-		    Lev0=0;
-		  }
-
-		  for (Int_t ii=0;ii<NumImages;ii++){
-		      if (Write_McTrig){
-			  McTrig[iconcount]->SetFirstLevel ((ii+1)*Lev0);
-			  McTrig[iconcount]->SetTime(Trigger_CT[0]->GetFirstLevelTime(ii),ii+1);
-			  Trigger_CT[0]->GetMapDiskriminator(trigger_map);
-			  McTrig[iconcount]->SetMapPixels(trigger_map,ii);
+				if( Write_RawEvt )
+				  {
+				    //
+				    //  Fill the header of this event 
+				    //
+		      
+				    EvtHeader[iconcount]->
+				      FillHeader( (UInt_t) (ntshow + nshow),0);
+
+				    //   fill pixel information
+				    if (Lev1 || Write_All_Images){
+				      if (addElecNoise) Fadc_CT[0]->DigitalNoise();
+				      for(UInt_t i=0;
+					  i<((MGeomCam*)(camgeom.UncheckedAt(0)))
+					    ->GetNumPixels();i++){
+					//
+					// AM 15 01 2004: commented out "continue" 
+					// statement, so that also pixels with no 
+					// C-photons will be written to the output 
+					// in case the camera is run with no noise.
+					// if(!Fadc_CT[0]->IsPixelUsed(i)) continue;
+
+					for (j=0;j<FADC_slices_written;j++){
+					  fadcValues->AddAt(Fadc_CT[0]->
+							    GetFadcSignal(i,j),j);
+					  fadcValuesLow->AddAt(Fadc_CT[0]->
+							       GetFadcLowGainSignal(i,j),j);
+					}
+					EvtData[iconcount]->AddPixel(i,fadcValues,0);
+					EvtData[iconcount]->AddPixel(i,fadcValuesLow,kTRUE);
+				      }
+				    }
+				  }
+			      }
+			    //
+			    // Increase counter of analised trigger conditions
+			    //
+			    iconcount++;
+			  }
 		      }
-		    //
-		    //  fill inside the class fadc the member output
-		    //
-
-		    Fadc_CT[0]->TriggeredFadc(Trigger_CT[0]->GetFirstLevelTime(ii));
-	    
-		    if( Write_RawEvt ){
-		      //
-		      //  Fill the header of this event 
-		      //
-		      
-		      EvtHeader[iconcount]->FillHeader ( (UInt_t) (ntshow + nshow),0);
-		      //   fill pixel information
-		      if (Lev1 || Write_All_Images){
-			if (addElecNoise) Fadc_CT[0]->DigitalNoise();
-			for(UInt_t i=0;
-			    i<((MGeomCam*)(camgeom.UncheckedAt(0)))
-			      ->GetNumPixels();i++){
-//
-// AM 15 01 2004: commented out "continue" statement, so that also pixels 
-// with no C-photons will be written to the output in case the camera
-// is run with no noise.		    
-//  			  if(!Fadc_CT[0]->IsPixelUsed(i)) continue;
-//
-			  for (j=0;j<FADC_SLICES;j++){
-			    fadcValues->AddAt(Fadc_CT[0]->GetFadcSignal(i,j),j);
-			    fadcValuesLow->AddAt(Fadc_CT[0]->GetFadcLowGainSignal(i,j),j);
-			  }
-			  EvtData[iconcount]->AddPixel(i,fadcValues,0);
-			  EvtData[iconcount]->AddPixel(i,fadcValuesLow,kTRUE);
-			}
-		      }
+		    else{
+		      break;
 		    }
 		  }
-		  //
-		  // Increase counter of analised trigger conditions
-		  //
-		  iconcount++;
+		if (!btrigger) break;
+	      }
+	    if (btrigger){
+
+	      //
+	      //   fill the MMcEvt with all information  
+	      //
+
+	      if(!flagstoring)
+		nstoredevents++;
+	      flagstoring = 1;
+	      
+	      if (Write_McEvt) {
+		Float_t ftime, ltime;
+		if (reflector_file_version<6){
+		  mcevth[0].get_times(&ftime, &ltime);
+		  McEvt[0]->Fill( 0, 
+				  (UShort_t) mcevth[0].get_primary() , 
+				  mcevth[0].get_energy(),
+				  -1.0,
+				  -1.0,
+				  -1.0, 
+				  mcevth[0].get_theta(), 
+				  mcevth[0].get_phi(), 
+				  mcevth[0].get_core(),
+				  coreX,
+				  coreY,
+				  impactD[0],
+				  phiCT[0],
+				  thetaCT[0],
+				  ftime,
+				  ltime,
+				  0,
+				  0,
+				  0,
+				  0,
+				  0,
+				  0,
+				  0,
+				  (UInt_t)mcevth[0].get_CORSIKA(), 
+				  (UInt_t)mcevth[0].get_AtmAbs(), 
+				  (UInt_t)(mcevth[0].get_MirrAbs()+
+					   mcevth[0].get_OutOfMirr()+
+					   mcevth[0].get_BlackSpot()), 
+				  (UInt_t) ncph[0], 
+				  (UInt_t) inumphe_CT[0],
+				  (UInt_t) inumphensb[0]+inumphe_CT[0],
+				  -1.0,
+				  -1.0,
+				  -1.0);
+		}
+		else{
+		  Float_t Nmax, t0, tmax, a, b, c, chi2;
+		  mcevth_2[0].get_times(&ftime, &ltime);
+		  chi2=mcevth_2[0].get_NKGfit(&Nmax, &t0, &tmax, &a, &b, &c);
+		  McEvt[0]->Fill((UInt_t) mcevth_2[0].get_evt_number(),
+				 (UShort_t) mcevth_2[0].get_primary() , 
+				 mcevth_2[0].get_energy(),
+				 mcevth_2[0].get_thick0(),
+				 mcevth_2[0].get_first_target(),
+				 mcevth_2[0].get_z_first_int(),
+				 mcevth_2[0].get_theta(), 
+				 mcevth_2[0].get_phi(), 
+				 mcevth_2[0].get_core(),
+				 coreX,
+				 coreY,
+				 impactD[0],
+				 mcevth_2[0].get_phi_CT(),
+				 mcevth_2[0].get_theta_CT(),
+				 ftime,
+				 ltime,
+				 Nmax,
+				 t0,
+				 tmax,
+				 a,
+				 b,
+				 c,
+				 chi2,
+				 (UInt_t)mcevth_2[0].get_CORSIKA(), 
+				 (UInt_t)mcevth_2[0].get_AtmAbs(), 
+				 (UInt_t)(mcevth_2[0].get_MirrAbs()+
+					  mcevth_2[0].get_OutOfMirr()+
+					  mcevth_2[0].get_BlackSpot()), 
+				 (UInt_t) ncph[0], 
+				 (UInt_t) inumphe_CT[0],
+				 (UInt_t) inumphensb[0]+inumphe_CT[0],
+				 mcevth_2[0].get_ElecFraction(),
+				 mcevth_2[0].get_MuonFraction(),
+				 mcevth_2[0].get_OtherFraction());
 		}
 	      }
-	      else{
-		break;
+	      //  Fill the Tree with the current leaves of each branch
+	      i=EvtTree.Fill() ;
+
+	      //  Clear the branches
+	      if(Write_McTrig){
+		for(int i=0;i<numberBranches;i++){
+		  McTrig[i]->Clear() ;
+		}
 	      }
+	      if( Write_RawEvt ){
+		for(int i=0;i<numberBranches;i++){
+		  EvtHeader[i]->Clear() ;
+		  EvtData[i]->ResetPixels (0, 0);
+		}
+	      }
+	      if (Write_McEvt)
+		McEvt[0]->Clear() ;  
 	    }
-	    if (!btrigger) break;
 	  }
-	  if (btrigger){
-
-	    //
-	    //   fill the MMcEvt with all information  
-	    //
-
-	    if(!flagstoring)
-	      nstoredevents++;
-	    flagstoring = 1;
-
-	    if (Write_McEvt) {
-	      Float_t ftime, ltime;
-	      if (reflector_file_version<6){
-		mcevth[0].get_times(&ftime, &ltime);
-		McEvt[0]->Fill( 0, 
-			     (UShort_t) mcevth[0].get_primary() , 
-			     mcevth[0].get_energy(),
-			     -1.0,
-			     -1.0,
-			     -1.0, 
-			     mcevth[0].get_theta(), 
-			     mcevth[0].get_phi(), 
-			     mcevth[0].get_core(),
-			     coreX,
-			     coreY,
-			     impactD[0],
-			     phiCT[0],
-			     thetaCT[0],
-			     ftime,
-			     ltime,
-			     0,
-			     0,
-			     0,
-			     0,
-			     0,
-			     0,
-			     0,
-			     (UInt_t)mcevth[0].get_CORSIKA(), 
-			     (UInt_t)mcevth[0].get_AtmAbs(), 
-			     (UInt_t)(mcevth[0].get_MirrAbs()+mcevth[0].get_OutOfMirr()+mcevth[0].get_BlackSpot()), 
-			     (UInt_t) ncph[0], 
-			     (UInt_t) inumphe_CT[0],
-			     (UInt_t) inumphensb[0]+inumphe_CT[0],
-			     -1.0,
-			     -1.0,
-			     -1.0);
-	      }
-	      else{
-		Float_t Nmax, t0, tmax, a, b, c, chi2;
-		mcevth_2[0].get_times(&ftime, &ltime);
-		chi2=mcevth_2[0].get_NKGfit(&Nmax, &t0, &tmax, &a, &b, &c);
-		McEvt[0]->Fill((UInt_t) mcevth_2[0].get_evt_number(),
-			    (UShort_t) mcevth_2[0].get_primary() , 
-			     mcevth_2[0].get_energy(),
-			     mcevth_2[0].get_thick0(),
-			     mcevth_2[0].get_first_target(),
-			     mcevth_2[0].get_z_first_int(),
-			     mcevth_2[0].get_theta(), 
-			     mcevth_2[0].get_phi(), 
-			     mcevth_2[0].get_core(),
-			     coreX,
-			     coreY,
-			     impactD[0],
-			     mcevth_2[0].get_phi_CT(),
-			     mcevth_2[0].get_theta_CT(),
-			     ftime,
-			     ltime,
-			     Nmax,
-			     t0,
-			     tmax,
-			     a,
-			     b,
-			     c,
-			     chi2,
-			     (UInt_t)mcevth_2[0].get_CORSIKA(), 
-			     (UInt_t)mcevth_2[0].get_AtmAbs(), 
-			     (UInt_t)(mcevth_2[0].get_MirrAbs()+mcevth_2[0].get_OutOfMirr()+mcevth_2[0].get_BlackSpot()), 
-			     (UInt_t) ncph[0], 
-			     (UInt_t) inumphe_CT[0],
-			     (UInt_t) inumphensb[0]+inumphe_CT[0],
-			     mcevth_2[0].get_ElecFraction(),
-			     mcevth_2[0].get_MuonFraction(),
-			     mcevth_2[0].get_OtherFraction());
-	      }
-	    }
-	    //  Fill the Tree with the current leaves of each branch
-	    i=EvtTree.Fill() ;
-
-	    //  Clear the branches
-	    if(Write_McTrig){
-	      for(int i=0;i<numberBranches;i++){
-		McTrig[i]->Clear() ;
-	      }
-	    }
-	    if( Write_RawEvt ){
-	      for(int i=0;i<numberBranches;i++){
-		EvtHeader[i]->Clear() ;
-		EvtData[i]->ResetPixels (0, 0);
-	      }
-	    }
-	    if (Write_McEvt)
-	      McEvt[0]->Clear() ;  
-	  }
-	}
+
 	//  We study a single trigger condition
 	else {
-
+	  
 	  // Set to zero the flag to know if some conditon has triggered
 	  btrigger=0;
 	  flagstoring = 0;
 	  
-	  for(int ict=0;ict<ct_Number;ict++){
-
-	    //  Setting trigger conditions
-	    Trigger_CT[ict]->SetMultiplicity(Trigger_multiplicity[ict]);
-	    Trigger_CT[ict]->SetTopology(Trigger_topology[ict]);
-	    for (int i=0;i<ct_NPixels;i++)
-	      fpixelthres[i]=qThreshold[ict][i];
-
-	    // Rise the discrimnator threshold to avoid huge rates
-	    if(riseDiskThres>0.0 && simulateNSB && nphe2NSB<=inumphe)
-	      for(int ii=0;ii<ct_NPixels;ii++){
-		if( nsb_phepns_rotated[ict][ii]>riseDiskThres)
-		  fpixelthres[ii]=secureDiskThres;
-	      }
+	  for(int ict = 0; ict < ct_Number; ict++)
+	    {
+
+	      //  Setting trigger conditions
+	      Trigger_CT[ict]->SetMultiplicity(Trigger_multiplicity[ict]);
+	      Trigger_CT[ict]->SetTopology(Trigger_topology[ict]);
+	      for (int i=0;i<ct_NPixels;i++)
+		fpixelthres[i]=qThreshold[ict][i];
+
+	      // Rise the discrimnator threshold to avoid huge rates
+	      if(riseDiskThres>0.0 && simulateNSB && nphe2NSB<=inumphe)
+		for(int ii=0;ii<ct_NPixels;ii++)
+		  {
+		    if( nsb_phepns_rotated[ict][ii]>riseDiskThres)
+		      fpixelthres[ii]=secureDiskThres;
+		  }
 	  
-	    Trigger_CT[ict]->SetThreshold(fpixelthres);
+	      Trigger_CT[ict]->SetThreshold(fpixelthres);
 				 
-	    Trigger_CT[ict]->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. 
-	    //
+	      Trigger_CT[ict]->Diskriminate() ; 
+
+	      //
+	      //   Look if in all the signals in the trigger signal branch
+	      //   is a possible Trigger. Therefore we have to discriminate all
+	      //   the simulated analog signals (Method Diskriminate in class
+	      //   MTrigger). We look simultaneously for the moments at which
+	      //   there are more than TRIGGER_MULTI pixels above the 
+	      //   CHANNEL_THRESHOLD. 
+	      //
 	    
 	    Lev0MT[ict] = (Short_t) Trigger_CT[ict]->ZeroLevel() ; 
@@ -2344,25 +2403,27 @@
 	    //
 	  
-	    if ( Lev0MT[ict] > 0 || Write_All_Images) {
-
+	    if ( Lev0MT[ict] > 0 || Write_All_Images)
 	      Lev1MT[ict]= Trigger_CT[ict]->FirstLevel();
-	    }
-	    if (Lev1MT[ict]>0){
+
+	    if (Lev1MT[ict]>0)
 	      ++ntrigger[ict];
-	    }
+
 	  }
 
 	  Int_t NumImages = 0;
 	  Int_t CT_triggered=0;
-	  for(int ict=0;ict<ct_Number;ict++){
-	    if(NumImages==0 && Lev1MT[ict]>0)
-	      CT_triggered=ict;
-	    NumImages = (NumImages>=Lev1MT[ict])?NumImages:1;
-	    Lev0MT[ict]=1;
-	    if (Lev1MT[ict]==0 && Write_All_Images){ 
-	      NumImages=1;
-	      Lev0MT[ict]=0;
+	  for(int ict=0;ict<ct_Number;ict++)
+	    {
+	      if(NumImages==0 && Lev1MT[ict]>0)
+		CT_triggered=ict;
+	      NumImages = (NumImages>=Lev1MT[ict]) ? NumImages : 1;
+	      Lev0MT[ict]=1;
+
+	      if (Lev1MT[ict]==0 && Write_All_Images)
+		{
+		  NumImages=1;
+		  Lev0MT[ict]=0;
+		}
 	    }
-	  }
 
 	  for(int ict=0;ict<ct_Number;ict++){
@@ -2419,8 +2480,9 @@
 // 		    if(!Fadc_CT[ict]->IsPixelUsed(i)) continue;
 //
-		    for (j=0;j<FADC_SLICES;j++){
-		      fadcValues->AddAt(Fadc_CT[ict]->GetFadcSignal(i,j),j);
-		      fadcValuesLow->AddAt(Fadc_CT[ict]->GetFadcLowGainSignal(i,j),j);
-		    }
+		    for (j = 0; j < FADC_slices_written; j++)
+		      {
+			fadcValues->AddAt(Fadc_CT[ict]->GetFadcSignal(i,j),j);
+			fadcValuesLow->AddAt(Fadc_CT[ict]->GetFadcLowGainSignal(i,j),j);
+		      }
 		    EvtData[ict]->AddPixel(i,fadcValues,0);
 		    EvtData[ict]->AddPixel(i,fadcValuesLow,kTRUE);
@@ -3929,9 +3991,7 @@
   // reset variables
 
-  for ( i=0; i<camgeom->GetNumPixels(); ++i ){
-    
+  for ( i=0; i<camgeom->GetNumPixels(); ++i )
     nphe[i] = 0.0;
-    
-  }
+
 
   *itotnphe = 0;
@@ -4171,20 +4231,21 @@
 
 
-  // open input file 
-  
-  log(SIGNATURE, "Opening starfield input \"rfl\" file %s\n", iname );
-
-  infile = fopen( iname, "r" );
-
-  // check if the starfield input file exists
-
-  if ( infile == NULL ) {
-
-    log( SIGNATURE, "Cannot open starfield input file: %s\n", iname );
-
-    log( SIGNATURE, "There is not NSB from the Stars\n");
-
-    return (0);
-  }
+  if (strlen(iname) == 0)
+    {
+      log( SIGNATURE, "No starfield input file has been provided.\n");
+      return (0);
+    }
+  else // check if the starfield input file exists and open it
+    {
+      log(SIGNATURE, "Opening starfield input \"rfl\" file %s\n", iname );
+      infile = fopen( iname, "r" );
+
+      if ( infile == NULL )
+	{
+	  log( SIGNATURE, "ERROR! Cannot open starfield input file: %s\n", iname );
+	  exit(-1);
+	}
+    }
+
 
   // get signature, and check it
@@ -4211,5 +4272,7 @@
 			      FADC_shape_out,
 			      FADC_resp_integ_out,FADC_resp_fwhm_out,
-			      get_trig_delay());
+			      get_trig_delay(),
+			      FADC_slices_per_ns,
+			      FADC_slices_written);
 
   // initialize flag  
@@ -4363,4 +4426,12 @@
 //
 // $Log: not supported by cvs2svn $
+// Revision 1.71  2004/09/17 09:20:52  moralejo
+//
+// Updated some calls to current version of Mars:
+//
+// - EvtData[i]->InitRead(RunHeader)  instead of  EvtData[i]->Init(RunHeader);
+// - MRawRunHeader::kMagicNumber instead of just kMagicNumber
+// - EvtData[i]->ResetPixels (0, 0) instead of EvtData[i]->DeletePixels();
+//
 // Revision 1.70  2004/09/16 15:23:12  moralejo
 //
@@ -4524,5 +4595,5 @@
 // A small back has been solved. Before, while not using the option
 // writte_all_images, not all triggered showers were stored. Now it is solved.
-// For that it is importatn taht the less restrictive trigger option is
+// For that it is important that the less restrictive trigger option is
 // checked first.
 // A new facility has been introduced and now one can choose the step size in
