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

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