source: trunk/MagicSoft/Cosy/main/MCaos.cc@ 4643

Last change on this file since 4643 was 4105, checked in by tbretz, 21 years ago
*** empty log message ***
File size: 10.9 KB
Line 
1#include "MCaos.h"
2
3#include <iostream>
4#include <iomanip>
5
6#include <TSystem.h>
7#include <TFile.h>
8#include <TTree.h>
9#include <TH1.h>
10#include <TH2.h>
11#include <TGraph.h>
12#include <TCanvas.h>
13
14#include "MTime.h"
15
16#include "Led.h"
17#include "Ring.h"
18#include "Rings.h"
19#include "FilterLed.h"
20
21#include "coord.h"
22
23void MCaos::ReadResources(const char *name="leds.txt")
24{
25 ifstream fin(name);
26 if (!fin)
27 {
28 cout << "ERROR - Cannot open " << name << endl;
29 return;
30 }
31
32 fPositions.Clear();
33
34 cout << " Reading " << name << ":" << endl;
35 cout << "------------------------------" << endl;
36
37 while (1)
38 {
39 Double_t px, py, ox, oy;
40 fin >> px >> py >> ox >> oy;
41 if (!fin)
42 break;
43
44 cout << " Led #" << fPositions.GetEntriesFast() << ": ";
45 cout << setw(3) << px << " ";
46 cout << setw(3) << py << " (";
47 cout << setw(3) << ox << ", ";
48 cout << setw(3) << oy << ")" << endl;
49 AddPosition(px, py, ox, oy);
50 }
51 cout << "Found " << fPositions.GetEntriesFast() << " leds." << endl;
52}
53
54void MCaos::OpenFile()
55{
56 int i=0;
57 char name[100];
58 while (1)
59 {
60 sprintf(name, "data/data%03d.root", i++);
61 if (gSystem->AccessPathName(name, kFileExists))
62 break;
63 }
64
65 if (fFile)
66 delete fFile;
67
68 fFile = new TFile(name, "RECREATE");
69
70 if (!fFile->IsOpen())
71 {
72 delete fFile;
73 fFile = NULL;
74
75 cout << "Error: Cannot open file '" << name << "'" << endl;
76 }
77
78 TTree *tree = new TTree("Data", "Real CaOs Data");
79
80 fLeds = new Leds;
81 fEvtTime = 0;
82
83 tree->Branch("Leds", "TClonesArray", &fLeds);
84 tree->Branch("ZenithDist.", &fZenithDist, "fZenithDist/D");
85 tree->Branch("Azimuth.", &fAzimuth, "fAzimuth/D");
86 tree->Branch("EvtTime.", &fEvtTime, "fEvtTime/D");
87
88 cout << "Root file '" << name << "' open." << endl;
89}
90
91void MCaos::CloseFile()
92{
93 if (!fFile)
94 return;
95
96 const TString name = fFile->GetName();
97 const Double_t n = ((TTree*)fFile->Get("Data"))->GetEntries();
98
99 fFile->Write();
100 delete fFile;
101 fFile = NULL;
102
103 cout << "Root file closed (n=" << n << ")" << endl;
104
105 if (n<1)
106 {
107 gSystem->Unlink(name);
108 cout << "Root file deleted - no entries." << endl;
109 }
110}
111
112void MCaos::InitHistograms()
113{
114 if (fHistpr)
115 return;
116
117 Rings r;
118 r.CalcRings(fPositions);
119
120 const Ring &c = r.GetCenter();
121
122 Double_t xmin = c.GetX()-50;
123 Double_t xmax = c.GetX()+50;
124
125 Double_t ymin = c.GetY()-50;
126 Double_t ymax = c.GetY()+50;
127
128 Double_t rmin = c.GetR()-50;
129 Double_t rmax = c.GetR()+50;
130
131 Int_t xbin = 1001;
132 Int_t ybin = 1001;
133 Int_t rbin = 1001;
134
135 const Int_t sz = 50;
136 fHistled = new TH2F*[fPositions.GetEntriesFast()];
137 fHistw = new TH1F*[fPositions.GetEntriesFast()];
138 for (int i=0; i<fPositions.GetEntriesFast(); i++)
139 {
140 TString name = "LED";
141 TString title = "LED #";
142
143 name += i;
144 title += i;
145
146 const Led &p = fPositions(i);
147 fHistled[i] = new TH2F(name, title,
148 20*sz+1, p.GetX()-sz, p.GetX()+sz,
149 20*sz+1, p.GetY()-sz, p.GetY()+sz);
150 fHistled[i]->SetXTitle("x [pix]");
151 fHistled[i]->SetYTitle("counts");
152
153 name = "Angle";
154 title = "Angle of the Led #";
155
156 name += i;
157 title += i;
158
159 fHistw[i] = new TH1F;
160 fHistw[i]->SetNameTitle(name, title);
161 fHistw[i]->SetBins(721, -180.5, 180.5);
162 fHistw[i]->SetXTitle("\\Phi [deg]");
163 fHistw[i]->SetYTitle("counts");
164 }
165
166 fHistallw = new TH1F;
167 fHistallw->SetNameTitle("allw","Rotation angel");
168 fHistallw->SetBins(26, -25, 25);
169 fHistallw->SetXTitle("\\phi [arcmin]");
170 fHistallw->SetYTitle("counts");
171
172 fHistprxpry = new TH2F;
173 fHistprxpry->SetNameTitle("prx und pry","x- and y-coordinate of the ring-center");
174 fHistprxpry->SetBins(xbin, xmin, xmax, ybin, ymin, ymax);
175 fHistprxpry->SetXTitle("x [pix]");
176 fHistprxpry->SetYTitle("y [pix]");
177 fHistprxpry->SetZTitle("counts");
178
179 fGraphprx = new TGraph;
180 fGraphprx->SetTitle("time-developement of the x-coordinate of the ring-center");
181
182 fGraphpry = new TGraph;
183 fGraphpry->SetTitle("time-developement of the y-coordinate of the ring-center");
184
185 fGraphw = new TGraph;
186 fGraphw->SetTitle("Time-developement of rotation angle");
187
188 fHistpr = new TH1F("pr","Radius of the ring", rbin, rmin, rmax);
189 fHistpr->SetXTitle("r [pix]");
190 fHistpr->SetYTitle("counts");
191}
192
193void MCaos::DeleteHistograms()
194{
195 TH1F *dummy = fHistpr;
196 fHistpr=NULL;
197
198 if (!dummy)
199 return;
200
201 delete dummy;
202 delete fHistprxpry;
203 delete fHistallw;
204 delete fGraphprx;
205 delete fGraphpry;
206 delete fGraphr;
207
208 for (int i=0; i<6; i++)
209 {
210 delete fHistled[i];
211 delete fHistw[i];
212 delete fGraphw;
213 }
214 delete fHistled;
215 delete fHistw;
216}
217
218void MCaos::ShowHistograms()
219{
220 if (!fHistpr || fHistpr->GetEntries()<1)
221 return;
222
223 TH1 *h;
224
225 TCanvas *c = new TCanvas("cring", "Center of the ring", 800, 800);
226 c->Divide(2,2);
227 c->cd(1);
228 h = (TH1*)fHistprxpry->ProfileX();
229 h->Fit("gaus");
230 h->Draw();
231 h->SetBit(kCanDelete);
232 c->cd(2);
233 h = (TH1*)fHistprxpry->ProfileY();
234 h->Fit("gaus");
235 h->Draw();
236 h->SetBit(kCanDelete);
237 c->cd(3);
238 fHistpr->Fit("gaus");
239 fHistpr->DrawCopy();
240 c->cd(4);
241 fHistprxpry->DrawCopy(/*"surf2"*/);
242 c->Update();
243
244 const Int_t n1 = (Int_t)(sqrt(fPositions.GetEntriesFast())+1.0);
245 const Int_t n2 = (Int_t)(sqrt(fPositions.GetEntriesFast())+0.5);
246
247 TCanvas *c1 = new TCanvas("cpos", "Led Positions", 800, 600);
248 TCanvas *c2 = new TCanvas("cangle", "Angles of the Leds", 800, 600);
249 c1->Divide(n1, n2);
250 c2->Divide(n1, n2);
251 for (int i=0; i<fPositions.GetEntriesFast(); i++)
252 {
253 c1->cd(i+1);
254 fHistled[i]->DrawCopy();
255 c2->cd(i+1);
256 fHistw[i]->DrawCopy();
257 }
258 c1->Update();
259 c2->Update();
260
261 c = new TCanvas("ctime", "Timedevelopement of Center", 800, 800);
262 c->Divide(1,3);
263 c->cd(1);
264 h = fGraphprx->GetHistogram();
265 h->SetXTitle("time [s]");
266 h->SetYTitle("x [pix]");
267 h->DrawCopy();
268 ((TPad*)gPad)->SetSelected(NULL);
269 fGraphprx->DrawClone("ALP*")->SetBit(kCanDelete);
270 c->cd(2);
271 h = fGraphpry->GetHistogram();
272 h->SetXTitle("time [s]");
273 h->SetYTitle("y [pix]");
274 h->DrawCopy();
275 ((TPad*)gPad)->SetSelected(NULL);
276 fGraphpry->DrawClone("ALP*")->SetBit(kCanDelete);
277 c->cd(3);
278 h = fGraphr->GetHistogram();
279 h->SetXTitle("time [s]");
280 h->SetYTitle("r [pix]");
281 h->DrawCopy();
282 ((TPad*)gPad)->SetSelected(NULL);
283 fGraphr->DrawClone("ALP*")->SetBit(kCanDelete);
284 c->Modified();
285 c->Update();
286
287 c = new TCanvas("crot", "rotation angle", 800, 600);
288 c->Divide(2,1);
289 c->cd(1);
290 fHistallw->SetXTitle("\\phi [arcmin]");
291 fHistallw->SetYTitle("counts");
292 fHistallw->DrawCopy();
293 c->cd(2);
294 h = fGraphw->GetHistogram();
295 h->SetXTitle("time [s]");
296 h->SetYTitle("\\phi [arcmin]");
297 h->DrawCopy();
298 ((TPad*)gPad)->SetSelected(NULL);
299 fGraphw->DrawClone("ALP*")->SetBit(kCanDelete);
300
301
302 /* --------------------------------------------------------
303 * CALCULATE OFFSETS!
304 * --------------------------------------------------------
305 Rings r;
306 r.CalcRings(fPositions);
307
308 const Ring &c = r.GetCenter();
309 */
310}
311
312void MCaos::ResetHistograms()
313{
314 if (!fHistpr)
315 return;
316
317 fHistpr->Reset();
318 fHistprxpry->Reset();
319 fHistallw->Reset();
320 for (int i=0; i<6; i++)
321 {
322 fHistled[i]->Reset();
323 fHistw[i]->Reset();
324 }
325}
326
327Ring MCaos::Run(byte *img, bool printl, bool printr, const ZdAz &pos, const MTime &t)
328{
329 Leds &leds = *fLeds;
330 leds.Clear();
331
332 // img width height radius sigma
333 FilterLed f(img, 768, 576, 50, 3.0);
334
335 Int_t first=0;
336 for (int i=0; i<fPositions.GetEntriesFast(); i++)
337 {
338 // Try to find Led in this area
339 const Led &l0 = fPositions(i);
340 f.Execute(leds, l0.GetX(), l0.GetY());
341
342 // Loop over newly found Leds
343 for (int j=first; j<leds.GetEntries(); j++)
344 {
345 Led &l1 = leds(j);
346
347 // Add Offset to Led
348 l1.AddOffset(l0.GetDx(), l0.GetDy());
349
350 // Mark Led in image (FIXME: Move to MStarguider)
351 f.MarkPoint(l1.GetX(), l1.GetY(), l1.GetMag());
352
353 //old
354 /*
355 // Fill values into Histogram
356 if (!fHistpr)
357 continue;
358
359 fHistled[i]->Fill(l1.GetX(), l1.GetY());
360 fHistw[i]->Fill(l1.GetPhi());
361 */
362 }
363 first = leds.GetEntries();
364 }
365
366 Rings rings;
367 rings.CalcRings(leds, 265, 268);
368
369 const Ring &center = rings.GetCenter();
370
371 // FIXME!
372 static const MTime t0(t);
373 fEvtTime = t-t0;
374
375 if (fHistpr)
376 {
377 fHistpr->Fill(center.GetR());
378 fHistprxpry->Fill(center.GetX(), center.GetY());
379 fGraphprx->SetPoint(fGraphprx->GetN(), fEvtTime, center.GetX());
380 fGraphpry->SetPoint(fGraphpry->GetN(), fEvtTime, center.GetY());
381
382 //new
383 //-----
384 Double_t sum = 0;
385 for (int j=0; j<leds.GetEntries(); j++)
386 {
387 Led &l1 = leds(j);
388
389 fHistled[j]->Fill(l1.GetX(), l1.GetY());
390 //fHistw[j]->Fill(l1.GetPhi());
391
392 Double_t phi[6] =
393 {
394 0,
395 0,
396 0,
397 0,
398 0,
399 0
400 };
401
402 const Double_t w = (leds(j).GetPhi()-phi[j])*60;
403 sum += w;
404
405 fHistw[j]->Fill(w);
406 sum /= leds.GetEntries();
407 }
408 fGraphw->SetPoint(fGraphw->GetN(), fEvtTime, sum);
409 fHistallw->Fill(sum/leds.GetEntries());
410 //-----
411 }
412
413 if (printl)
414 leds.Print();
415 if (printr)
416 rings.Print();
417
418 if (fFile && leds.GetEntries()>0)
419 {
420 fZenithDist = pos.Zd(); //fCosy ? fCosy->GetPointingPos().Zd() : 0
421 fAzimuth = pos.Az(); //fCosy ? fCosy->GetPointingPos().Az() : 0;
422
423 TTree *t = (TTree*)fFile->Get("Data");
424 t->Fill();
425 }
426
427 return center;
428 /*
429 if (fCaosAnalyse->IsEntryEnabled(IDM_kStopAnalyse))
430 {
431 const Ring &center = rings.GetCenter();
432
433 Double_t phi[6] =
434 {
435 -124.727,
436 -61.0495,
437 -16.7907,
438 49.3119,
439 139.086
440 };
441
442 Double_t sum = 0;
443 for (int i=0; i<6 && leds.At(i); i++)
444 {
445 const Double_t w = (leds(i).GetPhi()-phi[i])*60;
446
447 sum += w;
448
449 fHistw[i]->Fill(w);
450 fHistv[i]->Fill(leds(i).GetPhi());
451 fGraphw[i]->SetPoint(fGraphw[i]->GetN(), fEvtTime, w);
452 }
453 fHistallw->Fill(sum/5);
454 }
455 */
456}
457
Note: See TracBrowser for help on using the repository browser.