source: trunk/MagicSoft/Mars/mimage/MImgCleanTGB.cc@ 6724

Last change on this file since 6724 was 2781, checked in by tbretz, 21 years ago
*** empty log message ***
File size: 12.1 KB
Line 
1/* ======================================================================== *\
2!
3! *
4! * This file is part of MARS, the MAGIC Analysis and Reconstruction
5! * Software. It is distributed to you in the hope that it can be a useful
6! * and timesaving tool in analysing Data of imaging Cerenkov telescopes.
7! * It is distributed WITHOUT ANY WARRANTY.
8! *
9! * Permission to use, copy, modify and distribute this software and its
10! * documentation for any purpose is hereby granted without fee,
11! * provided that the above copyright notice appear in all copies and
12! * that both that copyright notice and this permission notice appear
13! * in supporting documentation. It is provided "as is" without express
14! * or implied warranty.
15! *
16!
17!
18! Author(s): Thomas Bretz, 12/2000 <mailto:tbretz@astro.uni-wuerzburg.de>
19! Author(s): Harald Kornmayer, 1/2001
20! Author(s): Nadia Tonello, 4/2003 <mailto:tonello@mppmu.mpg.de>
21!
22! Copyright: MAGIC Software Development, 2000-2003
23!
24!
25\* ======================================================================== */
26
27/////////////////////////////////////////////////////////////////////////////
28//
29// MImgCleanTGB
30//
31//
32// Input Containers:
33// MGeomCam, MCerPhotEvt, MSigmabar
34//
35// Output Containers:
36// MCerPhotEvt
37//
38/////////////////////////////////////////////////////////////////////////////
39#include "MImgCleanTGB.h"
40
41#include <stdlib.h> // atof
42#include <fstream> // ofstream, SavePrimitive
43
44#include <TGFrame.h> // TGFrame
45#include <TGLabel.h> // TGLabel
46#include <TArrayC.h> // TArrayC
47#include <TGTextEntry.h> // TGTextEntry
48
49#include "MLog.h"
50#include "MLogManip.h"
51
52#include "MParList.h"
53#include "MSigmabar.h"
54
55#include "MGeomPix.h"
56#include "MGeomCam.h"
57
58#include "MCerPhotPix.h"
59#include "MCerPhotEvt.h"
60
61#include "MPedPhotPix.h"
62#include "MPedPhotCam.h"
63
64#include "MGGroupFrame.h" // MGGroupFrame
65
66ClassImp(MImgCleanTGB);
67
68using namespace std;
69
70enum {
71 kImgCleanLvl1,
72 kImgCleanLvl2
73};
74
75static const TString gsDefName = "MImgCleanTGB";
76static const TString gsDefTitle = "Task to perform image cleaning";
77
78// --------------------------------------------------------------------------
79//
80// Default constructor. Here you can specify the cleaning method and levels.
81// If you don't specify them the 'common standard' values 3.0 and 2.5 (sigma
82// above mean) are used.
83// Here you can also specify how many rings around the core pixels you want
84// to analyze (with the fixed lvl2). The default value for "rings" is 1.
85//
86MImgCleanTGB::MImgCleanTGB(const Float_t lvl1, const Float_t lvl2,
87 const char *name, const char *title)
88 : fSgb(NULL), fCleaningMethod(kStandard), fCleanLvl1(lvl1),
89 fCleanLvl2(lvl2), fCleanRings(1)
90
91{
92 fName = name ? name : gsDefName.Data();
93 fTitle = title ? title : gsDefTitle.Data();
94
95 Print();
96}
97
98
99Int_t MImgCleanTGB::CleanStep3b(MCerPhotPix &pix)
100{
101 const Int_t id = pix.GetPixId();
102
103 //
104 // check if the pixel's next neighbor is a core pixel.
105 // if it is a core pixel set pixel state to: used.
106 //
107 MGeomPix &gpix = (*fCam)[id];
108 const Int_t nnmax = gpix.GetNumNeighbors();
109
110 Int_t rc = 0;
111
112 for (Int_t j=0; j<nnmax; j++)
113 {
114 const Int_t id2 = gpix.GetNeighbor(j);
115
116 if (fEvt->GetPixById(id2) && fEvt->IsPixelUsed(id2))
117 rc++;
118 }
119 return rc;
120}
121
122// --------------------------------------------------------------------------
123//
124// Look for the boundary pixels around the core pixels
125// if a pixel has more than 2.5 (clean level 2.5) sigma, and
126// a core neigbor, it is declared as used.
127//
128void MImgCleanTGB::CleanStep3(Int_t num1, Int_t num2)
129{
130 const Int_t entries = fEvt->GetNumPixels();
131
132 Int_t *u = new Int_t[entries];
133
134 for (Int_t i=0; i<entries; i++)
135 {
136 //
137 // get pixel as entry il from list
138 //
139 MCerPhotPix &pix = (*fEvt)[i];
140 u[i] = CleanStep3b(pix);
141 }
142
143 for (Int_t i=0; i<entries; i++)
144 {
145 MCerPhotPix &pix = (*fEvt)[i];
146 if (u[i]<num1)
147 pix.SetPixelUnused();
148 if (u[i]>num2)
149 pix.SetPixelUsed();
150 }
151
152 delete u;
153}
154
155void MImgCleanTGB::CleanStep3(Byte_t *nb, Int_t num1, Int_t num2)
156{
157 const Int_t entries = fEvt->GetNumPixels();
158
159 for (Int_t i=0; i<entries; i++)
160 {
161 MCerPhotPix &pix = (*fEvt)[i];
162
163 const Int_t idx = pix.GetPixId();
164
165 if (nb[idx]<num1 && pix.IsPixelUsed())
166 {
167 const MGeomPix &gpix = (*fCam)[idx];
168 const Int_t nnmax = gpix.GetNumNeighbors();
169 for (Int_t j=0; j<nnmax; j++)
170 nb[gpix.GetNeighbor(j)]--;
171
172 pix.SetPixelUnused();
173 }
174 }
175
176 for (Int_t i=0; i<entries; i++)
177 {
178 MCerPhotPix &pix = (*fEvt)[i];
179
180 const Int_t idx = pix.GetPixId();
181 if (nb[idx]>num2 && !pix.IsPixelUsed())
182 {
183 const MGeomPix &gpix = (*fCam)[idx];
184 const Int_t nnmax = gpix.GetNumNeighbors();
185 for (Int_t j=0; j<nnmax; j++)
186 nb[gpix.GetNeighbor(j)]++;
187
188 pix.SetPixelUsed();
189 }
190 }
191}
192
193// --------------------------------------------------------------------------
194//
195// Check if MEvtHeader exists in the Parameter list already.
196// if not create one and add them to the list
197//
198Int_t MImgCleanTGB::PreProcess (MParList *pList)
199{
200 fCam = (MGeomCam*)pList->FindObject("MGeomCam");
201 if (!fCam)
202 {
203 *fLog << dbginf << "MGeomCam not found (no geometry information available)... aborting." << endl;
204 return kFALSE;
205 }
206
207 fEvt = (MCerPhotEvt*)pList->FindObject("MCerPhotEvt");
208 if (!fEvt)
209 {
210 *fLog << dbginf << "MCerPhotEvt not found... aborting." << endl;
211 return kFALSE;
212 }
213
214 if (fCleaningMethod == kDemocratic)
215 {
216 fSgb = (MSigmabar*)pList->FindObject("MSigmabar");
217 if (!fSgb)
218 {
219 *fLog << dbginf << "MSigmabar not found... aborting." << endl;
220 return kFALSE;
221 }
222 }
223 else
224 {
225 fPed = (MPedPhotCam*)pList->FindObject("MPedPhotCam");
226 if (!fPed)
227 {
228 *fLog << dbginf << "MPedPhotCam not found... aborting." << endl;
229 return kFALSE;
230 }
231 }
232
233 return kTRUE;
234}
235
236// --------------------------------------------------------------------------
237//
238// Cleans the image.
239//
240Int_t MImgCleanTGB::Process()
241{
242 const Int_t entries = fEvt->GetNumPixels();
243
244 Double_t sum = 0;
245 Double_t sq = 0;
246 Double_t w = 0;
247 Double_t w2 = 0;
248 for (Int_t i=0; i<entries; i++)
249 {
250 //
251 // get pixel as entry il from list
252 //
253 MCerPhotPix &pix = (*fEvt)[i];
254
255 const Int_t idx = pix.GetPixId();
256 const Double_t entry = pix.GetNumPhotons();
257 const Double_t factor = fCam->GetPixRatio(idx);
258 const Double_t factorsqrt = fCam->GetPixRatioSqrt(idx);
259 const Float_t noise = (*fPed)[idx].GetRms();
260
261 if (entry * factorsqrt <= fCleanLvl2 * noise)
262 {
263 sum += entry*factor;
264 sq += entry*entry*factor*factor;
265 w += factor;
266 w2 += factor*factor;
267 }
268 }
269
270 Double_t mean = sum/w;
271 Double_t sdev = sqrt(sq/w2 - mean*mean);
272
273 TArrayC n(fCam->GetNumPixels());
274 Byte_t *nb = (Byte_t*)n.GetArray();
275 //Byte_t *nb = new Byte_t[1000];
276 //memset(nb, 0, 577);
277
278 for (Int_t i=0; i<entries; i++)
279 {
280 //
281 // get pixel as entry il from list
282 //
283 MCerPhotPix &pix = (*fEvt)[i];
284 const Int_t idx = pix.GetPixId();
285
286 const Double_t entry = pix.GetNumPhotons();
287 const Double_t factor = fCam->GetPixRatio(idx);
288
289 if (entry*factor > fCleanLvl1*sdev)
290 {
291 pix.SetPixelUsed();
292
293 const MGeomPix &gpix = (*fCam)[idx];
294 const Int_t nnmax = gpix.GetNumNeighbors();
295 for (Int_t j=0; j<nnmax; j++)
296 nb[gpix.GetNeighbor(j)]++;
297 }
298 else
299 pix.SetPixelUnused();
300 }
301
302 CleanStep3(nb, 2, 3);
303 //CleanStep3(nb, 2, 3);
304 //CleanStep3(nb, 2, 3);
305
306 return kTRUE;
307}
308
309// --------------------------------------------------------------------------
310//
311// Print descriptor and cleaning levels.
312//
313void MImgCleanTGB::Print(Option_t *o) const
314{
315 *fLog << all << GetDescriptor() << " using ";
316 switch (fCleaningMethod)
317 {
318 case kDemocratic:
319 *fLog << "democratic";
320 break;
321 case kStandard:
322 *fLog << "standard";
323 break;
324 }
325 *fLog << " cleaning initialized with noise level " << fCleanLvl1 << " and " << fCleanLvl2;
326 *fLog << " (CleanRings=" << fCleanRings << ")" << endl;
327}
328
329// --------------------------------------------------------------------------
330//
331// Create two text entry fields, one for each cleaning level and a
332// describing text line.
333//
334void MImgCleanTGB::CreateGuiElements(MGGroupFrame *f)
335{
336 //
337 // Create a frame for line 3 and 4 to be able
338 // to align entry field and label in one line
339 //
340 TGHorizontalFrame *f1 = new TGHorizontalFrame(f, 0, 0);
341 TGHorizontalFrame *f2 = new TGHorizontalFrame(f, 0, 0);
342
343 /*
344 * --> use with root >=3.02 <--
345 *
346
347 TGNumberEntry *fNumEntry1 = new TGNumberEntry(frame, 3.0, 2, M_NENT_LVL1, kNESRealOne, kNEANonNegative);
348 TGNumberEntry *fNumEntry2 = new TGNumberEntry(frame, 2.5, 2, M_NENT_LVL1, kNESRealOne, kNEANonNegative);
349
350 */
351 TGTextEntry *entry1 = new TGTextEntry(f1, "****", kImgCleanLvl1);
352 TGTextEntry *entry2 = new TGTextEntry(f2, "****", kImgCleanLvl2);
353
354 // --- doesn't work like expected (until root 3.02?) --- fNumEntry1->SetAlignment(kTextRight);
355 // --- doesn't work like expected (until root 3.02?) --- fNumEntry2->SetAlignment(kTextRight);
356
357 entry1->SetText("3.0");
358 entry2->SetText("2.5");
359
360 entry1->Associate(f);
361 entry2->Associate(f);
362
363 TGLabel *l1 = new TGLabel(f1, "Cleaning Level 1");
364 TGLabel *l2 = new TGLabel(f2, "Cleaning Level 2");
365
366 l1->SetTextJustify(kTextLeft);
367 l2->SetTextJustify(kTextLeft);
368
369 //
370 // Align the text of the label centered, left in the row
371 // with a left padding of 10
372 //
373 TGLayoutHints *laylabel = new TGLayoutHints(kLHintsCenterY|kLHintsLeft, 10);
374 TGLayoutHints *layframe = new TGLayoutHints(kLHintsCenterY|kLHintsLeft, 5, 0, 10);
375
376 //
377 // Add one entry field and the corresponding label to each line
378 //
379 f1->AddFrame(entry1);
380 f2->AddFrame(entry2);
381
382 f1->AddFrame(l1, laylabel);
383 f2->AddFrame(l2, laylabel);
384
385 f->AddFrame(f1, layframe);
386 f->AddFrame(f2, layframe);
387
388 f->AddToList(entry1);
389 f->AddToList(entry2);
390 f->AddToList(l1);
391 f->AddToList(l2);
392 f->AddToList(laylabel);
393 f->AddToList(layframe);
394}
395
396// --------------------------------------------------------------------------
397//
398// Process the GUI Events comming from the two text entry fields.
399//
400Bool_t MImgCleanTGB::ProcessMessage(Int_t msg, Int_t submsg, Long_t param1, Long_t param2)
401{
402 if (msg!=kC_TEXTENTRY || submsg!=kTE_ENTER)
403 return kTRUE;
404
405 TGTextEntry *txt = (TGTextEntry*)FindWidget(param1);
406
407 if (!txt)
408 return kTRUE;
409
410 Float_t lvl = atof(txt->GetText());
411
412 switch (param1)
413 {
414 case kImgCleanLvl1:
415 fCleanLvl1 = lvl;
416 *fLog << "Cleaning level 1 set to " << lvl << " sigma." << endl;
417 return kTRUE;
418
419 case kImgCleanLvl2:
420 fCleanLvl2 = lvl;
421 *fLog << "Cleaning level 2 set to " << lvl << " sigma." << endl;
422 return kTRUE;
423 }
424
425 return kTRUE;
426}
427
428// --------------------------------------------------------------------------
429//
430// Implementation of SavePrimitive. Used to write the call to a constructor
431// to a macro. In the original root implementation it is used to write
432// gui elements to a macro-file.
433//
434void MImgCleanTGB::StreamPrimitive(ofstream &out) const
435{
436 out << " MImgCleanTGB " << GetUniqueName() << "(";
437 out << fCleanLvl1 << ", " << fCleanLvl2;
438
439 if (fName!=gsDefName || fTitle!=gsDefTitle)
440 {
441 out << ", \"" << fName << "\"";
442 if (fTitle!=gsDefTitle)
443 out << ", \"" << fTitle << "\"";
444 }
445 out << ");" << endl;
446
447 if (fCleaningMethod!=kDemocratic)
448 return;
449
450 out << " " << GetUniqueName() << ".SetMethod(MImgCleanTGB::kDemocratic);" << endl;
451
452 if (fCleanRings==1)
453 return;
454
455 out << " " << GetUniqueName() << ".SetCleanRings(" << fCleanRings << ");" << endl;
456}
Note: See TracBrowser for help on using the repository browser.