source: trunk/Mars/fact/plots/plotratescan.C@ 19831

Last change on this file since 19831 was 19266, checked in by tbretz, 6 years ago
Fixed some minor issues with the cling based interpreter - this should not have any effect at runtime maybe except stability
File size: 10.6 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, fAzMin, fAzMax, fZdMin, fZdMax ";
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 const char *az_beg = (*row)[6];
152 const char *az_end = (*row)[7];
153 const char *zd_beg = (*row)[8];
154 const char *zd_end = (*row)[9];
155
156 int voltage_on = (*row)[2] ? atoi((*row)[2]) : -1;
157 float overvoltage = (*row)[3] ? atof((*row)[3]) : -100;
158 float current = (*row)[4] ? atof((*row)[4]) : -1;
159
160 delete row;
161
162 gROOT->SetSelectedPad(0);
163 TCanvas *c = new TCanvas(time_beg+11, time_beg);
164
165 c->SetGrid();
166 c->SetLogy();
167 c->SetTopMargin(0.01);
168 c->SetRightMargin(0.005);
169
170 TPaveText leg(600, 1e5, 1050, 8e8, "br");
171 leg.SetBorderSize(1);
172 leg.SetFillColor(kWhite);
173 leg.SetTextAlign(12);
174 leg.AddText(Form("Ratescan %lld ", search_id));
175 //leg.AddText("");
176 leg.AddText(Form("Begin %s", time_beg));
177 leg.AddText(Form("End %s", time_end));
178 leg.AddText(Form("Az %s#circ to %s#circ", az_beg, az_end));
179 leg.AddText(Form("Zd %s#circ to %s#circ", zd_beg, zd_end));
180 //leg.AddText("");
181 if (voltage_on==0)
182 leg.AddText("Voltage off");
183 else
184 {
185 if (current>=0)
186 leg.AddText(Form("Current <I_{med}> = %.1f #muA", current));
187 if (overvoltage>-70)
188 leg.AddText(Form("Voltage #DeltaU = %+.2f V", overvoltage));
189 }
190
191 TH1D h0("frame", "", 1050, 0, 1050);
192 h0.SetDirectory(0);
193 h0.SetStats(kFALSE);
194 h0.SetXTitle("Threshold [dac counts]");
195 h0.SetYTitle("Trigger rate [Hz]");
196 h0.GetXaxis()->SetLabelSize(0.05);
197 h0.GetXaxis()->SetTitleSize(0.05);
198 h0.GetYaxis()->SetLabelSize(0.05);
199 h0.GetYaxis()->SetTitleSize(0.05);
200 h0.SetMaximum(8e8);
201 h0.SetMinimum(0.61);
202 h0.DrawCopy();
203
204 leg.DrawClone();
205
206 for (int i=0; i<40; i++)
207 {
208 gb[i].SetNameTitle(Form("Board%02d", i), "Board trigger rate (time 40)");
209 gb[i].DrawClone("P");
210 }
211
212 g.SetMarkerStyle(kFullDotMedium);
213 g.SetMarkerColor(kBlue);
214 g.SetLineColor(kBlue);
215 g.SetNameTitle("Camera", "Camera trigger rate");
216 g.DrawClone("PL");
217
218 TGraph gr("good_ratescan_edit.txt", "%lg %lg");
219 gr.SetLineColor(12);
220 gr.DrawClone("L");
221
222 c->Write();
223
224 TString name;
225 name += night;
226 name += "-ratescan ";
227 name += time_beg;
228
229 //c->SaveAs(name+".pdf");
230 //c->SaveAs(name+".eps");
231 //c->SaveAs("/loc_data/analysis/"+name+".png");
232 c->SaveAs(Form("/data1/analysis/ratescans/%04d/%02d/%02d/%06d_%lld.png", atoi(night)/10000, (atoi(night)/100)%100, atoi(night)%100, atoi(night), search_id));
233
234 delete c;
235
236 return kTRUE;
237}
238
239Int_t plotid(MSQLMagic &serv, fits &file, const char *_search_id, Int_t row)
240{
241 Long64_t search_id = atol(_search_id);
242
243 cout << " " << search_id << endl;
244
245 UInt_t offset = file.GetUInt("MJDREF");
246
247 bool old = file.HasColumn("Data0");
248
249 Double_t *ptime = (Double_t*)file.SetPtrAddress("Time");
250 UInt_t *pid = (UInt_t*) file.SetPtrAddress(old ? "Data0" : "Id");
251 Float_t *rates = (Float_t*) file.SetPtrAddress(old ? "Data5" : "BoardRate");
252 UInt_t *pth = (UInt_t*) file.SetPtrAddress(old ? "Data1" : "Threshold");
253 Float_t *ptrig = (Float_t*) file.SetPtrAddress(old ? "Data4" : "TriggerRate");
254 Float_t *pontime = (Float_t*) file.SetPtrAddress(old ? "Data3" : "RelOnTime");
255
256 if (!ptime || !pid || !rates || !pth || !ptrig || !pontime)
257 return -1;
258
259 TGraph g;
260 TGraph gb[40];
261
262 while (file.GetRow(row++))
263 {
264 if (pid[0]<search_id)
265 continue;
266
267 if (pid[0]!=search_id)
268 break;
269
270 g.SetPoint(g.GetN(), pth[0], ptrig[0]/pontime[0]);
271 for (int i=0; i<40; i++)
272 gb[i].SetPoint(gb[i].GetN(), pth[0], rates[i]*40);
273 }
274
275 if (g.GetN()>0)
276 plot(serv, search_id, g, gb);
277
278 return row;
279
280
281}
282
283Bool_t plotratescan(MSQLMagic &serv, const char *_night)
284{
285 Long64_t night = atol(_night);
286
287 TString fname = Form("/fact/aux/%04lld/%02lld/%02lld/%06lld.RATE_SCAN_DATA.fits",
288 night/10000, (night/100)%100, night%100, night);
289
290 cout << " " << fname << endl;
291
292 fits file(fname.Data());
293 if (!file)
294 {
295 cout << "ERROR - Cannot access " << fname << ": " << gSystem->GetError() << endl;
296 return kFALSE;
297 }
298
299 TString query;
300 query += "SELECT fRatescanID FROM Ratescan WHERE fNight=";
301 query += night;
302 query += " ORDER BY fRatescanID";
303 /*
304 query += "SELECT fRatescanID, fTimeBeg, fTimeEnd, fVoltageIsOn, ";
305 query += " fOvervoltage, fCurrentMedMean, fRateBegin, fRateEnd, ";
306 query += " fThresholdBegin, fThresholdEnd, fNumPoints ";
307 query += "WHERE fNight=";
308 query += night;
309 */
310 TSQLResult *res = serv.Query(query);
311 if (!res)
312 return kFALSE;
313
314 if (res->GetRowCount()==0)
315 {
316 delete res;
317 return kTRUE;
318 }
319
320 TString oname = Form("%06lld-ratescan.root", night);
321 //cout << " " << oname << '\n' << endl;
322 TFile rootfile(oname.Data(), "recreate");
323
324 TSQLRow *row = 0;
325
326 Int_t cnt = 0;
327 while ((row=res->Next()) && cnt>=0)
328 {
329 const char *id = (*row)[0];
330 cnt = plotid(serv, file, id, cnt);
331 delete row;
332 }
333
334 delete res;
335
336 cout << endl;
337
338 return cnt>=0;
339}
340
341Bool_t plotratescan(const char *night)
342{
343 MSQLMagic serv("sql.rc");
344 if (!serv.IsConnected())
345 {
346 cout << "ERROR - Connection to database failed." << endl;
347 return 0;
348 }
349
350 cout << "plotratescan" << endl;
351 cout << "------------" << endl;
352 cout << endl;
353 cout << "Connected to " << serv.GetName() << endl;
354 cout << "Night: " << night << endl;
355 cout << endl;
356
357 return plotratescan(serv, night);
358}
359
360Bool_t plotratescan()
361{
362 MSQLMagic serv("sql.rc");
363 if (!serv.IsConnected())
364 {
365 cout << "ERROR - Connection to database failed." << endl;
366 return 0;
367 }
368
369 cout << "plotratescan" << endl;
370 cout << "------------" << endl;
371 cout << endl;
372 cout << "Connected to " << serv.GetName() << endl;
373 cout << endl;
374
375 TSQLResult *res = serv.Query("SELECT fNight FROM Ratescan GROUP BY fNight ORDER BY fNight");
376 if (!res)
377 return kFALSE;
378
379 TSQLRow *row = 0;
380 while ((row=res->Next()))
381 {
382 const char *night = (*row)[0];
383 plotratescan(serv, night);
384 delete row;
385 }
386
387 delete res;
388
389 return kTRUE;
390}
Note: See TracBrowser for help on using the repository browser.