Ignore:
Timestamp:
07/05/08 20:01:55 (16 years ago)
Author:
tbretz
Message:
*** empty log message ***
File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/MagicSoft/Mars/datacenter/macros/plotoptical.C

    r8752 r8996  
    11/* ======================================================================== *\
    2 ! $Name: not supported by cvs2svn $:$Id: plotoptical.C,v 1.8 2007-10-07 21:11:01 tbretz Exp $
     2! $Name: not supported by cvs2svn $:$Id: plotoptical.C,v 1.9 2008-07-05 19:01:42 tbretz Exp $
    33! --------------------------------------------------------------------------
    44!
     
    7878//
    7979/////////////////////////////////////////////////////////////////////////////
    80 #include <iostream>
    81 #include <iomanip>
    82 
    83 #include <TH1.h>
    84 #include <TEnv.h>
    85 #include <TPad.h>
    86 #include <TLine.h>
    87 #include <TText.h>
    88 #include <TFrame.h>
    89 #include <TStyle.h>
    90 #include <TCanvas.h>
    91 #include <TPRegexp.h>
    92 #include <TSQLRow.h>
    93 #include <TSQLResult.h>
    94 #include <TGraphErrors.h>
    95 
    96 #include "MTime.h"
    97 #include "MAstro.h"
    98 #include "MDataSet.h"
    99 #include "MSQLMagic.h"
    100 #include "MStatusDisplay.h"
    101 
    102 class MPlot : public MParContainer
    103 {
    104 public:
    105     // Possible constants to group-by (average) over a certain period
    106     enum GroupBy_t
    107     {
    108         kNone,
    109         kGroupByPrimary,
    110         kGroupByHour,
    111         kGroupByNight,
    112         kGroupByWeek,
    113         kGroupByMonth,
    114         kGroupBySeason,
    115         kGroupByYear
    116     };
    117 
    118 private:
    119     MSQLMagic &fServer;         // Reference to the sql-server class
    120 
    121     MDataSet *fDataSet;         // A possible dtaset to highlite single points
    122 
    123     TString   fPrimaryDate;     // The name of the data we plot
    124     TString   fPrimaryNumber;   // The corresponding name for the key number
    125     TString   fSecondary;       // The value versus which the second plot is made
    126 
    127     TString   fRequestFrom;     // Start of a requested date range
    128     TString   fRequestTo;       // End of a requested date range
    129     Int_t     fRequestPeriod;   // A possible requested period
    130 
    131     Float_t   fPlotMin;
    132     Float_t   fPlotMax;
    133 
    134     Float_t   fHistMin;
    135     Float_t   fHistMax;
    136 
    137     TString   fDescription;     // The description (title) of the plot
    138     TString   fNameTab;         // The name of the tab in the display
    139 
    140     TString   fCondition;       // An additional condition added to the query
    141     GroupBy_t fGroupBy;         // A possible Group-By flag
    142 
    143     // --------------------------------------------------------------------------
    144     //
    145     // Function to plot the result of the query
    146     //
    147     void PlotTable(TSQLResult &res, TString name, Float_t fmin, Float_t fmax, Float_t resolution)
    148     {
    149         // Enable all otions in the statistics box
    150         gStyle->SetOptStat(111111);
    151 
    152         // Create TGraph objects
    153         TGraph &gt = res.GetFieldCount()>4 ? *new TGraphErrors : *new TGraph;
    154         gt.SetNameTitle(name, Form("%s vs Time", name.Data()));
    155         gt.SetMarkerStyle(kFullDotMedium);
    156 
    157         TGraph gz;
    158         gz.SetNameTitle(name, Form("%s vs <Zd>", name.Data()));
    159         gz.SetMarkerStyle(kFullDotMedium);
    160 
    161         TGraph gt0, gt1;
    162         gt0.SetMarkerColor(kRed);
    163         gt1.SetMarkerColor(kBlue);
    164         gt0.SetMarkerStyle(kFullDotLarge);
    165         gt1.SetMarkerStyle(kFullDotLarge);
    166 
    167         TGraph gz0, gz1;
    168         gz0.SetMarkerColor(kRed);
    169         gz1.SetMarkerColor(kBlue);
    170         gz0.SetMarkerStyle(kFullDotLarge);
    171         gz1.SetMarkerStyle(kFullDotLarge);
    172 
    173         Int_t first = -1;
    174         Int_t last  = -1;
    175 
    176         // Loop over the data
    177         TSQLRow *row=0;
    178         while ((row=res.Next()))
    179         {
    180             // Get all fields of this row
    181             const char *date = (*row)[0];
    182             const char *zd   = (*row)[1];
    183             const char *val  = (*row)[2];
    184             const char *snum = res.GetFieldCount()>3 ? (*row)[3] : 0;
    185             const char *verr = res.GetFieldCount()>4 ? (*row)[5] : 0;
    186 
    187             delete row;
    188 
    189             if (!date || !val || !zd)
    190                 continue;
    191 
    192             // check if date is valid
    193             MTime t(date);
    194             if (!t.SetSqlDateTime(date))
    195                 continue;
    196 
    197             // check if it belongs to the requested MAGIC period
    198             if (fRequestPeriod>0 && MAstro::GetMagicPeriod(t.GetMjd())!=fRequestPeriod)
    199                 continue;
    200 
    201             // Get axis range
    202             if (first<0)
    203                 first = TMath::Nint(TMath::Floor(t.GetMjd()));
    204             last = TMath::Nint(TMath::Ceil(t.GetMjd()));
    205 
    206             // Convert a possible key number into a integer
    207             UInt_t seq = snum ? atoi(snum) : 0;
    208 
    209             // convert primary and secondary value into floats
    210             Float_t value = atof(val);
    211             Float_t zenith = atof(zd);
    212 
    213             // If a datset is given add the point to the special TGraphs
    214             // used for highliting these dates
    215             if (fDataSet)
    216             {
    217                 if (fDataSet->HasOnSequence(seq))
    218                 {
    219                     gt1.SetPoint(gt1.GetN(), t.GetAxisTime(), value);
    220                     gz1.SetPoint(gz1.GetN(), zenith, value);
    221                 }
    222 
    223                 if (fDataSet->HasOffSequence(seq))
    224                 {
    225                     gt0.SetPoint(gt0.GetN(), t.GetAxisTime(), value);
    226                     gz0.SetPoint(gz0.GetN(), zenith, value);
    227                 }
    228             }
    229 
    230             // Add Data to TGraph
    231             gt.SetPoint(gt.GetN(), t.GetAxisTime(), value);
    232             gz.SetPoint(gz.GetN(), zenith, value);
    233 
    234             // Set error-bar, if one
    235             if (verr)
    236                 static_cast<TGraphErrors&>(gt).SetPointError(gt.GetN()-1, 0, atof(verr));
    237         }
    238 
    239         // If this is done earlier the plots remain empty since root 5.12/00
    240         if (fmax>fmin)
    241         {
    242             gt.SetMinimum(fmin);
    243             gt.SetMaximum(fmax);
    244             gz.SetMinimum(fmin);
    245             gz.SetMaximum(fmax);
    246         }
    247 
    248         gROOT->SetSelectedPad(0);
    249 
    250         // Create a TCanvas or open a new tab
    251         TString title = fNameTab.IsNull() ? name(name.First('.')+2, name.Length()) : fNameTab;
    252         TCanvas &c = fDisplay ? fDisplay->AddTab(title) : *new TCanvas;
    253         // Set fillcolor, remove border and divide pad
    254         c.SetFillColor(kWhite);
    255         c.SetBorderMode(0);
    256         c.Divide(1,2);
    257 
    258         // Output mean and rms to console
    259         cerr << setprecision(4) << setw(10) << title << ":   ";
    260         if (gt.GetN()==0)
    261         {
    262             cerr << "     <empty>" << endl;
    263             return;
    264         }
    265         cerr << setw(8) << gt.GetMean(2) << "+-" << setw(8) << gt.GetRMS(2) << "   ";
    266         if (gt0.GetN()>0 || gt1.GetN()>0)
    267         {
    268             cerr << setw(8) << gt1.GetMean(2) << "+-" << setw(8) << gt1.GetRMS(2) << "   ";
    269             cerr << setw(8) << gt0.GetMean(2) << "+-" << setw(8) << gt0.GetRMS(2);
    270         }
    271         cerr << endl;
    272 
    273         TVirtualPad *pad = gPad;
    274 
    275         // draw contants of pad 2 (counting starts at 0)
    276         pad->cd(2);
    277         gPad->SetBorderMode(0);
    278         gPad->SetFrameBorderMode(0);
    279         gPad->SetGridy();
    280 
    281         gPad->SetLeftMargin(0.06);
    282         gPad->SetRightMargin(0.06);
    283         gPad->SetBottomMargin(0.08);
    284 
    285         // format axis
    286         TH1 *h = gt.GetHistogram();
    287 
    288         h->SetXTitle("Time");
    289         h->SetYTitle(name);
    290         h->GetXaxis()->SetTimeDisplay(1);
    291         h->GetYaxis()->SetTitleOffset(0.8);
    292         h->GetXaxis()->SetTitleOffset(1.0);
    293         h->GetXaxis()->SetLabelOffset(0.01);
    294 
    295         // draw TGraph
    296         gt.DrawClone("AP");
    297         if (gt0.GetN()>0)
    298             gt0.DrawClone("P");
    299         if (gt1.GetN()>0)
    300             gt1.DrawClone("P");
    301 
    302         // Add lines and text showing the MAGIC periods
    303         TLine l;
    304         TText t;
    305         Int_t num=0;
    306         l.SetLineStyle(kDotted);
    307         l.SetLineColor(kBlue);
    308         t.SetTextColor(kBlue);
    309         l.SetLineWidth(1);
    310         t.SetTextSize(h->GetXaxis()->GetLabelSize());
    311         t.SetTextAlign(21);
    312         Int_t p0 = MAstro::GetMagicPeriod(first);
    313         for (Int_t p = first; p<last; p++)
    314         {
    315             Int_t p1 = MAstro::GetMagicPeriod(p);
    316             if (p1!=p0)
    317             {
    318                 l.DrawLine(MTime(p).GetAxisTime(), h->GetMinimum(), MTime(p).GetAxisTime(), h->GetMaximum());
    319                 t.DrawText(MTime(p+15).GetAxisTime(), h->GetMaximum(), Form("%d", p1));
    320                 num++;
    321             }
    322             p0 = p1;
    323         }
    324         if (num<4)
    325             gPad->SetGridx();
    326 
    327         const Double_t min = fHistMin>fHistMax ? h->GetMinimum()-resolution/2 : fHistMin;
    328         const Double_t max = fHistMin>fHistMax ? h->GetMaximum()+resolution/2 : fHistMax;
    329 
    330         // Use this to save the pad with the time development to a file
    331         //gPad->SaveAs(Form("plotdb-%s.eps", title.Data()));
    332 
    333         // Go back to first (upper) pad, format it and divide it again
    334         pad->cd(1);
    335         gPad->SetBorderMode(0);
    336         gPad->SetFrameBorderMode(0);
    337         gPad->Divide(2,1);
    338 
    339         TVirtualPad *pad2 = gPad;
    340 
    341         // format left pad
    342         pad2->cd(1);
    343         gPad->SetBorderMode(0);
    344         gPad->SetFrameBorderMode(0);
    345         gPad->SetGridx();
    346         gPad->SetGridy();
    347 
    348         // Create histogram
    349         const Int_t n = resolution>0 ? TMath::Nint((max-min)/resolution) : 50;
    350 
    351         TH1F hist("Hist", Form("Distribution of %s", fDescription.IsNull() ? name.Data() : fDescription.Data()), n, min, max);
    352         hist.SetDirectory(0);
    353 
    354         // Fill data into histogra,
    355         for (int i=0; i<gt.GetN(); i++)
    356             hist.Fill(gt.GetY()[i]);
    357 
    358         // Format histogram
    359         if (fDescription.IsNull())
    360             hist.SetXTitle(name);
    361         hist.SetYTitle("Counts");
    362 
    363         // plot histogram
    364         hist.DrawCopy("");
    365 
    366         // format right pad
    367         pad2->cd(2);
    368         gPad->SetBorderMode(0);
    369         gPad->SetFrameBorderMode(0);
    370         gPad->SetGridy();
    371 
    372         // format graph
    373         TH1 *h2 = gz.GetHistogram();
    374 
    375         h2->SetXTitle("Zd");
    376         h2->SetYTitle(name);
    377 
    378         // draw graph
    379         gz.DrawClone("AP");
    380 
    381         if (gz0.GetN()>0)
    382             gz0.DrawClone("P");
    383         if (gz1.GetN()>0)
    384             gz1.DrawClone("P");
    385     }
    386 
    387 public:
    388     MPlot(MSQLMagic &server) : fServer(server), fDataSet(NULL),
    389         fRequestPeriod(-1), fPlotMin(0), fPlotMax(-1), fHistMin(0), fHistMax(-1), fGroupBy(kNone)
    390     {
    391     }
    392     ~MPlot()
    393     {
    394         if (fDataSet)
    395             delete fDataSet;
    396     }
    397     void SetDataSet(const TString filename)
    398     {
    399         if (fDataSet)
    400         {
    401             delete fDataSet;
    402             fDataSet = NULL;
    403         }
    404         if (!filename.IsNull())
    405             fDataSet = new MDataSet(filename);
    406     }
    407     void SetPlotRange(Float_t min, Float_t max, Int_t n=5) { fPlotMin = min; fPlotMax = max; }
    408     void SetHistRange(Float_t min, Float_t max) { fHistMin = min; fHistMax = max; }
    409     void SetRequestRange(const char *from="", const char *to="") { fRequestFrom = from; fRequestTo = to; }
    410     void SetRequestPeriod(Int_t n=-1) { fRequestPeriod = n; }
    411     void SetCondition(const char *cond="") { fCondition = cond; }
    412     void SetDescription(const char *d, const char *t=0) { fDescription = d; fNameTab = t; }
    413     void SetGroupBy(GroupBy_t b=kGroupByWeek) { fGroupBy=b; }
    414     void SetPrimaryDate(const char *ts) { fPrimaryDate=ts; }
    415     void SetPrimaryNumber(const char *ts) { fPrimaryNumber=ts; }
    416     void SetSecondary(const char *ts) { fSecondary=ts; }
    417 
    418     Int_t QueryKeyOfSource(TString src)
    419     {
    420         return fServer.QueryKeyOfName("Object", src, kFALSE);
    421     }
    422 
    423     Bool_t Plot(const char *value, Float_t min=0, Float_t max=-1, Float_t resolution=0)
    424     {
    425         TString named  = fPrimaryDate;
    426         TString named2 = fSecondary;
    427         TString namev  = value;
    428 
    429         TString tablev = namev(0, namev.First('.'));
    430         TString valuev = namev(namev.First('.')+1, namev.Length());
    431 
    432         TString tabled = named(0, named.First('.'));
    433         TString valued = named(named.First('.')+1, named.Length());
    434 
    435         TString query="SELECT ";
    436         switch (fGroupBy)
    437         {
    438         case kNone:
    439         case kGroupByPrimary:
    440             query += Form("%s AS %s", valued.Data(), valued.Data()+1);
    441             break;
    442         case kGroupByHour:
    443             query += Form("DATE_FORMAT(%s, '%%Y-%%m-%%d %%H:30:00') AS %s ", fPrimaryDate.Data(), valued.Data()+1);
    444             break;
    445         case kGroupByNight:
    446             query += Form("DATE_FORMAT(ADDDATE(%s,Interval 12 hour), '%%Y-%%m-%%d 00:00:00') AS %s ", fPrimaryDate.Data(), valued.Data()+1);
    447             break;
    448         case kGroupByWeek:
    449             query += Form("DATE_FORMAT(ADDDATE(%s,Interval 12 hour), '%%x%%v') AS %s ", fPrimaryDate.Data(), valued.Data()+1);
    450             break;
    451         case kGroupByMonth:
    452             query += Form("DATE_FORMAT(ADDDATE(%s,Interval 12 hour), '%%Y-%%m-15 00:00:00') AS %s ", fPrimaryDate.Data(), valued.Data()+1);
    453             break;
    454         case kGroupBySeason:
    455             //query += Form("DATE_FORMAT(ADDDATE(%s,Interval 12 hour), '%%Y-%%m-15 00:00:00') AS %s ", fPrimaryDate.Data(), valued.Data()+1);
    456             break;
    457         case kGroupByYear:
    458             query += Form("DATE_FORMAT(ADDDATE(%s,Interval 12 hour), '%%Y-08-15 00:00:00') AS %s ", fPrimaryDate.Data(), valued.Data()+1);
    459             break;
    460         }
    461 
    462         if (fGroupBy==kNone)
    463         {
    464             query += ", ";
    465             query += fSecondary;
    466             query += ", ";
    467             query += value;
    468             query += ", ";
    469             query += fPrimaryNumber;
    470             query += " ";
    471         }
    472         else
    473         {
    474             query += ", AVG(";
    475             query += fSecondary;
    476             query += "), AVG(";
    477             query += value;
    478             query += "), ";
    479             query += fPrimaryNumber;
    480             query += ", STD(";
    481             query += fSecondary;
    482             query += "), STD(";
    483             query += value;
    484             query += ") ";
    485         }
    486 
    487         query += Form("FROM %s ", tabled.Data());
    488 
    489         TString where(fCondition);
    490 
    491         const Bool_t interval = !fRequestFrom.IsNull() && !fRequestTo.IsNull();
    492         if (interval)
    493         {
    494             if (!where.IsNull())
    495                 where += " AND ";
    496             where += Form("%s BETWEEN '%s' AND '%s' ",
    497                           fPrimaryDate.Data(), fRequestFrom.Data(), fRequestTo.Data());
    498         }
    499 
    500         if (!where.IsNull())
    501             where += " AND ";
    502         where += fCondition;
    503         where += " ";
    504 
    505         // ------------------------------
    506 
    507         query += fServer.GetJoins(tabled, query+" "+where);
    508 
    509         if (!where.IsNull())
    510         {
    511             query += "WHERE ";
    512             query += where;
    513         }
    514 
    515         if (fGroupBy!=kNone)
    516         {
    517             query += Form("GROUP BY %s ", valued.Data()+1);
    518             //query += Form(" HAVING COUNT(%s)=(COUNT(*)+1)/2 ", valuev.Data());
    519         }
    520         query += Form("ORDER BY %s ", valued.Data()+1);
    521 
    522         // ------------------------------
    523 
    524         TSQLResult *res = fServer.Query(query);
    525         if (!res)
    526             return kFALSE;
    527 
    528         if (max>min)
    529             PlotTable(*res, namev, min, max, resolution);
    530         else
    531             PlotTable(*res, namev, fPlotMin, fPlotMax, resolution);
    532 
    533         delete res;
    534         return kTRUE;
    535     }
    536 };
    537 
    538 void plotall(MPlot &plot, TString source)
     80#include "plotdb.C"
     81
     82void plotalloptical(MPlot &plot, TString source)
    53983{
    54084    // Setup here the values for timestamp and secondary (upper/right) plot
     
    54892    if (!source.IsNull())
    54993    {
    550         const Int_t key = plot.QueryKeyOfSource(source);
     94        const Int_t key = plot.GetServer().QueryKeyOfName("Object", source, kFALSE);
    55195        if (key<0)
    55296            return;
    55397        cond += Form(" AND Object.fObjectKEY=%d", key);
    554 
    55598    }
    55699
     
    625168int plotoptical(TString from, TString to, const char *source=0)
    626169{
    627     TEnv env("sql.rc");
    628 
    629     MSQLMagic serv(env);
     170    MSQLMagic serv("sql.rc");
    630171    if (!serv.IsConnected())
    631172    {
     
    648189    plot.SetDisplay(d);
    649190    plot.SetRequestRange(from, to);
    650     plotall(plot, source);
     191    plotalloptical(plot, source);
    651192    // Use this to create output plots automatically
    652193    //  d->SaveAsRoot("plotoptical.root");
     
    658199int plotoptical(const char *source)
    659200{
    660     TEnv env("sql.rc");
    661 
    662     MSQLMagic serv(env);
     201    MSQLMagic serv("sql.rc");
    663202    if (!serv.IsConnected())
    664203    {
     
    681220    plot.SetDisplay(d);
    682221    plot.SetRequestRange("", "");
    683     plotall(plot, source);
     222    plotalloptical(plot, source);
    684223    // Use this to create output plots automatically
    685224    //  d->SaveAsRoot("plotoptical.root");
     
    691230int plotoptical(Int_t period, const char *source="")
    692231{
    693     TEnv env("sql.rc");
    694 
    695     MSQLMagic serv(env);
     232    MSQLMagic serv("sql.rc");
    696233    if (!serv.IsConnected())
    697234    {
     
    714251    plot.SetDisplay(d);
    715252    plot.SetRequestPeriod(period);
    716     plotall(plot, source);
     253    plotalloptical(plot, source);
    717254
    718255    // Use this to create output plots automatically
Note: See TracChangeset for help on using the changeset viewer.