Index: trunk/MagicSoft/Mars/mjobs/MJCalibration.cc
===================================================================
--- trunk/MagicSoft/Mars/mjobs/MJCalibration.cc	(revision 4607)
+++ trunk/MagicSoft/Mars/mjobs/MJCalibration.cc	(revision 4609)
@@ -87,4 +87,5 @@
 #include "MJCalibration.h"
 
+#include <TEnv.h>
 #include <TFile.h>
 #include <TStyle.h>
@@ -96,4 +97,5 @@
 
 #include "MRunIter.h"
+#include "MSequence.h"
 #include "MParList.h"
 #include "MTaskList.h"
@@ -125,4 +127,5 @@
 #include "MRawFileRead.h"
 #include "MGeomApply.h"
+#include "MTaskEnv.h"
 #include "MBadPixelsMerge.h"
 #include "MBadPixelsCam.h"
@@ -158,5 +161,5 @@
 //
 MJCalibration::MJCalibration(const char *name, const char *title) 
-    : fRuns(0), fExtractor(NULL), fTimeExtractor(NULL), 
+    : fEnv(0), fRuns(0), fSequence(0), fExtractor(NULL), fTimeExtractor(NULL),
       fColor(MCalibrationCam::kNONE), fDisplayType(kNormalDisplay),
       fRelTimes(kFALSE), fDataCheck(kFALSE), fDebug(kFALSE)
@@ -171,4 +174,9 @@
 }
 
+MJCalibration::~MJCalibration()
+{
+    if (fEnv)
+        delete fEnv;
+}
 
 // --------------------------------------------------------------------------
@@ -224,5 +232,5 @@
     TString title = fDisplay->GetTitle();
     title += "--  Calibration ";
-    title += fRuns->GetRunsAsString();
+    title += fSequence ? Form("calib%06d", fSequence->GetSequence()) : fRuns->GetRunsAsString();
     title += "  --";
     fDisplay->SetTitle(title);
@@ -234,72 +242,38 @@
 
     // Create histograms to display
-    MHCamera disp1 (geomcam, Form("%s%s","Charge",(fRuns->GetRunsAsFileName()).Data()),
-                    "Fitted Mean Charges");
-    MHCamera disp2 (geomcam, Form("%s%s","SigmaCharge",(fRuns->GetRunsAsFileName()).Data()),
-                    "Sigma of Fitted Charges");
-    MHCamera disp3 (geomcam, Form("%s%s","RSigma",(fRuns->GetRunsAsFileName()).Data()),
-                    "Reduced Sigmas");
-    MHCamera disp4 (geomcam, Form("%s%s","RSigmaPerCharge",(fRuns->GetRunsAsFileName()).Data()),  
-                    "Reduced Sigma per Charge");
-    MHCamera disp5 (geomcam, Form("%s%s","NumPhes",(fRuns->GetRunsAsFileName()).Data()),
-                    "Nr. of Phe's (F-Factor Method)");
-    MHCamera disp6 (geomcam, Form("%s%s","ConvFADC2Phes",(fRuns->GetRunsAsFileName()).Data()),
-                    "Conversion Factor (F-Factor Method)");
-    MHCamera disp7 (geomcam, Form("%s%s","TotalFFactor",(fRuns->GetRunsAsFileName()).Data()),
-                    "Total F-Factor (F-Factor Method)");
-    MHCamera disp8 (geomcam, Form("%s%s","CascadesQEFFactor",(fRuns->GetRunsAsFileName()).Data()),
-                    "Cascades QE (F-Factor Method)");
-    MHCamera disp9 (geomcam, Form("%s%s","CascadesQEBlindPix",(fRuns->GetRunsAsFileName()).Data()),
-                    "Cascades QE (Blind Pixel Method)");
-    MHCamera disp10(geomcam, Form("%s%s","CascadesQEPINDiode",(fRuns->GetRunsAsFileName()).Data()),
-                    "Cascades QE (PIN Diode Method)");
-    MHCamera disp11(geomcam, Form("%s%s","CascadesQECombined",(fRuns->GetRunsAsFileName()).Data()),
-                    "Cascades QE (Combined Method)");
-    MHCamera disp12(geomcam, Form("%s%s","FFactorValid",(fRuns->GetRunsAsFileName()).Data()),
-                    "Pixels with valid F-Factor calibration");
-    MHCamera disp13(geomcam, Form("%s%s","BlindPixelValid",(fRuns->GetRunsAsFileName()).Data()), 
-                    "Pixels with valid BlindPixel calibration");
-    MHCamera disp14(geomcam, Form("%s%s","PINdiodeValid",(fRuns->GetRunsAsFileName()).Data()),
-                    "Pixels with valid PINDiode calibration");
-    MHCamera disp15(geomcam, Form("%s%s","CombinedValid",(fRuns->GetRunsAsFileName()).Data()),
-                    "Pixels with valid Combined calibration");
-    MHCamera disp16(geomcam, Form("%s%s","Saturation",(fRuns->GetRunsAsFileName()).Data()),
-                    "Pixels with saturated Hi Gain");
-    MHCamera disp17(geomcam, Form("%s%s","ConversionMeans",(fRuns->GetRunsAsFileName()).Data()), 
-                    "Conversion HiGain.vs.LoGain Means");
-    MHCamera disp18(geomcam, Form("%s%s","ConversionSigmas",(fRuns->GetRunsAsFileName()).Data()), 
-                    "Conversion HiGain.vs.LoGain Sigmas");
-    MHCamera disp19(geomcam, Form("%s%s","HiGainPickup",(fRuns->GetRunsAsFileName()).Data()),
-                    "Number Pickup events Hi Gain");
-    MHCamera disp20(geomcam, Form("%s%s","LoGainPickup",(fRuns->GetRunsAsFileName()).Data()),
-                    "Number Pickup events Lo Gain");
-    MHCamera disp21(geomcam, Form("%s%s","HiGainBlackout",(fRuns->GetRunsAsFileName()).Data()),
-                    "Number Blackout events Hi Gain");
-    MHCamera disp22(geomcam, Form("%s%s","LoGainBlackout",(fRuns->GetRunsAsFileName()).Data()), 
-                    "Number Blackout events Lo Gain");
-    MHCamera disp23(geomcam, Form("%s%s","Excluded",(fRuns->GetRunsAsFileName()).Data()),
-                    "Pixels previously excluded");
-    MHCamera disp24(geomcam, Form("%s%s","UnSuitable",(fRuns->GetRunsAsFileName()).Data()),
-                    "Pixels not suited for further analysis");
-    MHCamera disp25(geomcam, Form("%s%s","UnReliable",(fRuns->GetRunsAsFileName()).Data()),
-                    "Pixels not reliable for further analysis");
-    MHCamera disp26(geomcam, Form("%s%s","HiGainOscillating",(fRuns->GetRunsAsFileName()).Data()),
-                    "Oscillating Pixels High Gain");
-    MHCamera disp27(geomcam, Form("%s%s","LoGainOscillating",(fRuns->GetRunsAsFileName()).Data()),
-                    "Oscillating Pixels Low Gain");
-    MHCamera disp28(geomcam, Form("%s%s","AbsTimeMean",(fRuns->GetRunsAsFileName()).Data()),
-                    "Abs. Arrival Times");
-    MHCamera disp29(geomcam, Form("%s%s","AbsTimeRms",(fRuns->GetRunsAsFileName()).Data()),
-                    "RMS of Arrival Times");
-    MHCamera disp30(geomcam, Form("%s%s","MeanTime",(fRuns->GetRunsAsFileName()).Data()),
-                    "Mean Rel. Arrival Times");
-    MHCamera disp31(geomcam, Form("%s%s","SigmaTime",(fRuns->GetRunsAsFileName()).Data()),
-                    "Sigma Rel. Arrival Times");
-    MHCamera disp32(geomcam, Form("%s%s","TimeProb",(fRuns->GetRunsAsFileName()).Data()),
-                    "Probability of Time Fit");
-    MHCamera disp33(geomcam, Form("%s%s","TimeNotFitValid",(fRuns->GetRunsAsFileName()).Data()),
-                    "Pixels with not valid fit results");
-    MHCamera disp34(geomcam, Form("%s%s","TimeOscillating",(fRuns->GetRunsAsFileName()).Data()),
-                    "Oscillating Pixels");
+    MHCamera disp1 (geomcam, "Charge",            "Fitted Mean Charges");
+    MHCamera disp2 (geomcam, "SigmaCharge",       "Sigma of Fitted Charges");
+    MHCamera disp3 (geomcam, "RSigma",            "Reduced Sigmas");
+    MHCamera disp4 (geomcam, "RSigmaPerCharge",   "Reduced Sigma per Charge");
+    MHCamera disp5 (geomcam, "NumPhes",           "Nr. of Phe's (F-Factor Method)");
+    MHCamera disp6 (geomcam, "ConvFADC2Phes",     "Conversion Factor (F-Factor Method)");
+    MHCamera disp7 (geomcam, "TotalFFactor",      "Total F-Factor (F-Factor Method)");
+    MHCamera disp8 (geomcam, "CascadesQEFFactor", "Cascades QE (F-Factor Method)");
+    MHCamera disp9 (geomcam, "CascadesQEBlindPix","Cascades QE (Blind Pixel Method)");
+    MHCamera disp10(geomcam, "CascadesQEPINDiode","Cascades QE (PIN Diode Method)");
+    MHCamera disp11(geomcam, "CascadesQECombined","Cascades QE (Combined Method)");
+    MHCamera disp12(geomcam, "FFactorValid",      "Pixels with valid F-Factor calibration");
+    MHCamera disp13(geomcam, "BlindPixelValid",   "Pixels with valid BlindPixel calibration");
+    MHCamera disp14(geomcam, "PINdiodeValid",     "Pixels with valid PINDiode calibration");
+    MHCamera disp15(geomcam, "CombinedValid",     "Pixels with valid Combined calibration");
+    MHCamera disp16(geomcam, "Saturation",        "Pixels with saturated Hi Gain");
+    MHCamera disp17(geomcam, "ConversionMeans",   "Conversion HiGain.vs.LoGain Means");
+    MHCamera disp18(geomcam, "ConversionSigmas",  "Conversion HiGain.vs.LoGain Sigmas");
+    MHCamera disp19(geomcam, "HiGainPickup",      "Number Pickup events Hi Gain");
+    MHCamera disp20(geomcam, "LoGainPickup",      "Number Pickup events Lo Gain");
+    MHCamera disp21(geomcam, "HiGainBlackout",    "Number Blackout events Hi Gain");
+    MHCamera disp22(geomcam, "LoGainBlackout",    "Number Blackout events Lo Gain");
+    MHCamera disp23(geomcam, "Excluded",          "Pixels previously excluded");
+    MHCamera disp24(geomcam, "UnSuitable",        "Pixels not suited for further analysis");
+    MHCamera disp25(geomcam, "UnReliable",        "Pixels not reliable for further analysis");
+    MHCamera disp26(geomcam, "HiGainOscillating", "Oscillating Pixels High Gain");
+    MHCamera disp27(geomcam, "LoGainOscillating", "Oscillating Pixels Low Gain");
+    MHCamera disp28(geomcam, "AbsTimeMean",       "Abs. Arrival Times");
+    MHCamera disp29(geomcam, "AbsTimeRms",        "RMS of Arrival Times");
+    MHCamera disp30(geomcam, "MeanTime",          "Mean Rel. Arrival Times");
+    MHCamera disp31(geomcam, "SigmaTime",         "Sigma Rel. Arrival Times");
+    MHCamera disp32(geomcam, "TimeProb",          "Probability of Time Fit");
+    MHCamera disp33(geomcam, "TimeNotFitValid",   "Pixels with not valid fit results");
+    MHCamera disp34(geomcam, "TimeOscillating",   "Oscillating Pixels");
 
     // Fitted charge means and sigmas
@@ -665,6 +639,11 @@
 Bool_t MJCalibration::FindColor() 
 {
-
-  const UInt_t nruns = fRuns->GetNumRuns();
+    if (fSequence)
+    {
+        fColor = MCalibrationCam::kCT1;
+        return kTRUE;
+    }
+
+const UInt_t nruns = fRuns->GetNumRuns();
 
   if (nruns == 0)
@@ -880,9 +859,7 @@
     }
 
-  return kTRUE;
-}
-
-
-
+
+    return kTRUE;
+}
 
 // --------------------------------------------------------------------------
@@ -892,26 +869,41 @@
 const char* MJCalibration::GetOutputFile() const
 {
-
-  if (!fRuns)
-    return "";
-  
-  return Form("%s/%s-F1.root", (const char*)fOutputPath, (const char*)fRuns->GetRunsAsFileName());
-}
-
+    if (fSequence)
+        return Form("%s/calib%06d.root", (const char*)fOutputPath, fSequence->GetSequence());
+    if (!fRuns)
+        return "";
+
+    return Form("%s/%s-F1.root", (const char*)fOutputPath, (const char*)fRuns->GetRunsAsFileName());
+}
 
 Bool_t MJCalibration::IsUseBlindPixel() const 
 {
-  return TESTBIT(fDevices,kUseBlindPixel);
-}
-
+    return TESTBIT(fDevices,kUseBlindPixel);
+}
 
 Bool_t MJCalibration::IsUsePINDiode() const 
 {
-  return TESTBIT(fDevices,kUsePINDiode);
-}
-
-  
-
-
+    return TESTBIT(fDevices,kUsePINDiode);
+}
+
+void MJCalibration::SetEnv(const char *env)
+{
+    if (fEnv)
+        delete fEnv;
+    fEnv = new TEnv(env);
+}
+
+void MJCalibration::CheckEnv()
+{
+    if (!fEnv)
+        return;
+
+    TString e1 = fEnv->GetValue("MJCalibration.OutputPath", "");
+    if (!e1.IsNull())
+    {
+        e1.ReplaceAll("\015", "");
+        SetOutputPath(e1);
+    }
+}
 
 // --------------------------------------------------------------------------
@@ -925,4 +917,40 @@
 
     return kTRUE;
+}
+
+void MJCalibration::InitBlindPixel(MExtractBlindPixel &blindext,
+                                   MHCalibrationChargeBlindCam &blindcam)
+{
+    Int_t run = fSequence ? fSequence->GetLastRun() : fRuns->GetRuns()[fRuns->GetNumRuns()-1];
+
+    //
+    // Initialize the blind pixel. Unfortunately, there is a hardware difference
+    // in the first blind pixel until run "gkSecondBlindPixelInstallation" and the
+    // later setup. The first needs to use a filter because of the length of
+    // spurious NSB photon signals. The latter get better along extracting the amplitude
+    // from a small window.
+    //
+    if (run < gkSecondBlindPixelInstallation)
+    {
+        blindext.SetModified(kFALSE);
+        blindext.SetExtractionType(MExtractBlindPixel::kIntegral);
+        blindext.SetExtractionType(MExtractBlindPixel::kFilter);
+        blindext.SetRange(10,19,0,6);
+        blindext.SetNSBFilterLimit(70);
+        blindcam.SetFitFunc( MHCalibrationChargeBlindPix::kEPoisson5 );
+    }
+    else
+    {
+        blindext.SetModified(kTRUE);
+        blindext.SetExtractionType(MExtractBlindPixel::kAmplitude);
+        blindext.SetExtractionType(MExtractBlindPixel::kFilter);
+        blindext.SetRange(5,8,0,2);
+        blindext.SetNSBFilterLimit(38);
+
+        if (run < gkThirdBlindPixelInstallation)
+            blindext.SetNumBlindPixels(2);
+        else
+            blindext.SetNumBlindPixels(3);
+    }
 }
 
@@ -965,243 +993,211 @@
 Bool_t MJCalibration::ProcessFile(MPedestalCam &pedcam)
 {
-  if (!fRuns)
-    {
-      *fLog << err << "No Runs choosen... abort." << endl;
-      return kFALSE;
-    }
+    if (!fRuns && !fSequence)
+    {
+        *fLog << err << "No Runs choosen... abort." << endl;
+        return kFALSE;
+    }
+
+    if (!fSequence && fRuns->GetNumRuns() != fRuns->GetNumEntries())
+    {
+        *fLog << err << "Number of files found doesn't match number of runs... abort."
+            << fRuns->GetNumRuns() << " vs. " << fRuns->GetNumEntries() << endl;
+        return kFALSE;
+    }
+
+    *fLog << inf;
+    fLog->Separator(GetDescriptor());
+
+    if (!FindColor())
+        return kFALSE;
+
+    *fLog << "Calculate MCalibrationCam from ";
+    if (fSequence)
+        *fLog << "Sequence #" << fSequence->GetSequence() << endl;
+    else
+        *fLog << "Runs " << fRuns->GetRunsAsString() << endl;
+    *fLog << endl;
+
+    // Setup Tasklist
+    MParList plist;
+    MTaskList tlist;
+    plist.AddToList(&tlist);
+
+    MReadMarsFile read("Events");
+    MRawFileRead rawread(NULL);
+
+    MDirIter iter;
+    if (fSequence)
+        fSequence->SetupCalRuns(iter);
+
+    if (fDataCheck)
+    {
+        rawread.AddFiles(fSequence ? iter : *fRuns);
+        tlist.AddToList(&rawread);
+    }
+    else
+    {
+        read.DisableAutoScheme();
+        static_cast<MRead&>(read).AddFiles(fSequence ? iter : *fRuns);
+        tlist.AddToList(&read);
+    }
+
+    MHCalibrationChargeCam      chargecam;
+    MHCalibrationChargeBlindCam blindcam;
+
+    plist.AddToList(&pedcam);
+    plist.AddToList(&chargecam);
+    plist.AddToList(&blindcam);
+    plist.AddToList(&fBadPixels);
+    plist.AddToList(&fQECam);
+    plist.AddToList(&fCalibrationCam);
+    plist.AddToList(&fCalibrationBlindCam);
+    plist.AddToList(&fCalibrationPINDiode);
+    plist.AddToList(&fRelTimeCam);
+
+    MGeomApply               apply;
+    MBadPixelsMerge          merge(&fBadPixels);
+    MExtractPINDiode         pinext;
+    MExtractBlindPixel       blindext;
+    InitBlindPixel(blindext, blindcam);
+    MExtractSlidingWindow    extract2;
+    MExtractTimeFastSpline   timespline;
+    MCalibrationChargeCalc   calcalc;
+    if (!fSequence)
+    {
+        calcalc.SetOutputPath(fOutputPath);
+        calcalc.SetOutputFile(Form("%s-ChargeCalibStat.txt",(const char*)fRuns->GetRunsAsFileName()));
+    }
+
+    if (fDebug)
+    {
+        chargecam.SetDebug();
+        calcalc.SetDebug();
+    }
+
+    //
+    // As long as there are no DM's, have to colour by hand
+    //
+    calcalc.SetPulserColor(fColor);
+
+    MFillH fillpin("MHCalibrationChargePINDiode", "MExtractedSignalPINDiode");
+    MFillH fillbnd("MHCalibrationChargeBlindPix", "MExtractedSignalBlindPixel");
+    MFillH fillcam("MHCalibrationChargeCam",      "MExtractedSignalCam");
+    MFillH filltme("MHCalibrationRelTimeCam",     "MArrivalTimeCam");
+    fillpin.SetNameTab("PINDiode");
+    fillbnd.SetNameTab("BlindPix");
+    fillcam.SetNameTab("Charge");
+    filltme.SetNameTab("RelTimes");
   
-  if (fRuns->GetNumRuns() != fRuns->GetNumEntries())
-    {
-      *fLog << err << "Number of files found doesn't match number of runs... abort." 
-            << fRuns->GetNumRuns() << " vs. " << fRuns->GetNumEntries() << endl;
-      return kFALSE;
-    }
-
-  *fLog << inf;
-  fLog->Separator(GetDescriptor());
-
-  if (!FindColor())
-    return kFALSE;
-  
-  *fLog << "Calculate MCalibrationCam from Runs " << fRuns->GetRunsAsString() << endl;
-  *fLog << endl;
-  
-  // Setup Tasklist
-  MParList plist;
-  MTaskList tlist;
-  plist.AddToList(&tlist);
-  
-  MReadMarsFile read("Events");
-  MRawFileRead rawread(NULL);
-
-  if (fDataCheck)
-  {
-     rawread.AddFiles(*fRuns); 
-     tlist.AddToList(&rawread);
-  }
-  else
-  {
-      read.DisableAutoScheme();
-      static_cast<MRead&>(read).AddFiles(*fRuns);
-      tlist.AddToList(&read);
-  }
-
-  MHCalibrationChargeCam      chargecam;
-  MHCalibrationChargeBlindCam blindcam;
-
-  plist.AddToList(&pedcam);
-  plist.AddToList(&chargecam);  
-  plist.AddToList(&blindcam);  
-  plist.AddToList(&fBadPixels);
-  plist.AddToList(&fQECam);
-  plist.AddToList(&fCalibrationCam);
-  plist.AddToList(&fCalibrationBlindCam);
-  plist.AddToList(&fCalibrationPINDiode);
-  plist.AddToList(&fRelTimeCam);
-
-  MGeomApply               apply;
-  MExtractPINDiode         pinext;
-  MExtractBlindPixel       blindext;
-  
-  //
-  // Initialize the blind pixel. Unfortunately, there is a hardware difference 
-  // in the first blind pixel until run "gkSecondBlindPixelInstallation" and the 
-  // later setup. The first needs to use a filter because of the length of 
-  // spurious NSB photon signals. The latter get better along extracting the amplitude
-  // from a small window.
-  //
-  TArrayI arr = fRuns->GetRuns();
-  if (arr[fRuns->GetNumRuns()-1] < gkSecondBlindPixelInstallation)
-    {
-      blindext.SetModified(kFALSE);
-      blindext.SetExtractionType(MExtractBlindPixel::kIntegral);
-      blindext.SetExtractionType(MExtractBlindPixel::kFilter);
-      blindext.SetRange(10,19,0,6);
-      blindext.SetNSBFilterLimit(70);
-      blindcam.SetFitFunc( MHCalibrationChargeBlindPix::kEPoisson5 );
-    }
-  else
-    {
-      blindext.SetModified(kTRUE);
-      blindext.SetExtractionType(MExtractBlindPixel::kAmplitude);
-      blindext.SetExtractionType(MExtractBlindPixel::kFilter);
-      blindext.SetRange(5,8,0,2);
-      blindext.SetNSBFilterLimit(38);
-      if (arr[fRuns->GetNumRuns()-1] < gkThirdBlindPixelInstallation)
-        blindext.SetNumBlindPixels(2);
-      else
-        blindext.SetNumBlindPixels(3);
-    }
-  
-  MExtractSlidingWindow    extract2;
-  MExtractTimeFastSpline   timespline;
-  MCalibrationChargeCalc   calcalc;
-  calcalc.SetOutputPath(fOutputPath);
-  calcalc.SetOutputFile(Form("%s-ChargeCalibStat.txt",(const char*)fRuns->GetRunsAsFileName()));
-
-  if (fDebug)
-    {
-      chargecam.SetDebug();
-      calcalc.SetDebug();      
-    }
-
-  MCalibrationRelTimeCalc  timecalc;
-  timecalc.SetOutputPath(fOutputPath);
-  timecalc.SetOutputFile(Form("%s-TimeCalibStat.txt",(const char*)fRuns->GetRunsAsFileName()));
-  
-  // 
-  // As long as there are no DM's, have to colour by hand 
-  //
-  calcalc.SetPulserColor(fColor);
-  
-  MFillH fillpin("MHCalibrationChargePINDiode", "MExtractedSignalPINDiode");
-  MFillH fillbnd("MHCalibrationChargeBlindCam", "MExtractedSignalBlindPixel");
-  MFillH fillcam("MHCalibrationChargeCam",      "MExtractedSignalCam");
-  MFillH filltme("MHCalibrationRelTimeCam",     "MArrivalTimeCam");
-  fillpin.SetNameTab("PINDiode");
-  fillbnd.SetNameTab("BlindPix");
-  fillcam.SetNameTab("Charge");
-  filltme.SetNameTab("RelTimes");
-
-  TString drawoption;
-
-  if (fDisplayType == kDataCheckDisplay)
-    drawoption += "datacheck";
-  if (fDisplayType == kFullDisplay)
-    drawoption += " all";
-  
-  fillcam.SetDrawOption(drawoption.Data());
-  fillbnd.SetDrawOption(drawoption.Data());
-  fillpin.SetDrawOption(drawoption.Data());
-  filltme.SetDrawOption(drawoption.Data());
-  // 
-  // Apply a filter against cosmics
-  // (will have to be needed in the future
-  // when the calibration hardware-trigger is working)
-  // 
-  MFCosmics cosmics;
-  MContinue cont(&cosmics);
-  
-  //    tlist.AddToList(&merge);
-  tlist.AddToList(&apply);
-
-  if (fExtractor)
-    tlist.AddToList(fExtractor);
-  else
-    {
-      *fLog << warn << GetDescriptor() 
-            << ": No extractor has been chosen, take default MExtractSlidingWindow " << endl;
-      tlist.AddToList(&extract2);
-    }
-  
-  tlist.AddToList(&pinext);  
-  tlist.AddToList(&blindext);
-  
-  if (fRelTimes)
-    {
-      if (fTimeExtractor)
-        tlist.AddToList(fTimeExtractor);
-      else
+    TString drawoption;
+
+    if (fDisplayType == kDataCheckDisplay)
+        drawoption += "datacheck";
+    if (fDisplayType == kFullDisplay)
+        drawoption += " all";
+
+    fillcam.SetDrawOption(drawoption.Data());
+    fillbnd.SetDrawOption(drawoption.Data());
+    fillpin.SetDrawOption(drawoption.Data());
+    filltme.SetDrawOption(drawoption.Data());
+
+    //
+    // Apply a filter against cosmics
+    // (will have to be needed in the future
+    // when the calibration hardware-trigger is working)
+    //
+    MFCosmics cosmics;
+    MContinue cont(&cosmics);
+
+    tlist.AddToList(&merge);
+    tlist.AddToList(&apply);
+
+    MTaskEnv taskenv("ExtractSignal");
+    taskenv.SetDefault(fExtractor ? fExtractor : &extract2);
+
+    tlist.AddToList(&taskenv);
+    tlist.AddToList(&pinext);
+    tlist.AddToList(&blindext);
+
+    MTaskEnv taskenv2("ExtractTime");
+    taskenv2.SetDefault(fTimeExtractor ? fTimeExtractor : &timespline);
+
+    if (fRelTimes)
+        tlist.AddToList(&taskenv2);
+
+    if (fColor == MCalibrationCam::kCT1)
+        tlist.AddToList(&cont);
+
+    tlist.AddToList(&fillcam);
+    tlist.AddToList(&calcalc);
+
+    MCalibrationRelTimeCalc timecalc;
+    if (fRelTimes)
+    {
+        tlist.AddToList(&filltme);
+        tlist.AddToList(&timecalc);
+    }
+
+
+    // Create and setup the eventloop
+    MEvtLoop evtloop(fName);
+    evtloop.SetParList(&plist);
+    evtloop.SetDisplay(fDisplay);
+    evtloop.SetLogStream(fLog);
+    if (fEnv)
+        evtloop.ReadEnv(*fEnv);
+
+    // Execute first analysis
+    if (!evtloop.Eventloop())
+    {
+        *fLog << err << GetDescriptor() << ": Failed." << endl;
+        return kFALSE;
+    }
+
+    tlist.PrintStatistics();
+
+    //
+    // The next lines are necessary in order to avoid that
+    // the last entry drawn by MFillH gets deleted again from
+    // the display. No idea where this comes from...
+    //
+    /*
+    if (fDisplay)
+    {
+        if (IsUsePINDiode())
         {
-          *fLog << warn << GetDescriptor() 
-                << ": No extractor has been chosen, take default MTimeExtractSpline " << endl;
-          tlist.AddToList(&timespline);
+            MHCalibrationChargePINDiode *pin =
+                (MHCalibrationChargePINDiode*)plist.FindObject("MHCalibrationChargePINDiode");
+            pin->DrawClone(Form("nonew %s",drawoption.Data()));
         }
-    }
-
-  if (fColor == MCalibrationCam::kCT1)
-    tlist.AddToList(&cont);
-
-  tlist.AddToList(&fillcam);
-
-  if (fRelTimes)
-    {
-      tlist.AddToList(&filltme);
-      tlist.AddToList(&timecalc);
-    }
-  
-  if (IsUseBlindPixel())
-    tlist.AddToList(&fillbnd);
-  if (IsUsePINDiode())
-    tlist.AddToList(&fillpin);
-
-  tlist.AddToList(&calcalc);
-
-  // Create and setup the eventloop
-  MEvtLoop evtloop(fName);
-  evtloop.SetParList(&plist);
-  evtloop.SetDisplay(fDisplay);
-  evtloop.SetLogStream(fLog);
-  
-  // Execute first analysis
-  if (!evtloop.Eventloop())
-    {
-      *fLog << err << GetDescriptor() << ": Failed." << endl;
-      return kFALSE;
-    }
-  
-  tlist.PrintStatistics();
-
-  //
-  // The next lines are necessary in order to avoid that 
-  // the last entry drawn by MFillH gets deleted again from 
-  // the display. No idea where this comes from...
-  //
-  if (fDisplay)
-    {
-      if (IsUsePINDiode())
+        else if (IsUseBlindPixel())
         {
-          MHCalibrationChargePINDiode *pin = 
-            (MHCalibrationChargePINDiode*)plist.FindObject("MHCalibrationChargePINDiode");
-          pin->DrawClone(Form("nonew %s",drawoption.Data()));
+            MHCalibrationChargeBlindCam *cam =
+                (MHCalibrationChargeBlindCam*)plist.FindObject("MHCalibrationChargeBlindCam");
+            cam->DrawClone(Form("nonew %s",drawoption.Data()));
         }
-      else if (IsUseBlindPixel())
+        else if (fRelTimes)
         {
-          MHCalibrationChargeBlindCam *cam = 
-            (MHCalibrationChargeBlindCam*)plist.FindObject("MHCalibrationChargeBlindCam");
-          cam->DrawClone(Form("nonew %s",drawoption.Data()));
+            MHCalibrationRelTimeCam *cam =
+                (MHCalibrationRelTimeCam*)plist.FindObject("MHCalibrationRelTimeCam");
+            cam->DrawClone(Form("nonew %s",drawoption.Data()));
         }
-      else if (fRelTimes)
+        else
         {
-          MHCalibrationRelTimeCam *cam = 
-            (MHCalibrationRelTimeCam*)plist.FindObject("MHCalibrationRelTimeCam");
-          cam->DrawClone(Form("nonew %s",drawoption.Data()));
+            MHCalibrationChargeCam *cam =
+                (MHCalibrationChargeCam*)plist.FindObject("MHCalibrationChargeCam");
+            cam->DrawClone(Form("nonew %s",drawoption.Data()));
         }
-      else
-        {
-          MHCalibrationChargeCam *cam = 
-            (MHCalibrationChargeCam*)plist.FindObject("MHCalibrationChargeCam");
-          cam->DrawClone(Form("nonew %s",drawoption.Data()));
-        }
-    }
-  
-  DisplayResult(plist);
-
-  if (!WriteResult())
-    return kFALSE;
-  
-  *fLog << inf << GetDescriptor() << ": Done." << endl;
-  
-  return kTRUE;
+    }
+    */
+
+    DisplayResult(plist);
+
+    if (!WriteResult())
+        return kFALSE;
+
+    *fLog << inf << GetDescriptor() << ": Done." << endl;
+
+    return kTRUE;
 }
 
Index: trunk/MagicSoft/Mars/mjobs/MJCalibration.h
===================================================================
--- trunk/MagicSoft/Mars/mjobs/MJCalibration.h	(revision 4607)
+++ trunk/MagicSoft/Mars/mjobs/MJCalibration.h	(revision 4609)
@@ -21,95 +21,118 @@
 #endif
 
+class TEnv;
 class MRunIter;
+class MSequence;
 class MParList;
 class MPedestalCam;
 class MExtractor;
 class MExtractTime;
+
+class MExtractBlindPixel;
+class MHCalibrationChargeBlindCam;
+
 class MJCalibration : public MParContainer
 {
 private:
+    static const Int_t gkIFAEBoxInaugurationRun;         // Run number of first IFAE box calibration
+    static const Int_t gkSecondBlindPixelInstallation;   // Run number upon which second blind pixel was installed
+    static const Int_t gkThirdBlindPixelInstallation;    // Run number upon which third blind pixel was installed
 
-  static const Int_t gkIFAEBoxInaugurationRun;          // Run number of first IFAE box calibration
-  static const Int_t gkSecondBlindPixelInstallation;    // Run number upon which second blind pixel was installed
-  static const Int_t gkThirdBlindPixelInstallation;     // Run number upon which third blind pixel was installed  
+    TString fOutputPath;                                 // Path to the output files
 
-  TString fOutputPath;                                 // Path to the output files
+    TEnv           *fEnv;
+    MRunIter       *fRuns;                               // Calibration files
+    MSequence      *fSequence;                           // Sequence
+
+    MExtractor     *fExtractor;                          // Signal extractor
+    MExtractTime   *fTimeExtractor;                      // Arrival Time extractor
+
+    MBadPixelsCam              fBadPixels;               // Bad Pixels cam, can be set from previous runs
+    MCalibrationChargeCam      fCalibrationCam;          // Calibration conversion factors FADC2Phe
+    MCalibrationChargeBlindCam fCalibrationBlindCam;     // Calibration from Blind Pixel(s)
+    MCalibrationChargePINDiode fCalibrationPINDiode;     // Calibration from PIN Diode
+    MCalibrationQECam          fQECam;                   // Quantum efficiency, can be set from previous runs
+    MCalibrationRelTimeCam     fRelTimeCam;              // Calibration constants rel. times
+
+    MCalibrationCam::PulserColor_t fColor;               // Colour of the pulsed LEDs
+
+    enum  Display_t                                      // Possible Display types
+    {
+        kFullDisplay,
+        kDataCheckDisplay,
+        kNormalDisplay
+    };
+
+    Display_t fDisplayType;                              // Chosen Display type
+
+    enum  Device_t                                       // Possible devices for calibration
+    {
+        kUseBlindPixel,
+        kUsePINDiode
+    }; 
+
+    Byte_t fDevices;                                     // Bit-field for used devices for calibration
+
+    Bool_t fRelTimes;                                    // Flag if relative times have to be calibrated
+    Bool_t fDataCheck;                                   // Flag if the data check is run on raw data
+    Bool_t fDebug;
+
+    void   DisplayResult(MParList &plist);
+    Bool_t WriteResult();
+    void   CheckEnv();
+
+    // WORKAROUNDS!!!
+    Bool_t FindColor();
+    void   InitBlindPixel(MExtractBlindPixel &blindext,
+                          MHCalibrationChargeBlindCam &blindcam);
+
+public:
+    MJCalibration(const char *name=NULL, const char *title=NULL);
+    ~MJCalibration();
   
-  MRunIter       *fRuns;                               // Calibration files
-  MExtractor     *fExtractor;                          // Signal extractor
-  MExtractTime   *fTimeExtractor;                      // Arrival Time extractor
+    const char* GetOutputFile() const;
+    void SetEnv(const char *env);
+
+    MCalibrationChargeCam  &GetCalibrationCam() { return fCalibrationCam; }
+    MCalibrationRelTimeCam &GetRelTimeCam()     { return fRelTimeCam;     }
+    MCalibrationQECam      &GetQECam()          { return fQECam;          }
+    MBadPixelsCam          &GetBadPixels()      { return fBadPixels;      }
+
+    Bool_t IsUseBlindPixel() const;
+    Bool_t IsUsePINDiode()   const;
+
+    void SetBadPixels(const MBadPixelsCam &bad)    { bad.Copy(fBadPixels);   }
+    void SetExtractor(MExtractor* ext)             { fExtractor = ext; }
+    void SetTimeExtractor(MExtractTime* ext)       { fTimeExtractor = ext; }
+    void SetQECam(const MCalibrationQECam &qe)     { qe.Copy(fQECam);        }
+    void SetColor(const MCalibrationCam::PulserColor_t color) { fColor = color; }
+
+    void SetInput(MRunIter *iter) { fRuns=iter; }
+    void SetSequence(MSequence *seq) { fSequence=seq; }
+    void SetOutputPath(const char *path=".");
+
+    // Displays
+    void SetFullDisplay()      { fDisplayType = kFullDisplay;      }
+    void SetDataCheckDisplay() { fDisplayType = kDataCheckDisplay; }
+    void SetNormalDisplay()    { fDisplayType = kNormalDisplay;    }
+
+    // Rel. Time
+    void SetRelTimeCalibration(const Bool_t b=kTRUE) { fRelTimes = b; }
+
+    // Data Check
+    void SetDataCheck(const Bool_t b=kTRUE) { fDataCheck = b; SetDataCheckDisplay(); }
+
+    // Debug
+    void SetDebug(const Bool_t b=kTRUE) { fDebug = b; }
+
+    // Devices
+    void SetUseBlindPixel(const Bool_t b=kTRUE);
+    void SetUsePINDiode(const Bool_t b=kTRUE);
   
-  MBadPixelsCam              fBadPixels;               // Bad Pixels cam, can be set from previous runs
-  MCalibrationChargeCam      fCalibrationCam;          // Calibration conversion factors FADC2Phe
-  MCalibrationChargeBlindCam fCalibrationBlindCam;     // Calibration from Blind Pixel(s)
-  MCalibrationChargePINDiode fCalibrationPINDiode;     // Calibration from PIN Diode
-  MCalibrationQECam          fQECam;                   // Quantum efficiency, can be set from previous runs
-  MCalibrationRelTimeCam     fRelTimeCam;              // Calibration constants rel. times
+    Bool_t ReadCalibrationCam();
+    Bool_t ProcessFile(MPedestalCam &pedcam);
+    Bool_t Process(MPedestalCam &pedcam);
 
-  MCalibrationCam::PulserColor_t fColor;               // Colour of the pulsed LEDs
-
-  enum  Display_t   { kFullDisplay, kDataCheckDisplay, kNormalDisplay }; // Possible Display types
-  
-  Display_t fDisplayType;                              // Chosen Display type
-
-  enum  Device_t    { kUseBlindPixel, kUsePINDiode  }; // Possible devices for calibration
-
-  Byte_t fDevices;                                     // Bit-field for used devices for calibration
-  
-  Bool_t fRelTimes;                                    // Flag if relative times have to be calibrated
-  Bool_t fDataCheck;                                   // Flag if the data check is run on raw data
-  Bool_t fDebug;
-
-  void   DisplayResult(MParList &plist);
-  Bool_t WriteResult();
-  Bool_t FindColor();
-  
-public:
-
-  MJCalibration(const char *name=NULL, const char *title=NULL);
-  ~MJCalibration() {}
-  
-  const char* GetOutputFile() const;
-
-  MCalibrationChargeCam  &GetCalibrationCam()     { return fCalibrationCam; }  
-  MCalibrationRelTimeCam &GetRelTimeCam()         { return fRelTimeCam;     }
-  MCalibrationQECam      &GetQECam()              { return fQECam;          }    
-  MBadPixelsCam          &GetBadPixels()          { return fBadPixels;      }
-
-  Bool_t IsUseBlindPixel() const;
-  Bool_t IsUsePINDiode()   const;
-  
-  void SetBadPixels(const MBadPixelsCam &bad)     { bad.Copy(fBadPixels);   }
-  void SetExtractor(MExtractor* ext)              { fExtractor = ext; }
-  void SetTimeExtractor(MExtractTime* ext)       { fTimeExtractor = ext; }
-  void SetQECam    (const MCalibrationQECam &qe) { qe.Copy(fQECam);        }    
-  void SetColor    (const MCalibrationCam::PulserColor_t color) { fColor = color; }
-
-  void SetInput(MRunIter *iter) { fRuns=iter; }
-  void SetOutputPath(const char *path=".");
-  
-  // Displays
-  void SetFullDisplay()      { fDisplayType = kFullDisplay;      }
-  void SetDataCheckDisplay() { fDisplayType = kDataCheckDisplay; }
-  void SetNormalDisplay()    { fDisplayType = kNormalDisplay;    }
-  
-  // Rel. Time
-  void SetRelTimeCalibration(const Bool_t b=kTRUE) { fRelTimes         = b; }
-
-  // Data Check
-  void SetDataCheck         (const Bool_t b=kTRUE) { fDataCheck        = b; SetDataCheckDisplay(); }
-
-  // Debug
-  void SetDebug             (const Bool_t b=kTRUE) { fDebug           = b; }
-  
-  // Devices
-  void SetUseBlindPixel( const Bool_t b=kTRUE );
-  void SetUsePINDiode  ( const Bool_t b=kTRUE );  
-  
-  Bool_t ReadCalibrationCam();
-  Bool_t ProcessFile( MPedestalCam &pedcam );
-  Bool_t Process    ( MPedestalCam &pedcam );
-  
-  ClassDef(MJCalibration, 0) // Tool to run a calibration per pulser colour and intensity
+    ClassDef(MJCalibration, 0) // Tool to run a calibration per pulser colour and intensity
 };
 
Index: trunk/MagicSoft/Mars/mjobs/MJPedestal.cc
===================================================================
--- trunk/MagicSoft/Mars/mjobs/MJPedestal.cc	(revision 4607)
+++ trunk/MagicSoft/Mars/mjobs/MJPedestal.cc	(revision 4609)
@@ -104,5 +104,5 @@
 {
     if (fSequence)
-        return Form("%s/calped%05d", (const char*)fOutputPath, fSequence->GetSequence());
+        return Form("%s/calped%06d.root", (const char*)fOutputPath, fSequence->GetSequence());
 
     if (!fRuns)
@@ -422,4 +422,17 @@
 
     return kTRUE;
+}
+
+void MJPedestal::CheckEnv()
+{
+    if (!fEnv)
+        return;
+
+    TString e1 = fEnv->GetValue("MJPedestal.OutputPath", "");
+    if (!e1.IsNull())
+    {
+        e1.ReplaceAll("\015", "");
+        SetOutputPath(e1);
+    }
 }
 
@@ -449,4 +462,6 @@
     *fLog << endl;
 
+    CheckEnv();
+
     MParList  plist;
     MTaskList tlist;
@@ -493,4 +508,5 @@
         pedcalc.SetRange(fExtractor->GetHiGainFirst(), fExtractor->GetHiGainLast());
     }
+  /*
     else
     {
@@ -498,4 +514,5 @@
         *fLog << ": No extractor has been chosen, take default number of FADC samples " << endl;
     }
+    */
 
     tlist.AddToList(&geomapl);
Index: trunk/MagicSoft/Mars/mjobs/MJPedestal.h
===================================================================
--- trunk/MagicSoft/Mars/mjobs/MJPedestal.h	(revision 4607)
+++ trunk/MagicSoft/Mars/mjobs/MJPedestal.h	(revision 4609)
@@ -58,4 +58,6 @@
     void   DisplayOutliers(TH1D *hist) const;
 
+    void   CheckEnv();
+
 public:
     MJPedestal(const char *name=NULL, const char *title=NULL);
Index: trunk/MagicSoft/Mars/mjobs/MSequence.h
===================================================================
--- trunk/MagicSoft/Mars/mjobs/MSequence.h	(revision 4607)
+++ trunk/MagicSoft/Mars/mjobs/MSequence.h	(revision 4609)
@@ -50,4 +50,5 @@
     // Getter
     UInt_t GetSequence() const { return fSequence; }
+    UInt_t GetLastRun() const  { return fLastRun; }
 
     ClassDef(MSequence, 0)
