Index: trunk/MagicSoft/Mars/mjobs/MJPedestal.cc
===================================================================
--- trunk/MagicSoft/Mars/mjobs/MJPedestal.cc	(revision 5460)
+++ trunk/MagicSoft/Mars/mjobs/MJPedestal.cc	(revision 5506)
@@ -31,4 +31,37 @@
 #include "MJPedestal.h"
 
+#include "MLog.h"
+#include "MLogManip.h"
+
+#include "MTaskEnv.h"
+#include "MSequence.h"
+#include "MRunIter.h"
+#include "MParList.h"
+#include "MTaskList.h"
+#include "MEvtLoop.h"
+#include "MExtractor.h"
+
+#include "MStatusDisplay.h"
+
+#include "MGeomCam.h"
+#include "MHCamera.h"
+#include "MPedestalCam.h"
+#include "MPedestalPix.h"
+#include "MBadPixelsCam.h"
+
+#include "MCalibrationPedCam.h"
+#include "MCalibrationPix.h"
+#include "MHPedestalCam.h"
+#include "MHPedestalPix.h"
+#include "MFillH.h"
+
+
+#include "MReadMarsFile.h"
+#include "MRawFileRead.h"
+#include "MGeomApply.h"
+#include "MBadPixelsMerge.h"
+#include "MPedCalcPedRun.h"
+#include "MPedCalcFromLoGain.h"
+
 #include <TF1.h>
 #include <TEnv.h>
@@ -40,29 +73,5 @@
 #include <TSystem.h>
 #include <TLegend.h>
-
-#include "MLog.h"
-#include "MLogManip.h"
-
-#include "MTaskEnv.h"
-#include "MSequence.h"
-#include "MRunIter.h"
-#include "MParList.h"
-#include "MTaskList.h"
-#include "MEvtLoop.h"
-#include "MExtractor.h"
-
-#include "MStatusDisplay.h"
-
-#include "MGeomCam.h"
-#include "MHCamera.h"
-#include "MPedestalCam.h"
-#include "MBadPixelsCam.h"
-
-#include "MReadMarsFile.h"
-#include "MRawFileRead.h"
-#include "MGeomApply.h"
-#include "MBadPixelsMerge.h"
-#include "MPedCalcPedRun.h"
-#include "MPedCalcFromLoGain.h"
+#include <TPad.h>
 
 ClassImp(MJPedestal);
@@ -85,4 +94,5 @@
 const Float_t  MJPedestal::fgRefPedRmsGalacticOuter      = 4.2;
 
+const TString  MJPedestal::fgNameExtractorCam = "ExtractorResolution";
 // --------------------------------------------------------------------------
 //
@@ -95,4 +105,6 @@
 // - fExtractType to kUsePedRun
 // - fStorage to Normal Storage
+// - fExtractorResolution to kFALSE
+// - fNameExtractorCam to "ExtractorResolution"
 //
 MJPedestal::MJPedestal(const char *name, const char *title) 
@@ -105,4 +117,7 @@
     SetNormalStorage();
     SetUsePedRun();
+    SetPathIn("");
+    SetExtractorResolution(kFALSE);
+    SetNameExtractorCam();
 }
 
@@ -142,7 +157,13 @@
 
     TFile file(fname, "READ");
-    if (fPedestalCam.Read()<=0)
-    {
-        *fLog << err << "Unable to read MPedestalCam from " << fname << endl;
+    if (fPedestalCamIn.Read()<=0)
+    {
+        *fLog << err << "Unable to read incoming MPedestalCam from " << fname << endl;
+        return kFALSE;
+    }
+
+    if (fPedestalCamOut.Read()<=0)
+    {
+        *fLog << err << "Unable to read outgoing MPedestalCam from " << fname << endl;
         return kFALSE;
     }
@@ -167,5 +188,5 @@
 MExtractor *MJPedestal::ReadCalibration() const
 {
-    const TString fname = Form("%s/calib%06d.root", fPathIn.Data(), fSequence.GetSequence());
+    const TString fname = Form("%s/calib%06d.root",fPathIn.Data(), fSequence.GetSequence());
 
     *fLog << inf << "Reading from file: " << fname << endl;
@@ -213,33 +234,167 @@
     // Get container from list
     //
-    MGeomCam &geomcam = *(MGeomCam*)plist.FindObject("MGeomCam");
+    MGeomCam           &geomcam   = *(MGeomCam*)plist.FindObject("MGeomCam");
+    MCalibrationPedCam &calpedcam = *(MCalibrationPedCam*)plist.FindObject("MCalibrationPedCam");
 
     //
     // Create container to display
     //
-    MHCamera disp0(geomcam, "MPedestalCam;ped", "Mean Pedestal");
-    MHCamera disp1(geomcam, "MPedestalCam;RMS", "Pedestal RMS");
-
-    disp0.SetCamContent(fPedestalCam, 0);
-    disp0.SetCamError  (fPedestalCam, 1);
-
-    disp1.SetCamContent(fPedestalCam, 2);
-    disp1.SetCamError  (fPedestalCam, 3);
+    MHCamera disp0 (geomcam, "MPedestalCam;ped", "Mean Pedestal");
+    MHCamera disp1 (geomcam, "MPedestalCam;RMS", "Pedestal RMS");
+    MHCamera disp2 (geomcam, "MCalibPedCam;histmean", "Mean Pedestal (Hist.)");
+    MHCamera disp3 (geomcam, "MCalibPedCam;histsigma", "Pedestal RMS (Hist.)");
+    MHCamera disp4 (geomcam, "MCalibPedCam;ped", "Mean Pedestal");
+    MHCamera disp5 (geomcam, "MCalibPedCam;RMS", "Pedestal RMS");
+    MHCamera disp6 (geomcam, "MCalibDiffCam;ped", "Diff. Mean Pedestal (Hist.)");
+    MHCamera disp7 (geomcam, "MCalibDiffCam;RMS", "Diff. Pedestal RMS (Hist.)");
+    MHCamera disp8 (geomcam, "MCalibDiffCam;ped", "Diff. Mean Pedestal");
+    MHCamera disp9 (geomcam, "MCalibDiffCam;AbsRMS", "Diff. Abs. Pedestal RMS");
+    MHCamera disp10(geomcam, "MCalibDiffCam;RelRMS", "Diff. Rel. Pedestal RMS");
+
+    disp0.SetCamContent(fPedestalCamOut, 0);
+    disp0.SetCamError  (fPedestalCamOut, 1);
+
+    disp1.SetCamContent(fPedestalCamOut, 2);
+    disp1.SetCamError  (fPedestalCamOut, 3);
+
+    if (fExtractType == kUseHists)
+      {
+        disp2.SetCamContent(calpedcam, 0);
+        disp2.SetCamError  (calpedcam, 1);
+
+        disp3.SetCamContent(calpedcam, 2);
+        disp3.SetCamError  (calpedcam, 3);
+
+        disp4.SetCamContent(calpedcam, 5);
+        disp4.SetCamError  (calpedcam, 6);
+
+        disp5.SetCamContent(calpedcam, 7);
+        disp5.SetCamError  (calpedcam, 8);
+
+        for (UInt_t i=0;i<geomcam.GetNumPixels();i++)
+          {
+
+            MPedestalPix    &ped  = fPedestalCamOut[i];
+            MCalibrationPix &hist = calpedcam [i];
+            MBadPixelsPix &bad  = fBadPixels[i];
+
+            if (bad.IsUnsuitable())
+              continue;
+
+            disp6.Fill(i,ped.GetPedestal()-hist.GetHiGainMean());
+            disp6.SetUsed(i);
+
+            disp7.Fill(i,hist.GetHiGainSigma()-ped.GetPedestalRms());
+            if (TMath::Abs(ped.GetPedestalRms()-hist.GetHiGainSigma()) < 4.0)
+              disp7.SetUsed(i);
+
+            disp8.Fill(i,ped.GetPedestal()-hist.GetLoGainMean());
+            disp8.SetUsed(i);
+
+            disp9.Fill(i,hist.GetLoGainSigma()-ped.GetPedestalRms());
+            if (TMath::Abs(hist.GetLoGainSigma() - ped.GetPedestalRms()) < 4.0)
+              disp9.SetUsed(i);
+          }
+      }
+    
+    if (fExtractorResolution)
+      {
+        for (UInt_t i=0;i<geomcam.GetNumPixels();i++)
+          {
+
+            MPedestalPix &pedo  = fPedestalCamOut[i];
+            MPedestalPix &pedi  = fPedestalCamIn[i];
+            MBadPixelsPix &bad  = fBadPixels[i];
+
+            if (bad.IsUnsuitable())
+              continue;
+
+            const Float_t diff = pedo.GetPedestalRms()-pedi.GetPedestalRms();
+            const Float_t sum  = 0.5*(pedo.GetPedestalRms()+pedi.GetPedestalRms());
+
+            disp9.Fill(i,pedo.GetPedestalRms()-pedi.GetPedestalRms());
+            if (pedo.IsValid() && pedi.IsValid())
+              disp9.SetUsed(i);
+
+            disp10.Fill(i,sum == 0. ? 0. : diff/sum);
+            if (pedo.IsValid() && pedi.IsValid() && sum != 0.)
+              disp10.SetUsed(i);
+          }
+      }
 
     disp0.SetYTitle("Pedestal [counts/slice]");
     disp1.SetYTitle("RMS [counts/slice]");
+    disp2.SetYTitle("Hist. Mean  [counts/slice]");
+    disp3.SetYTitle("Hist. Sigma [counts/slice]");
+    disp4.SetYTitle("Calc. Mean  [counts/slice]");
+    disp5.SetYTitle("Calc. RMS   [counts/slice]");
+    disp6.SetYTitle("Diff. Mean  [counts/slice]");
+    disp7.SetYTitle("Diff. RMS   [counts/slice]");
+    disp8.SetYTitle("Diff. Mean  [counts/slice]");
+    disp9.SetYTitle("Abs. Diff. RMS   [counts/slice]");
+    disp10.SetYTitle("Rel. Diff. RMS   [1]");
 
     //
     // Display data
     //
-    TCanvas &c3 = fDisplay->AddTab("Pedestals");
-    c3.Divide(2,3);
-
-    if (fDisplayType != kDataCheckDisplay)
-    {
+    if (fDisplayType != kDataCheckDisplay && fExtractType != kUseHists && !fExtractorResolution)
+    {
+      TCanvas &c3 = fDisplay->AddTab("Pedestals");
+      c3.Divide(2,3);
+
+      disp0.CamDraw(c3, 1, 2, 1);
+      disp1.CamDraw(c3, 2, 2, 6);
+      return;
+    }
+
+    if (fExtractorResolution)
+      {
+
+        TCanvas &c3 = fDisplay->AddTab("Extr.Res.");
+        c3.Divide(2,3);
+        
         disp0.CamDraw(c3, 1, 2, 1);
         disp1.CamDraw(c3, 2, 2, 6);
+
+        TCanvas &c13 = fDisplay->AddTab("Diff.(Extr.-Pedestal)");
+        c13.Divide(2,3);
+        
+        disp9.CamDraw(c13, 1, 2, 5);
+        disp10.CamDraw(c13, 2, 2, 5);
         return;
-    }
+      }
+
+    if (fExtractType == kUseHists)
+      {
+
+        TCanvas &c3 = fDisplay->AddTab("Extractor Hist.");
+        c3.Divide(2,3);
+
+        disp2.CamDraw(c3, 1, 2, 1);
+        disp3.CamDraw(c3, 2, 2, 5);
+
+        TCanvas &c4 = fDisplay->AddTab("Extractor Calc.");
+        c4.Divide(2,3);
+        
+        disp4.CamDraw(c4, 1, 2, 1);
+        disp5.CamDraw(c4, 2, 2, 5);
+
+
+        TCanvas &c5 = fDisplay->AddTab("Difference Hist.");
+        c5.Divide(2,3);
+        
+        disp6.CamDraw(c5, 1, 2, 1);
+        disp7.CamDraw(c5, 2, 2, 5);
+
+        TCanvas &c6 = fDisplay->AddTab("Difference Calc.");
+        c6.Divide(2,3);
+        
+        disp8.CamDraw(c6, 1, 2, 1);
+        disp9.CamDraw(c6, 2, 2, 5);
+        return;
+      }
+
+    TCanvas &c3 = fDisplay->AddTab(fExtractorResolution ? "Extr.Res." : "Pedestals");
+    c3.Divide(2,3);
 
     c3.cd(1);
@@ -386,7 +541,11 @@
             }
             leg2->Draw();
+            delete leg2;
         }
-    }
-}
+
+    }
+
+}
+
 
 void  MJPedestal::DisplayReferenceLines(MHCamera *cam, const Int_t what) const
@@ -440,4 +599,5 @@
   leg->AddEntry(l3, "Closed Lids","l");
   leg->Draw();
+  delete leg;
 }
 
@@ -525,19 +685,23 @@
 Bool_t MJPedestal::CheckEnvLocal()
 {
-    SetDataCheck(GetEnv("DataCheck", fDataCheck));
-
-    if (HasEnv("DataCheckDisplay"))
-        fDisplayType = GetEnv("DataCheckDisplay", kFALSE) ? kDataCheckDisplay : kNormalDisplay;
-
-    if (HasEnv("UseData"))
-        fExtractType = GetEnv("UseData",kFALSE) ? kUseData : kUsePedRun;
-
-    if (HasEnv("UseExtractor"))
-        if (GetEnv("UseExtractor",kFALSE))
-            fExtractType = kUseExtractor;
-
-    SetNoStorage(GetEnv("DisableOutput", IsNoStorage()));
-
-    return kTRUE;
+
+  SetDataCheck(GetEnv("DataCheck", fDataCheck));
+  
+  if (HasEnv("DataCheckDisplay"))
+    fDisplayType = GetEnv("DataCheckDisplay", kFALSE) ? kDataCheckDisplay : kNormalDisplay;
+  
+  if (HasEnv("UseData"))
+    fExtractType = GetEnv("UseData",kFALSE) ? kUseData : kUsePedRun;
+  
+  if (HasEnv("UseHists"))
+    if (GetEnv("UseHists",kFALSE))
+      fExtractType = kUseHists;
+
+  SetExtractorResolution(GetEnv("ExtractorResolution", fExtractorResolution));
+  SetNameExtractorCam(GetEnv("NameExtractorCam", fNameExtractorCam));
+  
+  SetNoStorage(GetEnv("DisableOutput", IsNoStorage()));
+  
+  return kTRUE;
 }
 
@@ -575,5 +739,5 @@
     }
 
-    if (fPedestalCam.Write()<=0)
+    if (fPedestalCamOut.Write()<=0)
     {
         *fLog << err << "Unable to write MPedestalCam to " << oname << endl;
@@ -677,12 +841,23 @@
 
     // Setup Tasklist
-    plist.AddToList(&fPedestalCam);
+    plist.AddToList(&fPedestalCamOut);
     plist.AddToList(&fBadPixels);
-
+    
     MGeomApply      geomapl;
     MBadPixelsMerge merge(&fBadPixels);
 
     MPedCalcPedRun     pedcalc;
+    pedcalc.SetPedestalUpdate(kFALSE);
     MPedCalcFromLoGain pedlogain;
+    pedlogain.SetPedestalUpdate(kFALSE);
+
+    MHPedestalCam      hpedcam;
+    hpedcam.SetRenorm(kTRUE);
+    
+    MFillH fillped(&hpedcam, "MExtractedSignalCam");
+    fillped.SetBit(MFillH::kDoNotDisplay);
+
+    tlist.AddToList(&geomapl);
+    tlist.AddToList(&merge);
 
     if (!fPathIn.IsNull())
@@ -702,45 +877,68 @@
     {
     case kUseData:
-        taskenv.SetDefault(&pedlogain);
-        if (!fExtractor)
-            break;
+      taskenv.SetDefault(&pedlogain);
+      if (!fExtractor)
+        break;
+      tlist.AddToList(&taskenv);
+      if (fExtractorResolution && fExtractor->InheritsFrom("MExtractTimeAndCharge"))
+        {
+          plist.AddToList(&fPedestalCamIn);
+          fPedestalCamOut.SetName(fNameExtractorCam.Data());
+          pedlogain.SetNamePedestalCamOut(fNameExtractorCam.Data());
+          pedlogain.SetExtractor((MExtractTimeAndCharge*)fExtractor);
+        }
+      else
         pedlogain.SetExtractWindow(15, (Int_t)TMath::Nint(fExtractor->GetNumHiGainSamples()));
-        pedlogain.SetPedestalUpdate(kFALSE);
+      break;
+
+    case kUsePedRun:
+      taskenv.SetDefault(&pedcalc);
+      if (!fExtractor)
         break;
-
-    case kUsePedRun:
-        taskenv.SetDefault(&pedcalc);
-        if (!fExtractor)
-            break;
-        pedcalc.SetWindowSize((Int_t)TMath::Nint(fExtractor->GetNumHiGainSamples()));
-        pedcalc.SetRange(fExtractor->GetHiGainFirst(), fExtractor->GetHiGainLast());
-        break;
-
-    case kUseExtractor:
-        if (!fExtractor)
+      tlist.AddToList(&taskenv);
+      if (fExtractorResolution && fExtractor->InheritsFrom("MExtractTimeAndCharge"))
         {
-            *fLog << err << GetDescriptor();
-            *fLog << "Extraction Type kUseExtractor is chosen, but no extractor has been handed over" << endl;
-            return kFALSE;
+          plist.AddToList(&fPedestalCamIn);
+          fPedestalCamOut.SetName(fNameExtractorCam.Data());
+          pedcalc.SetNamePedestalCamOut(fNameExtractorCam.Data());
+          pedcalc.SetExtractor((MExtractTimeAndCharge*)fExtractor);
         }
-        taskenv.SetDefault(fExtractor);
-        if (fExtractor->IsNoiseCalculation())
-            break;
-
+      else
+        pedcalc.SetExtractWindow(fExtractor->GetHiGainFirst(),TMath::Nint(fExtractor->GetNumHiGainSamples()));
+      break;
+      
+    case kUseHists:
+      if (!fExtractor)
+        {
+          *fLog << err << GetDescriptor();
+          *fLog << ": Extraction Type kUseExtractor is chosen, but no extractor has been handed over" 
+                << endl;
+          return kFALSE;
+        }
+      
+      plist.AddToList(&hpedcam);
+      tlist.AddToList(fExtractor);
+      tlist.AddToList(&fillped);
+
+      break;
+    }
+
+    //
+    // Check that the extractor has the bit Noise Calculation() set which 
+    // means that the each "sliding" of the sliding window is not applied
+    //
+    if (fExtractorResolution && !fExtractor->IsNoiseCalculation())
+      {
         *fLog << warn << GetDescriptor();
-        *fLog <<"Extraction type is kUseExtractor, but extractor has kNoiseCalculation not set... set." << endl;
+        *fLog <<": Extraction type is kUseExtractor, but extractor has kNoiseCalculation not set... set." 
+              << endl;
         fExtractor->SetNoiseCalculation();
-        return kFALSE;
-    }
-
+      }
+    
     if (!fPathIn.IsNull())
     {
-        delete fExtractor;
-        fExtractor = 0;
-    }
-
-    tlist.AddToList(&geomapl);
-    tlist.AddToList(&merge);
-    tlist.AddToList(&taskenv);
+      delete fExtractor;
+      fExtractor = 0;
+    }
 
     //
Index: trunk/MagicSoft/Mars/mjobs/MJPedestal.h
===================================================================
--- trunk/MagicSoft/Mars/mjobs/MJPedestal.h	(revision 5460)
+++ trunk/MagicSoft/Mars/mjobs/MJPedestal.h	(revision 5506)
@@ -41,9 +41,14 @@
   static const Float_t  fgRefPedRmsGalacticOuter;      //! Ref. line ped. RMS for galactic source - outer pixels     
     
+  static const TString  fgNameExtractorCam;            //! "ExtractorResolution"
+
   MRunIter   *fRuns;                                   // Used pedestal runs
   MExtractor *fExtractor;                              // Signal extractor, used to find the nr. of used FADC slices
   
-  MPedestalCam  fPedestalCam;                          // Created pedestal results
+  MPedestalCam  fPedestalCamIn;                        // Handed over pedestal results
+  MPedestalCam  fPedestalCamOut;                       // Created pedestal results
   MBadPixelsCam fBadPixels;                            // Bad Pixels
+
+  TString fNameExtractorCam;                           // Name of the outgoing MPedestalCam if fExtractorResolution
   
   enum Display_t {kDataCheckDisplay, kNormalDisplay};  // Possible Display types
@@ -55,6 +60,8 @@
   Bool_t fDataCheck;                                   // Flag if the data check is run on raw data
 
-  enum Extract_t {kUseData, kUsePedRun, kUseExtractor}; // Possible flags for the extraction of the pedestal
+  enum Extract_t {kUseData, kUsePedRun, kUseHists };   // Possible flags for the extraction of the pedestal
   Extract_t fExtractType;                              // Chosen extractor type
+
+  Bool_t fExtractorResolution;                         // Flag if the extractor is used to calculate the pedestals
   
   MExtractor *ReadCalibration() const;
@@ -76,5 +83,5 @@
   MJPedestal(const char *name=NULL, const char *title=NULL);
 
-  MPedestalCam &GetPedestalCam()            { return fPedestalCam; }
+  MPedestalCam &GetPedestalCam()            { return fPedestalCamOut; }
   const MBadPixelsCam &GetBadPixels() const { return fBadPixels;   }
   
@@ -82,6 +89,4 @@
   const Bool_t IsDataCheck()    const { return fDataCheck; }
   const Bool_t IsUseData()      const { return fExtractType == kUseData;   }
-  const Bool_t IsUseExtractor() const { return fExtractType == kUseExtractor;   }
-  const Bool_t IsUsePedRun()    const { return fExtractType == kUsePedRun;   }
   
   Bool_t Process    ();
@@ -89,14 +94,16 @@
   
   void SetBadPixels(const MBadPixelsCam &bad) { bad.Copy(fBadPixels); }
+  void SetPedestals(const MPedestalCam  &ped) { ped.Copy(fPedestalCamIn); }  
   void SetExtractor(MExtractor* ext)          { fExtractor = ext;     }
   void SetInput(MRunIter  *iter)              { fRuns      = iter;    }
-  void SetUseData()                           { fExtractType = kUseData;      }
-  void SetUseExtractor()                      { fExtractType = kUseExtractor; }
-  void SetUsePedRun()                         { fExtractType = kUsePedRun;    }
+  void SetUseData()                           { fExtractType = kUseData;   }
+  void SetUseHists()                          { fExtractType = kUseHists;  }
+  void SetUsePedRun()                         { fExtractType = kUsePedRun; }
   void SetDataCheck(const Bool_t b=kTRUE)     { fDataCheck = b; b ? SetDataCheckDisplay() : SetNormalDisplay(); }
   void SetDataCheckDisplay()                  { fDisplayType = kDataCheckDisplay; }
   void SetNormalDisplay()                     { fDisplayType = kNormalDisplay;    }
+  void SetExtractorResolution(const Bool_t b=kTRUE) { fExtractorResolution = b;  }
+  void SetNameExtractorCam( const char* name= fgNameExtractorCam.Data())  { fNameExtractorCam = name;  }
   
-  void SetPedContainerName(const char *name)  { fPedestalCam.SetName(name); }
   
   // Storage
