Index: /trunk/MagicSoft/Mars/Changelog
===================================================================
--- /trunk/MagicSoft/Mars/Changelog	(revision 8168)
+++ /trunk/MagicSoft/Mars/Changelog	(revision 8169)
@@ -23,4 +23,7 @@
    * datacenter/macros/buildsequenceentries.C:
      - included a fix for 0000-00-00 times in the run-database
+
+   * datacenter/macros/plotdb.C:
+     - implemented a possibility to show average values with RMS
 
 
Index: /trunk/MagicSoft/Mars/datacenter/macros/plotdb.C
===================================================================
--- /trunk/MagicSoft/Mars/datacenter/macros/plotdb.C	(revision 8168)
+++ /trunk/MagicSoft/Mars/datacenter/macros/plotdb.C	(revision 8169)
@@ -1,4 +1,4 @@
 /* ======================================================================== *\
-! $Name: not supported by cvs2svn $:$Id: plotdb.C,v 1.28 2006-10-19 13:57:14 tbretz Exp $
+! $Name: not supported by cvs2svn $:$Id: plotdb.C,v 1.29 2006-10-27 13:36:18 tbretz Exp $
 ! --------------------------------------------------------------------------
 !
@@ -69,8 +69,8 @@
 #include <TFrame.h>
 #include <TStyle.h>
-#include <TGraph.h>
 #include <TCanvas.h>
 #include <TSQLRow.h>
 #include <TSQLResult.h>
+#include <TGraphErrors.h>
 
 #include "MTime.h"
@@ -82,4 +82,13 @@
 class MPlot : public MParContainer
 {
+public:
+    enum GroupBy_t
+    {
+        kNone,
+        kGroupByDay,
+        kGroupByWeek,
+        kGroupByMonth,
+        kGroupByYear
+    };
 private:
     MSQLServer &fServer;
@@ -87,16 +96,19 @@
     MDataSet *fDataSet;
 
-    TString fRequestFrom;
-    TString fRequestTo;
-    Int_t   fRequestPeriod;
-
-    Float_t fPlotMin;
-    Float_t fPlotMax;
-
-    Float_t fHistMin;
-    Float_t fHistMax;
-
-    TString fDescription;
-    TString fNameTab;
+    TString   fRequestFrom;
+    TString   fRequestTo;
+    Int_t     fRequestPeriod;
+
+    Float_t   fPlotMin;
+    Float_t   fPlotMax;
+
+    Float_t   fHistMin;
+    Float_t   fHistMax;
+
+    TString   fDescription;
+    TString   fNameTab;
+
+    TString   fCondition;
+    GroupBy_t fGroupBy;
 
     void PlotTable(TSQLResult &res, TString name, Float_t fmin, Float_t fmax, Float_t resolution)
@@ -106,5 +118,5 @@
         TSQLRow *row;
 
-        TGraph gt;
+        TGraph &gt = res.GetFieldCount()>4 ? *new TGraphErrors : *new TGraph;
         gt.SetNameTitle(name, Form("%s vs Time", name.Data()));
         gt.SetMarkerStyle(kFullDotMedium);
@@ -135,4 +147,5 @@
             const char *val  = (*row)[2];
             const char *snum = (*row)[3];
+            const char *verr = res.GetFieldCount()>4 ? (*row)[5] : 0;
             if (!date || !val || !zd || !snum)
                 continue;
@@ -171,4 +184,7 @@
             gt.SetPoint(gt.GetN(), t.GetAxisTime(), value);
             gz.SetPoint(gz.GetN(), zenith, value);
+
+            if (verr)
+                static_cast<TGraphErrors&>(gt).SetPointError(gt.GetN()-1, 0, atof(verr));
         }
 
@@ -299,5 +315,5 @@
 public:
     MPlot(MSQLServer &server) : fServer(server), fDataSet(NULL),
-        fRequestPeriod(-1), fPlotMin(0), fPlotMax(-1), fHistMin(0), fHistMax(-1)
+        fRequestPeriod(-1), fPlotMin(0), fPlotMax(-1), fHistMin(0), fHistMax(-1), fGroupBy(kNone)
     {
     }
@@ -317,13 +333,11 @@
             fDataSet = new MDataSet(filename);
     }
-    void SetPlotRange(Float_t min, Float_t max, Int_t n=5)
-    { fPlotMin = min; fPlotMax = max; }
-    void SetHistRange(Float_t min, Float_t max)
-    { fHistMin = min; fHistMax = max; }
-    void SetRequestRange(const char *from="", const char *to="")
-    { fRequestFrom = from; fRequestTo = to; }
-    void SetRequestPeriod(Int_t n=-1)
-    { fRequestPeriod = n; }
+    void SetPlotRange(Float_t min, Float_t max, Int_t n=5) { fPlotMin = min; fPlotMax = max; }
+    void SetHistRange(Float_t min, Float_t max) { fHistMin = min; fHistMax = max; }
+    void SetRequestRange(const char *from="", const char *to="") { fRequestFrom = from; fRequestTo = to; }
+    void SetRequestPeriod(Int_t n=-1) { fRequestPeriod = n; }
     void SetDescription(const char *d, const char *t=0) { fDescription = d; fNameTab = t; }
+    void SetCondition(const char *cond="") { fCondition = cond; }
+    void SetGroupBy(GroupBy_t b=kGroupByWeek) { fGroupBy=b; }
 
     Bool_t Plot(const char *value, Float_t min=0, Float_t max=-1, Float_t resolution=0)
@@ -341,15 +355,38 @@
 
         TString query;
-        query  = Form("select %s, %s, %s, Sequences.fSequenceFirst ",    valued.Data(), named2.Data(), valuev.Data());
+        if (fGroupBy==kNone)
+            query = Form("select %s, %s, %s, Sequences.fSequenceFirst ", valued.Data(), named2.Data(), valuev.Data());
+        else
+            query = Form("select %s, AVG(%s), AVG(%s), Sequences.fSequenceFirst, STD(%s), STD(%s) ", valued.Data(), named2.Data(), valuev.Data(), named2.Data(), valuev.Data());
+
+        switch (fGroupBy)
+        {
+        case kNone:
+            break;
+        case kGroupByDay:
+            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 left join %s ", tabled.Data(), tablev.Data());
-        query += Form("on %s.%s=%s.%s ",       tabled.Data(), join.Data(), tablev.Data(), join.Data());
+        query += Form("on %s.%s=%s.%s ", tabled.Data(), join.Data(), tablev.Data(), join.Data());
 
         const Bool_t interval = !fRequestFrom.IsNull() && !fRequestTo.IsNull();
 
-        if (!fDataSet && !interval && tabled=="Star")
+        if (!fDataSet && !interval && tablev=="Star")
         {
             if (!query.Contains("Star.fSequenceFirst"))
                 query += "left join Star on Sequences.fSequenceFirst=Star.fSequenceFirst ";
-            query += "where Star.fEffOnTime>300 ";
+            // This is from a plot PSF/MuonNumber
+            query += "where Star.fMuonNumber>300 ";
         }
 
@@ -359,4 +396,10 @@
             query += Form("fRunStart between '%s' and '%s' ",
                           fRequestFrom.Data(), fRequestTo.Data());
+        }
+
+        if (fGroupBy!=kNone)
+        {
+            query += Form(" GROUP BY %s ", valued.Data());
+            //query += Form(" HAVING COUNT(%s)=(COUNT(*)+1)/2 ", valuev.Data());
         }
 
@@ -383,4 +426,6 @@
 void plotall(MPlot &plot)
 {
+    //plot.SetGroupBy(MPlot::kGroupByWeek);
+
     //inner camera
     //from calib*.root
