source: trunk/MagicSoft/Mars/datacenter/macros/plotstat.C@ 7542

Last change on this file since 7542 was 7542, checked in by tbretz, 19 years ago
*** empty log message ***
File size: 15.3 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, 02/2006 <mailto:tbretz@astro.uni-wuerzburg.de>
19! Author(s): Daniela Dorner, 02/2006 <mailto:dorner@astro.uni-wuerzburg.de>
20!
21! Copyright: MAGIC Software Development, 2000-2006
22!
23!
24\* ======================================================================== */
25
26/////////////////////////////////////////////////////////////////////////////
27//
28// plotstat.C
29// ==========
30//
31// This macro is used to plot processing statistics from the db.
32//
33// Normally all period are processed. If you want to restric processing to
34// less periods, use:
35// > root plotstat.C+(20, -1)
36// means that all periods since 20 are taken into account. The two numbers
37// denote the first and last period taken into account, -1 means
38// open start or open end.
39//
40// Make sure, that database and password are corretly set in a resource
41// file called sql.rc and the resource file is found.
42//
43/////////////////////////////////////////////////////////////////////////////
44#include <iostream>
45#include <iomanip>
46
47#include <TH1.h>
48#include <TEnv.h>
49#include <TPad.h>
50#include <TLine.h>
51#include <TText.h>
52#include <TCanvas.h>
53#include <TLegend.h>
54#include <TPaveText.h>
55#include <TEllipse.h>
56#include <TSQLRow.h>
57#include <TSQLResult.h>
58
59#include "MH.h"
60#include "MTime.h"
61#include "MBinning.h"
62#include "MSQLServer.h"
63#include "MStatusDisplay.h"
64
65using namespace std;
66
67Double_t GetTime(MSQLServer &serv, TString query, TString from="", TString to="")
68{
69 if (!from.IsNull())
70 {
71 if (!query.Contains("where", TString::kIgnoreCase))
72 query += " where ";
73 else
74 query += " and ";
75
76 query += " fRunStart>'";
77 query += from;
78 query += "' and fRunStart<'";
79 query += to;
80 query += "'";
81 }
82
83 TSQLResult *res = serv.Query(query);
84 if (!res)
85 {
86 cout << "ERROR - Query failed: " << query << endl;
87 return -1;
88 }
89
90 TSQLRow *row=res->Next();
91 if (!row)
92 {
93 cout << "ERROR - Query " << query << " empty." << endl;
94 delete res;
95 return -1;
96 }
97
98 const char *time = (*row)[0];
99
100 delete res;
101 return time ? atof(time) : 0;
102}
103
104void DrawYears()
105{
106 UInt_t year = 2004;
107 Int_t period = 0;
108
109 MTime past, from, now(-1);
110 past.Set(2004, 1, 1, 13, 1);
111
112 TLine l;
113 l.SetLineStyle(kDotted);
114 l.SetLineColor(kWhite);
115 while (past<now)
116 {
117 if (period!=past.GetMagicPeriod())
118 {
119 period = past.GetMagicPeriod();
120 from = past;
121 }
122
123 if (past.Year()!=year)
124 {
125 Double_t dx = (past.GetMjd()-from.GetMjd())/28;
126 l.DrawLine(past.GetMagicPeriod()+dx, 0,
127 past.GetMagicPeriod()+dx, gPad->GetUymax());
128 year = past.Year();
129 }
130 past.SetMjd(past.GetMjd()+1);
131 }
132}
133
134void DrawCake(TH1F *h, Int_t p0=-1, Int_t p1=9999)
135{
136 gPad->Range(-1, -0.9, 1, 1.1);
137
138 gPad->SetFillColor(kWhite);
139 gPad->SetBorderMode(0);
140 gPad->SetFrameBorderMode(0);
141
142 const Double_t r1 = 0.8;
143 const Double_t r2 = 0.59;
144
145 const Double_t tot0 = h[0].Integral(p0, p1);
146 const Double_t tot1 = h[1].Integral(p0, p1);
147 const Double_t tot2 = h[2].Integral(p0, p1);
148 const Double_t tot3 = h[3].Integral(p0, p1);
149 const Double_t tot4 = h[4].Integral(p0, p1);
150 const Double_t tot5 = h[5].Integral(p0, p1);
151 const Double_t tot6 = h[6].Integral(p0, p1);
152 const Double_t tot7 = h[7].Integral(p0, p1);
153
154 TString title = Form("Total = %.1fh (excl=%.1fh)", tot1, tot1-tot2);
155
156 if (p0>0 && p1<9000 && p0==p1)
157 title.Prepend(Form("P%d: ", TMath::Nint(h[0].GetBinCenter(p0))));
158
159 TPaveText *pave = new TPaveText(-0.99, 0.92, 0.99, 1.09);
160 pave->SetBorderSize(1);
161 pave->AddText(title)->SetTextAlign(22);
162 pave->SetBit(kCanDelete);
163 pave->Draw();
164
165 if (tot1<0.1)
166 return;
167
168 TEllipse e;
169 e.SetFillColor(15);
170 //e.SetLineColor(15);
171 e.DrawEllipse(0, 0, r1*1.1, r2*1.1, 90, tot0/tot1*360+90, 0);
172
173 // 0 : hollow
174 // 1001 : Solid
175 // 2001 : hatch style
176 // 3000+pattern_number (see below)
177 // 4000 :the window is transparent.
178 // 4000 to 4100 the window is 100% transparent to 100% opaque
179
180 e.SetLineColor(10);//17);
181 e.SetFillColor(10);//17);
182 e.DrawEllipse(0, 0, r1, r2, 0, 360, 0);
183 e.SetLineColor(kBlack);
184
185 e.SetFillColor(kBlack);
186 e.DrawEllipse(0, 0, r1, r2, 90, tot2/tot1*360+90, 0);
187 //e.SetLineColor(kBlue);
188 e.SetFillColor(kBlue);
189 e.DrawEllipse(0, 0, r1, r2, 90, tot3/tot1*360+90, 0);
190 //e.SetLineColor(kRed);
191 e.SetFillColor(kRed);
192 e.DrawEllipse(0, 0, r1, r2, 90, tot4/tot1*360+90, 0);
193 //e.SetLineColor(kGreen);
194 e.SetFillColor(kGreen);
195 e.DrawEllipse(0, 0, r1, r2, 90, tot5/tot1*360+90, 0);
196 //e.SetLineColor(kRed+100);
197 e.SetFillColor(kRed+100);
198 e.DrawEllipse(0, 0, r1, r2, 90, tot6/tot1*360+90, 0);
199 //e.SetLineColor(kGreen+100);
200 e.SetFillColor(kGreen+100);
201 e.DrawEllipse(0, 0, r1, r2, 90, tot7/tot1*360+90, 0);
202
203 TText txt;
204 txt.SetTextSize(0.08);
205
206 txt.SetTextAlign(32);
207
208 txt.SetTextColor(kBlack);
209 txt.DrawText(0.99, 0.65, Form("%.1f%%", (tot2-tot3)/tot1*100));
210 txt.SetTextColor(kBlue);
211 txt.DrawText(0.99, -0.58, Form("%.1f%%", (tot3-tot4)/tot1*100));
212 txt.SetTextColor(kRed+100);
213 txt.DrawText(-0.35, -0.70, Form("%.1f%%", (tot6-tot7)/tot1*100));
214
215 txt.SetTextAlign(12);
216
217 txt.SetTextColor(kBlack);
218 txt.DrawText(0, 0.77, Form("%.1f%%", (tot1-tot2)/tot1*100));
219 txt.SetTextColor(15);
220 txt.DrawText(-0.99, 0.65, Form("%.1f%%", tot0/tot1*100));
221 txt.SetTextColor(kGreen+100);
222 txt.DrawText(-0.99, -0.58, Form("%.1f%%", tot7/tot1*100));
223 txt.SetTextColor(kRed);
224 txt.DrawText(0.35, -0.70, Form("%.1f%%", (tot4-tot5)/tot1*100));
225
226 txt.SetTextAlign(22);
227
228 txt.SetTextColor(kGreen);
229 txt.DrawText(0, -0.79, Form("%.1f%%", (tot5-tot6)/tot1*100));
230}
231
232void DrawLegend(TH1F *h)
233{
234 TLegend leg(0.01, 0.12, 0.99, 0.98, "Data Statistics");
235 leg.SetBorderSize(1);
236 leg.SetTextSize(0.1);
237
238 TH1 *hc[8];
239
240 for (int i=0; i<8; i++)
241 {
242 hc[i] = (TH1*)h[i].Clone();
243 hc[i]->SetBit(kCanDelete);
244 hc[i]->SetDirectory(0);
245 }
246
247 leg.AddEntry(hc[0], "Files available", "L");
248 leg.AddEntry(hc[1], "Excluded data", "F");
249 leg.AddEntry(hc[2], "Not sequenced", "F");
250 leg.AddEntry(hc[3], "Callisto not done", "F");
251 leg.AddEntry(hc[4], "Callisto failed", "F");
252 leg.AddEntry(hc[5], "Star not done", "F");
253 leg.AddEntry(hc[6], "Star failed", "F");
254 leg.AddEntry(hc[7], "Star OK", "F");
255
256 gROOT->SetSelectedPad(0);
257 leg.DrawClone()->SetBit(kCanDelete);;
258}
259
260Bool_t plot(MStatusDisplay &d, MSQLServer &serv, Int_t first, Int_t last)
261{
262 TString query[8];
263
264 // 0: All data for which are files available
265 query[0] = "select ";
266 query[0] += "SUM(if(TIME_TO_SEC(fRunStop)-TIME_TO_SEC(fRunStart)<0, ";
267 query[0] += "TIME_TO_SEC(fRunStop)-TIME_TO_SEC(fRunStart)+24*60*60, ";
268 query[0] += "TIME_TO_SEC(fRunStop)-TIME_TO_SEC(fRunStart)))/3600 ";
269 query[0] += "from RunData left join RunProcessStatus on RunData.fRunNumber=RunProcessStatus.fRunNumber ";
270 query[0] += "where fRunTypeKey=2 and ";
271 query[0] += " not ISNULL(fRawFileAvail)";
272
273 // 1: All data
274 query[1] = "select ";
275 query[1] += "SUM(if(TIME_TO_SEC(fRunStop)-TIME_TO_SEC(fRunStart)<0, ";
276 query[1] += "TIME_TO_SEC(fRunStop)-TIME_TO_SEC(fRunStart)+24*60*60, ";
277 query[1] += "TIME_TO_SEC(fRunStop)-TIME_TO_SEC(fRunStart)))/3600 ";
278 query[1] += "from RunData where fRunTypeKEY=2";
279
280 // 2: All data which is not excluded
281 query[2] = "select ";
282 query[2] += "SUM(if(TIME_TO_SEC(fRunStop)-TIME_TO_SEC(fRunStart)<0, ";
283 query[2] += "TIME_TO_SEC(fRunStop)-TIME_TO_SEC(fRunStart)+24*60*60, ";
284 query[2] += "TIME_TO_SEC(fRunStop)-TIME_TO_SEC(fRunStart)))/3600 ";
285 query[2] += "from RunData where fRunTypeKEY=2 and fExcludedFDAKEY=1";
286
287 // 3: All sequences
288 query[3] = "select SUM(fRunTime)/3600 from Sequences";
289
290 // 4: All sequences with callisto failed
291 query[4] = "select SUM(fRunTime)/3600 from Sequences left join SequenceProcessStatus on ";
292 query[4] += "Sequences.fSequenceFirst=SequenceProcessStatus.fSequenceFirst where ";
293 query[4] += "ISNULL(fCallisto) and not ISNULL(fFailedTime) and not ISNULL(fAllFilesAvail)";
294
295 // 5: All sequences with callisto=OK
296 query[5] = "select SUM(fRunTime)/3600 from Sequences left join SequenceProcessStatus on ";
297 query[5] += "Sequences.fSequenceFirst=SequenceProcessStatus.fSequenceFirst where not ISNULL(fCallisto)";
298
299 // 6: All sequences with star failed
300 query[6] = "select SUM(fRunTime)/3600 from Sequences left join SequenceProcessStatus on ";
301 query[6] += "Sequences.fSequenceFirst=SequenceProcessStatus.fSequenceFirst where ";
302 query[6] += "ISNULL(fStar) and not ISNULL(fFailedTime) and not ISNULL(fCallisto)";
303
304 // 7: All sequences with star=OK
305 query[7] = "select SUM(fRunTime)/3600 from Sequences left join SequenceProcessStatus on ";
306 query[7] += "Sequences.fSequenceFirst=SequenceProcessStatus.fSequenceFirst where not ISNULL(fStar)";
307
308 // 0: All data
309 // 1: All data which is not excluded
310 // 2: All data for which are files available
311 // 3: All sequences
312 // 4: All sequences with callisto=not done
313 // 5: All sequences with callisto not done and failed
314 // 6: All sequences with star=not done
315 // 7: All sequences with star not done and failed
316
317 MTime now(-1);
318 MTime past;
319 past.Set(2004, 1, 1, 13, 1);
320
321 if (first<0)
322 first=14;
323 if (last<0)
324 last=now.GetMagicPeriod();
325
326 TH1F h[8];
327
328 MBinning bins(last-first+1, first-0.5, last+0.5);
329 for (int i=0; i<8; i++)
330 {
331 bins.Apply(h[i]);
332 h[i].SetName(Form("H%d", i));
333 h[i].SetDirectory(0);
334 }
335
336 Int_t period = 0;
337 MTime from;
338
339 while (past<now)
340 {
341 if (past.GetMagicPeriod()!=period)
342 {
343 period = past.GetMagicPeriod();
344
345 if (period>=first && period<=last)
346 {
347 TString a = from.GetSqlDateTime();
348 TString b = past.GetSqlDateTime();
349
350 for (int i=0; i<8; i++)
351 h[i].Fill(period, GetTime(serv, query[i], a, b));
352 }
353
354 from = past;
355
356 }
357 past.SetMjd(past.GetMjd()+1);
358 }
359
360 for (int i=0; i<h[0].GetNbinsX(); i++)
361 h[0].SetBinError(i+1, 0.001);
362
363
364 h[4].Add(&h[5]);
365 h[6].Add(&h[7]);
366
367 TCanvas &c1 = d.AddTab("Hist");
368 c1.SetFillColor(kWhite);
369 c1.Divide(2,2);
370
371 c1.cd(1);
372
373 gPad->SetBorderMode(0);
374 gPad->SetFrameBorderMode(0);
375 gPad->SetFillColor(kWhite);
376 gPad->SetRightMargin(0.01);
377 gPad->SetTopMargin(0.02);
378 gPad->SetLeftMargin(0.09);
379 gPad->SetBottomMargin(0.12);
380 gPad->SetGridy();
381 gPad->SetPad(0, 0.5, 0.75, 1.0);
382
383 h[1].SetBit(TH1::kNoStats);
384 // h[0].GetXaxis()->SetRangeUser(13.5, period+0.5);
385 h[1].GetXaxis()->CenterTitle();
386 h[1].GetXaxis()->SetTitleSize(0.06);
387 h[1].GetYaxis()->SetTitleSize(0.06);
388 h[1].GetYaxis()->SetTitleOffset(0.7);
389 h[1].GetXaxis()->SetLabelSize(0.06);
390 h[1].GetYaxis()->SetLabelSize(0.06);
391 h[1].GetXaxis()->SetNdivisions(550);
392 h[1].SetYTitle("Time [h]");
393 h[1].SetFillColor(kWhite);
394 h[1].SetLineColor(kBlack);
395 h[1].DrawCopy("");
396
397 h[2].SetLineColor(kBlack);
398 h[2].SetFillColor(kBlack);
399 h[2].DrawCopy("Bsame");
400
401 h[3].SetLineColor(kBlue);
402 h[3].SetFillColor(kBlue);
403 h[3].DrawCopy("Bsame");
404
405 h[4].SetLineColor(kRed);
406 h[4].SetFillColor(kRed);
407 h[4].DrawCopy("Bsame");
408
409 h[5].SetLineColor(kGreen);
410 h[5].SetFillColor(kGreen);
411 h[5].DrawCopy("Bsame");
412
413 h[6].SetLineColor(kRed+100);
414 h[6].SetFillColor(kRed+100);
415 h[6].DrawCopy("Bsame");
416
417 h[7].SetLineColor(kGreen+100);
418 h[7].SetFillColor(kGreen+100);
419 h[7].DrawCopy("Bsame");
420
421 h[0].SetMarkerSize(0);
422 h[0].SetMarkerColor(15);
423 h[0].SetLineWidth(3);
424 h[0].SetLineColor(15);
425 h[0].DrawCopy("EPsame");
426
427 gPad->Update();
428 DrawYears();
429
430 c1.cd(4);
431
432 gPad->SetBorderMode(0);
433 gPad->SetFrameBorderMode(0);
434 gPad->SetPad(0.75, 0, 1.0, 0.5);
435
436 DrawCake(h);
437
438 TCanvas &cx = d.AddTab("All");
439 cx.SetBorderMode(0);
440 cx.SetFrameBorderMode(0);
441 cx.SetFillColor(kWhite);
442 cx.Divide(8,4);
443 cx.cd(1);
444 DrawLegend(h);
445
446 for (int i=0; i<h[0].GetNbinsX(); i++)
447 {
448 TCanvas &c = d.AddTab(Form("P%d", TMath::Nint(h[0].GetBinCenter(i+1))));
449
450 c.SetBorderMode(0);
451 c.SetFrameBorderMode(0);
452 c.SetPad(0.25, 0, 0.75, 1.0);
453 c.SetFillColor(kWhite);
454
455 DrawCake(h, i+1, i+1);
456
457 if (i<4*8)
458 {
459 cx.cd(i+2);
460 DrawCake(h, i+1, i+1);
461 }
462 }
463
464 c1.cd(2);
465
466 gPad->SetBorderMode(0);
467 gPad->SetFrameBorderMode(0);
468 gPad->SetRightMargin(0.01);
469 gPad->SetTopMargin(0.02);
470 gPad->SetLeftMargin(0.09);
471 gPad->SetBottomMargin(0.12);
472 gPad->SetPad(0, 0, 0.75, 0.5);
473 gPad->SetGridy();
474
475 h[1].Scale(0.01);
476 h[0].Divide(&h[1]);
477 h[2].Divide(&h[1]);
478 h[3].Divide(&h[1]);
479 h[4].Divide(&h[1]);
480 h[5].Divide(&h[1]);
481 h[6].Divide(&h[1]);
482 h[7].Divide(&h[1]);
483 h[1].Divide(&h[1]);
484 h[1].Scale(100);
485
486 for (int i=0; i<h[0].GetNbinsX(); i++)
487 h[0].SetBinError(i+1, 0.001);
488
489 h[1].GetXaxis()->SetNdivisions(550);
490 h[1].SetYTitle("%");
491 h[1].SetXTitle("Period");
492 h[1].GetYaxis()->SetRangeUser(0, 100);
493
494 h[1].DrawCopy();
495 h[2].DrawCopy("same");
496 h[3].DrawCopy("same");
497 h[4].DrawCopy("same");
498 h[5].DrawCopy("same");
499 h[6].DrawCopy("same");
500 h[7].DrawCopy("same");
501 h[0].DrawCopy("same");
502/*
503 TLine l;
504 l.SetLineColor(kWhite);
505 l.SetLineWidth(1);
506 l.SetLineStyle(kDotted);
507 for (int i=10; i<95; i+=10)
508 l.DrawLine(13.5, i, period+0.5, i);
509 l.SetLineStyle(kSolid);
510 l.DrawLine(13.5, 50, period+0.5, 50);
511 */
512 gPad->Update();
513 DrawYears();
514
515 c1.cd(3);
516
517 gPad->SetBorderMode(0);
518 gPad->SetFrameBorderMode(0);
519 gPad->SetPad(0.75, 0.5, 1.0, 1.0);
520
521 DrawLegend(h);
522
523 return kTRUE;
524}
525
526int plotstat(Int_t first=-1, Int_t last=-1)
527{
528 TEnv env("sql.rc");
529
530 MSQLServer serv(env);
531 if (!serv.IsConnected())
532 {
533 cout << "ERROR - Connection to database failed." << endl;
534 return 0;
535 }
536
537 cout << "plotstat" << endl;
538 cout << "--------" << endl;
539 cout << endl;
540 cout << "Connected to " << serv.GetName() << endl;
541 cout << endl;
542
543 MStatusDisplay *d = new MStatusDisplay;
544 d->SetWindowName(serv.GetName());
545 d->SetTitle(serv.GetName());
546
547 plot(*d, serv, first, last);
548
549 //d->SaveAsRoot("plotstat.root");
550 //d->SaveAsPS("plotstat.ps");
551
552 return 1;
553}
Note: See TracBrowser for help on using the repository browser.