Index: trunk/MagicSoft/Simulation/Detector/include-MLons/MLons.cxx
===================================================================
--- trunk/MagicSoft/Simulation/Detector/include-MLons/MLons.cxx	(revision 5103)
+++ trunk/MagicSoft/Simulation/Detector/include-MLons/MLons.cxx	(revision 5248)
@@ -3,5 +3,6 @@
 using namespace std;
 
-MLons::MLons(){
+MLons::MLons()
+{
   //-----------------------------------------------------------------
   //
@@ -9,19 +10,22 @@
   //
 
-  fTrigShape = 0.0;
+  fTrigShape = 0;
   fAmplTrig = 1.0;  
   fFwhmTrig = 2.0;
-
-  fFadcShape = 0.0;
+  fFadcSlicesPerNanosec = FADC_SLICES_PER_NSEC;
+
+  fFadcShape = 0;
   fIntegFadc = 1.0;
   fFwhmFadc = 2.0;
 
-  RandomNumber = new TRandom() ;
+  RandomNumber = new TRandom();
   RandomNumber -> SetSeed(0);
 
 }
 
-MLons::MLons(Float_t in_shapeT, Float_t in_amplT, Float_t in_FwhmT, 
-	     Float_t in_shapeF, Float_t in_integF, Float_t in_FwhmF){
+MLons::MLons(Int_t in_shapeT, Float_t in_amplT, Float_t in_FwhmT, 
+	     Int_t in_shapeF, Float_t in_integF, Float_t in_FwhmF,
+	     Float_t in_Fadc_Slices_per_ns)
+{
   //--------------------------------------------------------------------
   //
@@ -37,9 +41,14 @@
   fFwhmFadc = in_FwhmF;
 
+  fFadcSlicesPerNanosec = in_Fadc_Slices_per_ns;
+
+  MSLStored = new MStarLight;
+
   RandomNumber = new TRandom() ; 
   RandomNumber -> SetSeed(0);
 }
 
-void MLons::Reset() {
+void MLons::Reset() 
+{
   //-----------------------------------------------------------------
   //
@@ -47,13 +56,13 @@
   //
 
-  fTrigShape   = 0. ; 
-  fAmplTrig    = 0. ; 
-  fFwhmTrig    = 0. ; 
-
-  fFadcShape   = 0. ; 
-  fIntegFadc    = 0. ; 
-  fFwhmFadc    = 0. ; 
-
-  MSLStored.Reset();
+  fTrigShape   = 0; 
+  fAmplTrig    = 0.; 
+  fFwhmTrig    = 0.; 
+
+  fFadcShape   = 0; 
+  fIntegFadc   = 0.; 
+  fFwhmFadc    = 0.; 
+
+  MSLStored->Reset();
 
 }
@@ -110,11 +119,11 @@
 }
 
-void MLons::ReadBinaryMStarLight(char *filename){
-
-  MSLStored.ReadBinary(filename); 
-
-}
-
-Int_t MLons::CheckTrig(){
+void MLons::ReadBinaryMStarLight(char *filename)
+{
+  MSLStored->ReadBinary(filename); 
+}
+
+Int_t MLons::CheckTrig()
+{
   //--------------------------------------------------------------------
   //
@@ -122,12 +131,18 @@
   //
 
-  if (  fAmplTrig == MSLStored.GetAmplTrig() && 
-	fFwhmTrig == MSLStored.GetFwhmTrig())
+  if (  fAmplTrig == MSLStored->GetAmplTrig() && 
+	fFwhmTrig == MSLStored->GetFwhmTrig())
     return 1;
 
+  cout << "1 phe- pulse amplitudes for trigger (mV): " << fAmplTrig << "    "
+       << MSLStored->GetAmplTrig() << endl;
+  cout << "1 phe- pulse FWHM for trigger (ns):       " << fFwhmTrig << "    "
+       << MSLStored->GetFwhmTrig() << endl;
+
   return 0;
 }
 
-Int_t MLons::CheckFADC(){
+Int_t MLons::CheckFADC()
+{
   //--------------------------------------------------------------------
   //
@@ -135,14 +150,25 @@
   //
 
-  if (  fIntegFadc == MSLStored.GetIntegFadc() && 
-	fFwhmFadc == MSLStored.GetFwhmFadc() && 
-	fFadcShape == MSLStored.GetShapeFadc())
+  if (  fIntegFadc == MSLStored->GetIntegFadc() && 
+	fFwhmFadc == MSLStored->GetFwhmFadc() && 
+	fFadcShape == MSLStored->GetShapeFadc() &&
+	fFadcSlicesPerNanosec == MSLStored->GetFadcSlicesPerNanosec() )
     return 1;
 
+  cout << "1 phe- FADC pulse shape :             " << fFadcShape << "    " 
+       << MSLStored->GetShapeFadc() <<  endl;
+  cout << "1 phe- FADC pulse integral (counts) : " << fIntegFadc << "    " 
+       << MSLStored->GetIntegFadc() << endl;
+  cout << "1 phe- FADC pulse FWHM (ns) :         " << fFwhmFadc  << "    " 
+       << MSLStored->GetFwhmFadc() <<  endl;
+  cout << "FADC sampling frequencies (GHz) :     " << fFadcSlicesPerNanosec << "    "
+       << MSLStored->GetFadcSlicesPerNanosec() << endl;
+
   return 0;
 }
 
 Int_t MLons::GetResponse(Float_t in_br, Float_t in_pre,
-			 Float_t *out_tr, Float_t *out_Fr){
+			 Float_t *out_tr, Float_t *out_Fr)
+{
   //-------------------------------------------------------------------------
   //
@@ -154,89 +180,102 @@
   Char_t cstoredbright[10];
 
-  Int_t i;
   Int_t bin;
-  Float_t start_bin;
-  Float_t time;
-
+  Float_t start_time;
+
+
+  //
   // The following code line commented means that the simulation will crash if
   // some pixel ask for more lons than what we have in the database.
   // The following code line uncommented would mean that the simulation does
-  //  not crash if a pixel ask for more than it is simulated but would 
-  // assign to it lees lons.
-  if (in_br>49.9) in_br=49.9;  //  To avoid error for high required brightness
-                                 //  It has to be improved
+  // not crash if a pixel ask for more than it is simulated but would 
+  // assign to it less lons.
+  // I think this is unnecessary now. AM 5/10/2004
+  //  if (in_br>49.9) in_br=49.9;    //  To avoid error for high required brightness
+  //                                 //  It has to be improved
+  //
+
 
   //  Check if the the brightness is the same than the last time.
-  //  NOTE: Same means, the smae inside the required precision!!!
-
-  if(in_br<1.0){
+  //  NOTE: Same means, the same inside the required precision!!!
+
+  if(in_br<=1.0){
     sprintf(cbright,"%4.2f",in_br);
-    sprintf(cstoredbright,"%4.2f",MSLStored.GetBrightness());
+    sprintf(cstoredbright,"%4.2f",MSLStored->GetBrightness());
   }
   else{
     sprintf(cbright,"%3.1f",in_br);
-    sprintf(cstoredbright,"%3.1f",MSLStored.GetBrightness());
+    sprintf(cstoredbright,"%3.1f",MSLStored->GetBrightness());
   }
-  if (strcmp(cbright, cstoredbright)){
-    
-    // Building the filename
-    // Note: it would be nice to get an algorithm that gets the name of 
-    // files it needs to get brightness as a function of precison(in_pre),
-    // brightnes (in_br) and the Star_files that are in the "Path" directory.
-
-    strcpy(filename_slt, & path[0]);
-    strcat(filename_slt, "Brightness");
-    strcat(filename_slt, & cbright[0]);
+
+  if (strcmp(cbright, cstoredbright))
+    {
+      // Building the filename
+      // Note: it would be nice to get an algorithm that gets the name of 
+      // files it needs to get brightness as a function of precison(in_pre),
+      // brightnes (in_br) and the Star_files that are in the "Path" directory.
+
+      strcpy(filename_slt, & path[0]);
+      strcat(filename_slt, "Brightness");
+      strcat(filename_slt, & cbright[0]);
   
-    strcat(filename_slt, ".slt");
+      strcat(filename_slt, ".slt");
  
-    // If brightness is different it check if the new file has the
-    // required parameters.
-    // Note: I could be faster to store the whole trigger and fadc
-    // response and use it while brightness does not change. Then the
-    // root file should be open and close here.
-
-    ReadBinaryMStarLight( & filename_slt[0]);
-
-    if (!(CheckTrig() && CheckFADC())) {
-      cout<<"ERROR: The Database for light from Night Sky Background is wrong"<<endl<<"       Make sure that you generated the database with the same shape for Fadc and trigger taht you are asking now."<<endl; 
-      return 0;
+      // If brightness is different it check if the new file has the
+      // required parameters.
+      // Note: I could be faster to store the whole trigger and fadc
+      // response and use it while brightness does not change. Then the
+      // root file should be open and close here.
+
+      MSLStored->ReadBinary(filename_slt); 
+
+      if (!(CheckTrig() && CheckFADC()))
+	{
+	  cout << "ERROR: The Database for light from Night Sky Background is wrong"<<endl;
+	  cout << "       Make sure that you generated the database with the same shape" << endl;
+	  cout << "       for Fadc and trigger that you are asking now."<<endl; 
+	  return 0;
+	}
+      MSLStored->SetBrightness(in_br);
+
     }
-    MSLStored.SetBrightness(in_br);
-
- }
 
   // Random number that decides the set of bins that the program will get
-  start_bin=RandomNumber->Uniform(1.0e4);
+  start_time = RandomNumber->Uniform(TIMERANGE);
 
   //  Filling trigger response
 
-  bin=(Int_t)(start_bin*4);
-
-  for (i=0;i<TRIGGER_TIME_SLICES;i++){
-
-    time=((float)i)/SLICES_PER_NSEC;
-    
-    if (time>(bin-start_bin*4.0)/4.0) bin++;
-
-    if (bin>=TRIGBINS) bin=bin-TRIGBINS;
-
-    out_tr[i]=MSLStored.GetTrig(bin);
-  }
+  bin = (Int_t)(start_time*TRIG_SLICES_PER_NSEC);
+
+
+  if (bin+TRIGGER_TIME_SLICES > MSLStored->GetBinsTrig())
+    {
+      memcpy (out_tr, MSLStored->GetTrigPointer(bin), 
+	      (UInt_t) (MSLStored->GetBinsTrig()-bin)*sizeof(Float_t));
+      memcpy (out_tr, MSLStored->GetTrigPointer(0),
+	      (UInt_t) (bin+TRIGGER_TIME_SLICES-MSLStored->GetBinsTrig())*sizeof(Float_t));
+    }
+  else
+    memcpy (out_tr, MSLStored->GetTrigPointer(bin), 
+	    (UInt_t) TRIGGER_TIME_SLICES*sizeof(Float_t));
+
 
   //  Filling fadc response
 
-  bin=(Int_t) (start_bin*0.3);
-
-  for (i=0;i<(Int_t) SLICES_MFADC;i++){
-
-    time=((float)i)*WIDTH_FADC_TIMESLICE;
-    
-    if (time>(bin-start_bin*0.3)/0.3) bin++;
-
-    if (bin>=FADCBINS) bin=bin-FADCBINS;
-
-    out_Fr[i]=MSLStored.GetFadc(bin);
-  }
+  // Start bin in the NSB database:
+  bin = (Int_t) (start_time*MSLStored->GetBinsFadc()/MSLStored->GetTimeRange()); 
+
+  // If we go over the end of the database, continue at the begining of it: 
+  if (bin + (Int_t)(TOTAL_TRIGGER_TIME*fFadcSlicesPerNanosec) > MSLStored->GetBinsFadc())
+    {
+      memcpy (out_Fr, MSLStored->GetFadcPointer(bin), 
+	      (Int_t)(MSLStored->GetBinsFadc()-bin)*sizeof(Float_t));
+
+      memcpy (out_Fr, MSLStored->GetFadcPointer(0),
+	      (Int_t)(bin + TOTAL_TRIGGER_TIME*fFadcSlicesPerNanosec - 
+		       MSLStored->GetBinsFadc())*sizeof(Float_t));
+    }
+  else
+    memcpy (out_Fr, MSLStored->GetFadcPointer(bin), 
+	    (Int_t)(TOTAL_TRIGGER_TIME*fFadcSlicesPerNanosec*sizeof(Float_t)));
 
   return 1;
