Index: trunk/MagicSoft/Mars/msignal/MC_weights46.dat
===================================================================
--- trunk/MagicSoft/Mars/msignal/MC_weights46.dat	(revision 7095)
+++ trunk/MagicSoft/Mars/msignal/MC_weights46.dat	(revision 7098)
@@ -43,62 +43,62 @@
 # Low Gain Weights: 6 10
 # (Amplitude)  (Time)
-0.0138311 -0.0165919
-0.0117372 -0.0354005
-0.00232654 -0.0581018
--0.00791094 -0.0631271
--0.0131845 -0.0558022
--0.0154786 -0.0480772
--0.0171358 -0.0420274
--0.0199068 -0.0340744
--0.02688 -0.0117278
--0.0404328 0.0754397
--0.0357035 0.342673
-0.00409834 0.849274
-0.248584 1.52181
-0.528673 1.67974
-0.67261 1.47068
-0.735023 1.24965
-0.778865 1.10395
-0.832049 1.02856
-0.907609 1.00924
-1.07304 1.09208
-1.25051 1.14906
-1.37475 1.0253
-1.52056 0.653112
-1.53175 0.163076
-1.49683 -0.15273
-1.47188 -0.344252
-1.45258 -0.50129
-1.42348 -0.670218
-1.3673 -0.87756
-1.19832 -1.20557
-0.99973 -1.53248
-0.808836 -1.68211
-0.533208 -1.45238
-0.446023 -0.900265
-0.459037 -0.529769
-0.470945 -0.368053
-0.465845 -0.313525
-0.446981 -0.311926
-0.415815 -0.342317
-0.348688 -0.425439
-0.271752 -0.495437
-0.211195 -0.503879
-0.131582 -0.413276
-0.109187 -0.245156
-0.114005 -0.139006
-0.117671 -0.0939116
-0.116527 -0.078572
-0.11177 -0.0772896
-0.103901 -0.0840098
-0.0871027 -0.102357
-0.0758618 -0.119019
-0.0611863 -0.118991
-0.0430436 -0.0930814
-0.0390695 -0.0517162
-0.0409257 -0.027711
-0.0420581 -0.0186558
-0.0419035 -0.0164813
-0.0408279 -0.0174564
-0.0389147 -0.0202082
-0.0345678 -0.025232
+0.0446612 -0.385273
+0.038191 -0.00418687
+0.0386966 0.0212324
+0.0402881 0.0744799
+0.0415794 0.229615
+0.0598731 0.44332
+0.0758477 0.661518
+0.101509 1.10641
+0.159323 1.64997
+0.497256 2.83685
+0.245087 3.27499
+0.140546 2.46177
+0.58086 2.2849
+0.632721 2.45587
+0.72819 2.52835
+0.889583 2.48099
+0.980812 2.50031
+1.09885 2.55892
+1.21374 2.78769
+1.61928 3.08069
+1.38544 1.95583
+1.31998 1.1792
+1.50633 0.591226
+1.50916 0.0793899
+1.5008 -0.33188
+1.47339 -0.575386
+1.45362 -0.915309
+1.40214 -1.31593
+1.34175 -1.77904
+1.0661 -2.05471
+1.31087 -1.49798
+1.33793 -1.34758
+1.10172 -1.21719
+1.08133 -1.09356
+1.04007 -0.981455
+0.976745 -1.08299
+0.930979 -1.14774
+0.874203 -1.18348
+0.816708 -1.20126
+0.587354 -1.92869
+0.783078 -1.89621
+0.792771 -1.03439
+0.622278 -0.781807
+0.61184 -0.745831
+0.578792 -0.683741
+0.537336 -0.596328
+0.51443 -0.592858
+0.482294 -0.560586
+0.462351 -0.827587
+0.317989 -1.05649
+0.459672 -0.775035
+0.468287 -0.619961
+0.374182 -0.31635
+0.376946 -0.225242
+0.367075 -0.347444
+0.340737 -0.393231
+0.321054 -0.187384
+0.320654 -0.225558
+0.302148 -0.399499
+0.232954 -0.607578
Index: trunk/MagicSoft/Mars/msignal/MExtractFixedWindow.cc
===================================================================
--- trunk/MagicSoft/Mars/msignal/MExtractFixedWindow.cc	(revision 7095)
+++ trunk/MagicSoft/Mars/msignal/MExtractFixedWindow.cc	(revision 7098)
@@ -181,8 +181,10 @@
     case 8:
     case 10:
+    case 12:
       SetResolutionPerPheLoGain(0.011);      
       break;
     default:
       *fLog << warn << GetDescriptor() << ": Could not set the lo-gain extractor resolution/phe for window size " << wslogain << endl;
+      SetResolutionPerPheLoGain(0.011);
     }
 }
Index: trunk/MagicSoft/Mars/msignal/MExtractTimeAndChargeDigitalFilter.cc
===================================================================
--- trunk/MagicSoft/Mars/msignal/MExtractTimeAndChargeDigitalFilter.cc	(revision 7095)
+++ trunk/MagicSoft/Mars/msignal/MExtractTimeAndChargeDigitalFilter.cc	(revision 7098)
@@ -30,5 +30,4 @@
 //   Hendrik has promised to write more documentation
 //
-//
 //   The following variables have to be set by the derived class and 
 //   do not have defaults:
@@ -38,8 +37,15 @@
 //   - fSqrtLoGainSamples
 //
+// The reading of automatic weights files (color, type) can be switched
+// off using EnableAutomaticWeights(kFALSE).
+//
+// An empty name or "-" as the weights file name is a synonym for
+// setting all weights to 1
+//
 // Input Containers:
 //   MRawEvtData
 //   MRawRunHeader
 //   MPedestalCam
+//   [MCalibrationPattern]
 //
 // Output Containers:
@@ -53,12 +59,15 @@
 #include <fstream>
 
-#include <TFile.h>
-#include <TH1F.h>
-#include <TH2F.h>
-#include <TString.h>
+#include <TH1.h>
+#include <TH2.h>
 #include <TMatrix.h>
 
 #include "MLog.h"
 #include "MLogManip.h"
+
+#include "MParList.h"
+
+#include "MRawRunHeader.h"
+#include "MCalibrationPattern.h"
 
 #include "MPedestalPix.h"
@@ -78,5 +87,4 @@
 const Int_t  MExtractTimeAndChargeDigitalFilter::fgSignalStartBinHiGain    =  4;
 const Int_t  MExtractTimeAndChargeDigitalFilter::fgSignalStartBinLoGain    =  4;
-const TString MExtractTimeAndChargeDigitalFilter::fgNameWeightsFile        = "msignal/cosmics_weights.dat";
 const Float_t MExtractTimeAndChargeDigitalFilter::fgOffsetLoGain           =  1.7; // 5 ns
 const Float_t MExtractTimeAndChargeDigitalFilter::fgLoGainStartShift       = -1.8;
@@ -94,5 +102,5 @@
 //
 MExtractTimeAndChargeDigitalFilter::MExtractTimeAndChargeDigitalFilter(const char *name, const char *title) 
-    : fTimeShiftHiGain(0.), fTimeShiftLoGain(0.), fWeightsSet(kFALSE), fRandomIter(0)
+    : fTimeShiftHiGain(0.), fTimeShiftLoGain(0.), fAutomaticWeights(kTRUE), fRandomIter(0)
 {
     fName  = name  ? name  : "MExtractTimeAndChargeDigitalFilter";
@@ -104,5 +112,4 @@
     SetSignalStartBin();
 
-    SetNameWeightsFile();
     SetOffsetLoGain(fgOffsetLoGain);
     SetLoGainStartShift(fgLoGainStartShift);
@@ -184,4 +191,18 @@
 // --------------------------------------------------------------------------
 //
+// Executing MExtractTimeAndCharge::PreProcess and searching for
+// MCalibrationPattern
+//
+Int_t MExtractTimeAndChargeDigitalFilter::PreProcess(MParList *pList)
+{
+    if (!MExtractTimeAndCharge::PreProcess(pList))
+        return kFALSE;
+
+    fCalibPattern = (MCalibrationPattern*)pList->FindObject("MCalibrationPattern");
+    return kTRUE;
+}
+
+// --------------------------------------------------------------------------
+//
 // InitArrays
 //
@@ -190,29 +211,34 @@
 Bool_t MExtractTimeAndChargeDigitalFilter::InitArrays()
 {
-
-  Int_t range = (Int_t)(fHiGainLast - fHiGainFirst + 1 + fHiLoLast);
-
-  fHiGainSignal.Set(range);
-
-  range = (Int_t)(fLoGainLast - fLoGainFirst + 1);
-
-  fLoGainSignal.Set(range);
-
-  if (!fWeightsSet)
-    if (!ReadWeightsFile(fNameWeightsFile))
-      return kFALSE;
-
-  fTimeShiftHiGain = (Float_t)fHiGainFirst + 0.5 + 1./fBinningResolutionHiGain;
-  fTimeShiftLoGain = 0.5 + 1./fBinningResolutionLoGain;
-  // 
-  // We need here the effective number of samples which is about 2.5 in the case of a window
-  // size of 6. The exact numbers have to be found still.
-  //
-  fNumHiGainSamples = (Float_t)fWindowSizeHiGain/2.4;
-  fNumLoGainSamples = (Float_t)fWindowSizeLoGain/2.4;
-  fSqrtHiGainSamples = TMath::Sqrt(fNumHiGainSamples);
-  fSqrtLoGainSamples = TMath::Sqrt(fNumLoGainSamples);
-  
-  return kTRUE;
+    if (!fRunHeader)
+        return kFALSE;
+
+    const Int_t rangehi = (Int_t)(fHiGainLast - fHiGainFirst + 1 + fHiLoLast);
+    const Int_t rangelo = (Int_t)(fLoGainLast - fLoGainFirst + 1);
+
+    fHiGainSignal.Set(rangehi);
+    fLoGainSignal.Set(rangelo);
+
+    // Try to get automatic weights
+    if (!ReadAutomaticWeightsFile())
+        return kFALSE;
+
+    // If still no weights set try conservative way
+    if (fNameWeightsFileSet.IsNull())
+        if (!ReadWeightsFile(fNameWeightsFile))
+            return kFALSE;
+
+    fTimeShiftHiGain = (Float_t)fHiGainFirst + 0.5 + 1./fBinningResolutionHiGain;
+    fTimeShiftLoGain = 0.5 + 1./fBinningResolutionLoGain;
+    //
+    // We need here the effective number of samples which is about 2.5 in the case of a window
+    // size of 6. The exact numbers have to be found still.
+    //
+    fNumHiGainSamples = (Float_t)fWindowSizeHiGain/2.4;
+    fNumLoGainSamples = (Float_t)fWindowSizeLoGain/2.4;
+    fSqrtHiGainSamples = TMath::Sqrt(fNumHiGainSamples);
+    fSqrtLoGainSamples = TMath::Sqrt(fNumLoGainSamples);
+
+    return kTRUE;
 }
 
@@ -556,4 +582,5 @@
 //   MJPedestal.MExtractor.BinningResolutionLoGain: 10
 //   MJPedestal.MExtractor.WeightsFile: filename
+//   MJPedestal.MExtractor.AutomaticWeights: off
 //
 Int_t MExtractTimeAndChargeDigitalFilter::ReadEnv(const TEnv &env, TString prefix, Bool_t print)
@@ -563,5 +590,11 @@
   Byte_t lw = fWindowSizeLoGain;
   Bool_t rc = kFALSE;
-  
+
+  if (IsEnvDefined(env, prefix, "AutomaticWeights", print))
+  {
+      EnableAutomaticWeights(GetEnvValue(env, prefix, "AutomaticWeights", fAutomaticWeights));
+      rc = kTRUE;
+  }
+
   if (IsEnvDefined(env, prefix, "WindowSizeHiGain", print))
     {
@@ -601,7 +634,7 @@
   if (IsEnvDefined(env, prefix, "WeightsFile", print))
     {
-      if (!ReadWeightsFile(GetEnvValue(env, prefix, "WeightsFile", "")))
-        return kERROR;
-      rc = kTRUE;
+        SetNameWeightsFile(GetEnvValue(env, prefix, "WeightsFile", ""));
+        *fLog << all << "**********> " << fNameWeightsFile << endl;
+        rc = kTRUE;
     }
   
@@ -621,6 +654,4 @@
     // This is a fix for TEnv files edited with windows editors
     filename.ReplaceAll("\015", "");
-
-    SetNameWeightsFile(filename);
 
     fAmpWeightsHiGain .Set(fBinningResolutionHiGain*fWindowSizeHiGain);
@@ -629,5 +660,5 @@
     fTimeWeightsLoGain.Set(fBinningResolutionLoGain*fWindowSizeLoGain);
 
-    if (fNameWeightsFile.IsNull())
+    if (fNameWeightsFile.IsNull() || fNameWeightsFile=="-")
     {
         fAmpWeightsHiGain.Reset(1);
@@ -635,6 +666,12 @@
         fAmpWeightsLoGain.Reset(1);
         fTimeWeightsLoGain.Reset(1);
+        fNameWeightsFileSet = "-";
         return kTRUE;
     }
+
+    //filename = MJob::ExpandPath(filename);
+
+    if (fNameWeightsFileSet==filename)
+        return kTRUE;
 
     ifstream fin(filename.Data());
@@ -646,5 +683,5 @@
     }
 
-    *fLog << inf << "Reading weights file " << filename << "..." << flush;
+    *fLog << all << "DigitalFilter: Reading weights in " << filename << "..." << flush;
 
     Int_t len = 0;
@@ -780,7 +817,84 @@
       }
 
-    fWeightsSet = kTRUE;
+    fNameWeightsFileSet = filename;
 
     return kTRUE;
+}
+
+
+//----------------------------------------------------------------------------
+//
+// If automatic weight disabled or run type is pedestal no action is taken
+// and kTRUE is returned.
+// 
+// Otherwise:
+//  - If runtype is kRTData and filename starts with "MC_weights" "MC" is
+//    replaced by "cosmics"
+//  - If runtype is kRTMonteCarlo and filename starts with "cosmics_weights"
+//    "cosmics" is replaced by "MC"
+//  - If name filename begins with "calibration_weights_" for blue and green
+//    "UV" is replaced by "blue"
+//  - If name filename begins with "calibration_weights_" for UV and CT1
+//    "blue" is replaced by "UV"
+//
+// The replacements are done in this order. If no match is found no
+// replacement is done. Afterwards ReadWeightsFile for the resulting
+// path-/filename is called.
+//
+Bool_t MExtractTimeAndChargeDigitalFilter::ReadAutomaticWeightsFile()
+{
+    if (!fAutomaticWeights || fRunHeader->GetRunType()==MRawRunHeader::kRTPedestal)
+        return kTRUE;
+
+    *fLog << dbg << endl;
+    *fLog << underline << GetDescriptor() << endl;
+    *fLog << " Trying to get automatic weight for " << fNameWeightsFile << endl;
+
+    const Ssiz_t pos = fNameWeightsFile.Last('/')+1;
+    const Ssiz_t len = fNameWeightsFile.Length();
+
+    TString path = fNameWeightsFile(0, pos>=0?pos:len);
+    TString name = fNameWeightsFile(pos>=0?pos:0, len);
+
+    if (name.BeginsWith("cosmics_weights") && fRunHeader->GetRunType()==MRawRunHeader::kRTMonteCarlo)
+    {
+        name.Remove(0, 7);
+        name.Prepend("MC");
+        *fLog << "Run type: Monte Carlo data" << endl;
+    }
+    if (name.BeginsWith("MC_weights") && fRunHeader->GetRunType()==MRawRunHeader::kRTData)
+    {
+        name.Remove(0, 2);
+        name.Prepend("cosmics");
+        *fLog << "Run type: Cosmics data" << endl;
+    }
+
+    if (name.BeginsWith("calibration_weights_") && fCalibPattern)
+    {
+        *fLog << " Run type: Calibration with color " << (int)fCalibPattern->GetPulserColor() << ", setting ";
+        switch (fCalibPattern->GetPulserColor())
+        {
+        case MCalibrationCam::kBLUE:  // 2
+        case MCalibrationCam::kGREEN: // 1
+            *fLog << "blue/green" << endl;
+            name.ReplaceAll("UV", "blue");
+            break;
+
+        case MCalibrationCam::kUV:    // 3
+        case MCalibrationCam::kCT1:   // 0
+            *fLog << "UV/CT1" << endl;
+            name.ReplaceAll("blue", "UV");
+            break;
+        case MCalibrationCam::kNONE:
+            *fLog << "None" << endl;
+            break;
+        default: // kNONE=4
+            *fLog << "???" << endl;
+        }
+    }
+
+    path += name;
+
+    return ReadWeightsFile(path);
 }
 
@@ -1065,5 +1179,7 @@
     *fLog << " Window Size HiGain: " << fWindowSizeHiGain        << "  LoGain: " << fWindowSizeLoGain << endl;
     *fLog << " Binning Res HiGain: " << fBinningResolutionHiGain << "  LoGain: " << fBinningResolutionHiGain << endl;
-    *fLog << " Weights File:       " << fNameWeightsFile << endl;
+    *fLog << " Weights File desired: " << (fNameWeightsFile.IsNull()?"-":fNameWeightsFile) << endl;
+    if (!fNameWeightsFileSet.IsNull())
+        *fLog << " Weights File set:     " << fNameWeightsFileSet << endl;
 
     TString opt(o);
