Index: trunk/MagicSoft/Mars/Changelog
===================================================================
--- trunk/MagicSoft/Mars/Changelog	(revision 3767)
+++ trunk/MagicSoft/Mars/Changelog	(revision 3768)
@@ -18,4 +18,29 @@
 
                                                  -*-*- END OF LINE -*-*-
+
+ 2004/04/16: Abelardo Moralejo
+   * mcalib/MCalibrationQEPix.[h,cc]
+     - Added fAverageQE. Same role as gkDefaultAverageQE, but can be 
+       changed via SetAverageQE. Made public GetAverageQE and 
+       GetAverageQERelVar (this changes needed for MC).
+
+   * mcalib/MMcCalibrationCalc.[h,cc]
+   * manalysis/MMcCalibrationUpdate.[h,cc]
+     - Adapted to M. Gaug's changes in calibration classes. Behaviour
+       has been tested to be the same as before those changes. Now the
+       conversion factor from ADC counts to photoelectrons, and the
+       average QE (photons->photoelectrons) are calculated independently 
+       (and later combined by MCalibrate to obtain the conversion 
+       ADC->photons).
+
+   * mmain/MEventDisplay.cc, macros/mccalibrate.C, starmc.C
+     - Added call to MExtractSignal::SetSaturationLimit(240)  Affects 
+       only MC display. This was necessary because if electronic noise
+       is simulated in the FADC, sometimes saturated slices look not
+       saturated due to negative fluctuations, so it is better to set 
+       the saturation limit at a safe value (240 ADC counts). Changed 
+       signal integration range (only for MC), now from slices 5 to 10.
+
+
  2004/04/16: Markus Gaug
 
@@ -33,5 +58,4 @@
        into the "low-gain samples" and mislead thus sliding window to 
        be maximized on the tail of the high-gain pulse.
-
 
  2004/04/15: Markus Gaug
Index: trunk/MagicSoft/Mars/macros/mccalibrate.C
===================================================================
--- trunk/MagicSoft/Mars/macros/mccalibrate.C	(revision 3767)
+++ trunk/MagicSoft/Mars/macros/mccalibrate.C	(revision 3768)
@@ -46,5 +46,5 @@
   // ------------- user change -----------------
   TString* CalibrationFilename;
-  CalibrationFilename = new TString("nonoise/Gamma_zbin0_0*.root");
+  CalibrationFilename = new TString("../../gammas_nonoise/Gamma_zbin0_0*.root");
   // File to be used for the calibration (must be a camera file without added noise)
 
@@ -54,6 +54,6 @@
 
 
-  Int_t BinsHigh[2] = {5, 9}; // First and last FADC bin of the range to be integrated,
-  Int_t BinsLow[2]  = {5, 9}; // for high and low gain respectively.
+  Int_t BinsHigh[2] = {5, 10}; // First and last FADC bin of the range to be integrated,
+  Int_t BinsLow[2]  = {5, 10}; // for high and low gain respectively.
 
   // -------------------------------------------
@@ -95,4 +95,6 @@
 
   MExtractSignal    sigextract;
+  sigextract.SetSaturationLimit(240);
+
   // Define ADC slices to be integrated in high and low gain:
   sigextract.SetRange(BinsHigh[0], BinsHigh[1], BinsLow[0], BinsLow[1]);
@@ -101,5 +103,5 @@
 
   MCalibrate calib; // Transforms signals from ADC counts into photons.
-  calib.SetCalibrationMode(MCalibrate::kDummy);
+  calib.SetCalibrationMode(MCalibrate::kFfactor);
 
   //    MBlindPixelCalc   blind;
@@ -170,6 +172,4 @@
     }
 
-  calib.SetCalibrationMode(MCalibrate::kFfactor);
-
   //
   // Second loop: analysis loop
Index: trunk/MagicSoft/Mars/macros/starmc.C
===================================================================
--- trunk/MagicSoft/Mars/macros/starmc.C	(revision 3767)
+++ trunk/MagicSoft/Mars/macros/starmc.C	(revision 3768)
@@ -52,8 +52,9 @@
   // differences in gain of outer pixels)
   //
-  CalibrationFilename = new TString("nonoise/Gamma_zbin0_90_*.root");
+  CalibrationFilename = new TString("../../gammas_nonoise/Gamma_zbin0_90_*.root");
   // File to be used in the calibration (must be a camera file without added noise)
 
   Char_t* AnalysisFilename = "Gamma_zbin*.root";  // File to be analyzed
+
 
   // ------------- user change -----------------
@@ -73,6 +74,6 @@
   Float_t CleanLev[2] = {4., 3.}; // Tail cuts for image analysis
 
-  Int_t BinsHigh[2] = {5, 9}; // First and last FADC bin of the range to be integrated,
-  Int_t BinsLow[2]  = {5, 9}; // for high and low gain respectively.
+  Int_t BinsHigh[2] = {5, 10}; // First and last FADC bin of the range to be integrated,
+  Int_t BinsLow[2]  = {5, 10}; // for high and low gain respectively.
 
   // -------------------------------------------
@@ -114,4 +115,5 @@
 
   MExtractSignal    sigextract;
+  sigextract.SetSaturationLimit(240);
   // Define ADC slices to be integrated in high and low gain:
   sigextract.SetRange(BinsHigh[0], BinsHigh[1], BinsLow[0], BinsLow[1]);
@@ -120,4 +122,5 @@
 
   MCalibrate calib; // Transforms signals from ADC counts into photons.
+  calib.SetCalibrationMode(MCalibrate::kFfactor);
 
   //    MBlindPixelCalc   blind;
Index: trunk/MagicSoft/Mars/manalysis/MMcCalibrationUpdate.cc
===================================================================
--- trunk/MagicSoft/Mars/manalysis/MMcCalibrationUpdate.cc	(revision 3767)
+++ trunk/MagicSoft/Mars/manalysis/MMcCalibrationUpdate.cc	(revision 3768)
@@ -39,9 +39,11 @@
 //   MMcFadcHeader
 //   MRawRunHeader
-//  [MCalibrationCam] (if it existed previously)
+//  [MCalibrationChargeCam] (if it existed previously)
+//  [MCalibrationQECam] (if it existed previously)
 //
 //  Output Containers:
 //   MPedPhotCam
-//  [MCalibrationCam] (if it did not exist previously)
+//  [MCalibrationChargeCam] (if it did not exist previously)
+//  [MCalibrationQECam] (if it did not exist previously)
 //
 /////////////////////////////////////////////////////////////////////////////
@@ -78,6 +80,11 @@
     fTitle = title ? title : "Write MC pedestals and conversion factors into MCalibration Container";
 
-    fADC2PhInner = 1.;
-    fADC2PhOuter = 1.;
+    //
+    // As default we set 1 photon = 1 ADC count.
+    // (this will make Size to be in ADC counts if no previous calibration has been made)
+    // Hence:
+    //
+    fADC2PhElInner = MCalibrationQEPix::gkDefaultAverageQE;
+    fADC2PhElOuter = MCalibrationQEPix::gkDefaultAverageQE;
 
     fAmplitude = -1.;
@@ -114,8 +121,11 @@
 {
     fCalCam = (MCalibrationChargeCam*) pList->FindObject(AddSerialNumber("MCalibrationChargeCam"));
-    if (!fCalCam)
+    fQECam = (MCalibrationQECam*) pList->FindObject(AddSerialNumber("MCalibrationQECam"));
+
+    if (!fCalCam || !fQECam)
     {
         fCalCam = (MCalibrationChargeCam*) pList->FindCreateObj(AddSerialNumber("MCalibrationChargeCam"));
-        if (!fCalCam)
+        fQECam = (MCalibrationQECam*) pList->FindCreateObj(AddSerialNumber("MCalibrationQECam"));
+        if (!fCalCam || !fQECam)
             return kFALSE;
     }
@@ -123,20 +133,7 @@
     {
         fFillCalibrationCam = kFALSE;
-        *fLog << inf << AddSerialNumber("MCalibrationChargeCam") << " already exists... " << endl;
-    }
-
-    fQECam = (MCalibrationQECam*) pList->FindObject(AddSerialNumber("MCalibrationQECam"));
-    if (!fQECam)
-    {
-        fQECam = (MCalibrationQECam*) pList->FindCreateObj(AddSerialNumber("MCalibrationQECam"));
-        if (!fQECam)
-            return kFALSE;
-    }
-    else
-    {
-        fFillQECam = kFALSE;
-        *fLog << inf << AddSerialNumber("MCalibrationQECam") << " already exists... " << endl;
-    }
-
+        *fLog << inf << AddSerialNumber("MCalibrationChargeCam") << " and " <<
+	  AddSerialNumber("MCalibrationQECam") << " already exist... " << endl;
+    }
 
     fPedPhotCam = (MPedPhotCam*) pList->FindCreateObj(AddSerialNumber("MPedPhotCam"));
@@ -246,7 +243,7 @@
 
     if (fOuterPixelsGainScaling)
-      fADC2PhOuter = fADC2PhInner * (fAmplitude / fAmplitudeOuter);
+      fADC2PhElOuter = fADC2PhElInner * (fAmplitude / fAmplitudeOuter);
     else
-      fADC2PhOuter = fADC2PhInner;
+      fADC2PhElOuter = fADC2PhElInner;
 
 
@@ -272,12 +269,8 @@
 	// and outer pixels).
 	//
-	Float_t adc2phot = (fGeom->GetPixRatio(i) < fGeom->GetPixRatio(0))?
-	  fADC2PhOuter : fADC2PhInner;
-
-        //
-        // FIXME: This has now to be split into a adc2phe part and a phe2phot (==QE) part
-        //
-        const Float_t qe = MCalibrationQEPix::gkDefaultAverageQE;
-	calpix.SetMeanConvFADC2Phe(adc2phot*qe); // here, the FADC to phe part should go.
+	Float_t adc2photel = (fGeom->GetPixRatio(i) < fGeom->GetPixRatio(0))?
+	  fADC2PhElOuter : fADC2PhElInner;
+
+	calpix.SetMeanConvFADC2Phe(adc2photel);
         calpix.SetMeanConvFADC2PheVar(0.);
         calpix.SetMeanFFactorFADC2Phot(0.);
@@ -334,7 +327,11 @@
 
         MCalibrationChargePix &calpix = (MCalibrationChargePix&)(*fCalCam)[i];
-        //        MCalibrationQEPix     &qepix  = (MCalibrationQEPix&)    (*fQECam) [i];
-
-        Float_t qe       = MCalibrationQEPix::gkDefaultAverageQE;
+        MCalibrationQEPix &qepix = (MCalibrationQEPix&)(*fQECam)[i];
+
+	qepix.SetAvNormFFactor(1.);
+	// This factor should convert the default average QE to average QE for a spectrum
+	// like that of Cherenkov light. See the documentration of MCalibrationQEPix.
+
+        Float_t qe       = qepix.GetAverageQE();
 	Float_t adc2phot = calpix.GetMeanConvFADC2Phe() / qe;
 	Float_t hi2lo    = calpix.GetConversionHiLo();
Index: trunk/MagicSoft/Mars/manalysis/MMcCalibrationUpdate.h
===================================================================
--- trunk/MagicSoft/Mars/manalysis/MMcCalibrationUpdate.h	(revision 3767)
+++ trunk/MagicSoft/Mars/manalysis/MMcCalibrationUpdate.h	(revision 3768)
@@ -28,9 +28,8 @@
     MExtractedSignalCam   *fSignalCam;
 
-    Float_t fADC2PhInner; // Conversion factor from ADC counts to photo-electrons
-    Float_t fADC2PhOuter; // for inner and outer pixels.
+    Float_t fADC2PhElInner; // Conversion factor from ADC counts to photo-electrons
+    Float_t fADC2PhElOuter; // for inner and outer pixels.
 
     Bool_t  fFillCalibrationCam;
-    Bool_t  fFillQECam;    
     Bool_t  fOuterPixelsGainScaling;
 
Index: trunk/MagicSoft/Mars/mcalib/MCalibrationQEPix.cc
===================================================================
--- trunk/MagicSoft/Mars/mcalib/MCalibrationQEPix.cc	(revision 3767)
+++ trunk/MagicSoft/Mars/mcalib/MCalibrationQEPix.cc	(revision 3768)
@@ -177,5 +177,6 @@
        fQEFFactorVar    ( MCalibrationCam::gkNumPulserColors ),  
        fQEPINDiode      ( MCalibrationCam::gkNumPulserColors ),    
-       fQEPINDiodeVar   ( MCalibrationCam::gkNumPulserColors ), 
+       fQEPINDiodeVar   ( MCalibrationCam::gkNumPulserColors ),
+       fAverageQE       ( gkDefaultAverageQE ), 
        fValidFlags      ( MCalibrationCam::gkNumPulserColors )
 {
@@ -368,5 +369,6 @@
 const Float_t MCalibrationQEPix::GetAverageQE( const Float_t zenith ) const 
 {
-  return gkDefaultAverageQE ; 
+  //  return gkDefaultAverageQE ;
+  return fAverageQE; 
 }
 
Index: trunk/MagicSoft/Mars/mcalib/MCalibrationQEPix.h
===================================================================
--- trunk/MagicSoft/Mars/mcalib/MCalibrationQEPix.h	(revision 3767)
+++ trunk/MagicSoft/Mars/mcalib/MCalibrationQEPix.h	(revision 3768)
@@ -48,5 +48,6 @@
   Float_t fAvNormPINDiode;                   // Normalization w.r.t. default QE (PIN Diode Method)
   Float_t fAvNormPINDiodeVar;                // Variance norm. w.r.t. def. QE (PIN Diode Method)
-                                             
+  Float_t fAverageQE;                        // Average QE for Cascade spectrum (default 0.18)
+
   TArrayC fValidFlags;                       // Bit-field for valid flags, one array entry for each color
   Byte_t  fAvailableFlags;                   // Bit-field for available flags
@@ -62,7 +63,4 @@
   void  AddAveragePINDiodeQEs  ( const MCalibrationCam::PulserColor_t col, Float_t &wav, Float_t &sumw );
 
-  const Float_t GetAverageQE     ( const Float_t zenith=0. ) const;  
-  const Float_t GetAverageQERelVar( const Float_t zenith=0. ) const;
-  
   const Float_t GetAvNormBlindPixelRelVar()  const;
   const Float_t GetAvNormCombinedRelVar()  const;
@@ -81,4 +79,8 @@
   
   // Getters
+
+  const Float_t GetAverageQE     ( const Float_t zenith=0. ) const;  
+  const Float_t GetAverageQERelVar( const Float_t zenith=0. ) const;
+  
   Float_t GetDefaultQE                   ( const MCalibrationCam::PulserColor_t col ) const;
   Float_t GetDefaultQERelVar             ( const MCalibrationCam::PulserColor_t col ) const;  
@@ -122,4 +124,5 @@
 
   // Setters 
+  void SetAverageQE            ( const Float_t f )  { fAverageQE            = f; }
   void SetAvNormBlindPixel     ( const Float_t f )  { fAvNormBlindPixel     = f; }      
   void SetAvNormBlindPixelVar  ( const Float_t f )  { fAvNormBlindPixelVar  = f; }   
Index: trunk/MagicSoft/Mars/mcalib/MMcCalibrationCalc.cc
===================================================================
--- trunk/MagicSoft/Mars/mcalib/MMcCalibrationCalc.cc	(revision 3767)
+++ trunk/MagicSoft/Mars/mcalib/MMcCalibrationCalc.cc	(revision 3768)
@@ -35,5 +35,7 @@
 //
 //  Output Containers:
+//  (containers mus exist already, they are filled with new values).
 //   MCalibrationChargeCam
+//   MCalibrationQECam  
 //
 /////////////////////////////////////////////////////////////////////////////
@@ -69,8 +71,8 @@
 {
     fName  = name  ? name  : "MMcCalibrationCalc";
-    fTitle = title ? title : "Calculate and write conversion factors into MCalibrationCam Container";
-
-    fHistRatio = new TH1F(AddSerialNumber("HistRatio"), "log10(fPassPhotCone/fSize)", 1500, -3., 3.);
-    fHistRatio->SetXTitle("log_{10}(fPassPhotCone / fSize) [phot/ADC count]");
+    fTitle = title ? title : "Calculate and write conversion factors into MCalibrationChargeCam and MCalibrationQECam containers";
+
+    fHistRatio = new TH1F(AddSerialNumber("HistRatio"), "log10(fPhotElfromShower/fSize)", 1500, -3., 3.);
+    fHistRatio->SetXTitle("log_{10}(fPhotElfromShower / fSize) [photel/ADC count]");
 }
 
@@ -100,5 +102,6 @@
 {
     fHistRatio->Reset();
-    fADC2Phot = 0;
+    fADC2PhotEl = 0;
+    fPhot2PhotEl = 0;
 
     fCalCam = (MCalibrationChargeCam*) pList->FindObject(AddSerialNumber("MCalibrationChargeCam"));
@@ -204,11 +207,16 @@
     // FIXME? The present cut (1000 "inner-pixel-counts") is somehow
     // arbitrary. Might it be optimized?
-    //
-    if (fHillas->GetSize()<1000)
+    //   
+
+    const Float_t size = fHillas->GetSize(); 
+    // Size will at this point be in ADC counts (still uncalibrated)
+
+    if (size < 1000)
         return kTRUE;
 
-    fADC2Phot += fMcEvt->GetPassPhotCone()/fHillas->GetSize();
-
-    fHistRatio->Fill(TMath::Log10(fMcEvt->GetPassPhotCone()/fHillas->GetSize()));
+    fPhot2PhotEl += (Float_t) fMcEvt->GetPhotElfromShower() /
+      (Float_t) fMcEvt->GetPassPhotCone();
+
+    fHistRatio->Fill(TMath::Log10(fMcEvt->GetPhotElfromShower()/size));
 
     return kTRUE;
@@ -228,16 +236,12 @@
     }
 
-    fADC2Phot /= n;
-
-    //
-    // For the calibration we no longer use the mean,
-    // but the peak of the distribution:
+    fPhot2PhotEl /= n;   // Average quantum efficiency
+
+    //
+    // Find peak of log10(photel/Size) histogram:
     //
     const Int_t reach = 2;
-
     Stat_t summax = 0;
     Int_t  mode   = 0;
-
-    // FIXME: Is this necessary? We could use GetMaximumBin instead..
     for (Int_t ibin = 1+reach; ibin <= fHistRatio->GetNbinsX()-reach; ibin++)
     {
@@ -251,5 +255,5 @@
     }
 
-    fADC2Phot = TMath::Power(10, fHistRatio->GetBinCenter(mode));
+    fADC2PhotEl = TMath::Power(10, fHistRatio->GetBinCenter(mode));
 
     const Int_t num = fCalCam->GetSize();
@@ -257,15 +261,28 @@
     for (int i=0; i<num; i++)
     {
-
         MCalibrationChargePix &calpix = (MCalibrationChargePix&)(*fCalCam)[i];
-        //        MCalibrationQEPix     &qepix  = (MCalibrationQEPix&)    (*fQECam) [i];
-
-        const Float_t qe     = MCalibrationQEPix::gkDefaultAverageQE;
-        const Float_t factor = fADC2Phot/qe*calpix.GetMeanConvFADC2Phe();
-        //
-        // FIXME: Now, only the conversion to PHe is stored, need also the quantum efficiency simulated!!!
-        //
+	MCalibrationQEPix     &qepix  = (MCalibrationQEPix&)    (*fQECam) [i];
+
+	qepix.SetAverageQE(fPhot2PhotEl);
+
+	qepix.SetAvNormFFactor(1.);
+	// This factor should convert the default average QE for different colors to 
+        // average QE for a spectrum like that of Cherenkov light (see the documentration 
+	// of MCalibrationQEPix). 
+	// Here we obtain average QE using already a Cherenkov spectrum so AvNormFFacto 
+	// must be 1.
+
+	
+	Float_t factor = fADC2PhotEl;
+
+	//
+	// We take into account the (possibly) different gain of outer pixels:
+	//
+	if (fGeom->GetPixRatio(i) < 1.)
+	  factor *= fHeaderFadc->GetAmplitud()/fHeaderFadc->GetAmplitudOuter();
+
         calpix.SetMeanConvFADC2Phe(factor);
         calpix.SetMeanConvFADC2PheVar(0.);
+
         calpix.SetMeanFFactorFADC2Phot(0.);
     }
Index: trunk/MagicSoft/Mars/mcalib/MMcCalibrationCalc.h
===================================================================
--- trunk/MagicSoft/Mars/mcalib/MMcCalibrationCalc.h	(revision 3767)
+++ trunk/MagicSoft/Mars/mcalib/MMcCalibrationCalc.h	(revision 3768)
@@ -27,5 +27,6 @@
     MMcFadcHeader             *fHeaderFadc;
 
-    Float_t fADC2Phot;
+    Float_t fADC2PhotEl;   // Conversion factor (photel / ADC count)
+    Float_t fPhot2PhotEl;  // Conversion factor (photons / photoelectron) = average QE
     Long_t  fEvents;
 
Index: trunk/MagicSoft/Mars/mmain/MEventDisplay.cc
===================================================================
--- trunk/MagicSoft/Mars/mmain/MEventDisplay.cc	(revision 3767)
+++ trunk/MagicSoft/Mars/mmain/MEventDisplay.cc	(revision 3768)
@@ -265,5 +265,6 @@
 
         MExtractSignal* extra = new MExtractSignal();
-        extra->SetRange(5, 9, 5, 9);
+	extra->SetRange(5, 10, 5, 10);
+	extra->SetSaturationLimit(240);
 
         MMcCalibrationUpdate* mcupd = new MMcCalibrationUpdate;
@@ -271,5 +272,5 @@
 
         MCalibrate* mccal = new MCalibrate;
-        mccal->SetCalibrationMode(MCalibrate::kDummy);
+        mccal->SetCalibrationMode(MCalibrate::kFfactor);
 
         // MC
