source: trunk/Mars/fact/plots/DataEfficiency.C@ 18204

Last change on this file since 18204 was 17541, checked in by tbretz, 11 years ago
Fixed case of query and output path.
File size: 8.9 KB
Line 
1#include <iostream>
2#include <iomanip>
3#include <fstream>
4
5#include <TCanvas.h>
6#include <TSQLRow.h>
7#include <TSQLResult.h>
8#include <TEnv.h>
9#include <TH2F.h>
10#include <TPad.h>
11
12#include "MSQLMagic.h"
13#include "MStatusDisplay.h"
14#include "MTime.h"
15
16using namespace std;
17
18int DataEfficiency()
19{
20 MSQLServer serv("/home/fact/fast_setup_for_automatic_analysis/Mars.von.Thomas.2012.06.22/sql.rc");
21 if (!serv.IsConnected())
22 {
23 cout << "ERROR - Connection to database failed." << endl;
24 return 0;
25 }
26
27 // get first night from DB
28 TString query="SELECT Min(fNight) from ObservationTimes";
29 TSQLResult *res0 = serv.Query(query);
30 if (!res0)
31 return kFALSE;
32 TSQLRow *row0=res0->Next();
33 Int_t nightmin=atoi((*row0)[0]);
34 delete res0;
35
36 query="SELECT Min(fRunStart), Max(fRunStop) from RunInfo";
37 res0 = serv.Query(query);
38 if (!res0)
39 return kFALSE;
40 row0=res0->Next();
41 float start=MTime((*row0)[0]).GetAxisTime();
42 float stop=MTime((*row0)[1]).GetAxisTime();
43 delete res0;
44
45 cout << start << " " << stop << endl;
46
47 query="SELECT fNight, ";
48 //time from start to end of astronomical twilight
49 query+=" Time_to_sec(Timediff(fStopObservation, fStartObservation)) as astrotime, ";
50 //time from start of first to end of last run
51 query+=" Time_to_sec(Timediff(Max(fRunStop),Min(fRunStart))) as obstime, ";
52 // sum of durations of all runs with duration shorter than 30 min
53 query+=" Sum(if (Timediff(fRunStop,fRunStart)<'00:30:00', Time_to_sec(Timediff(fRunStop,fRunStart)), 0)) as runtime, ";
54 // sum of durations of all data runs with duration shorter than 30 min
55 query+=" Sum(if (Timediff(fRunStop,fRunStart)<'00:30:00' and fRunTypeKey=1, Time_to_sec(Timediff(fRunStop,fRunStart)), 0)) as runtime2, ";
56 //ratio all runs / runtime
57 query+=" Sum(Time_to_sec(if (Timediff(fRunStop,fRunStart)<'00:30:00', Time_to_sec(Timediff(fRunStop,fRunStart)), 0)))/Time_to_sec(Timediff(Max(fRunStop),Min(fRunStart))) as ratio, ";
58 //ratio all runs / astronomical twilight time
59 query+=" Sum(Time_to_sec(if (Timediff(fRunStop,fRunStart)<'00:30:00', Time_to_sec(Timediff(fRunStop,fRunStart)), 0)))/Time_to_sec(Timediff(fStopObservation, fStartObservation)) as ratio2, ";
60 //ratio data runs / runtime
61 query+=" Sum(Time_to_sec(if (Timediff(fRunStop,fRunStart)<'00:30:00' and fRunTypeKey=1, Time_to_sec(Timediff(fRunStop,fRunStart)), 0)))/Time_to_sec(Timediff(Max(fRunStop),Min(fRunStart))) as ratio3, ";
62 //ratio data runs / astronomical twilight time
63 query+=" Sum(Time_to_sec(if (Timediff(fRunStop,fRunStart)<'00:30:00' and fRunTypeKey=1, Time_to_sec(Timediff(fRunStop,fRunStart)), 0)))/Time_to_sec(Timediff(fStopObservation, fStartObservation)) as ratio4, ";
64 // time first to last run / range astronomical twilight
65 query+=" Time_to_sec(Timediff(Max(fRunStop),Min(fRunStart)))/Time_to_sec(Timediff(fStopObservation, fStartObservation)) as ratio5 ";
66 query+=" FROM ObservationTimes LEFT JOIN RunInfo USING(fNight) ";
67 //query+=" WHERE not isnull(fRunStart) and not isnull(fRunStop) group by fNight having not isnull(obstime) order by fNight; ";
68 query+=" group by fNight order by fNight; ";
69
70 cout << "Q: " << query << endl;
71 TSQLResult *res = serv.Query(query);
72 if (!res)
73 return kFALSE;
74
75 Int_t numrows=res->GetRowCount();
76
77 TH1F ratio("ratio","sum of time of data runs / time from start to stop of astronomical twilight", 21, 0.025, 1.025);
78 TH1F ratio2("ratio2","sum of time of data runs / time from beginning of first to end of last run", 21, 0.025, 1.025);
79 TH1F ratio3("ratio3","time from beginning of first to end of last run / time from start to stop of astronomical twilight", 21, 0.025, 1.025);
80
81 Float_t axis = MTime(nightmin/10000, (nightmin/100)%100, nightmin%100).GetAxisTime();
82 Float_t days = numrows*24*3600;
83
84 TH1F time1("time1","hours astronomical twilight", numrows, axis, axis+days);
85 TH1F time2("time2","hours from first to last run", numrows, axis, axis+days);
86 TH1F time3("time3","hours all runs", numrows, axis, axis+days);
87 TH1F time4("time4","hours all data runs", numrows, axis, axis+days);
88
89 TH1F eff2("eff2","hours from first to last run", numrows, axis, axis+days);
90 TH1F eff3("eff3","hours all runs", numrows, axis, axis+days);
91 TH1F eff4("eff4","hours all data runs", numrows, axis, axis+days);
92
93 //time1.SetRangeUser(start, stop);
94 //eff2.SetRangeUser(start, stop);
95
96
97 Int_t counter=0;
98 Int_t counter2=0;
99 TSQLRow *row=0;
100 while ((row=res->Next()))
101 {
102 counter++;
103 //0: night
104 //1: astronomical twilight range
105 //2: time when data was taken
106 //3: runtime all runs
107 //4: runtime data runs
108 //5: ratio runtime all runs / runtime
109 //6: ratio runtime all runs / astronomical twilight range
110 //7: ratio runtime data runs / runtime
111 //8: ratio runtime data runs / astronomical twilight range
112 if ((*row)[7])
113 ratio2.Fill(atof((*row)[7]));
114 if ((*row)[8])
115 ratio.Fill(atof((*row)[8]));
116 if ((*row)[9])
117 ratio3.Fill(atof((*row)[9]));
118 //if (atof((*row)[2])/60./60.>12)
119 //{
120 // cout << (*row)[0] << " " << atof((*row)[2])/60./60. << endl;
121 // counter2++;
122 //}
123 //cout << "c: " << counter << " night: " << (*row)[0] << " " << atof((*row)[1])/60./60.<< " " << atof((*row)[2])/60./60.<< " " << atof((*row)[3])/60./60.<< " " << atof((*row)[4])/60./60. << endl;
124 if (!(*row)[1])
125 continue;
126
127 float twilight = atof((*row)[1]);
128
129 time1.SetBinContent(counter, twilight/60./60.);
130 if ((*row)[2])
131 {
132 time2.SetBinContent(counter, atof((*row)[2])/60./60.);
133 eff2.SetBinContent(counter, atof((*row)[2])/twilight);
134 }
135 if ((*row)[3])
136 {
137 time3.SetBinContent(counter, atof((*row)[3])/60./60.);
138 eff3.SetBinContent(counter, atof((*row)[3])/twilight);
139 }
140 if ((*row)[4])
141 {
142 time4.SetBinContent(counter, atof((*row)[4])/60./60.);
143 eff4.SetBinContent(counter, atof((*row)[4])/twilight);
144 }
145 }
146 cout << "For " << counter2 << " nights the time from beginning of first to the end of the last run was > 12 hours." << endl;
147
148 delete res;
149
150 MStatusDisplay *d = new MStatusDisplay;
151
152 TCanvas &c1 = d->AddTab("Ratio Hist", "Ratio Hist");
153 c1.Divide(1,3);
154 c1.cd(1);
155 ratio.DrawCopy("hist");
156 c1.cd(2);
157 ratio2.DrawCopy("hist");
158 c1.cd(3);
159 ratio3.DrawCopy("hist");
160
161 TCanvas &c2 = d->AddTab("Times");
162 time1.GetXaxis()->SetTimeDisplay(true);
163 time1.GetXaxis()->SetTimeFormat("%m/%y %F1995-01-01 00:00:00 GMT");
164 time1.GetXaxis()->SetLabelSize(0.045);
165 time1.GetYaxis()->SetLabelSize(0.045);
166 time1.GetXaxis()->CenterTitle();
167 time1.GetYaxis()->CenterTitle();
168 time1.SetXTitle("Date");
169 time1.SetYTitle("Observation time / hours");
170 time1.SetFillColor(kBlue);
171 time1.SetTitle("Observation Times");
172 time1.SetMinimum(0);
173 time1.SetStats(kFALSE);
174 time1.DrawCopy("hist");
175 time2.SetFillColor(kGreen);
176 time2.DrawCopy("histsame");
177 time3.SetFillColor(kYellow);
178 time3.DrawCopy("histsame");
179 time4.SetFillColor(kRed+2);
180 time4.DrawCopy("histsame");
181
182 TCanvas &c3 = d->AddTab("Times2");
183 time1.SetFillColor(kBlue);
184 time1.SetTitle("Observation Times");
185 time1.SetStats(kFALSE);
186 time1.DrawCopy("hist");
187 time3.SetFillColor(kYellow);
188 time3.DrawCopy("histsame");
189 time4.SetFillColor(kRed+2);
190 time4.DrawCopy("histsame");
191
192 TCanvas &c4 = d->AddTab("Eff");
193 c4.SetGridy();
194 eff2.GetXaxis()->SetTimeDisplay(true);
195 eff2.GetXaxis()->SetTimeFormat("%m/%y %F1995-01-01 00:00:00 GMT");
196 eff2.GetXaxis()->SetLabelSize(0.045);
197 eff2.GetYaxis()->SetLabelSize(0.045);
198 eff2.GetXaxis()->CenterTitle();
199 eff2.GetYaxis()->CenterTitle();
200 eff2.SetXTitle("Date");
201 eff2.SetYTitle("Efficiency");
202 eff2.SetTitle("Observation efficiency");
203 eff2.SetMinimum(0);
204 eff2.SetMaximum(1);
205 eff2.SetStats(kFALSE);
206 eff2.SetLineColor(kGreen);
207 eff2.SetFillColor(kGreen);
208 eff2.DrawCopy("hist");
209 eff3.SetFillColor(kYellow);
210 eff3.SetLineColor(kYellow);
211 eff3.DrawCopy("histsame");
212 eff4.SetFillColor(kRed+2);
213 eff4.SetLineColor(kRed+2);
214 eff4.DrawCopy("histsame");
215
216 TCanvas &c5 = d->AddTab("Eff2", "Eff2");
217 c5.SetGridy();
218 eff3.GetXaxis()->SetTimeDisplay(true);
219 eff3.GetXaxis()->SetTimeFormat("%m/%y %F1995-01-01 00:00:00 GMT");
220 eff3.GetXaxis()->SetLabelSize(0.045);
221 eff3.GetYaxis()->SetLabelSize(0.045);
222 eff3.GetXaxis()->CenterTitle();
223 eff3.GetYaxis()->CenterTitle();
224 eff3.SetXTitle("Date");
225 eff3.SetYTitle("Efficiency");
226 eff3.SetTitle("Observation efficiency");
227 eff3.SetMinimum(0);
228 eff3.SetMaximum(1);
229 eff3.SetStats(kFALSE);
230 eff3.DrawCopy("hist");
231 eff4.DrawCopy("histsame");
232
233 d->SaveAs("DataEfficiency.root");
234
235 return 0;
236}
Note: See TracBrowser for help on using the repository browser.