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

Last change on this file since 7109 was 7109, checked in by tbretz, 19 years ago
*** empty log message ***
File size: 9.1 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), fPedestalLevelVariance(5), fNamePedPhotCam("MPedPhotCam"),
89 fCheckInProcess(kTRUE), fCheckInPostProcess(kFALSE)
90{
91 fName = name ? name : gsDefName.Data();
92 fTitle = title ? title : gsDefTitle.Data();
93}
94
95// --------------------------------------------------------------------------
96//
97//
98Int_t MBadPixelsCalc::PreProcess (MParList *pList)
99{
100 fBadPixels = (MBadPixelsCam*)pList->FindCreateObj(AddSerialNumber("MBadPixelsCam"));
101 if (!fBadPixels)
102 return kFALSE;
103
104 if (fPedestalLevel>0)
105 {
106 fPedPhotCam = (MPedPhotCam*)pList->FindObject(AddSerialNumber(fNamePedPhotCam), "MPedPhotCam");
107 if (!fPedPhotCam)
108 {
109 *fLog << err << fNamePedPhotCam << "[MPedPhotCam] not found... aborting." << endl;
110 return kFALSE;
111 }
112
113 fGeomCam = (MGeomCam*)pList->FindObject(AddSerialNumber("MGeomCam"));
114 if (!fGeomCam)
115 {
116 *fLog << err << "MGeomCam not found... aborting." << endl;
117 return kFALSE;
118 }
119 }
120
121 *fLog << inf << "Name of MPedPhotCam to get pedestal rms from: " << fNamePedPhotCam << endl;
122 if (fPedestalLevel)
123 *fLog << "Checking mean 'pedestal rms' against absolute value with level " << fPedestalLevel << endl;
124 if (fPedestalLevelVariance)
125 *fLog << "Checking mean 'pedestal rms' against its variance with level " << fPedestalLevelVariance << endl;
126
127 return kTRUE;
128}
129
130// --------------------------------------------------------------------------
131//
132// Check the pedestal Rms of the pixels: compute with 2 iterations the mean
133// for inner and outer pixels. Set as blind the pixels with too small or
134// too high pedestal Rms with respect to the mean.
135//
136Bool_t MBadPixelsCalc::CheckPedestalRms(MBadPixelsPix::UnsuitableType_t type) const
137{
138 if (!fGeomCam || !fPedPhotCam || !fBadPixels)
139 {
140 *fLog << err << "MBadPixelsCalc::CheckPedestalRms: ERROR - One of the necessary container are not initialized..." << endl;
141 return kFALSE;
142 }
143
144 if (fPedestalLevel<=0 && fPedestalLevelVariance<=0)
145 return kTRUE;
146
147 const Int_t entries = fPedPhotCam->GetSize();
148
149 const Int_t na = fGeomCam->GetNumAreas();
150
151 MArrayD meanrms(na);
152 MArrayI npix(na);
153
154 for (Int_t i=0; i<entries; i++)
155 {
156 if ((*fBadPixels)[i].IsUnsuitable())
157 continue;
158
159 const Double_t rms = (*fPedPhotCam)[i].GetRms();
160
161 if (rms<=0 || rms>=200*fGeomCam->GetPixRatioSqrt(i))
162 continue;
163
164 const Byte_t aidx = (*fGeomCam)[i].GetAidx();
165
166 meanrms[aidx] += rms;
167 npix[aidx]++;
168 }
169
170 //if no pixel has a minimum signal, return
171 for (int i=0; i<na; i++)
172 {
173 if (npix[i]==0 || meanrms[i]==0)
174 {
175 //fErrors[1]++; //no valid Pedestals Rms
176 return kFALSE;
177 }
178
179 meanrms[i] /= npix[i];
180 npix[i]=0;
181 }
182
183 MArrayD meanrms2(na);
184 MArrayD varrms2(na);
185 for (Int_t i=0; i<entries; i++)
186 {
187 if ((*fBadPixels)[i].IsUnsuitable())
188 continue;
189
190 const Double_t rms = (*fPedPhotCam)[i].GetRms();
191 const Byte_t aidx = (*fGeomCam)[i].GetAidx();
192
193 //Calculate the corrected means:
194 if (rms<=meanrms[aidx]/1.5 || rms>=meanrms[aidx]*1.5)
195 continue;
196
197 meanrms2[aidx] += rms;
198 varrms2 [aidx] += rms*rms;
199 npix[aidx]++;
200 }
201
202 //if no pixel has a minimum signal, return
203 MArrayD lolim1(na), lolim2(na); // Precalcualtion of limits
204 MArrayD uplim1(na), uplim2(na); // for speeed reasons
205 for (int i=0; i<na; i++)
206 {
207 if (npix[i]==0 || meanrms2[i]==0)
208 {
209 //fErrors[1]++; //no valid Pedestals Rms
210 return kFALSE;
211 }
212
213 meanrms2[i] /= npix[i];
214
215 if (fPedestalLevel>0)
216 {
217 lolim1[i] = meanrms2[i]/fPedestalLevel;
218 uplim1[i] = meanrms2[i]*fPedestalLevel;
219 }
220
221 if (fPedestalLevelVariance>0)
222 {
223 varrms2[i] /= npix[i];
224 varrms2[i] = TMath::Sqrt(varrms2[i]-meanrms2[i]*meanrms2[i]);
225
226 lolim2[i] = meanrms2[i]-fPedestalLevelVariance*varrms2[i];
227 uplim2[i] = meanrms2[i]+fPedestalLevelVariance*varrms2[i];
228 }
229 }
230
231 Int_t bads = 0;
232
233 //Blind the Bad Pixels
234 for (Int_t i=0; i<entries; i++)
235 {
236 if ((*fBadPixels)[i].IsUnsuitable())
237 continue;
238
239 const Double_t rms = (*fPedPhotCam)[i].GetRms();
240 const Byte_t aidx = (*fGeomCam)[i].GetAidx();
241
242 if ((fPedestalLevel<=0 || (rms>lolim1[aidx] && rms<=uplim1[aidx])) &&
243 (fPedestalLevelVariance<=0 || (rms>lolim2[aidx] && rms<=uplim2[aidx])))
244 continue;
245
246 (*fBadPixels)[i].SetUnsuitable(type);
247 if (type==MBadPixelsPix::kUnsuitableRun)
248 (*fBadPixels)[i].SetUncalibrated(MBadPixelsPix::kDeadPedestalRms);
249
250 bads++;
251 }
252
253 // Check if the number of pixels to blind is > 60% of total number of pixels
254 //
255 if (bads>0.5*entries)
256 {
257 *fLog << err << "ERROR - More than 50% unsuitable pixels... something must be wrong!" << endl;
258 return kFALSE;
259 }
260
261 return kTRUE;
262}
263
264Bool_t MBadPixelsCalc::CheckPedestalRms(MBadPixelsCam &cam, const MPedPhotCam &ped, MBadPixelsPix::UnsuitableType_t t)
265{
266 MBadPixelsCam *store1 = fBadPixels;
267 const MPedPhotCam *store2 = fPedPhotCam;
268
269 fBadPixels = &cam;
270 fPedPhotCam = &ped;
271
272 const Bool_t rc = CheckPedestalRms(t);
273
274 fBadPixels = store1;
275 fPedPhotCam = store2;
276
277 return rc;
278}
279
280Bool_t MBadPixelsCalc::CheckPedestalRms(MBadPixelsCam &cam, const MPedestalCam &ped, MBadPixelsPix::UnsuitableType_t t)
281{
282 return CheckPedestalRms(cam, MPedPhotCam(ped), t);
283}
284
285// --------------------------------------------------------------------------
286//
287//
288Int_t MBadPixelsCalc::Process()
289{
290 return fCheckInProcess ? CheckPedestalRms(MBadPixelsPix::kUnsuitableEvt) : kTRUE;
291}
292
293// --------------------------------------------------------------------------
294//
295//
296Int_t MBadPixelsCalc::PostProcess()
297{
298 return fCheckInPostProcess ? CheckPedestalRms(MBadPixelsPix::kUnsuitableRun) : kTRUE;
299}
300
301// --------------------------------------------------------------------------
302//
303// Read the setup from a TEnv, eg:
304// MBadPixelsCalc.PedestalLevel: 3.0
305// MBadPixelsCalc.PedestalLevelVariance: 3.0
306//
307Int_t MBadPixelsCalc::ReadEnv(const TEnv &env, TString prefix, Bool_t print)
308{
309 Bool_t rc = kFALSE;
310 if (IsEnvDefined(env, prefix, "PedestalLevel", print))
311 {
312 rc = kTRUE;
313 SetPedestalLevel(GetEnvValue(env, prefix, "PedestalLevel", fPedestalLevel));
314 }
315
316 if (IsEnvDefined(env, prefix, "PedestalLevelVariance", print))
317 {
318 rc = kTRUE;
319 SetPedestalLevelVariance(GetEnvValue(env, prefix, "PedestalLevelVariance", fPedestalLevelVariance));
320 }
321 return rc;
322}
Note: See TracBrowser for help on using the repository browser.