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

Last change on this file since 1487 was 1487, checked in by tbretz, 22 years ago
*** empty log message ***
  • Property svn:executable set to *
File size: 12.3 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
128//
129// takes the maximum pixel id from CleanStep1 as an argument
130//
131void MImgCleanStd::CleanStep2(Int_t max)
132{
133 const Int_t entries = fEvt->GetNumPixels();
134
135 //
136 // In the worst case we have to loop 6 times 577 times, to
137 // catch the behaviour of all next neighbors. Here we can gain
138 // much by using an array instead of checking through all pixels
139 // (MCerPhotEvt::IsPixelUsed) all the time.
140 //
141 Byte_t *ispixused = new Byte_t[max+1];
142
143 for (Int_t i=0; i<entries; i++)
144 {
145 MCerPhotPix &pix = (*fEvt)[i];
146 ispixused[pix.GetPixId()] = pix.IsPixelUsed();
147 }
148
149 for (Int_t i=0; i<entries; i++)
150 {
151 //
152 // get entry i from list
153 //
154 MCerPhotPix &pix = (*fEvt)[i];
155
156 //
157 // check if pixel is in use, if not goto next pixel in list
158 //
159 if (!pix.IsPixelUsed())
160 continue;
161
162 //
163 // get pixel id of this entry
164 //
165 const Int_t id = pix.GetPixId();
166
167 //
168 // count number of next neighbors of this pixel which
169 // state is 'used'
170 //
171 const MGeomPix &gpix = (*fCam)[id];
172 const Int_t nnmax = gpix.GetNumNeighbors();
173
174 Bool_t cnt = kFALSE;
175 for (Int_t j=0; j<nnmax; j++)
176 {
177 const Int_t id2 = gpix.GetNeighbor(j);
178
179 if (!ispixused[id2])
180 continue;
181
182 cnt = kTRUE;
183 break;
184 }
185 if (cnt)
186 continue;
187
188 //
189 // check if no next neighbor has the state 'used'
190 // set this pixel to 'unused', too.
191 //
192 pix.SetPixelUnused();
193 }
194
195 delete ispixused;
196
197 //
198 // now we declare all pixels that survive as CorePixels
199 //
200 for (Int_t i=0; i<entries; i++)
201 {
202 MCerPhotPix &pix = (*fEvt)[i];
203
204 if (pix.IsPixelUsed())
205 pix.SetPixelCore();
206 }
207}
208
209// --------------------------------------------------------------------------
210//
211// Look for the boundary pixels around the core pixels
212// if a pixel has more than 2.5 (clean level 2.5) sigma, and
213// a core neigbor it is declared as used.
214//
215void MImgCleanStd::CleanStep3()
216{
217 const Int_t entries = fEvt->GetNumPixels();
218
219 for (Int_t i=0; i<entries; i++)
220 {
221 //
222 // get pixel as entry il from list
223 //
224 MCerPhotPix &pix = (*fEvt)[i];
225
226 //
227 // if pixel is a core pixel go to the next pixel
228 //
229 if (pix.IsPixelCore())
230 continue;
231
232 //
233 // check the num of photons against the noise level
234 //
235 const Float_t entry = pix.GetNumPhotons();
236 const Float_t noise = pix.GetErrorPhot();
237
238 if (entry <= fCleanLvl2 * noise )
239 continue;
240
241 //
242 // get pixel id of this entry
243 //
244 const Int_t id = pix.GetPixId();
245
246 //
247 // check if the pixel's next neighbor is a core pixel.
248 // if it is a core pixel set pixel state to: used.
249 //
250 MGeomPix &gpix = (*fCam)[id];
251 const Int_t nnmax = gpix.GetNumNeighbors();
252
253 for (Int_t j=0; j<nnmax; j++)
254 {
255 const Int_t id2 = gpix.GetNeighbor(j);
256
257 if (!fEvt->IsPixelCore(id2))
258 continue;
259
260 pix.SetPixelUsed();
261 break;
262 }
263 }
264}
265
266// --------------------------------------------------------------------------
267//
268// check if MEvtHeader exists in the Parameter list already.
269// if not create one and add them to the list
270//
271Bool_t MImgCleanStd::PreProcess (MParList *pList)
272{
273 fCam = (MGeomCam*)pList->FindObject("MGeomCam");
274 if (!fCam)
275 {
276 *fLog << dbginf << "MGeomCam not found (no geometry information available)... aborting." << endl;
277 return kFALSE;
278 }
279
280 fEvt = (MCerPhotEvt*)pList->FindObject("MCerPhotEvt");
281 if (!fEvt)
282 {
283 *fLog << dbginf << "MCerPhotEvt not found... aborting." << endl;
284 return kFALSE;
285 }
286
287 return kTRUE;
288}
289
290
291// --------------------------------------------------------------------------
292//
293// Cleans the image.
294//
295Bool_t MImgCleanStd::Process()
296{
297 const Int_t max = CleanStep1();
298 CleanStep2(max);
299 CleanStep3();
300
301 return kTRUE;
302}
303
304// --------------------------------------------------------------------------
305//
306// Print descriptor and cleaning levels.
307//
308void MImgCleanStd::Print(Option_t *o) const
309{
310 *fLog << GetDescriptor() << " initialized with noise level ";
311 *fLog << fCleanLvl1 << " and " << fCleanLvl2 << endl;
312}
313
314// --------------------------------------------------------------------------
315//
316// Craete two text entry fields, one for each cleaning level and a
317// describing text line.
318//
319void MImgCleanStd::CreateGuiElements(MGGroupFrame *f)
320{
321 //
322 // Create a frame for line 3 and 4 to be able
323 // to align entry field and label in one line
324 //
325 TGHorizontalFrame *f1 = new TGHorizontalFrame(f, 0, 0);
326 TGHorizontalFrame *f2 = new TGHorizontalFrame(f, 0, 0);
327
328 /*
329 * --> use with root >=3.02 <--
330 *
331
332 TGNumberEntry *fNumEntry1 = new TGNumberEntry(frame, 3.0, 2, M_NENT_LVL1, kNESRealOne, kNEANonNegative);
333 TGNumberEntry *fNumEntry2 = new TGNumberEntry(frame, 2.5, 2, M_NENT_LVL1, kNESRealOne, kNEANonNegative);
334
335 */
336 TGTextEntry *entry1 = new TGTextEntry(f1, "****", kImgCleanLvl1);
337 TGTextEntry *entry2 = new TGTextEntry(f2, "****", kImgCleanLvl2);
338
339 // --- doesn't work like expected (until root 3.02?) --- fNumEntry1->SetAlignment(kTextRight);
340 // --- doesn't work like expected (until root 3.02?) --- fNumEntry2->SetAlignment(kTextRight);
341
342 entry1->SetText("3.0");
343 entry2->SetText("2.5");
344
345 entry1->Associate(f);
346 entry2->Associate(f);
347
348 TGLabel *l1 = new TGLabel(f1, "Cleaning Level 1");
349 TGLabel *l2 = new TGLabel(f2, "Cleaning Level 2");
350
351 l1->SetTextJustify(kTextLeft);
352 l2->SetTextJustify(kTextLeft);
353
354 //
355 // Align the text of the label centered, left in the row
356 // with a left padding of 10
357 //
358 TGLayoutHints *laylabel = new TGLayoutHints(kLHintsCenterY|kLHintsLeft, 10);
359 TGLayoutHints *layframe = new TGLayoutHints(kLHintsCenterY|kLHintsLeft, 5, 0, 10);
360
361 //
362 // Add one entry field and the corresponding label to each line
363 //
364 f1->AddFrame(entry1);
365 f2->AddFrame(entry2);
366
367 f1->AddFrame(l1, laylabel);
368 f2->AddFrame(l2, laylabel);
369
370 f->AddFrame(f1, layframe);
371 f->AddFrame(f2, layframe);
372
373 f->AddToList(entry1);
374 f->AddToList(entry2);
375 f->AddToList(l1);
376 f->AddToList(l2);
377 f->AddToList(laylabel);
378 f->AddToList(layframe);
379}
380
381// --------------------------------------------------------------------------
382//
383// Process the GUI Events comming from the two text entry fields.
384//
385Bool_t MImgCleanStd::ProcessMessage(Int_t msg, Int_t submsg, Long_t param1, Long_t param2)
386{
387 if (msg!=kC_TEXTENTRY || submsg!=kTE_ENTER)
388 return kTRUE;
389
390 TGTextEntry *txt = (TGTextEntry*)FindWidget(param1);
391
392 if (!txt)
393 return kTRUE;
394
395 Float_t lvl = atof(txt->GetText());
396
397 switch (param1)
398 {
399 case kImgCleanLvl1:
400 fCleanLvl1 = lvl;
401 *fLog << "Cleaning level 1 set to " << lvl << " sigma." << endl;
402 return kTRUE;
403
404 case kImgCleanLvl2:
405 fCleanLvl2 = lvl;
406 *fLog << "Cleaning level 2 set to " << lvl << " sigma." << endl;
407 return kTRUE;
408 }
409
410 return kTRUE;
411}
412
413// --------------------------------------------------------------------------
414//
415// Implementation of SavePrimitive. Used to write the call to a constructor
416// to a macro. In the original root implementation it is used to write
417// gui elements to a macro-file.
418//
419void MImgCleanStd::StreamPrimitive(ofstream &out) const
420{
421 out << " MImgCleanStd " << GetUniqueName() << "(";
422 out << fCleanLvl1 << ", " << fCleanLvl2;
423
424 if (fName!=gsDefName || fTitle!=gsDefTitle)
425 {
426 out << ", \"" << fName << "\"";
427 if (fTitle!=gsDefTitle)
428 out << ", \"" << fTitle << "\"";
429 }
430 out << ");" << endl;
431}
Note: See TracBrowser for help on using the repository browser.