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

Last change on this file since 7756 was 7756, checked in by tbretz, 18 years ago
*** empty log message ***
File size: 18.4 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 aree highlighted (blue:on, red:off)
52//
53// Make sure, that database and password are corretly set in a resource
54// file called sql.rc and the resource file is found.
55//
56// To draw sequences belonging to a DataSet in colors for highliting
57// change the defintition of 'const char *dataset=0;' in the code to something
58// like 'const char *dataset="dataset.txt";' to load your favourite dataset.
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 <TGraph.h>
72#include <TCanvas.h>
73#include <TSQLRow.h>
74#include <TSQLResult.h>
75
76#include "MTime.h"
77#include "MAstro.h"
78#include "MDataSet.h"
79#include "MSQLServer.h"
80#include "MStatusDisplay.h"
81
82// Replace this line
83const char *dataset=0;
84// by
85// const char *dataset = "/magic/datasets/00000/dataset00000003.txt";
86// to mark the sequences of your favourite dataset.
87
88class MPlot : public MParContainer
89{
90private:
91 MSQLServer &fServer;
92
93 MDataSet *fDataSet;
94
95 TString fRequestFrom;
96 TString fRequestTo;
97 Int_t fRequestPeriod;
98
99 Float_t fPlotMin;
100 Float_t fPlotMax;
101
102 Float_t fHistMin;
103 Float_t fHistMax;
104
105 TString fDescription;
106 TString fNameTab;
107
108 void PlotTable(TSQLResult &res, TString name, Float_t fmin, Float_t fmax, Float_t resolution)
109 {
110 gStyle->SetOptStat(111111);
111
112 TSQLRow *row;
113
114 TGraph gt;
115 gt.SetNameTitle(name, Form("%s vs Time", name.Data()));
116 gt.SetMarkerStyle(kFullDotMedium);
117
118 TGraph gz;
119 gz.SetNameTitle(name, Form("%s vs <Zd>", name.Data()));
120 gz.SetMarkerStyle(kFullDotMedium);
121
122 TGraph gt0, gt1;
123 gt0.SetMarkerColor(kRed);
124 gt1.SetMarkerColor(kBlue);
125 gt0.SetMarkerStyle(kFullDotLarge);
126 gt1.SetMarkerStyle(kFullDotLarge);
127
128 TGraph gz0, gz1;
129 gz0.SetMarkerColor(kRed);
130 gz1.SetMarkerColor(kBlue);
131 gz0.SetMarkerStyle(kFullDotLarge);
132 gz1.SetMarkerStyle(kFullDotLarge);
133
134 if (fmax>fmin)
135 {
136 gt.SetMinimum(fmin);
137 gt.SetMaximum(fmax);
138 gz.SetMinimum(fmin);
139 gz.SetMaximum(fmax);
140 }
141
142 Int_t first = -1;
143 Int_t last = -1;
144
145 while ((row=res.Next()))
146 {
147 const char *date = (*row)[0];
148 const char *zd = (*row)[1];
149 const char *val = (*row)[2];
150 const char *snum = (*row)[3];
151 if (!date || !val || !zd || !snum)
152 continue;
153
154 MTime t(date);
155 if (!t.SetSqlDateTime(date))
156 continue;
157
158 if (fRequestPeriod>0 && MAstro::GetMagicPeriod(t.GetMjd())!=fRequestPeriod)
159 continue;
160
161 if (first<0)
162 first = TMath::Nint(TMath::Floor(t.GetMjd()));
163 last = TMath::Nint(TMath::Ceil(t.GetMjd()));
164
165 UInt_t seq = atoi(snum);
166
167 Float_t value = atof(val);
168 Float_t zenith = atof(zd);
169
170 if (fDataSet)
171 {
172 if (fDataSet->HasOnSequence(seq))
173 {
174 gt1.SetPoint(gt1.GetN(), t.GetAxisTime(), value);
175 gz1.SetPoint(gz1.GetN(), zenith, value);
176 }
177
178 if (fDataSet->HasOffSequence(seq))
179 {
180 gt0.SetPoint(gt0.GetN(), t.GetAxisTime(), value);
181 gz0.SetPoint(gz0.GetN(), zenith, value);
182 }
183 }
184
185 gt.SetPoint(gt.GetN(), t.GetAxisTime(), value);
186 gz.SetPoint(gz.GetN(), zenith, value);
187 }
188
189 gROOT->SetSelectedPad(0);
190
191 TString title = fNameTab.IsNull() ? name(name.First('.')+2, name.Length()) : fNameTab;
192 TCanvas &c = fDisplay ? fDisplay->AddTab(title) : *new TCanvas;
193 c.SetFillColor(kWhite);
194 c.SetBorderMode(0);
195 c.Divide(1,2);
196
197 cerr << setprecision(4) << setw(10) << title << ": ";
198 cerr << setw(8) << gt.GetMean(2) << "+-" << setw(8) << gt.GetRMS(2) << " ";
199 if (gt0.GetN()>0 || gt1.GetN()>0)
200 {
201 cerr << setw(8) << gt1.GetMean(2) << "+-" << setw(8) << gt1.GetRMS(2) << " ";
202 cerr << setw(8) << gt0.GetMean(2) << "+-" << setw(8) << gt0.GetRMS(2);
203 }
204 cerr << endl;
205
206 TVirtualPad *pad = gPad;
207 pad->cd(2);
208 gPad->SetBorderMode(0);
209 gPad->SetFrameBorderMode(0);
210 gPad->SetGridy();
211
212 gPad->SetLeftMargin(0.06);
213 gPad->SetRightMargin(0.06);
214 gPad->SetBottomMargin(0.08);
215
216 TH1 *h = gt.GetHistogram();
217
218 h->SetXTitle("Time");
219 h->SetYTitle(name);
220 h->GetXaxis()->SetTimeDisplay(1);
221 h->GetYaxis()->SetTitleOffset(0.8);
222 h->GetXaxis()->SetTitleOffset(1.0);
223 h->GetXaxis()->SetLabelOffset(0.01);
224
225 gt.DrawClone("AP");
226 if (gt0.GetN()>0)
227 gt0.DrawClone("P");
228 if (gt1.GetN()>0)
229 gt1.DrawClone("P");
230
231 TLine l;
232 TText t;
233 Int_t num=0;
234 l.SetLineStyle(kDotted);
235 l.SetLineColor(kBlue);
236 t.SetTextColor(kBlue);
237 l.SetLineWidth(1);
238 t.SetTextSize(h->GetXaxis()->GetLabelSize());
239 t.SetTextAlign(21);
240 Int_t p0 = MAstro::GetMagicPeriod(first);
241 for (Int_t p = first; p<last; p++)
242 {
243 Int_t p1 = MAstro::GetMagicPeriod(p);
244 if (p1!=p0)
245 {
246 l.DrawLine(MTime(p).GetAxisTime(), h->GetMinimum(), MTime(p).GetAxisTime(), h->GetMaximum());
247 t.DrawText(MTime(p+15).GetAxisTime(), h->GetMaximum(), Form("%d", p1));
248 num++;
249 }
250 p0 = p1;
251 }
252 if (num<4)
253 gPad->SetGridx();
254
255 const Double_t min = fHistMin>fHistMax ? h->GetMinimum()-resolution/2 : fHistMin;
256 const Double_t max = fHistMin>fHistMax ? h->GetMaximum()+resolution/2 : fHistMax;
257
258 // Use this to save the pad with the time development to a file
259 //gPad->SaveAs(Form("plotdb-%s.eps", title.Data()));
260
261 pad->cd(1);
262 gPad->SetBorderMode(0);
263 gPad->SetFrameBorderMode(0);
264 gPad->Divide(2,1);
265
266 TVirtualPad *pad2 = gPad;
267 pad2->cd(1);
268 gPad->SetBorderMode(0);
269 gPad->SetFrameBorderMode(0);
270 gPad->SetGridx();
271 gPad->SetGridy();
272
273 const Int_t n = resolution>0 ? TMath::Nint((max-min)/resolution) : 50;
274
275 TH1F hist("Hist", Form("Distribution of %s", fDescription.IsNull() ? name.Data() : fDescription.Data()), n, min, max);
276 hist.SetDirectory(0);
277
278 for (int i=0; i<gt.GetN(); i++)
279 hist.Fill(gt.GetY()[i]);
280
281 if (fDescription.IsNull())
282 hist.SetXTitle(name);
283 hist.SetYTitle("Counts");
284
285 hist.DrawCopy("");
286
287 pad2->cd(2);
288 gPad->SetBorderMode(0);
289 gPad->SetFrameBorderMode(0);
290 gPad->SetGridy();
291
292 TH1 *h2 = gz.GetHistogram();
293
294 h2->SetXTitle("Zd");
295 h2->SetYTitle(name);
296
297 gz.DrawClone("AP");
298 if (gz0.GetN()>0)
299 gz0.DrawClone("P");
300 if (gz1.GetN()>0)
301 gz1.DrawClone("P");
302 }
303
304public:
305 MPlot(MSQLServer &server) : fServer(server), fDataSet(NULL),
306 fRequestPeriod(-1), fPlotMin(0), fPlotMax(-1), fHistMin(0), fHistMax(-1)
307 {
308 }
309 ~MPlot()
310 {
311 if (fDataSet)
312 delete fDataSet;
313 }
314 void SetDataSet(const TString filename)
315 {
316 if (fDataSet)
317 {
318 delete fDataSet;
319 fDataSet = NULL;
320 }
321 if (!filename.IsNull())
322 fDataSet = new MDataSet(filename);
323 }
324 void SetPlotRange(Float_t min, Float_t max, Int_t n=5)
325 { fPlotMin = min; fPlotMax = max; }
326 void SetHistRange(Float_t min, Float_t max)
327 { fHistMin = min; fHistMax = max; }
328 void SetRequestRange(const char *from="", const char *to="")
329 { fRequestFrom = from; fRequestTo = to; }
330 void SetRequestPeriod(Int_t n=-1)
331 { fRequestPeriod = n; }
332 void SetDescription(const char *d, const char *t=0) { fDescription = d; fNameTab = t; }
333
334 Bool_t Plot(const char *value, Float_t min=0, Float_t max=-1, Float_t resolution=0)
335 {
336 TString named = "Sequences.fRunStart";
337 TString named2 = "(Sequences.fZenithDistanceMin+Sequences.fZenithDistanceMax)/2";
338 TString namev = value;
339 TString join = "fSequenceFirst";
340
341 TString tablev = namev(0, namev.First('.'));
342 TString valuev = namev(namev.First('.')+1, namev.Length());
343
344 TString tabled = named(0, named.First('.'));
345 TString valued = named(named.First('.')+1, named.Length());
346
347 TString query;
348 query = Form("select %s, %s, %s, Sequences.fSequenceFirst ", valued.Data(), named2.Data(), valuev.Data());
349 query += Form("from %s left join %s ", tabled.Data(), tablev.Data());
350 query += Form("on %s.%s=%s.%s ", tabled.Data(), join.Data(), tablev.Data(), join.Data());
351
352 const Bool_t interval = !fRequestFrom.IsNull() && !fRequestTo.IsNull();
353
354 if (!fDataSet && !interval)
355 {
356 if (!query.Contains("Star.fSequenceFirst"))
357 query += "left join Star on Sequences.fSequenceFirst=Star.fSequenceFirst ";
358 query += "where Star.fEffOnTime>300 ";
359 }
360
361 if (interval)
362 {
363 query += query.Contains(" where ") ? "and " : "where ";
364 query += Form("fRunStart between '%s' and '%s' ",
365 fRequestFrom.Data(), fRequestTo.Data());
366 }
367
368 query += "order by fRunStart";
369
370 TSQLResult *res = fServer.Query(query);
371 if (!res)
372 {
373 cout << "ERROR - Query failed: " << query << endl;
374 return kFALSE;
375 }
376
377 if (max>min)
378 PlotTable(*res, namev, min, max, resolution);
379 else
380 PlotTable(*res, namev, fPlotMin, fPlotMax, resolution);
381
382
383 delete res;
384 return kTRUE;
385 }
386};
387
388void plotall(MPlot &plot)
389{
390 //inner camera
391 //from calib*.root
392 plot.SetDescription("Conversion Factor inner Camera;C_{I} [phe/fadc cnts]", "ConvI");
393 plot.Plot("Calibration.fConvFactorInner", 0, 0.5, 0.001);
394 plot.SetDescription("Mean Arrival Time inner Camera;T_{I} [sl]", "ArrTmI");
395 plot.Plot("Calibration.fArrTimeMeanInner", 0, 9.0, 0.1);
396 plot.SetDescription("RMS Arrival Time inner Camera;\\sigma_{T,I} [sl]", "RmsArrTmI");
397 plot.Plot("Calibration.fArrTimeRmsInner", 0, 2.5, 0.1);
398 //from signal*.root
399 plot.SetDescription("Mean Pedestal RMS inner Camera;\\sigma_{P,I} [phe]", "PedRmsI");
400 plot.Plot("Calibration.fMeanPedRmsInner", 0, 3.5, 0.05);
401 plot.SetDescription("Mean Signal inner Camera;S_{I} [phe]", "SignalI");
402 plot.Plot("Calibration.fMeanSignalInner", 0, 7.0, 0.05);
403 plot.SetDescription("Mean PulsePosCheck (falling edge) inner camera;T [sl]", "ChkPos");
404 plot.Plot("Calibration.fPulsePosCheckMean", 1, 15.0, 0.1);
405 plot.SetDescription("Rms PulsePosCheck (falling edge) inner camera;T [sl]", "ChkRms");
406 plot.Plot("Calibration.fPulsePosCheckRms", 0, 5.0, 0.1);
407 plot.SetDescription("Mean calibrated PulsePos;T", "PulPos");
408 plot.Plot("Calibration.fPulsePosMean", 1, 15.0, 0.1);
409 plot.SetDescription("Rms calibrated PulsePos;T", "PulRms");
410 plot.Plot("Calibration.fPulsePosRms", 0, 2.0, 0.1);
411 //from star*.root
412 //muon
413 plot.SetDescription("Point Spred Function;PSF [mm]");
414 plot.Plot("Star.fPSF", 0, 40, 0.5);
415 plot.SetDescription("Muon Calibration Ratio Data/MC;r [1]", "MuonCal");
416 plot.Plot("Star.fRatio", 0, 200, 0.5);
417 plot.SetDescription("Muon Rate after Muon Cuts;R [Hz]");
418 plot.Plot("Star.fMuonRate", 0, 2.0, 0.05);
419 //quality
420 plot.SetDescription("Camera Inhomogeneity;\\sigma [%]", "Inhom");
421 plot.Plot("Star.fInhomogeneity", 0, 100, 1);
422 //imgpar
423 plot.SetDescription("Mean Number of Islands after cleaning;N [#]", "NumIsl");
424 plot.Plot("Star.fMeanNumberIslands", 0.5, 4.5, 0.01);
425 plot.SetDescription("Measures effective on time;T_{eff} [s]", "EffOn");
426 plot.Plot("Star.fEffOnTime", 0, 10000, 150);
427 plot.SetDescription("Relative effective on time;T_{eff}/T_{obs} [ratio]", "RelTime");
428 plot.Plot("Star.fEffOnTime/Sequences.fRunTime", 0, 1.5, 0.01);
429 plot.SetDescription("Datarate [Hz]", "Rate");
430 plot.Plot("Star.fDataRate", 0, 600, 10);
431 plot.SetDescription("Maximum Humidity [%]", "Hum");
432 plot.Plot("Star.fMaxHumidity", 0, 100, 1);
433 //muon
434 plot.SetDescription("Number of Muons after Muon Cuts;N [#]");
435 plot.Plot("Star.fMuonNumber", 0, 10000, 100);
436 //outer camera
437 //from calib*.root
438 plot.SetDescription("Conversion Factor outer Camera;C_{O} [phe/fadc cnts]", "ConvO");
439 plot.Plot("Calibration.fConvFactorOuter", 0, 2.0, 0.01);
440 plot.SetDescription("Mean Arrival Time outer Camera;T_{O} [sl]", "ArrTmO");
441 plot.Plot("Calibration.fArrTimeMeanOuter", 0, 8.5, 0.1);
442 plot.SetDescription("RMS Arrival Time outer Camera;\\sigma_{T,O} [sl]", "RmsArrTmO");
443 plot.Plot("Calibration.fArrTimeRmsOuter", 0, 2.5, 0.1);
444 //from signal*.root
445 plot.SetDescription("Mean Pedestal RMS outer Camera;\\sigma_{P,O} [phe]", "PedRmsO");
446 plot.Plot("Calibration.fMeanPedRmsOuter", 0, 4.0, 0.05);
447 plot.SetDescription("Mean Signal outer Camera;S_{O} [phe]", "SignalO");
448 plot.Plot("Calibration.fMeanSignalOuter", 0, 4.0, 0.05);
449
450 plot.SetDescription("Median No. Stars recognized by the starguider;N", "StarsMed");
451 plot.Plot("Star.fNumStarsMed", 0, 70, 1);
452 plot.SetDescription("RMS No. Stars recognized by the starguider;\\sigma_{N}", "StarsRMS");
453 plot.Plot("Star.fNumStarsRMS", 0, 25, 1);
454 plot.SetDescription("Median No. Stars correlated by the starguider;N", "StarsMed");
455 plot.Plot("Star.fNumCorMed", 0, 70, 1);
456 plot.SetDescription("RMS No. Stars correlated by the starguider;\\sigma_{N}", "StarsRMS");
457 plot.Plot("Star.fNumCorRMS", 0, 25, 1);
458 plot.SetDescription("Median skbrightess measured by the starguider;B [au]", "BrightMed");
459 plot.Plot("Star.fBrightnessMean", 0, 111, 1);
460 plot.SetDescription("RMS skybrightess measured by the starguider;\\sigma_{B} [au]", "BrightRMS");
461 plot.Plot("Star.fBrightnessRMS", 0, 64, 1);
462}
463
464int plotdb(TString from, TString to)
465{
466 TEnv env("sql.rc");
467
468 MSQLServer serv(env);
469 if (!serv.IsConnected())
470 {
471 cout << "ERROR - Connection to database failed." << endl;
472 return 0;
473 }
474
475 cout << "plotdb" << endl;
476 cout << "------" << endl;
477 cout << endl;
478 cout << "Connected to " << serv.GetName() << endl;
479 cout << endl;
480
481 MStatusDisplay *d = new MStatusDisplay;
482 d->SetWindowName(serv.GetName());
483 d->SetTitle(serv.GetName());
484
485 MPlot plot(serv);
486 plot.SetDataSet(dataset);
487 plot.SetDisplay(d);
488 plot.SetRequestRange(from, to);
489 plotall(plot);
490 d->SaveAsRoot("plotdb.root");
491 d->SaveAsPS("plotdb.ps");
492
493 return 1;
494}
495
496int plotdb(const char *ds)
497{
498 TEnv env("sql.rc");
499
500 MSQLServer serv(env);
501 if (!serv.IsConnected())
502 {
503 cout << "ERROR - Connection to database failed." << endl;
504 return 0;
505 }
506
507 cout << "plotdb" << endl;
508 cout << "------" << endl;
509 cout << endl;
510 cout << "Connected to " << serv.GetName() << endl;
511 cout << endl;
512
513 MStatusDisplay *d = new MStatusDisplay;
514 d->SetWindowName(serv.GetName());
515 d->SetTitle(serv.GetName());
516
517 MPlot plot(serv);
518 plot.SetDataSet(ds);
519 plot.SetDisplay(d);
520 plot.SetRequestRange("", "");
521 plotall(plot);
522 d->SaveAsRoot("plotdb.root");
523 d->SaveAsPS("plotdb.ps");
524
525 return 1;
526}
527
528int plotdb(Int_t period)
529{
530 TEnv env("sql.rc");
531
532 MSQLServer serv(env);
533 if (!serv.IsConnected())
534 {
535 cout << "ERROR - Connection to database failed." << endl;
536 return 0;
537 }
538
539 cout << "plotdb" << endl;
540 cout << "------" << endl;
541 cout << endl;
542 cout << "Connected to " << serv.GetName() << endl;
543 cout << endl;
544
545 MStatusDisplay *d = new MStatusDisplay;
546 d->SetWindowName(serv.GetName());
547 d->SetTitle(serv.GetName());
548
549 MPlot plot(serv);
550 plot.SetDataSet(dataset);
551 plot.SetDisplay(d);
552 plot.SetRequestPeriod(period);
553 plotall(plot);
554 d->SaveAsRoot("plotdb.root");
555 d->SaveAsPS("plotdb.ps");
556
557 return 1;
558}
559
560int plotdb()
561{
562 return plotdb("", "");
563}
Note: See TracBrowser for help on using the repository browser.