source: branches/AddingGoogleTestEnvironment/fact/plots/plotratescan.C@ 19779

Last change on this file since 19779 was 15243, checked in by Daniela Dorner, 12 years ago
added (macro to plot ratescan info from db)
File size: 9.9 KB
Line 
1/*void Fit(TGraph &g, double rate_start=3e5, double rate_kink=80, double rate_end=1)
2{
3 double begin = -1;
4 double end = -1;
5
6 double kinkx = -1;
7 double kinky = 0;
8
9 TGraph gl;
10 for (int j=0; j<g.GetN(); j++)
11 {
12 gl.SetPoint(gl.GetN(), g.GetX()[j], log10(g.GetY()[j]));
13 if (g.GetY()[j]<rate_start && begin<0)
14 begin = g.GetX()[j];
15 if (g.GetY()[j]<rate_end && end<0)
16 {
17 end = g.GetX()[j];
18 //if (end<550)
19 // end = 750;
20 }
21 if (g.GetY()[j]<rate_kink && kinkx<0)
22 {
23 kinkx = g.GetX()[j];
24 kinky = g.GetY()[j];
25 }
26 }
27
28 if (end==-1)
29 end = 600;//g.GetX()[g.GetN()-1];
30
31 TF1 func2("f2", "log10("
32 " [1]*exp([2]*(x-[0]))"
33 "+ [1]*exp([3]*(x-[0]))"
34 //"+ [4]*exp([5]*(x-700))"
35 ")", begin, end);
36
37 Double_t v00 = g.Eval(kinkx-150, 0, "S");
38 Double_t v01 = g.Eval(kinkx- 50, 0, "S");
39
40 Double_t v10 = g.Eval(kinkx+100, 0, "S");
41 Double_t v11 = g.Eval(kinkx+200, 0, "S");
42
43 //Double_t v20 = g.Eval(700-50, 0, "S");
44 //Double_t v21 = g.Eval(700, 0, "S");
45 //Double_t v22 = g.Eval(700+50, 0, "S");
46
47 func2.SetParameter(0, kinkx);
48 func2.SetParameter(1, kinky);
49 func2.SetParameter(2, log(v01/v00)/100);
50 func2.SetParameter(3, log(v11/v10)/100);
51
52 func2.FixParameter(4, 0);
53 func2.FixParameter(5, 0);
54 func2.SetLineWidth(1);
55 func2.SetLineColor(kBlue);
56
57 gl.Fit(&func2, "N0", "", begin, end);
58
59 TF1 func("f1",
60 "[0]*exp([1]*(x-[6])) + "
61 "[2]*exp([3]*(x-[6])) + "
62 "[4]*exp([5]*(x-700))", begin, end);
63 func.FixParameter(0, func2.GetParameter(1));
64 func.FixParameter(1, func2.GetParameter(2));
65 func.FixParameter(2, func2.GetParameter(1));
66 func.FixParameter(3, func2.GetParameter(3));
67 func.FixParameter(4, func2.GetParameter(4));
68 func.FixParameter(5, func2.GetParameter(5));
69 func.FixParameter(6, func2.GetParameter(0));
70 func.SetLineWidth(2);
71 func.SetLineColor(kBlue);
72 func.DrawClone("same");
73 func.SetLineWidth(1);
74
75 cout << begin << " -- " << end << " : " << kinkx << endl;
76
77 func.SetLineStyle(kDashed);
78 func.SetRange(0, 1000);
79
80 func.FixParameter(0, 0);
81 func.FixParameter(2, 0);
82 func.FixParameter(4, func2.GetParameter(4));
83 func.DrawClone("same");
84
85 func.FixParameter(0, func2.GetParameter(1));
86 func.FixParameter(2, 0);
87 func.FixParameter(4, 0);
88 func.DrawClone("same");
89
90 func.FixParameter(0, 0);
91 func.FixParameter(2, func2.GetParameter(1));
92 func.FixParameter(4, 0);
93 func.DrawClone("same");
94
95
96 TLatex txt;
97 txt.SetTextColor(kBlue);
98 txt.SetTextAlign(33);
99 txt.DrawLatex(func2.GetParameter(0), func2.GetParameter(1),
100 Form("%.1fHz @ %.0f", func2.GetParameter(1), func2.GetParameter(0)));
101
102 // [1]*exp([2]*(x-[0])) / exp(1) = [1]*exp([3]*(x-[0]))
103 // exp([2]*(x-[0])) / exp(1) = exp([3]*(x-[0]))
104 // exp([2]*(x-[0])) / exp([3]*(x-[0])) = exp(1)
105 // exp([2]*(x-[0]) - [3]*(x-[0])) = exp(1)
106 // [2]*(x-[0]) - [3]*(x-[0]) = 1
107 //
108 // x = 1/([2]-[3]) + [0]
109
110
111 Double_t th = 1. / (func2.GetParameter(3)-func2.GetParameter(2)) + func2.GetParameter(0);
112 Double_t rate = pow(10, func2.Eval(th));
113 Double_t rate_end = pow(10, func2.Eval(end)-0.5);
114
115 txt.SetTextAlign(11);
116 txt.DrawLatex(th+10, rate, Form("%.1fHz @ %.0f", rate, th));
117
118 txt.SetTextAlign(12);
119 txt.DrawLatex(180, 3e6, Form("%.1f", 1./func2.GetParameter(2)));
120 txt.DrawLatex(end, rate_end, Form("%.0f", 1./func2.GetParameter(3)));
121
122 TLine line;
123 line.SetLineStyle(kDashed);
124 line.SetLineColor(kBlue);
125 line.DrawLine(th, 1e-2, th, 1e8);
126}*/
127
128Bool_t plot(MSQLMagic &serv, Long64_t search_id, TGraph &g, TGraph *gb)
129{
130 TString query;
131 query += "SELECT fTimeBegin, fTimeEnd, fVoltageIsOn, fOvervoltage, fCurrentMedMean, fNight ";
132 query += "FROM Ratescan WHERE fRatescanID=";
133 query += search_id;
134
135 TSQLResult *res = serv.Query(query);
136 if (!res)
137 return kFALSE;
138
139 if (res->GetRowCount()!=1)
140 {
141 cout << "ERROR - Row count is not 1." << endl;
142 delete res;
143 return kTRUE;
144 }
145
146 TSQLRow *row = res->Next();
147
148 const char *time_beg = (*row)[0];
149 const char *time_end = (*row)[1];
150 const char *night = (*row)[5];
151
152 int voltage_on = (*row)[2] ? atoi((*row)[2]) : -1;
153 float overvoltage = (*row)[3] ? atof((*row)[3]) : -100;
154 float current = (*row)[4] ? atof((*row)[4]) : -1;
155
156 delete row;
157
158 gROOT->SetSelectedPad(0);
159 TCanvas *c = new TCanvas(time_beg+11, time_beg);
160
161 c->SetGrid();
162 c->SetLogy();
163 c->SetTopMargin(0.01);
164 c->SetRightMargin(0.005);
165
166 TPaveText leg(600, 1e5, 1050, 8e8, "br");
167 leg.SetBorderSize(1);
168 leg.SetFillColor(kWhite);
169 leg.AddText("Ratescan");
170 leg.AddText("");
171 leg.AddText(Form("Begin %s", time_beg));
172 leg.AddText(Form("End %s", time_end));
173 leg.AddText("");
174 if (voltage_on==0)
175 leg.AddText("Voltage off");
176 else
177 {
178 if (current>=0)
179 leg.AddText(Form("Current <I_{med}> = %.1f #muA", current));
180 if (overvoltage>-70)
181 leg.AddText(Form("Voltage #DeltaU = %+.2f V", overvoltage));
182 }
183
184 TH1D h0("frame", "", 1050, 0, 1050);
185 h0.SetDirectory(0);
186 h0.SetStats(kFALSE);
187 h0.SetXTitle("Threshold [dac counts]");
188 h0.SetYTitle("Trigger rate [Hz]");
189 h0.GetXaxis()->SetLabelSize(0.05);
190 h0.GetXaxis()->SetTitleSize(0.05);
191 h0.GetYaxis()->SetLabelSize(0.05);
192 h0.GetYaxis()->SetTitleSize(0.05);
193 h0.SetMaximum(8e8);
194 h0.SetMinimum(0.61);
195 h0.DrawCopy();
196
197 leg.DrawClone();
198
199 for (int i=0; i<40; i++)
200 {
201 gb[i].SetNameTitle(Form("Board%02d", i), "Board trigger rate (time 40)");
202 gb[i].DrawClone("P");
203 }
204
205 g.SetMarkerStyle(kFullDotMedium);
206 g.SetMarkerColor(kBlue);
207 g.SetLineColor(kBlue);
208 g.SetNameTitle("Camera", "Camera trigger rate");
209 g.DrawClone("PL");
210
211 c->Write();
212
213 TString name;
214 name += night;
215 name += "-ratescan ";
216 name += time_beg;
217
218 c->SaveAs(name+".pdf");
219 c->SaveAs(name+".eps");
220 c->SaveAs(name+".png");
221
222 delete c;
223}
224
225Int_t plotid(MSQLMagic &serv, fits &file, const char *_search_id, Int_t row)
226{
227 Long64_t search_id = atol(_search_id);
228
229 cout << " " << search_id << endl;
230
231 UInt_t offset = file.GetUInt("MJDREF");
232
233 bool old = file.HasColumn("Data0");
234
235 Double_t *ptime = file.SetPtrAddress("Time");
236 UInt_t *pid = file.SetPtrAddress(old ? "Data0" : "Id");
237 Float_t *rates = file.SetPtrAddress(old ? "Data5" : "BoardRate");
238 UInt_t *pth = file.SetPtrAddress(old ? "Data1" : "Threshold");
239 Float_t *ptrig = file.SetPtrAddress(old ? "Data4" : "TriggerRate");
240 Float_t *pontime = file.SetPtrAddress(old ? "Data3" : "RelOnTime");
241
242 if (!ptime || !pid || !rates || !pth || !ptrig || !pontime)
243 return -1;
244
245 TGraph g;
246 TGraph gb[40];
247
248 while (file.GetRow(row++))
249 {
250 if (pid[0]<search_id)
251 continue;
252
253 if (pid[0]!=search_id)
254 break;
255
256 g.SetPoint(g.GetN(), pth[0], ptrig[0]/pontime[0]);
257 for (int i=0; i<40; i++)
258 gb[i].SetPoint(gb[i].GetN(), pth[0], rates[i]*40);
259 }
260
261 if (g.GetN()>0)
262 plot(serv, search_id, g, gb);
263
264 return row;
265
266
267}
268
269Bool_t plotratescan(MSQLMagic &serv, const char *_night)
270{
271 Long64_t night = atol(_night);
272
273 TString fname = Form("/fact/aux/%04d/%02d/%02d/%06d.RATE_SCAN_DATA.fits",
274 night/10000, (night/100)%100, night%100, night);
275
276 cout << " " << fname << endl;
277
278 fits file(fname.Data());
279 if (!file)
280 {
281 cout << "ERROR - Cannot access " << fname << ": " << gSystem->GetError() << endl;
282 return kFALSE;
283 }
284
285 TString query;
286 query += "SELECT fRatescanID FROM Ratescan WHERE fNight=";
287 query += night;
288 query += " ORDER BY fRatescanID";
289 /*
290 query += "SELECT fRatescanID, fTimeBeg, fTimeEnd, fVoltageIsOn, ";
291 query += " fOvervoltage, fCurrentMedMean, fRateBegin, fRateEnd, ";
292 query += " fThresholdBegin, fThresholdEnd, fNumPoints ";
293 query += "WHERE fNight=";
294 query += night;
295 */
296 TSQLResult *res = serv.Query(query);
297 if (!res)
298 return kFALSE;
299
300 if (res->GetRowCount()==0)
301 {
302 delete res;
303 return kTRUE;
304 }
305
306 TString oname = Form("%06d-ratescan.root", night);
307
308 cout << " " << oname << '\n' << endl;
309
310 TFile rootfile(oname.Data(), "recreate");
311
312 TSQLRow *row = 0;
313
314 Int_t cnt = 0;
315 while ((row=res->Next()) && cnt>=0)
316 {
317 const char *id = (*row)[0];
318 cnt = plotid(serv, file, id, cnt);
319 delete row;
320 }
321
322 delete res;
323
324 cout << endl;
325
326 return cnt>=0;
327}
328
329Bool_t plotratescan(const char *night)
330{
331 MSQLMagic serv("sql.rc");
332 if (!serv.IsConnected())
333 {
334 cout << "ERROR - Connection to database failed." << endl;
335 return 0;
336 }
337
338 cout << "plotratescan" << endl;
339 cout << "------------" << endl;
340 cout << endl;
341 cout << "Connected to " << serv.GetName() << endl;
342 cout << "Night: " << night << endl;
343 cout << endl;
344
345 return plotratescan(serv, night);
346}
347
348Bool_t plotratescan()
349{
350 MSQLMagic serv("sql.rc");
351 if (!serv.IsConnected())
352 {
353 cout << "ERROR - Connection to database failed." << endl;
354 return 0;
355 }
356
357 cout << "plotratescan" << endl;
358 cout << "------------" << endl;
359 cout << endl;
360 cout << "Connected to " << serv.GetName() << endl;
361 cout << endl;
362
363 TSQLResult *res = serv.Query("SELECT fNight FROM Ratescan GROUP BY fNight ORDER BY fNight");
364 if (!res)
365 return kFALSE;
366
367 TSQLRow *row = 0;
368 while ((row=res->Next()))
369 {
370 const char *night = (*row)[0];
371 plotratescan(serv, night);
372 delete row;
373 }
374
375 delete res;
376
377 return kTRUE;
378}
Note: See TracBrowser for help on using the repository browser.