source: trunk/MagicSoft/Mars/datacenter/macros/plotrundb.C@ 8200

Last change on this file since 8200 was 8186, checked in by tbretz, 19 years ago
*** empty log message ***
File size: 18.7 KB
Line 
1/* ======================================================================== *\
2! $Name: not supported by cvs2svn $:$Id: plotrundb.C,v 1.1 2006-11-01 08:54:04 tbretz Exp $
3! --------------------------------------------------------------------------
4!
5! *
6! * This file is part of MARS, the MAGIC Analysis and Reconstruction
7! * Software. It is distributed to you in the hope that it can be a useful
8! * and timesaving tool in analysing Data of imaging Cerenkov telescopes.
9! * It is distributed WITHOUT ANY WARRANTY.
10! *
11! * Permission to use, copy, modify and distribute this software and its
12! * documentation for any purpose is hereby granted without fee,
13! * provided that the above copyright notice appear in all copies and
14! * that both that copyright notice and this permission notice appear
15! * in supporting documentation. It is provided "as is" without express
16! * or implied warranty.
17! *
18!
19!
20! Author(s): Thomas Bretz, 05/2005 <mailto:tbretz@astro.uni-wuerzburg.de>
21! Author(s): Daniela Dorner, 05/2005 <mailto:dorner@astro.uni-wuerzburg.de>
22!
23! Copyright: MAGIC Software Development, 2000-2006
24!
25!
26\* ======================================================================== */
27
28/////////////////////////////////////////////////////////////////////////////
29//
30// plotundb.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
41//
42// In the DB these values are stored in the tables Calibration and Star.
43//
44// Usage:
45// .x plotundb.C --> all values in the DB are plotted
46// You can chose are certain period:
47// .x plotrundb.C(25) --> all values from period 25 are plotted
48// or a time period from a certain date to a certain date
49// .x plotrundb.C("2004-11-14 00:00:00", "2005-02-28 00:00:00")
50// --> 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 plotrundb.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
56//
57// Make sure, that database and password are corretly set in a resource
58// file called sql.rc and the resource file is found.
59//
60/////////////////////////////////////////////////////////////////////////////
61#include <iostream>
62#include <iomanip>
63
64#include <TH1.h>
65#include <TEnv.h>
66#include <TPad.h>
67#include <TLine.h>
68#include <TText.h>
69#include <TFrame.h>
70#include <TStyle.h>
71#include <TCanvas.h>
72#include <TPRegexp.h>
73#include <TSQLRow.h>
74#include <TSQLResult.h>
75#include <TGraphErrors.h>
76
77#include "MTime.h"
78#include "MAstro.h"
79#include "MDataSet.h"
80#include "MSQLMagic.h"
81#include "MStatusDisplay.h"
82
83class MPlot : public MParContainer
84{
85public:
86 enum GroupBy_t
87 {
88 kNone,
89 kGroupByPrimary,
90 kGroupByHour,
91 kGroupByNight,
92 kGroupByWeek,
93 kGroupByMonth,
94 kGroupByYear
95 };
96private:
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;
108
109 Float_t fPlotMin;
110 Float_t fPlotMax;
111
112 Float_t fHistMin;
113 Float_t fHistMax;
114
115 TString fDescription;
116 TString fNameTab;
117
118 TString fCondition;
119 GroupBy_t fGroupBy;
120
121 void PlotTable(TSQLResult &res, TString name, Float_t fmin, Float_t fmax, Float_t resolution)
122 {
123 gStyle->SetOptStat(111111);
124
125 TSQLRow *row;
126
127 TGraph &gt = res.GetFieldCount()>4 ? *new TGraphErrors : *new TGraph;
128 gt.SetNameTitle(name, Form("%s vs Time", name.Data()));
129 gt.SetMarkerStyle(kFullDotMedium);
130
131 TGraph gz;
132 gz.SetNameTitle(name, Form("%s vs <Zd>", name.Data()));
133 gz.SetMarkerStyle(kFullDotMedium);
134
135 TGraph gt0, gt1;
136 gt0.SetMarkerColor(kRed);
137 gt1.SetMarkerColor(kBlue);
138 gt0.SetMarkerStyle(kFullDotLarge);
139 gt1.SetMarkerStyle(kFullDotLarge);
140
141 TGraph gz0, gz1;
142 gz0.SetMarkerColor(kRed);
143 gz1.SetMarkerColor(kBlue);
144 gz0.SetMarkerStyle(kFullDotLarge);
145 gz1.SetMarkerStyle(kFullDotLarge);
146
147 Int_t first = -1;
148 Int_t last = -1;
149
150 while ((row=res.Next()))
151 {
152 const char *date = (*row)[0];
153 const char *zd = (*row)[1];
154 const char *val = (*row)[2];
155 const char *snum = res.GetFieldCount()>3 ? (*row)[3] : 0;
156 const char *verr = res.GetFieldCount()>4 ? (*row)[5] : 0;
157 if (!date || !val || !zd)
158 continue;
159
160 MTime t(date);
161 if (!t.SetSqlDateTime(date))
162 continue;
163
164 if (fRequestPeriod>0 && MAstro::GetMagicPeriod(t.GetMjd())!=fRequestPeriod)
165 continue;
166
167 if (first<0)
168 first = TMath::Nint(TMath::Floor(t.GetMjd()));
169 last = TMath::Nint(TMath::Ceil(t.GetMjd()));
170
171 UInt_t seq = snum ? atoi(snum) : 0;
172
173 Float_t value = atof(val);
174 Float_t zenith = atof(zd);
175
176 if (fDataSet)
177 {
178 if (fDataSet->HasOnSequence(seq))
179 {
180 gt1.SetPoint(gt1.GetN(), t.GetAxisTime(), value);
181 gz1.SetPoint(gz1.GetN(), zenith, value);
182 }
183
184 if (fDataSet->HasOffSequence(seq))
185 {
186 gt0.SetPoint(gt0.GetN(), t.GetAxisTime(), value);
187 gz0.SetPoint(gz0.GetN(), zenith, value);
188 }
189 }
190
191 gt.SetPoint(gt.GetN(), t.GetAxisTime(), value);
192 gz.SetPoint(gz.GetN(), zenith, value);
193
194 if (verr)
195 static_cast<TGraphErrors&>(gt).SetPointError(gt.GetN()-1, 0, atof(verr));
196 }
197
198 // If this is done earlier the plots remain empty since root 5.12/00
199 if (fmax>fmin)
200 {
201 gt.SetMinimum(fmin);
202 gt.SetMaximum(fmax);
203 gz.SetMinimum(fmin);
204 gz.SetMaximum(fmax);
205 }
206
207 gROOT->SetSelectedPad(0);
208
209 TString title = fNameTab.IsNull() ? name(name.First('.')+2, name.Length()) : fNameTab;
210 TCanvas &c = fDisplay ? fDisplay->AddTab(title) : *new TCanvas;
211 c.SetFillColor(kWhite);
212 c.SetBorderMode(0);
213 c.Divide(1,2);
214
215 cerr << setprecision(4) << setw(10) << title << ": ";
216 if (gt.GetN()==0)
217 {
218 cerr << " <empty>" << endl;
219 return;
220 }
221 cerr << setw(8) << gt.GetMean(2) << "+-" << setw(8) << gt.GetRMS(2) << " ";
222 if (gt0.GetN()>0 || gt1.GetN()>0)
223 {
224 cerr << setw(8) << gt1.GetMean(2) << "+-" << setw(8) << gt1.GetRMS(2) << " ";
225 cerr << setw(8) << gt0.GetMean(2) << "+-" << setw(8) << gt0.GetRMS(2);
226 }
227 cerr << endl;
228
229 TVirtualPad *pad = gPad;
230 pad->cd(2);
231 gPad->SetBorderMode(0);
232 gPad->SetFrameBorderMode(0);
233 gPad->SetGridy();
234
235 gPad->SetLeftMargin(0.06);
236 gPad->SetRightMargin(0.06);
237 gPad->SetBottomMargin(0.08);
238
239 TH1 *h = gt.GetHistogram();
240
241 h->SetXTitle("Time");
242 h->SetYTitle(name);
243 h->GetXaxis()->SetTimeDisplay(1);
244 h->GetYaxis()->SetTitleOffset(0.8);
245 h->GetXaxis()->SetTitleOffset(1.0);
246 h->GetXaxis()->SetLabelOffset(0.01);
247
248 gt.DrawClone("AP");
249 if (gt0.GetN()>0)
250 gt0.DrawClone("P");
251 if (gt1.GetN()>0)
252 gt1.DrawClone("P");
253
254 TLine l;
255 TText t;
256 Int_t num=0;
257 l.SetLineStyle(kDotted);
258 l.SetLineColor(kBlue);
259 t.SetTextColor(kBlue);
260 l.SetLineWidth(1);
261 t.SetTextSize(h->GetXaxis()->GetLabelSize());
262 t.SetTextAlign(21);
263 Int_t p0 = MAstro::GetMagicPeriod(first);
264 for (Int_t p = first; p<last; p++)
265 {
266 Int_t p1 = MAstro::GetMagicPeriod(p);
267 if (p1!=p0)
268 {
269 l.DrawLine(MTime(p).GetAxisTime(), h->GetMinimum(), MTime(p).GetAxisTime(), h->GetMaximum());
270 t.DrawText(MTime(p+15).GetAxisTime(), h->GetMaximum(), Form("%d", p1));
271 num++;
272 }
273 p0 = p1;
274 }
275 if (num<4)
276 gPad->SetGridx();
277
278 const Double_t min = fHistMin>fHistMax ? h->GetMinimum()-resolution/2 : fHistMin;
279 const Double_t max = fHistMin>fHistMax ? h->GetMaximum()+resolution/2 : fHistMax;
280
281 // Use this to save the pad with the time development to a file
282 //gPad->SaveAs(Form("plotdb-%s.eps", title.Data()));
283
284 pad->cd(1);
285 gPad->SetBorderMode(0);
286 gPad->SetFrameBorderMode(0);
287 gPad->Divide(2,1);
288
289 TVirtualPad *pad2 = gPad;
290 pad2->cd(1);
291 gPad->SetBorderMode(0);
292 gPad->SetFrameBorderMode(0);
293 gPad->SetGridx();
294 gPad->SetGridy();
295
296 const Int_t n = resolution>0 ? TMath::Nint((max-min)/resolution) : 50;
297
298 TH1F hist("Hist", Form("Distribution of %s", fDescription.IsNull() ? name.Data() : fDescription.Data()), n, min, max);
299 hist.SetDirectory(0);
300
301 for (int i=0; i<gt.GetN(); i++)
302 hist.Fill(gt.GetY()[i]);
303
304 if (fDescription.IsNull())
305 hist.SetXTitle(name);
306 hist.SetYTitle("Counts");
307
308 hist.DrawCopy("");
309
310 pad2->cd(2);
311 gPad->SetBorderMode(0);
312 gPad->SetFrameBorderMode(0);
313 gPad->SetGridy();
314
315 TH1 *h2 = gz.GetHistogram();
316
317 h2->SetXTitle("Zd");
318 h2->SetYTitle(name);
319
320 gz.DrawClone("AP");
321 if (gz0.GetN()>0)
322 gz0.DrawClone("P");
323 if (gz1.GetN()>0)
324 gz1.DrawClone("P");
325 }
326
327public:
328 MPlot(MSQLMagic &server) : fServer(server), fDataSet(NULL),
329 fRequestPeriod(-1), fPlotMin(0), fPlotMax(-1), fHistMin(0), fHistMax(-1), fGroupBy(kNone)
330 {
331 }
332 ~MPlot()
333 {
334 if (fDataSet)
335 delete fDataSet;
336 }
337 void SetDataSet(const TString filename)
338 {
339 if (fDataSet)
340 {
341 delete fDataSet;
342 fDataSet = NULL;
343 }
344 if (!filename.IsNull())
345 fDataSet = new MDataSet(filename);
346 }
347 void SetPlotRange(Float_t min, Float_t max, Int_t n=5) { fPlotMin = min; fPlotMax = max; }
348 void SetHistRange(Float_t min, Float_t max) { fHistMin = min; fHistMax = max; }
349 void SetRequestRange(const char *from="", const char *to="") { fRequestFrom = from; fRequestTo = to; }
350 void SetRequestPeriod(Int_t n=-1) { fRequestPeriod = n; }
351 void SetCondition(const char *cond="") { fCondition = cond; }
352 void SetDescription(const char *d, const char *t=0) { fDescription = d; fNameTab = t; }
353 void SetGroupBy(GroupBy_t b=kGroupByWeek) { fGroupBy=b; }
354 void SetPrimaryDate(const char *ts) { fPrimaryDate=ts; }
355 void SetPrimaryNumber(const char *ts) { fPrimaryNumber=ts; }
356 void SetSecondary(const char *ts) { fSecondary=ts; }
357
358 Bool_t Plot(const char *value, Float_t min=0, Float_t max=-1, Float_t resolution=0)
359 {
360 TString named = fPrimaryDate;
361 TString named2 = fSecondary;
362 TString namev = value;
363
364 TString tablev = namev(0, namev.First('.'));
365 TString valuev = namev(namev.First('.')+1, namev.Length());
366
367 TString tabled = named(0, named.First('.'));
368 TString valued = named(named.First('.')+1, named.Length());
369
370 TString query="SELECT ";
371 query += valued;
372 if (fGroupBy==kNone)
373 {
374 query += ", ";
375 query += fSecondary;
376 query += ", ";
377 query += value;
378 query += ", ";
379 query += fPrimaryNumber;
380 query += " ";
381 }
382 else
383 {
384 query += ", AVG(";
385 query += fSecondary;
386 query += "), AVG(";
387 query += value;
388 query += "), ";
389 query += fPrimaryNumber;
390 query += ", STD(";
391 query += fSecondary;
392 query += "), STD(";
393 query += value;
394 query += ") ";
395 }
396
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
419 query += Form("FROM %s ", tabled.Data());
420
421 const Bool_t interval = !fRequestFrom.IsNull() && !fRequestTo.IsNull();
422
423 TString where(fCondition);
424 if (!where.IsNull())
425 where += " AND ";
426 where += "RunData.fExcludedFDAKEY=1 ";
427
428 if (interval)
429 {
430 if (!where.IsNull())
431 where += " AND ";
432 where += Form("%s BETWEEN '%s' AND '%s' ",
433 fPrimaryDate.Data(), fRequestFrom.Data(), fRequestTo.Data());
434 }
435
436 // ------------------------------
437
438 query += fServer.GetJoins(tabled, query+" "+where);
439
440 if (!where.IsNull())
441 {
442 query += "WHERE ";
443 query += where;
444 }
445
446 if (fGroupBy!=kNone)
447 {
448 query += Form("GROUP BY %s ", valued.Data());
449 //query += Form(" HAVING COUNT(%s)=(COUNT(*)+1)/2 ", valuev.Data());
450 }
451 query += Form("ORDER BY %s ", fPrimaryDate.Data());
452
453
454 // ------------------------------
455
456 TSQLResult *res = fServer.Query(query);
457 if (!res)
458 {
459 cout << "ERROR - Query failed: " << query << endl;
460 return kFALSE;
461 }
462
463 if (max>min)
464 PlotTable(*res, namev, min, max, resolution);
465 else
466 PlotTable(*res, namev, fPlotMin, fPlotMax, resolution);
467
468
469 delete res;
470 return kTRUE;
471 }
472};
473
474void plotall(MPlot &plot)
475{
476 plot.SetGroupBy(MPlot::kGroupByNight);
477
478 plot.SetPrimaryDate("RunData.fRunStart");
479 plot.SetPrimaryNumber("RunData.fRunNumber");
480 plot.SetSecondary("RunData.fZenithDistance");
481
482 //inner camera
483 //from calib*.root
484 plot.SetDescription("DAQ Storage Rate;R_{DAQ,S} [Hz]", "StoreRate");
485 plot.Plot("RunData.fDaqStoreRate", -0.5, 999.5, 5);
486 plot.SetDescription("DAQ Trigger Rate;R_{DAQ,T} [Hz]", "TrigRate");
487 plot.Plot("RunData.fDaqTriggerRate", -0.5, 999.5, 5);
488 plot.SetDescription("Mean Trigger Rate;<R> [Hz]", "MeanTrig");
489 plot.Plot("RunData.fMeanTriggerRate", -0.5, 999.5, 5);
490 plot.SetDescription("L2 Trigger rate after prescaler;R_{L2,P}", "Presc");
491 plot.Plot("RunData.fL2RatePresc", -0.5, 999.5, 5);
492 plot.SetDescription("L2 Trigger rate before prescaler;R_{L2,U}", "Unpresc");
493 plot.Plot("RunData.fL2RateUnpresc", -0.5, 999.5, 5);
494
495 //from signal*.root
496 plot.SetDescription("Signal Position;<T_{S}> [sl]", "PosMean");
497 plot.Plot("DataCheck.fPositionSignal", -0.5, 29.5, 1);
498 plot.SetDescription("Signal Position FWHM;\\sigma_{S} [sl]", "PosFWHM");
499 plot.Plot("DataCheck.fPositionFWHM", -0.5, 29.5, 1);
500 plot.SetDescription("Signal Height;<H_{s}> [counts]", "SigMean");
501 plot.Plot("DataCheck.fHeightSignal", -0.5, 256.5, 1);
502 plot.SetDescription("Signal Height FWHM;\\sigma_{H} [counts]", "SigFWHM");
503 plot.Plot("DataCheck.fHeightFWHM", -0.5, 155.5, 1);
504
505 plot.SetDescription("Interleaved Signal Position;<T_{S}> [sl]", "IPosMean");
506 plot.Plot("DataCheck.fPositionSignalInterlaced", -0.5, 29.5, 1);
507 plot.SetDescription("Interleaved Signal Position FWHM;\\sigma_{S} [sl]", "IPosFWHM");
508 plot.Plot("DataCheck.fPositionFWHMInterlaced", -0.5, 29.5, 1);
509 plot.SetDescription("Interleaved Signal Height;<H_{s}> [counts]", "ISigMean");
510 plot.Plot("DataCheck.fHeightSignalInterlaced", -0.5, 256.5, 1);
511 plot.SetDescription("Interleaved Signal Height FWHM;\\sigma_{H} [counts]", "ISigFWHM");
512 plot.Plot("DataCheck.fHeightFWHMInterlaced", -0.5, 155.5, 1);
513}
514
515int plotrundb(TString from, TString to, const char *dataset=0)
516{
517 TEnv env("sql.rc");
518
519 MSQLMagic serv(env);
520 if (!serv.IsConnected())
521 {
522 cout << "ERROR - Connection to database failed." << endl;
523 return 0;
524 }
525
526 cout << "plotrundb" << endl;
527 cout << "---------" << endl;
528 cout << endl;
529 cout << "Connected to " << serv.GetName() << endl;
530 cout << endl;
531
532 MStatusDisplay *d = new MStatusDisplay;
533 d->SetWindowName(serv.GetName());
534 d->SetTitle(serv.GetName());
535
536 MPlot plot(serv);
537 plot.SetDataSet(dataset);
538 plot.SetDisplay(d);
539 plot.SetRequestRange(from, to);
540 plotall(plot);
541 d->SaveAsRoot("plotrundb.root");
542 d->SaveAsPS("plotrundb.ps");
543
544 return 1;
545}
546
547int plotrundb(const char *ds)
548{
549 TEnv env("sql.rc");
550
551 MSQLMagic serv(env);
552 if (!serv.IsConnected())
553 {
554 cout << "ERROR - Connection to database failed." << endl;
555 return 0;
556 }
557
558 cout << "plotrundb" << endl;
559 cout << "---------" << endl;
560 cout << endl;
561 cout << "Connected to " << serv.GetName() << endl;
562 cout << endl;
563
564 MStatusDisplay *d = new MStatusDisplay;
565 d->SetWindowName(serv.GetName());
566 d->SetTitle(serv.GetName());
567
568 MPlot plot(serv);
569 plot.SetDataSet(ds);
570 plot.SetDisplay(d);
571 plot.SetRequestRange("", "");
572 plotall(plot);
573 d->SaveAsRoot("plotrundb.root");
574 d->SaveAsPS("plotrundb.ps");
575
576 return 1;
577}
578
579int plotrundb(Int_t period, const char *dataset="")
580{
581 TEnv env("sql.rc");
582
583 MSQLMagic serv(env);
584 if (!serv.IsConnected())
585 {
586 cout << "ERROR - Connection to database failed." << endl;
587 return 0;
588 }
589
590 cout << "plotrundb" << endl;
591 cout << "---------" << endl;
592 cout << endl;
593 cout << "Connected to " << serv.GetName() << endl;
594 cout << endl;
595
596 MStatusDisplay *d = new MStatusDisplay;
597 d->SetWindowName(serv.GetName());
598 d->SetTitle(serv.GetName());
599
600 MPlot plot(serv);
601 plot.SetDataSet(dataset);
602 plot.SetDisplay(d);
603 plot.SetRequestPeriod(period);
604 plotall(plot);
605 d->SaveAsRoot("plotrundb.root");
606 d->SaveAsPS("plotrundb.ps");
607
608 return 1;
609}
610
611int plotrundb()
612{
613 return plotrundb("", "");
614}
Note: See TracBrowser for help on using the repository browser.