Index: trunk/MagicSoft/Simulation/Detector/Camera/camera.cxx
===================================================================
--- trunk/MagicSoft/Simulation/Detector/Camera/camera.cxx	(revision 407)
+++ trunk/MagicSoft/Simulation/Detector/Camera/camera.cxx	(revision 408)
@@ -21,7 +21,7 @@
 //
 // $RCSfile: camera.cxx,v $
-// $Revision: 1.9 $
-// $Author: blanch $ 
-// $Date: 2000-06-13 13:25:24 $
+// $Revision: 1.10 $
+// $Author: MagicSol $ 
+// $Date: 2000-07-04 14:10:20 $
 //
 ////////////////////////////////////////////////////////////////////////
@@ -200,5 +200,5 @@
 
 static int Write_McEvt  = TRUE;
-static int Write_McTrig = FALSE;
+static int Write_McTrig = TRUE;
 static int Write_RawEvt = FALSE;
 
@@ -220,4 +220,14 @@
 //@: flag: TRUE: loop trigger analysis over several thresholds, multiplicities and topologies; FALSE: a single trigger configuration
 static int Trigger_Loop = FALSE;
+
+//@: Properties of the trigger
+static float Trigger_gate_length = 3.0;
+static float Trigger_response_ampl = 1.0;
+static float Trigger_response_fwhm = 2.0;
+
+//@: Trigger conditions for a single trigger mode
+static float Trigger_threshold = 7.0;
+static int Trigger_multiplicity = 4;
+static int Trigger_topology = 2;
 
 //@: Upper and lower edges of the trigger loop
@@ -368,11 +378,9 @@
 
   char datname[256];          //@< data (ASCII) output file name
-  char diagname[256];         //@< diagnistic output file (ROOT format)
 
   char rootname[256] ;        //@< ROOT file name 
+  char rootname_loop[256] ;   //@< ROOT file name 
 
   char parname[256];          //@< parameters file name
-
-  char sign[20];              //@< initialize sign
 
   char flag[SIZE_OF_FLAGS + 1];  //@< flags in the .rfl file
@@ -428,6 +436,4 @@
   float fCorrection;          //@< Factor to apply to pixel values (def. 1.)
 
-  int trigger;                //@< trigger flag 
-  int itrigger;               //@< index of pixel fired
   int ntrigger = 0;           //@< number of triggers in the whole file
   int ithrescount;            //@< counter for loop over threshold trigger
@@ -441,10 +447,5 @@
   float degTriggerZone;       //@< trigger area in the camera (radius, in deg.)
 
-  float dtheta, dphi;         //@< deviations of CT from shower axis
-
   int still_in_loop = FALSE;
-
-  float *image_data;
-  int nvar, hidt;
 
   struct camera cam; // structure holding the camera definition
@@ -527,6 +528,12 @@
   FADC_Scan = get_FADC_Scan();
   Trigger_Scan = get_Trigger_Scan();
+
+  get_Trigger_properties( &Trigger_gate_length, &Trigger_response_ampl, &Trigger_response_fwhm);
+
   Trigger_Loop = get_Trigger_Loop(&Trigger_loop_lthres, &Trigger_loop_uthres, &Trigger_loop_lmult, &Trigger_loop_umult, &Trigger_loop_ltop, &Trigger_loop_utop);
 
+  if (!Trigger_Loop){
+    get_Trigger_Single (&Trigger_threshold, &Trigger_multiplicity, &Trigger_topology);
+  }
 
   // get filenames
@@ -535,6 +542,6 @@
   strcpy( starfieldname, get_starfield_filename() );
   strcpy( datname, get_data_filename() );
-  strcpy( diagname, get_diag_filename() );
   strcpy( rootname, get_root_filename() );
+  strcpy( rootname_loop, get_loop_filename() );
   strcpy( ct_filename, get_ct_filename() );
 
@@ -553,5 +560,5 @@
 
   log(SIGNATURE,
-      "%s:\n\t%20s:\t%s\n\t%20s:\t%s\n\t%20s:\t%s\n\t%20s:\t%s\n\t%20s:\t%s\n\t%20s:\t%s\n",
+      "%s:\n\t%20s:\t%s\n\t%20s:\t%s\n\t%20s:\t%s\n\t%20s:\t%s\n\t%20s:\t%s\n",
       "Filenames",
       "In", inname, 
@@ -559,9 +566,27 @@
       "CT", ct_filename, 
       "Data", datname,
-      "Diag", diagname,
       "ROOT",  rootname 
       );
 
-  
+  // log Trigger information
+
+  if (Trigger_Loop) {
+    log(SIGNATURE,
+	"%s:\n\t%20s: %i - %i\n\t%20s: %i - %i\n\t%20s: %i - %i\n\t%20s\n",
+	"Trigger Loop mode",
+	"Threshold",Trigger_loop_lthres,Trigger_loop_uthres,
+	"Multiplicity",Trigger_loop_lmult,Trigger_loop_umult,
+	"Topology",Trigger_loop_ltop,Trigger_loop_utop,
+	rootname_loop);
+  }
+  else{
+    log(SIGNATURE,
+   	"%s:\n\t%20s: %f\n\t%20s: %i\n\t%20s: %i\n",
+    	"Single Trigger mode",
+    	"Threshold",Trigger_threshold,
+    	"Multiplicity",Trigger_multiplicity,
+    	"Topology",Trigger_topology);
+  }   
+ 
   // log flags information
 
@@ -630,4 +655,5 @@
       log(SIGNATURE, "\tshower # %d\n", Skip[i]);
   }
+
   
   // read parameters from the ct.def file
@@ -647,5 +673,5 @@
   // initialise instance of Trigger and FADC classes
 
-  MTrigger  Trigger ;         //@< A instance of the Class MTrigger 
+  MTrigger  Trigger(Trigger_gate_length, Trigger_response_ampl, Trigger_response_fwhm);         //@< A instance of the Class MTrigger 
 
   MMcTrig *McTrig   = new MMcTrig() ; 
@@ -659,23 +685,44 @@
   MMcEvt  *McEvt = new MMcEvt (); 
 
-  // initalize the ROOT file 
+  //  
+  // initalize a temporal ROOT file 
   //
-
-  TFile outfile ( rootname , "RECREATE" ); 
-
+  
+  TFile outfile_temp ( rootname , "RECREATE" ); 
+
+  
   //      create a Tree for the Event data stream 
-
   TTree EvtTree("EvtTree","Events of Run");
 
   Int_t bsize=128000; Int_t split=1;
 
-  EvtTree.Branch("MRawEvt","MRawEvt", 
-  		 &Evt, bsize, split);
+  if(!Trigger_Loop){
+    
+    EvtTree.Branch("MRawEvt","MRawEvt", 
+		   &Evt, bsize, split);
+  }
 
   EvtTree.Branch("MMcEvt","MMcEvt", 
-  		 &McEvt, bsize, split);
-
+		 &McEvt, bsize, split);
+  
   EvtTree.Branch("MMcTrig","MMcTrig", 
-		   &McTrig, bsize, split);
+		 &McTrig, bsize, split);
+
+  
+  //      create a Tree for the Event data stream 
+  TTree EvtTree_loop("EvtTree","Events of Run");
+
+  if (Trigger_Loop && Write_RawEvt){
+    
+    EvtTree_loop.Branch("MRawEvt","MRawEvt", 
+			&Evt, bsize, split);
+    
+    EvtTree_loop.Branch("MMcEvt","MMcEvt", 
+			&McEvt, bsize, split);
+    
+    EvtTree_loop.Branch("MMcTrig","MMcTrig", 
+			&McTrig, bsize, split);
+    
+  }
 
   unsigned short ulli = 0 ; 
@@ -697,48 +744,4 @@
   }
   
-  // for safety and for dimensioning image_data: count the elements in the 
-  // diagnostic data branch
-
-  i=0;
-  i++; // "n"
-  i++; // "primary"
-  i++; // "energy"
-  i++; // "cored"
-  i++; // "impact"
-  i++; // "xcore"
-  i++; // "ycore"
-  i++; // "theta"
-  i++; // "phi"
-  i++; // "deviations"
-  i++; // "dtheta"
-  i++; // "dphi"
-  i++; // "trigger"
-  i++; // "ncphs"
-  i++; // "maxpassthr_phe"    
-  i++; // "nphes"
-  i++; // "nphes2"
-  i++; // "length"
-  i++; // "width"
-  i++; // "dist"
-  i++; // "xdist"
-  i++; // "azw"
-  i++; // "miss"
-  i++; // "alpha"
-  i++; // "conc2"
-  i++; // "conc3"
-  i++; // "conc4"
-  i++; // "conc5"
-  i++; // "conc6"
-  i++; // "conc7"
-  i++; // "conc8"
-  i++; // "conc9"
-  i++; // "conc10"
-  i++; // "asymx"
-  i++; // "asymy"
-  i++; // "phiasym"
-
-  nvar = i;
-  image_data = new float[nvar];
-
   // set plate scale (deg/cm) and trigger area (deg)
 
@@ -849,4 +852,23 @@
     // reading .rfl files 
     if(!isA( flag, FLAG_START_OF_RUN )){
+
+      //We write some trigger information in data file before exit
+
+      datafile<<ntshow<<" event(s), with a total of "<<ntcph<<" C.photons"<<endl;
+      if (Trigger_Loop){
+	datafile<<"Fraction of triggers: "<<endl;
+	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++){
+	      datafile<<"Thres "<<ithrescount<<", Multi "<<imulticount<<", Topo"<<itopocount<<": ";
+	      datafile<<((float)ntriggerloop[ithrescount-Trigger_loop_lthres][imulticount-Trigger_loop_lmult][itopocount-Trigger_loop_ltop] / ((float)ntshow) * 100.0)<<"% ("<<ntriggerloop[ithrescount-Trigger_loop_lthres][imulticount-Trigger_loop_lmult][itopocount-Trigger_loop_ltop]<<" out of "<<ntshow<<")"<<endl;
+	    }
+	  }   
+	}
+      }
+      else{
+	datafile<<"Fraction of triggers: "<<((float)ntrigger) / ((float)ntshow) * 100.0<<" ("<<ntrigger<<" out of "<<ntshow<<" )"<<endl;
+      }
+      //  We exit
       error( SIGNATURE, "Expected start of run flag, but found: %s\n", flag );
     }
@@ -1065,6 +1087,5 @@
 	
 	//   We study several trigger conditons
-	if(Trigger_Loop)
-
+	if(Trigger_Loop){
 	  //  Loop over trigger threshold
 	  for (ithrescount=Trigger_loop_lthres;ithrescount<=Trigger_loop_uthres;ithrescount++){
@@ -1089,5 +1110,5 @@
 
 	      Lev0=(Short_t) Trigger.ZeroLevel();
-	      if (Lev0>0){
+	      if (Lev0>0 || Write_All_Images){
 		Lev1=Lev2=0;
 
@@ -1102,5 +1123,4 @@
 		  //   Start the First Level Trigger simulation
 		  //
-
 		  McTrig->SetFirstLevel (Lev1=Trigger.FirstLevel());
 		  if(Lev1>0) {
@@ -1108,5 +1128,5 @@
 		    McTrig->SetTopology(itopocount);
 		    McTrig->SetMultiplicity(imulticount);
-		    McTrig->SetThreshold(fpixelthres);
+		    McTrig->SetThreshold(fpixelthres);		  
 		  }
 		  if(Lev1==0 && Write_All_Images){
@@ -1120,10 +1140,5 @@
 		    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
 		    
@@ -1132,35 +1147,65 @@
 			fadcValues[j]=fadc.GetFadcSignal(i,j);
 		      }
+		    }
+
+		    if( Write_RawEvt ){
+		      //
+		      //  Fill the header of this event 
+		      //
+		      
+		      Evt->FillHeader ( (UShort_t) (ntshow + nshow) ,  20 ) ; 
+		      
+		      //   fill pixel information
+		      
 		      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_loop.Fill() ;
+
+		      //    clear all
+		      Evt->Clear() ; 
+		      McEvt->Clear() ;  
 		    }
-		    //
-		    //   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() ;  
-		    
 		  }
+		  //
+		  //   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) ;
+
+		  EvtTree.Fill() ;
+	
 		  McTrig->Clear() ;
 		}
@@ -1169,8 +1214,15 @@
 	    }
 	  }
-
+	}
 	//  We study a single trigger condition
 	else {
 
+	  //  Setting trigger conditions
+	  Trigger.SetMultiplicity(Trigger_multiplicity);
+	  Trigger.SetTopology(Trigger_topology);
+	  for (i=0;i<TRIGGER_PIXELS;i++)
+	    fpixelthres[i]=Trigger_threshold;
+	  Trigger.SetThreshold(fpixelthres);
+				 
 	  Trigger.Diskriminate() ; 
 
@@ -1192,6 +1244,6 @@
 	  //
 	  
-	  if ( Lev0 > 0 ) {
-	    McTrig->SetFirstLevel (Lev1 = Trigger.FirstLevel());
+	  if ( Lev0 > 0 || Write_All_Images) {
+	    McTrig->SetFirstLevel (Lev1= Trigger.FirstLevel());
 	  }
 	  if (Lev1>0){
@@ -1217,5 +1269,5 @@
 	    //  Fill the header of this event 
 	    //
-       
+	    
 	    Evt->FillHeader ( (UShort_t) (ntshow + nshow) ,  20 ) ; 
 	    
@@ -1247,13 +1299,14 @@
 	    
 	    //   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 
@@ -1353,16 +1406,37 @@
   // put the Event to the root file
   //--
-  
+
+  EvtTree.Write() ; 
+  outfile_temp.Write() ;
+  outfile_temp.Close() ; 
+  
+  //  
+  // initalize the ROOT file 
+  //
+  
+  TFile outfile ( rootname , "RECREATE" ); 
   EvtTree.Write() ; 
   outfile.Write() ;
   outfile.Close() ; 
 
- 
+  if(Trigger_Loop && Write_RawEvt){
+    //++
+    // put the Event to the special root file
+    //--
+    TFile outfile_loop( rootname_loop , "RECREATE" );
+    EvtTree_loop.Write() ; 
+    outfile_loop.Write() ;
+
+    outfile_loop.Close() ; 
+  }
   // close input file
   
   log( SIGNATURE, "%d event(s), with a total of %d C.photons\n", 
        ntshow, ntcph );
+  datafile<<ntshow<<" event(s), with a total of "<<ntcph<<" C.photons"<<endl;
   if (Trigger_Loop){
+    log( SIGNATURE, "Trigger Mode. \n");
     log( SIGNATURE, "Fraction of triggers: \n");
+    datafile<<"Fraction of triggers: "<<endl;
     for (ithrescount=Trigger_loop_lthres;ithrescount<=Trigger_loop_uthres;ithrescount++){
       for (imulticount=Trigger_loop_lmult;imulticount<=Trigger_loop_umult;imulticount++){
@@ -1370,4 +1444,6 @@
 	  log( SIGNATURE, "Thres %d, Multi %d, Topo %d: %5.1f%% (%d out of %d)\n", 
 	       ithrescount,imulticount,itopocount,((float)ntriggerloop[ithrescount-Trigger_loop_lthres][imulticount-Trigger_loop_lmult][itopocount-Trigger_loop_ltop] / ((float)ntshow) * 100.0), ntriggerloop[ithrescount-Trigger_loop_lthres][imulticount-Trigger_loop_lmult][itopocount-Trigger_loop_ltop], ntshow);
+	  datafile<<"Thres "<<ithrescount<<", Multi "<<imulticount<<", Topo"<<itopocount<<": ";
+	  datafile<<((float)ntriggerloop[ithrescount-Trigger_loop_lthres][imulticount-Trigger_loop_lmult][itopocount-Trigger_loop_ltop] / ((float)ntshow) * 100.0)<<"% ("<<ntriggerloop[ithrescount-Trigger_loop_lthres][imulticount-Trigger_loop_lmult][itopocount-Trigger_loop_ltop]<<" out of "<<ntshow<<")"<<endl;
 	}
       }   
@@ -1377,4 +1453,5 @@
     log( SIGNATURE, "Fraction of triggers: %5.1f%% (%d out of %d)\n", 
 	 ((float)ntrigger) / ((float)ntshow) * 100.0, ntrigger, ntshow);
+    datafile<<"Fraction of triggers: "<<((float)ntrigger) / ((float)ntshow) * 100.0<<" ("<<ntrigger<<" out of "<<ntshow<<" )"<<endl;
   }
 
@@ -2603,5 +2680,5 @@
   int i, j, k, ii; // counters
 
-  MTrigger trigger;
+  MTrigger trigger(Trigger_gate_length, Trigger_response_ampl, Trigger_response_fwhm);
   MFadc flashadc;
 		      
@@ -2630,6 +2707,15 @@
 
   infile = fopen( iname, "r" );
-  if ( infile == NULL ) 
-    error( SIGNATURE, "Cannot open starfield input file: %s\n", iname );
+
+  // check if the satrfield 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);
+  }
   
   // get signature, and check it
@@ -2869,4 +2955,9 @@
 //
 // $Log: not supported by cvs2svn $
+// Revision 1.9  2000/06/13 13:25:24  blanch
+// The multiple arrays have been replaced, since they do not work
+// in alpha machines. Now we are using pointers and the command new
+// to allocate memory.
+//
 // Revision 1.8  2000/05/11 13:57:27  blanch
 // The option to loop over several trigger configurations has been included.
