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

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