Index: /trunk/MagicSoft/Mars/datacenter/macros/fillcalib.C
===================================================================
--- /trunk/MagicSoft/Mars/datacenter/macros/fillcalib.C	(revision 8926)
+++ /trunk/MagicSoft/Mars/datacenter/macros/fillcalib.C	(revision 8927)
@@ -19,5 +19,5 @@
 !   Author(s): Daniela Dorner, 08/2004 <mailto:dorner@astro.uni-wuerzburg.de>
 !
-!   Copyright: MAGIC Software Development, 2000-2006
+!   Copyright: MAGIC Software Development, 2000-2008
 !
 !
@@ -70,5 +70,5 @@
 #include <TSQLRow.h>
 
-#include "MSQLServer.h"
+#include "MSQLMagic.h"
 
 #include "MStatusArray.h"
@@ -79,33 +79,5 @@
 using namespace std;
 
-// --------------------------------------------------------------------------
-//
-// Checks whether an entry is already existing
-//
-Bool_t ExistStr(MSQLServer &serv, const char *column, const char *table, Int_t test)
-{
-    TString query(Form("SELECT %s FROM %s WHERE %s='%d'", column, table, column, test));
-    TSQLResult *res = serv.Query(query);
-    if (!res)
-        return kFALSE;
-
-    TSQLRow *row;
-
-    Bool_t rc = kFALSE;
-    while ((row=res->Next()))
-    {
-        if ((*row)[0])
-        {
-            rc = kTRUE;
-            break;
-        }
-    }
-
-    delete res;
-
-    return rc;
-}
-
-int Process(MSQLServer &serv, TString fname, Bool_t dummy)
+int Process(MSQLMagic &serv, TString fname)
 {
     //getting number of unsuitable, unreliable and isolated pixel
@@ -222,5 +194,4 @@
     TString meanconvouter=Form("%6.3f", meanconvo);
 
-
     //Getting sequ# from filename
     TString sequence = fname(TRegexp("calib[0-9]+[.]root$"));
@@ -233,4 +204,39 @@
     Int_t seq = atoi(sequence.Data()+5);
 
+    //getting the ratio of calibration events used
+    h = (TH1*)arr.FindObjectInCanvas("ArrTm;avg", "MHCamera", "ArrTm");
+    if (!h)
+    {
+        cout << "WARNING - Could not find histogram ArrTime;avg." << endl;
+        return 2;
+    }
+
+    UInt_t nevts = h->GetEntries();
+
+    TString query;
+    query = Form("SELECT SUM(fNumEvents) FROM RunData "
+                 "LEFT JOIN RunType ON RunType.fRunTypeKEY=RunData.fRunTypeKEY "
+                 "WHERE fSequenceFirst=%d AND RunType.fRunTypeName='Calibration'",
+                 seq);
+
+    TSQLResult *res = serv.Query(query);
+    if (!res)
+    {
+        cout << "ERROR - Query failed: " << query << endl;
+        return 2;
+    }
+
+    TSQLRow *row = res->Next();
+    if (!row)
+    {
+        cout << "ERROR - Query failed: " << query << endl;
+        return 2;
+    }
+
+    Float_t ratiocalib = 100.*nevts/atof((*row)[0]);
+
+    TString ratiocal = Form("%.1f", ratiocalib);
+
+    delete res;
 
     cout << "Sequence #" << seq << endl;
@@ -243,68 +249,32 @@
     cout << "  Mean Conv inner:   " << meanconvinner << endl;
     cout << "  Mean Conv outer:   " << meanconvouter << endl;
+    cout << "  Ratio Calib Evts:  " << ratiocal << endl;
 
     //inserting or updating the information in the database
-    TString query;
-    if (!ExistStr(serv, "fSequenceFirst", "Calibration", seq))
-    {
-        query = Form("INSERT Calibration SET"
-                     " fSequenceFirst=%d,"
-                     " fUnsuitableInner=%d, "
-                     " fUnsuitableOuter=%d, "
-                     " fUnreliableInner=%d, "
-                     " fUnreliableOuter=%d, "
-                     " fIsolatedInner=%d, "
-                     " fIsolatedOuter=%d, "
-                     " fIsolatedMaxCluster=%d, "
-                     " fArrTimeMeanInner=%s, "
-                     " fArrTimeRmsInner=%s, "
-                     " fArrTimeMeanOuter=%s, "
-                     " fArrTimeRmsOuter=%s, "
-                     " fConvFactorInner=%s, "
-                     " fConvFactorOuter=%s ",
-                     seq, (int)unsin, (int)unsout, (int)unrin,
-                     (int)unrout, (int)isoin, (int)isoout,
-                     (int)clumax,
-                     meaninner.Data(), rmsinner.Data(),
-                     meanouter.Data(), rmsouter.Data(),
-                     meanconvinner.Data(), meanconvouter.Data());
-    }
-    else
-    {
-        query = Form("UPDATE Calibration SET"
-                     " fUnsuitableInner=%d, "
-                     " fUnsuitableOuter=%d, "
-                     " fUnreliableInner=%d, "
-                     " fUnreliableOuter=%d, "
-                     " fIsolatedInner=%d, "
-                     " fIsolatedOuter=%d, "
-                     " fIsolatedMaxCluster=%d, "
-                     " fArrTimeMeanInner=%s, "
-                     " fArrTimeRmsInner=%s, "
-                     " fArrTimeMeanOuter=%s, "
-                     " fArrTimeRmsOuter=%s, "
-                     " fConvFactorInner=%s, "
-                     " fConvFactorOuter=%s "
-                     " WHERE fSequenceFirst=%d ",
-                     (int)unsin, (int)unsout, (int)unrin,(int)unrout,
-                     (int)isoin, (int)isoout, (int)clumax,
-                     meaninner.Data(), rmsinner.Data(),
-                     meanouter.Data(), rmsouter.Data(),
-                     meanconvinner.Data(), meanconvouter.Data(),
-                     seq);
-    }
-
-    if (dummy)
-        return 1;
-
-    TSQLResult *res = serv.Query(query);
-    if (!res)
-    {
-        cout << "ERROR - Query failed: " << query << endl;
-        return 2;
-    }
-    delete res;
-
-    return 1;
+    TString vars =
+        Form(" fSequenceFirst=%d,"
+             " fUnsuitableInner=%d, "
+             " fUnsuitableOuter=%d, "
+             " fUnreliableInner=%d, "
+             " fUnreliableOuter=%d, "
+             " fIsolatedInner=%d, "
+             " fIsolatedOuter=%d, "
+             " fIsolatedMaxCluster=%d, "
+             " fArrTimeMeanInner=%s, "
+             " fArrTimeRmsInner=%s, "
+             " fArrTimeMeanOuter=%s, "
+             " fArrTimeRmsOuter=%s, "
+             " fConvFactorInner=%s, "
+             " fConvFactorOuter=%s, "
+             " fRatioCalEvents=%s ",
+             seq, (int)unsin, (int)unsout, (int)unrin,
+             (int)unrout, (int)isoin, (int)isoout, (int)clumax,
+             meaninner.Data(), rmsinner.Data(),
+             meanouter.Data(), rmsouter.Data(),
+             meanconvinner.Data(), meanconvouter.Data(),
+             ratiocal.Data());
+
+    return serv.InsertUpdate("Calibration", "fSequenceFirst",
+                             Form("%d", seq), vars.Data()) ? 1 : 2;
 }
 
@@ -313,5 +283,5 @@
     TEnv env("sql.rc");
 
-    MSQLServer serv(env);
+    MSQLMagic serv(env);
     if (!serv.IsConnected())
     {
@@ -327,4 +297,6 @@
     cout << endl;
 
-    return Process(serv, fname, dummy);
+    serv.SetIsDummy(dummy);
+
+    return Process(serv, fname);
 }
Index: /trunk/MagicSoft/Mars/datacenter/macros/fillsignal.C
===================================================================
--- /trunk/MagicSoft/Mars/datacenter/macros/fillsignal.C	(revision 8926)
+++ /trunk/MagicSoft/Mars/datacenter/macros/fillsignal.C	(revision 8927)
@@ -19,5 +19,5 @@
 !   Author(s): Daniela Dorner, 04/2005 <mailto:dorner@astro.uni-wuerzburg.de>
 !
-!   Copyright: MAGIC Software Development, 2000-2006
+!   Copyright: MAGIC Software Development, 2000-2008
 !
 !
@@ -66,5 +66,5 @@
 #include <TSQLResult.h>
 
-#include "MSQLServer.h"
+#include "MSQLMagic.h"
 
 #include "MStatusArray.h"
@@ -77,5 +77,5 @@
 using namespace std;
 
-int Process(MSQLServer &serv, TString fname, Bool_t dummy)
+int Process(MSQLMagic &serv, TString fname)
 {
     TFile file(fname, "READ");
@@ -331,16 +331,14 @@
     // the macro fillcalib.C in the script fillcallisto
     // and so the table Calibration is always updated
-    TString query = Form("UPDATE Calibration SET "
-                         " fMeanPedRmsInner=%s,   fMeanPedRmsOuter=%s,  "
-                         " fMeanSignalInner=%s,   fMeanSignalOuter=%s,  "
-                         " fPulsePosMean=%s,      fPulsePosRms=%s,      "
-                         " fPulsePosCheckMean=%s, fPulsePosCheckRms=%s, "
-                         //" fPulsePosHiMean=%s,    fPulsePosHiRms=%s,    "
-                         //" fPulsePosLoMean=%s,    fPulsePosLoRms=%s,    "
-                         " fPulsePosOffMed=%s,    fPulsePosOffDev=%s,   "
-                         " fHiLoGainRatioMed=%s,  fHiLoGainRatioDev=%s,  "
-                         " fUnsuitable50=%d,  fUnsuitable01=%d, "
-                         " fUnsuitableMax=%s, fDeadMax=%s "
-                         " WHERE fSequenceFirst='%d' ",
+    TString vars = Form(" fMeanPedRmsInner=%s,   fMeanPedRmsOuter=%s,  "
+                        " fMeanSignalInner=%s,   fMeanSignalOuter=%s,  "
+                        " fPulsePosMean=%s,      fPulsePosRms=%s,      "
+                        " fPulsePosCheckMean=%s, fPulsePosCheckRms=%s, "
+                        //" fPulsePosHiMean=%s,    fPulsePosHiRms=%s,    "
+                        //" fPulsePosLoMean=%s,    fPulsePosLoRms=%s,    "
+                        " fPulsePosOffMed=%s,    fPulsePosOffDev=%s,   "
+                        " fHiLoGainRatioMed=%s,  fHiLoGainRatioDev=%s, "
+                        " fUnsuitable50=%d,      fUnsuitable01=%d, "
+                        " fUnsuitableMax=%s,     fDeadMax=%s "
                          meanrmsinner.Data(),  meanrmsouter.Data(),
                          meansiginner.Data(),  meansigouter.Data(),
@@ -349,21 +347,10 @@
                          //meanpulhi.Data(),     rmspulhi.Data(),
                          //meanpullo.Data(),     rmspullo.Data(),
-                         medpuloff.Data(),    devpuloff.Data(),
-                         medhilocal.Data(),   devhilocal.Data(),
-                         unsuitable50, unsuitable01,
-                         unsuitablemax.Data(), deadmax.Data(),
-                         seq);
-
-    if (dummy)
-        return 1;
-
-    TSQLResult *res = serv.Query(query);
-    if (!res)
-    {
-        cout << "ERROR - Query failed: " << query << endl;
-        return 2;
-    }
-    delete res;
-    return 1;
+                         medpuloff.Data(),     devpuloff.Data(),
+                         medhilocal.Data(),    devhilocal.Data(),
+                         unsuitable50,         unsuitable01,
+                         unsuitablemax.Data(), deadmax.Data());
+
+    return serv.Update("Calibration", vars, Form("fSequenceFirst=%d", seq), vars);
 }
 
@@ -372,5 +359,5 @@
     TEnv env("sql.rc");
 
-    MSQLServer serv(env);
+    MSQLMagic serv(env);
     if (!serv.IsConnected())
     {
@@ -386,5 +373,7 @@
     cout << endl;
 
+    serv.SetIsDummy(dummy);
+
     //process file
-    return Process(serv, fname, dummy);
+    return Process(serv, fname);
 }
Index: /trunk/MagicSoft/Mars/datacenter/macros/fillstar.C
===================================================================
--- /trunk/MagicSoft/Mars/datacenter/macros/fillstar.C	(revision 8926)
+++ /trunk/MagicSoft/Mars/datacenter/macros/fillstar.C	(revision 8927)
@@ -70,5 +70,5 @@
 #include <TSQLRow.h>
 
-#include "MSQLServer.h"
+#include "MSQLMagic.h"
 
 #include "MHCamera.h"
@@ -80,34 +80,5 @@
 using namespace std;
 
-// --------------------------------------------------------------------------
-//
-// Checks whether an entry is already existing
-//
-Bool_t ExistStr(MSQLServer &serv, const char *column, const char *table, Int_t test)
-{
-    TString query(Form("SELECT %s FROM %s WHERE %s='%d'", column, table, column, test));
-    TSQLResult *res = serv.Query(query);
-    if (!res)
-        return kFALSE;
-
-    TSQLRow *row;
-
-    Bool_t rc = kFALSE;
-    while ((row=res->Next()))
-    {
-        if ((*row)[0])
-        {
-            rc = kTRUE;
-            break;
-        }
-    }
-
-    delete res;
-
-    return rc;
-}
-
-
-int Process(MSQLServer &serv, TString fname, Bool_t dummy)
+int Process(MSQLMagic &serv, TString fname)
 {
     TFile file(fname, "READ");
@@ -310,81 +281,33 @@
     cout << "  Skybrightness            " << skybrightnessmed << " +/- " << skybrightnessrms  << endl;
 
-    TString query;
-    if (!ExistStr(serv, "fSequenceFirst", "Star", seq))
-    {
-        query = Form("INSERT Star SET"
-                     " fSequenceFirst=%d,"
-                     " fMeanNumberIslands=%s, "
-                     " fRatio=%s, "
-                     " fMuonNumber=%d, "
-                     " fEffOnTime=%d, "
-                     " fMuonRate=%s, "
-                     " fPSF=%s, "
-                     " fDataRate=%d, "
-                     " fSparkRate=%s, "
-                     " fMaxHumidity=%s ,"
-                     " fAvgTemperature=%s ,"
-                     " fAvgWindSpeed=%s ,"
-                     " fNumStarsMed=%s ,"
-                     " fNumStarsRMS=%s ,"
-                     " fNumStarsCorMed=%s ,"
-                     " fNumStarsCorRMS=%s ,"
-                     " fBrightnessMed=%s ,"
-                     " fBrightnessRMS=%s ,"
-                     " fInhomogeneity=%s ",
-                     seq, islands.Data(), ratio.Data(),
-                     num, effontime,
-                     muonrate.Data(), PSF.Data(),
-                     datarate, sparkrate.Data(), maxhum.Data(),
-                     avgtemp.Data(), avgwind.Data(),
-                     numstarsmed.Data(), numstarsrms.Data(),
-                     numcorsmed.Data(), numcorsrms.Data(),
-                     skybrightnessmed.Data(), skybrightnessrms.Data(),
-                     inhomogen.Data());
-    }
-    else
-    {
-        query = Form("UPDATE Star SET"
-                     " fMeanNumberIslands=%s, "
-                     " fRatio=%s, "
-                     " fMuonNumber=%d, "
-                     " fEffOnTime=%d, "
-                     " fMuonRate=%s, "
-                     " fPSF=%s, "
-                     " fDataRate=%d, "
-                     " fSparkRate=%s, "
-                     " fMaxHumidity=%s, "
-                     " fNumStarsMed=%s ,"
-                     " fNumStarsRMS=%s ,"
-                     " fNumStarsCorMed=%s ,"
-                     " fNumStarsCorRMS=%s ,"
-                     " fBrightnessMed=%s ,"
-                     " fBrightnessRMS=%s ,"
-                     " fInhomogeneity=%s "
-                     " WHERE fSequenceFirst=%d ",
-                     islands.Data(), ratio.Data(),
-                     num, effontime,
-                     muonrate.Data(), PSF.Data(),
-                     datarate, sparkrate.Data(), maxhum.Data(),
-                     numstarsmed.Data(), numstarsrms.Data(),
-                     numcorsmed.Data(), numcorsrms.Data(),
-                     skybrightnessmed.Data(), skybrightnessrms.Data(),
-                     inhomogen.Data(), seq);
-    }
-
-//    cout << "Q: " << query << endl;
-
-    if (dummy)
-        return 1;
-
-    TSQLResult *res = serv.Query(query);
-    if (!res)
-    {
-        cout << "ERROR - Query failed: " << query << endl;
-        return 2;
-    }
-    delete res;
-
-    return 1;
+    TString vars = Form(" fMeanNumberIslands=%s,"
+                        " fRatio=%s,"
+                        " fMuonNumber=%d,"
+                        " fEffOnTime=%d,"
+                        " fMuonRate=%s,"
+                        " fPSF=%s,"
+                        " fDataRate=%d,"
+                        " fSparkRate=%s,"
+                        " fMaxHumidity=%s,"
+                        " fAvgTemperature=%s,"
+                        " fAvgWindSpeed=%s,"
+                        " fNumStarsMed=%s,"
+                        " fNumStarsRMS=%s,"
+                        " fNumStarsCorMed=%s,"
+                        " fNumStarsCorRMS=%s,"
+                        " fBrightnessMed=%s,"
+                        " fBrightnessRMS=%s,"
+                        " fInhomogeneity=%s ",
+                        islands.Data(), ratio.Data(),
+                        num, effontime,
+                        muonrate.Data(), PSF.Data(),
+                        datarate, sparkrate.Data(), maxhum.Data(),
+                        avgtemp.Data(), avgwind.Data(),
+                        numstarsmed.Data(), numstarsrms.Data(),
+                        numcorsmed.Data(), numcorsrms.Data(),
+                        skybrightnessmed.Data(), skybrightnessrms.Data(),
+                        inhomogen.Data());
+
+    return serv.InsertUpdate("Star", "fSequenceFirst", Form("%d", seq), vars);
 }
 
@@ -393,5 +316,5 @@
     TEnv env("sql.rc");
 
-    MSQLServer serv(env);
+    MSQLMagic serv(env);
     if (!serv.IsConnected())
     {
@@ -401,5 +324,5 @@
 
     cout << "fillstar" << endl;
-    cout << "---------" << endl;
+    cout << "--------" << endl;
     cout << endl;
     cout << "Connected to " << serv.GetName() << endl;
@@ -407,4 +330,6 @@
     cout << endl;
 
-    return Process(serv, fname, dummy);
+    serv.SetIsDummy(dummy);
+
+    return Process(serv, fname);
 }
Index: /trunk/MagicSoft/Mars/datacenter/macros/plotstat.C
===================================================================
--- /trunk/MagicSoft/Mars/datacenter/macros/plotstat.C	(revision 8926)
+++ /trunk/MagicSoft/Mars/datacenter/macros/plotstat.C	(revision 8927)
@@ -1,4 +1,4 @@
 /* ======================================================================== *\
-! $Name: not supported by cvs2svn $:$Id: plotstat.C,v 1.4 2006-11-02 17:44:08 tbretz Exp $
+! $Name: not supported by cvs2svn $:$Id: plotstat.C,v 1.5 2008-06-06 17:02:07 tbretz Exp $
 ! --------------------------------------------------------------------------
 !
@@ -21,5 +21,5 @@
 !   Author(s): Daniela Dorner, 02/2006 <mailto:dorner@astro.uni-wuerzburg.de>
 !
-!   Copyright: MAGIC Software Development, 2000-2006
+!   Copyright: MAGIC Software Development, 2000-2008
 !
 !
@@ -47,5 +47,5 @@
 #include <iomanip>
 
-#include <TH1.h>
+#include <TH2.h>
 #include <TEnv.h>
 #include <TPad.h>
@@ -67,19 +67,26 @@
 using namespace std;
 
+TString GetFullQuery(TString query, TString from="", TString to="")
+{
+    if (from.IsNull())
+        return query;
+
+    if (!query.Contains("where", TString::kIgnoreCase))
+        query += " where ";
+    else
+        query += " and ";
+
+    query += " fRunStart>'";
+    query += from;
+    query += "' and fRunStart<'";
+    query += to;
+    query += "'";
+
+    return query;
+}
+
 Double_t GetTime(MSQLServer &serv, TString query, TString from="", TString to="")
 {
-    if (!from.IsNull())
-    {
-        if (!query.Contains("where", TString::kIgnoreCase))
-            query += " where ";
-        else
-            query += " and ";
-
-        query += " fRunStart>'";
-        query += from;
-        query += "' and fRunStart<'";
-        query += to;
-        query += "'";
-    }
+    query = GetFullQuery(query, from, to);
 
     TSQLResult *res = serv.Query(query);
@@ -100,6 +107,54 @@
     const char *time = (*row)[0];
 
+    const Double_t rc = time ? atof(time) : 0;
+
     delete res;
-    return time ? atof(time) : 0;
+    return rc<0 || rc>200 ? 0 : rc;
+}
+
+TArrayD GetObsDist(MSQLServer &serv, TString from="", TString to="")
+{
+    // 8: Sequenced RunTime per source and night
+    //query[8]  = "select SUM(TIME_TO_SEC(TIMEDIFF(fRunStop,fRunStart)))/3600, ";
+    //query[8] += "DATE_FORMAT(ADDDATE(fRunStart,Interval 12 hour), '%Y-%m-%d') as Start,";
+    //query[8] += "from RunData where fRunTypeKEY=2 and fExcludedFDAKEY=1 group by Start, fSourceKEY";
+
+    TString query;
+
+    query  = "SELECT SUM(fRunTime)/3600, ";
+    query += "DATE_FORMAT(ADDDATE(fRunStart, INTERVAL 12 hour), '%Y-%m-%d') AS Start ";
+    query += "FROM Sequences ";
+
+    query  = GetFullQuery(query, from, to);
+    query += " GROUP BY Start, fSourceKEY";
+
+
+    TSQLResult *res = serv.Query(query);
+    if (!res)
+    {
+        cout << "ERROR - Query failed: " << query << endl;
+        return -1;
+    }
+
+    TSQLRow *row = 0;
+
+    TArrayD arr;
+
+    while ((row=res->Next()))
+    {
+        const char *time = (*row)[0];
+
+        const Double_t rc = time ? atof(time) : 0;
+
+        if (rc>0 && rc<200)
+        {
+            arr.Set(arr.GetSize()+1);
+            arr[arr.GetSize()-1] = rc;
+        }
+    }
+
+    delete res;
+
+    return arr;
 }
 
@@ -265,8 +320,5 @@
 
     // 0: All data for which are files available
-    query[0]  = "select ";
-    query[0] += "SUM(if(TIME_TO_SEC(fRunStop)-TIME_TO_SEC(fRunStart)<0, ";
-    query[0] += "TIME_TO_SEC(fRunStop)-TIME_TO_SEC(fRunStart)+24*60*60, ";
-    query[0] += "TIME_TO_SEC(fRunStop)-TIME_TO_SEC(fRunStart)))/3600 ";
+    query[0]  = "select SUM(TIME_TO_SEC(TIMEDIFF(fRunStop, fRunStart)))/3600 ";
     query[0] += "from RunData left join RunProcessStatus on RunData.fRunNumber=RunProcessStatus.fRunNumber ";
     query[0] += "where fRunTypeKey=2 and ";
@@ -274,15 +326,9 @@
 
     // 1: All data
-    query[1]  = "select ";
-    query[1] += "SUM(if(TIME_TO_SEC(fRunStop)-TIME_TO_SEC(fRunStart)<0, ";
-    query[1] += "TIME_TO_SEC(fRunStop)-TIME_TO_SEC(fRunStart)+24*60*60, ";
-    query[1] += "TIME_TO_SEC(fRunStop)-TIME_TO_SEC(fRunStart)))/3600 ";
+    query[1]  = "select SUM(TIME_TO_SEC(TIMEDIFF(fRunStop,fRunStart)))/3600 ";
     query[1] += "from RunData where fRunTypeKEY=2";
 
     // 2: All data which is not excluded
-    query[2]  = "select ";
-    query[2] += "SUM(if(TIME_TO_SEC(fRunStop)-TIME_TO_SEC(fRunStart)<0, ";
-    query[2] += "TIME_TO_SEC(fRunStop)-TIME_TO_SEC(fRunStart)+24*60*60, ";
-    query[2] += "TIME_TO_SEC(fRunStop)-TIME_TO_SEC(fRunStart)))/3600 ";
+    query[2]  = "select SUM(TIME_TO_SEC(TIMEDIFF(fRunStop,fRunStart)))/3600 ";
     query[2] += "from RunData where fRunTypeKEY=2 and fExcludedFDAKEY=1";
 
@@ -327,12 +373,20 @@
 
     TH1F h[8];
-
-    MBinning bins(last-first+1, first-0.5, last+0.5);
+    TH2F h8;
+
+    MBinning binsp(last-first+1, first-0.5, last+0.5);
+    MBinning binst(4*7, 0, 7);
     for (int i=0; i<8; i++)
     {
-        bins.Apply(h[i]);
+        binsp.Apply(h[i]);
         h[i].SetName(Form("H%d", i));
         h[i].SetDirectory(0);
     }
+
+    MH::SetBinning(&h8, &binsp, &binst);
+    h8.SetNameTitle("ObsTime", "Distribution of observation time per exposure");
+    h8.SetXTitle("Obs. time per exposure [h]");
+    h8.SetYTitle("Counts");
+    h8.SetDirectory(0);
 
     Int_t period = 0;
@@ -352,4 +406,9 @@
                 for (int i=0; i<8; i++)
                     h[i].Fill(period-1, GetTime(serv, query[i], a, b));
+
+                TArrayD arr(GetObsDist(serv, a, b));
+
+                for (int i=0; i<arr.GetSize(); i++)
+                    h8.Fill(period-1, arr[i]);
             }
 
@@ -369,4 +428,28 @@
     h[4].Add(&h[5]);
     h[6].Add(&h[7]);
+
+    // --------------------------------------------
+
+    TCanvas &c0 = d.AddTab("ObsDist");
+    c0.SetFillColor(kWhite);
+
+    gPad->SetBorderMode(0);
+    gPad->SetFrameBorderMode(0);
+    gPad->SetFillColor(kWhite);
+    gPad->SetRightMargin(0.01);
+    //gPad->SetTopMargin(0.02);
+    //gPad->SetLeftMargin(0.09);
+    //gPad->SetBottomMargin(0.12);
+    gPad->SetGridx();
+    gPad->SetGridy();
+    gPad->SetLogy();
+
+    TH1 * p = h8.ProjectionY();
+
+    p->SetDirectory(0);
+    p->SetBit(kCanDelete);
+    p->Draw();
+
+    // --------------------------------------------
 
     TCanvas &c1 = d.AddTab("Hist");
@@ -441,4 +524,6 @@
     DrawCake(h);
 
+    // --------------------------------------------
+
     TCanvas &cx = d.AddTab("All");
     cx.SetBorderMode(0);
@@ -451,10 +536,20 @@
     for (int i=0; i<h[0].GetNbinsX(); i++)
     {
-        TCanvas &c = d.AddTab(Form("P%d", TMath::Nint(h[0].GetBinCenter(i+1))));
-
+        const Int_t num = TMath::Nint(h[0].GetBinCenter(i+1));
+
+        TCanvas &c = d.AddTab(Form("P%d", num));
         c.SetBorderMode(0);
         c.SetFrameBorderMode(0);
-        c.SetPad(0.25, 0, 0.75, 1.0);
         c.SetFillColor(kWhite);
+
+        c.cd();
+
+        TPad *pad1 = new TPad(Form("Pad%da", num), "", 0.05, 0, 0.55, 1);
+        pad1->SetBorderMode(0);
+        pad1->SetFrameBorderMode(0);
+        pad1->SetFillColor(kWhite);
+        pad1->SetBit(kCanDelete);
+        pad1->Draw();
+        pad1->cd();
 
         DrawCake(h, i+1, i+1);
@@ -465,4 +560,41 @@
             DrawCake(h, i+1, i+1);
         }
+
+        c.cd();
+
+        TPad *pad2 = new TPad(Form("Pad%db", num), "", 0.6, 0.02, 1, 0.48);
+        pad2->SetBorderMode(0);
+        pad2->SetFrameBorderMode(0);
+        pad2->SetFillColor(kWhite);
+        pad2->SetBit(kCanDelete);
+        pad2->SetGridx();
+        pad2->SetGridy();
+        pad2->SetRightMargin(0.01);
+        pad2->Draw();
+        pad2->cd();
+
+        TH1 * p = h8.ProjectionY(Form("Obs%d", num), i+1, i+1);
+
+        p->Rebin(2);
+        p->SetBit(TH1::kNoStats);
+        p->SetTitle("");
+        p->SetXTitle("Obs. time per exposure [h]");
+        p->SetYTitle("Counts");
+        p->SetDirectory(0);
+        p->SetBit(kCanDelete);
+        p->Draw();
+
+        /*
+        c.cd();
+
+        TPaveText *pave = new TPaveText(0.6, 0.52, 0.98, 0.98);
+        pave->SetBorderSize(1);
+        pave->AddText(Form("Period: %d", num))->SetTextAlign(22);
+        pave->AddSeperator();
+        pave->AddText(Form("Start: %s", num));
+        pave->AddText(Form("End:   %s", num));
+        pave->SetBit(kCanDelete);
+        pave->Draw();
+        */
     }
 
