Index: trunk/MagicSoft/Mars/mjobs/MJExtractCalibTest.cc
===================================================================
--- trunk/MagicSoft/Mars/mjobs/MJExtractCalibTest.cc	(revision 5941)
+++ trunk/MagicSoft/Mars/mjobs/MJExtractCalibTest.cc	(revision 5942)
@@ -45,4 +45,5 @@
 #include "MParList.h"
 #include "MTaskList.h"
+#include "MTaskEnv.h"
 #include "MEvtLoop.h"
 
@@ -52,4 +53,6 @@
 #include "MBadPixelsCam.h"
 #include "MBadPixelsTreat.h"
+#include "MBadPixelsCalc.h"
+#include "MBadPixelsMerge.h"
 #include "MCerPhotEvt.h"
 #include "MArrivalTime.h"
@@ -64,5 +67,5 @@
 #include "MReadMarsFile.h"
 #include "MGeomApply.h"
-#include "MExtractSlidingWindow.h"
+#include "MExtractTimeAndChargeSlidingWindow.h"
 #include "MExtractor.h"
 #include "MExtractTime.h"
@@ -73,5 +76,8 @@
 #include "MCalibrateData.h"
 #include "MCalibrateRelTimes.h"
-#include "MPedPhotCalc.h"
+
+#include "MTriggerPattern.h"
+#include "MTriggerPatternDecode.h"
+#include "MFTriggerPattern.h"
 
 #include "MStatusDisplay.h"
@@ -89,5 +95,5 @@
 MJExtractCalibTest::MJExtractCalibTest(const char *name, const char *title) 
     : fUseCosmicsFilter(kTRUE), fRuns(NULL), fExtractor(NULL), fTimeExtractor(NULL),
-      fDisplayType(kNormalDisplay)
+      fDisplayType(kNormalDisplay), fGeometry("MGeomCamMagic")
 {
     fName  = name  ? name  : "MJExtractCalibTest";
@@ -247,15 +253,21 @@
 
 
-Bool_t MJExtractCalibTest::ProcessD(MPedestalCam &pedcam, MCalibrationChargeCam &calcam, MCalibrationQECam &qecam)
+Bool_t MJExtractCalibTest::Process(MPedestalCam &pedcam, 
+				   MCalibrationChargeCam &calcam, MCalibrationQECam &qecam, 
+				   MCalibrationRelTimeCam &reltime, 
+				   Byte_t filetype)
 {
     // const TString fname = GetOutputFile();
   
 //  if (gSystem->AccessPathName(fname, kFileExists))
-    return ProcessFileD(pedcam,calcam,qecam);
+    return ProcessFile(pedcam,calcam,qecam,reltime,filetype);
   
     // return kTRUE;
 }
 
-Bool_t MJExtractCalibTest::ProcessFileD(MPedestalCam &pedcam, MCalibrationChargeCam &calcam, MCalibrationQECam &qecam)
+Bool_t MJExtractCalibTest::ProcessFile(MPedestalCam &pedcam, 
+				       MCalibrationChargeCam &calcam, MCalibrationQECam &qecam, 
+				       MCalibrationRelTimeCam &relcam,
+				       Byte_t filetype)
 {
   if (!fRuns)
@@ -269,25 +281,33 @@
       return kFALSE;
     }
-  
+
   *fLog << inf;
   fLog->Separator(GetDescriptor());
   *fLog << "Calculate MExtractedSignalCam from Runs " << fRuns->GetRunsAsString() << endl;
   *fLog << endl;
-  
+
+  MBadPixelsCam badcam;
+  
+  // Setup Lists
+  MParList plist;
+  plist.AddToList(this); // take care of fDisplay!
+  plist.AddToList(&fTestCam);
+  plist.AddToList(&fTestTimeCam);
+  plist.AddToList(&badcam);
+  plist.AddToList(&pedcam);
+  plist.AddToList(&calcam);
+  plist.AddToList(&qecam);
+  plist.AddToList(&relcam);
+
   MCerPhotEvt          cerphot;
   MPedPhotCam          pedphot;
   MHCalibrationTestCam testcam;
 
-  // Setup Lists
-  MParList plist;
-  plist.AddToList(&pedcam);
-  plist.AddToList(&calcam);
-  plist.AddToList(&qecam);
   plist.AddToList(&cerphot);
   plist.AddToList(&pedphot);
   plist.AddToList(&testcam);
-  plist.AddToList(&fTestCam);
-  plist.AddToList(&fBadPixels);
-
+
+  pedcam.SetName("MPedestalFundamental");
+  
   MTaskList tlist;
   plist.AddToList(&tlist);
@@ -298,11 +318,64 @@
   static_cast<MRead&>(read).AddFiles(*fRuns);
   
+  // Check for interleaved events
+  MTriggerPatternDecode decode;
+  MFTriggerPattern      fcalib;
+  fcalib.DenyCalibration();
+  MContinue conttp(&fcalib, "ContTrigPattern");
+
   MGeomApply            apply; // Only necessary to craete geometry
-  MExtractSlidingWindow extract2;
+  apply.SetGeometry(fGeometry);  
+  MBadPixelsMerge       merge(&fBadPixels);
+
+  MExtractTimeAndChargeSlidingWindow extrsw;
+  MExtractTimeFastSpline             extime;
+  extime.SetPedestals(&pedcam);
+
+  MTaskEnv taskenv1("ExtractSignal");
+  MTaskEnv taskenv2("ExtractTime");
+
+  if (fExtractor)
+    { 
+      fExtractor->SetPedestals(&pedcam);
+      taskenv1.SetDefault(fExtractor);
+    }
+  if (fTimeExtractor)
+    {
+      fTimeExtractor->SetPedestals(&pedcam);
+      taskenv2.SetDefault(fTimeExtractor);
+    }
+  else
+    {
+      extrsw.SetPedestals(&pedcam);
+      extrsw.SetWindowSize(8,8);
+      taskenv2.SetDefault(&extrsw);
+      *fLog << warn << GetDescriptor() 
+            << ": No extractor has been chosen, take default MExtractTimeAndChargeSlidingWindow " << endl;
+    }
+
   MCalibrateData        photcalc;
-  photcalc.SetCalibrationMode(MCalibrateData::kFfactor);
-  MPedPhotCalc          pedphotcalc;  
+  MCalibrateRelTimes    caltimes;
+  if (filetype==3) // MC file
+    {
+      photcalc.SetCalibrationMode(MCalibrateData::kFfactor);
+      photcalc.SetPedestalFlag(MCalibrateData::kRun);
+      photcalc.AddPedestal("MPedestalCam", "MPedPhotFundamental");
+    }
+  else
+    {
+      photcalc.SetCalibrationMode(MCalibrateData::kFfactor);
+      photcalc.AddPedestal("Fundamental");
+      photcalc.SetPedestalFlag(MCalibrateData::kEvent);
+      photcalc.SetSignalType(MCalibrateData::kPhot);
+    }
+
+  MBadPixelsCalc        badcalc;
   MBadPixelsTreat       badtreat;
-  badtreat.SetUseInterpolation();
+  badtreat.SetProcessTimes(kFALSE);
+
+  badcalc.SetNamePedPhotCam("MPedPhotFundamental");
+  //badtreat.SetUseInterpolation();
+  badtreat.AddNamePedPhotCam("MPedPhotFundamental");
+
   MCalibrationTestCalc  testcalc;
 
@@ -313,35 +386,39 @@
     }
   
-  MHCamEvent evt("ExtSignal");
-  evt.SetType(0);
-  MFillH fill(&evt, "MExtractedSignalCam");
+  MHCamEvent evt0(0,"Signal", "Un-Calibrated Signal;;S [FADC cnts]" );
+  MHCamEvent evt1(3,"CalSig", "Calibrated and Interpolated Signal;;S [\\gamma]");
+  MHCamEvent evt2(0,"Times" , "Arrival Time;;T [slice]");
+
+  MFillH fill0(&evt0, "MExtractedSignalCam", "FillUncalibrated");
+  MFillH fill1(&evt1, "MCerPhotEvt", "FillCalibrated");
+  MFillH fill2(&evt2, "MArrivalTime","FillTimes");
   
   MFillH fillcam("MHCalibrationTestCam", "MCerPhotEvt");
   fillcam.SetNameTab("Test");
+  MFillH filltme("MHCalibrationTestTimeCam", "MArrivalTime");
+  filltme.SetNameTab("TestTime");
 
   MFCosmics cosmics;
-  MContinue cont(&cosmics);
+  cosmics.SetNamePedestalCam("MPedestalFundamental");
+  MContinue contcos(&cosmics,"ContCosmics");
   
   tlist.AddToList(&read);
+  tlist.AddToList(&decode);
   tlist.AddToList(&apply);
-
-  if (fExtractor)
-    tlist.AddToList(fExtractor);
-  else
-  {
-      *fLog << warn << GetDescriptor() 
-            << ": No extractor has been chosen, take default MExtractSlidingWindow " << endl;
-      tlist.AddToList(&extract2);
-  }
-
-
-  if (fUseCosmicsFilter)
-    tlist.AddToList(&cont);
-
-  tlist.AddToList(&fill);
+  tlist.AddToList(&merge);
+  tlist.AddToList(&conttp);
+  tlist.AddToList(&taskenv1);
+  if (!fExtractor->InheritsFrom("MExtractTimeAndCharge"))
+    tlist.AddToList(&taskenv2);
+  tlist.AddToList(&contcos);
+  tlist.AddToList(&fill0);
   tlist.AddToList(&photcalc);
-  tlist.AddToList(&pedphotcalc);
+  tlist.AddToList(&caltimes);
+  tlist.AddToList(&badcalc);
   tlist.AddToList(&badtreat);
+  tlist.AddToList(&fill1);
+  tlist.AddToList(&fill2);
   tlist.AddToList(&fillcam);
+  tlist.AddToList(&filltme);
   tlist.AddToList(&testcalc);
   
@@ -363,5 +440,5 @@
   DisplayResult(plist);
 
-  if (!WriteResultD())
+  if (!WriteResult())
     return kFALSE;
 
@@ -370,110 +447,4 @@
   return kTRUE;
 }
-
-Bool_t MJExtractCalibTest::ProcessT(MPedestalCam &pedcam, MCalibrationRelTimeCam &relcam)
-{
-
-//  const TString fname = GetOutputFile();
-  
-//  if (gSystem->AccessPathName(fname, kFileExists))
-    return ProcessFileT(pedcam,relcam);
-  
-//  return kTRUE;
-}
-
-Bool_t MJExtractCalibTest::ProcessFileT(MPedestalCam &pedcam, MCalibrationRelTimeCam &relcam)
-{
-
-  if (!fRuns)
-    {
-      *fLog << err << "No Runs choosen... abort." << endl;
-      return kFALSE;
-    }
-  if (fRuns->GetNumRuns() != fRuns->GetNumEntries())
-    {
-      *fLog << err << "Number of files found doesn't match number of runs... abort." << endl;
-      return kFALSE;
-    }
-  
-  *fLog << inf;
-  fLog->Separator(GetDescriptor());
-  *fLog << "Calculate MExtractedSignalCam from Runs " << fRuns->GetRunsAsString() << endl;
-  *fLog << endl;
-  
-  MArrivalTime     arrtime;
-
-  // Setup Lists
-  MParList plist;
-  plist.AddToList(&pedcam);
-  plist.AddToList(&relcam);
-  plist.AddToList(&arrtime);
-  plist.AddToList(&fTestTimeCam);
-  plist.AddToList(&fBadPixels);
-  
-  MTaskList tlist;
-  plist.AddToList(&tlist);
-
-  // Setup Task-lists
-  MReadMarsFile read("Events");
-  read.DisableAutoScheme();
-  static_cast<MRead&>(read).AddFiles(*fRuns);
-  
-  MGeomApply             apply; // Only necessary to craete geometry
-  MExtractTimeFastSpline extract;
-  MExtractSlidingWindow  extcharge; // Only for the cosmics filter
-  MCalibrateRelTimes     timecalc;
-  MFCosmics              cosmics;
-  MContinue              cont(&cosmics);
-
-  MHCamEvent evt("ExtTimes");
-  evt.SetType(0);
-  MFillH fill(&evt, "MArrivalTimeCam");
-  
-  MFillH fillcam("MHCalibrationTestTimeCam", "MArrivalTime");
-  fillcam.SetNameTab("TestTime");
-
-  tlist.AddToList(&read);
-  tlist.AddToList(&apply);
-
-  if (fTimeExtractor)
-    tlist.AddToList(fTimeExtractor);
-  else
-  {
-      *fLog << warn << GetDescriptor() 
-            << ": No extractor has been chosen, take default MExtractTimeFastSpline " << endl;
-      tlist.AddToList(&extract);
-  }
-
-  tlist.AddToList(&extcharge);
-  tlist.AddToList(&cont);
-  tlist.AddToList(&fill);
-  tlist.AddToList(&timecalc);
-  tlist.AddToList(&fillcam);
-  
-  // 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();
-  
-  DisplayResultT(plist);
-
-  if (!WriteResultT())
-    return kFALSE;
-
-  *fLog << inf << GetDescriptor() << ": Done." << endl;
-  
-  return kTRUE;
-}
-
 
 Bool_t MJExtractCalibTest::ReadPedPhotCam()
@@ -514,5 +485,5 @@
 }
 
-Bool_t MJExtractCalibTest::WriteResultD()
+Bool_t MJExtractCalibTest::WriteResult()
 {
     
@@ -544,35 +515,14 @@
     }
 
+    if (fTestTimeCam.Write()<=0)
+    {
+        *fLog << err << "Unable to write MCalibrationTestCam to " << oname << endl;
+        return kFALSE;
+    }
+
     return kTRUE;
 
 }
 
-Bool_t MJExtractCalibTest::WriteResultT()
-{
-    
-    if (fPathOut.IsNull())
-        return kTRUE;
-    
-    const TString oname(GetOutputFile());
-
-    *fLog << inf << "Writing to file: " << oname << endl;
-    
-    TFile file(oname, "UPDATE");
-    
-    if (fDisplay && fDisplay->Write()<=0)
-    {
-        *fLog << err << "Unable to write MStatusDisplay to " << oname << endl;
-        return kFALSE;
-    }
-
-    if (fTestTimeCam.Write()<=0)
-    {
-        *fLog << err << "Unable to write MHCalibrationTestTimeCam to " << oname << endl;
-        return kFALSE;
-    }
-
-    return kTRUE;
-
-}
 
 
