source: trunk/MagicSoft/Mars/manalysis/MImgCleanStd.cc@ 1521

Last change on this file since 1521 was 1503, checked in by tbretz, 22 years ago
*** empty log message ***
  • Property svn:executable set to *
File size: 12.4 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@uni-sw.gwdg.de>
19! Author(s): Harald Kornmayer 1/2001 (harald@mppmu.mpg.de)
20!
21! Copyright: MAGIC Software Development, 2000-2002
22!
23!
24\* ======================================================================== */
25
26/////////////////////////////////////////////////////////////////////////////
27// //
28// MImgCleanStd //
29// //
30// This is the standard image cleaning. If you want to know how it works //
31// Please look at the three CleanSteps and Process //
32// //
33// FIXME: MImgCleanStd is not yet completely optimized for speed. //
34// Maybe we don't have to loop over all pixels all the time... //
35// //
36// Input Containers: //
37// MGeomCam, MCerPhotEvt //
38// //
39// Output Containers: //
40// -/- //
41// //
42/////////////////////////////////////////////////////////////////////////////
43#include "MImgCleanStd.h"
44
45#include <stdlib.h> // atof
46#include <fstream.h> // ofstream, SavePrimitive
47
48#include <TGFrame.h> // TGFrame
49#include <TGLabel.h> // TGLabel
50#include <TGTextEntry.h> // TGTextEntry
51
52#include "MLog.h"
53#include "MLogManip.h"
54
55#include "MParList.h"
56#include "MGeomPix.h"
57#include "MGeomCam.h"
58#include "MCerPhotPix.h"
59#include "MCerPhotEvt.h"
60
61#include "MGGroupFrame.h" // MGGroupFrame
62
63ClassImp(MImgCleanStd);
64
65enum {
66 kImgCleanLvl1,
67 kImgCleanLvl2
68};
69
70static const TString gsDefName = "MImgCleanStd";
71static const TString gsDefTitle = "Task to perform a standard image cleaning";
72
73// --------------------------------------------------------------------------
74//
75// Default constructor. Here you can specify the cleaning levels. If you
76// don't specify them the 'common standard' values 3.0 and 2.5 (sigma
77// above mean) are used
78//
79MImgCleanStd::MImgCleanStd(const Float_t lvl1, const Float_t lvl2,
80 const char *name, const char *title)
81 : fCleanLvl1(lvl1), fCleanLvl2(lvl2)
82{
83 fName = name ? name : gsDefName.Data();
84 fTitle = title ? title : gsDefTitle.Data();
85
86 Print();
87}
88
89// --------------------------------------------------------------------------
90//
91// This method looks for all pixels with an entry (photons)
92// that is three times bigger than the noise of the pixel
93// (std: 3 sigma, clean level 1)
94//
95// Returns the maximum Pixel Id (used for ispixused in CleanStep2)
96//
97Int_t MImgCleanStd::CleanStep1()
98{
99 const Int_t entries = fEvt->GetNumPixels();
100
101 Int_t max = entries;
102
103 //
104 // check the number of all pixels against the noise level and
105 // set them to 'unused' state if necessary
106 //
107 for (Int_t i=0; i<entries; i++ )
108 {
109 MCerPhotPix &pix = (*fEvt)[i];
110
111 const Float_t entry = pix.GetNumPhotons();
112 const Float_t noise = pix.GetErrorPhot();
113
114 // COBB: '<=' to skip entry=noise=0
115 if (entry <= fCleanLvl1 * noise)
116 pix.SetPixelUnused();
117
118 if (pix.GetPixId()>max)
119 max = pix.GetPixId();
120 }
121 return max;
122}
123
124// --------------------------------------------------------------------------
125//
126// check if the survived pixel have a neighbor, that also
127// survived, otherwise set pixel to unused. (removes pixels without
128// neighbors)
129//
130// takes the maximum pixel id from CleanStep1 as an argument
131//
132void MImgCleanStd::CleanStep2(Int_t max)
133{
134 const Int_t entries = fEvt->GetNumPixels();
135
136 //
137 // In the worst case we have to loop 6 times 577 times, to
138 // catch the behaviour of all next neighbors. Here we can gain
139 // much by using an array instead of checking through all pixels
140 // (MCerPhotEvt::IsPixelUsed) all the time.
141 //
142 Byte_t *ispixused = new Byte_t[max+1];
143 memset(ispixused, 0, max+1);
144
145 for (Int_t i=0; i<entries; i++)
146 {
147 MCerPhotPix &pix = (*fEvt)[i];
148 ispixused[pix.GetPixId()] = pix.IsPixelUsed();
149 }
150
151 for (Int_t i=0; i<entries; i++)
152 {
153 //
154 // get entry i from list
155 //
156 MCerPhotPix &pix = (*fEvt)[i];
157
158 //
159 // check if pixel is in use, if not goto next pixel in list
160 //
161 if (!pix.IsPixelUsed())
162 continue;
163
164 //
165 // get pixel id of this entry
166 //
167 const Int_t id = pix.GetPixId();
168
169 //
170 // count number of next neighbors of this pixel which
171 // state is 'used'
172 //
173 const MGeomPix &gpix = (*fCam)[id];
174 const Int_t nnmax = gpix.GetNumNeighbors();
175
176 Bool_t cnt = kFALSE;
177 for (Int_t j=0; j<nnmax; j++)
178 {
179 const Int_t id2 = gpix.GetNeighbor(j);
180
181 if (!ispixused[id2])
182 continue;
183
184 cnt = kTRUE;
185 break;
186 }
187 if (cnt)
188 continue;
189
190 //
191 // check if no next neighbor has the state 'used'
192 // set this pixel to 'unused', too.
193 //
194 pix.SetPixelUnused();
195 }
196
197 delete ispixused;
198
199 //
200 // now we declare all pixels that survive as CorePixels
201 //
202 for (Int_t i=0; i<entries; i++)
203 {
204 MCerPhotPix &pix = (*fEvt)[i];
205
206 if (pix.IsPixelUsed())
207 pix.SetPixelCore();
208 }
209}
210
211// --------------------------------------------------------------------------
212//
213// Look for the boundary pixels around the core pixels
214// if a pixel has more than 2.5 (clean level 2.5) sigma, and
215// a core neigbor it is declared as used.
216//
217void MImgCleanStd::CleanStep3()
218{
219 const Int_t entries = fEvt->GetNumPixels();
220
221 for (Int_t i=0; i<entries; i++)
222 {
223 //
224 // get pixel as entry il from list
225 //
226 MCerPhotPix &pix = (*fEvt)[i];
227
228 //
229 // if pixel is a core pixel go to the next pixel
230 //
231 if (pix.IsPixelCore())
232 continue;
233
234 //
235 // check the num of photons against the noise level
236 //
237 const Float_t entry = pix.GetNumPhotons();
238 const Float_t noise = pix.GetErrorPhot();
239
240 if (entry <= fCleanLvl2 * noise )
241 continue;
242
243 //
244 // get pixel id of this entry
245 //
246 const Int_t id = pix.GetPixId();
247
248 //
249 // check if the pixel's next neighbor is a core pixel.
250 // if it is a core pixel set pixel state to: used.
251 //
252 MGeomPix &gpix = (*fCam)[id];
253 const Int_t nnmax = gpix.GetNumNeighbors();
254
255 for (Int_t j=0; j<nnmax; j++)
256 {
257 const Int_t id2 = gpix.GetNeighbor(j);
258
259 if (!fEvt->IsPixelCore(id2))
260 continue;
261
262 pix.SetPixelUsed();
263 break;
264 }
265 }
266}
267
268// --------------------------------------------------------------------------
269//
270// check if MEvtHeader exists in the Parameter list already.
271// if not create one and add them to the list
272//
273Bool_t MImgCleanStd::PreProcess (MParList *pList)
274{
275 fCam = (MGeomCam*)pList->FindObject("MGeomCam");
276 if (!fCam)
277 {
278 *fLog << dbginf << "MGeomCam not found (no geometry information available)... aborting." << endl;
279 return kFALSE;
280 }
281
282 fEvt = (MCerPhotEvt*)pList->FindObject("MCerPhotEvt");
283 if (!fEvt)
284 {
285 *fLog << dbginf << "MCerPhotEvt not found... aborting." << endl;
286 return kFALSE;
287 }
288
289 return kTRUE;
290}
291
292
293// --------------------------------------------------------------------------
294//
295// Cleans the image.
296//
297Bool_t MImgCleanStd::Process()
298{
299 const Int_t max = CleanStep1();
300 CleanStep2(max);
301 CleanStep3();
302
303 return kTRUE;
304}
305
306// --------------------------------------------------------------------------
307//
308// Print descriptor and cleaning levels.
309//
310void MImgCleanStd::Print(Option_t *o) const
311{
312 *fLog << GetDescriptor() << " initialized with noise level ";
313 *fLog << fCleanLvl1 << " and " << fCleanLvl2 << endl;
314}
315
316// --------------------------------------------------------------------------
317//
318// Craete two text entry fields, one for each cleaning level and a
319// describing text line.
320//
321void MImgCleanStd::CreateGuiElements(MGGroupFrame *f)
322{
323 //
324 // Create a frame for line 3 and 4 to be able
325 // to align entry field and label in one line
326 //
327 TGHorizontalFrame *f1 = new TGHorizontalFrame(f, 0, 0);
328 TGHorizontalFrame *f2 = new TGHorizontalFrame(f, 0, 0);
329
330 /*
331 * --> use with root >=3.02 <--
332 *
333
334 TGNumberEntry *fNumEntry1 = new TGNumberEntry(frame, 3.0, 2, M_NENT_LVL1, kNESRealOne, kNEANonNegative);
335 TGNumberEntry *fNumEntry2 = new TGNumberEntry(frame, 2.5, 2, M_NENT_LVL1, kNESRealOne, kNEANonNegative);
336
337 */
338 TGTextEntry *entry1 = new TGTextEntry(f1, "****", kImgCleanLvl1);
339 TGTextEntry *entry2 = new TGTextEntry(f2, "****", kImgCleanLvl2);
340
341 // --- doesn't work like expected (until root 3.02?) --- fNumEntry1->SetAlignment(kTextRight);
342 // --- doesn't work like expected (until root 3.02?) --- fNumEntry2->SetAlignment(kTextRight);
343
344 entry1->SetText("3.0");
345 entry2->SetText("2.5");
346
347 entry1->Associate(f);
348 entry2->Associate(f);
349
350 TGLabel *l1 = new TGLabel(f1, "Cleaning Level 1");
351 TGLabel *l2 = new TGLabel(f2, "Cleaning Level 2");
352
353 l1->SetTextJustify(kTextLeft);
354 l2->SetTextJustify(kTextLeft);
355
356 //
357 // Align the text of the label centered, left in the row
358 // with a left padding of 10
359 //
360 TGLayoutHints *laylabel = new TGLayoutHints(kLHintsCenterY|kLHintsLeft, 10);
361 TGLayoutHints *layframe = new TGLayoutHints(kLHintsCenterY|kLHintsLeft, 5, 0, 10);
362
363 //
364 // Add one entry field and the corresponding label to each line
365 //
366 f1->AddFrame(entry1);
367 f2->AddFrame(entry2);
368
369 f1->AddFrame(l1, laylabel);
370 f2->AddFrame(l2, laylabel);
371
372 f->AddFrame(f1, layframe);
373 f->AddFrame(f2, layframe);
374
375 f->AddToList(entry1);
376 f->AddToList(entry2);
377 f->AddToList(l1);
378 f->AddToList(l2);
379 f->AddToList(laylabel);
380 f->AddToList(layframe);
381}
382
383// --------------------------------------------------------------------------
384//
385// Process the GUI Events comming from the two text entry fields.
386//
387Bool_t MImgCleanStd::ProcessMessage(Int_t msg, Int_t submsg, Long_t param1, Long_t param2)
388{
389 if (msg!=kC_TEXTENTRY || submsg!=kTE_ENTER)
390 return kTRUE;
391
392 TGTextEntry *txt = (TGTextEntry*)FindWidget(param1);
393
394 if (!txt)
395 return kTRUE;
396
397 Float_t lvl = atof(txt->GetText());
398
399 switch (param1)
400 {
401 case kImgCleanLvl1:
402 fCleanLvl1 = lvl;
403 *fLog << "Cleaning level 1 set to " << lvl << " sigma." << endl;
404 return kTRUE;
405
406 case kImgCleanLvl2:
407 fCleanLvl2 = lvl;
408 *fLog << "Cleaning level 2 set to " << lvl << " sigma." << endl;
409 return kTRUE;
410 }
411
412 return kTRUE;
413}
414
415// --------------------------------------------------------------------------
416//
417// Implementation of SavePrimitive. Used to write the call to a constructor
418// to a macro. In the original root implementation it is used to write
419// gui elements to a macro-file.
420//
421void MImgCleanStd::StreamPrimitive(ofstream &out) const
422{
423 out << " MImgCleanStd " << GetUniqueName() << "(";
424 out << fCleanLvl1 << ", " << fCleanLvl2;
425
426 if (fName!=gsDefName || fTitle!=gsDefTitle)
427 {
428 out << ", \"" << fName << "\"";
429 if (fTitle!=gsDefTitle)
430 out << ", \"" << fTitle << "\"";
431 }
432 out << ");" << endl;
433}
Note: See TracBrowser for help on using the repository browser.