Index: trunk/MagicSoft/Mars/mtemp/mifae/macros/alpha_plot.C
===================================================================
--- trunk/MagicSoft/Mars/mtemp/mifae/macros/alpha_plot.C	(revision 4408)
+++ trunk/MagicSoft/Mars/mtemp/mifae/macros/alpha_plot.C	(revision 4429)
@@ -25,12 +25,7 @@
 }
 
-//void alpha_plot(TString f_on_name  = "../HillasFiles/20040319_Mrk421_30_15.KDummy*.root",
-void alpha_plot(TString f_on_name  = "../HillasFiles/20040319_OffMrk421_30_15.KDummy*.root",
-		TString f_off_name = "../HillasFiles/20040319_OffMrk421_30_15.KDummy*.root",
+void alpha_plot(TString f_on_name  = "../HillasFiles/Mrk421/*_H.root",
+		TString f_off_name = "../HillasFiles/OffMrk421/*_H.root",
 		TString f_src_name = "../HillasFiles/20040319_OffMrk421.fake.pos")	     
-/*void alpha_plot(TString f_on_name  = "../HillasFiles/mrk421OnMisp3015*.root",
-		TString f_off_name = "../HillasFiles/mrk421OffMisp3015*.root",
-		TString f_src_name = "../HillasFiles/20040215_Mrk421.pos")
-*/
 {
 
@@ -38,12 +33,12 @@
     
     //cuts
-    Float_t sizemin   = 500.; //[ADC]
+    Float_t sizemin   = 2000.; //[ADC]
     Float_t sizemax   = 10000000000.; //[ADC]
-    Float_t widthmin  = 0.; 
-    Float_t widthmax  = 0.8;
-    Float_t lengthmin = 0.; 
-    Float_t lengthmax = 0.8;
-    Float_t distmin   = 0.; 
-    Float_t distmax   = 2.;
+    Float_t widthmin  = 0.06; 
+    Float_t widthmax  = 0.12;
+    Float_t lengthmin = 0.10; 
+    Float_t lengthmax = 0.26;
+    Float_t distmin   = 0.3; 
+    Float_t distmax   = 1.2;
     Float_t alphamin   = 0.; 
     Float_t alphamax   = 90.;
@@ -52,5 +47,5 @@
     Float_t sigexccmin = 0.;
     Float_t sigexccmax = 30.;
-    Float_t bkgnormmin = 0.;
+    Float_t bkgnormmin = 30.;
     Float_t bkgnormmax = 90.;
     
@@ -173,37 +168,42 @@
 
     //cuts
-    TString sizestr = "MHillas.fSize < ";
+    TString sizestr = "(MHillas.fSize < ";
     sizestr += sizemin;
-    sizestr += " || ";
+    sizestr += ") || (";
     sizestr += "MHillas.fSize > ";
     sizestr += sizemax;
+    sizestr += ")";
     MF sizefilter(sizestr);
     
-    TString widthstr = "{MHillas.fWidth/315.} < ";
+    TString widthstr = "({MHillas.fWidth/315.} < ";
     widthstr += widthmin;
-    widthstr += " || ";
+    widthstr += ") || (";
     widthstr += "{MHillas.fWidth/315.} > ";
     widthstr += widthmax;
+    widthstr += ")";
     MF widthfilter(widthstr);
     
-    TString lengthstr = "{MHillas.fLength/315.} < ";
+    TString lengthstr = "({MHillas.fLength/315.} < ";
     lengthstr += lengthmin;
-    lengthstr += " || ";
+    lengthstr += ") || (";
     lengthstr += "{MHillas.fLength/315.} > ";
     lengthstr += lengthmax;
+    lengthstr += ")";
     MF lengthfilter(lengthstr);
     
-    TString diststr = "{MHillasSrc.fDist/315.} < ";
+    TString diststr = "({MHillasSrc.fDist/315.} < ";
     diststr += distmin;
-    diststr += " || ";
+    diststr += ") || (";
     diststr += "{MHillasSrc.fDist/315.} > ";
     diststr += distmax;
+    diststr += ")";
     MF distfilter(diststr);
     
-    TString alphastr = "{abs(MHillasSrc.fAlpha)} < ";
+    TString alphastr = "({abs(MHillasSrc.fAlpha)} < ";
     alphastr += alphamin;
-    alphastr += " || ";
+    alphastr += ") || (";
     alphastr += "{abs(MHillasSrc.fAlpha)} > ";
     alphastr += alphamax;
+    alphastr += ")";
     MF alphafilter(alphastr);
     
@@ -219,5 +219,6 @@
     MContinue cont_odd(&oddfilter);
     
-    MSrcPosFromFile srccalc(f_src_name);
+    //    MSrcPosFromFile srccalc(f_src_name);
+    MSrcPlace srccalc;
     
     MHillasSrcCalc csrc_on;
@@ -245,5 +246,5 @@
     tlist_on.AddToList(&csrc_on);
     tlist_on.AddToList(&fsrcpos_on);
-    tlist_on.AddToList(&cont_odd);
+//    tlist_on.AddToList(&cont_odd);
     tlist_on.AddToList(&cont_size);
     tlist_on.AddToList(&cont_width);
@@ -357,5 +358,5 @@
     read_off.DisableAutoScheme();
     
-    srccalc.SetMode(MSrcPosFromFile::kOff);
+    srccalc.SetMode(MSrcPlace::kOff);
 
     MHillasSrcCalc csrc_off;
@@ -376,5 +377,5 @@
     tlist_off.AddToList(&csrc_off);
     tlist_off.AddToList(&fsrcpos_off);
-    tlist_off.AddToList(&cont_even);
+//    tlist_off.AddToList(&cont_even);
     tlist_off.AddToList(&cont_size);
     tlist_off.AddToList(&cont_width);
Index: trunk/MagicSoft/Mars/mtemp/mifae/macros/findstars.C
===================================================================
--- trunk/MagicSoft/Mars/mtemp/mifae/macros/findstars.C	(revision 4408)
+++ trunk/MagicSoft/Mars/mtemp/mifae/macros/findstars.C	(revision 4429)
@@ -47,5 +47,5 @@
 
 
-void findstars(const TString filename="dc_2004_03_17_01_16_51_20440_Mrk421.root", const TString directory="/nfs/magic/CaCodata/rootdata/Mrk421/Period015/2004_03_17/", const UInt_t numEvents = 0)
+void findstars(const TString filename="20040422_*_D_Mrk421_E.root", const TString directory="/local_disk/Data/rootdata/Mrk421/Period016/2004_04_22/", const UInt_t numEvents = 0)
 {
 
@@ -81,6 +81,5 @@
   MGeomApply geomapl;
   TString continuoslightfile = 
-    //    "/home/Javi/mnt_magic_data/CaCo/rootdata/Miscellaneous/Period016/2004_04_16/dc_2004_04_16_04_46_18_22368_Off3c279-2CL100.root";
-     "/nfs/magic/CaCodata/rootdata/Miscellaneous/Period016/2004_04_16/dc_2004_04_16_04_46_18_22368_Off3c279-2CL100.root";
+    "/local_disk/CaCoData/rootdata/Miscellaneous/Period016/2004_04_16/dc_2004_04_16_04_46_18_22368_Off3c279-2CL100.root";
 
   Float_t mindc = 0.9; //[uA]
@@ -93,6 +92,6 @@
   const TArrayS blindpixels(numblind,(Short_t*)x);
   Float_t ringinterest = 100; //[mm]
-  Float_t tailcut = 2.5;
-  UInt_t integratedevents = 1;
+  Float_t tailcut = 3.5;
+  UInt_t integratedevents = 4;
 
   MFindStars findstars;
Index: trunk/MagicSoft/Mars/mtemp/mifae/macros/hillasONOFF.C
===================================================================
--- trunk/MagicSoft/Mars/mtemp/mifae/macros/hillasONOFF.C	(revision 4408)
+++ trunk/MagicSoft/Mars/mtemp/mifae/macros/hillasONOFF.C	(revision 4429)
@@ -27,9 +27,11 @@
        
     // define file, tree, canvas
-    TFile *fdata = new TFile(onname);
+    TChain *fdata = new TChain;
+    fdata->AddFile(onname);
     TTree *t = Parameters;                                                     
     
     // define file, tree, canvas
-    TFile *fdata2 = new TFile(offname);
+    TChain *fdata2 = new TChain;
+    fdata2->AddFile(offname);
     TTree *t2 = Parameters;  
    
Index: trunk/MagicSoft/Mars/mtemp/mifae/macros/hvnotnominal.C
===================================================================
--- trunk/MagicSoft/Mars/mtemp/mifae/macros/hvnotnominal.C	(revision 4408)
+++ trunk/MagicSoft/Mars/mtemp/mifae/macros/hvnotnominal.C	(revision 4429)
@@ -47,5 +47,5 @@
 
 
-void hvnotnominal(const TString filename="20040319_20821_D_Mrk421_S.root", const TString directory="/nfs/magic/CaCodata/2004_03_19/")
+void hvnotnominal(const TString filename="20040319_20821_D_Mrk421_S.root", const TString directory="/nfs/magic/CaCodata/2004_03_19/", Float_t percent = 0.01)
 {
 
@@ -86,5 +86,5 @@
   MGeomApply geomapl;
   MFHVNotNominal fHVNominal;
-  TString hvnominal = "/nfs/ifae.es/user/j/jlopez/Mars/ConfigFiles/HVSettings_FF35q.conf";
+  TString hvnominal = "/mnt/users/jlopez/Mars/Files4Mars/Config/HVSettings_FF35q.conf";
   fHVNominal.SetHVNominalValues(hvnominal);
   //  fHVNominal.SetMaxNumPixelsDeviated(10);
@@ -105,8 +105,61 @@
   //
 
-  if (!evtloop.Eventloop())
+  MHCamera hvnotnominal(geomcam);
+  MHCamera lowhvnotnominal(geomcam);
+  MHCamera uphvnotnominal(geomcam);
+
+  if (!evtloop.PreProcess())
     return;
 
+  TArrayD nominal = fHVNominal.GetHVNominal();
+  TArrayD lownominal = nominal;
+  TArrayD upnominal = nominal;
+  
+  for (UInt_t pix=0; pix<nominal.GetSize(); pix++)
+    {
+      lownominal[pix] *= (1-percent);
+      upnominal[pix]  *= (1+percent);
+    }
+  
+  
+  while(tlist.Process())
+    {
+      
+      hvnotnominal.CntCamContent(hvcam,lownominal,0,kFALSE);
+      hvnotnominal.CntCamContent(hvcam,upnominal,0,kTRUE);
+      
+      lowhvnotnominal.CntCamContent(hvcam,lownominal,0,kFALSE);
+      uphvnotnominal.CntCamContent(hvcam,upnominal,0,kTRUE);
+    }
+  
+
+  evtloop.PostProcess();
+  
   tlist.PrintStatistics();
+  
+  TCanvas c1;
+  c1.Divide(2,1);
+  c1.cd(1);
+  lowhvnotnominal.SetPrettyPalette();
+  lowhvnotnominal.Draw();
+  gPad->cd(1);
+  gPad->Modified();
+  gPad->Update();
+  c1.cd(2);
+  uphvnotnominal.SetPrettyPalette();
+  uphvnotnominal.Draw();
+  gPad->cd(2);
+  gPad->Modified();
+  gPad->Update();
+
+  TCanvas c2;
+  hvnotnominal.SetPrettyPalette();
+  hvnotnominal.Draw();
+  gPad->cd(1);
+  gPad->Modified();
+  gPad->Update();
+
+  if (!HandleInput())
+    break;
 
 }
Index: trunk/MagicSoft/Mars/mtemp/mifae/macros/lightcurve.C
===================================================================
--- trunk/MagicSoft/Mars/mtemp/mifae/macros/lightcurve.C	(revision 4408)
+++ trunk/MagicSoft/Mars/mtemp/mifae/macros/lightcurve.C	(revision 4429)
@@ -1,56 +1,5 @@
-Double_t ChiSquareNDof(TH1D *h1, TH1D *h2)
-{
-    Double_t chiq = 0.;
-    Double_t chi;
-    Double_t error;
-    Int_t nbinsnozero = 0;
-
-    Int_t nbins = h1->GetNbinsX();
-    if (nbins != h2->GetNbinsX() || nbins == 0)
-	return -1;
-
-    for (UInt_t bin=1; bin<=nbins; bin++)
-    {
-	error = sqrt(h1->GetBinError(bin)*h1->GetBinError(bin) +
-			   h2->GetBinError(bin)*h2->GetBinError(bin));
-	if (error != 0)
-	{
-	    chi = (h1->GetBinContent(bin)-h2->GetBinContent(bin))/error;
-	    chiq += chi*chi;
-	    nbinsnozero++;
-	}
-    }
-
-    return (nbinsnozero>0?chiq/nbinsnozero:0);
-}
-
-Int_t GetBin(Double_t size, Int_t numberSizeBins, Double_t sizeBins[])
-{
-
-  Int_t result = -1;
-
-  Int_t lowerbin = 0;
-  Int_t upperbin = numberSizeBins;
-  Int_t bin;
-
-  Int_t count = 0;
-
-  if (size >= sizeBins[0])
-    {
-      while (upperbin - lowerbin > 1 && count++<=numberSizeBins)
-	{
-	  bin = (upperbin+lowerbin)/2;
-	  if (size >= sizeBins[bin])
-	    lowerbin = bin;
-	  else
-	    upperbin = bin;
-	}
-      result = count<=numberSizeBins?lowerbin:-1;
-    }
-
-  return result;
-
-}
-
+
+Double_t ChiSquareNDof(TH1D *h1, TH1D *h2);
+Int_t GetBin(Double_t size, Int_t numberSizeBins, Double_t sizeBins[]);
 
 void lightcurve(TString f_on_name = "../HillasFiles/Mrk421/*_H.root", 
@@ -67,6 +16,6 @@
   // Configuration
   const Bool_t debug = kFALSE;
-  const Double_t timebin = 600.; //[sec]
-  TString psname = "./20040420_Mrk421.600s.ps";
+  const Double_t timebin = 1380.; //[sec]
+  TString psname = "./20040421_Mrk421.1380s.ps";
 
   //Constanst
@@ -94,19 +43,31 @@
   TObjArray timediffHistoOn;
   
-  const Int_t numberSizeBins = 4;
-  Double_t sizeBins[numberSizeBins] = {1292., 1897., 2785., 4087.}; //[Photons]
+  const Int_t numberSizeBins = 3;
+  Double_t sizeBins[numberSizeBins] = {1897., 2785., 4087.}; //[Photons]
 
   //cuts
 
-  Double_t widthmin[numberSizeBins]  = { 0.06, 0.06, 0.06, 0.06 }; 
-  Double_t widthmax[numberSizeBins]  = { 0.10, 0.10, 0.12, 0.12 };
-  Double_t lengthmin[numberSizeBins] = { 0.10, 0.10, 0.10, 0.10 }; 
-  Double_t lengthmax[numberSizeBins] = { 0.20, 0.25, 0.26, 0.36 };
-  Double_t distmin[numberSizeBins]   = { 0.30, 0.30, 0.30, 0.30 }; 
-  Double_t distmax[numberSizeBins]   = { 1.20, 1.20, 1.20, 1.20 };
+  Double_t widthmin[numberSizeBins]  = { 0.06, 0.06, 0.06 }; 
+  Double_t widthmax[numberSizeBins]  = { 0.10, 0.12, 0.12 };
+  Double_t lengthmin[numberSizeBins] = { 0.10, 0.10, 0.10 }; 
+  Double_t lengthmax[numberSizeBins] = { 0.25, 0.26, 0.36 };
+  Double_t distmin[numberSizeBins]   = { 0.30, 0.30, 0.30 }; 
+  Double_t distmax[numberSizeBins]   = { 1.20, 1.20, 1.20 };
+
+//   const Int_t numberSizeBins = 4;
+//   Double_t sizeBins[numberSizeBins] = {1292., 1897., 2785., 4087.}; //[Photons]
+
+//   //cuts
+
+//   Double_t widthmin[numberSizeBins]  = { 0.06, 0.06, 0.06, 0.06 }; 
+//   Double_t widthmax[numberSizeBins]  = { 0.10, 0.10, 0.12, 0.12 };
+//   Double_t lengthmin[numberSizeBins] = { 0.10, 0.10, 0.10, 0.10 }; 
+//   Double_t lengthmax[numberSizeBins] = { 0.20, 0.25, 0.26, 0.36 };
+//   Double_t distmin[numberSizeBins]   = { 0.30, 0.30, 0.30, 0.30 }; 
+//   Double_t distmax[numberSizeBins]   = { 1.20, 1.20, 1.20, 1.20 };
 
   //alpha plot integration for gammas
   Double_t sigexccmin = 0.;
-  Double_t sigexccmax = 20.;
+  Double_t sigexccmax = 15.;
   Double_t bkgnormmin = 40.;
   Double_t bkgnormmax = 80.;
@@ -128,4 +89,5 @@
   MRawEvtHeader evtheader_on;
   MTime              time_on;
+  MLiveTime      livetime_on;
   MHillas          hillas_on;
   MHillasSrc    hillassrc_on;
@@ -137,4 +99,5 @@
   plist_on.AddToList(&evtheader_on);
   plist_on.AddToList(&time_on);
+  plist_on.AddToList(&livetime_on);
   plist_on.AddToList(&hillas_on);
   plist_on.AddToList(&hillassrc_on);
@@ -186,5 +149,8 @@
   MSrcPlace srcplace;
   MHillasSrcCalc csrc_on;
-  
+
+  MLiveTimeCalc livetimecalc_on;
+  livetimecalc_on.SetRealTimeBinSize(timebin);
+
 //   // prints
 //   MPrint pevent("MRawEvtHeader");
@@ -195,4 +161,5 @@
   //tasklist
   tlist_on.AddToList(&read_on);
+  tlist_on.AddToList(&livetimecalc_on);
   tlist_on.AddToList(&srcplace);
   tlist_on.AddToList(&csrc_on);
@@ -212,18 +179,4 @@
     return;
   
-  Double_t mjdFirstEventofBin=0;
-  Double_t mjdFirstEvent=0;
-
-  MTime mtimeFirstEventofBin;
-  MTime mtimeFirstEvent;
-
-  Double_t mjdLastEvent=0;
-  MTime  mtimeLastEvent=0;
-  UInt_t   evtLastEvent;
-  UInt_t   runLastEvent=0;
-  MTime    mtimeLastEvent;
-
-  Bool_t flag = kFALSE;
-
   numberOnEventsAfterCleaning[numberTimeBins-1] = 0;
   zenithMinimumOn[numberTimeBins-1] = 100.;
@@ -252,275 +205,145 @@
   TF1 *f1 = new TF1("exp","expo",mintimediff,maxtimediff);	      
 
+  Double_t lastSrcPosX = 0;
+  Double_t lastSrcPosY = 0;
+
   while(tlist_on.Process())
     {
-      // Compute live time 
-      UInt_t   run = runheader_on.GetRunNumber();
-      UInt_t   evt = evtheader_on.GetDAQEvtNumber();
-      Double_t mjd = time_on.GetMjd();
+      numberOnEventsAfterCleaning[numberTimeBins-1]++;
       
-      numberOnEventsAfterCleaning[numberTimeBins-1]++;
-				  
-      if (mjd == 0)
-      {
-	
-	  if (debug)
-	    {
-	      cout << "MTime::GetTime() == 0" << endl;
-	      cout.precision(15);
-	      cout << "last event:\t run "<< runLastEvent << " event " << evtLastEvent << " mjd " << mjdLastEvent << endl;
-	      cout << "present event:\t run " << run << " event " << evt << " mjd " << mjd <<  endl;
-	      mtimeLastEvent.Print();
-	      time_on.Print();
-	    }
-	  
-	  flag = kTRUE;
-       }
-      else if (mjd-mjdLastEvent < 0 && flag)
-        {
-	  
-	  if (debug)
-	    {
-	      cout << "mjd-mjdLastEvent < 0" << endl;
-	      cout.precision(15);
-	      cout << "last event:\t run "<< runLastEvent << " event " << evtLastEvent << " mjd " << mjdLastEvent << endl;
-	      cout << "present event:\t run " << run << " event " << evt << " mjd " << mjd <<  endl;
-	      mtimeLastEvent.Print();
-	      time_on.Print();	
-	    }
-	  
-	  flag = kTRUE;
-       }
-      else if (mjd-mjdLastEvent == 0 && flag)
-        {
-	  
-	  if (debug)
-	    {
-	      cout << "mjd-mjdLastEvent == 0" << endl;
-	      cout.precision(15);
-	      cout << "last event:\t run "<< runLastEvent << " event " << evtLastEvent << " mjd " << mjdLastEvent << endl;
-	      cout << "present event:\t run " << run << " event " << evt << " mjd " << mjd <<  endl;
-	      mtimeLastEvent.Print();
-	      time_on.Print();	
-	    }
-
-	  flag = kTRUE;
-       }
-      else if (evt-evtLastEvent <= 0)
-        {
-	  
-	  if (debug)
-	    {
-	      cout << "evt-evtLastEvent <= 0" << endl;
-	      cout.precision(15);
-	      cout << "last event:\t run "<< runLastEvent << " event " << evtLastEvent << " mjd " << mjdLastEvent << endl;
-	      cout << "present event:\t run " << run << " event " << evt << " mjd " << mjd <<  endl;
-	      mtimeLastEvent.Print();
-	      time_on.Print();	
-	    }
-
-	  flag = kTRUE;
-        }
-
-      else if ((Int_t)mjd == mjd)
-        {
-	  
-	  if (debug)
-	    {
-	      cout << "(Int_t)mjd == mjd" << endl;
-	      cout.precision(15);
-	      cout << "last event:\t run "<< runLastEvent << " event " << evtLastEvent << " mjd " << mjdLastEvent << endl;
-	      cout << "present event:\t run " << run << " event " << evt << " mjd " << mjd <<  endl;
-	      mtimeLastEvent.Print();
-	      time_on.Print();	
-	    }
-
-	  flag = kTRUE;
-        }
+      if (srcpos_on.GetX() == 0. && srcpos_on.GetY() == 0.)
+	srcpos_on.SetXY(lastSrcPosX,lastSrcPosY);
       else
 	{
-
-	  if (flag && debug)
+	  lastSrcPosX = srcpos_on.GetX();
+	  lastSrcPosY = srcpos_on.GetY();
+	}
+      srcplace.CallProcess();
+      csrc_on.CallProcess();
+
+      
+      Double_t size = hillas_on.GetSize();
+      Double_t width = hillas_on.GetWidth()*geomcam.GetConvMm2Deg();
+      Double_t length = hillas_on.GetLength()*geomcam.GetConvMm2Deg();
+      Double_t meanx = hillas_on.GetMeanX()*geomcam.GetConvMm2Deg();
+      Double_t meany = hillas_on.GetMeanY()*geomcam.GetConvMm2Deg();
+      Double_t centerdist = TMath::Sqrt(meanx*meanx + meany*meany);
+      Double_t dist = hillassrc_on.GetDist()*geomcam.GetConvMm2Deg();
+      Double_t alpha = hillassrc_on.GetAlpha();
+      Double_t srcposx = srcpos_on.GetX();
+      Double_t srcposy = srcpos_on.GetY();
+      Double_t zenith = drive_on.GetNominalZd();
+	  
+
+      Int_t sizebin = GetBin(size,numberSizeBins,sizeBins);
+	  
+      if (sizebin >= 0)
+	{
+	  if (width > widthmin[sizebin] && width < widthmax[sizebin])
 	    {
-	    
-	      cout << "flag = TRUE" << endl;
-	      cout.precision(15);
-	      cout << "last event:\t run "<< runLastEvent << " event " << evtLastEvent << " mjd " << mjdLastEvent << endl;
-	      cout << "present event:\t run " << run << " event " << evt << " mjd " << mjd <<  endl;
-	      mtimeLastEvent.Print();
-	      time_on.Print();	
-	      
-	      flag = kFALSE;
-
-	    }
-
-	  if ( run !=  runLastEvent )
-	    {
-	      if ( mjdLastEvent != 0 )
+	      if (length > lengthmin[sizebin] && length < lengthmax[sizebin])
 		{
-		  timeOn[numberTimeBins-1] += (mjdLastEvent-mjdFirstEvent)*kDay;
-		  
-		  cout.precision(10);
-		  cout << "timeOn[" << numberTimeBins-1 << "] " << timeOn[numberTimeBins-1] << " mjdLastEvent " << mjdLastEvent << " mjdFirstEvent " << mjdFirstEvent << endl;
-		  if (flag && debug)
-		    {
-		      cout << "last event:\t run "<< runLastEvent << " event " << evtLastEvent << " mjd " << mjdLastEvent << endl;
-		      cout << "present event:\t run " << run << " event " << evt << " mjd " << mjd <<  endl;
-		      mtimeLastEvent.Print();
-		      time_on.Print();	
-		    }
-		  
-		}
-	      
-	      mjdFirstEvent = mjd;
-	    }
-	  else if (mjdFirstEventofBin != 0)
-	    hTimeDiff_on_timebin->Fill((mjd-mjdLastEvent)*kDay*kSec);
-	  
-	  if (mjdFirstEventofBin == 0)
-	    {
-	      mjdFirstEvent = mjd;
-	      mjdFirstEventofBin = mjd;
-	    }
-
-	  evtLastEvent = evt;
-	  runLastEvent = run;
-	  mjdLastEvent = mjd;
-	  mtimeLastEvent = time_on;
-      
-	  Double_t size = hillas_on.GetSize();
-	  Double_t width = hillas_on.GetWidth()*geomcam.GetConvMm2Deg();
-	  Double_t length = hillas_on.GetLength()*geomcam.GetConvMm2Deg();
-	  Double_t meanx = hillas_on.GetMeanX()*geomcam.GetConvMm2Deg();
-	  Double_t meany = hillas_on.GetMeanY()*geomcam.GetConvMm2Deg();
-	  Double_t centerdist = TMath::Sqrt(meanx*meanx + meany*meany);
-  	  Double_t dist = hillassrc_on.GetDist()*geomcam.GetConvMm2Deg();
-	  Double_t alpha = hillassrc_on.GetAlpha();
-	  Double_t srcposx = srcpos_on.GetX();
-	  Double_t srcposy = srcpos_on.GetY();
-	  Double_t zenith = drive_on.GetNominalZd();
-	  
-
-	  Int_t sizebin = GetBin(size,numberSizeBins,sizeBins);
-	  
-	  if (sizebin >= 0)
-	    {
-	      if (width > widthmin[sizebin] && width < widthmax[sizebin])
-		{
-		  if (length > lengthmin[sizebin] && length < lengthmax[sizebin])
-		    {
-		      if (dist > distmin[sizebin] && centerdist < distmax[sizebin])
-			{      
-
-			  //General histos
-			  hSize_on->Fill(log10(size));
-			  hWidth_on->Fill(width);
-			  hLength_on->Fill(length);
-			  hDist_on->Fill(dist);
-			  hAlpha_on_abs->Fill(TMath::Abs(alpha));
-			  hAlpha_on->Fill(alpha);
-			  hSrcPos_on->Fill(srcposx,srcposy);
-			  
-			  // Time bin histos
-			  hAlpha_on_abs_timebin->Fill(TMath::Abs(alpha));
-			  hSrcPos_on_timebin->Fill(srcposx,srcposy);
-			  hCosZenith_on_timebin->Fill(TMath::Cos(zenith*kConvDegToRad));
-			  
-			  if (zenith != 0 && zenith < zenithMinimumOn[numberTimeBins-1])
-			    zenithMinimumOn[numberTimeBins-1] = zenith;
-			  if (zenith>zenithMaximumOn[numberTimeBins-1])
-			    zenithMaximumOn[numberTimeBins-1] = zenith;
-			  
-			}
+		  if (dist > distmin[sizebin] && centerdist < distmax[sizebin])
+		    {      
+		      
+		      //General histos
+		      hSize_on->Fill(log10(size));
+		      hWidth_on->Fill(width);
+		      hLength_on->Fill(length);
+		      hDist_on->Fill(dist);
+		      hAlpha_on_abs->Fill(TMath::Abs(alpha));
+		      hAlpha_on->Fill(alpha);
+		      hSrcPos_on->Fill(srcposx,srcposy);
+		      
+		      // Time bin histos
+		      hAlpha_on_abs_timebin->Fill(TMath::Abs(alpha));
+		      hSrcPos_on_timebin->Fill(srcposx,srcposy);
+		      hCosZenith_on_timebin->Fill(TMath::Cos(zenith*kConvDegToRad));
+		      
+		      if (zenith != 0 && zenith < zenithMinimumOn[numberTimeBins-1])
+			zenithMinimumOn[numberTimeBins-1] = zenith;
+		      if (zenith>zenithMaximumOn[numberTimeBins-1])
+			zenithMaximumOn[numberTimeBins-1] = zenith;
+		      
 		    }
 		}
 	    }
+	}
+      
+      
+      // Check if you are overload the maxim time bin
+      if (numberTimeBins != livetime_on.GetNumberTimeBins())
+	{
+	  timeOn[numberTimeBins-1] = livetime_on.GetLiveTimeTArray().At(numberTimeBins-1);
+	  meanTimeBinOn[numberTimeBins-1] = livetime_on.GetMeanRealTimeTArray().At(numberTimeBins-1);
+	  widthTimeBinOn[numberTimeBins-1] = livetime_on.GetWidthRealTimeTArray().At(numberTimeBins-1);
+
+	  //Compute the number of on events
+	  numberOnEvents[numberTimeBins-1] = (Double_t) hAlpha_on_abs_timebin->Integral((Int_t)sigexccmin*nbins_abs/90+1,(Int_t)sigexccmax*nbins_abs/90);
+	  numberBkgEventsToNormOn[numberTimeBins-1] =  (Double_t)  hAlpha_on_abs_timebin->Integral((Int_t)bkgnormmin*nbins_abs/90+1,(Int_t)bkgnormmax*nbins_abs/90);	      
 	  
-  
-      // Check if you are overload the maxim time bin
-        if ((mjd-mjdFirstEventofBin)*kDay >= timebin)
-	  {
-	      //Compute the time on
-	      timeOn[numberTimeBins-1] += (mjdLastEvent-mjdFirstEvent)*kDay;
-	      widthTimeBinOn[numberTimeBins-1] = (mjdLastEvent-mjdFirstEventofBin)/2;
-	      meanTimeBinOn[numberTimeBins-1] = mjdFirstEventofBin + widthTimeBinOn[numberTimeBins-1];
-	      
-	      cout << "timeOn[" << numberTimeBins-1 << "] " << timeOn[numberTimeBins-1] << " mjdLastEvent " << mjdLastEvent << " mjdFirstEvent " << mjdFirstEvent << endl;
-	      cout << "mjd " << mjd << " mjdFirstEventofBin " << mjdFirstEventofBin << " widthTimeBinOn " <<  widthTimeBinOn[numberTimeBins-1] << " meanTimeBinOn " << meanTimeBinOn[numberTimeBins-1] << ' ' <<  mjdFirstEventofBin + widthTimeBinOn[numberTimeBins-1] << endl;
-	      
-	      //Compute the number of on events
-	      numberOnEvents[numberTimeBins-1] = (Double_t) hAlpha_on_abs_timebin->Integral((Int_t)sigexccmin*nbins_abs/90+1,(Int_t)sigexccmax*nbins_abs/90+1);
-	      numberBkgEventsToNormOn[numberTimeBins-1] =  (Double_t)  hAlpha_on_abs_timebin->Integral((Int_t)bkgnormmin*nbins_abs/90+1,(Int_t)bkgnormmax*nbins_abs/90+1);	      
-	      //Initialize variables
-	      mjdFirstEvent = mjd;
-	      mjdFirstEventofBin = mjd;
-
-	      hTimeDiff_on_timebin->Fit("exp","RQ0");
-	      deadFractionOn[numberTimeBins-1] = (nbins_timediff/(maxtimediff-mintimediff))*TMath::Exp(f1->GetParameter(0))/(TMath::Abs(f1->GetParameter(1))*hTimeDiff_on_timebin->GetEntries());
-	      cout << "1-> Exp(" << f1->GetParameter(0) << " + " << f1->GetParameter(1) << "*x) +- [" <<  f1->GetParError(0) << ' ' << f1->GetParError(1) << "]" << endl;
-	      cout << "Calc entries " << (nbins_timediff/(maxtimediff-mintimediff))*TMath::Exp(f1->GetParameter(0))/(TMath::Abs(f1->GetParameter(1))) << " +- Nt*(" << TMath::Sqrt( f1->GetParameter(0)*f1->GetParError(0)*f1->GetParameter(0)*f1->GetParError(0) + (f1->GetParError(1)*f1->GetParError(1))/(f1->GetParameter(1)*f1->GetParameter(1)))  << ") meas entries " << hTimeDiff_on_timebin->GetEntries() << " dead fraction " << deadFractionOn[numberTimeBins-1] << endl;
-
-	      alphaHistoOn.AddLast(hAlpha_on_abs_timebin);
-	      srcposHistoOn.AddLast(hSrcPos_on_timebin);
-	      coszenithHistoOn.AddLast(hCosZenith_on_timebin);
-	      timediffHistoOn.AddLast(hTimeDiff_on_timebin);
-	      
-// 	      cout << "hAlpha_on_abs_timebin " << hAlpha_on_abs_timebin <<  " alphaHistoOn [" << numberTimeBins-1 << "] " << alphaHistoOn[numberTimeBins-1] << endl;
-
-	      //Increase the number of time bins and all needed arrays
-	      numberTimeBins++;
-	      
-	      timeOn.Set(numberTimeBins);
-	      numberOnEvents.Set(numberTimeBins);
-	      numberBkgEventsToNormOn.Set(numberTimeBins);
-	      widthTimeBinOn.Set(numberTimeBins);
-	      meanTimeBinOn.Set(numberTimeBins);
-	      zenithMinimumOn.Set(numberTimeBins);
-	      zenithMaximumOn.Set(numberTimeBins);
-	      deadFractionOn.Set(numberTimeBins);
-	      numberOnEventsAfterCleaning.Set(numberTimeBins);
-
-	      timeOn[numberTimeBins-1] = 0.;
-	      zenithMinimumOn[numberTimeBins-1] = 100.;
-	      zenithMaximumOn[numberTimeBins-1] = 0.;
-	      numberOnEventsAfterCleaning[numberTimeBins-1] = 0;
-	      
-	      alphaTitle =  Form("%s%02i","hAlphaOn",numberTimeBins-1);         
-	      hAlpha_on_abs_timebin = new TH1F (alphaTitle,"",nbins_abs,minalpha_abs,maxalpha_abs);
-	      
-	      srcposTitle =  Form("%s%02i","hSrcPosOn",numberTimeBins-1);
-	      hSrcPos_on_timebin = new TH2F (srcposTitle,"",nbins_srcpos,minsrcpos,maxsrcpos,nbins_srcpos,minsrcpos,maxsrcpos);
-
-	      coszenithTitle =  Form("%s%02i","hCosZenithOn",numberTimeBins-1);
-	      hCosZenith_on_timebin = new TH1F (coszenithTitle,"",nbins_coszenith,mincoszenith,maxcoszenith);
-
-	      timediffTitle =  Form("%s%02i","hTimeDiffOn",numberTimeBins-1);
-	      hTimeDiff_on_timebin = new TH1F (timediffTitle,"",nbins_timediff,mintimediff,maxtimediff);
-
-	      flag = kTRUE;
-	  }
+// 	  hTimeDiff_on_timebin->Fit("exp","RQ0");
+// 	  deadFractionOn[numberTimeBins-1] = (nbins_timediff/(maxtimediff-mintimediff))*TMath::Exp(f1->GetParameter(0))/(TMath::Abs(f1->GetParameter(1))*hTimeDiff_on_timebin->GetEntries());
+// 	  cout << "1-> Exp(" << f1->GetParameter(0) << " + " << f1->GetParameter(1) << "*x) +- [" <<  f1->GetParError(0) << ' ' << f1->GetParError(1) << "]" << endl;
+// 	  cout << "Calc entries " << (nbins_timediff/(maxtimediff-mintimediff))*TMath::Exp(f1->GetParameter(0))/(TMath::Abs(f1->GetParameter(1))) << " +- Nt*(" << TMath::Sqrt( f1->GetParameter(0)*f1->GetParError(0)*f1->GetParameter(0)*f1->GetParError(0) + (f1->GetParError(1)*f1->GetParError(1))/(f1->GetParameter(1)*f1->GetParameter(1)))  << ") meas entries " << hTimeDiff_on_timebin->GetEntries() << " dead fraction " << deadFractionOn[numberTimeBins-1] << endl;
+	  deadFractionOn[numberTimeBins-1] = 1.;
+	  
+	  alphaHistoOn.AddLast(hAlpha_on_abs_timebin);
+	  srcposHistoOn.AddLast(hSrcPos_on_timebin);
+	  coszenithHistoOn.AddLast(hCosZenith_on_timebin);
+	  timediffHistoOn.AddLast(hTimeDiff_on_timebin);
+	  
+	  numberTimeBins = livetime_on.GetNumberTimeBins();
+	  
+	  timeOn.Set(numberTimeBins);
+	  numberOnEvents.Set(numberTimeBins);
+	  numberBkgEventsToNormOn.Set(numberTimeBins);
+	  widthTimeBinOn.Set(numberTimeBins);
+	  meanTimeBinOn.Set(numberTimeBins);
+	  zenithMinimumOn.Set(numberTimeBins);
+	  zenithMaximumOn.Set(numberTimeBins);
+	  deadFractionOn.Set(numberTimeBins);
+	  numberOnEventsAfterCleaning.Set(numberTimeBins);
+	  
+	  timeOn[numberTimeBins-1] = 0.;
+	  zenithMinimumOn[numberTimeBins-1] = 100.;
+	  zenithMaximumOn[numberTimeBins-1] = 0.;
+	  numberOnEventsAfterCleaning[numberTimeBins-1] = 0;
+	  
+	  alphaTitle =  Form("%s%02i","hAlphaOn",numberTimeBins-1);         
+	  hAlpha_on_abs_timebin = new TH1F (alphaTitle,"",nbins_abs,minalpha_abs,maxalpha_abs);
+	  
+	  srcposTitle =  Form("%s%02i","hSrcPosOn",numberTimeBins-1);
+	  hSrcPos_on_timebin = new TH2F (srcposTitle,"",nbins_srcpos,minsrcpos,maxsrcpos,nbins_srcpos,minsrcpos,maxsrcpos);
+	  
+	  coszenithTitle =  Form("%s%02i","hCosZenithOn",numberTimeBins-1);
+	  hCosZenith_on_timebin = new TH1F (coszenithTitle,"",nbins_coszenith,mincoszenith,maxcoszenith);
+	  
+	  timediffTitle =  Form("%s%02i","hTimeDiffOn",numberTimeBins-1);
+	  hTimeDiff_on_timebin = new TH1F (timediffTitle,"",nbins_timediff,mintimediff,maxtimediff);
+	  
+	}
+      
+    }
 	
-	}
-	
-    }
-
-  
-  //Compute the time on for last time bin
-  timeOn[numberTimeBins-1] += (mjdLastEvent-mjdFirstEvent)*kDay;
-  widthTimeBinOn[numberTimeBins-1] = (mjd-mjdFirstEventofBin)/2;
-  meanTimeBinOn[numberTimeBins-1] = mjdFirstEventofBin + widthTimeBinOn[numberTimeBins-1];
+  loop_on.PostProcess();
+  
+  tlist_on.PrintStatistics();
+  
+  timeOn[numberTimeBins-1] = livetime_on.GetLiveTimeTArray().At(numberTimeBins-1);
+  meanTimeBinOn[numberTimeBins-1] = livetime_on.GetMeanRealTimeTArray().At(numberTimeBins-1);
+  widthTimeBinOn[numberTimeBins-1] = livetime_on.GetWidthRealTimeTArray().At(numberTimeBins-1);
+  
   //Compute the number of on events for the last time bin
-  numberOnEvents[numberTimeBins-1] = (Double_t) hAlpha_on_abs_timebin->Integral((Int_t)sigexccmin*nbins_abs/90+1,(Int_t)sigexccmax*nbins_abs/90+1);
-  numberBkgEventsToNormOn[numberTimeBins-1] =  (Double_t)  hAlpha_on_abs_timebin->Integral((Int_t)bkgnormmin*nbins_abs/90+1,(Int_t)bkgnormmax*nbins_abs/90+1);	      
-
-  hTimeDiff_on_timebin->Fit("exp","RQ0");
-  deadFractionOn[numberTimeBins-1] = (nbins_timediff/(maxtimediff-mintimediff))*TMath::Exp(f1->GetParameter(0))/(TMath::Abs(f1->GetParameter(1))*hTimeDiff_on_timebin->GetEntries());
-
-  cout.precision(5);
-  cout << "2-> Exp(" << f1->GetParameter(0) << " + " << f1->GetParameter(1) << "*x) +- [" <<  f1->GetParError(0) << ' ' << f1->GetParError(1) << "]" << endl;
-  cout << "Calc entries " << (nbins_timediff/(maxtimediff-mintimediff))*TMath::Exp(f1->GetParameter(0))/TMath::Abs(f1->GetParameter(1)) << " +- Nt*(" << TMath::Sqrt( f1->GetParameter(0)*f1->GetParError(0)*f1->GetParameter(0)*f1->GetParError(0) + (f1->GetParError(1)*f1->GetParError(1))/(f1->GetParameter(1)*f1->GetParameter(1)))  << ") meas entries " << hTimeDiff_on_timebin->GetEntries() << " dead fraction " << deadFractionOn[numberTimeBins-1] << endl;
-
-  cout.precision(10);
-  cout << "timeOn[" << numberTimeBins-1 << "] " << timeOn[numberTimeBins-1] << " mjdLastEvent " << mjdLastEvent << " mjdFirstEvent " << mjdFirstEvent << endl;
-  cout << "mjd " << mjd << " mjdFirstEventofBin " << mjdFirstEventofBin << " meanTimeBinOn " << meanTimeBinOn[numberTimeBins-1] << " widthTimeBinOn " <<  widthTimeBinOn[numberTimeBins-1] << endl;
+  numberOnEvents[numberTimeBins-1] = (Double_t) hAlpha_on_abs_timebin->Integral((Int_t)sigexccmin*nbins_abs/90+1,(Int_t)sigexccmax*nbins_abs/90);
+  numberBkgEventsToNormOn[numberTimeBins-1] =  (Double_t)  hAlpha_on_abs_timebin->Integral((Int_t)bkgnormmin*nbins_abs/90+1,(Int_t)bkgnormmax*nbins_abs/90);	      
+
+//   hTimeDiff_on_timebin->Fit("exp","RQ0");
+//   deadFractionOn[numberTimeBins-1] = (nbins_timediff/(maxtimediff-mintimediff))*TMath::Exp(f1->GetParameter(0))/(TMath::Abs(f1->GetParameter(1))*hTimeDiff_on_timebin->GetEntries());
+
+//   cout.precision(5);
+//   cout << "2-> Exp(" << f1->GetParameter(0) << " + 
+//   cout << "Calc entries " << (nbins_timediff/(maxtimediff-mintimediff))*TMath::Exp(f1->GetParameter(0))/TMath::Abs(f1->GetParameter(1)) << " +- Nt*(" << TMath::Sqrt( f1->GetParameter(0)*f1->GetParError(0)*f1->GetParameter(0)*f1->GetParError(0) + (f1->GetParError(1)*f1->GetParError(1))/(f1->GetParameter(1)*f1->GetParameter(1)))  << ") meas entries " << hTimeDiff_on_timebin->GetEntries() << " dead fraction " << deadFractionOn[numberTimeBins-1] << endl;
+  deadFractionOn[numberTimeBins-1] = 1.;
 
   alphaHistoOn.AddLast(hAlpha_on_abs_timebin);
@@ -529,16 +352,4 @@
   timediffHistoOn.AddLast(hTimeDiff_on_timebin);
 
-  //  cout << "hAlpha_on_abs_timebin " << hAlpha_on_abs_timebin <<  " alphaHistoOn [" << numberTimeBins-1 << "] " << alphaHistoOn[numberTimeBins-1] << endl;
-
-  loop_on.PostProcess();
-  
-  tlist_on.PrintStatistics();
-  
-  for (UInt_t bin=0; bin<numberTimeBins; bin++)
-    cout << bin << " timeOn " << timeOn[bin] << " min-max zenithOn " << zenithMinimumOn[bin] << "-" <<  zenithMaximumOn[bin] << " min-max cos(zenithOn) " << TMath::Cos(zenithMinimumOn[bin]*kConvDegToRad) << "-" <<  TMath::Cos(zenithMaximumOn[bin]*kConvDegToRad) << " numberOnEvents " << numberOnEvents[bin] << " numberOnEventsAfterCleaning " << numberOnEventsAfterCleaning[bin] << " deadFractionOn " << deadFractionOn[bin] << endl;
-
-
-     
-
   //-----------------------OFF------------------------
 
@@ -552,4 +363,6 @@
   TArrayD numberBkgEventsToNormOff(numberTimeBins);
   TArrayD onOffNormFactor(numberTimeBins);
+  TArrayD onOffNormFactorWithEvents(numberTimeBins);
+  TArrayD onOffNormFactorWithLiveTime(numberTimeBins);
 
   TArrayD numberExcessEvents(numberTimeBins);
@@ -590,4 +403,5 @@
   MRawEvtHeader evtheader_off;
   MTime              time_off;
+  MLiveTime      livetime_off;
   MHillas          hillas_off;
   MHillasSrc    hillassrc_off;
@@ -599,4 +413,5 @@
   plist_off.AddToList(&evtheader_off);
   plist_off.AddToList(&time_off);
+  plist_off.AddToList(&livetime_off);
   plist_off.AddToList(&hillas_off);
   plist_off.AddToList(&hillassrc_off);
@@ -627,6 +442,9 @@
   MHillasSrcCalc csrc_off;
   
-  //tasklist
+  MLiveTimeCalc livetimecalc_off;
+
+ //tasklist
   tlist_off.AddToList(&read_off);
+  tlist_off.AddToList(&livetimecalc_off);
   tlist_off.AddToList(&srcplace_timebin); // This is just to run the preprocess correctely
   tlist_off.AddToList(&csrc_off); // This is just to run the preprocess correctely
@@ -714,10 +532,4 @@
 	    }
 	}
-      
-      //       if (zenith >  zenithMaximumOn[numberTimeBins-1])
-      // 	{
-      // 	  cout << "Exit loop because we arrive to zenith = " << zenith << endl;
-      // 	  break;
-      // 	}
     }
   
@@ -734,5 +546,16 @@
   for (UInt_t bin=0; bin<numberTimeBins; bin++)
     {
-      
+      cout.precision(5);
+      cout << bin << " timeOn " << timeOn[bin] << " mean-width time " << meanTimeBinOn[bin] << "-" << widthTimeBinOn[bin] << endl;
+    }
+  livetime_off.Print("last");
+  livetime_off.Print("all");
+  cout << "livetime_off.GetLiveTime() " << livetime_off.GetLiveTime() << endl;
+
+  for (UInt_t bin=0; bin<numberTimeBins; bin++)
+    {
+      timeOff[bin] = livetime_off.GetLiveTime();
+
+      //
       meanTriggerRateOn[bin] = numberOnEventsAfterCleaning[bin]/timeOn[bin];
       errorMeanTriggerRateOn[bin] = TMath::Sqrt(numberOnEventsAfterCleaning[bin])/timeOn[bin];
@@ -745,9 +568,11 @@
       widthTimeBinOnInSec[bin] = widthTimeBinOn[bin]*kDay;
 
-      numberOffEvents[bin] = (Double_t) ((TH1F*)alphaHistoOff[bin])->Integral((Int_t)sigexccmin*nbins_abs/90+1,(Int_t)sigexccmax*nbins_abs/90+1);
-      numberBkgEventsToNormOff[bin] =  (Double_t) ((TH1F*)alphaHistoOff[bin])->Integral((Int_t)bkgnormmin*nbins_abs/90+1,(Int_t)bkgnormmax*nbins_abs/90+1);
+      numberOffEvents[bin] = (Double_t) ((TH1F*)alphaHistoOff[bin])->Integral((Int_t)sigexccmin*nbins_abs/90+1,(Int_t)sigexccmax*nbins_abs/90);
+      numberBkgEventsToNormOff[bin] =  (Double_t) ((TH1F*)alphaHistoOff[bin])->Integral((Int_t)bkgnormmin*nbins_abs/90+1,(Int_t)bkgnormmax*nbins_abs/90);
       if (numberOffEvents[bin] != 0 && numberBkgEventsToNormOff[bin] != 0)
 	{
-	  onOffNormFactor[bin] = numberBkgEventsToNormOn[bin]/numberBkgEventsToNormOff[bin];
+	  onOffNormFactorWithEvents[bin] = numberBkgEventsToNormOn[bin]/numberBkgEventsToNormOff[bin];
+	  onOffNormFactorWithLiveTime[bin] = timeOn[bin]/timeOff[bin];
+	  onOffNormFactor[bin] = onOffNormFactorWithEvents[bin];
 	  numberExcessEvents[bin] = numberOnEvents[bin] - onOffNormFactor[bin]*numberOffEvents[bin];
 	  errorNumberExcessEvents[bin] = TMath::Sqrt(numberOnEvents[bin] + onOffNormFactor[bin]*onOffNormFactor[bin]*numberOffEvents[bin]);
@@ -756,4 +581,5 @@
 	  numberExcessEventsPerMin[bin] = 60.*numberExcessEvents[bin]/timeOn[bin]*(deadFractionOn[bin]>1.?deadFractionOn[bin]:1.);
 	  errorNumberExcessEventsPerMin[bin] = 60.*errorNumberExcessEvents[bin]/timeOn[bin];
+
 	}
     }
@@ -762,10 +588,11 @@
     {
       cout.precision(5);
-      cout << bin << " timeOn " << timeOn[bin] << " mean-width time " << meanTimeBinOn[bin] << "-" << widthTimeBinOn[bin] << endl;
+      cout << bin << " timeOn " << timeOn[bin] << " mean-width time " << meanTimeBinOnInSec[bin] << "-" << widthTimeBinOnInSec[bin] << endl;
       cout << " numberOnEvents\t " << numberOnEvents[bin] << endl;
       cout << " numberOffEvents\t " << numberOffEvents[bin] << endl;
       cout << " numberBkgEventsToNormOn\t " << numberBkgEventsToNormOn[bin] << endl;
       cout << " numberBkgEventsToNormOff\t " << numberBkgEventsToNormOff[bin] << endl;
-      cout << " onOffNormFactor\t " << onOffNormFactor[bin] << endl;
+      cout << " onOffNormFactorWithEvents\t " << onOffNormFactorWithEvents[bin] << endl;
+      cout << " onOffNormFactorWithLiveTime\t " << onOffNormFactorWithLiveTime[bin] << endl;
       cout << " numberExcessEvents\t " <<  numberExcessEvents[bin] <<  " +- " << errorNumberExcessEvents[bin] << endl;
       cout << " deadFraction\t" << deadFractionOn[bin] << endl;
@@ -796,5 +623,4 @@
   TGraphErrors* cosmicrategraph = new TGraphErrors(numberTimeBins,meanTimeBinOnInSec.GetArray(),meanTriggerRateOn.GetArray(),widthTimeBinOnInSec.GetArray(),errorMeanTriggerRateOn.GetArray());
   cosmicrategraph->SetTitle("Cosmic Rate");
-  cosmicrategraph->SetMinimum(0.);
   cosmicrategraph->SetMarkerStyle(21);
   cosmicrategraph->SetMarkerSize(0.03);
@@ -810,5 +636,5 @@
   Float_t maxAlphaHistoHeight = 0;
   
-  for (UInt_t bin=0; bin<numberTimeBins-1; bin++)
+  for (UInt_t bin=0; bin<numberTimeBins; bin++)
     {
       for (UInt_t i=1; i<=nbins_abs; i++)
@@ -816,5 +642,6 @@
 	  maxAlphaHistoHeight = ((TH1F*)alphaHistoOn[bin])->GetBinContent(i);
     }      
- 
+  
+  cout << "maxAlphaHistoHeight " << maxAlphaHistoHeight << endl;
 
   for (UInt_t bin=0; bin<numberTimeBins-1; bin++)
@@ -897,8 +724,8 @@
   // ############################################################################
 
-  Double_t norm_on_abs  = (Double_t) hAlpha_on_abs->Integral((Int_t)bkgnormmin*nbins_abs/90+1,(Int_t)bkgnormmax*nbins_abs/90+1);
-  Double_t exces_on_abs = (Double_t) hAlpha_on_abs->Integral((Int_t)sigexccmin*nbins_abs/90+1,(Int_t)sigexccmax*nbins_abs/90+1);
-  Double_t norm_off_abs  = (Double_t) hAlpha_off_abs->Integral((Int_t)bkgnormmin*nbins_abs/90+1,(Int_t)bkgnormmax*nbins_abs/90+1);
-  Double_t exces_off_abs = (Double_t) hAlpha_off_abs->Integral((Int_t)sigexccmin*nbins_abs/90+1,(Int_t)sigexccmax*nbins_abs/90+1);
+  Double_t norm_on_abs  = (Double_t) hAlpha_on_abs->Integral((Int_t)bkgnormmin*nbins_abs/90+1,(Int_t)bkgnormmax*nbins_abs/90);
+  Double_t exces_on_abs = (Double_t) hAlpha_on_abs->Integral((Int_t)sigexccmin*nbins_abs/90+1,(Int_t)sigexccmax*nbins_abs/90);
+  Double_t norm_off_abs  = (Double_t) hAlpha_off_abs->Integral((Int_t)bkgnormmin*nbins_abs/90+1,(Int_t)bkgnormmax*nbins_abs/90);
+  Double_t exces_off_abs = (Double_t) hAlpha_off_abs->Integral((Int_t)sigexccmin*nbins_abs/90+1,(Int_t)sigexccmax*nbins_abs/90);
   Double_t norm = norm_on_abs/norm_off_abs;
 
@@ -1296,3 +1123,56 @@
 
 
-
+Double_t ChiSquareNDof(TH1D *h1, TH1D *h2)
+{
+    Double_t chiq = 0.;
+    Double_t chi;
+    Double_t error;
+    Int_t nbinsnozero = 0;
+
+    Int_t nbins = h1->GetNbinsX();
+    if (nbins != h2->GetNbinsX() || nbins == 0)
+	return -1;
+
+    for (UInt_t bin=1; bin<=nbins; bin++)
+    {
+	error = sqrt(h1->GetBinError(bin)*h1->GetBinError(bin) +
+			   h2->GetBinError(bin)*h2->GetBinError(bin));
+	if (error != 0)
+	{
+	    chi = (h1->GetBinContent(bin)-h2->GetBinContent(bin))/error;
+	    chiq += chi*chi;
+	    nbinsnozero++;
+	}
+    }
+
+    return (nbinsnozero>0?chiq/nbinsnozero:0);
+}
+
+Int_t GetBin(Double_t size, Int_t numberSizeBins, Double_t sizeBins[])
+{
+
+  Int_t result = -1;
+
+  Int_t lowerbin = 0;
+  Int_t upperbin = numberSizeBins;
+  Int_t bin;
+
+  Int_t count = 0;
+
+  if (size >= sizeBins[0])
+    {
+      while (upperbin - lowerbin > 1 && count++<=numberSizeBins)
+	{
+	  bin = (upperbin+lowerbin)/2;
+	  if (size >= sizeBins[bin])
+	    lowerbin = bin;
+	  else
+	    upperbin = bin;
+	}
+      result = count<=numberSizeBins?lowerbin:-1;
+    }
+
+  return result;
+
+}
+
Index: trunk/MagicSoft/Mars/mtemp/mifae/macros/makeHillas.C
===================================================================
--- trunk/MagicSoft/Mars/mtemp/mifae/macros/makeHillas.C	(revision 4408)
+++ trunk/MagicSoft/Mars/mtemp/mifae/macros/makeHillas.C	(revision 4429)
@@ -71,4 +71,5 @@
 
   MJPedestal pedloop0;
+  pedloop0.SetOutputPath("./");
   pedloop0.SetInput(&pedcaliter);
   pedloop0.SetExtractor(&extractor);
@@ -86,4 +87,5 @@
   MJCalibration calloop;
 
+  calloop.SetOutputPath("./");
   calloop.SetExtractor(&extractor);
   calloop.SetInput(&caliter);
@@ -101,4 +103,5 @@
   // First Compute the pedestals
   MJPedestal pedloop;
+  pedloop.SetOutputPath("./");
   pedloop.SetInput(&pediter);
 
@@ -225,4 +228,5 @@
   read4.AddTree("Drive");
   static_cast<MRead&>(read4).AddFiles(datiter); 
+  read4.AddToBranchList("MReportCurrents.*");
 
   // Task that use Slow control information 
@@ -252,14 +256,6 @@
   MSrcPosFromStars srcposfromstar;
     
-  // set bad pixels 
-  MBlindPixelCalc   blind;
-  MBlindPixelCalc   blind2;
-  const Short_t x[16] = {330,395,329,396,389,
-                         323,388,322,384,385,
-                         386,387,321,320,319,
-                         394};
-  const TArrayS bp(16,(Short_t*)x);
-  blind.SetPixelIndices(bp);
-  blind2.SetPixelIndices(bp);
+  MBadPixelsTreat   interpolatebadpixels;
+  interpolatebadpixels.SetUseInterpolation();
   
   MImgCleanStd      clean(lcore,ltail);
@@ -315,5 +311,6 @@
   tlist4.AddToList(&extractor,"Events");
   tlist4.AddToList(&photcalc,"Events");
-  //tlist4.AddToList(&blind,"Events");
+  if(calflag==11)
+    tlist4.AddToList(&interpolatebadpixels);
   tlist4.AddToList(&clean,"Events");
 
@@ -332,5 +329,4 @@
   //tlist4.AddToList(&blind2,"Events");
   tlist4.AddToList(&hcalc,"Events");
-  //  tlist4.AddToList(&srcposcalc,"Events");
   tlist4.AddToList(&csrc1,"Events");
   tlist4.AddToList(&write,"Events");
@@ -350,4 +346,5 @@
 
   tlist4.PrintStatistics();    
+
 }
 //-------------------------------------------------------------------------------
@@ -476,8 +473,8 @@
     }
 
-   pedcaliter.Reset();
-   pediter.Reset();
-   caliter.Reset();
-   datiter.Reset();
+  pedcaliter.Reset();
+  pediter.Reset();
+  caliter.Reset();
+  datiter.Reset();
   TString pfile;
 
Index: trunk/MagicSoft/Mars/mtemp/mifae/macros/observationTime.C
===================================================================
--- trunk/MagicSoft/Mars/mtemp/mifae/macros/observationTime.C	(revision 4408)
+++ trunk/MagicSoft/Mars/mtemp/mifae/macros/observationTime.C	(revision 4429)
@@ -6,5 +6,5 @@
 		     Int_t iniRun=0,
 		     Int_t finRun=99999,
-		     TString filename="/mnt/users/jrico/magic/mars/mars/mtemp/mifae/hillas/AllCrabNebula_3015_kDefault_MispE.root",
+		     TString filename="/mnt/users/jrico/magic/mars/mars/mtemp/mifae/hillas/AllCrabNebula_3015_kDefault_MispE.root"
 )
 {    
