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

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