source: tags/Mars-V0.9.5/datacenter/macros/plotstat.C

Last change on this file was 7558, 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 (1)
340 {
341 if (past.GetMagicPeriod()!=period)
342 {
343 period = past.GetMagicPeriod();
344
345 if (period>first && period-1<=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-1, GetTime(serv, query[i], a, b));
352 }
353
354 if (past>now)
355 break;
356
357 from = past;
358
359 }
360 past.SetMjd(past.GetMjd()+1);
361 }
362
363 for (int i=0; i<h[0].GetNbinsX(); i++)
364 h[0].SetBinError(i+1, 0.001);
365
366
367 h[4].Add(&h[5]);
368 h[6].Add(&h[7]);
369
370 TCanvas &c1 = d.AddTab("Hist");
371 c1.SetFillColor(kWhite);
372 c1.Divide(2,2);
373
374 c1.cd(1);
375
376 gPad->SetBorderMode(0);
377 gPad->SetFrameBorderMode(0);
378 gPad->SetFillColor(kWhite);
379 gPad->SetRightMargin(0.01);
380 gPad->SetTopMargin(0.02);
381 gPad->SetLeftMargin(0.09);
382 gPad->SetBottomMargin(0.12);
383 gPad->SetGridy();
384 gPad->SetPad(0, 0.5, 0.75, 1.0);
385
386 h[1].SetBit(TH1::kNoStats);
387 // h[0].GetXaxis()->SetRangeUser(13.5, period+0.5);
388 h[1].GetXaxis()->CenterTitle();
389 h[1].GetXaxis()->SetTitleSize(0.06);
390 h[1].GetYaxis()->SetTitleSize(0.06);
391 h[1].GetYaxis()->SetTitleOffset(0.7);
392 h[1].GetXaxis()->SetLabelSize(0.06);
393 h[1].GetYaxis()->SetLabelSize(0.06);
394 h[1].GetXaxis()->SetNdivisions(550);
395 h[1].SetYTitle("Time [h]");
396 h[1].SetFillColor(kWhite);
397 h[1].SetLineColor(kBlack);
398 h[1].DrawCopy("");
399
400 h[2].SetLineColor(kBlack);
401 h[2].SetFillColor(kBlack);
402 h[2].DrawCopy("Bsame");
403
404 h[3].SetLineColor(kBlue);
405 h[3].SetFillColor(kBlue);
406 h[3].DrawCopy("Bsame");
407
408 h[4].SetLineColor(kRed);
409 h[4].SetFillColor(kRed);
410 h[4].DrawCopy("Bsame");
411
412 h[5].SetLineColor(kGreen);
413 h[5].SetFillColor(kGreen);
414 h[5].DrawCopy("Bsame");
415
416 h[6].SetLineColor(kRed+100);
417 h[6].SetFillColor(kRed+100);
418 h[6].DrawCopy("Bsame");
419
420 h[7].SetLineColor(kGreen+100);
421 h[7].SetFillColor(kGreen+100);
422 h[7].DrawCopy("Bsame");
423
424 h[0].SetMarkerSize(0);
425 h[0].SetMarkerColor(15);
426 h[0].SetLineWidth(3);
427 h[0].SetLineColor(15);
428 h[0].DrawCopy("EPsame");
429
430 gPad->Update();
431 DrawYears();
432
433 c1.cd(4);
434
435 gPad->SetBorderMode(0);
436 gPad->SetFrameBorderMode(0);
437 gPad->SetPad(0.75, 0, 1.0, 0.5);
438
439 DrawCake(h);
440
441 TCanvas &cx = d.AddTab("All");
442 cx.SetBorderMode(0);
443 cx.SetFrameBorderMode(0);
444 cx.SetFillColor(kWhite);
445 cx.Divide(8,4);
446 cx.cd(1);
447 DrawLegend(h);
448
449 for (int i=0; i<h[0].GetNbinsX(); i++)
450 {
451 TCanvas &c = d.AddTab(Form("P%d", TMath::Nint(h[0].GetBinCenter(i+1))));
452
453 c.SetBorderMode(0);
454 c.SetFrameBorderMode(0);
455 c.SetPad(0.25, 0, 0.75, 1.0);
456 c.SetFillColor(kWhite);
457
458 DrawCake(h, i+1, i+1);
459
460 if (i<4*8)
461 {
462 cx.cd(i+2);
463 DrawCake(h, i+1, i+1);
464 }
465 }
466
467 c1.cd(2);
468
469 gPad->SetBorderMode(0);
470 gPad->SetFrameBorderMode(0);
471 gPad->SetRightMargin(0.01);
472 gPad->SetTopMargin(0.02);
473 gPad->SetLeftMargin(0.09);
474 gPad->SetBottomMargin(0.12);
475 gPad->SetPad(0, 0, 0.75, 0.5);
476 gPad->SetGridy();
477
478 h[1].Scale(0.01);
479 h[0].Divide(&h[1]);
480 h[2].Divide(&h[1]);
481 h[3].Divide(&h[1]);
482 h[4].Divide(&h[1]);
483 h[5].Divide(&h[1]);
484 h[6].Divide(&h[1]);
485 h[7].Divide(&h[1]);
486 h[1].Divide(&h[1]);
487 h[1].Scale(100);
488
489 for (int i=0; i<h[0].GetNbinsX(); i++)
490 h[0].SetBinError(i+1, 0.001);
491
492 h[1].GetXaxis()->SetNdivisions(550);
493 h[1].SetYTitle("%");
494 h[1].SetXTitle("Period");
495 h[1].GetYaxis()->SetRangeUser(0, 100);
496
497 h[1].DrawCopy();
498 h[2].DrawCopy("same");
499 h[3].DrawCopy("same");
500 h[4].DrawCopy("same");
501 h[5].DrawCopy("same");
502 h[6].DrawCopy("same");
503 h[7].DrawCopy("same");
504 h[0].DrawCopy("same");
505/*
506 TLine l;
507 l.SetLineColor(kWhite);
508 l.SetLineWidth(1);
509 l.SetLineStyle(kDotted);
510 for (int i=10; i<95; i+=10)
511 l.DrawLine(13.5, i, period+0.5, i);
512 l.SetLineStyle(kSolid);
513 l.DrawLine(13.5, 50, period+0.5, 50);
514 */
515 gPad->Update();
516 DrawYears();
517
518 c1.cd(3);
519
520 gPad->SetBorderMode(0);
521 gPad->SetFrameBorderMode(0);
522 gPad->SetPad(0.75, 0.5, 1.0, 1.0);
523
524 DrawLegend(h);
525
526 return kTRUE;
527}
528
529int plotstat(Int_t first=-1, Int_t last=-1)
530{
531 TEnv env("sql.rc");
532
533 MSQLServer serv(env);
534 if (!serv.IsConnected())
535 {
536 cout << "ERROR - Connection to database failed." << endl;
537 return 0;
538 }
539
540 cout << "plotstat" << endl;
541 cout << "--------" << endl;
542 cout << endl;
543 cout << "Connected to " << serv.GetName() << endl;
544 cout << endl;
545
546 MStatusDisplay *d = new MStatusDisplay;
547 d->SetWindowName(serv.GetName());
548 d->SetTitle(serv.GetName());
549
550 plot(*d, serv, first, last);
551
552 //d->SaveAsRoot("plotstat.root");
553 //d->SaveAsPS("plotstat.ps");
554
555 return 1;
556}
Note: See TracBrowser for help on using the repository browser.