source: trunk/MagicSoft/Mars/datacenter/macros/plotdb.C@ 8131

Last change on this file since 8131 was 8131, checked in by tbretz, 18 years ago
*** empty log message ***
File size: 19.0 KB
Line 
1/* ======================================================================== *\
2!
3! *
4! * This file is part of MARS, the MAGIC Analysis and Reconstruction
5! * Software. It is distributed to you in the hope that it can be a useful
6! * and timesaving tool in analysing Data of imaging Cerenkov telescopes.
7! * It is distributed WITHOUT ANY WARRANTY.
8! *
9! * Permission to use, copy, modify and distribute this software and its
10! * documentation for any purpose is hereby granted without fee,
11! * provided that the above copyright notice appear in all copies and
12! * that both that copyright notice and this permission notice appear
13! * in supporting documentation. It is provided "as is" without express
14! * or implied warranty.
15! *
16!
17!
18! Author(s): Thomas Bretz, 05/2005 <mailto:tbretz@astro.uni-wuerzburg.de>
19! Author(s): Daniela Dorner, 05/2005 <mailto:dorner@astro.uni-wuerzburg.de>
20!
21! Copyright: MAGIC Software Development, 2000-2006
22!
23!
24\* ======================================================================== */
25
26/////////////////////////////////////////////////////////////////////////////
27//
28// plotdb.C
29// ========
30//
31// This macro is used to read quality parameters from the DB and plot them.
32//
33// The parameters are from the following files:
34// calib*.root:mean conversion factor, mean arrival time, rms arrival time
35// (each parameter for inner and outer camera)
36// signal*.root: mean pedestal rms (for inner and outer camera)
37// star*.root: PSF, # of Muons, Effective OnTime, Muon rate,
38// Ratio MC/Data(MuonSize) and mean number of islands
39//
40// In the DB these values are stored in the tables Calibration and Star.
41//
42// Usage:
43// .x plotdb.C --> all values in the DB are plotted
44// You can chose are certain period:
45// .x plotdb.C(25) --> all values from period 25 are plotted
46// or a time period from a certain date to a certain date
47// .x plotdb.C("2004-11-14 00:00:00", "2005-02-28 00:00:00")
48// --> all values from 14.11.2004 0h to 28.2.2005 0h are plotted
49// or all data, but with dataset data highlighted
50// .x plotdb.C("dataset.txt")
51// --> the sequences defined in dataset.txt are highlighted (blue:on, red:off)
52// --> You can also add a dataset-name as last argument to one of the
53// calls above
54//
55// Make sure, that database and password are corretly set in a resource
56// file called sql.rc and the resource file is found.
57//
58/////////////////////////////////////////////////////////////////////////////
59#include <iostream>
60#include <iomanip>
61
62#include <TH1.h>
63#include <TEnv.h>
64#include <TPad.h>
65#include <TLine.h>
66#include <TText.h>
67#include <TFrame.h>
68#include <TStyle.h>
69#include <TGraph.h>
70#include <TCanvas.h>
71#include <TSQLRow.h>
72#include <TSQLResult.h>
73
74#include "MTime.h"
75#include "MAstro.h"
76#include "MDataSet.h"
77#include "MSQLServer.h"
78#include "MStatusDisplay.h"
79
80class MPlot : public MParContainer
81{
82private:
83 MSQLServer &fServer;
84
85 MDataSet *fDataSet;
86
87 TString fRequestFrom;
88 TString fRequestTo;
89 Int_t fRequestPeriod;
90
91 Float_t fPlotMin;
92 Float_t fPlotMax;
93
94 Float_t fHistMin;
95 Float_t fHistMax;
96
97 TString fDescription;
98 TString fNameTab;
99
100 void PlotTable(TSQLResult &res, TString name, Float_t fmin, Float_t fmax, Float_t resolution)
101 {
102 gStyle->SetOptStat(111111);
103
104 TSQLRow *row;
105
106 TGraph gt;
107 gt.SetNameTitle(name, Form("%s vs Time", name.Data()));
108 gt.SetMarkerStyle(kFullDotMedium);
109
110 TGraph gz;
111 gz.SetNameTitle(name, Form("%s vs <Zd>", name.Data()));
112 gz.SetMarkerStyle(kFullDotMedium);
113
114 TGraph gt0, gt1;
115 gt0.SetMarkerColor(kRed);
116 gt1.SetMarkerColor(kBlue);
117 gt0.SetMarkerStyle(kFullDotLarge);
118 gt1.SetMarkerStyle(kFullDotLarge);
119
120 TGraph gz0, gz1;
121 gz0.SetMarkerColor(kRed);
122 gz1.SetMarkerColor(kBlue);
123 gz0.SetMarkerStyle(kFullDotLarge);
124 gz1.SetMarkerStyle(kFullDotLarge);
125
126 Int_t first = -1;
127 Int_t last = -1;
128
129 while ((row=res.Next()))
130 {
131 const char *date = (*row)[0];
132 const char *zd = (*row)[1];
133 const char *val = (*row)[2];
134 const char *snum = (*row)[3];
135 if (!date || !val || !zd || !snum)
136 continue;
137
138 MTime t(date);
139 if (!t.SetSqlDateTime(date))
140 continue;
141
142 if (fRequestPeriod>0 && MAstro::GetMagicPeriod(t.GetMjd())!=fRequestPeriod)
143 continue;
144
145 if (first<0)
146 first = TMath::Nint(TMath::Floor(t.GetMjd()));
147 last = TMath::Nint(TMath::Ceil(t.GetMjd()));
148
149 UInt_t seq = atoi(snum);
150
151 Float_t value = atof(val);
152 Float_t zenith = atof(zd);
153
154 if (fDataSet)
155 {
156 if (fDataSet->HasOnSequence(seq))
157 {
158 gt1.SetPoint(gt1.GetN(), t.GetAxisTime(), value);
159 gz1.SetPoint(gz1.GetN(), zenith, value);
160 }
161
162 if (fDataSet->HasOffSequence(seq))
163 {
164 gt0.SetPoint(gt0.GetN(), t.GetAxisTime(), value);
165 gz0.SetPoint(gz0.GetN(), zenith, value);
166 }
167 }
168
169 gt.SetPoint(gt.GetN(), t.GetAxisTime(), value);
170 gz.SetPoint(gz.GetN(), zenith, value);
171 }
172
173 // If this is done earlier the plots remain empty since root 5.12/00
174 if (fmax>fmin)
175 {
176 gt.SetMinimum(fmin);
177 gt.SetMaximum(fmax);
178 gz.SetMinimum(fmin);
179 gz.SetMaximum(fmax);
180 }
181
182 gROOT->SetSelectedPad(0);
183
184 TString title = fNameTab.IsNull() ? name(name.First('.')+2, name.Length()) : fNameTab;
185 TCanvas &c = fDisplay ? fDisplay->AddTab(title) : *new TCanvas;
186 c.SetFillColor(kWhite);
187 c.SetBorderMode(0);
188 c.Divide(1,2);
189
190 cerr << setprecision(4) << setw(10) << title << ": ";
191 cerr << setw(8) << gt.GetMean(2) << "+-" << setw(8) << gt.GetRMS(2) << " ";
192 if (gt0.GetN()>0 || gt1.GetN()>0)
193 {
194 cerr << setw(8) << gt1.GetMean(2) << "+-" << setw(8) << gt1.GetRMS(2) << " ";
195 cerr << setw(8) << gt0.GetMean(2) << "+-" << setw(8) << gt0.GetRMS(2);
196 }
197 cerr << endl;
198
199 TVirtualPad *pad = gPad;
200 pad->cd(2);
201 gPad->SetBorderMode(0);
202 gPad->SetFrameBorderMode(0);
203 gPad->SetGridy();
204
205 gPad->SetLeftMargin(0.06);
206 gPad->SetRightMargin(0.06);
207 gPad->SetBottomMargin(0.08);
208
209 TH1 *h = gt.GetHistogram();
210
211 h->SetXTitle("Time");
212 h->SetYTitle(name);
213 h->GetXaxis()->SetTimeDisplay(1);
214 h->GetYaxis()->SetTitleOffset(0.8);
215 h->GetXaxis()->SetTitleOffset(1.0);
216 h->GetXaxis()->SetLabelOffset(0.01);
217
218 gt.DrawClone("AP");
219 if (gt0.GetN()>0)
220 gt0.DrawClone("P");
221 if (gt1.GetN()>0)
222 gt1.DrawClone("P");
223
224 TLine l;
225 TText t;
226 Int_t num=0;
227 l.SetLineStyle(kDotted);
228 l.SetLineColor(kBlue);
229 t.SetTextColor(kBlue);
230 l.SetLineWidth(1);
231 t.SetTextSize(h->GetXaxis()->GetLabelSize());
232 t.SetTextAlign(21);
233 Int_t p0 = MAstro::GetMagicPeriod(first);
234 for (Int_t p = first; p<last; p++)
235 {
236 Int_t p1 = MAstro::GetMagicPeriod(p);
237 if (p1!=p0)
238 {
239 l.DrawLine(MTime(p).GetAxisTime(), h->GetMinimum(), MTime(p).GetAxisTime(), h->GetMaximum());
240 t.DrawText(MTime(p+15).GetAxisTime(), h->GetMaximum(), Form("%d", p1));
241 num++;
242 }
243 p0 = p1;
244 }
245 if (num<4)
246 gPad->SetGridx();
247
248 const Double_t min = fHistMin>fHistMax ? h->GetMinimum()-resolution/2 : fHistMin;
249 const Double_t max = fHistMin>fHistMax ? h->GetMaximum()+resolution/2 : fHistMax;
250
251 // Use this to save the pad with the time development to a file
252 //gPad->SaveAs(Form("plotdb-%s.eps", title.Data()));
253
254 pad->cd(1);
255 gPad->SetBorderMode(0);
256 gPad->SetFrameBorderMode(0);
257 gPad->Divide(2,1);
258
259 TVirtualPad *pad2 = gPad;
260 pad2->cd(1);
261 gPad->SetBorderMode(0);
262 gPad->SetFrameBorderMode(0);
263 gPad->SetGridx();
264 gPad->SetGridy();
265
266 const Int_t n = resolution>0 ? TMath::Nint((max-min)/resolution) : 50;
267
268 TH1F hist("Hist", Form("Distribution of %s", fDescription.IsNull() ? name.Data() : fDescription.Data()), n, min, max);
269 hist.SetDirectory(0);
270
271 for (int i=0; i<gt.GetN(); i++)
272 hist.Fill(gt.GetY()[i]);
273
274 if (fDescription.IsNull())
275 hist.SetXTitle(name);
276 hist.SetYTitle("Counts");
277
278 hist.DrawCopy("");
279
280 pad2->cd(2);
281 gPad->SetBorderMode(0);
282 gPad->SetFrameBorderMode(0);
283 gPad->SetGridy();
284
285 TH1 *h2 = gz.GetHistogram();
286
287 h2->SetXTitle("Zd");
288 h2->SetYTitle(name);
289
290 gz.DrawClone("AP");
291 if (gz0.GetN()>0)
292 gz0.DrawClone("P");
293 if (gz1.GetN()>0)
294 gz1.DrawClone("P");
295 }
296
297public:
298 MPlot(MSQLServer &server) : fServer(server), fDataSet(NULL),
299 fRequestPeriod(-1), fPlotMin(0), fPlotMax(-1), fHistMin(0), fHistMax(-1)
300 {
301 }
302 ~MPlot()
303 {
304 if (fDataSet)
305 delete fDataSet;
306 }
307 void SetDataSet(const TString filename)
308 {
309 if (fDataSet)
310 {
311 delete fDataSet;
312 fDataSet = NULL;
313 }
314 if (!filename.IsNull())
315 fDataSet = new MDataSet(filename);
316 }
317 void SetPlotRange(Float_t min, Float_t max, Int_t n=5)
318 { fPlotMin = min; fPlotMax = max; }
319 void SetHistRange(Float_t min, Float_t max)
320 { fHistMin = min; fHistMax = max; }
321 void SetRequestRange(const char *from="", const char *to="")
322 { fRequestFrom = from; fRequestTo = to; }
323 void SetRequestPeriod(Int_t n=-1)
324 { fRequestPeriod = n; }
325 void SetDescription(const char *d, const char *t=0) { fDescription = d; fNameTab = t; }
326
327 Bool_t Plot(const char *value, Float_t min=0, Float_t max=-1, Float_t resolution=0)
328 {
329 TString named = "Sequences.fRunStart";
330 TString named2 = "(Sequences.fZenithDistanceMin+Sequences.fZenithDistanceMax)/2";
331 TString namev = value;
332 TString join = "fSequenceFirst";
333
334 TString tablev = namev(0, namev.First('.'));
335 TString valuev = namev(namev.First('.')+1, namev.Length());
336
337 TString tabled = named(0, named.First('.'));
338 TString valued = named(named.First('.')+1, named.Length());
339
340 TString query;
341 query = Form("select %s, %s, %s, Sequences.fSequenceFirst ", valued.Data(), named2.Data(), valuev.Data());
342 query += Form("from %s left join %s ", tabled.Data(), tablev.Data());
343 query += Form("on %s.%s=%s.%s ", tabled.Data(), join.Data(), tablev.Data(), join.Data());
344
345 const Bool_t interval = !fRequestFrom.IsNull() && !fRequestTo.IsNull();
346
347 if (!fDataSet && !interval && tabled=="Star")
348 {
349 if (!query.Contains("Star.fSequenceFirst"))
350 query += "left join Star on Sequences.fSequenceFirst=Star.fSequenceFirst ";
351 query += "where Star.fEffOnTime>300 ";
352 }
353
354 if (interval)
355 {
356 query += query.Contains(" where ") ? "and " : "where ";
357 query += Form("fRunStart between '%s' and '%s' ",
358 fRequestFrom.Data(), fRequestTo.Data());
359 }
360
361 query += "order by fRunStart";
362
363 TSQLResult *res = fServer.Query(query);
364 if (!res)
365 {
366 cout << "ERROR - Query failed: " << query << endl;
367 return kFALSE;
368 }
369
370 if (max>min)
371 PlotTable(*res, namev, min, max, resolution);
372 else
373 PlotTable(*res, namev, fPlotMin, fPlotMax, resolution);
374
375
376 delete res;
377 return kTRUE;
378 }
379};
380
381void plotall(MPlot &plot)
382{
383 //inner camera
384 //from calib*.root
385 plot.SetDescription("Conversion Factor inner Camera;C_{I} [phe/fadc cnts]", "ConvI");
386 plot.Plot("Calibration.fConvFactorInner", 0, 0.5, 0.002);
387 plot.SetDescription("Mean Arrival Time inner Camera;T_{I} [sl]", "ArrTmI");
388 plot.Plot("Calibration.fArrTimeMeanInner", 0, 9.0, 0.1);
389 plot.SetDescription("RMS Arrival Time inner Camera;\\sigma_{T,I} [sl]", "RmsArrTmI");
390 plot.Plot("Calibration.fArrTimeRmsInner", 0, 0.8, 0.01);
391 plot.SetDescription("Number of unsuitable pixels inner Camera;N{I}", "UnsuitI");
392 plot.Plot("Calibration.fUnsuitableInner", 0, 25, 1);
393
394 //from signal*.root
395 plot.SetDescription("Mean Pedestal RMS inner Camera;\\sigma_{P,I} [phe]", "PedRmsI");
396 plot.Plot("Calibration.fMeanPedRmsInner", 0, 3.5, 0.05);
397 plot.SetDescription("Mean Signal inner Camera;S_{I} [phe]", "SignalI");
398 plot.Plot("Calibration.fMeanSignalInner", 0, 7.0, 0.05);
399 plot.SetDescription("Mean PulsePosCheck (falling edge) inner camera;T [sl]", "ChkPos");
400 plot.Plot("Calibration.fPulsePosCheckMean", 1, 15.0, 0.1);
401 plot.SetDescription("Rms PulsePosCheck (falling edge) inner camera;T [sl]", "ChkRms");
402 plot.Plot("Calibration.fPulsePosCheckRms", 0, 5.0, 0.1);
403 plot.SetDescription("Mean calibrated PulsePos;T", "PulPos");
404 plot.Plot("Calibration.fPulsePosMean", 1, 15.0, 0.1);
405 plot.SetDescription("Rms calibrated PulsePos;T", "PulRms");
406 plot.Plot("Calibration.fPulsePosRms", 0, 2.0, 0.02);
407
408 plot.SetDescription("Hi-/Lo-Gain offset;", "PulOff");
409 plot.Plot("Calibration.fPulsePosOffMed", -0.33, 0.33, 0.01);
410 plot.SetDescription("Hi-/Lo-Gain ratio;", "HiLoRatio");
411 plot.Plot("Calibration.fHiLoGainRatioMed", 10, 12.5, 0.05);
412
413 //from star*.root
414 //muon
415 plot.SetDescription("Point Spred Function;PSF [mm]");
416 plot.Plot("Star.fPSF", 0, 30, 0.5);
417 plot.SetDescription("Muon Calibration Ratio Data/MC;r [1]", "MuonCal");
418 plot.Plot("Star.fRatio", 0, 200, 0.5);
419 plot.SetDescription("Muon Rate after Muon Cuts;R [Hz]");
420 plot.Plot("Star.fMuonRate", 0, 2.0, 0.05);
421 //quality
422 plot.SetDescription("Camera Inhomogeneity;\\sigma [%]", "Inhom");
423 plot.Plot("Star.fInhomogeneity", 0, 100, 1);
424 //imgpar
425 plot.SetDescription("Mean Number of Islands after cleaning;N [#]", "NumIsl");
426 plot.Plot("Star.fMeanNumberIslands", 0.5, 4.5, 0.01);
427 plot.SetDescription("Measures effective on time;T_{eff} [s]", "EffOn");
428 plot.Plot("Star.fEffOnTime", 0, 10000, 150);
429 plot.SetDescription("Relative effective on time;T_{eff}/T_{obs} [ratio]", "RelTime");
430 plot.Plot("Star.fEffOnTime/Sequences.fRunTime", 0.006, 1.506, 0.01);
431 plot.SetDescription("Datarate [Hz]", "Rate");
432 plot.Plot("Star.fDataRate", 0, 600, 10);
433 plot.SetDescription("Maximum Humidity [%]", "Hum");
434 plot.Plot("Star.fMaxHumidity", 0, 100, 1);
435
436 //muon
437 //plot.SetDescription("Number of Muons after Muon Cuts;N [#]");
438 //plot.Plot("Star.fMuonNumber", 0, 10000, 100);
439
440 // starguider
441 plot.SetDescription("Median No. Stars recognized by the starguider;N_{0}", "StarsMed");
442 plot.Plot("Star.fNumStarsMed", 0, 100, 1);
443 plot.SetDescription("RMS No. Stars recognized by the starguider;\\sigma_{N_{0}}", "StarsRMS");
444 plot.Plot("Star.fNumStarsRMS", 0, 25, 1);
445 plot.SetDescription("Median No. Stars correlated by the starguider;N", "CorMed");
446 plot.Plot("Star.fNumStarsCorMed", 0, 100, 1);
447 plot.SetDescription("RMS No. Stars correlated by the starguider;\\sigma_{N}", "CorRMS");
448 plot.Plot("Star.fNumStarsCorRMS", 0, 25, 1);
449 plot.SetDescription("Relative number of correlated stars;N/N_{0} [%]", "StarsRel");
450 plot.Plot("Star.fNumStarsCorMed/Star.fNumStarsMed*100", 0, 100, 10);
451 plot.SetDescription("Median skbrightess measured by the starguider;B [au]", "BrightMed");
452 plot.Plot("Star.fBrightnessMed", 0, 111, 1);
453 plot.SetDescription("RMS skybrightess measured by the starguider;\\sigma_{B} [au]", "BrightRMS");
454 plot.Plot("Star.fBrightnessRMS", 0, 64, 1);
455
456 //outer camera
457 //from calib*.root
458 plot.SetDescription("Conversion Factor outer Camera;C_{O} [phe/fadc cnts]", "ConvO");
459 plot.Plot("Calibration.fConvFactorOuter", 0, 2.0, 0.01);
460 plot.SetDescription("Mean Arrival Time outer Camera;T_{O} [sl]", "ArrTmO");
461 plot.Plot("Calibration.fArrTimeMeanOuter", 0, 8.5, 0.1);
462 plot.SetDescription("RMS Arrival Time outer Camera;\\sigma_{T,O} [sl]", "RmsArrTmO");
463 plot.Plot("Calibration.fArrTimeRmsOuter", 0, 1.0, 0.01);
464 plot.SetDescription("Number of unsuitable pixels outer Camera;N{O}", "UnsuitO");
465 plot.Plot("Calibration.fUnsuitableOuter", 0, 25, 1);
466 //from signal*.root
467 plot.SetDescription("Mean Pedestal RMS outer Camera;\\sigma_{P,O} [phe]", "PedRmsO");
468 plot.Plot("Calibration.fMeanPedRmsOuter", 0, 4.0, 0.05);
469 plot.SetDescription("Mean Signal outer Camera;S_{O} [phe]", "SignalO");
470 plot.Plot("Calibration.fMeanSignalOuter", 0, 4.0, 0.05);
471}
472
473int plotdb(TString from, TString to, const char *dataset=0)
474{
475 TEnv env("sql.rc");
476
477 MSQLServer serv(env);
478 if (!serv.IsConnected())
479 {
480 cout << "ERROR - Connection to database failed." << endl;
481 return 0;
482 }
483
484 cout << "plotdb" << endl;
485 cout << "------" << endl;
486 cout << endl;
487 cout << "Connected to " << serv.GetName() << endl;
488 cout << endl;
489
490 MStatusDisplay *d = new MStatusDisplay;
491 d->SetWindowName(serv.GetName());
492 d->SetTitle(serv.GetName());
493
494 MPlot plot(serv);
495 plot.SetDataSet(dataset);
496 plot.SetDisplay(d);
497 plot.SetRequestRange(from, to);
498 plotall(plot);
499 d->SaveAsRoot("plotdb.root");
500 d->SaveAsPS("plotdb.ps");
501
502 return 1;
503}
504
505int plotdb(const char *ds)
506{
507 TEnv env("sql.rc");
508
509 MSQLServer serv(env);
510 if (!serv.IsConnected())
511 {
512 cout << "ERROR - Connection to database failed." << endl;
513 return 0;
514 }
515
516 cout << "plotdb" << endl;
517 cout << "------" << endl;
518 cout << endl;
519 cout << "Connected to " << serv.GetName() << endl;
520 cout << endl;
521
522 MStatusDisplay *d = new MStatusDisplay;
523 d->SetWindowName(serv.GetName());
524 d->SetTitle(serv.GetName());
525
526 MPlot plot(serv);
527 plot.SetDataSet(ds);
528 plot.SetDisplay(d);
529 plot.SetRequestRange("", "");
530 plotall(plot);
531 d->SaveAsRoot("plotdb.root");
532 d->SaveAsPS("plotdb.ps");
533
534 return 1;
535}
536
537int plotdb(Int_t period, const char *dataset="")
538{
539 TEnv env("sql.rc");
540
541 MSQLServer serv(env);
542 if (!serv.IsConnected())
543 {
544 cout << "ERROR - Connection to database failed." << endl;
545 return 0;
546 }
547
548 cout << "plotdb" << endl;
549 cout << "------" << endl;
550 cout << endl;
551 cout << "Connected to " << serv.GetName() << endl;
552 cout << endl;
553
554 MStatusDisplay *d = new MStatusDisplay;
555 d->SetWindowName(serv.GetName());
556 d->SetTitle(serv.GetName());
557
558 MPlot plot(serv);
559 plot.SetDataSet(dataset);
560 plot.SetDisplay(d);
561 plot.SetRequestPeriod(period);
562 plotall(plot);
563 d->SaveAsRoot("plotdb.root");
564 d->SaveAsPS("plotdb.ps");
565
566 return 1;
567}
568
569int plotdb()
570{
571 return plotdb("", "");
572}
Note: See TracBrowser for help on using the repository browser.