Index: trunk/MagicSoft/Mars/mjobs/MJCalibrateSignalFromOutside.cc
===================================================================
--- trunk/MagicSoft/Mars/mjobs/MJCalibrateSignalFromOutside.cc	(revision 5858)
+++ trunk/MagicSoft/Mars/mjobs/MJCalibrateSignalFromOutside.cc	(revision 5859)
@@ -22,5 +22,4 @@
 !
 \* ======================================================================== */
-
 /////////////////////////////////////////////////////////////////////////////
 //
@@ -54,4 +53,6 @@
 #include "MHCamEvent.h"
 #include "MPedestalCam.h"
+#include "MPedPhotCam.h"
+#include "MCalibrationHiLoCam.h"
 #include "MBadPixelsCam.h"
 
@@ -62,7 +63,26 @@
 #include "MCalibrationChargePINDiode.h"
 
+#include "MCalibrationChargeCalc.h"
+#include "MCalibrationRelTimeCalc.h"
+
+#include "MCalibrationIntensityChargeCam.h"
+#include "MCalibrationIntensityBlindCam.h"
+#include "MCalibrationIntensityRelTimeCam.h"
+#include "MCalibrationIntensityQECam.h"
+#include "MBadPixelsIntensityCam.h"
+
+#include "MHCalibrationChargeCam.h"
+#include "MHCalibrationChargeBlindCam.h"
+#include "MHCalibrationChargePINDiode.h"
+#include "MHCalibrationRelTimeCam.h"
+
+#include "MCalibCalcFromPast.h"
+
 #include "MReadReports.h"
 #include "MReadMarsFile.h"
 #include "MRawFileRead.h"
+#include "MTriggerPatternDecode.h"
+#include "MFTriggerPattern.h"
+#include "MContinue.h"
 #include "MGeomApply.h"
 #include "MMcPedestalCopy.h"
@@ -70,4 +90,6 @@
 #include "MPedCalcFromLoGain.h"
 #include "MExtractor.h"
+#include "MExtractPINDiode.h"
+#include "MExtractBlindPixel.h"
 #include "MTaskEnv.h"
 #include "MCalibrateData.h"
@@ -90,5 +112,5 @@
 //
 MJCalibrateSignalFromOutside::MJCalibrateSignalFromOutside(const char *name, const char *title)
-    : fIsDataCheck(kFALSE), fGeometry("MGeomCamMagic")
+    : fIsDataCheck(kFALSE), fGeometry("MGeomCamMagic"), fInterlaced(kFALSE)
 {
     fName  = name  ? name  : "MJCalibrateSignalFromOutside";
@@ -121,5 +143,5 @@
 }
 
-Bool_t MJCalibrateSignalFromOutside::ProcessFile(MPedestalCam &pedcamab, MPedestalCam &pedcamrms, MCalibrationChargeCam &chargecam, MCalibrationQECam &qecam, MCalibrationRelTimeCam &relcam, Byte_t filetype)
+Bool_t MJCalibrateSignalFromOutside::ProcessFile(MPedestalCam &pedcamab, MPedestalCam &pedcam2, MCalibrationChargeCam &chargecam, MCalibrationQECam &qecam, MCalibrationRelTimeCam &relcam, Byte_t filetype)
 {
     // --------------------------------------------------------------------------------
@@ -153,9 +175,56 @@
     plist.AddToList(this); // take care of fDisplay!
     plist.AddToList(&fBadPixels);
-    plist.AddToList(&pedcamrms);
+    plist.AddToList(&pedcam2);
+    plist.AddToList(&pedcamab);
     plist.AddToList(&chargecam);
     plist.AddToList(&qecam);
     plist.AddToList(&relcam);
 
+    MCalibrationIntensityChargeCam  intenscharge;
+    MCalibrationIntensityRelTimeCam intensreltime;
+    MCalibrationIntensityQECam      intensqe;
+    MCalibrationIntensityBlindCam   intensblind;
+    MBadPixelsIntensityCam          intensbad;
+
+    MHCalibrationChargeCam      hchargecam;
+    MHCalibrationChargeBlindCam hblindcam;
+    MHCalibrationChargePINDiode hpindiode;
+    MHCalibrationRelTimeCam     hrelcam;
+
+    //
+    // Calibration Results containers
+    //
+    if (fInterlaced)
+      {
+        plist.AddToList(&intensqe);
+        plist.AddToList(&intenscharge);
+        plist.AddToList(&intensblind);
+        //        plist.AddToList(&intensCalibrationPINDiode);
+        plist.AddToList(&intensreltime);
+        plist.AddToList(&intensbad);
+
+        hchargecam.SetLast(499.5);
+        hchargecam.SetNbins(300);
+
+        hchargecam.SetLoGain(kFALSE);
+        hrelcam.SetLoGain(kFALSE);
+
+        hchargecam.SetOscillations(kFALSE);
+        hrelcam.SetOscillations(kFALSE);
+
+        plist.AddToList(&hchargecam);
+        plist.AddToList(&hblindcam);
+        plist.AddToList(&hrelcam);
+        plist.AddToList(&hpindiode);
+    }
+
+    MPedPhotCam pedphotcam;
+    plist.AddToList(&pedphotcam);
+
+    MFillH fillhilo("MHCalibrationHiLoCam",      "MExtractedSignalCam",        "FillHiLoCam");
+    fillhilo.SetBit(MFillH::kDoNotDisplay);
+
+    MCalibrationHiLoCam hilocam;
+    plist.AddToList(&hilocam);
     // Setup Tasklist
     MTaskList tlist;
@@ -181,19 +250,59 @@
       }
 
+    // Check for interleaved events
+    MTriggerPatternDecode decode;
+
     MGeomApply             apply; // Only necessary to create geometry
     apply.SetGeometry(fGeometry);
     MBadPixelsMerge        merge(&fBadPixels);
 
-    MPedCalcFromLoGain     pedlo1("MPedCalcMeanFromLoGain");
+    MPedestalCam pedcam;
+
+    pedcamab.SetName("MPedestalFundamental");
+    pedcam2.SetName("MPedestalFromExtractorRndm");
+    pedcam.SetName("MPedestalFromExtractor");
+    plist.AddToList(&pedcam);
+    plist.AddToList(&pedcam2);
+    plist.AddToList(&pedcamab);
+
+    // Do signal and pedestal calculation
+    MPedCalcFromLoGain     pedlo1("MPedCalcFundamental");
     pedlo1.SetPedestalUpdate(kTRUE);
-    pedlo1.SetPedestalsOut(&pedcamab);
-
-    MPedCalcFromLoGain     pedlo2("MPedCalcRmsFromLoGain");
+    pedlo1.SetNamePedestalCamOut("MPedestalFundamental");
+
+    MPedCalcFromLoGain     pedlo2("MPedCalcWithExtractorRndm");
     pedlo2.SetPedestalUpdate(kTRUE);
-    pedlo2.SetPedestalsIn(&pedcamab);
-    pedlo2.SetPedestalsOut(&pedcamrms);
-
-    if (fExtractor->InheritsFrom("MExtractTimeAndCharge"))
-      pedlo2.SetExtractor((MExtractTimeAndCharge*)fExtractor);
+    pedlo2.SetRandomCalculation(kTRUE);
+    pedlo2.SetNamePedestalCamIn("MPedestalFundamental");
+    pedlo2.SetNamePedestalCamOut("MPedestalFromExtractorRndm");
+
+    MPedCalcFromLoGain     pedlo3("MPedCalcWithExtractor");
+    pedlo3.SetPedestalUpdate(kTRUE);
+    pedlo3.SetRandomCalculation(kFALSE);
+    pedlo3.SetNamePedestalCamIn("MPedestalFundamental");
+    pedlo3.SetNamePedestalCamOut("MPedestalFromExtractor");
+
+    if (fExtractor)
+    {
+        fExtractor->SetPedestals(&pedcamab);
+
+        if (fExtractor->InheritsFrom("MExtractTimeAndCharge"))
+        {
+            pedlo2.SetExtractor((MExtractTimeAndCharge*)fExtractor);
+            pedlo3.SetExtractor((MExtractTimeAndCharge*)fExtractor);
+            pedlo1.SetExtractWindow(15, 0/*obsolete*/);
+            pedlo2.SetExtractWindow(15, 0/*obsolete*/);
+            pedlo3.SetExtractWindow(15, 0/*obsolete*/);
+        }
+        else
+        {
+            // FIXME: How to get the fixed value 15 automatically?
+            const Int_t f = (Int_t)(15.5+fExtractor->GetHiGainFirst());
+            const Int_t n = (Int_t)(15.5+fExtractor->GetNumHiGainSamples());
+            pedlo1.SetExtractWindow(f, n);
+            pedlo2.SetExtractWindow(f, n);
+            pedlo3.SetExtractWindow(f, n);
+        }
+    }
 
     MMcPedestalCopy        pcopy;
@@ -205,12 +314,17 @@
         calib.SetCalibrationMode(MCalibrateData::kFfactor);
         calib.SetPedestalFlag(MCalibrateData::kRun);
+        calib.AddPedestal("MPedestalCam", "MPedPhotFundamental");
+        calib.AddPedestal("MPedestalCam", "MPedPhotFromExtractor");
+        calib.AddPedestal("MPedestalCam", "MPedPhotFromExtractorRndm");
     }
     else
     {
-        calib.AddPedestal("MPedestalCam", "MPedPhotFromExtractorRndm");
         calib.AddPedestal("Fundamental");
+        calib.AddPedestal("FromExtractor");
+        calib.AddPedestal("FromExtractorRndm");
         calib.SetPedestalFlag(MCalibrateData::kEvent);
-    }
-
+        calib.SetSignalType(MCalibrateData::kPhe);
+    }
+    
     MCalibrateRelTimes     caltm;
     MBadPixelsCalc         bpcal;
@@ -225,4 +339,6 @@
     MHCamEvent evt6(2, "Unsuitable",  "Unsuitable event ratio;;%");
     MHCamEvent evt7(0, "Times",       "Arrival Time;;T [slice]");
+    MHCamEvent evt8(0, "HiLoConv",    "Ratio Hi-Lo Gain Signal;;Ratio [1]");
+
     evt0.EnableVariance();
     evt1.EnableVariance();
@@ -232,13 +348,15 @@
     evt5.EnableVariance();
     evt7.EnableVariance();
-
-    MFillH fill0(&evt0, &pedcamab,             "FillPedFLG");
-    MFillH fill1(&evt1, "MPedestalCam",        "FillPedRmsFLG");
-    MFillH fill2(&evt2, "MExtractedSignalCam", "FillExtracted");
-    MFillH fill3(&evt3, "MPedPhotCam",         "FillPedPhot");
-    MFillH fill4(&evt4, "MPedPhotCam",         "FillPedRMS");
-    MFillH fill5(&evt5, "MCerPhotEvt",         "FillInterpolated");
+    evt8.EnableVariance();
+
+    MFillH fill0(&evt0, "MPedestalFundamental",   "FillPedFLG");
+    MFillH fill1(&evt1, "MPedestalFromExtractorRndm", "FillPedRmsFLG");
+    MFillH fill2(&evt2, "MExtractedSignalCam",    "FillExtracted");
+    MFillH fill3(&evt3, "MPedPhotFundamental",    "FillPedPhot");
+    MFillH fill4(&evt4, "MPedPhotFromExtractorRndm","FillPedRMS");
+    MFillH fill5(&evt5, "MCerPhotEvt",            "FillInterpolated");
     MFillH fill6(&evt6, "MBadPixelsCam",       "FillUnsuitable");
     MFillH fill7(&evt7, "MArrivalTime",        "FillTimes");
+    MFillH fill8(&evt8, &hilocam,              "FillHiLo");
 
     MWriteRootFile write(2, Form("%s{s/_D_/_Y_}", fPathOut.Data()), fOverwrite);
@@ -288,4 +406,5 @@
     tlist2.AddToList(&apply);
     tlist2.AddToList(&merge);
+    tlist2.AddToList(&decode);
     if (filetype==3)
         tlist2.AddToList(&pcopy);
@@ -295,11 +414,69 @@
         tlist2.AddToList(&pedlo2);
     }
+    MExtractPINDiode        pinext;
+    MExtractBlindPixel      blindext;
+
+    MFTriggerPattern fcalib;
+    
+    MCalibrationChargeCalc  chargecalc;
+    MCalibrationRelTimeCalc relcalc;
+
+    MCalibCalcFromPast      pastcalc;
+
+    pinext.SetPedestals(&pedcamab);
+    blindext.SetPedestals(&pedcamab);
+    chargecalc.SetPedestals(&pedcam2);
+
+    pastcalc.SetChargeCalc(&chargecalc);
+    pastcalc.SetRelTimeCalc(&relcalc);
+    pastcalc.SetCalibrate(&calib);
+
     tlist2.AddToList(&fill0);
     tlist2.AddToList(&fill1);
     tlist2.AddToList(&taskenv1);
     tlist2.AddToList(&fill2);
+    tlist2.AddToList(&pinext);
+    tlist2.AddToList(&blindext);
+    //
+    // Calibration histogramming
+    //
+    MFillH fillpin("MHCalibrationChargePINDiode", "MExtractedSignalPINDiode",   "FillPINDiode");
+    MFillH fillbnd("MHCalibrationChargeBlindCam", "MExtractedSignalBlindPixel", "FillBlindCam");
+    MFillH fillcam("MHCalibrationChargeCam",      "MExtractedSignalCam",        "FillChargeCam");
+    MFillH filltme("MHCalibrationRelTimeCam",     "MArrivalTimeCam",            "FillRelTime");
+    fillpin.SetBit(MFillH::kDoNotDisplay);
+    fillbnd.SetBit(MFillH::kDoNotDisplay);
+    fillcam.SetBit(MFillH::kDoNotDisplay);
+    filltme.SetBit(MFillH::kDoNotDisplay);
+
+    //
+    // Calibration Results containers
+    //
+    if (fInterlaced)
+      {
+        fcalib.DenyCalibration();
+        fcalib.SetInverted();
+
+        chargecalc.SetFilter(&fcalib);
+        pastcalc.SetFilter(  &fcalib);
+        fillcam.SetFilter(&fcalib);
+        filltme.SetFilter(&fcalib);
+        fillbnd.SetFilter(&fcalib);
+        fillpin.SetFilter(&fcalib);
+
+        tlist2.AddToList(&fcalib);
+        tlist2.AddToList(&pastcalc);
+        tlist2.AddToList(&fillcam);
+        tlist2.AddToList(&filltme);
+        tlist2.AddToList(&fillbnd);
+        tlist2.AddToList(&fillpin);
+        tlist2.AddToList(&chargecalc);
+        tlist2.AddToList(&relcalc);
+      }
+    
     tlist2.AddToList(&calib);
     if (&relcam)
       tlist2.AddToList(&caltm);
+    tlist2.AddToList(&fillhilo);
     tlist2.AddToList(&fill3);
     tlist2.AddToList(&bpcal);
@@ -309,4 +486,5 @@
     tlist2.AddToList(&fill6);
     tlist2.AddToList(&fill7);
+    tlist2.AddToList(&fill8);
 
     // Setup List for Drive-tree
@@ -316,6 +494,8 @@
     tlist.AddToList(IsDataCheck() ? (MTask*)&rawread : (MTask*)&readreal);
     tlist.AddToList(&tlist2, IsDataCheck()?"All":"Events");
-    if (!IsDataCheck())
-        tlist.AddToList(&pcalc, "Drive");
+
+    //    if (!IsDataCheck())
+    //        tlist.AddToList(&pcalc, "Drive");
+
     tlist.AddToList(&write);
 
@@ -337,4 +517,17 @@
     tlist.PrintStatistics();
 
+    
+    if (fInterlaced)
+      {
+        TObjArray cont;
+        cont.Add(&intensqe);
+        cont.Add(&intenscharge);
+        cont.Add(&intensblind);
+        //        cont.Add(&intensCalibrationPINDiode);
+        cont.Add(&intensreltime);
+        cont.Add(&intensbad);
+        WriteContainer(cont);
+      }
+
     if (!WriteResult())
         return kFALSE;
