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

Last change on this file since 3472 was 3126, checked in by tbretz, 21 years ago
*** empty log message ***
File size: 9.0 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// MBadPixelsCam
39//
40// Output Containers:
41// MCerPhotEvt
42//
43/////////////////////////////////////////////////////////////////////////////
44#include "MBadPixelsTreat.h"
45
46#include <fstream>
47
48#include "MLog.h"
49#include "MLogManip.h"
50
51#include "MParList.h"
52
53#include "MGeomPix.h"
54#include "MGeomCam.h"
55
56#include "MCerPhotPix.h"
57#include "MCerPhotEvt.h"
58
59#include "MPedPhotPix.h"
60#include "MPedPhotCam.h"
61
62#include "MBadPixelsPix.h"
63#include "MBadPixelsCam.h"
64
65ClassImp(MBadPixelsTreat);
66
67using namespace std;
68
69static const TString gsDefName = "MBadPixelsTreat";
70static const TString gsDefTitle = "Task to treat bad pixels (interpolation, unmapping)";
71
72// --------------------------------------------------------------------------
73//
74// Default constructor.
75//
76MBadPixelsTreat::MBadPixelsTreat(const char *name, const char *title)
77 : fFlags(0)
78{
79 fName = name ? name : gsDefName.Data();
80 fTitle = title ? title : gsDefTitle.Data();
81}
82
83// --------------------------------------------------------------------------
84//
85// - Try to find or create MBlindPixels in parameter list.
86// - get the MCerPhotEvt from the parlist (abort if missing)
87// - if no pixels are given by the user try to determin the starfield
88// from the monte carlo run header.
89//
90Int_t MBadPixelsTreat::PreProcess (MParList *pList)
91{
92 fBadPixels = (MBadPixelsCam*)pList->FindObject(AddSerialNumber("MBadPixelsCam"));
93 if (!fBadPixels)
94 {
95 *fLog << err << "MBadPixelsCam not found... aborting." << endl;
96 return kFALSE;
97 }
98
99 fPedPhot = (MPedPhotCam*)pList->FindObject(AddSerialNumber("MPedPhotCam"));
100 if (!fPedPhot)
101 {
102 *fLog << err << "MPedPhotCam not found... aborting." << endl;
103 return kFALSE;
104 }
105
106 fEvt = (MCerPhotEvt*)pList->FindObject(AddSerialNumber("MCerPhotEvt"));
107 if (!fEvt)
108 {
109 *fLog << err << "MCerPhotEvt not found... aborting." << endl;
110 return kFALSE;
111 }
112
113 fGeomCam = (MGeomCam*)pList->FindObject(AddSerialNumber("MGeomCam"));
114 if (!fGeomCam && TESTBIT(fFlags, kUseInterpolation))
115 {
116 *fLog << err << "No camera geometry available... can't use interpolation." << endl;
117 return kFALSE;
118 }
119
120 return kTRUE;
121}
122
123// --------------------------------------------------------------------------
124//
125// Replaces each pixel (signal, signal error, pedestal, pedestal rms)
126// by the average of its surrounding pixels.
127// If TESTBIT(fFlags, kUseCentralPixel) is set the central pixel is also
128// included.
129//
130// NT: Informations about the interpolated pedestals are added.
131// When the option Interpolated is called, the blind pixel with the new
132// values of signal and fluttuation is included in the calculation of
133// the Image Parameters.
134//
135void MBadPixelsTreat::Interpolate() const
136{
137 const UShort_t entries = fGeomCam->GetNumPixels();
138
139 //
140 // Create arrays
141 //
142 Double_t *nphot = new Double_t[entries];
143 Double_t *perr = new Double_t[entries];
144 Double_t *ped = new Double_t[entries];
145 Double_t *pedrms = new Double_t[entries];
146
147 //
148 // Loop over all pixels
149 //
150 for (UShort_t i=0; i<entries; i++)
151 {
152 //
153 // Check whether pixel with idx i is blind
154 //
155 if ((*fBadPixels)[i].IsBad())
156 continue;
157
158 //
159 // Get a pointer to this pixel. If it is not yet existing
160 // create a new entry for this pixel in MCerPhotEvt
161 //
162 const MCerPhotPix *pix = fEvt->GetPixById(i);
163 if (!pix)
164 pix = fEvt->AddPixel(i, 0, 0);
165
166 //
167 // Get the corresponding geometry and pedestal
168 //
169 const MGeomPix &gpix = (*fGeomCam)[i];
170 const MPedPhotPix &ppix = (*fPedPhot)[i];
171
172 // Do Not-Use-Central-Pixel
173 const Bool_t nucp = !TESTBIT(fFlags, kUseCentralPixel);
174
175 Int_t num = nucp ? 0 : 1;
176
177 nphot[i] = nucp ? 0 : pix->GetNumPhotons();
178 ped[i] = nucp ? 0 : ppix.GetMean();
179 perr[i] = nucp ? 0 : Sqr(pix->GetErrorPhot());
180 pedrms[i] = nucp ? 0 : Sqr(ppix.GetRms());
181
182 //
183 // The values are rescaled to the small pixels area for the right comparison
184 //
185 const Double_t ratio = fGeomCam->GetPixRatio(i);
186
187 nphot[i] *= ratio;
188 ped[i] *= ratio;
189 perr[i] *= Sqr(ratio);
190 pedrms[i] *= Sqr(pedrms[i]);
191
192 //
193 // Loop over all its neighbors
194 //
195 const Int_t n = gpix.GetNumNeighbors();
196 for (int j=0; j<n; j++)
197 {
198 const UShort_t nidx = gpix.GetNeighbor(j);
199
200 //
201 // Do not use blind neighbors
202 //
203 if ((*fBadPixels)[i].IsBad())
204 continue;
205
206 //
207 // Check whether the neighbor has a signal stored
208 //
209 const MCerPhotPix *evtpix = fEvt->GetPixById(nidx);
210 if (!evtpix)
211 continue;
212
213 //
214 // Get the geometry for the neighbor
215 //
216 const Double_t nratio = fGeomCam->GetPixRatio(nidx);
217 MPedPhotPix &nppix = (*fPedPhot)[nidx];
218
219 //
220 //The error is calculated as the quadratic sum of the errors
221 //
222 ped[i] += nratio*nppix.GetMean();
223 nphot[i] += nratio*evtpix->GetNumPhotons();
224 perr[i] += Sqr(nratio*evtpix->GetErrorPhot());
225 pedrms[i] += Sqr(nratio*nppix.GetRms());
226
227 num++;
228 }
229
230 //
231 // Now the mean is calculated and the values rescaled back to the pixel area
232 //
233 nphot[i] /= num*ratio;
234 ped[i] /= num*ratio;
235 perr[i] = TMath::Sqrt(perr[i]/num)*ratio;
236 pedrms[i] = TMath::Sqrt(pedrms[i]/num)*ratio;
237
238 }
239
240 //
241 // Now the new pixel values are calculated and can be replaced in
242 // the corresponding containers
243 //
244 for (UShort_t i=0; i<entries; i++)
245 {
246 //
247 // Do not use blind neighbors
248 //
249 if ((*fBadPixels)[i].IsBad())
250 continue;
251
252 //
253 // It must exist, we have created it in the loop before.
254 //
255 fEvt->GetPixById(i)->Set(nphot[i], perr[i]);
256 (*fPedPhot)[i].Set(ped[i], pedrms[i]);
257 }
258
259 //
260 // Delete the intermediat arrays
261 //
262 delete nphot;
263 delete perr;
264 delete ped;
265 delete pedrms;
266}
267
268// --------------------------------------------------------------------------
269//
270// Removes all blind pixels from the analysis by setting their state
271// to unused.
272//
273void MBadPixelsTreat::Unmap() const
274{
275 const UShort_t entries = fEvt->GetNumPixels();
276
277 //
278 // remove the pixels in fPixelsIdx if they are set to be used,
279 // (set them to 'unused' state)
280 //
281 for (UShort_t i=0; i<entries; i++)
282 {
283 MCerPhotPix &pix = (*fEvt)[i];
284
285 if ((*fBadPixels)[pix.GetPixId()].IsBad())
286 pix.SetPixelUnused();
287 }
288}
289
290// --------------------------------------------------------------------------
291//
292// Treat the blind pixels
293//
294Int_t MBadPixelsTreat::Process()
295{
296 if (TESTBIT(fFlags, kUseInterpolation) && fGeomCam)
297 Interpolate();
298 else
299 Unmap();
300
301 return kTRUE;
302}
303
304void MBadPixelsTreat::StreamPrimitive(ofstream &out) const
305{
306 out << " MBadPixelsTreat " << GetUniqueName();
307 if (fName!=gsDefName || fTitle!=gsDefTitle)
308 {
309 out << "(\"" << fName << "\"";
310 if (fTitle!=gsDefTitle)
311 out << ", \"" << fTitle << "\"";
312 out <<")";
313 }
314 out << ";" << endl;
315
316 if (TESTBIT(fFlags, kUseInterpolation))
317 out << " " << GetUniqueName() << ".SetUseInterpolation();" << endl;
318 if (TESTBIT(fFlags, kUseCentralPixel))
319 out << " " << GetUniqueName() << ".SetUseCentralPixel();" << endl;
320}
Note: See TracBrowser for help on using the repository browser.