source: tags/Mars-V0.10.3/mbadpixels/MBadPixelsTreat.cc

Last change on this file was 8132, checked in by tbretz, 18 years ago
*** empty log message ***
File size: 19.9 KB
Line 
1/* ======================================================================== *\
2! $Name: not supported by cvs2svn $:$Id: MBadPixelsTreat.cc,v 1.37 2006-10-19 13:57:40 tbretz Exp $
3! --------------------------------------------------------------------------
4!
5! *
6! * This file is part of MARS, the MAGIC Analysis and Reconstruction
7! * Software. It is distributed to you in the hope that it can be a useful
8! * and timesaving tool in analysing Data of imaging Cerenkov telescopes.
9! * It is distributed WITHOUT ANY WARRANTY.
10! *
11! * Permission to use, copy, modify and distribute this software and its
12! * documentation for any purpose is hereby granted without fee,
13! * provided that the above copyright notice appear in all copies and
14! * that both that copyright notice and this permission notice appear
15! * in supporting documentation. It is provided "as is" without express
16! * or implied warranty.
17! *
18!
19!
20! Author(s): Oscar Blanch 12/2001 <mailto:blanch@ifae.es>
21! Author(s): Thomas Bretz 08/2002 <mailto:tbretz@astro.uni-wuerzburg.de>
22! Author(s): Stefan Ruegamer <mailto:snruegam@astro.uni-wuerzburg.de>
23!
24! Copyright: MAGIC Software Development, 2000-2006
25!
26!
27\* ======================================================================== */
28
29/////////////////////////////////////////////////////////////////////////////
30//
31// MBadPixelsTreat
32//
33// You can use MBadPixelsTreat::SetUseInterpolation to replaced the
34// bad pixels by the average of its neighbors instead of unmapping
35// them. If you want to include the central pixel use
36// MBadPixelsTreat::SetUseCentralPixel. The bad pixels are taken from
37// an existing MBadPixelsCam.
38//
39// It check if there are enough neighbors to calculate the mean
40// If not, unmap the pixel. The minimum number of good neighbors
41// should be set using SetNumMinNeighbors
42//
43// If you want to interpolate unreliable pixels and unsuitable
44// (broken) pixels use SetHardTreatment().
45//
46//
47// Options:
48// --------
49// SetHardTreatment: Also interpolate unreliable pixels not only unsuitable
50// SetUseInterpolation: Interpolate pixels instead of unmapping them
51// SetUseCentralPixel: also use the pixel itself for interpolation
52// SetProcessPedestalRun: process the pedestals once per run/file
53// SetProcessPedestalEvt: process the pedestal once per event
54// SetProcessTimes: do interpolation of the arrival time
55//
56// If the arrival time treatment is switched on and "MPedPhotFromExtractor"
57// and "MPedPhotFromExtractorRndm" are found the pixel is filled with
58// a random gaus calculated from these two MPedPhotCams in the case
59// the pixels is detected as background.
60//
61//
62// Input Containers:
63// MSignalCam
64// MPedPhotCam
65// MBadPixelsCam
66// [MGeomCam]
67//
68// Output Containers:
69// MSignalCam
70//
71/////////////////////////////////////////////////////////////////////////////
72#include "MBadPixelsTreat.h"
73
74#include <fstream>
75
76#include <TEnv.h>
77#include <TRandom.h>
78#include <TObjString.h>
79
80//#include "MArrayD.h" // Used instead of TArrayD because operator[] has no range check
81
82#include "MLog.h"
83#include "MLogManip.h"
84
85#include "MParList.h"
86
87#include "MGeomPix.h"
88#include "MGeomCam.h"
89
90#include "MSignalPix.h"
91#include "MSignalCam.h"
92
93#include "MPedPhotPix.h"
94#include "MPedPhotCam.h"
95
96#include "MBadPixelsPix.h"
97#include "MBadPixelsCam.h"
98
99ClassImp(MBadPixelsTreat);
100
101using namespace std;
102
103static const TString gsDefName = "MBadPixelsTreat";
104static const TString gsDefTitle = "Task to treat bad pixels (interpolation, unmapping)";
105
106// --------------------------------------------------------------------------
107//
108// Default constructor.
109//
110MBadPixelsTreat::MBadPixelsTreat(const char *name, const char *title)
111 : fGeomCam(0), fEvt(0), fBadPixels(0), fPedPhot1(0), fPedPhot2(0),
112 fFlags(0), fNumMinNeighbors(3), fMaxArrivalTimeDiff(0.9)
113{
114 fName = name ? name : gsDefName.Data();
115 fTitle = title ? title : gsDefTitle.Data();
116
117 SetUseInterpolation();
118 SetProcessPedestal();
119 SetProcessTimes();
120}
121
122// --------------------------------------------------------------------------
123//
124// Returns the status of a pixel. If kHardTreatment is set a pixel must
125// be unsuitable or uriliable to be treated. If not it is treated only if
126// it is unsuitable
127// (IsBad() checks for any flag)
128//
129Bool_t MBadPixelsTreat::IsPixelBad(Int_t idx) const
130{
131 return TESTBIT(fFlags, kHardTreatment) ? (*fBadPixels)[idx].IsBad():(*fBadPixels)[idx].IsUnsuitable();
132}
133
134void MBadPixelsTreat::AddNamePedPhotCam(const char *name)
135{
136 fNamePedPhotCams.Add(new TObjString(name));
137}
138
139// --------------------------------------------------------------------------
140//
141// - Try to find or create MBlindPixels in parameter list.
142// - get the MSignalCam from the parlist (abort if missing)
143// - if no pixels are given by the user try to determin the starfield
144// from the monte carlo run header.
145//
146Int_t MBadPixelsTreat::PreProcess (MParList *pList)
147{
148 fBadPixels = (MBadPixelsCam*)pList->FindObject(AddSerialNumber("MBadPixelsCam"));
149 if (!fBadPixels)
150 {
151 *fLog << err << AddSerialNumber("MBadPixelsCam") << " not found... aborting." << endl;
152 return kFALSE;
153 }
154
155 fEvt = (MSignalCam*)pList->FindObject(AddSerialNumber("MSignalCam"));
156 if (!fEvt)
157 {
158 *fLog << err << AddSerialNumber("MSignalCam") << " not found... aborting." << endl;
159 return kFALSE;
160 }
161
162 fGeomCam = 0;
163 if (!IsUseInterpolation())
164 return kTRUE;
165
166 fGeomCam = (MGeomCam*)pList->FindObject(AddSerialNumber("MGeomCam"));
167 if (!fGeomCam)
168 {
169 *fLog << err << AddSerialNumber("MGeomCam") << " not found - can't use interpolation... abort." << endl;
170 *fLog << " Use MBadPixelsTreat::SetUseInterpolation(kFALSE) to switch interpolation" << endl;
171 *fLog << " off and use unmapping instead." << endl;
172 return kFALSE;
173 }
174
175 const Bool_t proc = IsProcessPedestalEvt() || IsProcessPedestalRun();
176
177 if (fNamePedPhotCams.GetSize()>0 && !proc)
178 {
179 *fLog << err << "Pedestal list contains entries, but pedestal treatment is switched off... abort." << endl;
180 return kFALSE;
181 }
182
183 if (proc)
184 {
185 if (fNamePedPhotCams.GetSize()==0)
186 {
187 *fLog << inf << "No container names specified... using default: MPedPhotCam." << endl;
188 AddNamePedPhotCam();
189 }
190
191 fPedPhotCams.Clear();
192
193 TIter Next(&fNamePedPhotCams);
194 TObject *o=0;
195 while ((o=Next()))
196 {
197 TObject *p = pList->FindObject(AddSerialNumber(o->GetName()), "MPedPhotCam");
198 if (!p)
199 {
200 *fLog << err << AddSerialNumber(o->GetName()) << " [MPedPhotCam] not found... aborting." << endl;
201 return kFALSE;
202 }
203
204 fPedPhotCams.Add(p);
205 }
206 }
207
208 if (IsProcessTimes())
209 {
210 fPedPhot1 = (MPedPhotCam*)pList->FindObject("MPedPhotFromExtractor", "MPedPhotCam");
211 fPedPhot2 = (MPedPhotCam*)pList->FindObject("MPedPhotFromExtractorRndm", "MPedPhotCam");
212
213 *fLog << inf << "Additional no-signal-interpolation switched ";
214 *fLog << (fPedPhot1 && fPedPhot2 ? "on" : "off");
215 *fLog << "." << endl;
216 }
217
218 if (IsProcessPedestalEvt())
219 *fLog << inf << "Processing Pedestals once per event..." << endl;
220 if (IsProcessPedestalRun())
221 *fLog << inf << "Processing Pedestals once per run..." << endl;
222 if (IsProcessTimes())
223 *fLog << inf << "Processing Arrival Times once per event..." << endl;
224
225 return kTRUE;
226}
227
228// --------------------------------------------------------------------------
229//
230// Replaces each pixel (signal, signal error, pedestal, pedestal rms)
231// by the average of its surrounding pixels.
232// If TESTBIT(fFlags, kUseCentralPixel) is set the central pixel is also
233// included.
234//
235void MBadPixelsTreat::InterpolateSignal() const
236{
237 const UShort_t entries = fGeomCam->GetNumPixels();
238
239 //
240 // Loop over all pixels
241 //
242 for (UShort_t i=0; i<entries; i++)
243 {
244 //
245 // Check whether pixel with idx i is blind
246 //
247 if (!IsPixelBad(i))
248 continue;
249
250 //
251 // Get the corresponding geometry and pedestal
252 //
253 MSignalPix &pix = (*fEvt)[i];
254 const MGeomPix &gpix = (*fGeomCam)[i];
255
256 // Do Not-Use-Central-Pixel
257 const Bool_t nucp = !TESTBIT(fFlags, kUseCentralPixel);
258
259 Int_t num = nucp ? 0 : 1;
260
261 Double_t nphot = nucp ? 0 : pix.GetNumPhotons();
262 Double_t perr = nucp ? 0 : Pow2(pix.GetErrorPhot());
263
264 //
265 // The values are rescaled to the small pixels area for the right comparison
266 //
267 const Double_t ratio = fGeomCam->GetPixRatio(i);
268
269 nphot *= ratio;
270 perr *= ratio;
271
272 //
273 // Loop over all its neighbors
274 //
275 const Int_t n = gpix.GetNumNeighbors();
276 for (int j=0; j<n; j++)
277 {
278 const UShort_t nidx = gpix.GetNeighbor(j);
279
280 //
281 // Do not use blind neighbors
282 //
283 if (IsPixelBad(nidx))
284 continue;
285
286 //
287 // Get the geometry for the neighbor
288 //
289 const Double_t nratio = fGeomCam->GetPixRatio(nidx);
290
291 //
292 //The error is calculated as the quadratic sum of the errors
293 //
294 const MSignalPix &evtpix = (*fEvt)[nidx];
295
296 nphot += nratio*evtpix.GetNumPhotons();
297 perr += nratio*Pow2(evtpix.GetErrorPhot());
298
299 num++;
300 }
301
302 // Check if there are enough neighbors to calculate the mean
303 // If not, unmap the pixel. The maximum number of blind neighbors
304 // should be 2
305 if (num<fNumMinNeighbors)
306 {
307 pix.SetPixelUnmapped();
308 continue;
309 }
310
311 //
312 // Now the mean is calculated and the values rescaled back
313 // to the pixel area
314 //
315 nphot /= num*ratio;
316 perr = TMath::Sqrt(perr/(num*ratio));
317
318 pix.Set(nphot, perr);
319 }
320}
321
322// --------------------------------------------------------------------------
323//
324void MBadPixelsTreat::InterpolatePedestals(MPedPhotCam &pedphot) const
325{
326 const Int_t entries = pedphot.GetSize();
327
328 //
329 // Loop over all pixels
330 //
331 for (UShort_t i=0; i<entries; i++)
332 {
333 //
334 // Check whether pixel with idx i is blind
335 //
336 if (!IsPixelBad(i))
337 continue;
338
339 //
340 // Get the corresponding geometry and pedestal
341 //
342 const MGeomPix &gpix = (*fGeomCam)[i];
343 const MPedPhotPix &ppix = pedphot[i];
344
345 // Do Not-Use-Central-Pixel
346 const Bool_t nucp = !TESTBIT(fFlags, kUseCentralPixel);
347
348 Int_t num = nucp ? 0 : 1;
349
350 Double_t ped = nucp ? 0 : ppix.GetMean();
351 Double_t rms = nucp ? 0 : Pow2(ppix.GetRms());
352
353 //
354 // The values are rescaled to the small pixels area for the right comparison
355 //
356 const Double_t ratio = fGeomCam->GetPixRatio(i);
357
358 ped *= ratio;
359 rms *= ratio;
360
361 //
362 // Loop over all its neighbors
363 //
364 const Int_t n = gpix.GetNumNeighbors();
365 for (int j=0; j<n; j++)
366 {
367 const UShort_t nidx = gpix.GetNeighbor(j);
368
369 //
370 // Do not use blind neighbors
371 //
372 if (IsPixelBad(nidx))
373 continue;
374
375 //
376 // Get the geometry for the neighbor
377 //
378 const Double_t nratio = fGeomCam->GetPixRatio(nidx);
379 const MPedPhotPix &nppix = pedphot[nidx];
380
381 //
382 //The error is calculated as the quadratic sum of the errors
383 //
384 ped += nratio*nppix.GetMean();
385 rms += nratio*Pow2(nppix.GetRms());
386
387 num++;
388 }
389
390 // Check if there are enough neighbors to calculate the mean
391 // If not, unmap the pixel. The minimum number of good neighbors
392 // should be fNumMinNeighbors
393 if (num<fNumMinNeighbors)
394 {
395 (*fEvt)[i].SetPixelUnmapped();
396 continue;
397 }
398
399 //
400 // Now the mean is calculated and the values rescaled back
401 // to the pixel area
402 //
403 ped /= num*ratio;
404 rms = TMath::Sqrt(rms/(num*ratio));
405
406 pedphot[i].Set(ped, rms);
407 }
408 pedphot.SetReadyToSave();
409}
410
411// --------------------------------------------------------------------------
412//
413// loop over all MPedPhotCam and interpolate them
414//
415void MBadPixelsTreat::InterpolatePedestals() const
416{
417 TIter Next(&fPedPhotCams);
418 MPedPhotCam *cam=0;
419 while ((cam=(MPedPhotCam*)Next()))
420 {
421 InterpolatePedestals(*cam);
422 cam->ReCalc(*fGeomCam, fBadPixels);
423 }
424}
425
426// --------------------------------------------------------------------------
427//
428void MBadPixelsTreat::InterpolateTimes() const
429{
430 const Int_t n = fEvt->GetNumPixels();
431 for (int i=0; i<n; i++)
432 {
433 // Check whether pixel with idx i is bad
434 if (!IsPixelBad(i))
435 continue;
436
437 // Geometry of bad pixel
438 const MGeomPix &gpix = (*fGeomCam)[i];
439
440 // Number of neighbor pixels
441 const Int_t n2 = gpix.GetNumNeighbors();
442
443 // Copy the arrival time of all neighboring bad pixels
444 // to a new array for simplicity
445 Double_t time[6];
446 Int_t cnt = 0;
447 for (Int_t j=0; j<n2; j++)
448 {
449 const Int_t idx = gpix.GetNeighbor(j);
450 if (!IsPixelBad(idx))
451 time[cnt++] = (*fEvt)[idx].GetArrivalTime();
452 }
453
454 // if there are too few neighbours, don't interpolate the pixel
455 //if ((cnt < 3 && n2 > 3) || (cnt < 2 && n2 == 3))
456 if (cnt<fNumMinNeighbors)
457 {
458 (*fEvt)[i].SetPixelUnmapped();
459 continue;
460 }
461
462 Double_t min = FLT_MAX; // Find minimum arrival time
463 Double_t max = -FLT_MAX; // Find maximum arrival time
464
465 Double_t sum2 = 0; // Sum of arrival times of the pixels
466 Int_t cnt2 = 0; // Number of pixels summed in sum2
467
468 for (Int_t j=0; j<cnt; j++)
469 {
470 const Double_t tm1 = time[j]; // time of one neighbor pixel
471 const Double_t tm2 = time[(j+1)%cnt]; // time of its neighbor pixel
472
473 // Calculate mean arrival time of pixel probably inside the shower
474 if (TMath::Abs(tm1 - tm2)<fMaxArrivalTimeDiff)
475 {
476 sum2 += tm1+tm2;
477 cnt2++;
478 }
479
480 // Find minimum arrival time
481 if (tm1<min)
482 min = tm1;
483
484 // Find maximum arrival time
485 if (tm1>max)
486 max = tm1;
487 }
488
489 // If less than two nbeighbors belong to a shower the pixel doesn't
490 // belong to the shower, too. Set the arrival time to a uniform
491 // random value, otherwise use the mean value of the pixels belonging
492 // to the shower.
493 if (cnt2<=2)
494 {
495 sum2 = gRandom->Uniform(max-min)+min; // FIXME? Set Seed value?
496
497 // Proceed with a treatment of the signal of empty pixels
498 // better than the interpolation. (FIXME: Maybe a function
499 // different from a gaussian could be a better choice...)
500 if (fPedPhot1 && fPedPhot2)
501 {
502 const Int_t aidx = gpix.GetAidx();
503 // This is to which bias level the signal fluctuates
504 const Double_t mean = fPedPhot1->GetArea(aidx).GetMean();
505 // This is how the signal fluctuates
506 const Double_t rms = fPedPhot2->GetArea(aidx).GetRms();
507 const Double_t phe = gRandom->Gaus(mean, rms);
508
509 (*fEvt)[i].SetNumPhotons(phe);
510 }
511 }
512 else
513 sum2 /= cnt2*2;
514
515 (*fEvt)[i].SetArrivalTime(sum2);
516 }
517}
518
519// --------------------------------------------------------------------------
520//
521// Removes all blind pixels from the analysis by setting their state
522// to unused.
523//
524void MBadPixelsTreat::Unmap() const
525{
526 const UShort_t entries = fEvt->GetNumPixels();
527
528 //
529 // remove the pixels in fPixelsIdx if they are set to be used,
530 // (set them to 'unused' state)
531 //
532 for (UShort_t i=0; i<entries; i++)
533 {
534 if (IsPixelBad(i))
535 (*fEvt)[i].SetPixelUnmapped();
536 }
537}
538
539// --------------------------------------------------------------------------
540//
541// Interpolate Pedestals if kProcessPedestal not set
542//
543Bool_t MBadPixelsTreat::ReInit(MParList *pList)
544{
545 if (IsUseInterpolation() && IsProcessPedestalRun())
546 InterpolatePedestals();
547 return kTRUE;
548}
549
550// --------------------------------------------------------------------------
551//
552// Treat the blind pixels
553//
554Int_t MBadPixelsTreat::Process()
555{
556 if (IsUseInterpolation())
557 {
558 InterpolateSignal();
559 if (IsProcessPedestalEvt())
560 InterpolatePedestals();
561 if (IsProcessTimes())
562 InterpolateTimes();
563 }
564 else
565 Unmap();
566
567 return kTRUE;
568}
569
570void MBadPixelsTreat::StreamPrimitive(ostream &out) const
571{
572 out << " MBadPixelsTreat " << GetUniqueName();
573 if (fName!=gsDefName || fTitle!=gsDefTitle)
574 {
575 out << "(\"" << fName << "\"";
576 if (fTitle!=gsDefTitle)
577 out << ", \"" << fTitle << "\"";
578 out <<")";
579 }
580 out << ";" << endl;
581
582 if (IsUseInterpolation())
583 out << " " << GetUniqueName() << ".SetUseInterpolation();" << endl;
584 if (IsUseCentralPixel())
585 out << " " << GetUniqueName() << ".SetUseCentralPixel();" << endl;
586 if (IsProcessPedestalRun())
587 out << " " << GetUniqueName() << ".SetProcessPedestalRun();" << endl;
588 if (IsProcessPedestalEvt())
589 out << " " << GetUniqueName() << ".SetProcessPedestalEvt();" << endl;
590 if (IsProcessTimes())
591 out << " " << GetUniqueName() << ".SetProcessTimes();" << endl;
592 if (IsHardTreatment())
593 out << " " << GetUniqueName() << ".SetHardTreatment();" << endl;
594 if (fNumMinNeighbors!=3)
595 out << " " << GetUniqueName() << ".SetNumMinNeighbors(" << (int)fNumMinNeighbors << ");" << endl;
596}
597
598// --------------------------------------------------------------------------
599//
600// Read the setup from a TEnv, eg:
601// MBadPixelsTreat.UseInterpolation: no
602// MBadPixelsTreat.UseCentralPixel: no
603// MBadPixelsTreat.HardTreatment: no
604// MBadPixelsTreat.ProcessPedestalRun: no
605// MBadPixelsTreat.ProcessPedestalEvt: no
606// MBadPixelsTreat.NumMinNeighbors: 3
607// MBadPixelsTreat.MaxArrivalTimeDiff: 0.9
608//
609Int_t MBadPixelsTreat::ReadEnv(const TEnv &env, TString prefix, Bool_t print)
610{
611 Bool_t rc = kFALSE;
612 if (IsEnvDefined(env, prefix, "UseInterpolation", print))
613 {
614 rc = kTRUE;
615 SetUseInterpolation(GetEnvValue(env, prefix, "UseInterpolation", IsUseInterpolation()));
616 }
617 if (IsEnvDefined(env, prefix, "UseCentralPixel", print))
618 {
619 rc = kTRUE;
620 SetUseCentralPixel(GetEnvValue(env, prefix, "UseCentralPixel", IsUseCentralPixel()));
621 }
622 if (IsEnvDefined(env, prefix, "HardTreatment", print))
623 {
624 rc = kTRUE;
625 SetHardTreatment(GetEnvValue(env, prefix, "HardTreatment", IsHardTreatment()));
626 }
627 if (IsEnvDefined(env, prefix, "ProcessPedestalRun", print))
628 {
629 rc = kTRUE;
630 SetProcessPedestalRun(GetEnvValue(env, prefix, "ProcessPedestalRun", IsProcessPedestalRun()));
631 }
632 if (IsEnvDefined(env, prefix, "ProcessPedestalEvt", print))
633 {
634 rc = kTRUE;
635 SetProcessPedestalEvt(GetEnvValue(env, prefix, "ProcessPedestalEvt", IsProcessPedestalEvt()));
636 }
637 if (IsEnvDefined(env, prefix, "ProcessTimes", print))
638 {
639 rc = kTRUE;
640 SetProcessTimes(GetEnvValue(env, prefix, "ProcessTimes", IsProcessTimes()));
641 }
642 if (IsEnvDefined(env, prefix, "NumMinNeighbors", print))
643 {
644 rc = kTRUE;
645 SetNumMinNeighbors(GetEnvValue(env, prefix, "NumMinNeighbors", fNumMinNeighbors));
646 }
647 if (IsEnvDefined(env, prefix, "MaxArrivalTimeDiff", print))
648 {
649 rc = kTRUE;
650 SetMaxArrivalTimeDiff(GetEnvValue(env, prefix, "MaxArrivalTimeDiff", fMaxArrivalTimeDiff));
651 }
652 return rc;
653}
Note: See TracBrowser for help on using the repository browser.