source: trunk/MagicSoft/Mars/msignal/MExtractSlidingWindow.cc@ 4093

Last change on this file since 4093 was 3951, checked in by gaug, 21 years ago
*** empty log message ***
File size: 9.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): Thomas Bretz, 02/2004 <mailto:tbretz@astro.uni-wuerzburg.de>
19! Hendrik Bartko, 01/2004 <mailto:hbartko@mppmu.mpg.de>
20! Markus Gaug , 04/2004 <mailto:markus@ifae.es>
21!
22! Copyright: MAGIC Software Development, 2000-2004
23!
24!
25\* ======================================================================== */
26
27//////////////////////////////////////////////////////////////////////////////
28//
29// MExtractSlidingWindow
30//
31// Extracts the signal from a sliding window of size fHiGainWindowSize and
32// fLoGainWindowSize. The signal is the one which maximizes the integral
33// contents.
34//
35// Call: SetRange(higainfirst, higainlast, logainfirst, logainlast)
36// to modify the ranges in which the window is allowed to move.
37// Defaults are:
38//
39// fHiGainFirst = fgHiGainFirst = 0
40// fHiGainLast = fgHiGainLast = 14
41// fLoGainFirst = fgLoGainFirst = 3
42// fLoGainLast = fgLoGainLast = 14
43//
44// Call: SetWindowSize(windowhigain, windowlogain)
45// to modify the sliding window widths. Windows have to be an even number.
46// In case of odd numbers, the window will be modified.
47//
48// Defaults are:
49//
50// fHiGainWindowSize = fgHiGainWindowSize = 6
51// fLoGainWindowSize = fgLoGainWindowSize = 6
52//
53//////////////////////////////////////////////////////////////////////////////
54#include "MExtractSlidingWindow.h"
55#include "MExtractor.h"
56
57#include "MLog.h"
58#include "MLogManip.h"
59
60#include "MExtractedSignalCam.h"
61
62ClassImp(MExtractSlidingWindow);
63
64using namespace std;
65
66const Byte_t MExtractSlidingWindow::fgHiGainFirst = 0;
67const Byte_t MExtractSlidingWindow::fgHiGainLast = 14;
68const Byte_t MExtractSlidingWindow::fgLoGainFirst = 3;
69const Byte_t MExtractSlidingWindow::fgLoGainLast = 14;
70const Byte_t MExtractSlidingWindow::fgHiGainWindowSize = 6;
71const Byte_t MExtractSlidingWindow::fgLoGainWindowSize = 6;
72// --------------------------------------------------------------------------
73//
74// Default constructor.
75//
76// Sets:
77// - fWindowSizeHiGain to fgHiGainWindowSize
78// - fWindowSizeLoGain to fgLoGainWindowSize
79//
80// Calls:
81// - SetRange(fgHiGainFirst, fgHiGainLast, fgLoGainFirst, fgLoGainLast)
82//
83MExtractSlidingWindow::MExtractSlidingWindow(const char *name, const char *title)
84 : fWindowSizeHiGain(fgHiGainWindowSize),
85 fWindowSizeLoGain(fgLoGainWindowSize)
86{
87
88 fName = name ? name : "MExtractSlidingWindow";
89 fTitle = title ? title : "Signal Extractor for a sliding FADC window";
90
91 SetRange(fgHiGainFirst, fgHiGainLast, fgLoGainFirst, fgLoGainLast);
92}
93
94// --------------------------------------------------------------------------
95//
96// SetRange:
97//
98// Calls:
99// - MExtractor::SetRange(hifirst,hilast,lofirst,lolast);
100// - SetWindowSize(fWindowSizeHiGain,fWindowSizeLoGain);
101//
102void MExtractSlidingWindow::SetRange(Byte_t hifirst, Byte_t hilast, Byte_t lofirst, Byte_t lolast)
103{
104
105 MExtractor::SetRange(hifirst,hilast,lofirst,lolast);
106
107 //
108 // Redo the checks if the window is still inside the ranges
109 //
110
111 SetWindowSize(fWindowSizeHiGain,fWindowSizeLoGain);
112
113}
114
115
116// --------------------------------------------------------------------------
117//
118// The ReInit calls:
119// - MExtractor::ReInit()
120// - fSignals->SetUsedFADCSlices(fHiGainFirst, fHiGainLast+fHiLoLast, fNumHiGainSamples,
121// fLoGainFirst, fLoGainLast, fNumLoGainSamples);
122//
123Bool_t MExtractSlidingWindow::ReInit(MParList *pList)
124{
125
126 MExtractor::ReInit(pList);
127
128 fSignals->SetUsedFADCSlices(fHiGainFirst, fHiGainLast+fHiLoLast, fNumHiGainSamples,
129 fLoGainFirst, fLoGainLast, fNumLoGainSamples);
130
131 return kTRUE;
132
133}
134
135// --------------------------------------------------------------------------
136//
137// Checks:
138// - if a window is odd, subtract one
139// - if a window is bigger than the one defined by the ranges, set it to the available range
140// - if a window is smaller than 2, set it to 2
141//
142// Sets:
143// - fNumHiGainSamples to: (Float_t)fWindowSizeHiGain
144// - fNumLoGainSamples to: (Float_t)fWindowSizeLoGain
145// - fSqrtHiGainSamples to: TMath::Sqrt(fNumHiGainSamples)
146// - fSqrtLoGainSamples to: TMath::Sqrt(fNumLoGainSamples)
147//
148void MExtractSlidingWindow::SetWindowSize(Byte_t windowh, Byte_t windowl)
149{
150
151 fWindowSizeHiGain = windowh & ~1;
152 fWindowSizeLoGain = windowl & ~1;
153
154 if (fWindowSizeHiGain != windowh)
155 *fLog << warn << GetDescriptor() << ": Hi Gain window size has to be even, set to: "
156 << int(fWindowSizeHiGain) << " samples " << endl;
157
158 if (fWindowSizeLoGain != windowl)
159 *fLog << warn << GetDescriptor() << ": Lo Gain window size has to be even, set to: "
160 << int(fWindowSizeLoGain) << " samples " << endl;
161
162 const Byte_t availhirange = (fHiGainLast-fHiGainFirst+1) & ~1;
163 const Byte_t availlorange = (fLoGainLast-fLoGainFirst+1) & ~1;
164
165 if (fWindowSizeHiGain > availhirange)
166 {
167 *fLog << warn << GetDescriptor()
168 << Form("%s%2i%s%2i%s%2i%s",": Hi Gain window size: ",(int)fWindowSizeHiGain,
169 " is bigger than available range: [",(int)fHiGainFirst,",",(int)fHiGainLast,"]") << endl;
170 *fLog << warn << GetDescriptor()
171 << ": Will set window size to: " << (int)availhirange << endl;
172 fWindowSizeHiGain = availhirange;
173 }
174
175 if (fWindowSizeLoGain > availlorange)
176 {
177 *fLog << warn << GetDescriptor()
178 << Form("%s%2i%s%2i%s%2i%s",": Lo Gain window size: ",(int)fWindowSizeLoGain,
179 " is bigger than available range: [",(int)fLoGainFirst,",",(int)fLoGainLast,"]") << endl;
180 *fLog << warn << GetDescriptor()
181 << ": Will set window size to: " << (int)availlorange << endl;
182 fWindowSizeLoGain = availlorange;
183 }
184
185 if (fWindowSizeHiGain<2)
186 {
187 fWindowSizeHiGain = 2;
188 *fLog << warn << GetDescriptor() << ": Hi Gain window size set to two samples" << endl;
189 }
190
191 if (fWindowSizeLoGain<2)
192 {
193 fWindowSizeLoGain = 2;
194 *fLog << warn << GetDescriptor() << ": Lo Gain window size set to two samples" << endl;
195 }
196
197 fNumHiGainSamples = (Float_t)fWindowSizeHiGain;
198 fNumLoGainSamples = (Float_t)fWindowSizeLoGain;
199
200 fSqrtHiGainSamples = TMath::Sqrt(fNumHiGainSamples);
201 fSqrtLoGainSamples = TMath::Sqrt(fNumLoGainSamples);
202
203}
204
205
206// --------------------------------------------------------------------------
207//
208// FindSignalHiGain:
209//
210// - Loop from ptr to (ptr+fWindowSizeHiGain)
211// - Sum up contents of *ptr
212// - If *ptr is greater than fSaturationLimit, raise sat by 1
213// - Loop from (ptr+fWindowSizeHiGain) to (ptr+fHiGainLast-fHiGainFirst)
214// - Sum the content of *(ptr+fWindowSizeHiGain) and subtract *ptr
215// - Check if the sum has become bigger and store it in case yes.
216//
217void MExtractSlidingWindow::FindSignalHiGain(Byte_t *ptr, Byte_t *logain, Int_t &max, Byte_t &sat) const
218{
219 const Byte_t *end = ptr + fHiGainLast - fHiGainFirst + 1;
220
221 Int_t sum=0;
222
223 //
224 // Calculate the sum of the first fWindowSize slices
225 //
226 sat = 0;
227 Byte_t *p = ptr;
228
229 while (p<ptr+fWindowSizeHiGain-fHiLoLast)
230 {
231 sum += *p;
232 if (*p++ >= fSaturationLimit)
233 sat++;
234 }
235
236 //
237 // Check for saturation in all other slices
238 //
239 while (p<end)
240 if (*p++ >= fSaturationLimit)
241 sat++;
242
243 //
244 // If part of the "low-Gain" slices are used,
245 // repeat steps one and two for the logain part until fHiLoLast
246 //
247 Byte_t *l = logain;
248 while (l<logain+fHiLoLast)
249 {
250 sum += *l;
251 if (*l++ >= fSaturationLimit)
252 sat++;
253 }
254
255
256 //
257 // Calculate the i-th sum as
258 // sum_i+1 = sum_i + slice[i+8] - slice[i]
259 // This is fast and accurate (because we are using int's)
260 //
261 max=sum;
262 for (p=ptr; p+fWindowSizeHiGain-fHiLoLast<end; p++)
263 {
264 sum += *(p+fWindowSizeHiGain-fHiLoLast) - *p;
265 if (sum>max)
266 max = sum;
267 }
268
269 for (l=logain; l<logain+fHiLoLast; l++)
270 {
271 sum += *l - *p++;
272 if (sum>max)
273 max = sum;
274 }
275
276}
277
278
279// --------------------------------------------------------------------------
280//
281// FindSignalLoGain:
282//
283// - Loop from ptr to (ptr+fWindowSizeLoGain)
284// - Sum up contents of *ptr
285// - If *ptr is greater than fSaturationLimit, raise sat by 1
286// - Loop from (ptr+fWindowSizeLoGain) to (ptr+fLoGainLast-fLoGainFirst)
287// - Sum the content of *(ptr+fWindowSizeLoGain) and subtract *ptr
288// - Check if the sum has become bigger and store it in case yes.
289//
290void MExtractSlidingWindow::FindSignalLoGain(Byte_t *ptr, Int_t &max, Byte_t &sat) const
291{
292 const Byte_t *end = ptr + fLoGainLast - fLoGainFirst + 1;
293
294 Int_t sum=0;
295
296 //
297 // Calculate the sum of the first fWindowSize slices
298 //
299 sat = 0;
300 Byte_t *p = ptr;
301 while (p<ptr+fWindowSizeLoGain)
302 {
303 sum += *p;
304 if (*p++ >= fSaturationLimit)
305 sat++;
306 }
307
308 //
309 // Check for saturation in all other slices
310 //
311 while (p<end)
312 if (*p++ >= fSaturationLimit)
313 sat++;
314
315 //
316 // Calculate the i-th sum as
317 // sum_i+1 = sum_i + slice[i+8] - slice[i]
318 // This is fast and accurate (because we are using int's)
319 //
320 max=sum;
321 for (p=ptr; p+fWindowSizeLoGain<end; p++)
322 {
323 sum += *(p+fWindowSizeLoGain) - *p;
324 if (sum>max)
325 max = sum;
326 }
327}
328
Note: See TracBrowser for help on using the repository browser.