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

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