Index: /trunk/MagicSoft/Mars/Changelog
===================================================================
--- /trunk/MagicSoft/Mars/Changelog	(revision 4383)
+++ /trunk/MagicSoft/Mars/Changelog	(revision 4384)
@@ -19,4 +19,23 @@
 
                                                  -*-*- END OF LINE -*-*-
+
+
+ 2004/07/14: Hendrik Bartko
+  * mpedestal/MPedPhotCam.[h,cc]
+    - added average areas and sectors (in analogy MPedestalCam)
+    - added new function InitAreas(const UInt_t i)
+    - added new function InitSectors(const UInt_t i)
+    - added new function GetNumAreas()
+    - added new function GetNumSectors()
+    - added new function ReCalc, computes the average values for the
+      sectors from all pixels which are not marked 
+      MBadPixelsPix::kUnsuitableRun
+  * mpedestal/MPedPhotPix.[h,cc]
+    - added a variable for the number of events from which the 
+      pedestals are computed
+  * manalysis/MGeomApply.cc
+    - changed initialization of MPedPhotCam
+
+
 
  2004/07/14: Markus Gaug
Index: /trunk/MagicSoft/Mars/manalysis/MGeomApply.cc
===================================================================
--- /trunk/MagicSoft/Mars/manalysis/MGeomApply.cc	(revision 4383)
+++ /trunk/MagicSoft/Mars/manalysis/MGeomApply.cc	(revision 4384)
@@ -18,5 +18,6 @@
 !   Author(s): Thomas Bretz, 09/2003 <mailto:tbretz@astro.uni-wuerzburg.de>
 !              Markus Gaug,  03/2004 <mailto:markus@ifae.es>
-!
+!              Hendrik Bartko, 07/2003 <mailto:hbartko@mppmu.mpg.de>
+!         
 !   Copyright: MAGIC Software Development, 2000-2004
 !
@@ -156,5 +157,5 @@
     MPedPhotCam *pedphot = (MPedPhotCam*)pList->FindObject(AddSerialNumber("MPedPhotCam"));
     if (pedphot)
-        pedphot->InitSize(cam->GetNumPixels());
+        pedphot->Init(*cam);
 
     MExtractedSignalCam *ext = (MExtractedSignalCam*)pList->FindObject(AddSerialNumber("MExtractedSignalCam"));
Index: /trunk/MagicSoft/Mars/mpedestal/MPedPhotCam.cc
===================================================================
--- /trunk/MagicSoft/Mars/mpedestal/MPedPhotCam.cc	(revision 4383)
+++ /trunk/MagicSoft/Mars/mpedestal/MPedPhotCam.cc	(revision 4384)
@@ -17,6 +17,7 @@
 !
 !   Author(s): Thomas Bretz, 12/2003 <mailto:tbretz@astro.uni-wuerzburg.de>
-!
-!   Copyright: MAGIC Software Development, 2000-2003
+!              Hendrik Bartko, 07/2003 <mailto:hbartko@mppmu.mpg.de>
+!
+!   Copyright: MAGIC Software Development, 2000-2004
 !
 !
@@ -30,8 +31,14 @@
 // of photons)
 //
+// Version 2:
+// ----------
+//   - added fAreas
+//   - added fSectors
+//
 /////////////////////////////////////////////////////////////////////////////
 #include "MPedPhotCam.h"
-#include "MPedPhotPix.h"
-
+
+#include <TArrayI.h>
+#include <TArrayD.h>
 #include <TClonesArray.h>
 
@@ -39,5 +46,11 @@
 #include "MLogManip.h"
 
+#include "MPedPhotPix.h"
+
 #include "MGeomCam.h"
+#include "MGeomPix.h"
+
+#include "MBadPixelsCam.h"
+#include "MBadPixelsPix.h"
 
 ClassImp(MPedPhotCam);
@@ -54,8 +67,7 @@
     fTitle = title ? title : "Storage container for all Pedestal Information in the camera (in units of photons)";
 
-    fArray = new TClonesArray("MPedPhotPix", 1);
-
-//    for (int i=0; i<577; i++)
-//        new ((*fArray)[i]) MPedPhotPix;
+    fArray   = new TClonesArray("MPedPhotPix", 1);
+    fAreas   = new TClonesArray("MPedPhotPix", 1);
+    fSectors = new TClonesArray("MPedPhotPix", 1);
 }
 
@@ -67,4 +79,6 @@
 {
     delete fArray;
+    delete fAreas;
+    delete fSectors;
 }
 
@@ -78,4 +92,39 @@
 }
 
+// -------------------------------------------------------------------
+//
+// Calls TClonesArray::ExpandCreate() for:
+// - fAverageAreas
+//
+void MPedPhotCam::InitAreas(const UInt_t i)
+{
+    fAreas->ExpandCreate(i);
+}
+
+// -------------------------------------------------------------------
+//
+// Calls TClonesArray::ExpandCreate() for:
+// - fAverageSectors
+//
+void MPedPhotCam::InitSectors(const UInt_t i)
+{
+    fSectors->ExpandCreate(i);
+}
+
+// -------------------------------------------------------------------
+//
+// Calls:
+// - InitSize()
+// - InitAverageAreas()
+// - InitAverageSectors()
+//
+void MPedPhotCam::Init(const MGeomCam &geom)
+{
+    InitSize(geom.GetNumPixels());
+    InitAreas(geom.GetNumAreas());
+    InitSectors(geom.GetNumSectors());
+}
+
+
 // --------------------------------------------------------------------------
 //
@@ -89,4 +138,22 @@
 // --------------------------------------------------------------------------
 //
+// Get the size of the MPedPhotCam
+//
+Int_t MPedPhotCam::GetNumSectors() const
+{
+    return fSectors->GetEntriesFast();
+}
+
+// --------------------------------------------------------------------------
+//
+// Get the size of the MPedPhotCam
+//
+Int_t MPedPhotCam::GetNumAreas() const
+{
+    return fAreas->GetEntriesFast();
+}
+
+// --------------------------------------------------------------------------
+//
 // Get i-th pixel (pixel number)
 //
@@ -100,24 +167,124 @@
 // Get i-th pixel (pixel number)
 //
-MPedPhotPix &MPedPhotCam::operator[](Int_t i) const
+const MPedPhotPix &MPedPhotCam::operator[](Int_t i) const
 {
     return *static_cast<MPedPhotPix*>(fArray->UncheckedAt(i));
 }
 
-/*
-// --------------------------------------------------------------------------
-//
-// Check if position i is inside bounds
-//
-Bool_t MPedPhotCam::CheckBounds(Int_t i)
-{
-    return i < fArray->GetEntriesFast();
-} 
-*/
+// --------------------------------------------------------------------------
+//
+// Get i-th average pixel (area number)
+//
+MPedPhotPix &MPedPhotCam::GetArea(UInt_t i)
+{
+    return *static_cast<MPedPhotPix*>(fAreas->UncheckedAt(i));
+}
+
+// --------------------------------------------------------------------------
+//
+// Get i-th average pixel (sector number)
+//
+MPedPhotPix &MPedPhotCam::GetSector(UInt_t i)
+{
+    return *static_cast<MPedPhotPix*>(fSectors->UncheckedAt(i));
+}
+
+// --------------------------------------------------------------------------
+//
+// Get i-th average pixel (area number)
+//
+const MPedPhotPix &MPedPhotCam::GetArea(UInt_t i)const
+{
+    return *static_cast<MPedPhotPix*>(fAreas->UncheckedAt(i));
+}
+
+// --------------------------------------------------------------------------
+//
+// Get i-th average pixel (sector number)
+//
+const MPedPhotPix &MPedPhotCam::GetSector(UInt_t i) const
+{
+    return *static_cast<MPedPhotPix*>(fSectors->UncheckedAt(i));
+}
+
+// --------------------------------------------------------------------------
+//
+// Call clear of all three TClonesArray
+//
 void MPedPhotCam::Clear(Option_t *o)
 {
-    fArray->ForEach(TObject, Clear)();
-}
-
+    { fArray->ForEach(TObject, Clear)(); }
+    { fAreas->ForEach(TObject, Clear)(); }
+    { fSectors->ForEach(TObject, Clear)(); }
+}
+
+// --------------------------------------------------------------------------
+//
+// Calculates the avarage pedestal and pedestal rms for all sectors
+// and pixel sizes. The geometry container is used to get the necessary
+// geometry information (sector number, size index) If the bad pixel
+// container is given all pixels which have the flag 'bad' are ignored
+// in the calculation of the sector and size average.
+//
+void MPedPhotCam::ReCalc(const MGeomCam &geom, MBadPixelsCam *bad)
+{
+    
+    const Int_t np = GetSize();
+    const Int_t ns = GetNumSectors();
+    const Int_t na = GetNumAreas();
+
+    
+
+    TArrayI acnt(na);
+    TArrayI scnt(ns);
+    TArrayD asumx(na);
+    TArrayD ssumx(ns);
+    TArrayD asumr(na);
+    TArrayD ssumr(ns);
+
+
+    for (int i=0; i<np; i++)
+    {
+        
+
+      if (bad && (*bad)[i].IsUnsuitable(MBadPixelsPix::kUnsuitableRun))
+	continue; //was: .IsBad()       
+
+        // Create sums for areas and sectors
+        const UInt_t aidx = geom[i].GetAidx();
+        const UInt_t sect = geom[i].GetSector();
+
+        const MPedPhotPix &pix = (*this)[i];
+
+        const UInt_t  ne   = pix.GetNumEvents();
+        const Float_t mean = pix.GetMean();
+	const Float_t rms  = pix.GetRms();
+
+        asumx[aidx] += ne*mean;
+	asumr[aidx] += ne*rms;
+        acnt[aidx]  += ne;
+
+        ssumx[sect] += ne*mean;
+	ssumr[sect] += ne*rms;
+        scnt[sect]  += ne;
+    
+
+    }
+
+    for (int i=0; i<ns; i++){
+      if (scnt[i]>0)  GetSector(i).Set(ssumx[i]/scnt[i], ssumr[i]/scnt[i], scnt[i]);
+      else GetSector(i).Set(-1., -1., 0);
+    }
+
+    for (int i=0; i<na; i++){
+      if (acnt[i]>0) GetArea(i).Set(asumx[i]/acnt[i], asumr[i]/acnt[i], acnt[i]);
+      else  GetArea(i).Set(-1., -1., 0);
+    }
+}
+
+// --------------------------------------------------------------------------
+//
+// print contents
+//
 void MPedPhotCam::Print(Option_t *o) const
 {
@@ -138,58 +305,44 @@
     }
 }
-/*
-Float_t MPedPhotCam::GetPedestalMin(const MGeomCam *geom) const
-{
-    if (fArray->GetEntries() <= 0)
-        return 50.;
-
-    Float_t minval = (*this)[0].GetPedestalRms();
-
-    for (Int_t i=1; i<fArray->GetEntries(); i++)
-    {
-        const MPedPhotPix &pix = (*this)[i];
-
-        Float_t testval = pix.GetPedestalRms();
-
-        if (geom)
-            testval *= geom->GetPixRatio(i);
-
-        if (testval < minval)
-            minval = testval;
-    }
-    return minval;
-}
-
-Float_t MPedPhotCam::GetPedestalMax(const MGeomCam *geom) const
-{
-    if (fArray->GetEntries() <= 0)
-        return 50.;
-
-    Float_t maxval = (*this)[0].GetPedestalRms();
-
-    for (Int_t i=1; i<fArray->GetEntries(); i++)
-    {
-        const MPedPhotPix &pix = (*this)[i];
-
-        Float_t testval = pix.GetPedestalRms();
-
-        if (geom)
-            testval *= geom->GetPixRatio(i);
-
-        if (testval > maxval)
-            maxval = testval;
-    }
-    return maxval;
-}
-*/
+
+// --------------------------------------------------------------------------
+//
+// See MCamEvent
+//
 Bool_t MPedPhotCam::GetPixelContent(Double_t &val, Int_t idx, const MGeomCam &cam, Int_t type) const
 {
+
+    const Float_t ped      = (*this)[idx].GetMean();
+    const Float_t rms      = (*this)[idx].GetRms();
+    const UInt_t  nevt     = (*this)[idx].GetNumEvents();
+
+
+    Float_t pederr;
+    Float_t rmserr; 
+
+    if (nevt>0)
+    {
+        pederr = rms/TMath::Sqrt((Float_t)nevt);
+        rmserr = rms/TMath::Sqrt((Float_t)nevt)/2.;
+    }
+    else
+    {
+        pederr = -1;
+        rmserr = -1;
+    }
+
     switch (type)
     {
     case 0:
-        val = (*this)[idx].GetMean();
+        val = ped;// (*this)[idx].GetMean();
         break;
     case 1:
-        val = (*this)[idx].GetRms();
+        val = rms; // (*this)[idx].GetRms();
+        break;
+    case 2:
+        val = pederr; // new
+        break;
+    case 3:
+      val = rmserr; // new
         break;
     default:
Index: /trunk/MagicSoft/Mars/mpedestal/MPedPhotCam.h
===================================================================
--- /trunk/MagicSoft/Mars/mpedestal/MPedPhotCam.h	(revision 4383)
+++ /trunk/MagicSoft/Mars/mpedestal/MPedPhotCam.h	(revision 4384)
@@ -13,9 +13,16 @@
 class MGeomCam;
 class MPedPhotPix;
+class MBadPixelsCam;
 
 class MPedPhotCam : public MParContainer, public MCamEvent
 {
 private:
-    TClonesArray *fArray; // FIXME: Change TClonesArray away from a pointer?
+    TClonesArray *fArray;    // FIXME: Change TClonesArray away from a pointer?
+    TClonesArray *fAreas;    //-> Array of MPedPhotPix, one per pixel area
+    TClonesArray *fSectors;  //-> Array of MPedPhotPix, one per camera sector
+
+    //  void InitSize(const UInt_t i);
+    void InitAreas(const UInt_t i);
+    void InitSectors(const UInt_t i);
 
 public:
@@ -25,21 +32,29 @@
     void Clear(Option_t *o="");
 
-    void InitSize(const UInt_t i);
+    void Init(const MGeomCam &geom);
+    void InitSize(const UInt_t i); // HB
     Int_t GetSize() const;
 
     MPedPhotPix &operator[](Int_t i);
-    MPedPhotPix &operator[](Int_t i) const;
+    const MPedPhotPix &operator[](Int_t i) const;
 
-    //    Float_t GetPedestalMin(const MGeomCam *cam) const;
-    //    Float_t GetPedestalMax(const MGeomCam *cam) const;
+          MPedPhotPix &GetArea(UInt_t i);
+    const MPedPhotPix &GetArea(UInt_t i) const;
 
-    //    Bool_t CheckBounds(Int_t i);
+    Int_t GetNumAreas() const;
+
+          MPedPhotPix &GetSector(UInt_t i);
+    const MPedPhotPix &GetSector(UInt_t i) const;
+
+    Int_t GetNumSectors() const;
 
     void Print(Option_t *o="") const;
+
+    void ReCalc(const MGeomCam &geom, MBadPixelsCam *bad);
 
     Bool_t GetPixelContent(Double_t &val, Int_t idx, const MGeomCam &cam, Int_t type=0) const;
     void DrawPixelContent(Int_t num) const;
 
-    ClassDef(MPedPhotCam, 1)	// Storage Container for all pedestal information of the camera (in units of photons)
+    ClassDef(MPedPhotCam, 2)	// Storage Container for all pedestal information of the camera (in units of photons)
 };
 
Index: /trunk/MagicSoft/Mars/mpedestal/MPedPhotPix.cc
===================================================================
--- /trunk/MagicSoft/Mars/mpedestal/MPedPhotPix.cc	(revision 4383)
+++ /trunk/MagicSoft/Mars/mpedestal/MPedPhotPix.cc	(revision 4384)
@@ -16,7 +16,7 @@
 !
 !
-!   Author(s): Thomas Bretz  12/2000 <mailto:tbretz@uni-sw.gwdg.de>
+!   Author(s): Thomas Bretz  12/2000 <mailto:tbretz@astro.uni-wuerzburg.de>
 !
-!   Copyright: MAGIC Software Development, 2000-2001
+!   Copyright: MAGIC Software Development, 2000-2004
 !
 !
@@ -28,5 +28,9 @@
 //
 // This is the storage container to hold informations about the pedestal
-// (offset) value of one Pixel (PMT) in units of photons
+// (offset) value of one Pixel (PMT) in units of photons.
+//
+// Version 2:
+// ----------
+//   - added fNumEvents
 //
 /////////////////////////////////////////////////////////////////////////////
@@ -50,4 +54,4 @@
     fMean = -1;
     fRms  = -1;
+    fNumEvents = 0;
 }
-
Index: /trunk/MagicSoft/Mars/mpedestal/MPedPhotPix.h
===================================================================
--- /trunk/MagicSoft/Mars/mpedestal/MPedPhotPix.h	(revision 4383)
+++ /trunk/MagicSoft/Mars/mpedestal/MPedPhotPix.h	(revision 4384)
@@ -9,6 +9,8 @@
 {
 private:
-    Float_t fMean; // [phot] mean value of pedestal (should be 0)
-    Float_t fRms;  // [phot] rms of mean
+    Float_t fMean;      // [phot] mean value of pedestal (should be 0)
+    Float_t fRms;       // [phot] rms of mean
+
+    UInt_t  fNumEvents; // [n] number of events used to calculate mean (0=n/a)
 
 public:
@@ -17,14 +19,15 @@
     void Clear(Option_t *o="");
 
-    Float_t GetMean() const { return fMean; }
-    Float_t GetRms() const  { return fRms; }
+    Float_t GetMean() const      { return fMean; }
+    Float_t GetRms() const       { return fRms; }
+    UInt_t  GetNumEvents() const { return fNumEvents; }
 
     //void SetMean(Float_t f) { fMean = f; }
     void SetRms(Float_t f)  { fRms  = f; }
-    void Set(Float_t m, Float_t r) { fMean = m; fRms = r; }
+    void Set(Float_t m, Float_t r, UInt_t n=1) { fMean = m; fRms = r; fNumEvents=n; }
 
     Bool_t IsValid() const { return fRms>=0; }
 
-    ClassDef(MPedPhotPix, 1) // Storage Container for Pedestal information of one pixel in units of photons
+    ClassDef(MPedPhotPix, 2) // Storage Container for Pedestal information of one pixel in units of photons
 };
 
