Index: trunk/MagicSoft/Simulation/Detector/Camera/camera.cxx
===================================================================
--- trunk/MagicSoft/Simulation/Detector/Camera/camera.cxx	(revision 379)
+++ trunk/MagicSoft/Simulation/Detector/Camera/camera.cxx	(revision 383)
@@ -21,7 +21,7 @@
 //
 // $RCSfile: camera.cxx,v $
-// $Revision: 1.6 $
+// $Revision: 1.7 $
 // $Author: blanch $ 
-// $Date: 2000-03-20 18:35:11 $
+// $Date: 2000-03-24 18:10:46 $
 //
 ////////////////////////////////////////////////////////////////////////
@@ -48,4 +48,6 @@
 
 #include "TROOT.h"
+
+#include "TApplication.h"
 
 #include "TFile.h"
@@ -54,5 +56,6 @@
 #include "TCanvas.h"
 
-#include "MDiag.h"
+#include "MTrigger.hxx"
+#include "MFadc.hxx"
 
 #include "MRawEvt.h"
@@ -60,6 +63,4 @@
 #include "MMcTrig.hxx"
 
-#include "MTrigger.hxx"
-
 /*!@" 
 
@@ -69,4 +70,5 @@
 
 #include "camera.h"
+
 //!@}
 
@@ -88,4 +90,5 @@
 #undef  __DEBUG__
 
+
 //!@}
 
@@ -196,4 +199,8 @@
 static int Write_All_Data = FALSE;
 
+static int Write_McEvt  = TRUE;
+static int Write_McTrig = FALSE;
+static int Write_RawEvt = FALSE;
+
 //@: flag: TRUE: selection on the energy
 static int Select_Energy = TRUE;
@@ -204,4 +211,10 @@
 //@: Upper edge of the selected energy range (in GeV)
 static float Select_Energy_ue = 100000.0; 
+
+//@: flag: TRUE: show all fadc singnal in the screen; FALSE: don't
+static int FADC_Scan = FALSE;
+
+//@: flag: TRUE: show all trigger singnal in the screen; FALSE: don't
+static int Trigger_Scan = FALSE;
 
 //!@}
@@ -319,22 +332,4 @@
   @"*/
 
-/*!@"
-
-  The following are the set of parameters calculated for each image.
-  The routines for their calculations are in |moments.cxx|.
-
-  @"*/
-
-//!@{
-// parameters of the images
-
-static Moments_Info *moments_ptr; 
-static LenWid_Info *lenwid_ptr;
-
-static float *maxs;
-static int *nmaxs;
-static float length, width, dist, xdist, azw, miss, alpha, *conc; 
-static float phiasym, asymx, asymy;
-static float charge, smax, maxtrigthr_phe;
 
 //!@}
@@ -360,9 +355,13 @@
   char inname[256];           //@< input file name
   char starfieldname[256];    //@< starfield input file name
+
   char datname[256];          //@< data (ASCII) output file name
   char diagname[256];         //@< diagnistic output file (ROOT format)
+
   char rootname[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
@@ -372,4 +371,5 @@
 
   MCEventHeader mcevth;       //@< Event Header class (MC)
+  MCCphoton cphoton;          //@< Cherenkov Photon class (MC)
 
   Photoelectron *photoe = NULL; //@< array of the photoelectrons of one event
@@ -421,7 +421,4 @@
   int itrigger;               //@< index of pixel fired
   int ntrigger = 0;           //@< number of triggers in the whole file
-  //  MTrigger  Trigger ;         //@< A instance of the Class MTrigger 
-
-  //	MMcTrig *McTrig   = new MMcTrig() ; 
 
   float plateScale_cm2deg;    //@< plate scale (deg/cm)
@@ -508,4 +505,11 @@
   Write_All_Data = get_write_all_data();
 
+  Write_McEvt  = get_write_McEvt()  ; 
+  Write_McTrig = get_write_McTrig() ; 
+  Write_RawEvt = get_write_RawEvt() ; 
+
+  FADC_Scan = get_FADC_Scan();
+  Trigger_Scan = get_Trigger_Scan();
+
   // get filenames
 
@@ -550,4 +554,13 @@
       "Write_All_Images",  ONoff(Write_All_Images), 
       "Write_All_Data",    ONoff(Write_All_Data));
+
+  // log flags information
+
+  log(SIGNATURE,
+      "%s:\n\t%20s: %s\n\t%20s: %s\n\t%20s: %s\n",
+      "Root output",
+      "Write_McEvt",   ONoff(Write_McEvt),  
+      "Write_McTrig",  ONoff(Write_McTrig),  
+      "Write_RawEvt",  ONoff(Write_RawEvt));
       
   // log parameters information
@@ -597,27 +610,18 @@
   photoe = new Photoelectron[iMAXNUMPHE];
 
+  Int_t Lev0, Lev1, Lev2 ; 
+
   // initialise ROOT
 
   TROOT simple("simple", "MAGIC Telescope Monte Carlo");
 
-  // prepare ROOT tree for the diagnostic data 
-  
-  TFile *hfile;
-
-  hfile = new TFile( diagname,"RECREATE", "MAGIC Telescope MC diagnostic data");
-  
-  // Create the ROOT Tree for the diagnostic data
-  
-  TTree *tree = new TTree("T","MAGIC Telescope MC diagnostic data");
-  tree->SetAutoSave(100000000); 
-  
-  Int_t split = 1;
-  Int_t bsize = 64000;
-  MDiagEventobject  *event = 0;
-  
-  // Create one branch. If splitlevel is set, event is a superbranch
-  // creating a sub branch for each data member of the Eventobject event.
-  
-  tree->Branch("event", "MDiagEventobject", &event, bsize, split);
+  // initialise instance of Trigger and FADC classes
+
+  MTrigger  Trigger ;         //@< A instance of the Class MTrigger 
+
+  MMcTrig *McTrig   = new MMcTrig() ; 
+
+  MFadc fadc ;                //@< A instance of the Class MFadc
+
 
   // Prepare the raw data output
@@ -635,5 +639,5 @@
   TTree EvtTree("EvtTree","Events of Run");
 
-  bsize=128000; split=1;
+  Int_t bsize=128000; Int_t split=1;
 
   EvtTree.Branch("MRawEvt","MRawEvt", 
@@ -643,5 +647,25 @@
   		 &McEvt, bsize, split);
 
-
+  EvtTree.Branch("MMcTrig","MMcTrig", 
+		   &McTrig, bsize, split);
+
+  unsigned short ulli = 0 ; 
+
+  TApplication theAppTrigger("App", &argc, argv);
+
+  if (gROOT->IsBatch()) {
+    fprintf(stderr, "%s: cannot run in batch mode\n", argv[0]);
+    //    return 1;
+  }
+
+  if(FADC_Scan){
+  TApplication theAppFadc("App", &argc, argv);
+
+  if (gROOT->IsBatch()) {
+    fprintf(stderr, "%s: cannot run in batch mode\n", argv[0]);
+    //    return 1;
+  }
+  }
+  
   // for safety and for dimensioning image_data: count the elements in the 
   // diagnostic data branch
@@ -720,12 +744,4 @@
     }
   
-//     for(i=0; i<cam.inumpixels; i++){
-//       cout << i;
-//       for(j=0; j<iNUMWAVEBANDS; j++){
-// 	   cout << " " << j << " " << nsbrate_phepns[i][j];
-//       }
-//       cout << "\n";
-//     }
-
     // calculate diffuse rate correcting for the pixel size
 
@@ -780,6 +796,4 @@
   fnpixclean = new float [ ct_NPixels ];
 
-  moments_ptr = moments( anaPixels, NULL, NULL, 0.0, 1 );
-
   // initialize baseline 
 
@@ -787,8 +801,4 @@
     baseline_mv[i] = 0.;
   }
-
-  //  Instance of the Trigger class
-
-  MTrigger  Trigger ;         //@< A instance of the Class MTrigger 
        
   //!@' @#### Main loop.
@@ -820,5 +830,11 @@
 
       while( isA( flag, FLAG_START_OF_EVENT   )){ // while there is a next event
-	
+
+	//
+	// Clear Trigger and Fadc
+	//
+      	Trigger.Reset() ; 
+	fadc.Reset() ; 
+
 	++nshow;
 	log(SIGNATURE, "Event %d(+%d)\n", nshow, ntshow);
@@ -999,12 +1015,12 @@
 
 	//   
-	//   remove the prvious values of the analog signal for
-	//   each pixel and put the new ones from photoe array
+	//   Put  values of the analog signal for
+	//   each pixel from photoe array
 	//
-	Trigger.Reset();
 	
 	for(i=0;i<inumphe;i++){
-	  Trigger.FillShow(photoe[i].ipixnum,float((photoe[i].iarrtime_ns-arrtmin_ns+TOTAL_TRIGGER_TIME/10.0)));
-	}
+	  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)))); 
+}
 
 	//
@@ -1014,5 +1030,9 @@
 	//   
 	Trigger.ElecNoise() ;
-	
+	fadc.ElecNoise() ;
+	
+
+	Trigger.Diskriminate() ; 
+
 	//
 	//   look if in all the signals in the trigger signal branch
@@ -1024,18 +1044,14 @@
 	//
 
-	Trigger.Diskriminate();
-
-	//McTrig->SetZeroLevel(trigger = (Short_t) Trigger.ZeroLevel()) ; 
-	trigger = (Short_t) Trigger.ZeroLevel();
+	McTrig->SetZeroLevel( Lev0 = (Short_t) Trigger.ZeroLevel() ) ; 
+
+	Lev1 = Lev2 = 0 ; 
 	
 	//
 	//   Start the First Level Trigger simulation
 	//
-	if ( trigger > 0 ) {
-	  //	  McTrig->SetFirstLevel ( trigger = Trigger.FirstLevel() )  ;
-	  trigger = Trigger.FirstLevel();
-	}
-	if ( trigger>0 ) {
-	  cout << "TRIG"<<"  "<<thetashw <<"  "<< trigger  << endl;
+
+	if ( Lev0 > 0 ) {
+	  McTrig->SetFirstLevel ( Lev1 = Trigger.FirstLevel() )  ;
 	}
 
@@ -1044,207 +1060,109 @@
 // 	}
 	
-	if ( trigger>0 ) {
-	  
+	//if ( trigger>0 ) {
+	if (Lev1>0){
+	
 	  itrigger = i;
 	  ++ntrigger;
 	  
 	  memcpy( fnpixclean, fnpix, sizeof(float) * ct_NPixels );
-	  
-	  
-#ifdef __ISLANDS__
-	  
-	  //!@' @#### Islands algorithm.
-	  //@'
-	  
-	  //++
-	  // islands counting, and cleanning
-	  //--
-	  
-	  if ( countIslands )
-	    do_islands( ct_NPixels, fnpixclean, pixneig, npixneig, 
-			countIslands, nIslandsCut);
-	  
-#endif // __ISLANDS__
-	  
-	  
-	  //!@' @#### Calculation of parameters of the image.
-	  //@'
-	  
-	  //++
-	  // moments calculation
-	  //--
-	  
-	  // calculate moments and other things
-	  
-	  moments_ptr = moments( anaPixels, fnpixclean, pixary,
-				 plateScale_cm2deg, 0 );
-	  
-	  charge = moments_ptr->charge ;
-	  smax   = moments_ptr->smax   ;
-	  maxs   = moments_ptr->maxs   ;
-	  nmaxs  = moments_ptr->nmaxs  ;
-	  length = moments_ptr->length ;
-	  width  = moments_ptr->width  ;
-	  dist   = moments_ptr->dist   ;
-	  xdist  = moments_ptr->xdist  ;
-	  azw    = moments_ptr->azw    ;
-	  miss   = moments_ptr->miss   ;
-	  alpha  = moments_ptr->alpha  ;
-	  conc   = moments_ptr->conc   ;
-	  asymx  = moments_ptr->asymx  ;
-	  asymx  = moments_ptr->asymx  ;
-	  phiasym= moments_ptr->phi;
-	  
-	  lenwid_ptr = lenwid( anaPixels, fnpixclean, pixary,
-			       plateScale_cm2deg,
-			       ct_PixelWidth_corner_2_corner_half);
-	  
-	  
-	  // fill the diagnostic Tree
-	  
-	  event = new MDiagEventobject();
-	  
-	  i=0;
-	  image_data[i] =	event->n       = hidt/10; i++;
-	  image_data[i] =       event->primary = mcevth.get_primary(); i++;
-	  image_data[i] =	event->energy  = mcevth.get_energy(); i++;
-	  image_data[i] =	event->cored   = coreD;  i++;
-	  image_data[i] =	event->impact  = impactD; i++;
-	  image_data[i] =	event->xcore   = coreX; i++;
-	  image_data[i] =	event->ycore   = coreY; i++;
-	  image_data[i] =	event->theta   = mcevth.get_theta(); i++;
-	  image_data[i] =	event->phi     = mcevth.get_phi(); i++;
-	  image_data[i] =       event->deviations = mcevth.get_deviations (&dtheta, &dphi); i++;
-	  image_data[i] =       event->dtheta  = dtheta; i++;
-	  image_data[i] =       event->dphi    = dphi; i++;
-	  image_data[i] =       event->trigger = trigger; i++;
-	  image_data[i] =	event->ncphs   = ncph; i++;
-	  image_data[i] =	event->maxpassthr_phe   = maxtrigthr_phe; i++;
-	  image_data[i] =	event->nphes   = charge; i++;
-	  image_data[i] =	event->nphes2  = smax; i++;
-	  image_data[i] =	event->length  = length; i++;
-	  image_data[i] =	event->width   = width; i++;
-	  image_data[i] =	event->dist    = dist; i++;
-	  image_data[i] =	event->xdist   = xdist; i++;
-	  image_data[i] =	event->azw     = azw; i++;
-	  image_data[i] =	event->miss    = miss; i++;
-	  image_data[i] =	event->alpha   = alpha; i++;
-	  image_data[i] =	event->conc2   = conc[0]; i++;
-	  image_data[i] =	event->conc3   = conc[1]; i++;
-	  image_data[i] =	event->conc4   = conc[2]; i++;
-	  image_data[i] =	event->conc5   = conc[3]; i++;
-	  image_data[i] =	event->conc6   = conc[4]; i++;
-	  image_data[i] =	event->conc7   = conc[5]; i++;
-	  image_data[i] =	event->conc8   = conc[6]; i++;
-	  image_data[i] =	event->conc9   = conc[7]; i++;
-	  image_data[i] =	event->conc10  = conc[8]; i++;
-	  image_data[i] =	event->asymx   = asymx; i++;
-	  image_data[i] =	event->asymy   = asymy; i++;
-	  image_data[i] =	event->phiasym = phiasym; i++;
-	  
-	  // there should be "nvar" variables
-	  
-	  if ( i != nvar ) 
-	    error( SIGNATURE, "Wrong entry number for diagnostic data.\n" );
-	  
-	  tree->Fill();
-	  delete event;
-	  
-	  // put information in the data file, 
-	  
-	  datafile << ntrigger;
-	  for(i=0;i<nvar;i++) {
-	    datafile << ' ' << image_data[i];
+	}
+	//
+	//  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. ) ; 
 	  }
-	  
-	  // revert the fnpixclean matrix into fnpix
-	  // (now we do this, but maybe in a future we want to
-	  // use both fnpix and fnpixclean for different things
-	  
-	  memcpy( fnpix, fnpixclean, sizeof(float) * ct_NPixels );
-	  
-	  // put this information in the data file, 
-	  
-	  if ( Write_All_Data ) {
-	    datafile << ' ' << -9999;
-	    for ( i=0; i<ct_NPixels; ++i )
-	      datafile << ' ' << fnpix[i];
+	}
+
+	if(Trigger_Scan){
+	  if ( Lev0 > 0 ) {
+	    Trigger.ShowSignal(McEvt) ;
 	  }
-	  
-	  datafile << endl;  
-	  
-	  mcevth.set_trigger( TRUE );
-	  
-	  log(SIGNATURE, "TRIGGER\n");
-	  
-	} else { // ( trigger == FALSE )
-	  
-	  event = new MDiagEventobject();
-	  
-	  i=0;
-	  image_data[i] =	event->n       = hidt/10; i++;
-	  image_data[i] =       event->primary = mcevth.get_primary(); i++;
-	  image_data[i] =	event->energy  = mcevth.get_energy(); i++;
-	  image_data[i] =	event->cored   = coreD = mcevth.get_core(&coreX, &coreY); i++;
-	  image_data[i] =	event->impact  = coreD; i++;
-	  image_data[i] =	event->xcore   = coreX; i++;
-	  image_data[i] =	event->ycore   = coreY; i++;
-	  image_data[i] =	event->theta   = mcevth.get_theta(); i++;
-	  image_data[i] =	event->phi     = mcevth.get_phi(); i++;
-	  image_data[i] =       event->deviations = mcevth.get_deviations(&dtheta, &dphi); i++;
-	  image_data[i] =       event->dtheta = dtheta; i++;
-	  image_data[i] =       event->dphi = dphi; i++;
-	  image_data[i] =       event->trigger = trigger; i++;
-	  image_data[i] =	event->ncphs   = ncph; i++;
-	  image_data[i] =	event->maxpassthr_phe   = maxtrigthr_phe; i++;
-	  image_data[i] =	-1.; i++;
-	  image_data[i] =	-1.; i++;
-	  image_data[i] =	-1.; i++;
-	  image_data[i] =	-1.; i++;
-	  image_data[i] =	-1.; i++;
-	  image_data[i] =	-1.; i++;
-	  image_data[i] =	-1.; i++;
-	  image_data[i] =	-1.; i++;
-	  image_data[i] =	-1.; i++;
-	  image_data[i] =	-1.; i++;
-	  image_data[i] =	-1.; i++;
-	  image_data[i] =	-1.; i++;
-	  image_data[i] =	-1.; i++;
-	  image_data[i] =	-1.; i++;
-	  image_data[i] =	-1.; i++;
-	  image_data[i] =	-1.; i++;
-	  image_data[i] =	-1.; i++;
-	  image_data[i] =	-1.; i++;
-	  image_data[i] =	-1.; i++;
-	  image_data[i] =	-1.; i++;
-	  image_data[i] =	-1.; i++;
-	  
-	  // there should be "nvar" variables
-	  
-	  if ( i != nvar ) 
-	    error( SIGNATURE, "Wrong entry length for Ntuple.\n" );
-	  
-	  tree->Fill();
-	  delete event;
-	  
-	  // put this information in the data file, 
-	  
-	  if ( Write_All_Data ) {
+	}
+
+	//    clear all
+	Evt->Clear() ; 
+	McEvt->Clear() ; 
+	McTrig->Clear() ; 
+      
+	
+	//++++++++++++++++++++++++++++++++++++++++++++++++++
+	// at this point we have a camera full of
+	// ph.e.s
+	// we should first apply the trigger condition,
+	// and if there's trigger, then clean the image,
+	// calculate the islands statistics and the
+	// other parameters of the image (Hillas' parameters
+	// and so on).
+	//--------------------------------------------------
+	
+#ifdef __DEBUG__  
+	printf("\n");
+	
+	for ( ici=0; ici<PIX_ARRAY_SIDE; ++ici ) {
+	  
+	  for ( icj=0; icj<PIX_ARRAY_SIDE; ++icj ) {
 	    
-	    datafile << ntrigger;
-	    for ( i=0; i<nvar; ++i )
-	      datafile << ' ' << image_data[i];
-	    
-	    datafile << -9999;
-	    for ( i=0; i<ct_NPixels; ++i )
-	      datafile << ' ' << fnpix[i];
-	    
-	    datafile << endl;  
+	    if ( (int)pixels[ici][icj][PIXNUM] > -1 ) {
+	      
+	      if ( fnpix[(int)pixels[ici][icj][PIXNUM]] > 0. ) {
+		
+		printf ("@@ %4d %4d %10f %10f %4f (%4d %4d)\n", nshow, 
+			(int)pixels[ici][icj][PIXNUM], 
+			pixels[ici][icj][PIXX],
+			pixels[ici][icj][PIXY],
+			fnpix[(int)pixels[ici][icj][PIXNUM]], ici, icj);
+		
+	      } 
+	    }  
 	  }
-	  
-	  mcevth.set_trigger( FALSE );
-	  
-	} // trigger == FALSE 
-		
+	}
+	
+	for (i=0; i<ct_NPixels; ++i) {
+	  printf("%d (%d): ", i, npixneig[i]);
+	  for (j=0; j<npixneig[i]; ++i) 
+	    printf(" %d", pixneig[i][j]);
+	  printf("\n");
+	}
+	
+#endif // __DEBUG__
+	  	  	  
+	
 	//!@' @#### Save data.
 	//@'
@@ -1324,8 +1242,4 @@
   }
   datafile.close();
-
-  hfile->Write();
-
-  hfile->Close();
 
   // program finished
@@ -2827,4 +2741,7 @@
 //
 // $Log: not supported by cvs2svn $
+// 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.
+//
 // Revision 1.5  2000/02/18 17:40:35  petry
 // This version includes drastic changes compared to camera.cxx 1.4.
