Index: /trunk/MagicSoft/Mars/Changelog
===================================================================
--- /trunk/MagicSoft/Mars/Changelog	(revision 5727)
+++ /trunk/MagicSoft/Mars/Changelog	(revision 5728)
@@ -26,4 +26,6 @@
        signals and fix the extraction window from the occurrance of 
        saturation, then. Important for high-intensity calibration events.
+
+
 
  2005/01/07 Abelardo Moralejo
@@ -76,4 +78,7 @@
      - fixed a bug in GetMaxIdxHiLoGain which caused the loop to
        read one byte behind the end of the array
+     - fixed HasLoGain() which really did weired things, means accessing
+       sometimes memory somewhere. This could theoretically result
+       in assigning the wrong lo-gains to the wrong pixels.
 
    * mtrigger/MTriggerPattern.[h,cc], mtrigger/MTriggerPatternDecode.[h,cc],
Index: /trunk/MagicSoft/Mars/mbadpixels/MBadPixelsTreat.cc
===================================================================
--- /trunk/MagicSoft/Mars/mbadpixels/MBadPixelsTreat.cc	(revision 5727)
+++ /trunk/MagicSoft/Mars/mbadpixels/MBadPixelsTreat.cc	(revision 5728)
@@ -67,4 +67,5 @@
 
 #include <TEnv.h>
+#include <TObjString.h>
 
 #include "MArrayD.h" // Used instead of TArrayD because operator[] has no range check
@@ -101,5 +102,5 @@
 //
 MBadPixelsTreat::MBadPixelsTreat(const char *name, const char *title)
-  : fFlags(0), fNumMinNeighbors(3), fNamePedPhotCam("MPedPhotCam")
+  : fFlags(0), fNumMinNeighbors(3)
 {
     fName  = name  ? name  : gsDefName.Data();
@@ -123,4 +124,9 @@
 }
 
+void MBadPixelsTreat::AddNamePedPhotCam(const char *name)
+{
+    fNamePedPhotCams.Add(new TObjString(name));
+}
+
 // --------------------------------------------------------------------------
 //
@@ -159,15 +165,37 @@
     }
 
-    fPedPhot = 0;
-    if (IsProcessPedestalEvt() || IsProcessPedestalRun())
-    {
-        fPedPhot = (MPedPhotCam*)pList->FindObject(AddSerialNumber(fNamePedPhotCam), "MPedPhotCam");
-        if (!fPedPhot)
-        {
-            *fLog << err << AddSerialNumber("MPedPhotCam") << " not found... aborting." << endl;
-            *fLog << " Use  MBadPixelsTreat::SetProcessPedestalRun(kFALSE)  and" << endl;
-            *fLog << " MBadPixelsTreat::SetProcessPedestalEvt(kFALSE) to switch" << endl;
-            *fLog << " Pedestal treatment off." << endl;
-            return kFALSE;
+    const Bool_t proc = IsProcessPedestalEvt() || IsProcessPedestalRun();
+
+    if (fNamePedPhotCams.GetSize()>0 && !proc)
+    {
+        *fLog << err << "Pedestal list contains entries, but pedestal treatment is switched off... abort." << endl;
+        return kFALSE;
+    }
+
+    if (proc)
+    {
+        if (fNamePedPhotCams.GetSize()==0)
+        {
+            *fLog << inf << "No container names specified... using default: MPedPhotCam." << endl;
+            AddNamePedPhotCam();
+        }
+
+        fPedPhotCams.Clear();
+
+        TIter Next(&fNamePedPhotCams);
+        TObject *o=0;
+        while ((o=Next()))
+        {
+            TObject *p = pList->FindObject(AddSerialNumber(o->GetName()), "MPedPhotCam");
+            if (!p)
+            {
+                *fLog << err << AddSerialNumber(o->GetName()) << " [MPedPhotCam] not found... aborting." << endl;
+                //*fLog << " Use  MBadPixelsTreat::SetProcessPedestalRun(kFALSE)  and" << endl;
+                //*fLog << " MBadPixelsTreat::SetProcessPedestalEvt(kFALSE) to switch" << endl;
+                //*fLog << " Pedestal treatment off." << endl;
+                return kFALSE;
+            }
+
+            fPedPhotCams.Add(p);
         }
     }
@@ -313,7 +341,7 @@
 // --------------------------------------------------------------------------
 //
-void MBadPixelsTreat::InterpolatePedestals() const
-{
-    const Int_t entries = fPedPhot->GetSize();
+void MBadPixelsTreat::InterpolatePedestals(MPedPhotCam &pedphot) const
+{
+    const Int_t entries = pedphot.GetSize();
 
     // Create arrays (FIXME: Check if its possible to create it only once)
@@ -336,5 +364,5 @@
         //
         const MGeomPix    &gpix = (*fGeomCam)[i];
-        const MPedPhotPix &ppix = (*fPedPhot)[i];
+        const MPedPhotPix &ppix = pedphot[i];
 
         // Do Not-Use-Central-Pixel
@@ -372,5 +400,5 @@
             //
             const Double_t    nratio = fGeomCam->GetPixRatio(nidx);
-            const MPedPhotPix &nppix = (*fPedPhot)[nidx];
+            const MPedPhotPix &nppix = pedphot[nidx];
 
             //
@@ -402,5 +430,21 @@
         rms[i]  = TMath::Sqrt(rms[i]/(num*ratio));
 
-        (*fPedPhot)[i].Set(ped[i], rms[i]);
+        pedphot[i].Set(ped[i], rms[i]);
+    }
+    pedphot.SetReadyToSave();
+}
+
+// --------------------------------------------------------------------------
+//
+// loop over all MPedPhotCam and interpolate them
+//
+void MBadPixelsTreat::InterpolatePedestals() const
+{
+    TIter Next(&fPedPhotCams);
+    MPedPhotCam *cam=0;
+    while ((cam=(MPedPhotCam*)Next()))
+    {
+        InterpolatePedestals(*cam);
+        cam->ReCalc(*fGeomCam, fBadPixels);
     }
 }
@@ -427,5 +471,5 @@
         const Int_t n0 = gpix.GetNumNeighbors();
 
-        MArrayD time(6);
+        MArrayD time(n0);
         for (int j=0; j<n0; j++)
             time[j] = (*fTimes)[gpix.GetNeighbor(j)];
@@ -436,13 +480,13 @@
         Double_t min=FLT_MAX;
         for (int j=0; j<n0; j++)
-            for (int k=1; k<j+1; k++)
+            for (int k=0; k<j; k++)
             {
-                const Double_t diff = TMath::Abs(time[n0-k] - time[n0-k-j]);
+                const Double_t diff = TMath::Abs(time[j] - time[k]);
 
                 if (diff>=min && diff<250)
                     continue;
 
-                p0 = n0-k;
-                p1 = n0-k-j;
+                p0  = j;
+                p1  = k;
                 min = diff;
             }
Index: /trunk/MagicSoft/Mars/mbadpixels/MBadPixelsTreat.h
===================================================================
--- /trunk/MagicSoft/Mars/mbadpixels/MBadPixelsTreat.h	(revision 5727)
+++ /trunk/MagicSoft/Mars/mbadpixels/MBadPixelsTreat.h	(revision 5728)
@@ -16,13 +16,16 @@
 private:
     MGeomCam      *fGeomCam;   //!
-    MPedPhotCam   *fPedPhot;   //!
+    //MPedPhotCam   *fPedPhot;   //!
     MCerPhotEvt   *fEvt;       //!
     MArrivalTime  *fTimes;     //!
     MBadPixelsCam *fBadPixels; //!
 
+    TList fPedPhotCams;
+
     Byte_t fFlags;       // flag for the method which is used
     Byte_t fNumMinNeighbors;
 
-    TString fNamePedPhotCam; // name of the 'MPedPhotCam' container
+    //TString fNamePedPhotCam; // name of the 'MPedPhotCam' container
+    TList fNamePedPhotCams;
 
     enum
@@ -40,4 +43,5 @@
     void InterpolateTimes() const;
     void InterpolateSignal() const;
+    void InterpolatePedestals(MPedPhotCam &pedphot) const;
     void InterpolatePedestals() const;
 
@@ -92,5 +96,9 @@
 
     void SetNumMinNeighbors(Byte_t num) { fNumMinNeighbors=num; }
-    void SetNamePedPhotCam(const char *name)    { fNamePedPhotCam = name; }
+    void AddNamePedPhotCam(const char *name="MPedPhotCam");
+    void SetNamePedPhotCam(const char *name)
+    {
+        AddNamePedPhotCam(name);
+    } // Deprecated! Use AddNamePedPhotCam instead (directly)
 
     ClassDef(MBadPixelsTreat, 1) // Task to treat bad pixels (interpolation, unmapping)
Index: /trunk/MagicSoft/Mars/mraw/MRawEvtPixelIter.cc
===================================================================
--- /trunk/MagicSoft/Mars/mraw/MRawEvtPixelIter.cc	(revision 5727)
+++ /trunk/MagicSoft/Mars/mraw/MRawEvtPixelIter.cc	(revision 5728)
@@ -71,4 +71,62 @@
 // --------------------------------------------------------------------------
 //
+// return kTRUE  the lo gains exist for the actual pixel, else return kFALSE
+//
+#include <iostream>
+Bool_t MRawEvtPixelIter::HasLoGain() const
+{
+    // We have no lo-gain at all
+    if (!fLoGainId)
+        return kFALSE;
+
+    // This is to make the first call of this function in Next()
+    // work properly! NEVER call this function before Next()
+    if (fNumLoGainEntry==0)
+        return kTRUE;
+
+//    cout << fNumLoGainEntry << " " << flush;
+//    cout << fData->fLoGainPixId->GetSize() << " " << flush;
+
+    if (fNumLoGainEntry>fData->fLoGainPixId->GetSize())
+        return kFALSE;
+
+/*
+    cout << fNumLoGainEntry << " " << flush;
+    cout << fData->fLoGainPixId->GetSize() << " " << flush;
+    cout << fData->fLoGainPixId->GetArray() << " " << flush;
+    cout << fLoGainId << endl;
+    */
+//    cout << (int)(fLoGainId-fData->fLoGainPixId->GetArray()) << " " << flush;
+//    cout << (int)(fHiGainId-fData->fHiGainPixId->GetArray()) << " " << flush;
+
+    Bool_t rc = *fHiGainId!=*fLoGainId;
+
+//    cout << "done." << endl;
+
+
+    if (rc)
+        return kFALSE;
+    /*
+    // We have no lo-gain at all
+    if (!fLoGainId)
+        return kFALSE;
+
+    // This is to make the first call of this function in Next()
+    // work properly! NEVER call this function before Next()
+    if (fNumLoGainEntry==0)
+        return kTRUE;
+
+    // Make sure, that we don't exceed the last entry in the array!
+    if (fNumLoGainEntry==fData->fLoGainPixId->GetSize())
+        return kFALSE;
+
+    // Check whether the current position in lo-gain fits the hi-gain
+    return *fHiGainId!=*fLoGainId;
+     */
+    return kTRUE;
+}
+
+// --------------------------------------------------------------------------
+//
 // It steps to the next pixel. If there is no next pixel NULL is returned.
 // If a next pixel where found, a pointer to the primary given (constructor)
@@ -86,16 +144,15 @@
     // if we are already at the last entry there is no 'next' entry anymore
     //
-    if (fNumLoGainEntry != fData->fLoGainPixId->GetSize())
-        if (HasLoGain())
-        {
-            //
-            // if higainpixid and logainpixid of the actual pixel are
-            // identical then we have to move the pointer to the next
-            // entry in the lo gains
-            //
-            fNumLoGainEntry++;
-            fLoGainId++;
-            fLoGainPos += fNumLoGainSamples;
-        }
+    if (HasLoGain())
+    {
+        //
+        // if higainpixid and logainpixid of the actual pixel are
+        // identical then we have to move the pointer to the next
+        // entry in the lo gains
+        //
+        fNumLoGainEntry++;
+        fLoGainId++;
+        fLoGainPos += fNumLoGainSamples;
+    }
 
     //
Index: /trunk/MagicSoft/Mars/mraw/MRawEvtPixelIter.h
===================================================================
--- /trunk/MagicSoft/Mars/mraw/MRawEvtPixelIter.h	(revision 5727)
+++ /trunk/MagicSoft/Mars/mraw/MRawEvtPixelIter.h	(revision 5728)
@@ -90,11 +90,5 @@
     Short_t GetIdxMaxHiLoGainSample() const;
 
-    Bool_t HasLoGain() const
-    {
-        //
-        // return kTRUE  the lo gains exist for the actual pixel, else return kFALSE
-        //
-        return fLoGainId && *fHiGainId==*fLoGainId;
-    }
+    Bool_t HasLoGain() const;
     Bool_t IsABFlagValid() const { return fABFlags ? kTRUE : kFALSE; }
     Bool_t HasABFlag() const
Index: /trunk/MagicSoft/Mars/msignal/MExtractor.cc
===================================================================
--- /trunk/MagicSoft/Mars/msignal/MExtractor.cc	(revision 5727)
+++ /trunk/MagicSoft/Mars/msignal/MExtractor.cc	(revision 5728)
@@ -422,7 +422,7 @@
         *fLog << GetDescriptor() << ":" << endl;
 
-    *fLog << " Hi Gain Range:  " << (int)fHiGainFirst << " " << (int)fHiGainLast << endl;
-    *fLog << " Lo Gain Range:  " << (int)fLoGainFirst << " " << (int)fLoGainLast << endl;
-    *fLog << " Saturation Lim: " << (int)fSaturationLimit << endl;
+    *fLog << " Hi Gain Range:      " << (int)fHiGainFirst << " " << (int)fHiGainLast << endl;
+    *fLog << " Lo Gain Range:      " << (int)fLoGainFirst << " " << (int)fLoGainLast << endl;
+    *fLog << " Saturation Lim:     " << (int)fSaturationLimit << endl;
     *fLog << " Num Samples HiGain: " << fNumHiGainSamples << "  LoGain: " << fNumLoGainSamples << endl;
 }
