Index: /trunk/MagicSoft/Mars/Changelog
===================================================================
--- /trunk/MagicSoft/Mars/Changelog	(revision 8211)
+++ /trunk/MagicSoft/Mars/Changelog	(revision 8212)
@@ -18,4 +18,13 @@
 
                                                  -*-*- END OF LINE -*-*-
+
+ 2006/11/03 Thomas Bretz
+
+   * datacenter/macros/plotoptical.C, datacenter/macros/plotdb.C,
+     datacenter/macros/plotrundb.C:
+     - improved grouping
+     - added comments
+
+
 
  2006/11/02 Daniela Dorner
Index: /trunk/MagicSoft/Mars/datacenter/macros/plotdb.C
===================================================================
--- /trunk/MagicSoft/Mars/datacenter/macros/plotdb.C	(revision 8211)
+++ /trunk/MagicSoft/Mars/datacenter/macros/plotdb.C	(revision 8212)
@@ -1,4 +1,4 @@
 /* ======================================================================== *\
-! $Name: not supported by cvs2svn $:$Id: plotdb.C,v 1.31 2006-11-01 08:53:26 tbretz Exp $
+! $Name: not supported by cvs2svn $:$Id: plotdb.C,v 1.32 2006-11-03 10:52:36 tbretz Exp $
 ! --------------------------------------------------------------------------
 !
@@ -84,4 +84,5 @@
 {
 public:
+    // Possible constants to group-by (average) over a certain period
     enum GroupBy_t
     {
@@ -92,18 +93,20 @@
         kGroupByWeek,
         kGroupByMonth,
+        kGroupBySeason,
         kGroupByYear
     };
+
 private:
-    MSQLMagic &fServer;
-
-    MDataSet *fDataSet;
-
-    TString   fPrimaryDate;
-    TString   fPrimaryNumber;
-    TString   fSecondary;
-
-    TString   fRequestFrom;
-    TString   fRequestTo;
-    Int_t     fRequestPeriod;
+    MSQLMagic &fServer;         // Reference to the sql-server class
+
+    MDataSet *fDataSet;         // A possible dtaset to highlite single points
+
+    TString   fPrimaryDate;     // The name of the data we plot
+    TString   fPrimaryNumber;   // The corresponding name for the key number
+    TString   fSecondary;       // The value versus which the second plot is made
+
+    TString   fRequestFrom;     // Start of a requested date range
+    TString   fRequestTo;       // End of a requested date range
+    Int_t     fRequestPeriod;   // A possible requested period
 
     Float_t   fPlotMin;
@@ -113,16 +116,22 @@
     Float_t   fHistMax;
 
-    TString   fDescription;
-    TString   fNameTab;
-
-    TString   fCondition;
-    GroupBy_t fGroupBy;
-
+    TString   fDescription;     // The description (title) of the plot
+    TString   fNameTab;         // The name of the tab in the display
+
+    TString   fCondition;       // An additional condition added to the query
+    GroupBy_t fGroupBy;         // A possible Group-By flag
+
+    // --------------------------------------------------------------------------
+    //
+    // Function to plot the result of the query
+    //
     void PlotTable(TSQLResult &res, TString name, Float_t fmin, Float_t fmax, Float_t resolution)
     {
+        // Enable all otions in the statistics box
         gStyle->SetOptStat(111111);
 
         TSQLRow *row;
 
+        // Create TGraph objects
         TGraph &gt = res.GetFieldCount()>4 ? *new TGraphErrors : *new TGraph;
         gt.SetNameTitle(name, Form("%s vs Time", name.Data()));
@@ -148,6 +157,8 @@
         Int_t last  = -1;
 
+        // Loop over the data
         while ((row=res.Next()))
         {
+            // Get all fields of this row
             const char *date = (*row)[0];
             const char *zd   = (*row)[1];
@@ -158,20 +169,27 @@
                 continue;
 
+            // check if date is valid
             MTime t(date);
             if (!t.SetSqlDateTime(date))
                 continue;
 
+            // check if it belongs to the requested MAGIC period
             if (fRequestPeriod>0 && MAstro::GetMagicPeriod(t.GetMjd())!=fRequestPeriod)
                 continue;
 
+            // Get axis range
             if (first<0)
                 first = TMath::Nint(TMath::Floor(t.GetMjd()));
             last = TMath::Nint(TMath::Ceil(t.GetMjd()));
 
+            // Convert a possible key number into a integer
             UInt_t seq = snum ? atoi(snum) : 0;
-    
+
+            // convert primary and secondary value into floats
             Float_t value = atof(val);
             Float_t zenith = atof(zd);
 
+            // If a datset is given add the point to the special TGraphs
+            // used for highliting these dates
             if (fDataSet)
             {
@@ -189,7 +207,9 @@
             }
 
+            // Add Data to TGraph
             gt.SetPoint(gt.GetN(), t.GetAxisTime(), value);
             gz.SetPoint(gz.GetN(), zenith, value);
 
+            // Set error-bar, if one
             if (verr)
                 static_cast<TGraphErrors&>(gt).SetPointError(gt.GetN()-1, 0, atof(verr));
@@ -207,10 +227,13 @@
         gROOT->SetSelectedPad(0);
 
+        // Create a TCanvas or open a new tab
         TString title = fNameTab.IsNull() ? name(name.First('.')+2, name.Length()) : fNameTab;
         TCanvas &c = fDisplay ? fDisplay->AddTab(title) : *new TCanvas;
+        // Set fillcolor, remove border and divide pad
         c.SetFillColor(kWhite);
         c.SetBorderMode(0);
         c.Divide(1,2);
 
+        // Output mean and rms to console
         cerr << setprecision(4) << setw(10) << title << ":   ";
         if (gt.GetN()==0)
@@ -228,4 +251,6 @@
 
         TVirtualPad *pad = gPad;
+
+        // draw contants of pad 2 (counting starts at 0)
         pad->cd(2);
         gPad->SetBorderMode(0);
@@ -237,4 +262,5 @@
         gPad->SetBottomMargin(0.08);
 
+        // format axis
         TH1 *h = gt.GetHistogram();
 
@@ -246,4 +272,5 @@
         h->GetXaxis()->SetLabelOffset(0.01);
 
+        // draw TGraph
         gt.DrawClone("AP");
         if (gt0.GetN()>0)
@@ -252,4 +279,5 @@
             gt1.DrawClone("P");
 
+        // Add lines and text showing the MAGIC periods
         TLine l;
         TText t;
@@ -282,4 +310,5 @@
         //gPad->SaveAs(Form("plotdb-%s.eps", title.Data()));
 
+        // Go back to first (upper) pad, format it and divide it again
         pad->cd(1);
         gPad->SetBorderMode(0);
@@ -288,4 +317,6 @@
 
         TVirtualPad *pad2 = gPad;
+
+        // format left pad
         pad2->cd(1);
         gPad->SetBorderMode(0);
@@ -294,4 +325,5 @@
         gPad->SetGridy();
 
+        // Create histogram
         const Int_t n = resolution>0 ? TMath::Nint((max-min)/resolution) : 50;
 
@@ -299,13 +331,17 @@
         hist.SetDirectory(0);
 
+        // Fill data into histogra,
         for (int i=0; i<gt.GetN(); i++)
             hist.Fill(gt.GetY()[i]);
 
+        // Format histogram
         if (fDescription.IsNull())
             hist.SetXTitle(name);
         hist.SetYTitle("Counts");
 
+        // plot histogram
         hist.DrawCopy("");
 
+        // format right pad
         pad2->cd(2);
         gPad->SetBorderMode(0);
@@ -313,4 +349,5 @@
         gPad->SetGridy();
 
+        // format graph
         TH1 *h2 = gz.GetHistogram();
 
@@ -318,5 +355,7 @@
         h2->SetYTitle(name);
 
+        // draw graph
         gz.DrawClone("AP");
+
         if (gz0.GetN()>0)
             gz0.DrawClone("P");
@@ -369,5 +408,30 @@
 
         TString query="SELECT ";
-        query += valued;
+        switch (fGroupBy)
+        {
+        case kNone:
+        case kGroupByPrimary:
+            query += valued;
+            break;
+        case kGroupByHour:
+            query += Form("DATE_FORMAT(%s, '%%Y-%%m-%%d %%H:30:00') AS %s ", fPrimaryDate.Data(), valued.Data());
+            break;
+        case kGroupByNight:
+            query += Form("DATE_FORMAT(ADDDATE(%s,Interval 12 hour), '%%Y-%%m-%%d 00:00:00') AS %s ", fPrimaryDate.Data(), valued.Data());
+            break;
+        case kGroupByWeek:
+            query += Form("DATE_FORMAT(ADDDATE(%s,Interval 12 hour), '%%x%%v') AS %s ", fPrimaryDate.Data(), valued.Data());
+            break;
+        case kGroupByMonth:
+            query += Form("DATE_FORMAT(ADDDATE(%s,Interval 12 hour), '%%Y-%%m-15 00:00:00') AS %s ", fPrimaryDate.Data(), valued.Data());
+            break;
+        case kGroupBySeason:
+            //query += Form("DATE_FORMAT(ADDDATE(%s,Interval 12 hour), '%%Y-%%m-15 00:00:00') AS %s ", fPrimaryDate.Data(), valued.Data());
+            break;
+        case kGroupByYear:
+            query += Form("DATE_FORMAT(ADDDATE(%s,Interval 12 hour), '%%Y-08-15 00:00:00') AS %s ", fPrimaryDate.Data(), valued.Data());
+            break;
+        }
+
         if (fGroupBy==kNone)
         {
@@ -395,26 +459,4 @@
         }
 
-        switch (fGroupBy)
-        {
-        case kNone:
-        case kGroupByPrimary:
-            break;
-        case kGroupByHour:
-            query += Form(", DATE_FORMAT(%s, '%%Y-%%m-%%d %%H') AS %s ", named.Data(), valued.Data());
-            break;
-        case kGroupByNight:
-            query += Form(", DATE_FORMAT(ADDDATE(%s,Interval 12 hour), '%%Y-%%m-%%d') AS %s ", named.Data(), valued.Data());
-            break;
-        case kGroupByWeek:
-            query += Form(", DATE_FORMAT(ADDDATE(%s,Interval 12 hour), '%%x%%v') AS %s ", named.Data(), valued.Data());
-            break;
-        case kGroupByMonth:
-            query += Form(", DATE_FORMAT(ADDDATE(%s,Interval 12 hour), '%%Y-%%m') AS %s ", named.Data(), valued.Data());
-            break;
-        case kGroupByYear:
-            query += Form(", DATE_FORMAT(ADDDATE(%s,Interval 12 hour), '%%Y') AS %s ", named.Data(), valued.Data());
-            break;
-        }
-
         query += Form("FROM %s ", tabled.Data());
 
@@ -452,5 +494,5 @@
             //query += Form(" HAVING COUNT(%s)=(COUNT(*)+1)/2 ", valuev.Data());
         }
-        query += Form("ORDER BY %s ", fPrimaryDate.Data());
+        query += Form("ORDER BY %s ", valued.Data());
 
 
Index: /trunk/MagicSoft/Mars/datacenter/macros/plotoptical.C
===================================================================
--- /trunk/MagicSoft/Mars/datacenter/macros/plotoptical.C	(revision 8211)
+++ /trunk/MagicSoft/Mars/datacenter/macros/plotoptical.C	(revision 8212)
@@ -1,4 +1,4 @@
 /* ======================================================================== *\
-! $Name: not supported by cvs2svn $:$Id: plotoptical.C,v 1.4 2006-11-01 08:53:26 tbretz Exp $
+! $Name: not supported by cvs2svn $:$Id: plotoptical.C,v 1.5 2006-11-03 10:52:36 tbretz Exp $
 ! --------------------------------------------------------------------------
 !
@@ -28,30 +28,49 @@
 /////////////////////////////////////////////////////////////////////////////
 //
-// plotdb.C
-// ========
-//
-// This macro is used to read quality parameters from the DB and plot them.
-// 
-// The parameters are from the following files:
-// calib*.root:mean conversion factor, mean arrival time, rms arrival time
-// (each parameter for inner and outer camera)
-// signal*.root: mean pedestal rms (for inner and outer camera)
-// star*.root: PSF, # of Muons, Effective OnTime, Muon rate,
-// Ratio MC/Data(MuonSize) and mean number of islands
+// plotoptical.C
+// =============
+//
+// This macro is used to read optical data from the DB and plot them.
 // 
 // In the DB these values are stored in the tables Calibration and Star.
-// 
-// Usage:
-//   .x plotdb.C   --> all values in the DB are plotted
-// You can chose are certain period:
-//   .x plotdb.C(25)   --> all values from period 25 are plotted
+//
+//
+// To plot the whole database simple use:
+//   .x plotdb.C+
+//
+// Plot only data for one source. For the available sources see the database
+//   .x plotdb.C+("source")
+//
+// You can chose are certain MAGIC-period:
+//   .x plotdb.C+(25)   --> all values from period 25 are plotted
+//
+// MAGIC periods correspond to one moon-phase and are defined as
+// moon period-284. For details see MAstro::MoonPeriod and
+// MAstro::MagicPeriod.
+//
 // or a time period from a certain date to a certain date
-//   .x plotdb.C("2004-11-14 00:00:00", "2005-02-28 00:00:00")
+//   .x plotdb.C+("2004-11-14 00:00:00", "2005-02-28 00:00:00")
 //  --> all values from 14.11.2004 0h to 28.2.2005 0h are plotted
-// or all data, but with dataset data highlighted
-//   .x plotdb.C("dataset.txt")
-//  --> the sequences defined in dataset.txt are highlighted (blue:on, red:off)
-//  --> You can also add a dataset-name as last argument to one of the
-//      calls above
+//   .x plotdb.C+("2004-11-14 00:00:00", "2005-02-28 00:00:00", "source")
+//  --> all values from 14.11.2004 0h to 28.2.2005 0h of "source" are plotted
+//
+//
+// For details of the plots produced see the function plotall() below.
+//
+//
+// The plot title and axis-titles are created by:
+//    plot.SetDescription("Title;x", "TabName");
+//
+// Drawing the plot is initiated by
+//    plot.Plot("OpticalData.fExposure", min, max, width);
+//
+// While OpticalData.fExposure can be any kind of variable to plot.
+// min and max are the minimum and maximum of the histogram which is
+// filled and width is the bin-width of this histogram.
+//
+// To group data (average) of a certain period use:
+//    plot.GroupBy(MPlot::kGroupByNight);
+// before initiating the plot.
+//
 //
 // Make sure, that database and password are corretly set in a resource
@@ -84,4 +103,5 @@
 {
 public:
+    // Possible constants to group-by (average) over a certain period
     enum GroupBy_t
     {
@@ -92,18 +112,20 @@
         kGroupByWeek,
         kGroupByMonth,
+        kGroupBySeason,
         kGroupByYear
     };
+
 private:
-    MSQLMagic &fServer;
-
-    MDataSet *fDataSet;
-
-    TString   fPrimaryDate;
-    TString   fPrimaryNumber;
-    TString   fSecondary;
-
-    TString   fRequestFrom;
-    TString   fRequestTo;
-    Int_t     fRequestPeriod;
+    MSQLMagic &fServer;         // Reference to the sql-server class
+
+    MDataSet *fDataSet;         // A possible dtaset to highlite single points
+
+    TString   fPrimaryDate;     // The name of the data we plot
+    TString   fPrimaryNumber;   // The corresponding name for the key number
+    TString   fSecondary;       // The value versus which the second plot is made
+
+    TString   fRequestFrom;     // Start of a requested date range
+    TString   fRequestTo;       // End of a requested date range
+    Int_t     fRequestPeriod;   // A possible requested period
 
     Float_t   fPlotMin;
@@ -113,16 +135,22 @@
     Float_t   fHistMax;
 
-    TString   fDescription;
-    TString   fNameTab;
-
-    TString   fCondition;
-    GroupBy_t fGroupBy;
-
+    TString   fDescription;     // The description (title) of the plot
+    TString   fNameTab;         // The name of the tab in the display
+
+    TString   fCondition;       // An additional condition added to the query
+    GroupBy_t fGroupBy;         // A possible Group-By flag
+
+    // --------------------------------------------------------------------------
+    //
+    // Function to plot the result of the query
+    //
     void PlotTable(TSQLResult &res, TString name, Float_t fmin, Float_t fmax, Float_t resolution)
     {
+        // Enable all otions in the statistics box
         gStyle->SetOptStat(111111);
 
         TSQLRow *row;
 
+        // Create TGraph objects
         TGraph &gt = res.GetFieldCount()>4 ? *new TGraphErrors : *new TGraph;
         gt.SetNameTitle(name, Form("%s vs Time", name.Data()));
@@ -148,6 +176,8 @@
         Int_t last  = -1;
 
+        // Loop over the data
         while ((row=res.Next()))
         {
+            // Get all fields of this row
             const char *date = (*row)[0];
             const char *zd   = (*row)[1];
@@ -158,20 +188,27 @@
                 continue;
 
+            // check if date is valid
             MTime t(date);
             if (!t.SetSqlDateTime(date))
                 continue;
 
+            // check if it belongs to the requested MAGIC period
             if (fRequestPeriod>0 && MAstro::GetMagicPeriod(t.GetMjd())!=fRequestPeriod)
                 continue;
 
+            // Get axis range
             if (first<0)
                 first = TMath::Nint(TMath::Floor(t.GetMjd()));
             last = TMath::Nint(TMath::Ceil(t.GetMjd()));
 
+            // Convert a possible key number into a integer
             UInt_t seq = snum ? atoi(snum) : 0;
-    
+
+            // convert primary and secondary value into floats
             Float_t value = atof(val);
             Float_t zenith = atof(zd);
 
+            // If a datset is given add the point to the special TGraphs
+            // used for highliting these dates
             if (fDataSet)
             {
@@ -189,7 +226,9 @@
             }
 
+            // Add Data to TGraph
             gt.SetPoint(gt.GetN(), t.GetAxisTime(), value);
             gz.SetPoint(gz.GetN(), zenith, value);
 
+            // Set error-bar, if one
             if (verr)
                 static_cast<TGraphErrors&>(gt).SetPointError(gt.GetN()-1, 0, atof(verr));
@@ -207,10 +246,13 @@
         gROOT->SetSelectedPad(0);
 
+        // Create a TCanvas or open a new tab
         TString title = fNameTab.IsNull() ? name(name.First('.')+2, name.Length()) : fNameTab;
         TCanvas &c = fDisplay ? fDisplay->AddTab(title) : *new TCanvas;
+        // Set fillcolor, remove border and divide pad
         c.SetFillColor(kWhite);
         c.SetBorderMode(0);
         c.Divide(1,2);
 
+        // Output mean and rms to console
         cerr << setprecision(4) << setw(10) << title << ":   ";
         if (gt.GetN()==0)
@@ -228,4 +270,6 @@
 
         TVirtualPad *pad = gPad;
+
+        // draw contants of pad 2 (counting starts at 0)
         pad->cd(2);
         gPad->SetBorderMode(0);
@@ -237,4 +281,5 @@
         gPad->SetBottomMargin(0.08);
 
+        // format axis
         TH1 *h = gt.GetHistogram();
 
@@ -246,4 +291,5 @@
         h->GetXaxis()->SetLabelOffset(0.01);
 
+        // draw TGraph
         gt.DrawClone("AP");
         if (gt0.GetN()>0)
@@ -252,4 +298,5 @@
             gt1.DrawClone("P");
 
+        // Add lines and text showing the MAGIC periods
         TLine l;
         TText t;
@@ -282,4 +329,5 @@
         //gPad->SaveAs(Form("plotdb-%s.eps", title.Data()));
 
+        // Go back to first (upper) pad, format it and divide it again
         pad->cd(1);
         gPad->SetBorderMode(0);
@@ -288,4 +336,6 @@
 
         TVirtualPad *pad2 = gPad;
+
+        // format left pad
         pad2->cd(1);
         gPad->SetBorderMode(0);
@@ -294,4 +344,5 @@
         gPad->SetGridy();
 
+        // Create histogram
         const Int_t n = resolution>0 ? TMath::Nint((max-min)/resolution) : 50;
 
@@ -299,13 +350,17 @@
         hist.SetDirectory(0);
 
+        // Fill data into histogra,
         for (int i=0; i<gt.GetN(); i++)
             hist.Fill(gt.GetY()[i]);
 
+        // Format histogram
         if (fDescription.IsNull())
             hist.SetXTitle(name);
         hist.SetYTitle("Counts");
 
+        // plot histogram
         hist.DrawCopy("");
 
+        // format right pad
         pad2->cd(2);
         gPad->SetBorderMode(0);
@@ -313,4 +368,5 @@
         gPad->SetGridy();
 
+        // format graph
         TH1 *h2 = gz.GetHistogram();
 
@@ -318,4 +374,5 @@
         h2->SetYTitle(name);
 
+        // draw graph
         gz.DrawClone("AP");
 
@@ -375,5 +432,30 @@
 
         TString query="SELECT ";
-        query += valued;
+        switch (fGroupBy)
+        {
+        case kNone:
+        case kGroupByPrimary:
+            query += valued;
+            break;
+        case kGroupByHour:
+            query += Form("DATE_FORMAT(%s, '%%Y-%%m-%%d %%H:30:00') AS %s ", fPrimaryDate.Data(), valued.Data());
+            break;
+        case kGroupByNight:
+            query += Form("DATE_FORMAT(ADDDATE(%s,Interval 12 hour), '%%Y-%%m-%%d 00:00:00') AS %s ", fPrimaryDate.Data(), valued.Data());
+            break;
+        case kGroupByWeek:
+            query += Form("DATE_FORMAT(ADDDATE(%s,Interval 12 hour), '%%x%%v') AS %s ", fPrimaryDate.Data(), valued.Data());
+            break;
+        case kGroupByMonth:
+            query += Form("DATE_FORMAT(ADDDATE(%s,Interval 12 hour), '%%Y-%%m-15 00:00:00') AS %s ", fPrimaryDate.Data(), valued.Data());
+            break;
+        case kGroupBySeason:
+            //query += Form("DATE_FORMAT(ADDDATE(%s,Interval 12 hour), '%%Y-%%m-15 00:00:00') AS %s ", fPrimaryDate.Data(), valued.Data());
+            break;
+        case kGroupByYear:
+            query += Form("DATE_FORMAT(ADDDATE(%s,Interval 12 hour), '%%Y-08-15 00:00:00') AS %s ", fPrimaryDate.Data(), valued.Data());
+            break;
+        }
+
         if (fGroupBy==kNone)
         {
@@ -401,33 +483,19 @@
         }
 
-        switch (fGroupBy)
-        {
-        case kNone:
-        case kGroupByPrimary:
-            break;
-        case kGroupByHour:
-            query += Form(", DATE_FORMAT(%s, '%%Y-%%m-%%d %%H') AS %s ", named.Data(), valued.Data());
-            break;
-        case kGroupByNight:
-            query += Form(", DATE_FORMAT(ADDDATE(%s,Interval 12 hour), '%%Y-%%m-%%d') AS %s ", named.Data(), valued.Data());
-            break;
-        case kGroupByWeek:
-            query += Form(", DATE_FORMAT(ADDDATE(%s,Interval 12 hour), '%%x%%v') AS %s ", named.Data(), valued.Data());
-            break;
-        case kGroupByMonth:
-            query += Form(", DATE_FORMAT(ADDDATE(%s,Interval 12 hour), '%%Y-%%m') AS %s ", named.Data(), valued.Data());
-            break;
-        case kGroupByYear:
-            query += Form(", DATE_FORMAT(ADDDATE(%s,Interval 12 hour), '%%Y') AS %s ", named.Data(), valued.Data());
-            break;
-        }
-
         query += Form("FROM %s ", tabled.Data());
 
         TString where(fCondition);
+
+        const Bool_t interval = !fRequestFrom.IsNull() && !fRequestTo.IsNull();
+        if (interval)
+        {
+            if (!where.IsNull())
+                where += " AND ";
+            where += Form("%s BETWEEN '%s' AND '%s' ",
+                          fPrimaryDate.Data(), fRequestFrom.Data(), fRequestTo.Data());
+        }
 
         if (!where.IsNull())
             where += " AND ";
-
         where += fCondition;
         where += " ";
@@ -448,6 +516,5 @@
             //query += Form(" HAVING COUNT(%s)=(COUNT(*)+1)/2 ", valuev.Data());
         }
-        query += Form("ORDER BY %s ", fPrimaryDate.Data());
-
+        query += Form("ORDER BY %s ", valued.Data());
 
         // ------------------------------
@@ -469,8 +536,11 @@
 void plotall(MPlot &plot, TString source)
 {
+    // Setup here the values for timestamp and secondary (upper/right) plot
     plot.SetPrimaryDate("OpticalData.fTimestamp");
     plot.SetPrimaryNumber("OpticalData.fTimestamp");
     plot.SetSecondary("OpticalData.fZenithDistance");
 
+    // This is the condition to take only the "ok" flagged data
+    // and to restrict the query to a given source (if any)
     TString cond = "fStatusKEY=13";
     if (!source.IsNull())
@@ -484,52 +554,68 @@
     plot.SetCondition(cond);
 
+    // Plot exposure
     plot.SetDescription("Exposure;T_{E} [s]", "Expo");
     plot.Plot("OpticalData.fExposure",  0, 900, 60);
 
+    // plot sky level
     plot.SetDescription("Sky Level;B [s^{-1}]", "SkyLvl");
     plot.Plot("OpticalData.fSkyLevel/OpticalData.fExposure", 0, 5.0, 0.01);
 
+    // plot FWHM
     plot.SetDescription("Full Width Half Maximum;FWHM [s^{-1}]", "Fwhm");
     plot.Plot("OpticalData.fFWHM/OpticalData.fExposure",  0, 0.05, 0.001);
 
+    // plot Aperture Radius
     plot.SetDescription("Aperture Radius;R_{A}", "ApRad");
     plot.Plot("OpticalData.fApertureRadius", 0, 10, 1);
 
     /*
-     plot.SetDescription("Instrumental Magnitude;M_{I}\\cdot s^{-1}", "InstMag/s");
-     plot.Plot("OpticalData.fInstrumentalMag/OpticalData.fExposure",  0, 0.2, 0.005);
-
-     plot.SetDescription("Instrumental Magnitude Error;\\sigma_{M}\\cdot s^{-1}", "MagErr/s");
-     plot.Plot("OpticalData.fInstrumentalMagErr/OpticalData.fExposure",  0, 0.01, 0.0002);
-
+     // Plot instrumental magnitude
      plot.SetDescription("Instrumental Magnitude;M_{I}", "InstMag");
      plot.Plot("OpticalData.fInstrumentalMag",  0, 30, 0.5);
 
+     // Plot error of instrumental magnitude
      plot.SetDescription("Instrumental Magnitude Error;\\sigma_{M}", "MagErr");
      plot.Plot("OpticalData.fInstrumentalMagErr",  0, 1, 0.01);
      */
 
+    // Plot magnitude corrected for the exposure
     plot.SetDescription("m_{1};m_{1}", "M1");
     plot.Plot("OpticalData.fInstrumentalMag+2.5*log10(OpticalData.fExposure)", 10, 35, 0.2);
 
+    // Now take out all points named */BL from further queries
+    // And skip all sources the magnitude is not known
     cond += " AND Object.fObjectName NOT LIKE '%/BL' AND NOT ISNULL(Object.fMagnitude) ";
     plot.SetCondition(cond);
 
+    // Formula to calculate the extinction
     TString ext("3080/25.0*pow(10, (OpticalData.fInstrumentalMag+2.5*log10(OpticalData.fExposure)-Object.fMagnitude)/-2.5)");
-    ext += "+0.0028*fZenithDistance-0.08";
-
+    // Add this to correct for the ZA dependancy
+    //    ext += "+0.0028*fZenithDistance-0.08";
+
+    // Group all data of one image together and plot extinction
     plot.SetGroupBy(MPlot::kGroupByPrimary);
     plot.SetDescription("m_{1}-m_{true} (Extinction per Image);m_{1}-m_{true}", "ExtImg");
-    plot.Plot(ext/*Form("AVG(%s)", ext.Data())*/, 0.05, 1.2, 0.01);
-
+    plot.Plot(ext, 0.05, 1.2, 0.01);
+
+    // Group data hourly together and plot extinction
     plot.SetGroupBy(MPlot::kGroupByHour);
     plot.SetDescription("m_{1}-m_{true} (Extinction per Hour);m_{1}-m_{true}", "ExtHour");
-    plot.Plot(ext/*Form("AVG(%s), date_format(fTimeStamp, '%%Y-%%m-%%d %%H') as fTimeStamp", ext.Data())*/,
-              0.5, 1.2, 0.01);
-
+    plot.Plot(ext, 0.5, 1.2, 0.01);
+
+    // Group data hourly together and plot extinction
     plot.SetGroupBy(MPlot::kGroupByNight);
     plot.SetDescription("m_{1}-m_{true} (Extinction per Night);m_{1}-m_{true}", "ExtNight");
-    plot.Plot(ext/*Form("AVG(%s), date_format(adddate(fTimeStamp,Interval 12 hour),'%%Y-%%m-%%d') as fTimeStamp", ext.Data())*/,
-              0.5, 1.2, 0.01);
+    plot.Plot(ext, 0.5, 1.2, 0.01);
+
+    // Group data monthly together and plot extinction
+    plot.SetGroupBy(MPlot::kGroupByMonth);
+    plot.SetDescription("m_{1}-m_{true} (Extinction per Month);m_{1}-m_{true}", "ExtMonth");
+    plot.Plot(ext, 0.5, 1.2, 0.01);
+
+    // Group data yearly together and plot extinction
+    plot.SetGroupBy(MPlot::kGroupByYear);
+    plot.SetDescription("m_{1}-m_{true} (Extinction per Year);m_{1}-m_{true}", "ExtYear");
+    plot.Plot(ext, 0.5, 1.2, 0.01);
 }
 
@@ -556,10 +642,11 @@
 
     MPlot plot(serv);
-//    plot.SetDataSet(dataset);
+    // plot.SetDataSet(dataset);
     plot.SetDisplay(d);
     plot.SetRequestRange(from, to);
     plotall(plot, source);
-    d->SaveAsRoot("plotoptical.root");
-    d->SaveAsPS("plotoptical.ps");
+    // Use this to create output plots automatically
+    //  d->SaveAsRoot("plotoptical.root");
+    //  d->SaveAsPS("plotoptical.ps");
 
     return 1;
@@ -588,10 +675,11 @@
 
     MPlot plot(serv);
-//    plot.SetDataSet(ds);
+    // plot.SetDataSet(ds);
     plot.SetDisplay(d);
     plot.SetRequestRange("", "");
     plotall(plot, source);
-    d->SaveAsRoot("plotoptical.root");
-    d->SaveAsPS("plotoptical.ps");
+    // Use this to create output plots automatically
+    //  d->SaveAsRoot("plotoptical.root");
+    //  d->SaveAsPS("plotoptical.ps");
 
     return 1;
@@ -620,10 +708,12 @@
 
     MPlot plot(serv);
-//    plot.SetDataSet(dataset);
+    // plot.SetDataSet(dataset);
     plot.SetDisplay(d);
     plot.SetRequestPeriod(period);
     plotall(plot, source);
-    d->SaveAsRoot("plotoptical.root");
-    d->SaveAsPS("plotoptical.ps");
+
+    // Use this to create output plots automatically
+    //  d->SaveAsRoot("plotoptical.root");
+    //  d->SaveAsPS("plotoptical.ps");
 
     return 1;
Index: /trunk/MagicSoft/Mars/datacenter/macros/plotrundb.C
===================================================================
--- /trunk/MagicSoft/Mars/datacenter/macros/plotrundb.C	(revision 8211)
+++ /trunk/MagicSoft/Mars/datacenter/macros/plotrundb.C	(revision 8212)
@@ -1,4 +1,4 @@
 /* ======================================================================== *\
-! $Name: not supported by cvs2svn $:$Id: plotrundb.C,v 1.1 2006-11-01 08:54:04 tbretz Exp $
+! $Name: not supported by cvs2svn $:$Id: plotrundb.C,v 1.2 2006-11-03 10:52:36 tbretz Exp $
 ! --------------------------------------------------------------------------
 !
@@ -84,4 +84,5 @@
 {
 public:
+    // Possible constants to group-by (average) over a certain period
     enum GroupBy_t
     {
@@ -92,18 +93,20 @@
         kGroupByWeek,
         kGroupByMonth,
+        kGroupBySeason,
         kGroupByYear
     };
+
 private:
-    MSQLMagic &fServer;
-
-    MDataSet *fDataSet;
-
-    TString   fPrimaryDate;
-    TString   fPrimaryNumber;
-    TString   fSecondary;
-
-    TString   fRequestFrom;
-    TString   fRequestTo;
-    Int_t     fRequestPeriod;
+    MSQLMagic &fServer;         // Reference to the sql-server class
+
+    MDataSet *fDataSet;         // A possible dtaset to highlite single points
+
+    TString   fPrimaryDate;     // The name of the data we plot
+    TString   fPrimaryNumber;   // The corresponding name for the key number
+    TString   fSecondary;       // The value versus which the second plot is made
+
+    TString   fRequestFrom;     // Start of a requested date range
+    TString   fRequestTo;       // End of a requested date range
+    Int_t     fRequestPeriod;   // A possible requested period
 
     Float_t   fPlotMin;
@@ -113,16 +116,22 @@
     Float_t   fHistMax;
 
-    TString   fDescription;
-    TString   fNameTab;
-
-    TString   fCondition;
-    GroupBy_t fGroupBy;
-
+    TString   fDescription;     // The description (title) of the plot
+    TString   fNameTab;         // The name of the tab in the display
+
+    TString   fCondition;       // An additional condition added to the query
+    GroupBy_t fGroupBy;         // A possible Group-By flag
+
+    // --------------------------------------------------------------------------
+    //
+    // Function to plot the result of the query
+    //
     void PlotTable(TSQLResult &res, TString name, Float_t fmin, Float_t fmax, Float_t resolution)
     {
+        // Enable all otions in the statistics box
         gStyle->SetOptStat(111111);
 
         TSQLRow *row;
 
+        // Create TGraph objects
         TGraph &gt = res.GetFieldCount()>4 ? *new TGraphErrors : *new TGraph;
         gt.SetNameTitle(name, Form("%s vs Time", name.Data()));
@@ -148,6 +157,8 @@
         Int_t last  = -1;
 
+        // Loop over the data
         while ((row=res.Next()))
         {
+            // Get all fields of this row
             const char *date = (*row)[0];
             const char *zd   = (*row)[1];
@@ -158,20 +169,27 @@
                 continue;
 
+            // check if date is valid
             MTime t(date);
             if (!t.SetSqlDateTime(date))
                 continue;
 
+            // check if it belongs to the requested MAGIC period
             if (fRequestPeriod>0 && MAstro::GetMagicPeriod(t.GetMjd())!=fRequestPeriod)
                 continue;
 
+            // Get axis range
             if (first<0)
                 first = TMath::Nint(TMath::Floor(t.GetMjd()));
             last = TMath::Nint(TMath::Ceil(t.GetMjd()));
 
+            // Convert a possible key number into a integer
             UInt_t seq = snum ? atoi(snum) : 0;
-    
+
+            // convert primary and secondary value into floats
             Float_t value = atof(val);
             Float_t zenith = atof(zd);
 
+            // If a datset is given add the point to the special TGraphs
+            // used for highliting these dates
             if (fDataSet)
             {
@@ -189,7 +207,9 @@
             }
 
+            // Add Data to TGraph
             gt.SetPoint(gt.GetN(), t.GetAxisTime(), value);
             gz.SetPoint(gz.GetN(), zenith, value);
 
+            // Set error-bar, if one
             if (verr)
                 static_cast<TGraphErrors&>(gt).SetPointError(gt.GetN()-1, 0, atof(verr));
@@ -207,10 +227,13 @@
         gROOT->SetSelectedPad(0);
 
+        // Create a TCanvas or open a new tab
         TString title = fNameTab.IsNull() ? name(name.First('.')+2, name.Length()) : fNameTab;
         TCanvas &c = fDisplay ? fDisplay->AddTab(title) : *new TCanvas;
+        // Set fillcolor, remove border and divide pad
         c.SetFillColor(kWhite);
         c.SetBorderMode(0);
         c.Divide(1,2);
 
+        // Output mean and rms to console
         cerr << setprecision(4) << setw(10) << title << ":   ";
         if (gt.GetN()==0)
@@ -228,4 +251,6 @@
 
         TVirtualPad *pad = gPad;
+
+        // draw contants of pad 2 (counting starts at 0)
         pad->cd(2);
         gPad->SetBorderMode(0);
@@ -237,4 +262,5 @@
         gPad->SetBottomMargin(0.08);
 
+        // format axis
         TH1 *h = gt.GetHistogram();
 
@@ -246,4 +272,5 @@
         h->GetXaxis()->SetLabelOffset(0.01);
 
+        // draw TGraph
         gt.DrawClone("AP");
         if (gt0.GetN()>0)
@@ -252,4 +279,5 @@
             gt1.DrawClone("P");
 
+        // Add lines and text showing the MAGIC periods
         TLine l;
         TText t;
@@ -282,4 +310,5 @@
         //gPad->SaveAs(Form("plotdb-%s.eps", title.Data()));
 
+        // Go back to first (upper) pad, format it and divide it again
         pad->cd(1);
         gPad->SetBorderMode(0);
@@ -288,4 +317,6 @@
 
         TVirtualPad *pad2 = gPad;
+
+        // format left pad
         pad2->cd(1);
         gPad->SetBorderMode(0);
@@ -294,4 +325,5 @@
         gPad->SetGridy();
 
+        // Create histogram
         const Int_t n = resolution>0 ? TMath::Nint((max-min)/resolution) : 50;
 
@@ -299,13 +331,17 @@
         hist.SetDirectory(0);
 
+        // Fill data into histogra,
         for (int i=0; i<gt.GetN(); i++)
             hist.Fill(gt.GetY()[i]);
 
+        // Format histogram
         if (fDescription.IsNull())
             hist.SetXTitle(name);
         hist.SetYTitle("Counts");
 
+        // plot histogram
         hist.DrawCopy("");
 
+        // format right pad
         pad2->cd(2);
         gPad->SetBorderMode(0);
@@ -313,4 +349,5 @@
         gPad->SetGridy();
 
+        // format graph
         TH1 *h2 = gz.GetHistogram();
 
@@ -318,5 +355,7 @@
         h2->SetYTitle(name);
 
+        // draw graph
         gz.DrawClone("AP");
+
         if (gz0.GetN()>0)
             gz0.DrawClone("P");
@@ -369,5 +408,30 @@
 
         TString query="SELECT ";
-        query += valued;
+        switch (fGroupBy)
+        {
+        case kNone:
+        case kGroupByPrimary:
+            query += valued;
+            break;
+        case kGroupByHour:
+            query += Form("DATE_FORMAT(%s, '%%Y-%%m-%%d %%H:30:00') AS %s ", fPrimaryDate.Data(), valued.Data());
+            break;
+        case kGroupByNight:
+            query += Form("DATE_FORMAT(ADDDATE(%s,Interval 12 hour), '%%Y-%%m-%%d 00:00:00') AS %s ", fPrimaryDate.Data(), valued.Data());
+            break;
+        case kGroupByWeek:
+            query += Form("DATE_FORMAT(ADDDATE(%s,Interval 12 hour), '%%x%%v') AS %s ", fPrimaryDate.Data(), valued.Data());
+            break;
+        case kGroupByMonth:
+            query += Form("DATE_FORMAT(ADDDATE(%s,Interval 12 hour), '%%Y-%%m-15 00:00:00') AS %s ", fPrimaryDate.Data(), valued.Data());
+            break;
+        case kGroupBySeason:
+            //query += Form("DATE_FORMAT(ADDDATE(%s,Interval 12 hour), '%%Y-%%m-15 00:00:00') AS %s ", fPrimaryDate.Data(), valued.Data());
+            break;
+        case kGroupByYear:
+            query += Form("DATE_FORMAT(ADDDATE(%s,Interval 12 hour), '%%Y-08-15 00:00:00') AS %s ", fPrimaryDate.Data(), valued.Data());
+            break;
+        }
+
         if (fGroupBy==kNone)
         {
@@ -395,26 +459,4 @@
         }
 
-        switch (fGroupBy)
-        {
-        case kNone:
-        case kGroupByPrimary:
-            break;
-        case kGroupByHour:
-            query += Form(", DATE_FORMAT(%s, '%%Y-%%m-%%d %%H') AS %s ", named.Data(), valued.Data());
-            break;
-        case kGroupByNight:
-            query += Form(", DATE_FORMAT(ADDDATE(%s,Interval 12 hour), '%%Y-%%m-%%d') AS %s ", named.Data(), valued.Data());
-            break;
-        case kGroupByWeek:
-            query += Form(", DATE_FORMAT(ADDDATE(%s,Interval 12 hour), '%%x%%v') AS %s ", named.Data(), valued.Data());
-            break;
-        case kGroupByMonth:
-            query += Form(", DATE_FORMAT(ADDDATE(%s,Interval 12 hour), '%%Y-%%m') AS %s ", named.Data(), valued.Data());
-            break;
-        case kGroupByYear:
-            query += Form(", DATE_FORMAT(ADDDATE(%s,Interval 12 hour), '%%Y') AS %s ", named.Data(), valued.Data());
-            break;
-        }
-
         query += Form("FROM %s ", tabled.Data());
 
@@ -449,5 +491,5 @@
             //query += Form(" HAVING COUNT(%s)=(COUNT(*)+1)/2 ", valuev.Data());
         }
-        query += Form("ORDER BY %s ", fPrimaryDate.Data());
+        query += Form("ORDER BY %s ", valued.Data());
 
 
