source: trunk/MagicSoft/Mars/mbadpixels/MBadPixelsTreat.cc@ 4603

Last change on this file since 4603 was 4586, checked in by hbartko, 21 years ago
*** empty log message ***
File size: 16.0 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): Oscar Blanch 12/2001 <mailto:blanch@ifae.es>
19! Author(s): Thomas Bretz 08/2002 <mailto:tbretz@astro.uni-wuerzburg.de>
20!
21! Copyright: MAGIC Software Development, 2000-2004
22!
23!
24\* ======================================================================== */
25
26/////////////////////////////////////////////////////////////////////////////
27//
28// MBadPixelsTreat
29//
30// You can use MBadPixelsTreat::SetUseInterpolation to replaced the
31// bad pixels by the average of its neighbors instead of unmapping
32// them. If you want to include the central pixel use
33// MBadPixelsTreat::SetUseCentralPixel. The bad pixels are taken from
34// an existing MBadPixelsCam.
35//
36// It check if there are enough neighbors to calculate the mean
37// If not, unmap the pixel. The minimum number of good neighbors
38// should be set using SetNumMinNeighbors
39//
40// If you want to interpolate unreliable pixels and unsuitable
41// (broken) pixels use SetHardTreatment().
42//
43//
44// Input Containers:
45// MCerPhotEvt
46// MPedPhotCam
47// MBadPixelsCam
48// [MGeomCam]
49//
50// Output Containers:
51// MCerPhotEvt
52//
53/////////////////////////////////////////////////////////////////////////////
54#include "MBadPixelsTreat.h"
55
56#include <fstream>
57
58#include <TArrayD.h>
59
60#include "MLog.h"
61#include "MLogManip.h"
62
63#include "MParList.h"
64
65#include "MGeomPix.h"
66#include "MGeomCam.h"
67
68#include "MCerPhotPix.h"
69#include "MCerPhotEvt.h"
70
71#include "MPedPhotPix.h"
72#include "MPedPhotCam.h"
73
74#include "MBadPixelsPix.h"
75#include "MBadPixelsCam.h"
76
77ClassImp(MBadPixelsTreat);
78
79using namespace std;
80
81static const TString gsDefName = "MBadPixelsTreat";
82static const TString gsDefTitle = "Task to treat bad pixels (interpolation, unmapping)";
83
84// --------------------------------------------------------------------------
85//
86// Default constructor.
87//
88MBadPixelsTreat::MBadPixelsTreat(const char *name, const char *title)
89 : fFlags(0), fNumMinNeighbors(3), fNamePedPhotContainer("MPedPhotCam")
90{
91 fName = name ? name : gsDefName.Data();
92 fTitle = title ? title : gsDefTitle.Data();
93}
94
95// --------------------------------------------------------------------------
96//
97// Returns the status of a pixel. If kHardTreatment is set a pixel must
98// be unsuitable or uriliable to be treated. If not it is treated only if
99// it is unsuitable
100// (IsBad() checks for any flag)
101//
102Bool_t MBadPixelsTreat::IsPixelBad(Int_t idx) const
103{
104 return TESTBIT(fFlags, kHardTreatment) ? (*fBadPixels)[idx].IsBad():(*fBadPixels)[idx].IsUnsuitable() ;
105}
106
107// --------------------------------------------------------------------------
108//
109// - Try to find or create MBlindPixels in parameter list.
110// - get the MCerPhotEvt from the parlist (abort if missing)
111// - if no pixels are given by the user try to determin the starfield
112// from the monte carlo run header.
113//
114Int_t MBadPixelsTreat::PreProcess (MParList *pList)
115{
116 fBadPixels = (MBadPixelsCam*)pList->FindObject(AddSerialNumber("MBadPixelsCam"));
117 if (!fBadPixels)
118 {
119 *fLog << err << "MBadPixelsCam not found... aborting." << endl;
120 return kFALSE;
121 }
122
123 fPedPhot = (MPedPhotCam*)pList->FindObject(fNamePedPhotContainer, AddSerialNumber("MPedPhotCam"));
124 if (!fPedPhot)
125 {
126 *fLog << err << "MPedPhotCam not found... aborting." << endl;
127 return kFALSE;
128 }
129
130 fEvt = (MCerPhotEvt*)pList->FindObject(AddSerialNumber("MCerPhotEvt"));
131 if (!fEvt)
132 {
133 *fLog << err << "MCerPhotEvt not found... aborting." << endl;
134 return kFALSE;
135 }
136
137 fGeomCam = (MGeomCam*)pList->FindObject(AddSerialNumber("MGeomCam"));
138 if (!fGeomCam && TESTBIT(fFlags, kUseInterpolation))
139 {
140 *fLog << err << "MGeomCam not found... can't use interpolation." << endl;
141 return kFALSE;
142 }
143
144 return kTRUE;
145}
146
147// --------------------------------------------------------------------------
148//
149// Replaces each pixel (signal, signal error, pedestal, pedestal rms)
150// by the average of its surrounding pixels.
151// If TESTBIT(fFlags, kUseCentralPixel) is set the central pixel is also
152// included.
153//
154void MBadPixelsTreat::InterpolateSignal() const
155{
156 const UShort_t entries = fGeomCam->GetNumPixels();
157
158 //
159 // Create arrays (FIXME: Check if its possible to create it only once)
160 //
161 TArrayD nphot(entries);
162 TArrayD perr(entries);
163
164 //
165 // Loop over all pixels
166 //
167 for (UShort_t i=0; i<entries; i++)
168 {
169 MCerPhotPix *pix = fEvt->GetPixById(i);
170
171 //
172 // Check whether pixel with idx i is blind
173 //
174 if (pix && !IsPixelBad(i))
175 continue;
176
177 //
178 // Get a pointer to this pixel. If it is not yet existing
179 // create a new entry for this pixel in MCerPhotEvt
180 //
181 if (!pix)
182 {
183 pix = fEvt->AddPixel(i, 0, 0);
184 (*fBadPixels)[i].SetUnsuitable(MBadPixelsPix::kUnsuitableEvt);
185 }
186
187 //
188 // Get the corresponding geometry and pedestal
189 //
190 const MGeomPix &gpix = (*fGeomCam)[i];
191
192 // Do Not-Use-Central-Pixel
193 const Bool_t nucp = !TESTBIT(fFlags, kUseCentralPixel);
194
195 Int_t num = nucp ? 0 : 1;
196
197 nphot[i] = nucp ? 0 : pix->GetNumPhotons();
198 perr[i] = nucp ? 0 : Pow2(pix->GetErrorPhot());
199
200 //
201 // The values are rescaled to the small pixels area for the right comparison
202 //
203 const Double_t ratio = fGeomCam->GetPixRatio(i);
204
205 nphot[i] *= ratio;
206 perr[i] *= ratio;
207
208 //
209 // Loop over all its neighbors
210 //
211 const Int_t n = gpix.GetNumNeighbors();
212 for (int j=0; j<n; j++)
213 {
214 const UShort_t nidx = gpix.GetNeighbor(j);
215
216 //
217 // Do not use blind neighbors
218 //
219 if (IsPixelBad(nidx))
220 continue;
221
222 //
223 // Check whether the neighbor has a signal stored
224 //
225 const MCerPhotPix *evtpix = fEvt->GetPixById(nidx);
226 if (!evtpix)
227 continue;
228
229 //
230 // Get the geometry for the neighbor
231 //
232 const Double_t nratio = fGeomCam->GetPixRatio(nidx);
233
234 //
235 //The error is calculated as the quadratic sum of the errors
236 //
237 nphot[i] += nratio*evtpix->GetNumPhotons();
238 perr[i] += nratio* Pow2(evtpix->GetErrorPhot());
239
240 num++;
241 }
242
243 // Check if there are enough neighbors to calculate the mean
244 // If not, unmap the pixel. The maximum number of blind neighbors
245 // should be 2
246 if (num<fNumMinNeighbors)
247 {
248 pix->SetPixelUnmapped();
249 continue;
250 }
251
252 //
253 // Now the mean is calculated and the values rescaled back
254 // to the pixel area
255 //
256 nphot[i] /= (num*ratio);
257 perr[i] = TMath::Sqrt(perr[i]/(num*ratio));
258
259 pix->Set(nphot[i], perr[i]);
260 }
261}
262
263// --------------------------------------------------------------------------
264//
265void MBadPixelsTreat::InterpolatePedestals() const
266{
267 const Int_t entries = fPedPhot->GetSize();
268
269 // Create arrays (FIXME: Check if its possible to create it only once)
270 TArrayD ped(entries);
271 TArrayD rms(entries);
272
273 //
274 // Loop over all pixels
275 //
276 for (UShort_t i=0; i<entries; i++)
277 {
278 //
279 // Check whether pixel with idx i is blind
280 //
281 if (!IsPixelBad(i))
282 continue;
283
284 //
285 // Get the corresponding geometry and pedestal
286 //
287 const MGeomPix &gpix = (*fGeomCam)[i];
288 const MPedPhotPix &ppix = (*fPedPhot)[i];
289
290 // Do Not-Use-Central-Pixel
291 const Bool_t nucp = !TESTBIT(fFlags, kUseCentralPixel);
292
293 Int_t num = nucp ? 0 : 1;
294
295 ped[i] = nucp ? 0 : ppix.GetMean();
296 rms[i] = nucp ? 0 : Pow2(ppix.GetRms());
297
298 //
299 // The values are rescaled to the small pixels area for the right comparison
300 //
301 const Double_t ratio = fGeomCam->GetPixRatio(i);
302
303 ped[i] *= ratio;
304 rms[i] *= ratio;
305
306 //
307 // Loop over all its neighbors
308 //
309 const Int_t n = gpix.GetNumNeighbors();
310 for (int j=0; j<n; j++)
311 {
312 const UShort_t nidx = gpix.GetNeighbor(j);
313
314 //
315 // Do not use blind neighbors
316 //
317 if (IsPixelBad(nidx))
318 continue;
319
320 //
321 // Get the geometry for the neighbor
322 //
323 const Double_t nratio = fGeomCam->GetPixRatio(nidx);
324 const MPedPhotPix &nppix = (*fPedPhot)[nidx];
325
326 //
327 //The error is calculated as the quadratic sum of the errors
328 //
329 ped[i] += nratio*nppix.GetMean();
330 rms[i] += nratio*Pow2(nppix.GetRms());
331
332 num++;
333 }
334
335 // Check if there are enough neighbors to calculate the mean
336 // If not, unmap the pixel. The minimum number of good neighbors
337 // should be fNumMinNeighbors
338 if (num < fNumMinNeighbors)
339 {
340 MCerPhotPix *pix =fEvt->GetPixById(i);
341 if (!pix)
342 pix = fEvt->AddPixel(i, 0, 0);
343 pix->SetPixelUnmapped();
344 continue;
345 }
346
347 //
348 // Now the mean is calculated and the values rescaled back
349 // to the pixel area
350 //
351 ped[i] /= (num*ratio);
352 rms[i] = TMath::Sqrt(rms[i]/(num*ratio));
353
354 (*fPedPhot)[i].Set(ped[i], rms[i]);
355 }
356}
357
358// --------------------------------------------------------------------------
359//
360// Replaces each pixel (signal, signal error, pedestal, pedestal rms)
361// by the average of its surrounding pixels.
362// If TESTBIT(fFlags, kUseCentralPixel) is set the central pixel is also
363// included.
364//
365// NT: Informations about the interpolated pedestals are added.
366// When the option Interpolated is called, the blind pixel with the new
367// values of signal and fluttuation is included in the calculation of
368// the Image Parameters.
369//
370/*
371void MBadPixelsTreat::Interpolate() const
372{
373 const UShort_t entries = fGeomCam->GetNumPixels();
374
375 //
376 // Create arrays
377 //
378 TArrayD nphot(entries);
379 TArrayD perr(entries);
380 TArrayD ped(entries);
381 TArrayD pedrms(entries);
382
383 //
384 // Loop over all pixels
385 //
386 for (UShort_t i=0; i<entries; i++)
387 {
388 MCerPhotPix *pix = fEvt->GetPixById(i);
389
390 //
391 // Check whether pixel with idx i is blind
392 //
393 if (pix && (*fBadPixels)[i].IsOK())
394 continue;
395
396 //
397 // Get a pointer to this pixel. If it is not yet existing
398 // create a new entry for this pixel in MCerPhotEvt
399 //
400 if (!pix)
401 {
402 pix = fEvt->AddPixel(i, 0, 0);
403 (*fBadPixels)[i].SetUnsuitable(MBadPixelsPix::kUnsuitableEvt);
404 }
405
406 //
407 // Get the corresponding geometry and pedestal
408 //
409 const MGeomPix &gpix = (*fGeomCam)[i];
410 const MPedPhotPix &ppix = (*fPedPhot)[i];
411
412 // Do Not-Use-Central-Pixel
413 const Bool_t nucp = !TESTBIT(fFlags, kUseCentralPixel);
414
415 Int_t num = nucp ? 0 : 1;
416
417 nphot[i] = nucp ? 0 : pix->GetNumPhotons();
418 ped[i] = nucp ? 0 : ppix.GetMean();
419 perr[i] = nucp ? 0 : Pow2(pix->GetErrorPhot());
420 pedrms[i] = nucp ? 0 : Pow2(ppix.GetRms());
421
422 //
423 // The values are rescaled to the small pixels area for the right comparison
424 //
425 const Double_t ratio = fGeomCam->GetPixRatio(i);
426
427 if (nucp)
428 {
429 nphot[i] *= ratio;
430 perr[i] *= ratio;
431 ped[i] *= ratio;
432 pedrms[i] *= ratio;
433 }
434
435 //
436 // Loop over all its neighbors
437 //
438 const Int_t n = gpix.GetNumNeighbors();
439 for (int j=0; j<n; j++)
440 {
441 const UShort_t nidx = gpix.GetNeighbor(j);
442
443 //
444 // Do not use blind neighbors
445 //
446 if ((*fBadPixels)[nidx].IsBad())
447 continue;
448
449 //
450 // Check whether the neighbor has a signal stored
451 //
452 const MCerPhotPix *evtpix = fEvt->GetPixById(nidx);
453 if (!evtpix)
454 continue;
455
456 //
457 // Get the geometry for the neighbor
458 //
459 const Double_t nratio = fGeomCam->GetPixRatio(nidx);
460 MPedPhotPix &nppix = (*fPedPhot)[nidx];
461
462 //
463 // The error is calculated as the quadratic sum of the errors
464 //
465 nphot[i] += nratio*evtpix->GetNumPhotons();
466 ped[i] += nratio*nppix.GetMean();
467 perr[i] += nratio*Pow2(evtpix->GetErrorPhot());
468 pedrms[i] += nratio*Pow2(nppix.GetRms());
469
470 num++;
471 }
472
473 if (num<fNumMinNeighbors)
474 {
475 pix->SetPixelUnmapped();
476 nphot[i] = 0;
477 ped[i] = 0;
478 perr[i] = 0;
479 pedrms[i] = 0;
480 continue;
481 }
482
483 //
484 // Now the mean is calculated and the values rescaled back to the pixel area
485 //
486 nphot[i] /= num*ratio;
487 ped[i] /= num*ratio;
488 perr[i] = TMath::Sqrt(perr[i]/(num*ratio));
489 pedrms[i] = TMath::Sqrt(pedrms[i]/(num*ratio));
490
491 }
492
493 //
494 // Now the new pixel values are calculated and can be replaced in
495 // the corresponding containers
496 //
497 for (UShort_t i=0; i<entries; i++)
498 {
499 //
500 // Do not use blind neighbors
501 //
502 if ((*fBadPixels)[i].IsOK())
503 continue;
504
505 //
506 // It must exist, we have created it in the loop before.
507 //
508 fEvt->GetPixById(i)->Set(nphot[i], perr[i]);
509 (*fPedPhot)[i].Set(ped[i], pedrms[i]);
510 }
511}
512*/
513
514// --------------------------------------------------------------------------
515//
516// Removes all blind pixels from the analysis by setting their state
517// to unused.
518//
519void MBadPixelsTreat::Unmap() const
520{
521 const UShort_t entries = fEvt->GetNumPixels();
522
523 //
524 // remove the pixels in fPixelsIdx if they are set to be used,
525 // (set them to 'unused' state)
526 //
527 for (UShort_t i=0; i<entries; i++)
528 {
529 MCerPhotPix &pix = (*fEvt)[i];
530
531 if (IsPixelBad(pix.GetPixId()))
532 pix.SetPixelUnmapped();
533 }
534}
535
536// --------------------------------------------------------------------------
537//
538// Interpolate Pedestals if kProcessRMS not set
539//
540Bool_t MBadPixelsTreat::ReInit(MParList *pList)
541{
542 if (!TESTBIT(fFlags, kProcessRMS))
543 InterpolatePedestals();
544 return kTRUE;
545}
546
547// --------------------------------------------------------------------------
548//
549// Treat the blind pixels
550//
551Int_t MBadPixelsTreat::Process()
552{
553 if (TESTBIT(fFlags, kUseInterpolation) && fGeomCam)
554 {
555 InterpolateSignal();
556 if (TESTBIT(fFlags, kProcessRMS))
557 InterpolatePedestals();
558 }
559 else
560 Unmap();
561
562 return kTRUE;
563}
564
565void MBadPixelsTreat::StreamPrimitive(ofstream &out) const
566{
567 out << " MBadPixelsTreat " << GetUniqueName();
568 if (fName!=gsDefName || fTitle!=gsDefTitle)
569 {
570 out << "(\"" << fName << "\"";
571 if (fTitle!=gsDefTitle)
572 out << ", \"" << fTitle << "\"";
573 out <<")";
574 }
575 out << ";" << endl;
576
577 if (TESTBIT(fFlags, kUseInterpolation))
578 out << " " << GetUniqueName() << ".SetUseInterpolation();" << endl;
579 if (TESTBIT(fFlags, kUseCentralPixel))
580 out << " " << GetUniqueName() << ".SetUseCentralPixel();" << endl;
581}
Note: See TracBrowser for help on using the repository browser.