Index: trunk/MagicSoft/Simulation/Detector/Camera/camera.cxx
===================================================================
--- trunk/MagicSoft/Simulation/Detector/Camera/camera.cxx	(revision 2388)
+++ trunk/MagicSoft/Simulation/Detector/Camera/camera.cxx	(revision 2389)
@@ -21,7 +21,7 @@
 //
 // $RCSfile: camera.cxx,v $
-// $Revision: 1.62 $
+// $Revision: 1.63 $
 // $Author: blanch $ 
-// $Date: 2003-09-26 11:25:07 $
+// $Date: 2003-10-17 19:38:31 $
 //
 ////////////////////////////////////////////////////////////////////////
@@ -207,4 +207,6 @@
 static float FADC_resp_fwhm_out = MFADC_RESPONSE_FWHM;
 static float FADC_noise = 2.0;
+static float DIGITAL_noise = 2.0;
+static float FADC_high2low = HIGH2LOWGAIN;
 
 //@: Trigger conditions for a single trigger mode
@@ -358,4 +360,5 @@
   float l2, m2, n2;           //@< auxiliary variables
   float num, den;      //@< auxiliary variables
+  float factorqe_NSB[100];          //@< factor on the NSB depending of QE
 
   int nshow=0;                //@< partial number of shower in a given run
@@ -390,4 +393,6 @@
   Float_t fadc_elecnoise[CAMERA_PIXELS];  //@< Electronic niose for each pixel
   Float_t fadc_pedestals[CAMERA_PIXELS];  //@< array for fadc pedestals values
+  Float_t fadc_sigma[CAMERA_PIXELS];      //@< array for fadc pedestals sigma
+  Float_t fadc_sigma_low[CAMERA_PIXELS];      //@< array for fadc pedestals sigma
 
   float ext[iNUMWAVEBANDS] = { //@< average atmospheric extinction in each waveband
@@ -567,6 +572,7 @@
       TriggerPixels[i]=TRIGGER_PIXELS_9;
       break;
-    default: camgeom[i]=new MGeomCamMagic;
-      TriggerPixels[i]=TRIGGER_PIXELS_1;
+    default:
+      error( SIGNATURE, "Camera geometry %i is not defined. Stoping camera program ...", GeometryCamera[i]);
+      exit(1);
       break;
     }
@@ -590,4 +596,5 @@
     read_QE(qe_filename,ict);
   }
+
   read_WC();
 
@@ -614,4 +621,5 @@
   get_FADC_properties( &FADC_response_ampl, &FADC_response_fwhm,
 		       &FADC_resp_ampl_out, &FADC_resp_fwhm_out);
+  FADC_high2low=get_High_to_Low();
 
   // FIXME --- tirgger properties may depend on the Camrea geometry
@@ -658,6 +666,7 @@
   // get different parameters of the simulation
 
-  addElecNoise = add_elec_noise(&FADC_noise, &Trigger_noise);
+  addElecNoise = add_elec_noise(&FADC_noise, &DIGITAL_noise, &Trigger_noise);
   simulateNSB = get_nsb( &meanNSB, &nphe2NSB );  
+
 
   // get selections on the parameters
@@ -677,4 +686,15 @@
     strcpy( qe_filename, get_qe_filename(ict));
 
+    // Look to factor for NSB respect to emi_coat PMTs
+    if(!strcmp(qe_filename,"../Data/qe-emi-coat.RFL.dat"))
+      factorqe_NSB[ict]=EMICOAT_NSB;
+    else if(!strcmp(qe_filename,"../Data/qe-emi.RFL.dat"))
+      factorqe_NSB[ict]=EMI_NSB;
+    else if(!strcmp(qe_filename, "../Data/qe-intevac_hpd.RFL.dat"))
+      factorqe_NSB[ict]=HPD_NSB;
+    else{
+      log(SIGNATURE,"WARNING : the factor for the diffuse NSB for this QE file (%s) is not known. The number of phe(ns stated in the inputcard will be used.\n",qe_filename); 
+      factorqe_NSB[ict]=1.0;
+    }
     log(SIGNATURE,
 	"%s:\n\t%20s:\t%s\n\t%20s:\t%s\n\t%20s\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",
@@ -689,4 +709,14 @@
 	);
     
+  // log parameters information
+  
+  log(SIGNATURE,
+      "%s:\n\t%20s: %f: %s\n\t%20s: %f\n\t%20s: %f\n",
+      "Parameters",
+      "NSB (phes/ ns 0.1*0.1 deg^2 239 m^2)", meanNSB*factorqe_NSB[ict], ONoff(simulateNSB),
+      "Pedestals = ", get_FADC_pedestal(),
+      "High to Low gain = ", FADC_high2low
+      );
+  
     // log Trigger information
     
@@ -741,13 +771,4 @@
       "Write_RawEvt",  ONoff(Write_RawEvt));
       
-  // log parameters information
-  
-  log(SIGNATURE,
-      "%s:\n\t%20s: %f: %s\n\t%20s: %f\n",
-      "Parameters",
-      "NSB (phes/pixel)", meanNSB, ONoff(simulateNSB),
-      "Pedestals = ", get_FADC_pedestal()
-      );
-  
   // log selections
   
@@ -785,8 +806,4 @@
   }    
 
-  // set all random numbers seeds
-
-  setall( get_seeds(0), get_seeds(1) );
-
   // get list of showers to evt. skip
 
@@ -836,4 +853,5 @@
 		    Trigger_response_ampl, 
 		    Trigger_response_fwhm);  //@< A instance of the Class MTrigger 
+    Trigger_CT[i]->SetSeed(UInt_t(i+get_seeds(0)));
   }
 
@@ -849,9 +867,10 @@
   // is active: secureDiskThres*10000+riseDiskThres
 
-  if(riseDiskThres>0.0)
-    qThreshold[0][0]=((UInt_t)secureDiskThres*100)*100+riseDiskThres;
-  else
-    qThreshold[0][0]=0.0;
-  
+  for (int ict=0; ict<ct_Number;ict++){
+    if(riseDiskThres>0.0)
+      qThreshold[ict][0]=((UInt_t)secureDiskThres*100)*100+riseDiskThres;
+    else
+      qThreshold[ict][0]=0.0;
+  } 
   //  Initialise McTrig information class if we want to save trigger informtion
 
@@ -920,9 +939,13 @@
 
     Fadc_CT[ict]->SetPedestals(input_pedestals);
+    Fadc_CT[ict]->SetHigh2LowGain(FADC_high2low);
     
     // Generate databse for the Fadc electronic noise
-    if (addElecNoise)        
+    if (addElecNoise){        
       Fadc_CT[ict]->SetElecNoise(FADC_noise);
-  }
+      Fadc_CT[ict]->SetDigitalNoise(DIGITAL_noise);
+    }
+  }
+
   // Prepare the raw data output
 
@@ -997,7 +1020,10 @@
 		      &HeaderTrig[0]);
   }
-  if (ct_Number==1)
+  if (ct_Number==1){
     HeaderTree.Branch("MGeomCam",GeometryName[0],
 		      &camdummy[0]);
+    HeaderTree.Branch("MMcConfigRunHeader","MMcConfigRunHeader",
+		      &McConfigRunHeader[0]);
+  }
   else{
     char branchname[20];
@@ -1010,4 +1036,13 @@
 			&camdummy[ict]);
     }
+    for(int i=0;i<numberBranches;i++){
+      sprintf(help,"%i",i+1);
+      strcpy (branchname, "MMcConfigRunHeader;");
+      strcat (branchname, & help[0]);
+      strcat (branchname, ".");
+      HeaderTree.Branch(branchname,"MMcConfigRunHeader",
+			&McConfigRunHeader[i]);
+    }
+    
   }
 
@@ -1113,6 +1148,12 @@
 
     HeaderFadc[0]->SetShape(0.0);
-    HeaderFadc[0]->SetAmplitud(FADC_response_ampl);
-    HeaderFadc[0]->SetFwhm(FADC_response_fwhm);
+    HeaderFadc[0]->SetAmplitud(FADC_response_ampl/
+			       sqrt(2*M_PI*FADC_response_fwhm
+				    *FADC_response_fwhm),
+			       FADC_resp_ampl_out/
+			       sqrt(2*M_PI*FADC_resp_fwhm_out
+				    *FADC_resp_fwhm_out));
+    HeaderFadc[0]->SetFwhm(FADC_response_fwhm,FADC_resp_fwhm_out);
+    HeaderFadc[0]->SetLow2High(FADC_high2low);
     HeaderFadc[0]->SetPedestal(&fadc_pedestals[0],
 			       ((MGeomCam*)(camgeom.UncheckedAt(0)))->GetNumPixels());
@@ -1122,7 +1163,14 @@
   if(!Trigger_Loop && Write_McFADC && ct_Number>1){
     for(int i=0;i<ct_Number;i++){
+      Fadc_CT[i]->GetPedestals(&fadc_pedestals[0]);
       HeaderFadc[i]->SetShape(0.0);
-      HeaderFadc[i]->SetAmplitud(FADC_response_ampl);
-      HeaderFadc[i]->SetFwhm(FADC_response_fwhm);
+      HeaderFadc[i]->SetAmplitud(FADC_response_ampl/
+				 sqrt(2*M_PI*FADC_response_fwhm
+				      *FADC_response_fwhm),
+				 FADC_resp_ampl_out/
+				 sqrt(2*M_PI*FADC_resp_fwhm_out
+				      *FADC_resp_fwhm_out));
+      HeaderFadc[i]->SetFwhm(FADC_response_fwhm,FADC_resp_fwhm_out);
+      HeaderFadc[i]->SetLow2High(FADC_high2low);
       HeaderFadc[i]->SetPedestal(&fadc_pedestals[0],((MGeomCam*)(camgeom.UncheckedAt(i)))->GetNumPixels());
       HeaderFadc[i]->SetElecNoise(&fadc_elecnoise[0],((MGeomCam*)(camgeom.UncheckedAt(i)))->GetNumPixels());
@@ -1134,7 +1182,15 @@
       for (imulticount=0;imulticount<=Trigger_loop_umult-Trigger_loop_lmult;imulticount++){
 	for(itopocount=0;itopocount<=Trigger_loop_utop-Trigger_loop_ltop;itopocount++){
+	  Fadc_CT[0]->GetPedestals(&fadc_pedestals[0]);
 	  HeaderFadc[iconcount]->SetShape(0.0);
-	  HeaderFadc[iconcount]->SetAmplitud(Trigger_response_ampl);
-	  HeaderFadc[iconcount]->SetFwhm(Trigger_response_fwhm);
+	  HeaderFadc[iconcount]->SetAmplitud(FADC_response_ampl/
+					     sqrt(2*M_PI*FADC_response_fwhm
+						  *FADC_response_fwhm),
+					     FADC_resp_ampl_out/
+					     sqrt(2*M_PI*FADC_resp_fwhm_out
+						  *FADC_resp_fwhm_out));
+	  HeaderFadc[iconcount]->SetFwhm(FADC_response_fwhm,
+					 FADC_resp_fwhm_out);
+	  HeaderFadc[iconcount]->SetLow2High(FADC_high2low);
 	  HeaderFadc[iconcount]->SetPedestal(&fadc_pedestals[0], ct_NPixels);
 	  HeaderFadc[iconcount]->SetElecNoise(&fadc_elecnoise[0], ct_NPixels);
@@ -1145,4 +1201,11 @@
   }
 
+  // UnSet flag in pixel 0 (not used for trigger) that indicates if secure pixel
+  // is active once it has been stored
+
+  for (int ict=0; ict<ct_Number;ict++){
+      qThreshold[ict][0]=999999.99;
+  } 
+
 
   //      create a Tree for the Event data stream 
@@ -1157,7 +1220,4 @@
   if(!Trigger_Loop && ct_Number==1){
     
-    HeaderTree.Branch("MMcConfigRunHeader","MMcConfigRunHeader",
-		      &McConfigRunHeader[0]);
-
     if (Write_RawEvt){
       EvtTree.Branch("MRawEvtHeader","MRawEvtHeader", 
@@ -1173,15 +1233,4 @@
   else{
 
-    if(ct_Number>1)
-      for(char branchname[10],i=0;i<numberBranches;i++){
-      
-	sprintf(help,"%i",i+1);
-	strcpy (branchname, "MMcConfigRunHeader;");
-	strcat (branchname, & help[0]);
-	strcat (branchname, ".");
-	HeaderTree.Branch(branchname,"MMcConfigRunHeader",
-			  &McConfigRunHeader[i]);
-      }
-    
     if (Write_McTrig){
       for(char branchname[10],i=0;i<numberBranches;i++){
@@ -1303,5 +1352,5 @@
 	const Float_t size=
 	  (*((MGeomCam*)(camgeom.UncheckedAt(ict))))[ui].GetD();
-	diffnsb_phepns[ict][ui] = (Int_t(meanNSB*factorNSB_ct*100*size*size+0.5))/(100.0*size*size) * size*size;
+	diffnsb_phepns[ict][ui] = (Int_t(meanNSB*factorNSB_ct*100*size*size+0.5))/(100.0*size*size) * size*size*factorqe_NSB[ict];
       }
     }
@@ -1321,4 +1370,55 @@
       }
     }
+  }
+
+
+  //!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
+  //
+  //  Now a empty event with the conditin in which the camera is run
+  //  is simulated. IN this way one gets an estimation of the 
+  //  sigma for the pedestal of each FADC channel.
+  //  This sigma computtaion is done assuming any noise taht effects 
+  //  the FADC but there is no rotation of the Star Field (otherwise it
+  //  should be done for each event)
+  //
+  //!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
+
+  for(int ict=0;ict<ct_Number;ict++){
+    Fadc_CT[ict]->Reset() ; 
+    if (addElecNoise){	
+      Fadc_CT[ict]->ElecNoise(FADC_noise) ;
+    }
+    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);
+	}
+      }
+    }
+    Fadc_CT[ict]->Pedestals();
+    for(UInt_t ui=0;
+	ui<((MGeomCam*)(camgeom.UncheckedAt(ict)))->GetNumPixels();
+	ui++){
+      fadc_sigma[ui]=Fadc_CT[ict]->GetPedestalNoise(ui,1);
+      fadc_sigma_low[ui]=Fadc_CT[ict]->GetPedestalNoise(ui,0);
+    }
+    HeaderFadc[ict]->SetPedestalSigma(&fadc_sigma_low[0],&fadc_sigma[0],
+				      ((MGeomCam*)(camgeom.UncheckedAt(ict)))
+				      ->GetNumPixels());
   }
 
@@ -1873,4 +1973,5 @@
 		      //   fill pixel information
 		      if (Lev1){
+			if (addElecNoise) Fadc_CT[0]->DigitalNoise();
 			for(UInt_t i=0;
 			    i<((MGeomCam*)(camgeom.UncheckedAt(0)))
@@ -2047,4 +2148,5 @@
 	  
 	    if ( Lev0MT[ict] > 0 || Write_All_Images) {
+
 	      Lev1MT[ict]= Trigger_CT[ict]->FirstLevel();
 	    }
@@ -2110,4 +2212,5 @@
 	      
 		if (Lev1MT[ict]){
+		   if (addElecNoise) Fadc_CT[ict]->DigitalNoise();
 		  for(UInt_t i=0;
 		      i<((MGeomCam*)(camgeom.UncheckedAt(ict)))->GetNumPixels();
@@ -3736,6 +3839,6 @@
   int k, ii; // counters
 
-  MTrigger trigger(397,camgeom,Trigger_gate_length, Trigger_overlaping_time, Trigger_response_ampl, Trigger_response_fwhm);
-  MFadc flashadc;
+  MTrigger trigger(camgeom->GetNumPixels(),camgeom,Trigger_gate_length, Trigger_overlaping_time, Trigger_response_ampl, Trigger_response_fwhm);
+  MFadc flashadc(camgeom->GetNumPixels());
 
   static float wl_nm[iNUMWAVEBANDS + 1] = { WAVEBANDBOUND1,
@@ -3955,5 +4058,5 @@
   TRandom random;             // Random numbers generator
 
-  random.SetSeed ((UInt_t) (RandomNumber*1000));
+  random.SetSeed(get_seeds(1));
 
   ii = *inphe; // avoid dereferencing
@@ -4054,4 +4157,7 @@
 //
 // $Log: not supported by cvs2svn $
+// Revision 1.62  2003/09/26 11:25:07  blanch
+// Modification to be able to read MGeomCam branch for any Geometry.
+//
 // Revision 1.61  2003/09/25 17:09:20  blanch
 // Bug on the number of phe from diffuse NSB fixed.
