source: trunk/MagicSoft/Mars/mbadpixels/MBadPixelsCalc.cc@ 7304

Last change on this file since 7304 was 7130, checked in by tbretz, 19 years ago
*** empty log message ***
File size: 10.3 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): Thomas Bretz, 02/2004 <mailto:tbretz@astro.uni.wuerzburg.de>
19!
20! Copyright: MAGIC Software Development, 2000-2004
21!
22!
23\* ======================================================================== */
24
25/////////////////////////////////////////////////////////////////////////////
26//
27// MBadPixelsCalc
28//
29//
30// The job of the task is to determin bad pixels event-wise. This must be
31// redone for each event. This particular task is for what is explained
32// below.
33// New algorithms may enter new tasks.
34//
35//
36// Check the pedestal RMS of every pixel with respect to the mean
37// pedestal RMS of the camera.
38//
39// The pixels can be set as blind if the pedestalRMS is too big or 0.
40//
41// If you don't want to use this option set the PedestalLevel<=0;
42//
43// MBadPixelsCalc calc;
44// calc.SetPedestalLevel(-1);
45//
46//
47// Input Containers:
48// [MPedPhotCam]
49// [MGeomCam]
50//
51// Output Containers:
52// MBadPixels
53//
54/////////////////////////////////////////////////////////////////////////////
55#include "MBadPixelsCalc.h"
56
57#include <TEnv.h>
58
59#include "MArrayI.h"
60#include "MArrayD.h"
61
62#include "MLog.h"
63#include "MLogManip.h"
64
65#include "MParList.h"
66
67#include "MGeomCam.h"
68#include "MGeomPix.h"
69
70#include "MPedPhotCam.h"
71#include "MPedPhotPix.h"
72
73#include "MBadPixelsCam.h"
74#include "MBadPixelsPix.h"
75
76ClassImp(MBadPixelsCalc);
77
78using namespace std;
79
80static const TString gsDefName = "MBadPixelsCalc";
81static const TString gsDefTitle = "Find hot spots (star, broken pixels, etc)";
82
83// --------------------------------------------------------------------------
84//
85// Default constructor.
86//
87MBadPixelsCalc::MBadPixelsCalc(const char *name, const char *title)
88 : fPedestalLevel(3), fPedestalLevelVarianceLo(5),
89 fPedestalLevelVarianceHi(5), fNamePedPhotCam("MPedPhotCam"),
90 fCheckInProcess(kTRUE), fCheckInPostProcess(kFALSE)
91{
92 fName = name ? name : gsDefName.Data();
93 fTitle = title ? title : gsDefTitle.Data();
94}
95
96// --------------------------------------------------------------------------
97//
98//
99Int_t MBadPixelsCalc::PreProcess (MParList *pList)
100{
101 fBadPixels = (MBadPixelsCam*)pList->FindCreateObj(AddSerialNumber("MBadPixelsCam"));
102 if (!fBadPixels)
103 return kFALSE;
104
105 if (fPedestalLevel>0)
106 {
107 fPedPhotCam = (MPedPhotCam*)pList->FindObject(AddSerialNumber(fNamePedPhotCam), "MPedPhotCam");
108 if (!fPedPhotCam)
109 {
110 *fLog << err << fNamePedPhotCam << "[MPedPhotCam] not found... aborting." << endl;
111 return kFALSE;
112 }
113
114 fGeomCam = (MGeomCam*)pList->FindObject(AddSerialNumber("MGeomCam"));
115 if (!fGeomCam)
116 {
117 *fLog << err << "MGeomCam not found... aborting." << endl;
118 return kFALSE;
119 }
120 }
121
122 *fLog << inf << "Name of MPedPhotCam to get pedestal rms from: " << fNamePedPhotCam << endl;
123 if (fPedestalLevel>0)
124 *fLog << "Checking mean 'pedestal rms' against absolute value with level " << fPedestalLevel << endl;
125 if (fPedestalLevelVarianceLo>0)
126 *fLog << "Checking mean 'pedestal rms' against its lower variance with level " << fPedestalLevelVarianceLo << endl;
127 if (fPedestalLevelVarianceHi>0)
128 *fLog << "Checking mean 'pedestal rms' against its upper variance with level " << fPedestalLevelVarianceHi << endl;
129
130 return kTRUE;
131}
132
133// --------------------------------------------------------------------------
134//
135// Check the pedestal Rms of the pixels: compute with 2 iterations the mean
136// for inner and outer pixels. Set as blind the pixels with too small or
137// too high pedestal Rms with respect to the mean.
138//
139Bool_t MBadPixelsCalc::CheckPedestalRms(MBadPixelsPix::UnsuitableType_t type) const
140{
141 const Bool_t checklo = fPedestalLevelVarianceLo>0;
142 const Bool_t checkhi = fPedestalLevelVarianceHi>0;
143
144 if (fPedestalLevel<=0 && !checklo && !checkhi)
145 return kTRUE;
146
147 if (!fGeomCam || !fPedPhotCam || !fBadPixels)
148 {
149 *fLog << err << "MBadPixelsCalc::CheckPedestalRms: ERROR - One of the necessary container are not initialized..." << endl;
150 return kFALSE;
151 }
152
153 const Int_t entries = fPedPhotCam->GetSize();
154
155 const Int_t na = fGeomCam->GetNumAreas();
156
157 MArrayD meanrms(na);
158 MArrayI npix(na);
159
160 for (Int_t i=0; i<entries; i++)
161 {
162 if ((*fBadPixels)[i].IsUnsuitable())
163 continue;
164
165 const Double_t rms = (*fPedPhotCam)[i].GetRms();
166
167 if (rms<=0 || rms>=200*fGeomCam->GetPixRatioSqrt(i))
168 continue;
169
170 const Byte_t aidx = (*fGeomCam)[i].GetAidx();
171
172 meanrms[aidx] += rms;
173 npix[aidx]++;
174 }
175
176 //if no pixel has a minimum signal, return
177 Int_t counter=0;
178 for (int i=0; i<na; i++)
179 {
180 if (npix[i]==0 || meanrms[i]==0) //no valid Pedestals Rms
181 {
182 counter++;
183 continue;
184 }
185
186 meanrms[i] /= npix[i];
187 npix[i]=0;
188 }
189
190 if (counter==na)
191 {
192 *fLog << err << "MBadPixelsCalc::CheckPedestalRms: ERROR - No pixel seems to contain a valid pedestal RMS..." << endl;
193 return kFALSE;
194 }
195
196 MArrayD meanrms2(na);
197 MArrayD varrms2(na);
198 for (Int_t i=0; i<entries; i++)
199 {
200 if ((*fBadPixels)[i].IsUnsuitable())
201 continue;
202
203 const Double_t rms = (*fPedPhotCam)[i].GetRms();
204 const Byte_t aidx = (*fGeomCam)[i].GetAidx();
205
206 //Calculate the corrected means:
207 if (rms<=meanrms[aidx]/1.5 || rms>=meanrms[aidx]*1.5)
208 continue;
209
210 meanrms2[aidx] += rms;
211 varrms2 [aidx] += rms*rms;
212 npix[aidx]++;
213 }
214
215 //if no pixel has a minimum signal, return
216 MArrayD lolim1(na), lolim2(na); // Precalcualtion of limits
217 MArrayD uplim1(na), uplim2(na); // for speeed reasons
218 counter = 0;
219 for (int i=0; i<na; i++)
220 {
221 if (npix[i]==0 || meanrms2[i]==0)
222 {
223 counter++;
224 continue;
225 }
226
227 meanrms2[i] /= npix[i];
228
229 if (fPedestalLevel>0)
230 {
231 lolim1[i] = meanrms2[i]/fPedestalLevel;
232 uplim1[i] = meanrms2[i]*fPedestalLevel;
233 }
234
235 if (checklo || checkhi)
236 {
237 varrms2[i] /= npix[i];
238 varrms2[i] = TMath::Sqrt(varrms2[i]-meanrms2[i]*meanrms2[i]);
239
240 lolim2[i] = meanrms2[i]-fPedestalLevelVarianceLo*varrms2[i];
241 uplim2[i] = meanrms2[i]+fPedestalLevelVarianceHi*varrms2[i];
242 }
243 }
244
245 if (counter==na)
246 {
247 *fLog << err << "MBadPixelsCalc::CheckPedestalRms: ERROR - No pixel seems to contain a valid pedestal RMS anymore..." << endl;
248 return kFALSE;
249 }
250
251 Int_t bads = 0;
252
253 //Blind the Bad Pixels
254 for (Int_t i=0; i<entries; i++)
255 {
256 if ((*fBadPixels)[i].IsUnsuitable())
257 continue;
258
259 const Double_t rms = (*fPedPhotCam)[i].GetRms();
260 const Byte_t aidx = (*fGeomCam)[i].GetAidx();
261
262 if ((fPedestalLevel<=0 || (rms> lolim1[aidx] && rms<=uplim1[aidx])) &&
263 (!checklo || rms> lolim2[aidx]) &&
264 (!checkhi || rms<=uplim2[aidx])
265 )
266 continue;
267
268 (*fBadPixels)[i].SetUnsuitable(type);
269 if (type==MBadPixelsPix::kUnsuitableRun)
270 (*fBadPixels)[i].SetUncalibrated(MBadPixelsPix::kDeadPedestalRms);
271
272 bads++;
273 }
274
275 // Check if the number of pixels to blind is > 60% of total number of pixels
276 //
277 if (bads>0.5*entries)
278 {
279 *fLog << err << "ERROR - More than 50% unsuitable pixels... something must be wrong!" << endl;
280 return kFALSE;
281 }
282
283 return kTRUE;
284}
285
286Bool_t MBadPixelsCalc::CheckPedestalRms(MBadPixelsCam &cam, const MPedPhotCam &ped, MBadPixelsPix::UnsuitableType_t t)
287{
288 MBadPixelsCam *store1 = fBadPixels;
289 const MPedPhotCam *store2 = fPedPhotCam;
290
291 fBadPixels = &cam;
292 fPedPhotCam = &ped;
293
294 const Bool_t rc = CheckPedestalRms(t);
295
296 fBadPixels = store1;
297 fPedPhotCam = store2;
298
299 return rc;
300}
301
302Bool_t MBadPixelsCalc::CheckPedestalRms(MBadPixelsCam &cam, const MPedestalCam &ped, MBadPixelsPix::UnsuitableType_t t)
303{
304 return CheckPedestalRms(cam, MPedPhotCam(ped), t);
305}
306
307// --------------------------------------------------------------------------
308//
309//
310Int_t MBadPixelsCalc::Process()
311{
312 return fCheckInProcess ? CheckPedestalRms(MBadPixelsPix::kUnsuitableEvt) : kTRUE;
313}
314
315// --------------------------------------------------------------------------
316//
317//
318Int_t MBadPixelsCalc::PostProcess()
319{
320 return fCheckInPostProcess ? CheckPedestalRms(MBadPixelsPix::kUnsuitableRun) : kTRUE;
321}
322
323// --------------------------------------------------------------------------
324//
325// Read the setup from a TEnv, eg:
326// MBadPixelsCalc.PedestalLevel: 3.0
327//
328// MBadPixelsCalc.PedestalLevelVariance: 5.0
329// overwrites
330// MBadPixelsCalc.PedestalLevelVarianceLo: 5.0
331// and
332// MBadPixelsCalc.PedestalLevelVarianceHi: 5.0
333//
334Int_t MBadPixelsCalc::ReadEnv(const TEnv &env, TString prefix, Bool_t print)
335{
336 Bool_t rc = kFALSE;
337 if (IsEnvDefined(env, prefix, "PedestalLevel", print))
338 {
339 rc = kTRUE;
340 SetPedestalLevel(GetEnvValue(env, prefix, "PedestalLevel", fPedestalLevel));
341 }
342 if (IsEnvDefined(env, prefix, "PedestalLevelVarianceLo", print))
343 {
344 rc = kTRUE;
345 SetPedestalLevelVarianceLo(GetEnvValue(env, prefix, "PedestalLevelVarianceLo", fPedestalLevelVarianceLo));
346 }
347 if (IsEnvDefined(env, prefix, "PedestalLevelVarianceHi", print))
348 {
349 rc = kTRUE;
350 SetPedestalLevelVarianceHi(GetEnvValue(env, prefix, "PedestalLevelVarianceHi", fPedestalLevelVarianceHi));
351 }
352
353 if (IsEnvDefined(env, prefix, "PedestalLevelVariance", print))
354 {
355 rc = kTRUE;
356 SetPedestalLevelVariance(GetEnvValue(env, prefix, "PedestalLevelVariance", -1));
357 }
358 return rc;
359}
Note: See TracBrowser for help on using the repository browser.