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

Last change on this file since 2422 was 2422, 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 "MPedestalPix.h"
62#include "MPedestalCam.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 = (MPedestalCam*)pList->FindObject("MPedestalCam");
226 if (!fPed)
227 {
228 *fLog << dbginf << "MPedestalCam 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 Double_t entry = pix.GetNumPhotons();
256 const Double_t factor = fCam->GetPixRatio(pix.GetPixId());
257 const Float_t noise = (*fPed)[pix.GetPixId()].GetPedestalRms();
258
259 if (entry * TMath::Sqrt(factor) <= fCleanLvl2 * noise)
260 {
261 sum += entry*factor;
262 sq += entry*entry*factor*factor;
263 w += factor;
264 w2 += factor*factor;
265 }
266 }
267
268 Double_t mean = sum/w;
269 Double_t sdev = sqrt(sq/w2 - mean*mean);
270
271 TArrayC n(fCam->GetNumPixels());
272 Byte_t *nb = (Byte_t*)n.GetArray();
273 //Byte_t *nb = new Byte_t[1000];
274 //memset(nb, 0, 577);
275
276 for (Int_t i=0; i<entries; i++)
277 {
278 //
279 // get pixel as entry il from list
280 //
281 MCerPhotPix &pix = (*fEvt)[i];
282 const Int_t idx = pix.GetPixId();
283
284 const Double_t entry = pix.GetNumPhotons();
285 const Double_t factor = fCam->GetPixRatio(idx);
286
287 if (entry*factor > fCleanLvl1*sdev)
288 {
289 pix.SetPixelUsed();
290
291 const MGeomPix &gpix = (*fCam)[idx];
292 const Int_t nnmax = gpix.GetNumNeighbors();
293 for (Int_t j=0; j<nnmax; j++)
294 nb[gpix.GetNeighbor(j)]++;
295 }
296 else
297 pix.SetPixelUnused();
298 }
299
300 CleanStep3(nb, 2, 3);
301 //CleanStep3(nb, 2, 3);
302 //CleanStep3(nb, 2, 3);
303
304 return kTRUE;
305}
306
307// --------------------------------------------------------------------------
308//
309// Print descriptor and cleaning levels.
310//
311void MImgCleanTGB::Print(Option_t *o) const
312{
313 *fLog << all << GetDescriptor() << " using ";
314 switch (fCleaningMethod)
315 {
316 case kDemocratic:
317 *fLog << "democratic";
318 break;
319 case kStandard:
320 *fLog << "standard";
321 break;
322 }
323 *fLog << " cleaning initialized with noise level " << fCleanLvl1 << " and " << fCleanLvl2;
324 *fLog << " (CleanRings=" << fCleanRings << ")" << endl;
325}
326
327// --------------------------------------------------------------------------
328//
329// Create two text entry fields, one for each cleaning level and a
330// describing text line.
331//
332void MImgCleanTGB::CreateGuiElements(MGGroupFrame *f)
333{
334 //
335 // Create a frame for line 3 and 4 to be able
336 // to align entry field and label in one line
337 //
338 TGHorizontalFrame *f1 = new TGHorizontalFrame(f, 0, 0);
339 TGHorizontalFrame *f2 = new TGHorizontalFrame(f, 0, 0);
340
341 /*
342 * --> use with root >=3.02 <--
343 *
344
345 TGNumberEntry *fNumEntry1 = new TGNumberEntry(frame, 3.0, 2, M_NENT_LVL1, kNESRealOne, kNEANonNegative);
346 TGNumberEntry *fNumEntry2 = new TGNumberEntry(frame, 2.5, 2, M_NENT_LVL1, kNESRealOne, kNEANonNegative);
347
348 */
349 TGTextEntry *entry1 = new TGTextEntry(f1, "****", kImgCleanLvl1);
350 TGTextEntry *entry2 = new TGTextEntry(f2, "****", kImgCleanLvl2);
351
352 // --- doesn't work like expected (until root 3.02?) --- fNumEntry1->SetAlignment(kTextRight);
353 // --- doesn't work like expected (until root 3.02?) --- fNumEntry2->SetAlignment(kTextRight);
354
355 entry1->SetText("3.0");
356 entry2->SetText("2.5");
357
358 entry1->Associate(f);
359 entry2->Associate(f);
360
361 TGLabel *l1 = new TGLabel(f1, "Cleaning Level 1");
362 TGLabel *l2 = new TGLabel(f2, "Cleaning Level 2");
363
364 l1->SetTextJustify(kTextLeft);
365 l2->SetTextJustify(kTextLeft);
366
367 //
368 // Align the text of the label centered, left in the row
369 // with a left padding of 10
370 //
371 TGLayoutHints *laylabel = new TGLayoutHints(kLHintsCenterY|kLHintsLeft, 10);
372 TGLayoutHints *layframe = new TGLayoutHints(kLHintsCenterY|kLHintsLeft, 5, 0, 10);
373
374 //
375 // Add one entry field and the corresponding label to each line
376 //
377 f1->AddFrame(entry1);
378 f2->AddFrame(entry2);
379
380 f1->AddFrame(l1, laylabel);
381 f2->AddFrame(l2, laylabel);
382
383 f->AddFrame(f1, layframe);
384 f->AddFrame(f2, layframe);
385
386 f->AddToList(entry1);
387 f->AddToList(entry2);
388 f->AddToList(l1);
389 f->AddToList(l2);
390 f->AddToList(laylabel);
391 f->AddToList(layframe);
392}
393
394// --------------------------------------------------------------------------
395//
396// Process the GUI Events comming from the two text entry fields.
397//
398Bool_t MImgCleanTGB::ProcessMessage(Int_t msg, Int_t submsg, Long_t param1, Long_t param2)
399{
400 if (msg!=kC_TEXTENTRY || submsg!=kTE_ENTER)
401 return kTRUE;
402
403 TGTextEntry *txt = (TGTextEntry*)FindWidget(param1);
404
405 if (!txt)
406 return kTRUE;
407
408 Float_t lvl = atof(txt->GetText());
409
410 switch (param1)
411 {
412 case kImgCleanLvl1:
413 fCleanLvl1 = lvl;
414 *fLog << "Cleaning level 1 set to " << lvl << " sigma." << endl;
415 return kTRUE;
416
417 case kImgCleanLvl2:
418 fCleanLvl2 = lvl;
419 *fLog << "Cleaning level 2 set to " << lvl << " sigma." << endl;
420 return kTRUE;
421 }
422
423 return kTRUE;
424}
425
426// --------------------------------------------------------------------------
427//
428// Implementation of SavePrimitive. Used to write the call to a constructor
429// to a macro. In the original root implementation it is used to write
430// gui elements to a macro-file.
431//
432void MImgCleanTGB::StreamPrimitive(ofstream &out) const
433{
434 out << " MImgCleanTGB " << GetUniqueName() << "(";
435 out << fCleanLvl1 << ", " << fCleanLvl2;
436
437 if (fName!=gsDefName || fTitle!=gsDefTitle)
438 {
439 out << ", \"" << fName << "\"";
440 if (fTitle!=gsDefTitle)
441 out << ", \"" << fTitle << "\"";
442 }
443 out << ");" << endl;
444
445 if (fCleaningMethod!=kDemocratic)
446 return;
447
448 out << " " << GetUniqueName() << ".SetMethod(MImgCleanTGB::kDemocratic);" << endl;
449
450 if (fCleanRings==1)
451 return;
452
453 out << " " << GetUniqueName() << ".SetCleanRings(" << fCleanRings << ");" << endl;
454}
Note: See TracBrowser for help on using the repository browser.