Index: trunk/MagicSoft/Simulation/Detector/Camera/camera.cxx
===================================================================
--- trunk/MagicSoft/Simulation/Detector/Camera/camera.cxx	(revision 577)
+++ trunk/MagicSoft/Simulation/Detector/Camera/camera.cxx	(revision 646)
@@ -21,7 +21,7 @@
 //
 // $RCSfile: camera.cxx,v $
-// $Revision: 1.16 $
+// $Revision: 1.17 $
 // $Author: magicsol $ 
-// $Date: 2001-01-18 18:44:40 $
+// $Date: 2001-02-23 11:05:57 $
 //
 ////////////////////////////////////////////////////////////////////////
@@ -61,4 +61,5 @@
 #include "MTrigger.hxx"
 #include "MFadc.hxx"
+#include "MLons.hxx"
 
 #include "MRawEvtData.h"
@@ -67,5 +68,6 @@
 #include "MMcEvt.hxx"
 #include "MMcTrig.hxx"
-#include "MHeaderTrig.hxx"
+#include "MMcTrigHeader.hxx"
+#include "MMcFadcHeader.hxx"
 
 /*!@" 
@@ -272,7 +274,4 @@
 //@: contents of the pixels (ph.e.)
 static float *fnpix;    
-
-//@: contents of the pixels (ph.e.) after cleanning
-static float *fnpixclean; 
 
  
@@ -391,4 +390,6 @@
   char parname[256];          //@< parameters file name
 
+  char nsbpathname[256];      //@< directory with the dataabse for th NSB
+
   char flag[SIZE_OF_FLAGS + 1];  //@< flags in the .rfl file
 
@@ -399,5 +400,6 @@
   MCCphoton cphoton;          //@< Cherenkov Photon class (MC)
 
-  int inumphe;                //@< number of photoelectrons in an event
+  int inumphe;                //@< number of photoelectrons in an event from showers
+  float inumphensb;             //@< number of photoelectrons in an event from nsb
 
   float arrtmin_ns;           //@ arrival time of the first photoelectron
@@ -406,9 +408,9 @@
   float thetaCT, phiCT;       //@< parameters of a given shower
   float thetashw, phishw;     //@< parameters of a given shower
-  float coreD, coreX, coreY;  //@< core position and distance
+  float coreD, coreX, coreY;         //@< core position
   float impactD;              //@< impact parameter
   float l1, m1, n1;           //@< auxiliary variables
   float l2, m2, n2;           //@< auxiliary variables
-  float num, den;             //@< auxiliary variables
+  float num, den;      //@< auxiliary variables
 
   int nshow=0;                //@< partial number of shower in a given run
@@ -425,4 +427,13 @@
   float nsbrate_phepns[iMAXNUMPIX][iNUMWAVEBANDS];    //@< non-diffuse nsb 
                                                      //@< photoelectron rates 
+  float nsb_phepns[iMAXNUMPIX];      //@< NSB values for each pixel
+
+  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
+                                                //@< response from the database
+  Byte_t trigger_map[((Int_t)(TRIGGER_PIXELS/8))+1];//@< Pixels on when the
+                                                    //@< camera triggers
+
   float ext[iNUMWAVEBANDS] = { //@< average atmospheric extinction in each waveband
     EXTWAVEBAND1,
@@ -432,6 +443,4 @@
     EXTWAVEBAND5
   };                          
-  float baseline_mv[iMAXNUMPIX]; //@< The baseline (mV) caused by the NSB; to be subtracted
-                                 //@< in order to simulate the preamps' AC coupling 
 
   float qThreshold;           //@< Threshold value
@@ -467,4 +476,11 @@
 
   int ch, errflg = 0;          //@< used by getopt
+
+  //!@' @#### Initialisation of some variables
+  //@'
+
+  for(i=0;i<iMAXNUMPIX;i++)
+    for(j=0;j<iNUMWAVEBANDS;j++)
+      nsbrate_phepns[i][j]=0.0;    //@< Starlight rates initialised at 0.0
 
   /*!@'
@@ -561,4 +577,5 @@
   strcpy( rootname_loop, get_loop_filename() );
   strcpy( ct_filename, get_ct_filename() );
+  strcpy( nsbpathname, get_nsb_directory() );
 
   // get different parameters of the simulation
@@ -576,8 +593,9 @@
 
   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",
+      "%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",
       "Filenames",
       "In", inname, 
       "Stars", starfieldname,
+      "NSB database", nsbpathname,
       "CT", ct_filename, 
       "Data", datname,
@@ -697,5 +715,5 @@
 
   MMcTrig **McTrig; 
-  MHeaderTrig **HeaderTrig; 
+  MMcTrigHeader **HeaderTrig; 
 
   if (Write_McTrig){
@@ -707,8 +725,8 @@
     }
 
-    HeaderTrig = new MHeaderTrig * [icontrigger];
+    HeaderTrig = new MMcTrigHeader * [icontrigger];
   
     for (i=0;i<icontrigger;i++) {
-      HeaderTrig[i] = new MHeaderTrig();
+      HeaderTrig[i] = new MMcTrigHeader();
     }
   }
@@ -716,4 +734,10 @@
   MFadc fadc ;                //@< A instance of the Class MFadc
 
+  // Set the FADC pedestals for that run
+  // The values of pedestal follow a flat distribution. Some modifications
+  // mut be done to simulate a more realistic distribution of the pedestals.
+  // This simualtion is done int the SetPedestals methode inside the 
+  // class MFadc
+  fadc.SetPedestals(50);
 
   // Prepare the raw data output
@@ -762,15 +786,15 @@
   if(!Trigger_Loop && Write_McTrig){
     
-    HeaderTree.Branch("MHeaderTrig","MHeaderTrig", 
+    HeaderTree.Branch("MMcTrigHeader","MMcTrigHeader", 
 		 &HeaderTrig[0], bsize, split);    
   }
   if (Trigger_Loop && Write_McTrig){
-    for(char branchname[10],i=0;i<icontrigger;i++){
+    for(char branchname[20],i=0;i<icontrigger;i++){
       
       sprintf(help,"%i",i+1);
-      strcpy (branchname, "MHeaderTrig");
+      strcpy (branchname, "MMcTrigHeader");
       strcat (branchname, & help[0]);
       strcat (branchname, ".");
-      HeaderTree.Branch(branchname,"MHeaderTrig", 
+      HeaderTree.Branch(branchname,"MMcTrigHeader", 
 		     &HeaderTrig[i], bsize, split);
     }
@@ -787,5 +811,8 @@
     }
     HeaderTrig[0]->SetThreshold( fpixelthres);
-
+    HeaderTrig[0]->SetAmplitud(Trigger_response_ampl);
+    HeaderTrig[0]->SetFwhm(Trigger_response_fwhm);
+    HeaderTrig[0]->SetOverlap(Trigger_overlaping_time);
+    HeaderTrig[0]->SetGate(Trigger_gate_length);
   }
   if(Trigger_Loop && Write_McTrig){
@@ -800,4 +827,8 @@
 	  }
 	  HeaderTrig[iconcount]->SetThreshold( fpixelthres);
+	  HeaderTrig[iconcount]->SetAmplitud(Trigger_response_ampl);
+	  HeaderTrig[iconcount]->SetFwhm(Trigger_response_fwhm);
+	  HeaderTrig[iconcount]->SetOverlap(Trigger_overlaping_time);
+	  HeaderTrig[iconcount]->SetGate(Trigger_gate_length);
 	  iconcount++;
 	}
@@ -902,4 +933,10 @@
   // prepare the NSB simulation
 
+  //  Instance of the Mlons class
+  MLons lons(Trigger_response_ampl, Trigger_response_fwhm,
+	     MFADC_RESPONSE_AMPLITUDE, MFADC_RESPONSE_FWHM);
+
+  lons.SetPath(nsbpathname);
+
   if( simulateNSB ){
 
@@ -911,4 +948,12 @@
 			  &cam,
 			  nsbrate_phepns );
+    float dum;
+    for(i=0; i<cam.inumpixels;i++){
+      dum=0;
+      for(j=0;j<iNUMWAVEBANDS;j++)
+	dum+=nsbrate_phepns[i][j];
+      cout<<"Wuschel "<<dum<<endl;
+    }
+
     if (k != 0){
       cout << "Error when reading starfield... \nExiting.\n";
@@ -923,4 +968,11 @@
     }
 
+    // calculate nsb rate including diffuse and starlight
+    for(i=0; i<cam.inumpixels;i++){
+      nsb_phepns[i]= diffnsb_phepns[i];
+      for(j=0;j<iNUMWAVEBANDS;j++)
+	nsb_phepns[i]=nsb_phepns[i]+ nsbrate_phepns[i][j];
+      cout<<"Huschel "<<nsb_phepns[i]<<endl;
+    }
   }
 
@@ -966,12 +1018,6 @@
   
   fnpix = new float [ ct_NPixels ];
-  fnpixclean = new float [ ct_NPixels ];
-
-  // initialize baseline 
-
-  for(i=0; i<cam.inumpixels; i++){
-    baseline_mv[i] = 0.;
-  }
-       
+
+     
   //!@' @#### Main loop.
   //@'
@@ -1023,9 +1069,9 @@
 	
 	fread( (char*)&mcevth, mcevth.mysize(), 1, inputfile );
-	
+
 	// calculate core distance and impact parameter
 	
 	coreD = mcevth.get_core(&coreX, &coreY);
-	
+		
 	// calculate impact parameter (shortest distance betwee the original
 	// trajectory of the primary (assumed shower-axis) and the
@@ -1102,8 +1148,4 @@
 	  impactD = fabs(num)/den;
 	  
-	  // fprintf(stderr, "[%f %f,%f %f] (%f %f %f) (%f %f %f) %f/%f = ",
-	  //         thetashw, phishw, thetaCT, phiCT, l1, m1, n1, l2, m2, n2,
-	  //         num, den);
-	  
 	}
 
@@ -1127,23 +1169,16 @@
 	if(simulateNSB){
 
-	  k = produce_nsb_phes( &arrtmin_ns, // will be changed by the function!
-				&arrtmax_ns, // will be changed by the function!
-				thetaCT,
-				&cam,
-				nsbrate_phepns,
-				diffnsb_phepns,
-				ext,
-				fnpix,   // 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
-				);
-
-	  if( k != 0 ){ // non-zero returnvalue means error
-	    cout << "Exiting.\n";
-	    exit(1);
-	  }
-	  
+	  //  Fill trigger and fadc response in the trigger class from teh database
+	  for(i=0;i<cam.inumpixels;i++)
+	    if(nsb_phepns[i]>0.0){
+	      k=lons.GetResponse(nsb_phepns[i],0.1,
+	      			 & nsb_trigresp[0],& nsb_fadcresp[0]);
+	      if(k==0){
+		cout << "Exiting.\n";
+		exit(1);
+	      }
+ 	      Trigger.SetNSB(i,nsb_trigresp);
+	      fadc.Set(i,nsb_fadcresp);
+	    }
 	}// end if(simulateNSB) ...
 
@@ -1167,5 +1202,5 @@
 	  exit(1);
 	}
-	  
+  
 	log(SIGNATURE, "End of this event: %d cphs(+%d). . .\n",
 	    ncph, ntcph);
@@ -1191,6 +1226,10 @@
 	}
 	
-	cout << "Total number of phes: " << inumphe <<endl;
-	
+	cout << "Total number of phes: " << inumphe<<" + ";
+	inumphensb=0;
+	for (i=0;i<cam.inumpixels;i++){
+	  inumphensb+=nsb_phepns[i]*TOTAL_TRIGGER_TIME;
+	}
+	cout<<inumphensb<<endl;
 
 	//++++++++++++++++++++++++++++++++++++++++++++++++++
@@ -1206,5 +1245,4 @@
 	// TRIGGER HERE
 
-
 	//
 	//   now the noise of the electronic 
@@ -1215,4 +1253,9 @@
 	
 	fadc.ElecNoise() ;
+
+	//  now a shift in the fadc signal due to the pedestlas is 
+	//  intorduced
+	//  This is done inside the class MFadc by the method Pedestals
+	fadc.Pedestals();
 
 	//   We study several trigger conditons
@@ -1274,7 +1317,8 @@
 		    if (Write_McTrig)
 		      McTrig[iconcount]->SetTime(Trigger.GetFirstLevelTime(ii),ii+1);
-		    if (Write_McTrig)
-		      McTrig[iconcount]->SetPixel(Trigger.GetFirstLevelPixel(ii),ii+1);
-		    
+		    if (Write_McTrig){
+		      Trigger.GetMapDiskriminator(trigger_map);
+		      McTrig[iconcount]->SetMapPixels(trigger_map,ii);
+		    }
 		    //
 		    //  fill inside the class fadc the member output
@@ -1292,5 +1336,5 @@
 		      for(i=0;i<ct_NPixels;i++){
 			for (j=0;j<FADC_SLICES;j++){
-			  fadcValues->AddAt((Char_t) (fadc.GetFadcSignal(i,j)),j);
+			  fadcValues->AddAt(fadc.GetFadcSignal(i,j),j);
 			}
 			EvtData[iconcount]->AddPixel(i+1000*ii,fadcValues,0);
@@ -1325,9 +1369,11 @@
 			   mcevth.get_coreY(),
 			   impactD,
-			   ulli, ulli, 
-			   (UShort_t) ncph, 
 			   ulli, 
-			   (UShort_t) ncph) ;
-	    }
+			   ulli, 
+			   (UInt_t) ncph, 
+			   (UInt_t) ncph, 
+			   (UInt_t) inumphe,
+			   (UInt_t) inumphensb+inumphe);
+		}
 	    //  Fill the Tree with the current leaves of each branch
 	    i=EvtTree.Fill() ;
@@ -1402,6 +1448,8 @@
 	      McTrig[0]->SetTime(Trigger.GetFirstLevelTime(ii),ii+1);
 
-	    if (Write_McTrig)
-	      McTrig[0]->SetPixel(Trigger.GetFirstLevelPixel(ii),ii+1);
+	    if (Write_McTrig){
+	      Trigger.GetMapDiskriminator(trigger_map);
+	      McTrig[0]->SetMapPixels(trigger_map,ii);
+	    }
 
 	    //  Fill Evt information
@@ -1419,5 +1467,5 @@
 	      for(i=0;i<ct_NPixels;i++){
 		for (j=0;j<FADC_SLICES;j++){
-		  fadcValues->AddAt((Char_t) (fadc.GetFadcSignal(i,j)),j);
+		  fadcValues->AddAt(fadc.GetFadcSignal(i,j),j);
 		}
 		EvtData[0]->AddPixel(i+ii*1000,fadcValues,0);
@@ -1437,9 +1485,11 @@
 			   mcevth.get_coreY(),
 			   impactD,
-			   ulli, ulli, 
-			   (UShort_t) ncph, 
 			   ulli, 
-			   (UShort_t) ncph) ; 
-	    }
+			   ulli, 
+			   (UInt_t) ncph, 
+			   (UInt_t) ncph, 
+			   (UInt_t) inumphe,
+			   (UInt_t) inumphensb+inumphe);
+		}
 	    //   We don not count photons out of the camera.	
 	    
@@ -3096,4 +3146,7 @@
 //
 // $Log: not supported by cvs2svn $
+// Revision 1.16  2001/01/18 18:44:40  magicsol
+// *** empty log message ***
+//
 // Revision 1.15  2001/01/17 09:32:27  harald
 // The changes are neccessary to have the same name for trees in MC and in
@@ -3119,4 +3172,7 @@
 //
 // $Log: not supported by cvs2svn $
+// Revision 1.16  2001/01/18 18:44:40  magicsol
+// *** empty log message ***
+//
 // Revision 1.15  2001/01/17 09:32:27  harald
 // The changes are neccessary to have the same name for trees in MC and in
