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

Last change on this file since 2381 was 2377, checked in by tbretz, 21 years ago
*** empty log message ***
File size: 20.9 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// The Image Cleaning task selects the pixels you use for the Hillas
32// parameters calculation.
33//
34// There are two methods to make the selection: the standard one, as done
35// in the analysis of CT1 data, and the democratic one, as suggested by
36// W.Wittek. The number of photo-electrons of a pixel is compared with the
37// pedestal RMS of the pixel itself (standard method) or with the average
38// RMS of the inner pixels (democratic method).
39// In both cases, the possibility to have a camera with pixels of
40// different area is taken into account.
41// The too noisy pixels can be recognized and eventally switched off
42// (Unmap: set blind pixels to UNUSED) separately, using the
43// MBlindPixelCalc Class. In the MBlindPixelCalc class there is also the
44// function to replace the value of the noisy pixels with the interpolation
45// of the content of the neighbors (SetUseInterpolation).
46//
47// Example:
48// ...
49// MBlindPixelCalc blind;
50// blind.SetUseInterpolation();
51// blind.SetUseBlindPixels();
52//
53// MImgCleanTGB clean;
54// ...
55// tlist.AddToList(&blind);
56// tlist.AddToList(&clean);
57//
58// Look at the MBlindPixelCalc Class for more details.
59//
60// Starting point: default values ----------------------------------------
61//
62// When an event is read, before the image cleaning, all the pixels that
63// are in MCerPhotEvt are set as USED and NOT CORE. All the pixels belong
64// to RING number 1 (like USED pixels).
65// Look at MCerPhotPix.h to see how these informations of the pixel are
66// stored.
67// The default cleaning METHOD is the STANDARD one and the number of the
68// rings around the CORE pixel it analyzes is 1. Look at the Constructor
69// of the class in MImgCleanTGB.cc to see (or change) the default values.
70//
71// Example: To modify this setting, use the member functions
72// SetMethod(MImgCleanTGB::kDemocratic) and SetCleanRings(UShort_t n).
73//
74// MImgCleanTGB:CleanStep1 -----------------------------------------------
75//
76// The first step of cleaning defines the CORE pixels. The CORE pixels are
77// the ones which contain the informations about the core of the electro-
78// magnetic shower.
79// The ratio (A_0/A_i) is calculated from fCam->GetPixRatio(i). A_0 is
80// the area of the central pixel of the camera, A_i is the area of the
81// examined pixel. In this way, if we have a MAGIC-like camera, with the
82// outer pixels bigger than the inner ones, the level of cleaning in the
83// two different regions is weighted.
84// This avoids problems of deformations of the shower images.
85// The signal S_i and the pedestal RMS Prms_i of the pixel are called from
86// the object MCerPhotPix.
87// If (default method = kStandard)
88//Begin_Html
89//&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<img src="images/MImgCleanTGB-f1.png">
90//End_Html
91// the pixel is set as CORE pixel. L_1 (n=1) is called "first level of
92// cleaning" (default: fCleanLvl1 = 3).
93// All the other pixels are set as UNUSED and belong to RING 0.
94// After this point, only the CORE pixels are set as USED, with RING
95// number 1.
96//
97// MImgCleanTGB:CleanStep2 ----------------------------------------------
98//
99// The second step of cleaning looks at the isolated CORE pixels and sets
100// them to UNUSED. An isolated pixel is a pixel without CORE neighbors.
101// At the end of this point, we have set as USED only CORE pixels with at
102// least one CORE neighbor.
103//
104// MImgCleanTGB:CleanStep3 ----------------------------------------------
105//
106// The third step of cleaning looks at all the pixels (USED or UNUSED) that
107// surround the USED pixels.
108// If the content of the analyzed pixel survives at the second level of
109// cleaning, i.e. if
110//Begin_Html
111//&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<img src="images/MImgCleanTGB-f1.png">
112//End_Html
113// the pixel is set as USED. L_2 (n=2) is called "second level of cleaning"
114// (default:fCleanLvl2 = 2.5).
115//
116// When the number of RINGS to analyze is 1 (default value), only the
117// pixels that have a neighbor CORE pixel are analyzed.
118//
119// There is the option to decide the number of times you want to repeat
120// this procedure (number of RINGS analyzed around the core pixels = n).
121// Every time the level of cleaning is the same (fCleanLvl2) and the pixel
122// will belong to ring r+1, 1 < r < n+1. This is described in
123// MImgCleanTGB:CleanStep4 .
124//
125// Dictionary and member functions ---------------------------------------
126//
127// Here there is the detailed description of the member functions and of
128// the terms commonly used in the class.
129//
130// STANDARD CLEANING:
131// =================
132// This is the method used for the CT1 data analysis. It is the default
133// method of the class.
134// The number of photo-electrons of a pixel (S_i) is compared to the
135// pedestal RMS of the pixel itself (Prms_i). To have the comparison to
136// the same photon density for all the pixels, taking into account they
137// can have different areas, we have to keep in mind that the number of
138// photons that hit each pixel, goes linearly with the area of the pixel.
139// The fluctuations of the LONS are proportional to sqrt(A_i), so when we
140// compare S_i with Prms_i, only a factor sqrt(A_0/A_i) is missing to
141// have the same (N.photons/Area) threshold for all the pixels.
142//
143// !!WARNING: if noise independent from the
144// pixel size (example: electronic noise) is introduced,
145// then the noise fluctuations are no longer proportional
146// to sqrt(A_i), and then the cut value (for a camera with
147// pixels of different sizes) resulting from the above
148// procedure would not be proportional to pixel size as we
149// intend. In that case, democratic cleaning is preferred.
150//
151// If
152//Begin_Html
153//&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<img src="images/MImgCleanTGB-f1.png">
154//End_Html
155// the pixel survives the cleaning and it is set as CORE (when L_n is the
156// first level of cleaning, fCleanLvl1) or USED (when L_n is the second
157// level of cleaning, fCleanLvl2).
158//
159// Example:
160//
161// MImgCleanTGB clean;
162// //creates a default Cleaning object, with default setting
163// ...
164// tlist.AddToList(&clean);
165// // add the image cleaning to the main task list
166//
167// DEMOCRATIC CLEANING:
168// ===================
169// You use this cleaning method when you want to compare the number of
170// photo-electons of each pixel with the average pedestal RMS
171// (fInnerNoise = fSgb->GetSigmabarInner()) of the inner pixels (for the
172// MAGIC camera they are the smaller ones):
173//Begin_Html
174//&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<img src="images/MImgCleanTGB-f2.png">
175//End_Html
176// In this case, the simple ratio (A_0/A_i) is used to weight the level of
177// cleaning, because both the inner and the outer pixels (that in MAGIC
178// have a different area) are compared to the same pedestal RMS, coming
179// from the inner pixels.
180// To calculate the average pedestal RMS of the inner pixels, you have to
181// add to the main task list an object of type MSigmabarCalc before the
182// MImgCleanTGB object. To know how the calculation of fInnerNoise is done
183// look at the MSigmabarCalc Class.
184//
185// Example:
186//
187// MSigmabarCalc sbcalc;
188// //creates an object that calcutates the average pedestal RMS
189// MImgCleanTGB clean;
190// ...
191// tlist.AddToList(&sbcalc);
192// tlist.AddToList(&clean);
193//
194// Member Function: SetMethod()
195// ============================
196// When you call the MImgCleanTGB task, the default method is kStandard.
197//
198// If you want to switch to the kDemocratic method you have to
199// call this member function.
200//
201// Example:
202//
203// MImgCleanTGB clean;
204// //creates a default Cleaning object, with default setting
205//
206// clean.SetMethod(MImgCleanTGB::kDemocratic);
207// //now the method of cleaning is changed to Democratic
208//
209// FIRST AND SECOND CLEANING LEVEL
210// ===============================
211// When you call the MImgCleanTGB task, the default cleaning levels are
212// fCleanLvl1 = 3, fCleanLvl2 = 2.5. You can change them easily when you
213// create the MImgCleanTGB object.
214//
215// Example:
216//
217// MImgCleanTGB clean(Float_t lvl1,Float_t lvl2);
218// //creates a default cleaning object, but the cleaning levels are now
219// //lvl1 and lvl2.
220//
221// RING NUMBER
222// ===========
223// The standard cleaning procedure is such that it looks for the
224// informations of the boundary part of the shower only on the first
225// neighbors of the CORE pixels.
226// There is the possibility now to look not only at the firs neighbors
227// (first ring),but also further away, around the CORE pixels. All the new
228// pixels you can find with this method, are tested with the second level
229// of cleaning and have to have at least an USED neighbor.
230//
231// They will be also set as USED and will be taken into account during the
232// calculation of the image parameters.
233// The only way to distinguish them from the other USED pixels, is the
234// Ring number, that is bigger than 1.
235//
236// Example: You can decide how many rings you want to analyze using:
237//
238// MImgCleanTGB clean;
239// //creates a default cleaning object (default number of rings =1)
240// clean.SetCleanRings(UShort_t r);
241// //now it looks r times around the CORE pixels to find new pixels with
242// //signal.
243//
244//
245// Input Containers:
246// MGeomCam, MCerPhotEvt, MSigmabar
247//
248// Output Containers:
249// MCerPhotEvt
250//
251/////////////////////////////////////////////////////////////////////////////
252#include "MImgCleanTGB.h"
253
254#include <stdlib.h> // atof
255#include <fstream> // ofstream, SavePrimitive
256
257#include <TGFrame.h> // TGFrame
258#include <TGLabel.h> // TGLabel
259#include <TArrayC.h> // TArrayC
260#include <TGTextEntry.h> // TGTextEntry
261
262#include "MLog.h"
263#include "MLogManip.h"
264
265#include "MParList.h"
266#include "MSigmabar.h"
267
268#include "MGeomPix.h"
269#include "MGeomCam.h"
270
271#include "MCerPhotPix.h"
272#include "MCerPhotEvt.h"
273
274#include "MPedestalPix.h"
275#include "MPedestalCam.h"
276
277#include "MGGroupFrame.h" // MGGroupFrame
278
279ClassImp(MImgCleanTGB);
280
281using namespace std;
282
283enum {
284 kImgCleanLvl1,
285 kImgCleanLvl2
286};
287
288static const TString gsDefName = "MImgCleanTGB";
289static const TString gsDefTitle = "Task to perform image cleaning";
290
291// --------------------------------------------------------------------------
292//
293// Default constructor. Here you can specify the cleaning method and levels.
294// If you don't specify them the 'common standard' values 3.0 and 2.5 (sigma
295// above mean) are used.
296// Here you can also specify how many rings around the core pixels you want
297// to analyze (with the fixed lvl2). The default value for "rings" is 1.
298//
299MImgCleanTGB::MImgCleanTGB(const Float_t lvl1, const Float_t lvl2,
300 const char *name, const char *title)
301 : fSgb(NULL), fCleaningMethod(kStandard), fCleanLvl1(lvl1),
302 fCleanLvl2(lvl2), fCleanRings(1)
303
304{
305 fName = name ? name : gsDefName.Data();
306 fTitle = title ? title : gsDefTitle.Data();
307
308 Print();
309}
310
311
312Int_t MImgCleanTGB::CleanStep3b(MCerPhotPix &pix)
313{
314 const Int_t id = pix.GetPixId();
315
316 //
317 // check if the pixel's next neighbor is a core pixel.
318 // if it is a core pixel set pixel state to: used.
319 //
320 MGeomPix &gpix = (*fCam)[id];
321 const Int_t nnmax = gpix.GetNumNeighbors();
322
323 Int_t rc = 0;
324
325 for (Int_t j=0; j<nnmax; j++)
326 {
327 const Int_t id2 = gpix.GetNeighbor(j);
328
329 if (fEvt->GetPixById(id2) && fEvt->IsPixelUsed(id2))
330 rc++;
331 }
332 return rc;
333}
334
335// --------------------------------------------------------------------------
336//
337// Look for the boundary pixels around the core pixels
338// if a pixel has more than 2.5 (clean level 2.5) sigma, and
339// a core neigbor, it is declared as used.
340//
341void MImgCleanTGB::CleanStep3(Int_t num1, Int_t num2)
342{
343 const Int_t entries = fEvt->GetNumPixels();
344
345 Int_t *u = new Int_t[entries];
346
347 for (Int_t i=0; i<entries; i++)
348 {
349 //
350 // get pixel as entry il from list
351 //
352 MCerPhotPix &pix = (*fEvt)[i];
353 u[i] = CleanStep3b(pix);
354 }
355
356 for (Int_t i=0; i<entries; i++)
357 {
358 MCerPhotPix &pix = (*fEvt)[i];
359 if (u[i]<num1)
360 pix.SetPixelUnused();
361 if (u[i]>num2)
362 pix.SetPixelUsed();
363 }
364
365 delete u;
366}
367
368void MImgCleanTGB::CleanStep3(Byte_t *nb, Int_t num1, Int_t num2)
369{
370 const Int_t entries = fEvt->GetNumPixels();
371
372 for (Int_t i=0; i<entries; i++)
373 {
374 MCerPhotPix &pix = (*fEvt)[i];
375
376 const Int_t idx = pix.GetPixId();
377
378 if (nb[idx]<num1 && pix.IsPixelUsed())
379 {
380 const MGeomPix &gpix = (*fCam)[idx];
381 const Int_t nnmax = gpix.GetNumNeighbors();
382 for (Int_t j=0; j<nnmax; j++)
383 nb[gpix.GetNeighbor(j)]--;
384
385 pix.SetPixelUnused();
386 }
387 }
388
389 for (Int_t i=0; i<entries; i++)
390 {
391 MCerPhotPix &pix = (*fEvt)[i];
392
393 const Int_t idx = pix.GetPixId();
394 if (nb[idx]>num2 && !pix.IsPixelUsed())
395 {
396 const MGeomPix &gpix = (*fCam)[idx];
397 const Int_t nnmax = gpix.GetNumNeighbors();
398 for (Int_t j=0; j<nnmax; j++)
399 nb[gpix.GetNeighbor(j)]++;
400
401 pix.SetPixelUsed();
402 }
403 }
404}
405
406// --------------------------------------------------------------------------
407//
408// Check if MEvtHeader exists in the Parameter list already.
409// if not create one and add them to the list
410//
411Int_t MImgCleanTGB::PreProcess (MParList *pList)
412{
413 fCam = (MGeomCam*)pList->FindObject("MGeomCam");
414 if (!fCam)
415 {
416 *fLog << dbginf << "MGeomCam not found (no geometry information available)... aborting." << endl;
417 return kFALSE;
418 }
419
420 fEvt = (MCerPhotEvt*)pList->FindObject("MCerPhotEvt");
421 if (!fEvt)
422 {
423 *fLog << dbginf << "MCerPhotEvt not found... aborting." << endl;
424 return kFALSE;
425 }
426
427 if (fCleaningMethod == kDemocratic)
428 {
429 fSgb = (MSigmabar*)pList->FindObject("MSigmabar");
430 if (!fSgb)
431 {
432 *fLog << dbginf << "MSigmabar not found... aborting." << endl;
433 return kFALSE;
434 }
435 }
436 else
437 {
438 fPed = (MPedestalCam*)pList->FindObject("MPedestalCam");
439 if (!fPed)
440 {
441 *fLog << dbginf << "MPedestalCam not found... aborting." << endl;
442 return kFALSE;
443 }
444 }
445
446 return kTRUE;
447}
448
449// --------------------------------------------------------------------------
450//
451// Cleans the image.
452//
453Int_t MImgCleanTGB::Process()
454{
455 const Int_t entries = fEvt->GetNumPixels();
456
457 Double_t sum = 0;
458 Double_t sq = 0;
459 Double_t w = 0;
460 Double_t w2 = 0;
461 for (Int_t i=0; i<entries; i++)
462 {
463 //
464 // get pixel as entry il from list
465 //
466 MCerPhotPix &pix = (*fEvt)[i];
467
468 const Double_t entry = pix.GetNumPhotons();
469 const Double_t factor = fCam->GetPixRatio(pix.GetPixId());
470 const Float_t noise = (*fPed)[pix.GetPixId()].GetPedestalRms();
471
472 if (entry * TMath::Sqrt(factor) <= fCleanLvl2 * noise)
473 {
474 sum += entry*factor;
475 sq += entry*entry*factor*factor;
476 w += factor;
477 w2 += factor*factor;
478 }
479 }
480
481 Double_t mean = sum/w;
482 Double_t sdev = sqrt(sq/w2 - mean*mean);
483
484 TArrayC n(fCam->GetNumPixels());
485 Byte_t *nb = (Byte_t*)n.GetArray();
486 //Byte_t *nb = new Byte_t[1000];
487 //memset(nb, 0, 577);
488
489 for (Int_t i=0; i<entries; i++)
490 {
491 //
492 // get pixel as entry il from list
493 //
494 MCerPhotPix &pix = (*fEvt)[i];
495 const Int_t idx = pix.GetPixId();
496
497 const Double_t entry = pix.GetNumPhotons();
498 const Double_t factor = fCam->GetPixRatio(idx);
499
500 if (entry*factor > fCleanLvl1*sdev)
501 {
502 pix.SetPixelUsed();
503
504 const MGeomPix &gpix = (*fCam)[idx];
505 const Int_t nnmax = gpix.GetNumNeighbors();
506 for (Int_t j=0; j<nnmax; j++)
507 nb[gpix.GetNeighbor(j)]++;
508 }
509 else
510 pix.SetPixelUnused();
511 }
512
513 CleanStep3(nb, 2, 3);
514 //CleanStep3(nb, 2, 3);
515 //CleanStep3(nb, 2, 3);
516
517 return kTRUE;
518}
519
520// --------------------------------------------------------------------------
521//
522// Print descriptor and cleaning levels.
523//
524void MImgCleanTGB::Print(Option_t *o) const
525{
526 *fLog << all << GetDescriptor() << " using ";
527 switch (fCleaningMethod)
528 {
529 case kDemocratic:
530 *fLog << "democratic";
531 break;
532 case kStandard:
533 *fLog << "standard";
534 break;
535 }
536 *fLog << " cleaning initialized with noise level " << fCleanLvl1 << " and " << fCleanLvl2;
537 *fLog << " (CleanRings=" << fCleanRings << ")" << endl;
538}
539
540// --------------------------------------------------------------------------
541//
542// Create two text entry fields, one for each cleaning level and a
543// describing text line.
544//
545void MImgCleanTGB::CreateGuiElements(MGGroupFrame *f)
546{
547 //
548 // Create a frame for line 3 and 4 to be able
549 // to align entry field and label in one line
550 //
551 TGHorizontalFrame *f1 = new TGHorizontalFrame(f, 0, 0);
552 TGHorizontalFrame *f2 = new TGHorizontalFrame(f, 0, 0);
553
554 /*
555 * --> use with root >=3.02 <--
556 *
557
558 TGNumberEntry *fNumEntry1 = new TGNumberEntry(frame, 3.0, 2, M_NENT_LVL1, kNESRealOne, kNEANonNegative);
559 TGNumberEntry *fNumEntry2 = new TGNumberEntry(frame, 2.5, 2, M_NENT_LVL1, kNESRealOne, kNEANonNegative);
560
561 */
562 TGTextEntry *entry1 = new TGTextEntry(f1, "****", kImgCleanLvl1);
563 TGTextEntry *entry2 = new TGTextEntry(f2, "****", kImgCleanLvl2);
564
565 // --- doesn't work like expected (until root 3.02?) --- fNumEntry1->SetAlignment(kTextRight);
566 // --- doesn't work like expected (until root 3.02?) --- fNumEntry2->SetAlignment(kTextRight);
567
568 entry1->SetText("3.0");
569 entry2->SetText("2.5");
570
571 entry1->Associate(f);
572 entry2->Associate(f);
573
574 TGLabel *l1 = new TGLabel(f1, "Cleaning Level 1");
575 TGLabel *l2 = new TGLabel(f2, "Cleaning Level 2");
576
577 l1->SetTextJustify(kTextLeft);
578 l2->SetTextJustify(kTextLeft);
579
580 //
581 // Align the text of the label centered, left in the row
582 // with a left padding of 10
583 //
584 TGLayoutHints *laylabel = new TGLayoutHints(kLHintsCenterY|kLHintsLeft, 10);
585 TGLayoutHints *layframe = new TGLayoutHints(kLHintsCenterY|kLHintsLeft, 5, 0, 10);
586
587 //
588 // Add one entry field and the corresponding label to each line
589 //
590 f1->AddFrame(entry1);
591 f2->AddFrame(entry2);
592
593 f1->AddFrame(l1, laylabel);
594 f2->AddFrame(l2, laylabel);
595
596 f->AddFrame(f1, layframe);
597 f->AddFrame(f2, layframe);
598
599 f->AddToList(entry1);
600 f->AddToList(entry2);
601 f->AddToList(l1);
602 f->AddToList(l2);
603 f->AddToList(laylabel);
604 f->AddToList(layframe);
605}
606
607// --------------------------------------------------------------------------
608//
609// Process the GUI Events comming from the two text entry fields.
610//
611Bool_t MImgCleanTGB::ProcessMessage(Int_t msg, Int_t submsg, Long_t param1, Long_t param2)
612{
613 if (msg!=kC_TEXTENTRY || submsg!=kTE_ENTER)
614 return kTRUE;
615
616 TGTextEntry *txt = (TGTextEntry*)FindWidget(param1);
617
618 if (!txt)
619 return kTRUE;
620
621 Float_t lvl = atof(txt->GetText());
622
623 switch (param1)
624 {
625 case kImgCleanLvl1:
626 fCleanLvl1 = lvl;
627 *fLog << "Cleaning level 1 set to " << lvl << " sigma." << endl;
628 return kTRUE;
629
630 case kImgCleanLvl2:
631 fCleanLvl2 = lvl;
632 *fLog << "Cleaning level 2 set to " << lvl << " sigma." << endl;
633 return kTRUE;
634 }
635
636 return kTRUE;
637}
638
639// --------------------------------------------------------------------------
640//
641// Implementation of SavePrimitive. Used to write the call to a constructor
642// to a macro. In the original root implementation it is used to write
643// gui elements to a macro-file.
644//
645void MImgCleanTGB::StreamPrimitive(ofstream &out) const
646{
647 out << " MImgCleanTGB " << GetUniqueName() << "(";
648 out << fCleanLvl1 << ", " << fCleanLvl2;
649
650 if (fName!=gsDefName || fTitle!=gsDefTitle)
651 {
652 out << ", \"" << fName << "\"";
653 if (fTitle!=gsDefTitle)
654 out << ", \"" << fTitle << "\"";
655 }
656 out << ");" << endl;
657
658 if (fCleaningMethod!=kDemocratic)
659 return;
660
661 out << " " << GetUniqueName() << ".SetMethod(MImgCleanTGB::kDemocratic);" << endl;
662
663 if (fCleanRings==1)
664 return;
665
666 out << " " << GetUniqueName() << ".SetCleanRings(" << fCleanRings << ");" << endl;
667}
Note: See TracBrowser for help on using the repository browser.