Index: trunk/MagicSoft/Mars/macros/AnalyseCT1.C
===================================================================
--- trunk/MagicSoft/Mars/macros/AnalyseCT1.C	(revision 1767)
+++ trunk/MagicSoft/Mars/macros/AnalyseCT1.C	(revision 1767)
@@ -0,0 +1,722 @@
+void AnalyseCT1()
+{
+    const char *offfile = "/hd43/rwagner/CT1/PreprocData/offdata.preproc"; 
+    const char *onfile = "/hd43/rwagner/CT1/PreprocData/mkn421_on.preproc"; 
+    //const char *mcfile = "/hd43/rwagner/CT1/PreprocData/mc_Gammas.preproc.0.6";
+    //const char *mcfile = "/hd43/rwagner/mkn421_mc.preproc";
+    const char *mcfile = "/hd43/rwagner/mkn421_mc_pedrms_0.001.preproc";
+    const char *filename;
+
+    Bool_t Data  = kFALSE;   // loop over data ?
+    Bool_t WLook = kFALSE;   // write out look-alike events for hadrons ?
+    Bool_t WHist = kFALSE;   // write out histograms for padding ?
+
+    Bool_t MC      = kTRUE;  // loop over MC gamma data ?
+    Bool_t RHist   = kTRUE;  // read in histograms for padding ?
+    Bool_t WLookMC = kTRUE;  // write out look-alike events for gammas ?
+
+
+    cout << "" << endl;
+    cout << "Macro AnalyseCT1 : Data, WHist = " << Data << ",  "
+         << WHist << endl;
+
+    cout << "                         RHist, MC   = " << RHist << ",  "
+         << MC << endl;
+    cout << "" << endl;
+
+  //---------------------------------------------------------------------
+  // start of section for ON data
+  // (in the CT1 analysis the ON data are also used as a sample representing
+  //  the hadrons for the g/h separation)  
+
+    // read ON data file 
+
+    //  - to produce the 2D-histogram "sigmabar versus Theta"
+    //    (to be used for the padding of the MC gamma data)
+
+    //  - to write a file of look-alike events (for hadrons)
+    //    (to be used later together with the corresponding MC gamma file
+    //     for the g/h separation in the analysis of the ON data)
+
+    //  - to write a file of ON events (after the standard cuts, 
+    //                                  before the g/h separation)
+    //    (to be used together with the corresponding MC gamma file
+    //     for the optimization of the g/h separation)
+
+ if (Data)
+ {
+    cout << "=====================================================" << endl;
+    cout << "Macro AnalyseCT1 : Start of section for ON data" << endl;
+
+    MTaskList tliston;
+    MParList pliston;
+    pliston.AddToList(&tliston);
+
+
+    //::::::::::::::::::::::::::::::::::::::::::
+
+    MBinning binssize("BinningSize");
+    binssize.SetEdgesLog(50, 10, 1.0e5);
+    pliston.AddToList(&binssize);
+
+    MBinning binsdistc("BinningDist");
+    binsdistc.SetEdges(50, 0, 1.4);
+    pliston.AddToList(&binsdistc);
+
+    MBinning binswidth("BinningWidth");
+    binswidth.SetEdges(50, 0, 1.0);
+    pliston.AddToList(&binswidth);
+
+    MBinning binslength("BinningLength");
+    binslength.SetEdges(50, 0, 1.0);
+    pliston.AddToList(&binslength);
+
+    MBinning binsalpha("BinningAlpha");
+    binsalpha.SetEdges(100, -100, 100);
+    pliston.AddToList(&binsalpha);
+
+    MBinning binsasym("BinningAsym");
+    binsasym.SetEdges(50, -1.5, 1.5);
+    pliston.AddToList(&binsasym);
+
+    MBinning binsht("BinningHeadTail");
+    binsht.SetEdges(50, -1.5, 1.5);
+    pliston.AddToList(&binsht);
+
+    MBinning binsm3l("BinningM3Long");
+    binsm3l.SetEdges(50, -1.5, 1.5);
+    pliston.AddToList(&binsm3l);
+
+    MBinning binsm3t("BinningM3Trans");
+    binsm3t.SetEdges(50, -1.5, 1.5);
+    pliston.AddToList(&binsm3t);
+
+   
+    //.....
+    MBinning binsb("BinningSigmabar");
+    binsb.SetEdges( 100,  0.0,  5.0);
+    pliston.AddToList(&binsb);
+
+    MBinning binth("BinningTheta");
+    binth.SetEdges( 70, -0.5, 69.5);    
+    pliston.AddToList(&binth);
+
+    MBinning binsdiff("BinningDiffsigma2");
+    binsdiff.SetEdges(100, -5.0, 20.0);
+    pliston.AddToList(&binsdiff);
+    //::::::::::::::::::::::::::::::::::::::::::
+
+
+    //-------------------------------------------
+    // create the tasks which should be executed 
+    //
+
+    filename = onfile;
+    readname = "ReadCT1data";
+    MCT1ReadPreProc read(filename, readname);
+
+    MSelBasic selbasic;
+
+    MBlindPixelCalc blind;
+    blind.SetUseInterpolation();
+    blind.SetUseBlindPixels();
+    // blind.SetUseCetralPixel();
+
+    // create an object of class MSigmabar, 
+    // because class MHSigmaTheta will look for it
+    MSigmabar sigbar;
+    pliston.AddToList(&sigbar);
+    MFillH fillsigtheta ("SigmaTheta[MHSigmaTheta]", "MMcEvt");
+    fillsigtheta.SetTitle("Task to make 2D plot Sigmabar vs Theta");
+
+    MImgCleanStd    clean; 
+
+    // calculate also extended image parameters
+    TString fHilName = "Hillas";
+    MHillasExt hext(fHilName);
+    pliston.AddToList(&hext);
+    MHillasExt *fHillas = &hext;
+
+    // name of output container for MHillasCalc
+    //      = name of MHillas object
+    MHillasCalc    hcalc(fHilName);
+
+    // name of output container for MHillasSrcCalc
+    //      = name of MHillasSrc object
+    TString fHilSrcName = "HillasSrc";
+    MHillasSrc hilsrc(fHilSrcName);
+    MHillasSrc *fHillasSrc = &hilsrc;
+    pliston.AddToList(&hilsrc);
+    MHillasSrcCalc csrc1("MSrcPosCam", fHilSrcName, "MHillas", "", fHilName);
+
+    MSelStandard selstand(fHillas, fHillasSrc);
+
+    MFillH hfill1("MHHillas",    fHilName);
+    MFillH hfill2("MHStarMap",   fHilName);
+
+    TString nxt = "HillasExt";
+    MHHillasExt hhilext(nxt, "", fHilName);
+    pliston.AddToList(&hhilext); 
+    TString namext = nxt;
+    namext += "[MHHillasExt]";
+    MFillH hfill3(namext, fHilSrcName);
+
+    MFillH hfill4("MHHillasSrc", fHilSrcName);
+
+    //+++++++++++++++++++++++++++++++++++++++++++++++++++
+    // prepare writing of look-alike events (for hadrons)
+
+    const char* mtxName = "MatrixHadrons";
+    Int_t howMany = 30000;
+    char* outName = "matrix_hadrons.root";
+
+    cout << "" << endl;
+    cout << "========================================================" << endl;
+    cout << "Matrix for (hadrons)" << endl;
+    cout << "input file                    = " << filename<< endl;
+    cout << "matrix name                   = " << mtxName << endl;
+    cout << "max. no.of look-alike events  = " << howMany << endl;
+    cout << "name of output root file      = " << outName << endl;
+    cout << "" << endl;
+
+
+    // matrix limitation for look-alike events (approximate number)
+    MFEventSelector selector("", "", readname);
+    selector.SetNumSelectEvts(howMany);
+    MFilterList flist;
+    flist.AddToList(&selector);
+
+    //
+    // --- setup the matrix and add it to the parlist
+    //
+    MHMatrix *pmatrix = new MHMatrix(mtxName);
+    MHMatrix& matrix = *pmatrix;
+    matrix.AddColumn("Hillas.fWidth");
+    matrix.AddColumn("Hillas.fLength");
+    matrix.AddColumn("Hillas.fWidth*Hillas.fLength/Hillas.fSize");
+    matrix.AddColumn("abs(Hillas.fAsym)");
+    matrix.AddColumn("abs(Hillas.fM3Long)");
+    matrix.AddColumn("abs(Hillas.fM3Trans)");
+    matrix.AddColumn("abs(HillasSrc.fHeadTail)");
+    matrix.AddColumn("Hillas.fConc");
+    matrix.AddColumn("Hillas.fConc1");
+    matrix.AddColumn("HillasSrc.fDist");
+    matrix.AddColumn("log10(Hillas.fSize)");
+    matrix.AddColumn("HillasSrc.fAlpha");
+    matrix.AddColumn("cos(MMcEvt.fTelescopeTheta)");
+    //matrix.AddColumn("MMcEvt.fTheta");
+    pliston.AddToList(pmatrix);
+
+    MFillH fillmatg(mtxName);
+    fillmatg.SetFilter(&flist);
+
+    //+++++++++++++++++++++++++++++++++++++++++++++++++++
+
+    MSelFinal selfinal(fHillas, fHillasSrc);
+
+    //*****************************
+    // tasks to be executed
+    
+    tliston.AddToList(&read);
+
+    tliston.AddToList(&selbasic);
+    tliston.AddToList(&blind);
+    tliston.AddToList(&fillsigtheta);
+    tliston.AddToList(&clean);
+
+    tliston.AddToList(&hcalc);
+    tliston.AddToList(&csrc1);
+
+    tliston.AddToList(&selstand);
+    tliston.AddToList(&hfill1);
+    tliston.AddToList(&hfill2);
+    tliston.AddToList(&hfill3);
+    tliston.AddToList(&hfill4);
+
+    tliston.AddToList(&flist);
+    tliston.AddToList(&fillmatg);
+
+    //tliston.AddToList(&selfinal);
+    //*****************************
+
+    //-------------------------------------------
+    // Execute event loop
+    //
+    MEvtLoop evtloop;
+    evtloop.SetParList(&pliston);
+
+    Int_t maxevents = 1000000000;
+    if ( !evtloop.Eventloop(maxevents) )
+        return;
+
+    tliston.PrintStatistics(0, kTRUE);
+
+    //-------------------------------------------
+    // Display the histograms
+    //
+    pliston.FindObject("MHHillas")->DrawClone();
+    pliston.FindObject("MHHillasSrc")->DrawClone();
+
+    //pliston.FindObject("MHHillasExt")->DrawClone();
+    pliston.FindObject(nxt)->DrawClone();
+
+    pliston.FindObject("MHStarMap")->DrawClone();
+
+
+    //^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
+    //
+    // ----------------------------------------------------------
+    //  Definition of the reference sample of look-alike events
+    //  (this is a subsample of the original sample)
+    // ----------------------------------------------------------
+    //
+    cout << "" << endl;
+    cout << "========================================================" << endl;
+    // Select a maximum of nmaxevts events from the sample of look-alike 
+    // events. They will form the reference sample.
+    Int_t nmaxevts  = 2000;
+
+    // target distribution for the variable in column refcolumn;    
+    // set refcolumn negative if distribution is not to be changed
+    Int_t   refcolumn = 12;
+    Int_t   nbins   =  10;
+    Float_t frombin = 0.5;
+    Float_t tobin   = 1.0;
+    TH1F *thsh = new TH1F("thsh","target distribution", 
+                           nbins, frombin, tobin);
+    thsh->SetXTitle("cos( \\Theta)");
+    thsh->SetYTitle("Counts");
+    Float_t dbin = (tobin-frombin)/nbins;
+    Float_t lbin = frombin +dbin*0.5;
+    for (Int_t j=1; j<=nbins; j++) 
+    {
+      thsh->Fill(lbin,1.0);
+      lbin += dbin;
+    }
+
+    cout << "" << endl;
+    cout << "========================================================" << endl;
+    cout << "Macro AnalyseCT1 : define reference sample for hadrons" << endl; 
+    cout << "Macro AnalyseCT1 : Parameters for reference sample : refcolumn, nmaxevts = "
+         << refcolumn << ",  " << nmaxevts << endl;    
+
+    if ( !matrix.DefRefMatrix(refcolumn, thsh, nmaxevts) ) 
+    {
+      cout << "Macro AnalyseCT1 : Reference matrix for hadrons cannot be defined" << endl;
+      return;
+    }
+    //^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
+
+    //-------------------------------------------
+    // write out look-alike events (for hadrons)
+    //
+    if (WLook)
+    {
+      TFile *writejens = new TFile(outName, "RECREATE", "");
+      matrix.Write();
+      cout << "Macro AnalyseCT1 : matrix of look-alike events for hadrons written onto file "
+           << outName << endl;
+
+      writejens->Close();
+      delete writejens;
+    }
+
+    //-------------------------------------------
+    // Write histograms onto a file
+  if (WHist)
+  {
+      MHSigmaTheta *sigtheta = 
+            (MHSigmaTheta*)pliston.FindObject("SigmaTheta");
+      if (!sigtheta)
+	{
+          *fLog << "Object 'SigmaTheta' not found" << endl;
+          return;
+	}
+      TH2D *fHSigTh    = sigtheta->GetSigmaTheta();
+      TH3D *fHSigPixTh = sigtheta->GetSigmaPixTheta();
+      TH3D *fHDifPixTh = sigtheta->GetDiffPixTheta();
+
+      TFile outfile("SigmaTheta.root", "RECREATE");
+      fHSigTh->Write();
+      fHSigPixTh->Write();
+      fHDifPixTh->Write();
+      outfile.Close();
+     
+      cout << "File 'SigmaTheta.root' was written out" << endl;
+  }
+
+
+    cout << "Macro AnalyseCT1 : End of section for ON data" << endl;
+    cout << "===================================================" << endl;
+ }
+  //---------------------------------------------------------------------
+
+  //---------------------------------------------------------------------
+  // start of section for MC gamma data
+
+    // read MC gamma data  
+
+    //    - to pad them
+    //      (using the 2D-histogram "sigmabar versus Theta" of the OFF data)
+
+    //    - to write a file of look-alike events (for gammas)
+    //      (to be used later together with the corresponding hadron file
+    //       for the g/h separation in the analysis of the ON data)
+
+    //    - to write a file of padded MC gamma events 
+    //      (after the standard cuts, before the g/h separation)
+    //      (to be used together with the corresponding hadron file
+    //       for the optimization of the g/h separation)
+
+    //    - to write a file of padded MC gamma events (after the final cuts) 
+    //      (to be used for the optimization of the energy estimator
+    //       and for calculating the effective collection areas)
+
+ if (MC)
+ {
+    cout << "=============================================================" 
+         << endl;
+    cout << "Macro : AnalyseCT1 : Start of section for MC gamma data" 
+         << endl;
+
+    MTaskList tlist;
+    MParList plist;
+
+    plist.AddToList(&tlist);
+
+    //------------------------------------
+    // Get the histograms "2D-ThetaSigmabar"
+    // and                "3D-ThetaPixSigma"
+    // and                "3D-ThetaPixDiff"
+  if (RHist)
+  {
+      cout << "Reading in file 'SigmaTheta.root' " << endl;
+
+      TFile *infile = new TFile("SigmaTheta.root");
+      infile->ls();
+
+      TH2D *fHSigmaTheta = 
+      (TH2D*) gROOT.FindObject("2D-ThetaSigmabar");
+      if (!fHSigmaTheta)
+	{
+          *fLog << "Object '2D-ThetaSigmabar' not found on root file" << endl;
+          return;
+	}
+      cout << "Object '2D-ThetaSigmabar' was read in" << endl;
+
+      TH3D *fHSigmaPixTheta = 
+      (TH3D*) gROOT.FindObject("3D-ThetaPixSigma");
+      if (!fHSigmaPixTheta)
+	{
+          *fLog << "Object '3D-ThetaPixSigma' not found on root file" << endl;
+          return;
+	}
+      cout << "Object '3D-ThetaPixSigma' was read in" << endl;
+
+      TH3D *fHDiffPixTheta = 
+      (TH3D*) gROOT.FindObject("3D-ThetaPixDiff");
+      if (!fHSigmaPixTheta)
+	{
+          *fLog << "Object '3D-ThetaPixDiff' not found on root file" << endl;
+          return;
+	}
+      cout << "Object '3D-ThetaPixDiff' was read in" << endl;
+  }
+  else
+  {
+      MHSigmaTheta *sigtheta = (MHSigmaTheta*)pliston.FindObject("SigmaTheta");
+      if (!sigtheta)
+      {
+        cout << "Object with name 'SigmaTheta' not found" << endl;
+        return;
+      }
+
+      TH2D *fHSigmaTheta    = sigtheta->GetSigmaTheta();
+      TH3D *fHSigmaPixTheta = sigtheta->GetSigmaPixTheta();
+      TH3D *fHDiffPixTheta  = sigtheta->GetDiffPixTheta();
+  }
+    //------------------------------------
+
+
+
+    //::::::::::::::::::::::::::::::::::::::::::
+
+    MBinning binssize("BinningSize");
+    binssize.SetEdgesLog(50, 10, 1.0e5);
+    plist.AddToList(&binssize);
+
+    MBinning binsdistc("BinningDist");
+    binsdistc.SetEdges(50, 0, 1.4);
+    plist.AddToList(&binsdistc);
+
+    MBinning binswidth("BinningWidth");
+    binswidth.SetEdges(50, 0, 1.0);
+    plist.AddToList(&binswidth);
+
+    MBinning binslength("BinningLength");
+    binslength.SetEdges(50, 0, 1.0);
+    plist.AddToList(&binslength);
+
+    MBinning binsalpha("BinningAlpha");
+    binsalpha.SetEdges(100, -100, 100);
+    plist.AddToList(&binsalpha);
+
+    MBinning binsasym("BinningAsym");
+    binsasym.SetEdges(50, -1.5, 1.5);
+    plist.AddToList(&binsasym);
+
+    MBinning binsht("BinningHeadTail");
+    binsht.SetEdges(50, -1.5, 1.5);
+    plist.AddToList(&binsht);
+
+    MBinning binsm3l("BinningM3Long");
+    binsm3l.SetEdges(50, -1.5, 1.5);
+    plist.AddToList(&binsm3l);
+
+    MBinning binsm3t("BinningM3Trans");
+    binsm3t.SetEdges(50, -1.5, 1.5);
+    plist.AddToList(&binsm3t);
+
+   
+    //.....
+    MBinning binsb("BinningSigmabar");
+    binsb.SetEdges( 100,  0.0,  5.0);
+    plist.AddToList(&binsb);
+
+    MBinning binth("BinningTheta");
+    binth.SetEdges( 70, -0.5, 69.5);    
+    plist.AddToList(&binth);
+
+    MBinning binsdiff("BinningDiffsigma2");
+    binsdiff.SetEdges(100, -5.0, 20.0);
+    plist.AddToList(&binsdiff);
+
+    //::::::::::::::::::::::::::::::::::::::::::
+
+    //-------------------------------------------
+    // create the tasks which should be executed 
+    //
+    filename = mcfile;
+    readname = "ReadCT1MC";
+    MCT1ReadPreProc read(filename, readname);
+
+    MSelBasic selbasic;
+   
+    MBlindPixelCalc blind;
+    blind.SetUseInterpolation();
+    blind.SetUseBlindPixels();
+    // blind.SetUseCetralPixel();
+    
+    // There are 2 options for Thomas Schweizer's padding
+    //     fPadFlag = 1   get Sigmabar from fHSigmaTheta
+    //                    and Sigma    from fHDiffPixTheta
+    //     fPadFlag = 2   get Sigma    from fHSigmaPixTheta
+    
+    MPadSchweizer padthomas("MPadSchweizer","Task for the padding (Schweizer)",
+                             fHSigmaTheta, fHSigmaPixTheta, fHDiffPixTheta);
+    padthomas.SetPadFlag(1);
+
+    //...........................................
+
+    MImgCleanStd    clean; //(0, 0);
+
+    // calculate also extended image parameters
+    TString fHilName = "Hillas";
+    MHillasExt hext(fHilName);
+    plist.AddToList(&hext);
+    MHillasExt *fHillas = &hext;
+
+    // name of output container for MHillasCalc
+    //      = name of MHillas object
+    MHillasCalc    hcalc(fHilName);
+
+    // name of output container for MHillasSrcCalc
+    //      = name of MHillasSrc object
+    TString fHilSrcName = "HillasSrc";
+    MHillasSrc hilsrc(fHilSrcName);
+    MHillasSrc *fHillasSrc = &hilsrc;
+    plist.AddToList(&hilsrc);
+    MHillasSrcCalc csrc1("MSrcPosCam", fHilSrcName, "MHillas", "", fHilName);
+
+    MSelStandard selstand(fHillas, fHillasSrc);
+
+    MFillH hfill1("MHHillas",    fHilName);
+    MFillH hfill2("MHStarMap",   fHilName);
+
+    TString nxt = "HillasExt";
+    MHHillasExt hhilext(nxt, "", fHilName);
+    plist.AddToList(&hhilext); 
+    TString namext = nxt;
+    namext += "[MHHillasExt]";
+    MFillH hfill3(namext, fHilSrcName);
+
+    MFillH hfill4("MHHillasSrc", fHilSrcName);
+
+    //+++++++++++++++++++++++++++++++++++++++++++++++++++
+    // prepare writing of look-alike events (for gammas)
+
+    const char* mtxName = "MatrixGammas";
+    Int_t howMany = 30000;
+    char* outName = "matrix_gammas.root";
+
+    cout << "" << endl;
+    cout << "========================================================" << endl;
+    cout << "Matrix for (gammas)" << endl;
+    cout << "input file                    = " << filename<< endl;
+    cout << "matrix name                   = " << mtxName << endl;
+    cout << "max. no.of look-alike events  = " << howMany << endl;
+    cout << "name of output root file      = " << outName << endl;
+    cout << "" << endl;
+
+
+    // matrix limitation for look-alike events (approximate number)
+    MFEventSelector selector("", "", readname);
+    selector.SetNumSelectEvts(howMany);
+    MFilterList flist;
+    flist.AddToList(&selector);
+
+    //
+    // --- setup the matrix and add it to the parlist
+    //
+    MHMatrix *pmatrix = new MHMatrix(mtxName);
+    MHMatrix& matrix = *pmatrix;
+    matrix.AddColumn("Hillas.fWidth");
+    matrix.AddColumn("Hillas.fLength");
+    matrix.AddColumn("Hillas.fWidth*Hillas.fLength/Hillas.fSize");
+    matrix.AddColumn("abs(Hillas.fAsym)");
+    matrix.AddColumn("abs(Hillas.fM3Long)");
+    matrix.AddColumn("abs(Hillas.fM3Trans)");
+    matrix.AddColumn("abs(HillasSrc.fHeadTail)");
+    matrix.AddColumn("Hillas.fConc");
+    matrix.AddColumn("Hillas.fConc1");
+    matrix.AddColumn("HillasSrc.fDist");
+    matrix.AddColumn("log10(Hillas.fSize)");
+    matrix.AddColumn("HillasSrc.fAlpha");
+    matrix.AddColumn("cos(MMcEvt.fTelescopeTheta)");
+    //matrix.AddColumn("MMcEvt.fTheta");
+    plist.AddToList(pmatrix);
+
+    MFillH fillmatg(mtxName);
+    fillmatg.SetFilter(&flist);
+
+    //+++++++++++++++++++++++++++++++++++++++++++++++++++
+
+
+    MSelFinal selfinal(fHillas, fHillasSrc);
+
+    //*****************************
+    // tasks to be executed
+
+    tlist.AddToList(&read);
+
+    tlist.AddToList(&selbasic);
+    tlist.AddToList(&blind);
+    tlist.AddToList(&padthomas);
+    tlist.AddToList(&clean);
+
+    tlist.AddToList(&hcalc);
+    tlist.AddToList(&csrc1);
+
+    tlist.AddToList(&selstand);
+    tlist.AddToList(&hfill1);
+    tlist.AddToList(&hfill2);
+    tlist.AddToList(&hfill3);
+    tlist.AddToList(&hfill4);
+
+    tlist.AddToList(&flist);
+    tlist.AddToList(&fillmatg);
+
+    //tlist.AddToList(&selfinal);
+    //*****************************
+
+
+    //-------------------------------------------
+    // Execute event loop
+    //
+    MEvtLoop evtloop;
+    evtloop.SetParList(&plist);
+
+    Int_t maxevents = 1000000000;
+    if ( !evtloop.Eventloop(maxevents) )
+        return;
+
+    tlist.PrintStatistics(0, kTRUE);
+
+    //-------------------------------------------
+    // Display the histograms
+    //
+    plist.FindObject("MHHillas")->DrawClone();
+    plist.FindObject("MHHillasSrc")->DrawClone();
+
+    //plist.FindObject("MHHillasExt")->DrawClone();
+    plist.FindObject(nxt)->DrawClone();
+
+    plist.FindObject("MHStarMap")->DrawClone();
+
+
+    //^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
+    //
+    // ----------------------------------------------------------
+    //  Definition of the reference sample of look-alike events
+    //  (this is a subsample of the original sample)
+    // ----------------------------------------------------------
+    //
+    cout << "" << endl;
+    cout << "========================================================" << endl;
+    // Select a maximum of nmaxevts events from the sample of look-alike 
+    // events. They will form the reference sample.
+    Int_t nmaxevts  = 2000;
+
+    // target distribution for the variable in column refcolumn;    
+    // set refcolumn negative if distribution is not to be changed
+    Int_t   refcolumn = 12;
+    Int_t   nbins   =  10;
+    Float_t frombin = 0.5;
+    Float_t tobin   = 1.0;
+    TH1F *thsh = new TH1F("thsh","target distribution", 
+                           nbins, frombin, tobin);
+    Float_t dbin = (tobin-frombin)/nbins;
+    Float_t lbin = frombin +dbin*0.5;
+    for (Int_t j=1; j<=nbins; j++) 
+    {
+      thsh->Fill(lbin,1.0);
+      lbin += dbin;
+    }
+
+    cout << "" << endl;
+    cout << "========================================================" << endl;
+    cout << "Macro AnalyseCT1 : define reference sample for gammas" << endl; 
+    cout << "Macro AnalyseCT1 : Parameters for reference sample : refcolumn, nmaxevts = "
+         << refcolumn << ",  " << nmaxevts << endl;    
+
+    if ( !matrix.DefRefMatrix(refcolumn, thsh, nmaxevts) ) 
+    {
+      cout << "Macro AnalyseCT1 : Reference matrix for gammas cannot be defined" << endl;
+      return;
+    }
+    //^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
+
+    //-------------------------------------------
+    // write out look-alike events (for gammas)
+    //
+    if (WLookMC)
+    {
+      TFile *writejens = new TFile(outName, "RECREATE", "");
+      matrix.Write();
+      cout << "Macro AnalyseCT1 : matrix of look-alike events for gammas written onto file "
+           << outName << endl;
+
+      writejens->Close();
+      delete writejens;
+    }
+
+    cout << "Macro AnalyseCT1 : End of section for MC gamma data" 
+         << endl;
+    cout << "=========================================================" 
+         << endl;
+ }
+}
+
+
+
+
Index: trunk/MagicSoft/Mars/manalysis/MSelFinal.cc
===================================================================
--- trunk/MagicSoft/Mars/manalysis/MSelFinal.cc	(revision 1767)
+++ trunk/MagicSoft/Mars/manalysis/MSelFinal.cc	(revision 1767)
@@ -0,0 +1,161 @@
+/* ======================================================================== *\
+!
+! *
+! * This file is part of MARS, the MAGIC Analysis and Reconstruction
+! * Software. It is distributed to you in the hope that it can be a useful
+! * and timesaving tool in analysing Data of imaging Cerenkov telescopes.
+! * It is distributed WITHOUT ANY WARRANTY.
+! *
+! * Permission to use, copy, modify and distribute this software and its
+! * documentation for any purpose is hereby granted without fee,
+! * provided that the above copyright notice appear in all copies and
+! * that both that copyright notice and this permission notice appear
+! * in supporting documentation. It is provided "as is" without express
+! * or implied warranty.
+! *
+!
+!
+!   Author(s): Wolfgang Wittek  02/2003 <wittek@mppmu.mpg.de>
+!
+!   Copyright: MAGIC Software Development, 2000-2003
+!
+!
+\* ======================================================================== */
+
+/////////////////////////////////////////////////////////////////////////////
+//                                                                         //
+//  MSelFinal                                                              //
+//                                                                         //
+//  This is a task to evaluate the Final Cuts                              //
+//  (these cuts define the final sample of gammas;                         //
+//   relevant for the calculation of the effective collection areas)       //
+//                                                                         //
+//  to be called after the calculation of the hadroness                    //
+//                                                                         //
+/////////////////////////////////////////////////////////////////////////////
+
+#include "math.h"
+#include "MSelFinal.h"
+
+#include "MParList.h"
+
+#include "MHillas.h"
+#include "MHillasSrc.h"
+#include "MCerPhotEvt.h"
+#include "MMcEvt.hxx"
+#include "MGeomCam.h"
+#include "MGeomPix.h"
+#include "MHadronness.h"
+
+#include "MLog.h"
+#include "MLogManip.h"
+
+ClassImp(MSelFinal);
+
+// --------------------------------------------------------------------------
+//
+// Default constructor.
+//
+MSelFinal::MSelFinal(const MHillas *parhil, const MHillasSrc *parhilsrc,
+                           const char *name, const char *title)
+{
+    fName  = name  ? name  : "MSelFinal";
+    fTitle = title ? title : "Task to evaluate the Final Cuts";
+
+    fHil    = parhil;
+    fHilsrc = parhilsrc;
+}
+
+// --------------------------------------------------------------------------
+//
+// 
+// 
+// 
+//
+Bool_t MSelFinal::PreProcess(MParList *pList)
+{
+    fHadronness = (MHadronness*)pList->FindObject("MHadronness");
+    if (!fHadronness)
+    {
+      *fLog << dbginf << "MHadronness not found... aborting." << endl;
+      return kFALSE;
+    }
+
+
+    fMcEvt = (MMcEvt*)pList->FindObject("MMcEvt");
+    if (!fMcEvt)
+    {
+        *fLog << dbginf << "MMcEvt not found... aborting." << endl;
+        return kFALSE;
+    }
+
+    fEvt = (MCerPhotEvt*)pList->FindObject("MCerPhotEvt");
+    if (!fEvt)
+    {
+        *fLog << dbginf << "MCerPhotEvt not found... aborting." << endl;
+        return kFALSE;
+    }
+
+
+    fCam = (MGeomCam*)pList->FindObject("MGeomCam");
+    if (!fCam)
+    {
+        *fLog << dbginf << "MGeomCam (Camera Geometry) missing in Parameter List... aborting." << endl;
+        return kFALSE;
+    }
+    fMm2Deg = fCam->GetConvMm2Deg();
+
+    *fLog << "fMm2Deg = " << fMm2Deg << endl;
+
+    memset(fErrors, 0, sizeof(fErrors));
+
+    return kTRUE;
+}
+
+// --------------------------------------------------------------------------
+//
+// Evaluate final cuts
+// 
+// if cuts are fulfilled      : return 0
+// if they are not fullfilled : skip the remaining tasks for this event
+//
+Bool_t MSelFinal::Process()
+{
+    Int_t rc = 0;
+
+    Double_t alphacut = 20.0;
+
+    Double_t modalpha = fabs( fHilsrc->GetAlpha() ); 
+    Double_t h = fHadronness->GetHadronness();
+
+    if ( h>0.5  ||  modalpha > alphacut )
+    {
+      *fLog << "MSelFinal::Process; h, alpha = " << h << ",  " 
+            << fHilsrc->GetAlpha() << endl;
+      rc = 1;
+    }    
+
+    fErrors[rc]++;
+
+    return rc==0 ? kTRUE : kCONTINUE;
+}
+
+// --------------------------------------------------------------------------
+//
+//  Prints some statistics about the Final selections.
+//
+Bool_t MSelFinal::PostProcess()
+{
+    if (GetNumExecutions()==0)
+        return kTRUE;
+
+    *fLog << inf << endl;
+    *fLog << GetDescriptor() << " execution statistics:" << endl;
+    *fLog << dec << setfill(' ');
+    *fLog << " " << setw(7) << fErrors[1] << " (" << setw(3) << (int)(fErrors[1]*100/GetNumExecutions()) << "%) Evts skipped due to: Final selections are not fullfilled" << endl;
+
+    *fLog << " " << fErrors[0] << " (" << (int)(fErrors[0]*100/GetNumExecutions()) << "%) Evts survived Final selections!" << endl;
+    *fLog << endl;
+
+    return kTRUE;
+}
Index: trunk/MagicSoft/Mars/manalysis/MSelFinal.h
===================================================================
--- trunk/MagicSoft/Mars/manalysis/MSelFinal.h	(revision 1767)
+++ trunk/MagicSoft/Mars/manalysis/MSelFinal.h	(revision 1767)
@@ -0,0 +1,59 @@
+#ifndef MARS_MSelFinal
+#define MARS_MSelFinal
+
+/////////////////////////////////////////////////////////////////////////////
+//                                                                         //
+// MSelFinal                                                               //
+//                                                                         //
+// Task to evaluate final cuts                                             //
+//                                                                         //
+/////////////////////////////////////////////////////////////////////////////
+
+#ifndef MARS_MTask
+#include "MTask.h"
+#endif
+
+class MGeomCam;
+class MCerPhotEvt;
+class MHillas;
+class MHillasSrc;
+class MMcEvt;
+class MHadronness;
+
+class MSelFinal : public MTask
+{
+private:
+    const MGeomCam    *fCam;      // Camera Geometry 
+    const MCerPhotEvt *fEvt;      // Cerenkov Photon Event 
+    const MMcEvt      *fMcEvt;       
+    const MHillas     *fHil;       
+    const MHillasSrc  *fHilsrc;       
+    const MHadronness *fHadronness;       
+
+          Double_t     fMm2Deg;   // conversion mm to degrees in camera
+          Int_t        fErrors[2];
+
+public:
+    MSelFinal(const MHillas *fHil, const MHillasSrc *fHilsrc,
+              const char *name=NULL, const char *title=NULL);
+
+    Bool_t PreProcess(MParList *pList);
+    Bool_t Process();
+    Bool_t PostProcess();
+
+
+    ClassDef(MSelFinal, 0)   // Task to evaluate final cuts
+};
+
+#endif
+
+
+
+
+
+
+
+
+
+
+
