Changeset 8212 for trunk/MagicSoft/Mars


Ignore:
Timestamp:
11/03/06 10:53:45 (18 years ago)
Author:
tbretz
Message:
*** empty log message ***
Location:
trunk/MagicSoft/Mars
Files:
4 edited

Legend:

Unmodified
Added
Removed
  • trunk/MagicSoft/Mars/Changelog

    r8210 r8212  
    1818
    1919                                                 -*-*- END OF LINE -*-*-
     20
     21 2006/11/03 Thomas Bretz
     22
     23   * datacenter/macros/plotoptical.C, datacenter/macros/plotdb.C,
     24     datacenter/macros/plotrundb.C:
     25     - improved grouping
     26     - added comments
     27
     28
    2029
    2130 2006/11/02 Daniela Dorner
  • trunk/MagicSoft/Mars/datacenter/macros/plotdb.C

    r8186 r8212  
    11/* ======================================================================== *\
    2 ! $Name: not supported by cvs2svn $:$Id: plotdb.C,v 1.31 2006-11-01 08:53:26 tbretz Exp $
     2! $Name: not supported by cvs2svn $:$Id: plotdb.C,v 1.32 2006-11-03 10:52:36 tbretz Exp $
    33! --------------------------------------------------------------------------
    44!
     
    8484{
    8585public:
     86    // Possible constants to group-by (average) over a certain period
    8687    enum GroupBy_t
    8788    {
     
    9293        kGroupByWeek,
    9394        kGroupByMonth,
     95        kGroupBySeason,
    9496        kGroupByYear
    9597    };
     98
    9699private:
    97     MSQLMagic &fServer;
    98 
    99     MDataSet *fDataSet;
    100 
    101     TString   fPrimaryDate;
    102     TString   fPrimaryNumber;
    103     TString   fSecondary;
    104 
    105     TString   fRequestFrom;
    106     TString   fRequestTo;
    107     Int_t     fRequestPeriod;
     100    MSQLMagic &fServer;         // Reference to the sql-server class
     101
     102    MDataSet *fDataSet;         // A possible dtaset to highlite single points
     103
     104    TString   fPrimaryDate;     // The name of the data we plot
     105    TString   fPrimaryNumber;   // The corresponding name for the key number
     106    TString   fSecondary;       // The value versus which the second plot is made
     107
     108    TString   fRequestFrom;     // Start of a requested date range
     109    TString   fRequestTo;       // End of a requested date range
     110    Int_t     fRequestPeriod;   // A possible requested period
    108111
    109112    Float_t   fPlotMin;
     
    113116    Float_t   fHistMax;
    114117
    115     TString   fDescription;
    116     TString   fNameTab;
    117 
    118     TString   fCondition;
    119     GroupBy_t fGroupBy;
    120 
     118    TString   fDescription;     // The description (title) of the plot
     119    TString   fNameTab;         // The name of the tab in the display
     120
     121    TString   fCondition;       // An additional condition added to the query
     122    GroupBy_t fGroupBy;         // A possible Group-By flag
     123
     124    // --------------------------------------------------------------------------
     125    //
     126    // Function to plot the result of the query
     127    //
    121128    void PlotTable(TSQLResult &res, TString name, Float_t fmin, Float_t fmax, Float_t resolution)
    122129    {
     130        // Enable all otions in the statistics box
    123131        gStyle->SetOptStat(111111);
    124132
    125133        TSQLRow *row;
    126134
     135        // Create TGraph objects
    127136        TGraph &gt = res.GetFieldCount()>4 ? *new TGraphErrors : *new TGraph;
    128137        gt.SetNameTitle(name, Form("%s vs Time", name.Data()));
     
    148157        Int_t last  = -1;
    149158
     159        // Loop over the data
    150160        while ((row=res.Next()))
    151161        {
     162            // Get all fields of this row
    152163            const char *date = (*row)[0];
    153164            const char *zd   = (*row)[1];
     
    158169                continue;
    159170
     171            // check if date is valid
    160172            MTime t(date);
    161173            if (!t.SetSqlDateTime(date))
    162174                continue;
    163175
     176            // check if it belongs to the requested MAGIC period
    164177            if (fRequestPeriod>0 && MAstro::GetMagicPeriod(t.GetMjd())!=fRequestPeriod)
    165178                continue;
    166179
     180            // Get axis range
    167181            if (first<0)
    168182                first = TMath::Nint(TMath::Floor(t.GetMjd()));
    169183            last = TMath::Nint(TMath::Ceil(t.GetMjd()));
    170184
     185            // Convert a possible key number into a integer
    171186            UInt_t seq = snum ? atoi(snum) : 0;
    172    
     187
     188            // convert primary and secondary value into floats
    173189            Float_t value = atof(val);
    174190            Float_t zenith = atof(zd);
    175191
     192            // If a datset is given add the point to the special TGraphs
     193            // used for highliting these dates
    176194            if (fDataSet)
    177195            {
     
    189207            }
    190208
     209            // Add Data to TGraph
    191210            gt.SetPoint(gt.GetN(), t.GetAxisTime(), value);
    192211            gz.SetPoint(gz.GetN(), zenith, value);
    193212
     213            // Set error-bar, if one
    194214            if (verr)
    195215                static_cast<TGraphErrors&>(gt).SetPointError(gt.GetN()-1, 0, atof(verr));
     
    207227        gROOT->SetSelectedPad(0);
    208228
     229        // Create a TCanvas or open a new tab
    209230        TString title = fNameTab.IsNull() ? name(name.First('.')+2, name.Length()) : fNameTab;
    210231        TCanvas &c = fDisplay ? fDisplay->AddTab(title) : *new TCanvas;
     232        // Set fillcolor, remove border and divide pad
    211233        c.SetFillColor(kWhite);
    212234        c.SetBorderMode(0);
    213235        c.Divide(1,2);
    214236
     237        // Output mean and rms to console
    215238        cerr << setprecision(4) << setw(10) << title << ":   ";
    216239        if (gt.GetN()==0)
     
    228251
    229252        TVirtualPad *pad = gPad;
     253
     254        // draw contants of pad 2 (counting starts at 0)
    230255        pad->cd(2);
    231256        gPad->SetBorderMode(0);
     
    237262        gPad->SetBottomMargin(0.08);
    238263
     264        // format axis
    239265        TH1 *h = gt.GetHistogram();
    240266
     
    246272        h->GetXaxis()->SetLabelOffset(0.01);
    247273
     274        // draw TGraph
    248275        gt.DrawClone("AP");
    249276        if (gt0.GetN()>0)
     
    252279            gt1.DrawClone("P");
    253280
     281        // Add lines and text showing the MAGIC periods
    254282        TLine l;
    255283        TText t;
     
    282310        //gPad->SaveAs(Form("plotdb-%s.eps", title.Data()));
    283311
     312        // Go back to first (upper) pad, format it and divide it again
    284313        pad->cd(1);
    285314        gPad->SetBorderMode(0);
     
    288317
    289318        TVirtualPad *pad2 = gPad;
     319
     320        // format left pad
    290321        pad2->cd(1);
    291322        gPad->SetBorderMode(0);
     
    294325        gPad->SetGridy();
    295326
     327        // Create histogram
    296328        const Int_t n = resolution>0 ? TMath::Nint((max-min)/resolution) : 50;
    297329
     
    299331        hist.SetDirectory(0);
    300332
     333        // Fill data into histogra,
    301334        for (int i=0; i<gt.GetN(); i++)
    302335            hist.Fill(gt.GetY()[i]);
    303336
     337        // Format histogram
    304338        if (fDescription.IsNull())
    305339            hist.SetXTitle(name);
    306340        hist.SetYTitle("Counts");
    307341
     342        // plot histogram
    308343        hist.DrawCopy("");
    309344
     345        // format right pad
    310346        pad2->cd(2);
    311347        gPad->SetBorderMode(0);
     
    313349        gPad->SetGridy();
    314350
     351        // format graph
    315352        TH1 *h2 = gz.GetHistogram();
    316353
     
    318355        h2->SetYTitle(name);
    319356
     357        // draw graph
    320358        gz.DrawClone("AP");
     359
    321360        if (gz0.GetN()>0)
    322361            gz0.DrawClone("P");
     
    369408
    370409        TString query="SELECT ";
    371         query += valued;
     410        switch (fGroupBy)
     411        {
     412        case kNone:
     413        case kGroupByPrimary:
     414            query += valued;
     415            break;
     416        case kGroupByHour:
     417            query += Form("DATE_FORMAT(%s, '%%Y-%%m-%%d %%H:30:00') AS %s ", fPrimaryDate.Data(), valued.Data());
     418            break;
     419        case kGroupByNight:
     420            query += Form("DATE_FORMAT(ADDDATE(%s,Interval 12 hour), '%%Y-%%m-%%d 00:00:00') AS %s ", fPrimaryDate.Data(), valued.Data());
     421            break;
     422        case kGroupByWeek:
     423            query += Form("DATE_FORMAT(ADDDATE(%s,Interval 12 hour), '%%x%%v') AS %s ", fPrimaryDate.Data(), valued.Data());
     424            break;
     425        case kGroupByMonth:
     426            query += Form("DATE_FORMAT(ADDDATE(%s,Interval 12 hour), '%%Y-%%m-15 00:00:00') AS %s ", fPrimaryDate.Data(), valued.Data());
     427            break;
     428        case kGroupBySeason:
     429            //query += Form("DATE_FORMAT(ADDDATE(%s,Interval 12 hour), '%%Y-%%m-15 00:00:00') AS %s ", fPrimaryDate.Data(), valued.Data());
     430            break;
     431        case kGroupByYear:
     432            query += Form("DATE_FORMAT(ADDDATE(%s,Interval 12 hour), '%%Y-08-15 00:00:00') AS %s ", fPrimaryDate.Data(), valued.Data());
     433            break;
     434        }
     435
    372436        if (fGroupBy==kNone)
    373437        {
     
    395459        }
    396460
    397         switch (fGroupBy)
    398         {
    399         case kNone:
    400         case kGroupByPrimary:
    401             break;
    402         case kGroupByHour:
    403             query += Form(", DATE_FORMAT(%s, '%%Y-%%m-%%d %%H') AS %s ", named.Data(), valued.Data());
    404             break;
    405         case kGroupByNight:
    406             query += Form(", DATE_FORMAT(ADDDATE(%s,Interval 12 hour), '%%Y-%%m-%%d') AS %s ", named.Data(), valued.Data());
    407             break;
    408         case kGroupByWeek:
    409             query += Form(", DATE_FORMAT(ADDDATE(%s,Interval 12 hour), '%%x%%v') AS %s ", named.Data(), valued.Data());
    410             break;
    411         case kGroupByMonth:
    412             query += Form(", DATE_FORMAT(ADDDATE(%s,Interval 12 hour), '%%Y-%%m') AS %s ", named.Data(), valued.Data());
    413             break;
    414         case kGroupByYear:
    415             query += Form(", DATE_FORMAT(ADDDATE(%s,Interval 12 hour), '%%Y') AS %s ", named.Data(), valued.Data());
    416             break;
    417         }
    418 
    419461        query += Form("FROM %s ", tabled.Data());
    420462
     
    452494            //query += Form(" HAVING COUNT(%s)=(COUNT(*)+1)/2 ", valuev.Data());
    453495        }
    454         query += Form("ORDER BY %s ", fPrimaryDate.Data());
     496        query += Form("ORDER BY %s ", valued.Data());
    455497
    456498
  • trunk/MagicSoft/Mars/datacenter/macros/plotoptical.C

    r8186 r8212  
    11/* ======================================================================== *\
    2 ! $Name: not supported by cvs2svn $:$Id: plotoptical.C,v 1.4 2006-11-01 08:53:26 tbretz Exp $
     2! $Name: not supported by cvs2svn $:$Id: plotoptical.C,v 1.5 2006-11-03 10:52:36 tbretz Exp $
    33! --------------------------------------------------------------------------
    44!
     
    2828/////////////////////////////////////////////////////////////////////////////
    2929//
    30 // plotdb.C
    31 // ========
    32 //
    33 // This macro is used to read quality parameters from the DB and plot them.
    34 //
    35 // The parameters are from the following files:
    36 // calib*.root:mean conversion factor, mean arrival time, rms arrival time
    37 // (each parameter for inner and outer camera)
    38 // signal*.root: mean pedestal rms (for inner and outer camera)
    39 // star*.root: PSF, # of Muons, Effective OnTime, Muon rate,
    40 // Ratio MC/Data(MuonSize) and mean number of islands
     30// plotoptical.C
     31// =============
     32//
     33// This macro is used to read optical data from the DB and plot them.
    4134//
    4235// In the DB these values are stored in the tables Calibration and Star.
    43 //
    44 // Usage:
    45 //   .x plotdb.C   --> all values in the DB are plotted
    46 // You can chose are certain period:
    47 //   .x plotdb.C(25)   --> all values from period 25 are plotted
     36//
     37//
     38// To plot the whole database simple use:
     39//   .x plotdb.C+
     40//
     41// Plot only data for one source. For the available sources see the database
     42//   .x plotdb.C+("source")
     43//
     44// You can chose are certain MAGIC-period:
     45//   .x plotdb.C+(25)   --> all values from period 25 are plotted
     46//
     47// MAGIC periods correspond to one moon-phase and are defined as
     48// moon period-284. For details see MAstro::MoonPeriod and
     49// MAstro::MagicPeriod.
     50//
    4851// or a time period from a certain date to a certain date
    49 //   .x plotdb.C("2004-11-14 00:00:00", "2005-02-28 00:00:00")
     52//   .x plotdb.C+("2004-11-14 00:00:00", "2005-02-28 00:00:00")
    5053//  --> all values from 14.11.2004 0h to 28.2.2005 0h are plotted
    51 // or all data, but with dataset data highlighted
    52 //   .x plotdb.C("dataset.txt")
    53 //  --> the sequences defined in dataset.txt are highlighted (blue:on, red:off)
    54 //  --> You can also add a dataset-name as last argument to one of the
    55 //      calls above
     54//   .x plotdb.C+("2004-11-14 00:00:00", "2005-02-28 00:00:00", "source")
     55//  --> all values from 14.11.2004 0h to 28.2.2005 0h of "source" are plotted
     56//
     57//
     58// For details of the plots produced see the function plotall() below.
     59//
     60//
     61// The plot title and axis-titles are created by:
     62//    plot.SetDescription("Title;x", "TabName");
     63//
     64// Drawing the plot is initiated by
     65//    plot.Plot("OpticalData.fExposure", min, max, width);
     66//
     67// While OpticalData.fExposure can be any kind of variable to plot.
     68// min and max are the minimum and maximum of the histogram which is
     69// filled and width is the bin-width of this histogram.
     70//
     71// To group data (average) of a certain period use:
     72//    plot.GroupBy(MPlot::kGroupByNight);
     73// before initiating the plot.
     74//
    5675//
    5776// Make sure, that database and password are corretly set in a resource
     
    84103{
    85104public:
     105    // Possible constants to group-by (average) over a certain period
    86106    enum GroupBy_t
    87107    {
     
    92112        kGroupByWeek,
    93113        kGroupByMonth,
     114        kGroupBySeason,
    94115        kGroupByYear
    95116    };
     117
    96118private:
    97     MSQLMagic &fServer;
    98 
    99     MDataSet *fDataSet;
    100 
    101     TString   fPrimaryDate;
    102     TString   fPrimaryNumber;
    103     TString   fSecondary;
    104 
    105     TString   fRequestFrom;
    106     TString   fRequestTo;
    107     Int_t     fRequestPeriod;
     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
    108130
    109131    Float_t   fPlotMin;
     
    113135    Float_t   fHistMax;
    114136
    115     TString   fDescription;
    116     TString   fNameTab;
    117 
    118     TString   fCondition;
    119     GroupBy_t fGroupBy;
    120 
     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    //
    121147    void PlotTable(TSQLResult &res, TString name, Float_t fmin, Float_t fmax, Float_t resolution)
    122148    {
     149        // Enable all otions in the statistics box
    123150        gStyle->SetOptStat(111111);
    124151
    125152        TSQLRow *row;
    126153
     154        // Create TGraph objects
    127155        TGraph &gt = res.GetFieldCount()>4 ? *new TGraphErrors : *new TGraph;
    128156        gt.SetNameTitle(name, Form("%s vs Time", name.Data()));
     
    148176        Int_t last  = -1;
    149177
     178        // Loop over the data
    150179        while ((row=res.Next()))
    151180        {
     181            // Get all fields of this row
    152182            const char *date = (*row)[0];
    153183            const char *zd   = (*row)[1];
     
    158188                continue;
    159189
     190            // check if date is valid
    160191            MTime t(date);
    161192            if (!t.SetSqlDateTime(date))
    162193                continue;
    163194
     195            // check if it belongs to the requested MAGIC period
    164196            if (fRequestPeriod>0 && MAstro::GetMagicPeriod(t.GetMjd())!=fRequestPeriod)
    165197                continue;
    166198
     199            // Get axis range
    167200            if (first<0)
    168201                first = TMath::Nint(TMath::Floor(t.GetMjd()));
    169202            last = TMath::Nint(TMath::Ceil(t.GetMjd()));
    170203
     204            // Convert a possible key number into a integer
    171205            UInt_t seq = snum ? atoi(snum) : 0;
    172    
     206
     207            // convert primary and secondary value into floats
    173208            Float_t value = atof(val);
    174209            Float_t zenith = atof(zd);
    175210
     211            // If a datset is given add the point to the special TGraphs
     212            // used for highliting these dates
    176213            if (fDataSet)
    177214            {
     
    189226            }
    190227
     228            // Add Data to TGraph
    191229            gt.SetPoint(gt.GetN(), t.GetAxisTime(), value);
    192230            gz.SetPoint(gz.GetN(), zenith, value);
    193231
     232            // Set error-bar, if one
    194233            if (verr)
    195234                static_cast<TGraphErrors&>(gt).SetPointError(gt.GetN()-1, 0, atof(verr));
     
    207246        gROOT->SetSelectedPad(0);
    208247
     248        // Create a TCanvas or open a new tab
    209249        TString title = fNameTab.IsNull() ? name(name.First('.')+2, name.Length()) : fNameTab;
    210250        TCanvas &c = fDisplay ? fDisplay->AddTab(title) : *new TCanvas;
     251        // Set fillcolor, remove border and divide pad
    211252        c.SetFillColor(kWhite);
    212253        c.SetBorderMode(0);
    213254        c.Divide(1,2);
    214255
     256        // Output mean and rms to console
    215257        cerr << setprecision(4) << setw(10) << title << ":   ";
    216258        if (gt.GetN()==0)
     
    228270
    229271        TVirtualPad *pad = gPad;
     272
     273        // draw contants of pad 2 (counting starts at 0)
    230274        pad->cd(2);
    231275        gPad->SetBorderMode(0);
     
    237281        gPad->SetBottomMargin(0.08);
    238282
     283        // format axis
    239284        TH1 *h = gt.GetHistogram();
    240285
     
    246291        h->GetXaxis()->SetLabelOffset(0.01);
    247292
     293        // draw TGraph
    248294        gt.DrawClone("AP");
    249295        if (gt0.GetN()>0)
     
    252298            gt1.DrawClone("P");
    253299
     300        // Add lines and text showing the MAGIC periods
    254301        TLine l;
    255302        TText t;
     
    282329        //gPad->SaveAs(Form("plotdb-%s.eps", title.Data()));
    283330
     331        // Go back to first (upper) pad, format it and divide it again
    284332        pad->cd(1);
    285333        gPad->SetBorderMode(0);
     
    288336
    289337        TVirtualPad *pad2 = gPad;
     338
     339        // format left pad
    290340        pad2->cd(1);
    291341        gPad->SetBorderMode(0);
     
    294344        gPad->SetGridy();
    295345
     346        // Create histogram
    296347        const Int_t n = resolution>0 ? TMath::Nint((max-min)/resolution) : 50;
    297348
     
    299350        hist.SetDirectory(0);
    300351
     352        // Fill data into histogra,
    301353        for (int i=0; i<gt.GetN(); i++)
    302354            hist.Fill(gt.GetY()[i]);
    303355
     356        // Format histogram
    304357        if (fDescription.IsNull())
    305358            hist.SetXTitle(name);
    306359        hist.SetYTitle("Counts");
    307360
     361        // plot histogram
    308362        hist.DrawCopy("");
    309363
     364        // format right pad
    310365        pad2->cd(2);
    311366        gPad->SetBorderMode(0);
     
    313368        gPad->SetGridy();
    314369
     370        // format graph
    315371        TH1 *h2 = gz.GetHistogram();
    316372
     
    318374        h2->SetYTitle(name);
    319375
     376        // draw graph
    320377        gz.DrawClone("AP");
    321378
     
    375432
    376433        TString query="SELECT ";
    377         query += valued;
     434        switch (fGroupBy)
     435        {
     436        case kNone:
     437        case kGroupByPrimary:
     438            query += valued;
     439            break;
     440        case kGroupByHour:
     441            query += Form("DATE_FORMAT(%s, '%%Y-%%m-%%d %%H:30:00') AS %s ", fPrimaryDate.Data(), valued.Data());
     442            break;
     443        case kGroupByNight:
     444            query += Form("DATE_FORMAT(ADDDATE(%s,Interval 12 hour), '%%Y-%%m-%%d 00:00:00') AS %s ", fPrimaryDate.Data(), valued.Data());
     445            break;
     446        case kGroupByWeek:
     447            query += Form("DATE_FORMAT(ADDDATE(%s,Interval 12 hour), '%%x%%v') AS %s ", fPrimaryDate.Data(), valued.Data());
     448            break;
     449        case kGroupByMonth:
     450            query += Form("DATE_FORMAT(ADDDATE(%s,Interval 12 hour), '%%Y-%%m-15 00:00:00') AS %s ", fPrimaryDate.Data(), valued.Data());
     451            break;
     452        case kGroupBySeason:
     453            //query += Form("DATE_FORMAT(ADDDATE(%s,Interval 12 hour), '%%Y-%%m-15 00:00:00') AS %s ", fPrimaryDate.Data(), valued.Data());
     454            break;
     455        case kGroupByYear:
     456            query += Form("DATE_FORMAT(ADDDATE(%s,Interval 12 hour), '%%Y-08-15 00:00:00') AS %s ", fPrimaryDate.Data(), valued.Data());
     457            break;
     458        }
     459
    378460        if (fGroupBy==kNone)
    379461        {
     
    401483        }
    402484
    403         switch (fGroupBy)
    404         {
    405         case kNone:
    406         case kGroupByPrimary:
    407             break;
    408         case kGroupByHour:
    409             query += Form(", DATE_FORMAT(%s, '%%Y-%%m-%%d %%H') AS %s ", named.Data(), valued.Data());
    410             break;
    411         case kGroupByNight:
    412             query += Form(", DATE_FORMAT(ADDDATE(%s,Interval 12 hour), '%%Y-%%m-%%d') AS %s ", named.Data(), valued.Data());
    413             break;
    414         case kGroupByWeek:
    415             query += Form(", DATE_FORMAT(ADDDATE(%s,Interval 12 hour), '%%x%%v') AS %s ", named.Data(), valued.Data());
    416             break;
    417         case kGroupByMonth:
    418             query += Form(", DATE_FORMAT(ADDDATE(%s,Interval 12 hour), '%%Y-%%m') AS %s ", named.Data(), valued.Data());
    419             break;
    420         case kGroupByYear:
    421             query += Form(", DATE_FORMAT(ADDDATE(%s,Interval 12 hour), '%%Y') AS %s ", named.Data(), valued.Data());
    422             break;
    423         }
    424 
    425485        query += Form("FROM %s ", tabled.Data());
    426486
    427487        TString where(fCondition);
     488
     489        const Bool_t interval = !fRequestFrom.IsNull() && !fRequestTo.IsNull();
     490        if (interval)
     491        {
     492            if (!where.IsNull())
     493                where += " AND ";
     494            where += Form("%s BETWEEN '%s' AND '%s' ",
     495                          fPrimaryDate.Data(), fRequestFrom.Data(), fRequestTo.Data());
     496        }
    428497
    429498        if (!where.IsNull())
    430499            where += " AND ";
    431 
    432500        where += fCondition;
    433501        where += " ";
     
    448516            //query += Form(" HAVING COUNT(%s)=(COUNT(*)+1)/2 ", valuev.Data());
    449517        }
    450         query += Form("ORDER BY %s ", fPrimaryDate.Data());
    451 
     518        query += Form("ORDER BY %s ", valued.Data());
    452519
    453520        // ------------------------------
     
    469536void plotall(MPlot &plot, TString source)
    470537{
     538    // Setup here the values for timestamp and secondary (upper/right) plot
    471539    plot.SetPrimaryDate("OpticalData.fTimestamp");
    472540    plot.SetPrimaryNumber("OpticalData.fTimestamp");
    473541    plot.SetSecondary("OpticalData.fZenithDistance");
    474542
     543    // This is the condition to take only the "ok" flagged data
     544    // and to restrict the query to a given source (if any)
    475545    TString cond = "fStatusKEY=13";
    476546    if (!source.IsNull())
     
    484554    plot.SetCondition(cond);
    485555
     556    // Plot exposure
    486557    plot.SetDescription("Exposure;T_{E} [s]", "Expo");
    487558    plot.Plot("OpticalData.fExposure",  0, 900, 60);
    488559
     560    // plot sky level
    489561    plot.SetDescription("Sky Level;B [s^{-1}]", "SkyLvl");
    490562    plot.Plot("OpticalData.fSkyLevel/OpticalData.fExposure", 0, 5.0, 0.01);
    491563
     564    // plot FWHM
    492565    plot.SetDescription("Full Width Half Maximum;FWHM [s^{-1}]", "Fwhm");
    493566    plot.Plot("OpticalData.fFWHM/OpticalData.fExposure",  0, 0.05, 0.001);
    494567
     568    // plot Aperture Radius
    495569    plot.SetDescription("Aperture Radius;R_{A}", "ApRad");
    496570    plot.Plot("OpticalData.fApertureRadius", 0, 10, 1);
    497571
    498572    /*
    499      plot.SetDescription("Instrumental Magnitude;M_{I}\\cdot s^{-1}", "InstMag/s");
    500      plot.Plot("OpticalData.fInstrumentalMag/OpticalData.fExposure",  0, 0.2, 0.005);
    501 
    502      plot.SetDescription("Instrumental Magnitude Error;\\sigma_{M}\\cdot s^{-1}", "MagErr/s");
    503      plot.Plot("OpticalData.fInstrumentalMagErr/OpticalData.fExposure",  0, 0.01, 0.0002);
    504 
     573     // Plot instrumental magnitude
    505574     plot.SetDescription("Instrumental Magnitude;M_{I}", "InstMag");
    506575     plot.Plot("OpticalData.fInstrumentalMag",  0, 30, 0.5);
    507576
     577     // Plot error of instrumental magnitude
    508578     plot.SetDescription("Instrumental Magnitude Error;\\sigma_{M}", "MagErr");
    509579     plot.Plot("OpticalData.fInstrumentalMagErr",  0, 1, 0.01);
    510580     */
    511581
     582    // Plot magnitude corrected for the exposure
    512583    plot.SetDescription("m_{1};m_{1}", "M1");
    513584    plot.Plot("OpticalData.fInstrumentalMag+2.5*log10(OpticalData.fExposure)", 10, 35, 0.2);
    514585
     586    // Now take out all points named */BL from further queries
     587    // And skip all sources the magnitude is not known
    515588    cond += " AND Object.fObjectName NOT LIKE '%/BL' AND NOT ISNULL(Object.fMagnitude) ";
    516589    plot.SetCondition(cond);
    517590
     591    // Formula to calculate the extinction
    518592    TString ext("3080/25.0*pow(10, (OpticalData.fInstrumentalMag+2.5*log10(OpticalData.fExposure)-Object.fMagnitude)/-2.5)");
    519     ext += "+0.0028*fZenithDistance-0.08";
    520 
     593    // Add this to correct for the ZA dependancy
     594    //    ext += "+0.0028*fZenithDistance-0.08";
     595
     596    // Group all data of one image together and plot extinction
    521597    plot.SetGroupBy(MPlot::kGroupByPrimary);
    522598    plot.SetDescription("m_{1}-m_{true} (Extinction per Image);m_{1}-m_{true}", "ExtImg");
    523     plot.Plot(ext/*Form("AVG(%s)", ext.Data())*/, 0.05, 1.2, 0.01);
    524 
     599    plot.Plot(ext, 0.05, 1.2, 0.01);
     600
     601    // Group data hourly together and plot extinction
    525602    plot.SetGroupBy(MPlot::kGroupByHour);
    526603    plot.SetDescription("m_{1}-m_{true} (Extinction per Hour);m_{1}-m_{true}", "ExtHour");
    527     plot.Plot(ext/*Form("AVG(%s), date_format(fTimeStamp, '%%Y-%%m-%%d %%H') as fTimeStamp", ext.Data())*/,
    528               0.5, 1.2, 0.01);
    529 
     604    plot.Plot(ext, 0.5, 1.2, 0.01);
     605
     606    // Group data hourly together and plot extinction
    530607    plot.SetGroupBy(MPlot::kGroupByNight);
    531608    plot.SetDescription("m_{1}-m_{true} (Extinction per Night);m_{1}-m_{true}", "ExtNight");
    532     plot.Plot(ext/*Form("AVG(%s), date_format(adddate(fTimeStamp,Interval 12 hour),'%%Y-%%m-%%d') as fTimeStamp", ext.Data())*/,
    533               0.5, 1.2, 0.01);
     609    plot.Plot(ext, 0.5, 1.2, 0.01);
     610
     611    // Group data monthly together and plot extinction
     612    plot.SetGroupBy(MPlot::kGroupByMonth);
     613    plot.SetDescription("m_{1}-m_{true} (Extinction per Month);m_{1}-m_{true}", "ExtMonth");
     614    plot.Plot(ext, 0.5, 1.2, 0.01);
     615
     616    // Group data yearly together and plot extinction
     617    plot.SetGroupBy(MPlot::kGroupByYear);
     618    plot.SetDescription("m_{1}-m_{true} (Extinction per Year);m_{1}-m_{true}", "ExtYear");
     619    plot.Plot(ext, 0.5, 1.2, 0.01);
    534620}
    535621
     
    556642
    557643    MPlot plot(serv);
    558 //    plot.SetDataSet(dataset);
     644    // plot.SetDataSet(dataset);
    559645    plot.SetDisplay(d);
    560646    plot.SetRequestRange(from, to);
    561647    plotall(plot, source);
    562     d->SaveAsRoot("plotoptical.root");
    563     d->SaveAsPS("plotoptical.ps");
     648    // Use this to create output plots automatically
     649    //  d->SaveAsRoot("plotoptical.root");
     650    //  d->SaveAsPS("plotoptical.ps");
    564651
    565652    return 1;
     
    588675
    589676    MPlot plot(serv);
    590 //    plot.SetDataSet(ds);
     677    // plot.SetDataSet(ds);
    591678    plot.SetDisplay(d);
    592679    plot.SetRequestRange("", "");
    593680    plotall(plot, source);
    594     d->SaveAsRoot("plotoptical.root");
    595     d->SaveAsPS("plotoptical.ps");
     681    // Use this to create output plots automatically
     682    //  d->SaveAsRoot("plotoptical.root");
     683    //  d->SaveAsPS("plotoptical.ps");
    596684
    597685    return 1;
     
    620708
    621709    MPlot plot(serv);
    622 //    plot.SetDataSet(dataset);
     710    // plot.SetDataSet(dataset);
    623711    plot.SetDisplay(d);
    624712    plot.SetRequestPeriod(period);
    625713    plotall(plot, source);
    626     d->SaveAsRoot("plotoptical.root");
    627     d->SaveAsPS("plotoptical.ps");
     714
     715    // Use this to create output plots automatically
     716    //  d->SaveAsRoot("plotoptical.root");
     717    //  d->SaveAsPS("plotoptical.ps");
    628718
    629719    return 1;
  • trunk/MagicSoft/Mars/datacenter/macros/plotrundb.C

    r8186 r8212  
    11/* ======================================================================== *\
    2 ! $Name: not supported by cvs2svn $:$Id: plotrundb.C,v 1.1 2006-11-01 08:54:04 tbretz Exp $
     2! $Name: not supported by cvs2svn $:$Id: plotrundb.C,v 1.2 2006-11-03 10:52:36 tbretz Exp $
    33! --------------------------------------------------------------------------
    44!
     
    8484{
    8585public:
     86    // Possible constants to group-by (average) over a certain period
    8687    enum GroupBy_t
    8788    {
     
    9293        kGroupByWeek,
    9394        kGroupByMonth,
     95        kGroupBySeason,
    9496        kGroupByYear
    9597    };
     98
    9699private:
    97     MSQLMagic &fServer;
    98 
    99     MDataSet *fDataSet;
    100 
    101     TString   fPrimaryDate;
    102     TString   fPrimaryNumber;
    103     TString   fSecondary;
    104 
    105     TString   fRequestFrom;
    106     TString   fRequestTo;
    107     Int_t     fRequestPeriod;
     100    MSQLMagic &fServer;         // Reference to the sql-server class
     101
     102    MDataSet *fDataSet;         // A possible dtaset to highlite single points
     103
     104    TString   fPrimaryDate;     // The name of the data we plot
     105    TString   fPrimaryNumber;   // The corresponding name for the key number
     106    TString   fSecondary;       // The value versus which the second plot is made
     107
     108    TString   fRequestFrom;     // Start of a requested date range
     109    TString   fRequestTo;       // End of a requested date range
     110    Int_t     fRequestPeriod;   // A possible requested period
    108111
    109112    Float_t   fPlotMin;
     
    113116    Float_t   fHistMax;
    114117
    115     TString   fDescription;
    116     TString   fNameTab;
    117 
    118     TString   fCondition;
    119     GroupBy_t fGroupBy;
    120 
     118    TString   fDescription;     // The description (title) of the plot
     119    TString   fNameTab;         // The name of the tab in the display
     120
     121    TString   fCondition;       // An additional condition added to the query
     122    GroupBy_t fGroupBy;         // A possible Group-By flag
     123
     124    // --------------------------------------------------------------------------
     125    //
     126    // Function to plot the result of the query
     127    //
    121128    void PlotTable(TSQLResult &res, TString name, Float_t fmin, Float_t fmax, Float_t resolution)
    122129    {
     130        // Enable all otions in the statistics box
    123131        gStyle->SetOptStat(111111);
    124132
    125133        TSQLRow *row;
    126134
     135        // Create TGraph objects
    127136        TGraph &gt = res.GetFieldCount()>4 ? *new TGraphErrors : *new TGraph;
    128137        gt.SetNameTitle(name, Form("%s vs Time", name.Data()));
     
    148157        Int_t last  = -1;
    149158
     159        // Loop over the data
    150160        while ((row=res.Next()))
    151161        {
     162            // Get all fields of this row
    152163            const char *date = (*row)[0];
    153164            const char *zd   = (*row)[1];
     
    158169                continue;
    159170
     171            // check if date is valid
    160172            MTime t(date);
    161173            if (!t.SetSqlDateTime(date))
    162174                continue;
    163175
     176            // check if it belongs to the requested MAGIC period
    164177            if (fRequestPeriod>0 && MAstro::GetMagicPeriod(t.GetMjd())!=fRequestPeriod)
    165178                continue;
    166179
     180            // Get axis range
    167181            if (first<0)
    168182                first = TMath::Nint(TMath::Floor(t.GetMjd()));
    169183            last = TMath::Nint(TMath::Ceil(t.GetMjd()));
    170184
     185            // Convert a possible key number into a integer
    171186            UInt_t seq = snum ? atoi(snum) : 0;
    172    
     187
     188            // convert primary and secondary value into floats
    173189            Float_t value = atof(val);
    174190            Float_t zenith = atof(zd);
    175191
     192            // If a datset is given add the point to the special TGraphs
     193            // used for highliting these dates
    176194            if (fDataSet)
    177195            {
     
    189207            }
    190208
     209            // Add Data to TGraph
    191210            gt.SetPoint(gt.GetN(), t.GetAxisTime(), value);
    192211            gz.SetPoint(gz.GetN(), zenith, value);
    193212
     213            // Set error-bar, if one
    194214            if (verr)
    195215                static_cast<TGraphErrors&>(gt).SetPointError(gt.GetN()-1, 0, atof(verr));
     
    207227        gROOT->SetSelectedPad(0);
    208228
     229        // Create a TCanvas or open a new tab
    209230        TString title = fNameTab.IsNull() ? name(name.First('.')+2, name.Length()) : fNameTab;
    210231        TCanvas &c = fDisplay ? fDisplay->AddTab(title) : *new TCanvas;
     232        // Set fillcolor, remove border and divide pad
    211233        c.SetFillColor(kWhite);
    212234        c.SetBorderMode(0);
    213235        c.Divide(1,2);
    214236
     237        // Output mean and rms to console
    215238        cerr << setprecision(4) << setw(10) << title << ":   ";
    216239        if (gt.GetN()==0)
     
    228251
    229252        TVirtualPad *pad = gPad;
     253
     254        // draw contants of pad 2 (counting starts at 0)
    230255        pad->cd(2);
    231256        gPad->SetBorderMode(0);
     
    237262        gPad->SetBottomMargin(0.08);
    238263
     264        // format axis
    239265        TH1 *h = gt.GetHistogram();
    240266
     
    246272        h->GetXaxis()->SetLabelOffset(0.01);
    247273
     274        // draw TGraph
    248275        gt.DrawClone("AP");
    249276        if (gt0.GetN()>0)
     
    252279            gt1.DrawClone("P");
    253280
     281        // Add lines and text showing the MAGIC periods
    254282        TLine l;
    255283        TText t;
     
    282310        //gPad->SaveAs(Form("plotdb-%s.eps", title.Data()));
    283311
     312        // Go back to first (upper) pad, format it and divide it again
    284313        pad->cd(1);
    285314        gPad->SetBorderMode(0);
     
    288317
    289318        TVirtualPad *pad2 = gPad;
     319
     320        // format left pad
    290321        pad2->cd(1);
    291322        gPad->SetBorderMode(0);
     
    294325        gPad->SetGridy();
    295326
     327        // Create histogram
    296328        const Int_t n = resolution>0 ? TMath::Nint((max-min)/resolution) : 50;
    297329
     
    299331        hist.SetDirectory(0);
    300332
     333        // Fill data into histogra,
    301334        for (int i=0; i<gt.GetN(); i++)
    302335            hist.Fill(gt.GetY()[i]);
    303336
     337        // Format histogram
    304338        if (fDescription.IsNull())
    305339            hist.SetXTitle(name);
    306340        hist.SetYTitle("Counts");
    307341
     342        // plot histogram
    308343        hist.DrawCopy("");
    309344
     345        // format right pad
    310346        pad2->cd(2);
    311347        gPad->SetBorderMode(0);
     
    313349        gPad->SetGridy();
    314350
     351        // format graph
    315352        TH1 *h2 = gz.GetHistogram();
    316353
     
    318355        h2->SetYTitle(name);
    319356
     357        // draw graph
    320358        gz.DrawClone("AP");
     359
    321360        if (gz0.GetN()>0)
    322361            gz0.DrawClone("P");
     
    369408
    370409        TString query="SELECT ";
    371         query += valued;
     410        switch (fGroupBy)
     411        {
     412        case kNone:
     413        case kGroupByPrimary:
     414            query += valued;
     415            break;
     416        case kGroupByHour:
     417            query += Form("DATE_FORMAT(%s, '%%Y-%%m-%%d %%H:30:00') AS %s ", fPrimaryDate.Data(), valued.Data());
     418            break;
     419        case kGroupByNight:
     420            query += Form("DATE_FORMAT(ADDDATE(%s,Interval 12 hour), '%%Y-%%m-%%d 00:00:00') AS %s ", fPrimaryDate.Data(), valued.Data());
     421            break;
     422        case kGroupByWeek:
     423            query += Form("DATE_FORMAT(ADDDATE(%s,Interval 12 hour), '%%x%%v') AS %s ", fPrimaryDate.Data(), valued.Data());
     424            break;
     425        case kGroupByMonth:
     426            query += Form("DATE_FORMAT(ADDDATE(%s,Interval 12 hour), '%%Y-%%m-15 00:00:00') AS %s ", fPrimaryDate.Data(), valued.Data());
     427            break;
     428        case kGroupBySeason:
     429            //query += Form("DATE_FORMAT(ADDDATE(%s,Interval 12 hour), '%%Y-%%m-15 00:00:00') AS %s ", fPrimaryDate.Data(), valued.Data());
     430            break;
     431        case kGroupByYear:
     432            query += Form("DATE_FORMAT(ADDDATE(%s,Interval 12 hour), '%%Y-08-15 00:00:00') AS %s ", fPrimaryDate.Data(), valued.Data());
     433            break;
     434        }
     435
    372436        if (fGroupBy==kNone)
    373437        {
     
    395459        }
    396460
    397         switch (fGroupBy)
    398         {
    399         case kNone:
    400         case kGroupByPrimary:
    401             break;
    402         case kGroupByHour:
    403             query += Form(", DATE_FORMAT(%s, '%%Y-%%m-%%d %%H') AS %s ", named.Data(), valued.Data());
    404             break;
    405         case kGroupByNight:
    406             query += Form(", DATE_FORMAT(ADDDATE(%s,Interval 12 hour), '%%Y-%%m-%%d') AS %s ", named.Data(), valued.Data());
    407             break;
    408         case kGroupByWeek:
    409             query += Form(", DATE_FORMAT(ADDDATE(%s,Interval 12 hour), '%%x%%v') AS %s ", named.Data(), valued.Data());
    410             break;
    411         case kGroupByMonth:
    412             query += Form(", DATE_FORMAT(ADDDATE(%s,Interval 12 hour), '%%Y-%%m') AS %s ", named.Data(), valued.Data());
    413             break;
    414         case kGroupByYear:
    415             query += Form(", DATE_FORMAT(ADDDATE(%s,Interval 12 hour), '%%Y') AS %s ", named.Data(), valued.Data());
    416             break;
    417         }
    418 
    419461        query += Form("FROM %s ", tabled.Data());
    420462
     
    449491            //query += Form(" HAVING COUNT(%s)=(COUNT(*)+1)/2 ", valuev.Data());
    450492        }
    451         query += Form("ORDER BY %s ", fPrimaryDate.Data());
     493        query += Form("ORDER BY %s ", valued.Data());
    452494
    453495
Note: See TracChangeset for help on using the changeset viewer.