Index: trunk/MagicSoft/Mars/mtemp/mifae/programs/makeHillas.cc
===================================================================
--- trunk/MagicSoft/Mars/mtemp/mifae/programs/makeHillas.cc	(revision 5191)
+++ trunk/MagicSoft/Mars/mtemp/mifae/programs/makeHillas.cc	(revision 5208)
@@ -22,5 +22,7 @@
 #include "MExtractor.h"
 #include "MExtractFixedWindow.h"
+#include "MExtractFixedWindowPeakSearch.h"
 #include "MExtractSlidingWindow.h"
+#include "MPedCalcFromLoGain.h"
 #include "MExtractSignal.h"
 #include "MCalibrationChargeCalc.h"
@@ -30,5 +32,5 @@
 #include "MCerPhotEvt.h"
 #include "MPedPhotCam.h"
-#include "MCalibrate.h"
+#include "MCalibrateData.h"
 #include "MPedPhotCalc.h"
 #include "MHillas.h"
@@ -65,9 +67,4 @@
 void makeHillas();
 
-// initial and final time slices to be used in signal extraction
-const Byte_t hifirst = 1;
-const Byte_t hilast  = 14;
-const Byte_t lofirst = 3;
-const Byte_t lolast  = 14;
 
 // declaration of variables read from datacards
@@ -84,7 +81,7 @@
 ULong_t  nmaxevents= 999999999;
 Short_t  calflag   = 1;
-Bool_t  caltimeflag   = kFALSE;
+Bool_t   caltimeflag   = kFALSE;
 Short_t  cleanflag   = 1;
-UShort_t  lrings     = 1;
+UShort_t lrings     = 1;
 Float_t  lcore     = 3.0;
 Float_t  ltail     = 1.5;
@@ -94,6 +91,13 @@
 Int_t    kalgorithm = 1;
 Int_t    nfiles    = 0;
+Int_t    hifirst = 0;
+Int_t    hilast  = 14;
+Int_t    lofirst = 2;
+Int_t    lolast  = 14;
+Int_t    wsize   = 6;
+Int_t    sext    = 0;
 
 const TString defaultcard="makehillas.datacard";
+char* chext[3]={"Fixed window","Sliding window","Peak Search"};
 /*************************************************************/
 static void Usage()
@@ -102,5 +106,5 @@
   gLog << "Usage is:" << endl;
   gLog << "   makeHillas [-h] [-?] <datacards>" << endl << endl;
-  gLog << "     <datacards>: datacards file name (dafault input.datacards)" << endl;
+  gLog << "     <datacards>: datacards file name (dafault makehillas.datacards)" << endl;
   gLog << "     -?/-h: This help" << endl << endl;
 }
@@ -135,18 +139,30 @@
 void makeHillas()
 {
-  // Set the general tasks/containers
-  MExtractFixedWindow    extractor;
-  extractor.SetRange(hifirst,hilast,lofirst,lolast);
+  // Set the signal extractor
+  MExtractor* extractor;
+  switch(sext)
+    {
+    case 0:
+      extractor = new MExtractFixedWindow();
+      break;
+    case 1:
+      extractor = new MExtractSlidingWindow();
+      ((MExtractSlidingWindow*)extractor)->SetWindowSize(wsize,wsize);
+      break;
+    case 2:
+      extractor = new MExtractFixedWindowPeakSearch();
+      ((MExtractFixedWindowPeakSearch*)extractor)->SetWindows(wsize,wsize,4);
+      break;
+    default:
+      extractor = new MExtractFixedWindow();
+      break;
+    }
+
+  extractor->SetRange(hifirst,hilast,lofirst,lolast);
  
-  //MExtractSlidingWindow    extractor;
-  //extractor.SetRange(hifirst,hilast,lofirst,lolast);
-  //extractor.SetWindowSize(4,4);
-  
-  MGeomCamMagic       geomcam;
-  MGeomApply          geomapl;
-
-  /************************************/
-  /* FIRST LOOP: PEDESTAL COMPUTATION */
-  /************************************/
+
+  /****************************************************/
+  /* FIRST LOOP: PEDESTAL COMPUTATION FOR CALIBRATION */
+  /****************************************************/
 
   // If you want to exclude pixels from the beginning, read 
@@ -155,6 +171,6 @@
 
   MJPedestal pedcalloop;
-  pedcalloop.SetInput(&pediter);
-  pedcalloop.SetExtractor(&extractor);
+  pedcalloop.SetInput(&pedcaliter);
+  pedcalloop.SetExtractor(extractor);
   //  pedloop.SetBadPixels(badcam);
   //pedcalloop.SetOutputPath(outpath.Data());
@@ -171,5 +187,5 @@
 
   calloop.SetRelTimeCalibration(caltimeflag);
-  calloop.SetExtractor(&extractor);
+  calloop.SetExtractor(extractor);
   calloop.SetInput(&caliter);
   calloop.SetBadPixels(pedcalloop.GetBadPixels());
@@ -180,234 +196,464 @@
       return;
 
-
-  /************************************************************************/
-  /*                THIRD LOOP: PEDESTAL CALIBRATION INTO PHOTONS         */
-  /************************************************************************/
-  // First Compute the pedestals
-  MJPedestal pedloop;
-  pedloop.SetInput(&pediter);
-
-  if (!pedloop.Process())
-    return;
-
-  MParList  plist3;
-  MTaskList tlist3;
-  plist3.AddToList(&tlist3);
-  
-  // containers
-  MCerPhotEvt         nphot;
-  MPedPhotCam         nphotrms;
-  MExtractedSignalCam sigcam;
-
-  plist3.AddToList(&geomcam);
-  plist3.AddToList(&pedloop.GetPedestalCam());
-  plist3.AddToList(&calloop.GetCalibrationCam());
-  plist3.AddToList(&calloop.GetQECam());
-  plist3.AddToList(&calloop.GetRelTimeCam());
-  plist3.AddToList(&calloop.GetBadPixels());
-  plist3.AddToList(&sigcam);
-  plist3.AddToList(&nphot);
-  plist3.AddToList(&nphotrms);
-
-  
-  MCalibrate::CalibrationMode_t calMode=MCalibrate::kDefault;  
-  if(calflag==0)
-    calMode=MCalibrate::kNone;
-  if(calflag==-1)
-    calMode=MCalibrate::kDummy;
-
-  //tasks
-  MReadMarsFile read3("Events");
-  static_cast<MRead&>(read3).AddFiles(pediter); 
-  read3.DisableAutoScheme();
- 
-  MCalibrate      photcalc(calMode);
-  MPedPhotCalc    photrmscalc; 
-  
-  tlist3.AddToList(&read3);
-  tlist3.AddToList(&geomapl);
-  tlist3.AddToList(&extractor);
-  tlist3.AddToList(&photcalc);
-  tlist3.AddToList(&photrmscalc);
-
-  // Create and setup the eventloop
-  MEvtLoop evtloop3;
-  evtloop3.SetParList(&plist3);
-  if (!evtloop3.Eventloop())
-    return;
-  
-  tlist3.PrintStatistics();
-
-  /************************************************************************/
-  /*                FOURTH LOOP: DATA CALIBRATION INTO PHOTONS            */
-  /************************************************************************/
-
-  MParList  plist4;
-  MTaskList tlist4;
-  plist4.AddToList(&tlist4);
-  
-  // containers 
-  MHillas       hillas;
-  MNewImagePar  newimagepar;
-  MSrcPosCam    source;
-  MRawRunHeader runhead;
-
-  MArrivalTimeCam   timecam;
-
-
-  // islands
-  MIslands      isl;
-  MIslands      isl2;
-  MIslands      isl3;
-
-  isl.SetName("MIslands");  
-  isl2.SetName("MIslands2");
-  isl3.SetName("MIslands3");
-
-  plist4.AddToList(&timecam);
-  plist4.AddToList(&isl);
-  
-  if (islflag == 2)
-    plist4.AddToList(&isl2);
-  if (islflag == 3)
-    plist4.AddToList(&isl3);
-  
-  plist4.AddToList(&geomcam);
-  plist4.AddToList(&pedloop.GetPedestalCam());
-  plist4.AddToList(&calloop.GetCalibrationCam());
-  plist4.AddToList(&calloop.GetQECam());
-  plist4.AddToList(&calloop.GetRelTimeCam());
-  plist4.AddToList(&calloop.GetBadPixels());
-  plist4.AddToList(&nphot);
-  plist4.AddToList(&nphotrms);
-  plist4.AddToList(&source);
-  plist4.AddToList(&hillas);
-  plist4.AddToList(&newimagepar);
-  plist4.AddToList(&runhead);
-
-  // cuts
-  MF cut(filter);
-  
-  //tasks
-  MReadMarsFile read4("Events");
-  static_cast<MRead&>(read4).AddFiles(datiter); 
-  read4.DisableAutoScheme();
-  
-  MImgCleanStd      clean(lcore,ltail);
-  clean.SetCleanRings(lrings);
-  MImgCleanStd::CleaningMethod_t cleanMeth= MImgCleanStd::kStandard;  
-  if(cleanflag==2)
-    cleanMeth=MImgCleanStd::kDemocratic;
-  clean.SetMethod(cleanMeth);
-
-  MArrivalTimeCalc2 timecalc;
-  MIslandsCalc       island;
-  island.SetOutputName("MIslands");
-  island.SetAlgorithm(kalgorithm);
-
-  MBadPixelsTreat   interpolatebadpixels;
-  interpolatebadpixels.SetUseInterpolation();
-  interpolatebadpixels.SetProcessPedestal();
-  //  interpolatebadpixels.SetSloppyTreatment();
-
-  MIslandsClean      islclean(lnew);
-  islclean.SetInputName("MIslands");
-  islclean.SetMethod(kmethod);
-      
-  MIslandsCalc       island2;
-  island2.SetOutputName("MIslands2");  
-  island2.SetAlgorithm(kalgorithm);
-
-  MIslandsCalc       island3;
-  island3.SetOutputName("MIslands3");  
-  
-  
-  MHillasCalc       hcalc;
-  MHillasSrcCalc    csrc1;
-  
-  MContinue applycut(&cut);
-  applycut.SetInverted(kTRUE);
-  MWriteRootFile write(outname,"RECREATE");
-  
-  MHillasDisplay*  disphillas=NULL;
-
-  write.AddContainer("MHillas"        , "Parameters");
-  write.AddContainer("MHillasSrc"     , "Parameters");
-  write.AddContainer("MHillasExt"     , "Parameters");
-  write.AddContainer("MNewImagePar"   , "Parameters");
-  write.AddContainer("MRawEvtHeader"  , "Parameters");
-  write.AddContainer("MRawRunHeader"  , "Parameters");
-  write.AddContainer("MTime"          , "Parameters");
-  write.AddContainer("MConcentration" , "Parameters");
-  write.AddContainer("MSrcPosCam"     , "Parameters");
-  write.AddContainer("MIslands"       , "Parameters");
-
-  if (islflag == 2) 
-    write.AddContainer("MIslands2" , "Parameters");
-  if (islflag == 3) 
-    write.AddContainer("MIslands3" , "Parameters");
-
-
-  if(display)
-    {
-      disphillas = new MHillasDisplay(&nphot,&geomcam);
-      disphillas->SetIslandsName("MIslands");
+  // Next loops are different if we take pedestals from data or from pedestal files
+  if(pediter.GetNumRuns()==0)
+    {
+      /************************************************************************/
+      /*                 THIRD (SMALL) LOOP TO GET INITIAl PEDESTALS          */
+      /************************************************************************/      
+      MParList  plist3;
+      MTaskList tlist3;
+      
+      // containers
+      MPedestalCam  pedcamdata;      
+      MGeomCamMagic geomcam;
+      
+      plist3.AddToList(&tlist3);
+      plist3.AddToList(&geomcam);
+      plist3.AddToList(&pedcamdata);
+            
+      //tasks
+      MReadMarsFile read3("Events");
+      static_cast<MRead&>(read3).AddFiles(datiter); 
+      read3.DisableAutoScheme();
+
+      MGeomApply      geomapl;
+
+      MPedCalcFromLoGain peddatacalc;
+      peddatacalc.SetPedestalUpdate(kFALSE);
+      peddatacalc.SetRange(hifirst,hilast,lofirst,lolast);
+      peddatacalc.SetWindowSize(wsize,wsize);
+      peddatacalc.SetNumEventsDump(500);
+      peddatacalc.SetMaxHiGainVar(40);
+      
+      tlist3.AddToList(&read3);
+      tlist3.AddToList(&geomapl);
+      tlist3.AddToList(&peddatacalc);
+      
+      // Create and setup the eventloop
+      MEvtLoop evtloop3;
+      evtloop3.SetParList(&plist3);
+      if (!evtloop3.Eventloop(500))
+	return;
+      
+      tlist3.PrintStatistics();
+      
+      /************************************************************************/
+      /*       FOURTH LOOP: PEDESTAL+DATA CALIBRATION INTO PHOTONS            */
+      /************************************************************************/
+      
+      MParList  plist4;
+      MTaskList tlist4;
+      plist4.AddToList(&tlist4);
+      
+      // containers 
+      MCerPhotEvt     nphot;
+      MPedPhotCam     nphotrms;
+      MHillas         hillas;
+      MNewImagePar    newimagepar;
+      MSrcPosCam      source;
+      MRawRunHeader   runhead;      
+      MArrivalTimeCam timecam;      
+      // islands
+      MIslands      isl;
+      MIslands      isl2;
+      MIslands      isl3;
+      
+      isl.SetName("MIslands");  
+      isl2.SetName("MIslands2");
+      isl3.SetName("MIslands3");
+      
+      plist4.AddToList(&timecam);
+      plist4.AddToList(&isl);
+      
       if (islflag == 2)
-	disphillas->SetIslandsName("MIslands2");
+	plist4.AddToList(&isl2);
       if (islflag == 3)
-	disphillas->SetIslandsName("MIslands3");
-    }      
-
-  tlist4.AddToList(&read4);
-  tlist4.AddToList(&geomapl);
-  tlist4.AddToList(&extractor);
-  tlist4.AddToList(&photcalc);
-  if(calflag==11 || calflag==21)
-    tlist4.AddToList(&interpolatebadpixels);
-  tlist4.AddToList(&clean);
-  tlist4.AddToList(&timecalc);
-  tlist4.AddToList(&island);
-
-  if (islflag == 2)
-    {
-      tlist4.AddToList(&islclean);
-      tlist4.AddToList(&island2);
-    }
-
-  if (islflag == 3)
-    {
-      tlist4.AddToList(&islclean);
-      tlist4.AddToList(&island3);
-    }
-  
-  tlist4.AddToList(&hcalc);
-  tlist4.AddToList(&csrc1);
-  if(filter.Length())
-    tlist4.AddToList(&applycut);
-  tlist4.AddToList(&write);
-  if(display)
-    {
-      disphillas->SetPSFile();
-      disphillas->SetPSFileName(psfilename);
-      if(display==2) 
-	disphillas->SetPause(kFALSE);	
-      tlist4.AddToList(disphillas);
-    }
-
-  // Create and setup the eventloop
-  MEvtLoop datloop;
-  datloop.SetParList(&plist4);
-
-  cout << "*************************************************************" << endl;
-  cout << "***   COMPUTING DATA USING EXTRACTED SIGNAL (IN PHOTONS)  ***" << endl;
-  cout << "*************************************************************" << endl;
-  
-  if (!datloop.Eventloop(nmaxevents))
-    return;
-
-  tlist4.PrintStatistics();    
-
+	plist4.AddToList(&isl3);
+      
+      plist4.AddToList(&geomcam);
+      plist4.AddToList(&pedcamdata);
+      plist4.AddToList(&calloop.GetCalibrationCam());
+      plist4.AddToList(&calloop.GetQECam());
+      plist4.AddToList(&calloop.GetRelTimeCam());
+      plist4.AddToList(&calloop.GetBadPixels());
+      plist4.AddToList(&nphot);
+      plist4.AddToList(&nphotrms);
+      plist4.AddToList(&source);
+      plist4.AddToList(&hillas);
+      plist4.AddToList(&newimagepar);
+      plist4.AddToList(&runhead);
+      
+      // cuts
+      MF cut(filter);
+      
+      //tasks
+      MReadMarsFile read4("Events");
+      static_cast<MRead&>(read4).AddFiles(datiter); 
+      read4.DisableAutoScheme();
+            
+      peddatacalc.SetPedestalUpdate(kTRUE);
+
+      MCalibrateData::CalibrationMode_t calMode=MCalibrateData::kDefault;  
+      if(calflag==0)
+	calMode=MCalibrateData::kNone;
+      if(calflag==-1)
+	calMode=MCalibrateData::kDummy;
+      MCalibrateData  photcalc;
+      photcalc.SetCalibrationMode(calMode);
+      photcalc.EnablePedestalType(MCalibrateData::kRun);
+      photcalc.EnablePedestalType(MCalibrateData::kEvent);
+
+      MImgCleanStd      clean(lcore,ltail);
+      clean.SetCleanRings(lrings);
+      MImgCleanStd::CleaningMethod_t cleanMeth= MImgCleanStd::kStandard;  
+      if(cleanflag==2)
+	cleanMeth=MImgCleanStd::kDemocratic;
+      clean.SetMethod(cleanMeth);
+      
+      MArrivalTimeCalc2 timecalc;
+      MIslandsCalc       island;
+      island.SetOutputName("MIslands");
+      island.SetAlgorithm(kalgorithm);
+      
+      MBadPixelsTreat   interpolatebadpixels;
+      interpolatebadpixels.SetUseInterpolation();
+      interpolatebadpixels.SetProcessPedestal();
+      
+      MIslandsClean      islclean(lnew);
+      islclean.SetInputName("MIslands");
+      islclean.SetMethod(kmethod);
+      
+      MIslandsCalc       island2;
+      island2.SetOutputName("MIslands2");  
+      island2.SetAlgorithm(kalgorithm);
+      
+      MIslandsCalc       island3;
+      island3.SetOutputName("MIslands3");  
+      
+      
+      MHillasCalc       hcalc;
+      MHillasSrcCalc    csrc1;
+      
+      MContinue applycut(&cut);
+      applycut.SetInverted(kTRUE);
+      MWriteRootFile write(outname,"RECREATE");
+      
+      MHillasDisplay*  disphillas=NULL;
+      
+      write.AddContainer("MHillas"        , "Parameters");
+      write.AddContainer("MHillasSrc"     , "Parameters");
+      write.AddContainer("MHillasExt"     , "Parameters");
+      write.AddContainer("MNewImagePar"   , "Parameters");
+      write.AddContainer("MRawEvtHeader"  , "Parameters");
+      write.AddContainer("MRawRunHeader"  , "Parameters");
+      write.AddContainer("MTime"          , "Parameters");
+      write.AddContainer("MConcentration" , "Parameters");
+      write.AddContainer("MSrcPosCam"     , "Parameters");
+      write.AddContainer("MIslands"       , "Parameters");
+      
+      if (islflag == 2) 
+	write.AddContainer("MIslands2" , "Parameters");
+      if (islflag == 3) 
+	write.AddContainer("MIslands3" , "Parameters");
+      
+      
+      if(display)
+	{
+	  disphillas = new MHillasDisplay(&nphot,&geomcam);
+	  disphillas->SetIslandsName("MIslands");
+	  if (islflag == 2)
+	    disphillas->SetIslandsName("MIslands2");
+	  if (islflag == 3)
+	    disphillas->SetIslandsName("MIslands3");
+	}      
+      
+      tlist4.AddToList(&read4);
+      tlist4.AddToList(&geomapl);
+      tlist4.AddToList(&peddatacalc);
+      tlist4.AddToList(extractor);
+      tlist4.AddToList(&timecalc);
+      tlist4.AddToList(&photcalc);
+      if(calflag==11 || calflag==21)
+      	tlist4.AddToList(&interpolatebadpixels);
+      tlist4.AddToList(&clean);
+      tlist4.AddToList(&island);
+      
+      if (islflag == 2)
+	{
+	  tlist4.AddToList(&islclean);
+	  tlist4.AddToList(&island2);
+	}
+      
+      if (islflag == 3)
+	{
+	  tlist4.AddToList(&islclean);
+	  tlist4.AddToList(&island3);
+	}
+      
+      tlist4.AddToList(&hcalc);
+      tlist4.AddToList(&csrc1);
+      if(filter.Length())
+	tlist4.AddToList(&applycut);
+      tlist4.AddToList(&write);
+      if(display)
+	{
+	  disphillas->SetPSFile();
+	  disphillas->SetPSFileName(psfilename);
+	  if(display==2) 
+	    disphillas->SetPause(kFALSE);	
+	  tlist4.AddToList(disphillas);
+	}
+      
+      // Create and setup the eventloop
+      MEvtLoop datloop;
+      datloop.SetParList(&plist4);
+      
+      cout << "*************************************************************" << endl;
+      cout << "***   COMPUTING DATA USING EXTRACTED SIGNAL (IN PHOTONS)  ***" << endl;
+      cout << "*************************************************************" << endl;
+      
+      if (!datloop.Eventloop(nmaxevents))
+	return;
+      
+      tlist4.PrintStatistics();    
+      delete extractor;
+    }
+  else
+    {      
+      /************************************************************************/
+      /*                THIRD LOOP: PEDESTAL CALIBRATION INTO PHOTONS         */
+      /************************************************************************/
+      
+      // First Compute the pedestals
+      MJPedestal pedloop;
+      pedloop.SetInput(&pediter);
+      
+      if (!pedloop.Process())
+	return;
+      
+      MParList  plist3;
+      MTaskList tlist3;
+      plist3.AddToList(&tlist3);
+      
+      // containers
+      MGeomCamMagic       geomcam;
+      MCerPhotEvt         nphot;
+      MPedPhotCam         nphotrms;
+      MExtractedSignalCam sigcam;
+      
+      plist3.AddToList(&geomcam);
+      plist3.AddToList(&pedloop.GetPedestalCam());
+      plist3.AddToList(&calloop.GetCalibrationCam());
+      plist3.AddToList(&calloop.GetQECam());
+      plist3.AddToList(&calloop.GetRelTimeCam());
+      plist3.AddToList(&calloop.GetBadPixels());
+      plist3.AddToList(&sigcam);
+      plist3.AddToList(&nphot);
+      plist3.AddToList(&nphotrms);
+      
+      
+      MCalibrateData::CalibrationMode_t calMode=MCalibrateData::kDefault;  
+      if(calflag==0)
+	calMode=MCalibrateData::kNone;
+      if(calflag==-1)
+	calMode=MCalibrateData::kDummy;
+      
+      //tasks
+      MReadMarsFile read3("Events");
+      static_cast<MRead&>(read3).AddFiles(pediter); 
+      read3.DisableAutoScheme();
+      
+      MGeomApply      geomapl;
+      MPedPhotCalc    photrmscalc; 
+      MCalibrateData  photcalc;
+      photcalc.SetCalibrationMode(calMode);
+      
+      tlist3.AddToList(&read3);
+      tlist3.AddToList(&geomapl);
+      tlist3.AddToList(extractor);
+      tlist3.AddToList(&photcalc);
+      tlist3.AddToList(&photrmscalc);
+      
+      // Create and setup the eventloop
+      MEvtLoop evtloop3;
+      evtloop3.SetParList(&plist3);
+      if (!evtloop3.Eventloop())
+	return;
+      
+      tlist3.PrintStatistics();
+      
+      /************************************************************************/
+      /*                FOURTH LOOP: DATA CALIBRATION INTO PHOTONS            */
+      /************************************************************************/
+      
+      MParList  plist4;
+      MTaskList tlist4;
+      plist4.AddToList(&tlist4);
+      
+      // containers 
+      MHillas       hillas;
+      MNewImagePar  newimagepar;
+      MSrcPosCam    source;
+      MRawRunHeader runhead;
+      
+      MArrivalTimeCam   timecam;
+      
+      
+      // islands
+      MIslands      isl;
+      MIslands      isl2;
+      MIslands      isl3;
+      
+      isl.SetName("MIslands");  
+      isl2.SetName("MIslands2");
+      isl3.SetName("MIslands3");
+      
+      plist4.AddToList(&timecam);
+      plist4.AddToList(&isl);
+      
+      if (islflag == 2)
+	plist4.AddToList(&isl2);
+      if (islflag == 3)
+	plist4.AddToList(&isl3);
+      
+      plist4.AddToList(&geomcam);
+      plist4.AddToList(&pedloop.GetPedestalCam());
+      plist4.AddToList(&calloop.GetCalibrationCam());
+      plist4.AddToList(&calloop.GetQECam());
+      plist4.AddToList(&calloop.GetRelTimeCam());
+      plist4.AddToList(&calloop.GetBadPixels());
+      plist4.AddToList(&nphot);
+      plist4.AddToList(&nphotrms);
+      plist4.AddToList(&source);
+      plist4.AddToList(&hillas);
+      plist4.AddToList(&newimagepar);
+      plist4.AddToList(&runhead);
+      
+      // cuts
+      MF cut(filter);
+      
+      //tasks
+      MReadMarsFile read4("Events");
+      static_cast<MRead&>(read4).AddFiles(datiter); 
+      read4.DisableAutoScheme();
+      
+      MImgCleanStd      clean(lcore,ltail);
+      clean.SetCleanRings(lrings);
+      MImgCleanStd::CleaningMethod_t cleanMeth= MImgCleanStd::kStandard;  
+      if(cleanflag==2)
+	cleanMeth=MImgCleanStd::kDemocratic;
+      clean.SetMethod(cleanMeth);
+      
+      MArrivalTimeCalc2 timecalc;
+      MIslandsCalc       island;
+      island.SetOutputName("MIslands");
+      island.SetAlgorithm(kalgorithm);
+      
+      MBadPixelsTreat   interpolatebadpixels;
+      interpolatebadpixels.SetUseInterpolation();
+      interpolatebadpixels.SetProcessPedestal();
+      //  interpolatebadpixels.SetSloppyTreatment();
+      
+      MIslandsClean      islclean(lnew);
+      islclean.SetInputName("MIslands");
+      islclean.SetMethod(kmethod);
+      
+      MIslandsCalc       island2;
+      island2.SetOutputName("MIslands2");  
+      island2.SetAlgorithm(kalgorithm);
+      
+      MIslandsCalc       island3;
+      island3.SetOutputName("MIslands3");  
+      
+      
+      MHillasCalc       hcalc;
+      MHillasSrcCalc    csrc1;
+      
+      MContinue applycut(&cut);
+      applycut.SetInverted(kTRUE);
+      MWriteRootFile write(outname,"RECREATE");
+      
+      MHillasDisplay*  disphillas=NULL;
+      
+      write.AddContainer("MHillas"        , "Parameters");
+      write.AddContainer("MHillasSrc"     , "Parameters");
+      write.AddContainer("MHillasExt"     , "Parameters");
+      write.AddContainer("MNewImagePar"   , "Parameters");
+      write.AddContainer("MRawEvtHeader"  , "Parameters");
+      write.AddContainer("MRawRunHeader"  , "Parameters");
+      write.AddContainer("MTime"          , "Parameters");
+      write.AddContainer("MConcentration" , "Parameters");
+      write.AddContainer("MSrcPosCam"     , "Parameters");
+      write.AddContainer("MIslands"       , "Parameters");
+      
+      if (islflag == 2) 
+	write.AddContainer("MIslands2" , "Parameters");
+      if (islflag == 3) 
+	write.AddContainer("MIslands3" , "Parameters");
+      
+      
+      if(display)
+	{
+	  disphillas = new MHillasDisplay(&nphot,&geomcam);
+	  disphillas->SetIslandsName("MIslands");
+	  if (islflag == 2)
+	    disphillas->SetIslandsName("MIslands2");
+	  if (islflag == 3)
+	    disphillas->SetIslandsName("MIslands3");
+	}      
+      
+      tlist4.AddToList(&read4);
+      tlist4.AddToList(&geomapl);
+      tlist4.AddToList(extractor);
+      tlist4.AddToList(&photcalc);
+      if(calflag==11 || calflag==21)
+	tlist4.AddToList(&interpolatebadpixels);
+      tlist4.AddToList(&clean);
+      tlist4.AddToList(&timecalc);
+      tlist4.AddToList(&island);
+      
+      if (islflag == 2)
+	{
+	  tlist4.AddToList(&islclean);
+	  tlist4.AddToList(&island2);
+	}
+      
+      if (islflag == 3)
+	{
+	  tlist4.AddToList(&islclean);
+	  tlist4.AddToList(&island3);
+	}
+      
+      tlist4.AddToList(&hcalc);
+      tlist4.AddToList(&csrc1);
+      if(filter.Length())
+	tlist4.AddToList(&applycut);
+      tlist4.AddToList(&write);
+      if(display)
+	{
+	  disphillas->SetPSFile();
+	  disphillas->SetPSFileName(psfilename);
+	  if(display==2) 
+	    disphillas->SetPause(kFALSE);	
+	  tlist4.AddToList(disphillas);
+	}
+      
+      // Create and setup the eventloop
+      MEvtLoop datloop;
+      datloop.SetParList(&plist4);
+      
+      cout << "*************************************************************" << endl;
+      cout << "***   COMPUTING DATA USING EXTRACTED SIGNAL (IN PHOTONS)  ***" << endl;
+      cout << "*************************************************************" << endl;
+      
+      if (!datloop.Eventloop(nmaxevents))
+	return;
+      
+      tlist4.PrintStatistics();    
+      delete extractor;
+    }
 }
-//-------------------------------------------------------------------------------
+  //-------------------------------------------------------------------------------
 
 Bool_t readDatacards(TString& filename)
@@ -450,5 +696,6 @@
 	    cout << "readDataCards Warning: adding pedestal runs to the existing list" << endl;
 	  ifun >> word;
-	  pediter.AddRuns(word.Data(),idirname.Data());
+	  if(strcmp(word.Data(),"0")!=0)
+	    pediter.AddRuns(word.Data(),idirname.Data());
 	}
 
@@ -530,4 +777,8 @@
 	}
       
+      // cleaning level
+      if(strcmp(word.Data(),"EXTRACTOR")==0)
+	ifun >> sext >> hifirst >> hilast >> lofirst >> lolast >> wsize;
+      
       if(strcmp(word.Data(),"ISLFLAG")==0)
 	{
@@ -549,4 +800,5 @@
 
   pediter.Reset();
+  pedcaliter.Reset();
   caliter.Reset();
   datiter.Reset();
@@ -557,10 +809,18 @@
   cout << "* Datacards read from file " << filename << endl;
   cout << "************************************************" << endl;
-  cout << "Pedestal file (s): "  << endl;
-  while(!(pfile=pediter.Next()).IsNull())
+  cout << "Pedestal file (s) for calibration: "  << endl;
+  while(!(pfile=pedcaliter.Next()).IsNull())
     cout << pfile << endl;
   cout << "Calibration file (s): "  << endl;
   while(!(pfile=caliter.Next()).IsNull())
     cout << pfile << endl;
+  if(pediter.GetNumRuns()>0)
+    {
+      cout << "Pedestal file (s): "  << endl;
+      while(!(pfile=pediter.Next()).IsNull())
+	cout << pfile << endl;
+    }
+  else 
+    cout << "Warning: Pedestals for data will be computed from data themselves" << endl;
   cout << "Data file (s): "  << endl;
   while(!(pfile=datiter.Next()).IsNull())
@@ -572,7 +832,8 @@
   if(display)
     cout << "Generating PS file: " << psfilename << endl;
+  cout << "Signal Extractor: " << chext[sext] << " with bounds ("<<hifirst<<","<<hilast<<","<<lofirst<<","<<lolast<<"), window size " << wsize << endl;
   cout << "Calibration: ";
   if(calflag==0)    
-    cout << "Pixel area proportional intercalibration" << endl;
+    cout << "Pixel area proportional intercalibration (kNone)" << endl;
   else if(calflag==-1)    
     cout << "No calibration whatsoever" << endl;
@@ -582,5 +843,5 @@
     cout << "Default calibration + bad pixels interpolation" << endl;
   cout << "Cleaning level: ("<<lcore<<","<<ltail<<") - " << lrings << "ring" << endl;
-  cout << "Cleaning methode: "<< cleanflag << endl;
+  cout << "Cleaning method: "<< cleanflag << endl;
   if (islflag == 1 || islflag == 2)
     cout << "Island calcultation..." << "using algorithm #" << kalgorithm <<endl;
@@ -591,7 +852,7 @@
   cout << "***********" << endl << endl;
   
-  if(!pediter.GetNumEntries())
-    {
-      cout << "No pedestal file name specified" << endl;
+  if(!pedcaliter.GetNumEntries())
+    {
+      cout << "No pedestal file for calibration specified" << endl;
       return kFALSE;
     }
