Index: trunk/MagicSoft/Mars/msignal/MExtractTimeAndChargeDigitalFilter.cc
===================================================================
--- trunk/MagicSoft/Mars/msignal/MExtractTimeAndChargeDigitalFilter.cc	(revision 5296)
+++ trunk/MagicSoft/Mars/msignal/MExtractTimeAndChargeDigitalFilter.cc	(revision 5298)
@@ -51,16 +51,17 @@
 #include "MExtractTimeAndChargeDigitalFilter.h"
 
+#include <errno.h>
+#include <fstream>
+
+#include <TFile.h>
+#include <TH1F.h>
+#include <TH2F.h>
+#include <TString.h>
+#include <TMatrix.h>
+
 #include "MLog.h"
 #include "MLogManip.h"
 
 #include "MPedestalPix.h"
-
-#include "TFile.h" 
-#include "TH1F.h" 
-#include "TH2F.h" 
-#include "TString.h"
-#include "TMatrix.h"
-
-#include <fstream>
 
 ClassImp(MExtractTimeAndChargeDigitalFilter);
@@ -78,4 +79,5 @@
 const Int_t  MExtractTimeAndChargeDigitalFilter::fgSignalStartBinHiGain = 4;
 const Int_t  MExtractTimeAndChargeDigitalFilter::fgSignalStartBinLoGain = 4;
+
 // --------------------------------------------------------------------------
 //
@@ -91,16 +93,14 @@
 MExtractTimeAndChargeDigitalFilter::MExtractTimeAndChargeDigitalFilter(const char *name, const char *title) 
 {
-
-  fName  = name  ? name  : "MExtractTimeAndChargeDigitalFilter";
-  fTitle = title ? title : "Digital Filter";
-
-  SetRange(fgHiGainFirst, fgHiGainLast, fgLoGainFirst, fgLoGainLast);
-  SetWindowSize();
-  SetBinningResolution();
-  SetSignalStartBin();
-
-  ReadWeightsFile("");
+    fName  = name  ? name  : "MExtractTimeAndChargeDigitalFilter";
+    fTitle = title ? title : "Digital Filter";
+
+    SetRange(fgHiGainFirst, fgHiGainLast, fgLoGainFirst, fgLoGainLast);
+    SetWindowSize();
+    SetBinningResolution();
+    SetSignalStartBin();
+
+    ReadWeightsFile("");
 }
-
 
 // ---------------------------------------------------------------------------------------
@@ -129,5 +129,6 @@
 
   if (fWindowSizeHiGain > availhirange)
-    {
+  {
+      // Please simplify this!
       *fLog << warn << GetDescriptor() 
             << Form("%s%2i%s%2i%s%2i%s",": Hi Gain window size: ",fWindowSizeHiGain,
@@ -150,4 +151,5 @@
       if (fWindowSizeLoGain > availlorange)
         {
+      // Please simplify this!
           *fLog << warn << GetDescriptor() 
                 << Form("%s%2i%s%2i%s%2i%s",": Lo Gain window size: ",fWindowSizeLoGain,
@@ -182,22 +184,32 @@
 Bool_t MExtractTimeAndChargeDigitalFilter::ReInit(MParList *pList)
 {
-
-  if (!MExtractTimeAndCharge::ReInit(pList))
-    return kFALSE;
-
-  Int_t range = fHiGainLast - fHiGainFirst + 1 + fHiLoLast;
-
-  fHiGainSignal.Set(range);
-
-  range = fLoGainLast - fLoGainFirst + 1;
-
-  fLoGainSignal.Set(range);
-
-  fTimeShiftHiGain = (Float_t)fHiGainFirst + 0.5 + 1./fBinningResolutionHiGain;
-  fTimeShiftLoGain = (Float_t)fLoGainFirst + 0.5 + 1./fBinningResolutionLoGain;
-
-  return kTRUE;
+    if (!MExtractTimeAndCharge::ReInit(pList))
+        return kFALSE;
+
+    fHiGainSignal.Set(fHiGainLast - fHiGainFirst + 1 + fHiLoLast);
+    fLoGainSignal.Set(fLoGainLast - fLoGainFirst + 1);
+
+    fTimeShiftHiGain = (Float_t)fHiGainFirst + 0.5 + 1./fBinningResolutionHiGain;
+    fTimeShiftLoGain = (Float_t)fLoGainFirst + 0.5 + 1./fBinningResolutionLoGain;
+
+    return kTRUE;
 }
 
+Int_t MExtractTimeAndChargeDigitalFilter::PreProcess(MParList *pList)
+{
+    *fLog << endl;
+    *fLog << inf << "Using the following weights: " << endl;
+    *fLog << "Hi-Gain:" << endl;
+
+    for (Int_t i=0; i<fBinningResolutionHiGain*fWindowSizeHiGain; i++)
+        *fLog << " " << fAmpWeightsHiGain[i] << " \t " << fTimeWeightsHiGain[i] << endl;
+
+    *fLog << "Lo-Gain:" << endl;
+
+    for (Int_t i=0; i<fBinningResolutionLoGain*fWindowSizeLoGain; i++)
+        *fLog << " " << fAmpWeightsLoGain[i] << " \t " << fTimeWeightsLoGain[i] << endl;
+
+    return MExtractTimeAndCharge::PreProcess(pList);
+}
 
 void MExtractTimeAndChargeDigitalFilter::FindTimeAndChargeHiGain(Byte_t *ptr, Byte_t *logain, Float_t &sum, Float_t &dsum, 
@@ -267,7 +279,7 @@
   const Float_t ABoffs = ped.GetPedestalABoffset();
 	
-  Float_t PedMean[2];
-  PedMean[0] = pedes + ABoffs;
-  PedMean[1] = pedes - ABoffs;
+  Float_t pedmean[2];
+  pedmean[0] = pedes + ABoffs;
+  pedmean[1] = pedes - ABoffs;
 
   //
@@ -286,5 +298,5 @@
       {
         const Int_t   idx = fBinningResolutionHiGain*sample+fBinningResolutionHalfHiGain;
-        const Float_t pex = fHiGainSignal[sample+i]-PedMean[(sample+i+abflag) & 0x1];
+        const Float_t pex = fHiGainSignal[sample+i]-pedmean[(sample+i+abflag) & 0x1];
 	sum              += fAmpWeightsHiGain [idx]*pex; 
 	time_sum         += fTimeWeightsHiGain[idx]*pex;
@@ -298,52 +310,49 @@
       }
     } /*   for (Int_t i=0;i<range-fWindowSizeHiGain;i++) */
-  
-  if (fmax!=0)
-    {
-      ftime_max        /= fmax;
-      Int_t t_iter      = Int_t(ftime_max*fBinningResolutionHiGain);
-      Int_t sample_iter = 0;
-
-      while ( t_iter > fBinningResolutionHalfHiGain-1 || t_iter < -fBinningResolutionHalfHiGain )
-        {
-          if (t_iter > fBinningResolutionHalfHiGain-1)
-            {
-              t_iter -= fBinningResolutionHiGain;
-              max_p--; 
-              sample_iter--;
-            }
-          if (t_iter < -fBinningResolutionHalfHiGain)
-            {
-              t_iter += fBinningResolutionHiGain;
-              max_p++; 
-              sample_iter++;
-            }
-        }
-      
-      sum = 0.;
-      //
-      // Slide with a window of size fWindowSizeHiGain over the sample 
-      // and multiply the entries with the corresponding weights
-      //
-      for (Int_t sample=0; sample < fWindowSizeHiGain; sample++)
-      {
-        const Int_t   idx = fBinningResolutionHiGain*sample + fBinningResolutionHalfHiGain + t_iter;
-        const Int_t   ids = max_p + sample;
-        const Float_t pex = ids < 0 ? 0. : 
-          ( ids > range ? 0. : fHiGainSignal[ids]-PedMean[(ids+abflag) & 0x1]);
-	sum              += fAmpWeightsHiGain [idx]*pex; 
-	time_sum         += fTimeWeightsHiGain[idx]*pex;
-      }
-
-      if (sum != 0.)
-	time = max_p + fTimeShiftHiGain /* this shifts the time to the start of the rising edge */
-             - ((Float_t)t_iter)/fBinningResolutionHiGain - time_sum/sum;
-      else 
-        time = 0.;
-    } /* if (max!=0) */ 
-    else 
-      time=0.;
-    
-  return;
+
+  time = 0;
+  if (fmax==0)
+      return;
+
+  ftime_max        /= fmax;
+  Int_t t_iter      = Int_t(ftime_max*fBinningResolutionHiGain);
+  Int_t sample_iter = 0;
+
+  while ( t_iter > fBinningResolutionHalfHiGain-1 || t_iter < -fBinningResolutionHalfHiGain )
+    {
+      if (t_iter > fBinningResolutionHalfHiGain-1)
+        {
+          t_iter -= fBinningResolutionHiGain;
+          max_p--; 
+          sample_iter--;
+        }
+      if (t_iter < -fBinningResolutionHalfHiGain)
+        {
+          t_iter += fBinningResolutionHiGain;
+          max_p++; 
+          sample_iter++;
+        }
+    }
+  
+  sum = 0.;
+  //
+  // Slide with a window of size fWindowSizeHiGain over the sample 
+  // and multiply the entries with the corresponding weights
+  //
+  for (Int_t sample=0; sample < fWindowSizeHiGain; sample++)
+  {
+    const Int_t   idx = fBinningResolutionHiGain*sample + fBinningResolutionHalfHiGain + t_iter;
+    const Int_t   ids = max_p + sample;
+    const Float_t pex = ids < 0 ? 0. : 
+      ( ids > range ? 0. : fHiGainSignal[ids]-pedmean[(ids+abflag) & 0x1]);
+    sum              += fAmpWeightsHiGain [idx]*pex; 
+    time_sum         += fTimeWeightsHiGain[idx]*pex;
+  }
+
+  if (sum == 0)
+      return;
+
+  time = max_p + fTimeShiftHiGain /* this shifts the time to the start of the rising edge */
+      - ((Float_t)t_iter)/fBinningResolutionHiGain - time_sum/sum;
 }
 
@@ -386,7 +395,7 @@
   const Float_t ABoffs = ped.GetPedestalABoffset();
 	
-  Float_t PedMean[2];
-  PedMean[0] = pedes + ABoffs;
-  PedMean[1] = pedes - ABoffs;
+  Float_t pedmean[2];
+  pedmean[0] = pedes + ABoffs;
+  pedmean[1] = pedes - ABoffs;
 
   //
@@ -405,5 +414,5 @@
       {
         const Int_t   idx = fBinningResolutionLoGain*sample+fBinningResolutionHalfLoGain;
-        const Float_t pex = fLoGainSignal[sample+i]-PedMean[(sample+i+abflag) & 0x1];
+        const Float_t pex = fLoGainSignal[sample+i]-pedmean[(sample+i+abflag) & 0x1];
 	sum              += fAmpWeightsLoGain [idx]*pex; 
 	time_sum         += fTimeWeightsLoGain[idx]*pex;
@@ -417,102 +426,110 @@
       }
     } /*   for (Int_t i=0;i<range-fWindowSizeLoGain;i++) */
-  
-  if (fmax!=0)
-    {
-      ftime_max        /= fmax;
-      Int_t t_iter      = Int_t(ftime_max*fBinningResolutionLoGain);
-      Int_t sample_iter = 0;
-
-      while ( t_iter > fBinningResolutionHalfLoGain-1 || t_iter < -fBinningResolutionHalfLoGain )
-        {
-          if (t_iter > fBinningResolutionHalfLoGain-1)
-            {
-              t_iter -= fBinningResolutionLoGain;
-              max_p--; 
-              sample_iter--;
-            }
-          if (t_iter < -fBinningResolutionHalfLoGain)
-            {
-              t_iter += fBinningResolutionLoGain;
-              max_p++; 
-              sample_iter++;
-            }
-        }
-      
-      sum = 0.;
-      //
-      // Slide with a window of size fWindowSizeLoGain over the sample 
-      // and multiply the entries with the corresponding weights
-      //
-      for (Int_t sample=0; sample < fWindowSizeLoGain; sample++)
-      {
-        const Int_t   idx = fBinningResolutionLoGain*sample + fBinningResolutionHalfLoGain + t_iter;
-        const Int_t   ids = max_p + sample;
-        const Float_t pex = ids < 0 ? 0. : 
-          ( ids > range ? 0. : fLoGainSignal[ids]-PedMean[(ids+abflag) & 0x1]);
-	sum              += fAmpWeightsLoGain [idx]*pex; 
-	time_sum         += fTimeWeightsLoGain[idx]*pex;
-      }
-
-      if (sum != 0.)
-	time = max_p + fTimeShiftLoGain /* this shifts the time to the start of the rising edge */
-             - ((Float_t)t_iter)/fBinningResolutionLoGain - time_sum/sum;
-      else 
-        time = 0.;
-    } /* if (max!=0) */ 
-    else 
-      time=0.;
-    
-  return;
+
+  time = 0;
+  if (fmax==0)
+      return;
+
+  ftime_max        /= fmax;
+  Int_t t_iter      = Int_t(ftime_max*fBinningResolutionLoGain);
+  Int_t sample_iter = 0;
+
+  while ( t_iter > fBinningResolutionHalfLoGain-1 || t_iter < -fBinningResolutionHalfLoGain )
+    {
+      if (t_iter > fBinningResolutionHalfLoGain-1)
+        {
+          t_iter -= fBinningResolutionLoGain;
+          max_p--; 
+          sample_iter--;
+        }
+      if (t_iter < -fBinningResolutionHalfLoGain)
+        {
+          t_iter += fBinningResolutionLoGain;
+          max_p++; 
+          sample_iter++;
+        }
+    }
+  
+  sum = 0.;
+
+  //
+  // Slide with a window of size fWindowSizeLoGain over the sample 
+  // and multiply the entries with the corresponding weights
+  //
+  for (Int_t sample=0; sample < fWindowSizeLoGain; sample++)
+  {
+    const Int_t   idx = fBinningResolutionLoGain*sample + fBinningResolutionHalfLoGain + t_iter;
+    const Int_t   ids = max_p + sample;
+    const Float_t pex = ids < 0 ? 0. : 
+      ( ids > range ? 0. : fLoGainSignal[ids]-pedmean[(ids+abflag) & 0x1]);
+    sum              += fAmpWeightsLoGain [idx]*pex; 
+    time_sum         += fTimeWeightsLoGain[idx]*pex;
+  }
+
+  if (sum == 0)
+      return;
+
+  time = max_p + fTimeShiftLoGain /* this shifts the time to the start of the rising edge */
+      - ((Float_t)t_iter)/fBinningResolutionLoGain - time_sum/sum;
 }
 
+// --------------------------------------------------------------------------
+//
+// Read the setup from a TEnv, eg:
+//   MJPedestal.MExtractor.WindowSizeHiGain: 6
+//   MJPedestal.MExtractor.WindowSizeLoGain: 6
+//   MJPedestal.MExtractor.BinningResolutionHiGain: 10
+//   MJPedestal.MExtractor.BinningResolutionLoGain: 10
+//   MJPedestal.MExtractor.WeightsFile: filename
+//
 Int_t MExtractTimeAndChargeDigitalFilter::ReadEnv(const TEnv &env, TString prefix, Bool_t print)
 {
-  
-  Byte_t hw = fWindowSizeHiGain;
-  Byte_t lw = fWindowSizeLoGain;
-  Bool_t rc = kFALSE;
-  
-  if (IsEnvDefined(env, prefix, "WindowSizeHiGain", print))
-    {
-      hw = GetEnvValue(env, prefix, "WindowSizeHiGain", hw);
-      rc = kTRUE;
-    }
-  if (IsEnvDefined(env, prefix, "WindowSizeLoGain", print))
-    {
-      lw = GetEnvValue(env, prefix, "WindowSizeLoGain", lw);
-      rc = kTRUE;
-    }
-  
-  if (rc)
-    SetWindowSize(hw, lw);
-    
-  if (IsEnvDefined(env, prefix, "BinningResolution", print))
-    {
-      SetBinningResolution(GetEnvValue(env, prefix, "BinningResolutionHiGain", fBinningResolutionHiGain),
-                           GetEnvValue(env, prefix, "BinningResolutionLoGain", fBinningResolutionLoGain));
-      rc = kTRUE;
-    }
-  
-  rc = MExtractor::ReadEnv(env, prefix, print) ? kTRUE : rc;
-  
-  return rc;
-}
-
-Int_t  MExtractTimeAndChargeDigitalFilter::PostProcess()
-{
-  
-  *fLog << endl;
-  *fLog << inf << "Used High Gain weights in the extractor: " << endl;
-  
-  for (Int_t i=0;i<fBinningResolutionHiGain*fWindowSizeHiGain; i++)
-    *fLog << inf << fAmpWeightsHiGain[i] << "   " << fTimeWeightsHiGain[i] << endl;
-
-  *fLog << endl;
-  *fLog << inf << "Used Low Gain weights in the extractor: " << endl;
-  for (Int_t i=0;i<fBinningResolutionLoGain*fWindowSizeLoGain; i++)
-    *fLog << inf << fAmpWeightsLoGain[i] << "   " << fTimeWeightsLoGain[i] << endl;
-  
-  return kTRUE;
+    Byte_t hw = fWindowSizeHiGain;
+    Byte_t lw = fWindowSizeLoGain;
+    Bool_t rc = kFALSE;
+
+    if (IsEnvDefined(env, prefix, "WindowSizeHiGain", print))
+    {
+        hw = GetEnvValue(env, prefix, "WindowSizeHiGain", hw);
+        rc = kTRUE;
+    }
+    if (IsEnvDefined(env, prefix, "WindowSizeLoGain", print))
+    {
+        lw = GetEnvValue(env, prefix, "WindowSizeLoGain", lw);
+        rc = kTRUE;
+    }
+
+    if (rc)
+        SetWindowSize(hw, lw);
+
+    Bool_t rc2 = kFALSE;
+    Int_t brh = fBinningResolutionHiGain;
+    Int_t brl = fBinningResolutionLoGain;
+
+    if (IsEnvDefined(env, prefix, "BinningResolutionHiGain", print))
+    {
+        brh = GetEnvValue(env, prefix, brh);
+        rc2 = kTRUE;
+    }
+    if (IsEnvDefined(env, prefix, "BinningResolutionLoGain", print))
+    {
+        brl = GetEnvValue(env, prefix, brl);
+        rc2 = kTRUE;
+    }
+
+    if (rc2)
+    {
+        SetBinningResolution(brh, brl);
+        rc = kTRUE;
+    }
+
+    if (IsEnvDefined(env, prefix, "WeightsFile", print))
+    {
+        if (!ReadWeightsFile(GetEnvValue(env, prefix, "WeightsFile", "")))
+            return kERROR;
+        rc = kTRUE;
+    }
+
+    return MExtractTimeAndCharge::ReadEnv(env, prefix, print) ? kTRUE : rc;
 }
 
@@ -526,106 +543,133 @@
 Bool_t MExtractTimeAndChargeDigitalFilter::ReadWeightsFile(TString filename)
 {
-
-  fAmpWeightsHiGain .Set(fBinningResolutionHiGain*fWindowSizeHiGain); 
-  fAmpWeightsLoGain .Set(fBinningResolutionLoGain*fWindowSizeLoGain); 
-  fTimeWeightsHiGain.Set(fBinningResolutionHiGain*fWindowSizeHiGain); 
-  fTimeWeightsLoGain.Set(fBinningResolutionLoGain*fWindowSizeLoGain); 
-  
-  if (filename.IsNull())
-    {
-      for (UInt_t i=0; i<fAmpWeightsHiGain.GetSize(); i++)
-        {
-          fAmpWeightsHiGain [i] = 1.;
-          fTimeWeightsHiGain[i] = 1.;
-        }
-      for (UInt_t i=0; i<fAmpWeightsLoGain.GetSize(); i++)
-        {
-          fAmpWeightsLoGain [i] = 1.;
-          fTimeWeightsLoGain[i] = 1.;
-        }
-      return kTRUE;
-    }
-
-  ifstream fin(filename.Data());
-  
-  if (!fin)
-    {
-      *fLog << err << GetDescriptor()
-            << ": No weights file found: " << filename << endl;
-      return kFALSE;
-    }
-
-  Int_t len = 0;
-  Int_t cnt = 0;
-  Bool_t hi = kFALSE;
-  Bool_t lo = kFALSE;
-  
-  TString str;
-
-  while (1)
-    {
-
-      str.ReadLine(fin);
-      if (!fin)
-        break;
-      
-
-      if (str.Contains("# High Gain Weights:"))
-        {
-          str.ReplaceAll("# High Gain Weights:","");
-          sscanf(str.Data(),"%2i%2i",&fWindowSizeHiGain,&fBinningResolutionHiGain);
-          *fLog << inf << "Found number of High Gain slices: " << fWindowSizeHiGain 
-                       << " and High Gain resolution: " << fBinningResolutionHiGain << endl;
-          len = fBinningResolutionHiGain*fWindowSizeHiGain;
-          fAmpWeightsHiGain .Set(len); 
-          fTimeWeightsHiGain.Set(len); 
-          hi = kTRUE;
-          continue;
-        }
-      
-      if (str.Contains("# Low Gain Weights:"))
-        {
-          str.ReplaceAll("# Low Gain Weights:","");
-          sscanf(str.Data(),"%2i%2i",&fWindowSizeLoGain,&fBinningResolutionLoGain);
-          *fLog << inf << "Found number of Low Gain slices: " << fWindowSizeLoGain 
-                       << " and Low Gain resolution: " << fBinningResolutionLoGain << endl;
-          len = fBinningResolutionLoGain*fWindowSizeHiGain;
-          fAmpWeightsLoGain .Set(len); 
-          fTimeWeightsLoGain.Set(len); 
-          lo = kTRUE;
-          continue;
-        }
-      
-      if (str.Contains("#"))
-        continue;
-
-      if (len == 0)
-        continue;
-
-      sscanf(str.Data(),"\t%f\t%f",lo ? &fAmpWeightsLoGain [cnt] : &fAmpWeightsHiGain [cnt],
-                                         lo ? &fTimeWeightsLoGain[cnt] : &fTimeWeightsHiGain[cnt]);
-
-      if (++cnt == len)
-        {
-          len = 0;
-          cnt = 0;
-        }
-    }
-
-  if (cnt != len)
-    {
-      *fLog << err << GetDescriptor()
-            << ": Size mismatch in weights file " << filename << endl;
-      return kFALSE;
-    }
-
-  if (!hi)
-    {
-      *fLog << err << GetDescriptor()
-            << ": No correct header found in weights file " << filename << endl;
-      return kFALSE;
-    }
-
-  return kTRUE;
+    fAmpWeightsHiGain .Set(fBinningResolutionHiGain*fWindowSizeHiGain);
+    fAmpWeightsLoGain .Set(fBinningResolutionLoGain*fWindowSizeLoGain);
+    fTimeWeightsHiGain.Set(fBinningResolutionHiGain*fWindowSizeHiGain);
+    fTimeWeightsLoGain.Set(fBinningResolutionLoGain*fWindowSizeLoGain);
+
+    if (filename.IsNull())
+    {
+        fAmpWeightsHiGain.Reset(1);
+        fTimeWeightsHiGain.Reset(1);
+        fAmpWeightsLoGain.Reset(1);
+        fTimeWeightsLoGain.Reset(1);
+        return kTRUE;
+    }
+
+    ifstream fin(filename.Data());
+    if (!fin)
+    {
+        *fLog << err << GetDescriptor() << ": ERROR - Cannot open file " << filename << ": ";
+        *fLog << strerror(errno) << endl;
+        return kFALSE;
+    }
+
+    *fLog << inf << "Reading weights file " << filename << "..." << flush;
+
+    Int_t len = 0;
+    Int_t cnt = 0;
+    Int_t line = 0;
+    Bool_t hi = kFALSE;
+    Bool_t lo = kFALSE;
+
+    TString str;
+
+    while (1)
+    {
+        str.ReadLine(fin);
+        if (!fin)
+            break;
+
+        line++;
+
+        if (str.Contains("# High Gain Weights:"))
+        {
+            if (hi)
+            {
+                *fLog << err << "ERROR - 'High Gain Weights' found twice in line #" << line << "." << endl;
+                return kFALSE;
+            }
+
+            if (2!=sscanf(str.Data(), "# High Gain Weights:%2i %2i", &fWindowSizeHiGain, &fBinningResolutionHiGain))
+            {
+                *fLog << err << "ERROR - Wrong number of arguments in line #" << line << ":" << endl;
+                *fLog << str << endl;
+                return kFALSE;
+            }
+
+            len = fBinningResolutionHiGain*fWindowSizeHiGain;
+            fAmpWeightsHiGain .Set(len);
+            fTimeWeightsHiGain.Set(len);
+            hi = kTRUE;
+            continue;
+        }
+
+        if (str.Contains("# Low Gain Weights:"))
+        {
+            if (lo)
+            {
+                *fLog << err << "ERROR - 'Lo Gain Weights' found twice in line #" << line << "." << endl;
+                return kFALSE;
+            }
+
+            if (2!=sscanf(str.Data(),"# Low Gain Weights:%2i %2i", &fWindowSizeLoGain, &fBinningResolutionLoGain))
+            {
+                *fLog << err << "ERROR - Wrong number of arguments in line #" << line << ":" << endl;
+                *fLog << str << endl;
+                return kFALSE;
+            }
+
+            len = fBinningResolutionLoGain*fWindowSizeHiGain;
+            fAmpWeightsLoGain .Set(len);
+            fTimeWeightsLoGain.Set(len);
+            lo = kTRUE;
+            continue;
+        }
+
+        // Handle lines with comments
+        if (str.Contains("#"))
+            continue;
+
+        // Nothing found so far
+        if (len == 0)
+            continue;
+
+        if (2!=sscanf(str.Data(), "%f %f",
+                      lo ? &fAmpWeightsLoGain [cnt] : &fAmpWeightsHiGain [cnt],
+                      lo ? &fTimeWeightsLoGain[cnt] : &fTimeWeightsHiGain[cnt]))
+        {
+            *fLog << err << "ERROR - Wrong number of arguments in line #" << line << ":" << endl;
+            *fLog << str << endl;
+            return kFALSE;
+        }
+
+        if (++cnt == len)
+        {
+            len = 0;
+            cnt = 0;
+        }
+    }
+
+    if (cnt != len)
+    {
+        *fLog << err << "Size mismatch in weights file " << filename << endl;
+        return kFALSE;
+    }
+
+    if (!hi)
+    {
+        *fLog << err << "No correct header found in weights file " << filename << endl;
+        return kFALSE;
+    }
+
+    *fLog << "done." << endl;
+
+    *fLog << inf << " File contains " << fWindowSizeHiGain << " hi-gain slices ";
+    *fLog << "and with a resolution of " << fBinningResolutionHiGain << endl;
+
+    *fLog << inf << " File contains " << fWindowSizeLoGain << " lo-gain slices ";
+    *fLog << "and with a resolution of " << fBinningResolutionLoGain << endl;
+
+    return kTRUE;
 }
 
Index: trunk/MagicSoft/Mars/msignal/MExtractTimeAndChargeDigitalFilter.h
===================================================================
--- trunk/MagicSoft/Mars/msignal/MExtractTimeAndChargeDigitalFilter.h	(revision 5296)
+++ trunk/MagicSoft/Mars/msignal/MExtractTimeAndChargeDigitalFilter.h	(revision 5298)
@@ -16,5 +16,4 @@
 {
 private:
-
   static const Byte_t fgHiGainFirst;
   static const Byte_t fgHiGainLast;
@@ -34,9 +33,9 @@
   Float_t fTimeShiftLoGain;
   
-  Int_t   fSignalStartBinHiGain;
-  Int_t   fSignalStartBinLoGain;
+  Int_t   fSignalStartBinHiGain; //!
+  Int_t   fSignalStartBinLoGain; //!
 
-  Int_t   fWindowSizeHiGain;            
-  Int_t   fWindowSizeLoGain;            
+  Int_t   fWindowSizeHiGain;
+  Int_t   fWindowSizeLoGain;
 
   Int_t   fBinningResolutionHiGain;
@@ -45,16 +44,15 @@
   Int_t   fBinningResolutionHalfLoGain;
   
-  MArrayF fAmpWeightsHiGain; 
-  MArrayF fTimeWeightsHiGain;
-  MArrayF fAmpWeightsLoGain; 
-  MArrayF fTimeWeightsLoGain;
+  MArrayF fAmpWeightsHiGain;     //!
+  MArrayF fTimeWeightsHiGain;    //!
+  MArrayF fAmpWeightsLoGain;     //!
+  MArrayF fTimeWeightsLoGain;    //!
 
-  Bool_t ReInit( MParList *pList );
-  Int_t PostProcess();  
-  
+  Bool_t ReInit(MParList *pList);
+  Int_t PreProcess(MParList *pList);
+
   Int_t  ReadEnv(const TEnv &env, TString prefix, Bool_t print);
-  
+
 protected:
-
   virtual void FindTimeAndChargeHiGain(Byte_t *firstused, Byte_t *logain, Float_t &sum, Float_t &dsum,
                                        Float_t &time, Float_t &dtime,
@@ -89,9 +87,6 @@
   }
   
-  ClassDef(MExtractTimeAndChargeDigitalFilter, 0)   // Hendrik's digital filter
+  ClassDef(MExtractTimeAndChargeDigitalFilter, 1)   // Hendrik's digital filter
 };
 
 #endif
-
-
-
