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

Last change on this file since 7485 was 7485, checked in by tbretz, 19 years ago
*** empty log message ***
File size: 13.6 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//
50// Make sure, that database and password are corretly set in a resource
51// file called sql.rc and the resource file is found.
52//
53/////////////////////////////////////////////////////////////////////////////
54#include <iostream>
55
56#include <TH1.h>
57#include <TEnv.h>
58#include <TPad.h>
59#include <TLine.h>
60#include <TText.h>
61#include <TFrame.h>
62#include <TStyle.h>
63#include <TGraph.h>
64#include <TCanvas.h>
65#include <TSQLRow.h>
66#include <TSQLResult.h>
67
68#include "MTime.h"
69#include "MAstro.h"
70#include "MSQLServer.h"
71#include "MStatusDisplay.h"
72
73class MPlot : public MParContainer
74{
75private:
76 MSQLServer &fServer;
77
78 TString fRequestFrom;
79 TString fRequestTo;
80 Int_t fRequestPeriod;
81
82 Float_t fPlotMin;
83 Float_t fPlotMax;
84
85 Float_t fHistMin;
86 Float_t fHistMax;
87
88 TString fDescription;
89 TString fNameTab;
90
91 void PlotTable(TSQLResult &res, TString name, Float_t fmin, Float_t fmax, Float_t resolution)
92 {
93 gStyle->SetOptStat(111111);
94
95 TSQLRow *row;
96
97 TGraph g;
98 g.SetNameTitle(name, Form("%s vs Time", name.Data()));
99 g.SetMarkerStyle(kFullDotMedium);
100
101 TGraph g2;
102 g2.SetNameTitle(name, Form("%s vs <Zd>", name.Data()));
103 g2.SetMarkerStyle(kFullDotMedium);
104
105 if (fmax>fmin)
106 {
107 g.SetMinimum(fmin);
108 g.SetMaximum(fmax);
109 g2.SetMinimum(fmin);
110 g2.SetMaximum(fmax);
111 }
112
113 Int_t first = -1;
114 Int_t last = -1;
115
116 while ((row=res.Next()))
117 {
118 const char *date = (*row)[0];
119 const char *zd = (*row)[1];
120 const char *val = (*row)[2];
121 if (!date || !val || !zd)
122 continue;
123
124 MTime t(date);
125 if (!t.SetSqlDateTime(date))
126 continue;
127
128 if (fRequestPeriod>0 && MAstro::GetMagicPeriod(t.GetMjd())!=fRequestPeriod)
129 continue;
130
131 if (first<0)
132 first = TMath::Nint(TMath::Floor(t.GetMjd()));
133 last = TMath::Nint(TMath::Ceil(t.GetMjd()));
134
135 Float_t value = atof(val);
136 Float_t zenith = atof(zd);
137 g.SetPoint(g.GetN(), t.GetAxisTime(), value);
138 g2.SetPoint(g2.GetN(), zenith, value);
139 }
140
141 gROOT->SetSelectedPad(0);
142
143 TString title = fNameTab.IsNull() ? name(name.First('.')+2, name.Length()) : fNameTab;
144 TCanvas &c = fDisplay ? fDisplay->AddTab(title) : *new TCanvas;
145 c.SetFillColor(kWhite);
146 c.SetBorderMode(0);
147 c.Divide(1,2);
148
149 TVirtualPad *pad = gPad;
150 pad->cd(2);
151 gPad->SetBorderMode(0);
152 gPad->SetFrameBorderMode(0);
153 gPad->SetGridy();
154
155 gPad->SetLeftMargin(0.06);
156 gPad->SetRightMargin(0.06);
157 gPad->SetBottomMargin(0.08);
158
159 TH1 *h = g.GetHistogram();
160
161 h->SetXTitle("Time");
162 h->SetYTitle(name);
163 h->GetXaxis()->SetTimeDisplay(1);
164 h->GetYaxis()->SetTitleOffset(0.8);
165 h->GetXaxis()->SetTitleOffset(1.0);
166 h->GetXaxis()->SetLabelOffset(0.01);
167
168 g.DrawClone("AP");
169
170 TLine l;
171 TText t;
172 Int_t num=0;
173 l.SetLineStyle(kDotted);
174 l.SetLineColor(kBlue);
175 t.SetTextColor(kBlue);
176 l.SetLineWidth(1);
177 t.SetTextSize(h->GetXaxis()->GetLabelSize());
178 t.SetTextAlign(21);
179 Int_t p0 = MAstro::GetMagicPeriod(first);
180 for (Int_t p = first; p<last; p++)
181 {
182 Int_t p1 = MAstro::GetMagicPeriod(p);
183 if (p1!=p0)
184 {
185 l.DrawLine(MTime(p).GetAxisTime(), h->GetMinimum(), MTime(p).GetAxisTime(), h->GetMaximum());
186 t.DrawText(MTime(p+15).GetAxisTime(), h->GetMaximum(), Form("%d", p1));
187 num++;
188 }
189 p0 = p1;
190 }
191 if (num<4)
192 gPad->SetGridx();
193
194 const Double_t min = fHistMin>fHistMax ? h->GetMinimum()-resolution/2 : fHistMin;
195 const Double_t max = fHistMin>fHistMax ? h->GetMaximum()+resolution/2 : fHistMax;
196
197 pad->cd(1);
198 gPad->SetBorderMode(0);
199 gPad->SetFrameBorderMode(0);
200 gPad->Divide(2,1);
201
202 TVirtualPad *pad2 = gPad;
203 pad2->cd(1);
204 gPad->SetBorderMode(0);
205 gPad->SetFrameBorderMode(0);
206 gPad->SetGridx();
207 gPad->SetGridy();
208
209 const Int_t n = resolution>0 ? TMath::Nint((max-min)/resolution) : 50;
210
211 TH1F hist("Hist", Form("Distribution of %s", fDescription.IsNull() ? name.Data() : fDescription.Data()), n, min, max);
212 hist.SetDirectory(0);
213
214 for (int i=0; i<g.GetN(); i++)
215 hist.Fill(g.GetY()[i]);
216
217 if (fDescription.IsNull())
218 hist.SetXTitle(name);
219 hist.SetYTitle("Counts");
220
221 hist.DrawCopy("");
222
223 pad2->cd(2);
224 gPad->SetBorderMode(0);
225 gPad->SetFrameBorderMode(0);
226 gPad->SetGridy();
227
228 TH1 *h2 = g2.GetHistogram();
229
230 h2->SetXTitle("Zd");
231 h2->SetYTitle(name);
232
233 g2.DrawClone("AP");
234 }
235
236public:
237 MPlot(MSQLServer &server) : fServer(server), fRequestPeriod(-1),
238 fPlotMin(0), fPlotMax(-1), fHistMin(0), fHistMax(-1)
239 {
240 }
241 void SetPlotRange(Float_t min, Float_t max, Int_t n=5)
242 { fPlotMin = min; fPlotMax = max; }
243 void SetHistRange(Float_t min, Float_t max)
244 { fHistMin = min; fHistMax = max; }
245 void SetRequestRange(const char *from="", const char *to="")
246 { fRequestFrom = from; fRequestTo = to; }
247 void SetRequestPeriod(Int_t n=-1)
248 { fRequestPeriod = n; }
249 void SetDescription(const char *d, const char *t=0) { fDescription = d; fNameTab = t; }
250
251 Bool_t Plot(const char *value, Float_t min=0, Float_t max=-1, Float_t resolution=0)
252 {
253 TString named = "Sequences.fRunStart";
254 TString named2 = "(Sequences.fZenithDistanceMin+Sequences.fZenithDistanceMax)/2";
255 TString namev = value;
256 TString join = "fSequenceFirst";
257
258 TString tablev = namev(0, namev.First('.'));
259 TString valuev = namev(namev.First('.')+1, namev.Length());
260
261 TString tabled = named(0, named.First('.'));
262 TString valued = named(named.First('.')+1, named.Length());
263
264 TString query;
265 query = Form("select %s, %s, %s ", valued.Data(), named2.Data(), valuev.Data());
266 query += Form("from %s left join %s ", tabled.Data(), tablev.Data());
267 query += Form("on %s.%s=%s.%s ", tabled.Data(), join.Data(), tablev.Data(), join.Data());
268
269 if (!fRequestFrom.IsNull() && !fRequestTo.IsNull())
270 query += Form("where fRunStart between '%s' and '%s' ",
271 fRequestFrom.Data(), fRequestTo.Data());
272
273 query += "order by fRunStart";
274
275 //cout << "q: " << query << endl;
276
277 TSQLResult *res = fServer.Query(query);
278 if (!res)
279 {
280 cout << "ERROR - Query failed: " << query << endl;
281 return kFALSE;
282 }
283
284 if (max>min)
285 PlotTable(*res, namev, min, max, resolution);
286 else
287 PlotTable(*res, namev, fPlotMin, fPlotMax, resolution);
288
289
290 delete res;
291 return kTRUE;
292 }
293};
294
295void plotall(MPlot &plot)
296{
297 //inner camera
298 //from calib*.root
299 plot.SetDescription("Conversion Factor inner Camera;C_{I} [phe/fadc cnts]", "ConvI");
300 plot.Plot("Calibration.fConvFactorInner", 0, 0.5, 0.001);
301 plot.SetDescription("Mean Arrival Time inner Camera;T_{I} [sl]", "ArrTmI");
302 plot.Plot("Calibration.fArrTimeMeanInner", 0, 9.0, 0.1);
303 plot.SetDescription("RMS Arrival Time inner Camera;\\sigma_{T,I} [sl]", "RmsArrTmI");
304 plot.Plot("Calibration.fArrTimeRmsInner", 0, 2.5, 0.1);
305 //from signal*.root
306 plot.SetDescription("Mean Pedestal RMS inner Camera;\\sigma_{P,I} [phe]", "PedRmsI");
307 plot.Plot("Calibration.fMeanPedRmsInner", 0, 3.5, 0.05);
308 plot.SetDescription("Mean Signal inner Camera;S_{I} [phe]", "SignalI");
309 plot.Plot("Calibration.fMeanSignalInner", 0, 7.0, 0.05);
310 plot.SetDescription("Mean PulsePosCheck (falling edge) inner camera;T [sl]", "ChkPos");
311 plot.Plot("Calibration.fPulsePosCheckMean", 1, 15.0, 0.1);
312 plot.SetDescription("Rms PulsePosCheck (falling edge) inner camera;T [sl]", "ChkRms");
313 plot.Plot("Calibration.fPulsePosCheckRms", 0, 5.0, 0.1);
314 plot.SetDescription("Mean calibrated PulsePos;T", "PulPos");
315 plot.Plot("Calibration.fPulsePosMean", 1, 15.0, 0.1);
316 plot.SetDescription("Rms calibrated PulsePos;T", "PulRms");
317 plot.Plot("Calibration.fPulsePosRms", 0, 2.0, 0.1);
318 //from star*.root
319 //muon
320 plot.SetDescription("Point Spred Function;PSF [mm]");
321 plot.Plot("Star.fPSF", 0, 40, 0.5);
322 plot.SetDescription("Muon Calibration Ratio Data/MC;r [1]", "MuonCal");
323 plot.Plot("Star.fRatio", 0, 200, 0.5);
324 plot.SetDescription("Muon Rate after Muon Cuts;R [Hz]");
325 plot.Plot("Star.fMuonRate", 0, 2.0, 0.05);
326 //quality
327 plot.SetDescription("Camera Inhomogeneity;\\sigma [%]", "Inhom");
328 plot.Plot("Star.fInhomogeneity", 0, 100, 1);
329 //imgpar
330 plot.SetDescription("Mean Number of Islands after cleaning;N [#]", "NumIsl");
331 plot.Plot("Star.fMeanNumberIslands", 0.5, 4.5, 0.01);
332 plot.SetDescription("Measures effective on time;T_{eff} [s]", "EffOn");
333 plot.Plot("Star.fEffOnTime", 0, 10000, 150);
334 plot.SetDescription("Datarate [Hz]", "Rate");
335 plot.Plot("Star.fDataRate", 0, 600, 10);
336 plot.SetDescription("Maximum Humidity [%]", "Hum");
337 plot.Plot("Star.fMaxHumidity", 0, 100, 1);
338 //muon
339 plot.SetDescription("Number of Muons after Muon Cuts;N [#]");
340 plot.Plot("Star.fMuonNumber", 0, 10000, 100);
341 //outer camera
342 //from calib*.root
343 plot.SetDescription("Conversion Factor outer Camera;C_{O} [phe/fadc cnts]", "ConvO");
344 plot.Plot("Calibration.fConvFactorOuter", 0, 2.0, 0.01);
345 plot.SetDescription("Mean Arrival Time outer Camera;T_{O} [sl]", "ArrTmO");
346 plot.Plot("Calibration.fArrTimeMeanOuter", 0, 8.5, 0.1);
347 plot.SetDescription("RMS Arrival Time outer Camera;\\sigma_{T,O} [sl]", "RmsArrTmO");
348 plot.Plot("Calibration.fArrTimeRmsOuter", 0, 2.5, 0.1);
349 //from signal*.root
350 plot.SetDescription("Mean Pedestal RMS outer Camera;\\sigma_{P,O} [phe]", "PedRmsO");
351 plot.Plot("Calibration.fMeanPedRmsOuter", 0, 4.0, 0.05);
352 plot.SetDescription("Mean Signal outer Camera;S_{O} [phe]", "SignalO");
353 plot.Plot("Calibration.fMeanSignalOuter", 0, 4.0, 0.05);
354}
355
356int plotdb(TString from="", TString to="")
357{
358 TEnv env("sql.rc");
359
360 MSQLServer serv(env);
361 if (!serv.IsConnected())
362 {
363 cout << "ERROR - Connection to database failed." << endl;
364 return 0;
365 }
366
367 cout << "plotdb" << endl;
368 cout << "------" << endl;
369 cout << endl;
370 cout << "Connected to " << serv.GetName() << endl;
371 cout << endl;
372
373 MStatusDisplay *d = new MStatusDisplay;
374 d->SetWindowName(serv.GetName());
375 d->SetTitle(serv.GetName());
376
377 MPlot plot(serv);
378 plot.SetDisplay(d);
379 plot.SetRequestRange(from, to);
380 plotall(plot);
381 d->SaveAsRoot("plotdb.root");
382 d->SaveAsPS("plotdb.ps");
383
384 return 1;
385}
386
387int plotdb(Int_t period)
388{
389 TEnv env("sql.rc");
390
391 MSQLServer serv(env);
392 if (!serv.IsConnected())
393 {
394 cout << "ERROR - Connection to database failed." << endl;
395 return 0;
396 }
397
398 cout << "plotdb" << endl;
399 cout << "------" << endl;
400 cout << endl;
401 cout << "Connected to " << serv.GetName() << endl;
402 cout << endl;
403
404 MStatusDisplay *d = new MStatusDisplay;
405 d->SetWindowName(serv.GetName());
406 d->SetTitle(serv.GetName());
407
408 MPlot plot(serv);
409 plot.SetDisplay(d);
410 plot.SetRequestPeriod(period);
411 plotall(plot);
412 d->SaveAsRoot("plotdb.root");
413 d->SaveAsPS("plotdb.ps");
414
415 return 1;
416}
Note: See TracBrowser for help on using the repository browser.