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

Last change on this file since 7097 was 7090, checked in by Daniela Dorner, 21 years ago
*** empty log message ***
File size: 10.9 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-2005
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 <TStyle.h>
62#include <TGraph.h>
63#include <TCanvas.h>
64#include <TSQLRow.h>
65#include <TSQLResult.h>
66
67#include "MTime.h"
68#include "MAstro.h"
69#include "MSQLServer.h"
70#include "MStatusDisplay.h"
71
72class MPlot : public MParContainer
73{
74private:
75 MSQLServer &fServer;
76
77 TString fRequestFrom;
78 TString fRequestTo;
79 Int_t fRequestPeriod;
80
81 Float_t fPlotMin;
82 Float_t fPlotMax;
83
84 Float_t fHistMin;
85 Float_t fHistMax;
86
87 TString fDescription;
88 TString fNameTab;
89
90 void PlotTable(TSQLResult &res, TString name, Float_t fmin, Float_t fmax, Float_t resolution)
91 {
92 gStyle->SetOptStat(111111);
93
94 TSQLRow *row;
95
96 TGraph g;
97 g.SetNameTitle(name, Form("%s vs Time", name.Data()));
98 g.SetMarkerStyle(kFullDotMedium);
99 if (fmax>fmin)
100 {
101 g.SetMinimum(fmin);
102 g.SetMaximum(fmax);
103 }
104
105 Int_t first = -1;
106 Int_t last = -1;
107
108 while ((row=res.Next()))
109 {
110 const char *date = (*row)[0];
111 const char *val = (*row)[1];
112 if (!date || !val)
113 continue;
114
115 MTime t(date);
116 if (!t.SetSqlDateTime(date))
117 continue;
118
119 if (fRequestPeriod>0 && MAstro::GetMagicPeriod(t.GetMjd())!=fRequestPeriod)
120 continue;
121
122 if (first<0)
123 first = TMath::Nint(TMath::Floor(t.GetMjd()));
124 last = TMath::Nint(TMath::Ceil(t.GetMjd()));
125
126 Float_t value = atof(val);
127 g.SetPoint(g.GetN(), t.GetAxisTime(), value);
128 }
129
130 gROOT->SetSelectedPad(0);
131
132 TString title = fNameTab.IsNull() ? name(name.First('.')+2, name.Length()) : fNameTab;
133 TCanvas &c = fDisplay ? fDisplay->AddTab(title) : *new TCanvas;
134 c.Divide(1,2);
135
136 TVirtualPad *pad = gPad;
137 pad->cd(2);
138 gPad->SetBorderMode(0);
139 gPad->SetGridy();
140
141 TH1 *h = g.GetHistogram();
142
143 h->SetXTitle("Time");
144 h->SetYTitle(name);
145 h->GetXaxis()->SetTimeDisplay(1);
146
147 g.DrawClone("AP");
148
149 TLine l;
150 TText t;
151 Int_t num=0;
152 l.SetLineStyle(kDotted);
153 l.SetLineColor(kBlue);
154 t.SetTextColor(kBlue);
155 l.SetLineWidth(1);
156 t.SetTextSize(h->GetXaxis()->GetLabelSize());
157 t.SetTextAlign(21);
158 Int_t p0 = MAstro::GetMagicPeriod(first);
159 for (Int_t p = first; p<last; p++)
160 {
161 Int_t p1 = MAstro::GetMagicPeriod(p);
162 if (p1!=p0)
163 {
164 l.DrawLine(MTime(p).GetAxisTime(), h->GetMinimum(), MTime(p).GetAxisTime(), h->GetMaximum());
165 t.DrawText(MTime(p+15).GetAxisTime(), h->GetMaximum(), Form("%d", p1));
166 num++;
167 }
168 p0 = p1;
169 }
170 if (num<4)
171 gPad->SetGridx();
172
173 const Double_t min = fHistMin>fHistMax ? h->GetMinimum()-resolution/2 : fHistMin;
174 const Double_t max = fHistMin>fHistMax ? h->GetMaximum()+resolution/2 : fHistMax;
175
176 pad->cd(1);
177 gPad->SetBorderMode(0);
178 gPad->SetGridx();
179 gPad->SetGridy();
180
181 const Int_t n = resolution>0 ? TMath::Nint((max-min)/resolution) : 50;
182
183 TH1F hist("Hist", Form("Distribution of %s", fDescription.IsNull() ? name.Data() : fDescription.Data()), n, min, max);
184 hist.SetDirectory(0);
185
186 for (int i=0; i<g.GetN(); i++)
187 hist.Fill(g.GetY()[i]);
188
189 if (fDescription.IsNull())
190 hist.SetXTitle(name);
191 hist.SetYTitle("Counts");
192
193 hist.DrawCopy("");
194 }
195
196public:
197 MPlot(MSQLServer &server) : fServer(server), fRequestPeriod(-1),
198 fPlotMin(0), fPlotMax(-1), fHistMin(0), fHistMax(-1)
199 {
200 }
201 void SetPlotRange(Float_t min, Float_t max, Int_t n=5)
202 { fPlotMin = min; fPlotMax = max; }
203 void SetHistRange(Float_t min, Float_t max)
204 { fHistMin = min; fHistMax = max; }
205 void SetRequestRange(const char *from="", const char *to="")
206 { fRequestFrom = from; fRequestTo = to; }
207 void SetRequestPeriod(Int_t n=-1)
208 { fRequestPeriod = n; }
209 void SetDescription(const char *d, const char *t=0) { fDescription = d; fNameTab = t; }
210
211 Bool_t Plot(const char *value, Float_t min=0, Float_t max=-1, Float_t resolution=0)
212 {
213 TString named = "Sequences.fRunStart";
214 TString namev = value;
215 TString join = "fSequenceFirst";
216
217 TString tablev = namev(0, namev.First('.'));
218 TString valuev = namev(namev.First('.')+1, namev.Length());
219
220 TString tabled = named(0, named.First('.'));
221 TString valued = named(named.First('.')+1, named.Length());
222
223 TString query;
224 query = Form("select %s, %s ", valued.Data(), valuev.Data());
225 query += Form("from %s left join %s ", tabled.Data(), tablev.Data());
226 query += Form("on %s.%s=%s.%s ", tabled.Data(), join.Data(), tablev.Data(), join.Data());
227
228 if (!fRequestFrom.IsNull() && !fRequestTo.IsNull())
229 query += Form("where fRunStart between '%s' and '%s' ",
230 fRequestFrom.Data(), fRequestTo.Data());
231
232 query += "order by fRunStart";
233
234 TSQLResult *res = fServer.Query(query);
235 if (!res)
236 {
237 cout << "ERROR - Query failed: " << query << endl;
238 return kFALSE;
239 }
240
241 if (max>min)
242 PlotTable(*res, namev, min, max, resolution);
243 else
244 PlotTable(*res, namev, fPlotMin, fPlotMax, resolution);
245
246
247 delete res;
248 return kTRUE;
249 }
250};
251
252void plotall(MPlot &plot)
253{
254 //inner camera
255 //from calib*.root
256 plot.SetDescription("Conversion Factor inner Camera;C_{I} [phe/fadc cnts]", "ConvI");
257 plot.Plot("Calibration.fConvFactorInner", 0, 0.5, 0.01);
258 plot.SetDescription("Mean Arrival Time inner Camera;T_{I} [sl]", "ArrTmI");
259 plot.Plot("Calibration.fArrTimeMeanInner", 0, 9.0, 0.1);
260 plot.SetDescription("RMS Arrival Time inner Camera;\\sigma_{T,I} [sl]", "RmsArrTmI");
261 plot.Plot("Calibration.fArrTimeRmsInner", 0, 2.5, 0.1);
262 //from signal*.root
263 plot.SetDescription("Mean Pedestal RMS inner Camera;\\sigma_{P,I} [phe]", "PedRmsI");
264 plot.Plot("Calibration.fMeanPedRmsInner", 0, 3.5, 0.1);
265 //from star*.root
266 //muon
267 plot.SetDescription("Point Spred Function;PSF [mm]");
268 plot.Plot("Star.fPSF", 0, 40, 0.5);
269 plot.SetDescription("Muon Calibration Ratio Data/MC;r [1]", "MuonCal");
270 plot.Plot("Star.fRatio", 0, 200, 0.5);
271 plot.SetDescription("Muon Rate after Muon Cuts;R [Hz]");
272 plot.Plot("Star.fMuonRate", 0, 2.0, 0.05);
273 //imgpar
274 plot.SetDescription("Mean Number of Islands after cleaning;N [#]", "NumIsl");
275 plot.Plot("Star.fMeanNumberIslands", 0.5, 4.5, 0.1);
276 plot.SetDescription("Measures effective on time;T_{eff} [s]", "EffOn");
277 plot.Plot("Star.fEffOnTime", 0, 10000, 60);
278 //muon
279 plot.SetDescription("Number of Muons after Muon Cuts;N [#]");
280 plot.Plot("Star.fMuonNumber", 0, 10000, 100);
281 //outer camera
282 //from calib*.root
283 plot.SetDescription("Conversion Factor outer Camera;C_{O} [phe/fadc cnts]", "ConvO");
284 plot.Plot("Calibration.fConvFactorOuter", 0, 2.0, 0.01);
285 plot.SetDescription("Mean Arrival Time outer Camera;T_{O} [sl]", "ArrTmO");
286 plot.Plot("Calibration.fArrTimeMeanOuter", 0, 8.5, 0.1);
287 plot.SetDescription("RMS Arrival Time outer Camera;\\sigma_{T,O} [sl]", "RmsArrTmO");
288 plot.Plot("Calibration.fArrTimeRmsOuter", 0, 2.5, 0.1);
289 //from signal*.root
290 plot.SetDescription("Mean Pedestal RMS outer Camera;\\sigma_{P,O} [phe]", "PedRmsO");
291 plot.Plot("Calibration.fMeanPedRmsOuter", 0, 4.0, 0.1);
292}
293
294void plotdb(TString from="", TString to="")
295{
296 TEnv env("sql.rc");
297
298 MSQLServer serv(env);
299 if (!serv.IsConnected())
300 {
301 cout << "ERROR - Connection to database failed." << endl;
302 return;
303 }
304
305 cout << "plotdb" << endl;
306 cout << "------" << endl;
307 cout << endl;
308 cout << "Connected to " << serv.GetName() << endl;
309 cout << endl;
310
311 serv.SelectDataBase("MyMagic");
312
313 MStatusDisplay *d = new MStatusDisplay;
314
315 MPlot plot(serv);
316 plot.SetDisplay(d);
317 plot.SetRequestRange(from, to);
318 plotall(plot);
319}
320
321void plotdb(Int_t period)
322{
323 TEnv env("sql.rc");
324
325 MSQLServer serv(env);
326 if (!serv.IsConnected())
327 {
328 cout << "ERROR - Connection to database failed." << endl;
329 return;
330 }
331
332 cout << "plotdb" << endl;
333 cout << "------" << endl;
334 cout << endl;
335 cout << "Connected to " << serv.GetName() << endl;
336 cout << endl;
337
338 serv.SelectDataBase("MyMagic");
339
340 MStatusDisplay *d = new MStatusDisplay;
341
342 MPlot plot(serv);
343 plot.SetDisplay(d);
344 plot.SetRequestPeriod(period);
345 plotall(plot);
346}
Note: See TracBrowser for help on using the repository browser.