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

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