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

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