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

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