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

Last change on this file since 3948 was 3925, checked in by gaug, 21 years ago
*** empty log message ***
File size: 9.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! 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
61ClassImp(MExtractSlidingWindow);
62
63using namespace std;
64
65const Byte_t MExtractSlidingWindow::fgHiGainFirst = 0;
66const Byte_t MExtractSlidingWindow::fgHiGainLast = 14;
67const Byte_t MExtractSlidingWindow::fgLoGainFirst = 3;
68const Byte_t MExtractSlidingWindow::fgLoGainLast = 14;
69const Byte_t MExtractSlidingWindow::fgHiGainWindowSize = 6;
70const Byte_t MExtractSlidingWindow::fgLoGainWindowSize = 6;
71// --------------------------------------------------------------------------
72//
73// Default constructor.
74//
75// Sets:
76// - fWindowSizeHiGain to fgHiGainWindowSize
77// - fWindowSizeLoGain to fgLoGainWindowSize
78//
79// Calls:
80// - SetRange(fgHiGainFirst, fgHiGainLast, fgLoGainFirst, fgLoGainLast)
81//
82MExtractSlidingWindow::MExtractSlidingWindow(const char *name, const char *title)
83 : fWindowSizeHiGain(fgHiGainWindowSize),
84 fWindowSizeLoGain(fgLoGainWindowSize)
85{
86
87 fName = name ? name : "MExtractSlidingWindow";
88 fTitle = title ? title : "Signal Extractor for a sliding FADC window";
89
90 SetRange(fgHiGainFirst, fgHiGainLast, fgLoGainFirst, fgLoGainLast);
91}
92
93// --------------------------------------------------------------------------
94//
95// SetRange:
96//
97// Calls:
98// - MExtractor::SetRange(hifirst,hilast,lofirst,lolast);
99// - SetWindowSize(fWindowSizeHiGain,fWindowSizeLoGain);
100//
101void MExtractSlidingWindow::SetRange(Byte_t hifirst, Byte_t hilast, Byte_t lofirst, Byte_t lolast)
102{
103
104 MExtractor::SetRange(hifirst,hilast,lofirst,lolast);
105
106 //
107 // Redo the checks if the window is still inside the ranges
108 //
109
110 SetWindowSize(fWindowSizeHiGain,fWindowSizeLoGain);
111
112}
113
114// --------------------------------------------------------------------------
115//
116// Checks:
117// - if a window is odd, subtract one
118// - if a window is bigger than the one defined by the ranges, set it to the available range
119// - if a window is smaller than 2, set it to 2
120//
121// Sets:
122// - fNumHiGainSamples to: (Float_t)fWindowSizeHiGain
123// - fNumLoGainSamples to: (Float_t)fWindowSizeLoGain
124// - fSqrtHiGainSamples to: TMath::Sqrt(fNumHiGainSamples)
125// - fSqrtLoGainSamples to: TMath::Sqrt(fNumLoGainSamples)
126//
127void MExtractSlidingWindow::SetWindowSize(Byte_t windowh, Byte_t windowl)
128{
129
130 fWindowSizeHiGain = windowh & ~1;
131 fWindowSizeLoGain = windowl & ~1;
132
133 if (fWindowSizeHiGain != windowh)
134 *fLog << warn << GetDescriptor() << ": Hi Gain window size has to be even, set to: "
135 << int(fWindowSizeHiGain) << " samples " << endl;
136
137 if (fWindowSizeLoGain != windowl)
138 *fLog << warn << GetDescriptor() << ": Lo Gain window size has to be even, set to: "
139 << int(fWindowSizeLoGain) << " samples " << endl;
140
141 const Byte_t availhirange = (fHiGainLast-fHiGainFirst+1) & ~1;
142 const Byte_t availlorange = (fLoGainLast-fLoGainFirst+1) & ~1;
143
144 if (fWindowSizeHiGain > availhirange)
145 {
146 *fLog << warn << GetDescriptor()
147 << Form("%s%2i%s%2i%s%2i%s",": Hi Gain window size: ",(int)fWindowSizeHiGain,
148 " is bigger than available range: [",(int)fHiGainFirst,",",(int)fHiGainLast,"]") << endl;
149 *fLog << warn << GetDescriptor()
150 << ": Will set window size to: " << (int)availhirange << endl;
151 fWindowSizeHiGain = availhirange;
152 }
153
154 if (fWindowSizeLoGain > availlorange)
155 {
156 *fLog << warn << GetDescriptor()
157 << Form("%s%2i%s%2i%s%2i%s",": Lo Gain window size: ",(int)fWindowSizeLoGain,
158 " is bigger than available range: [",(int)fLoGainFirst,",",(int)fLoGainLast,"]") << endl;
159 *fLog << warn << GetDescriptor()
160 << ": Will set window size to: " << (int)availlorange << endl;
161 fWindowSizeLoGain = availlorange;
162 }
163
164 if (fWindowSizeHiGain<2)
165 {
166 fWindowSizeHiGain = 2;
167 *fLog << warn << GetDescriptor() << ": Hi Gain window size set to two samples" << endl;
168 }
169
170 if (fWindowSizeLoGain<2)
171 {
172 fWindowSizeLoGain = 2;
173 *fLog << warn << GetDescriptor() << ": Lo Gain window size set to two samples" << endl;
174 }
175
176 fNumHiGainSamples = (Float_t)fWindowSizeHiGain;
177 fNumLoGainSamples = (Float_t)fWindowSizeLoGain;
178
179 fSqrtHiGainSamples = TMath::Sqrt(fNumHiGainSamples);
180 fSqrtLoGainSamples = TMath::Sqrt(fNumLoGainSamples);
181
182}
183
184
185// --------------------------------------------------------------------------
186//
187// FindSignalHiGain:
188//
189// - Loop from ptr to (ptr+fWindowSizeHiGain)
190// - Sum up contents of *ptr
191// - If *ptr is greater than fSaturationLimit, raise sat by 1
192// - Loop from (ptr+fWindowSizeHiGain) to (ptr+fHiGainLast-fHiGainFirst)
193// - Sum the content of *(ptr+fWindowSizeHiGain) and subtract *ptr
194// - Check if the sum has become bigger and store it in case yes.
195//
196void MExtractSlidingWindow::FindSignalHiGain(Byte_t *ptr, Byte_t *logain, Int_t &max, Byte_t &sat) const
197{
198 const Byte_t *end = ptr + fHiGainLast - fHiGainFirst + 1;
199
200 Int_t sum=0;
201
202 //
203 // Calculate the sum of the first fWindowSize slices
204 //
205 sat = 0;
206 Byte_t *p = ptr;
207
208 while (p<ptr+fWindowSizeHiGain-fHiLoLast)
209 {
210 sum += *p;
211 if (*p++ >= fSaturationLimit)
212 sat++;
213 }
214
215 //
216 // Check for saturation in all other slices
217 //
218 while (p<end)
219 if (*p++ >= fSaturationLimit)
220 sat++;
221
222 //
223 // If part of the "low-Gain" slices are used,
224 // repeat steps one and two for the logain part until fHiLoLast
225 //
226 Byte_t *l = logain;
227 while (l<logain+fHiLoLast)
228 {
229 sum += *l;
230 if (*l++ >= fSaturationLimit)
231 sat++;
232 }
233
234
235 //
236 // Calculate the i-th sum as
237 // sum_i+1 = sum_i + slice[i+8] - slice[i]
238 // This is fast and accurate (because we are using int's)
239 //
240 max=sum;
241 for (p=ptr; p+fWindowSizeHiGain-fHiLoLast<end; p++)
242 {
243 sum += *(p+fWindowSizeHiGain-fHiLoLast) - *p;
244 if (sum>max)
245 max = sum;
246 }
247
248 for (l=logain; l<logain+fHiLoLast; l++)
249 {
250 sum += *l - *p++;
251 if (sum>max)
252 max = sum;
253 }
254
255}
256
257
258// --------------------------------------------------------------------------
259//
260// FindSignalLoGain:
261//
262// - Loop from ptr to (ptr+fWindowSizeLoGain)
263// - Sum up contents of *ptr
264// - If *ptr is greater than fSaturationLimit, raise sat by 1
265// - Loop from (ptr+fWindowSizeLoGain) to (ptr+fLoGainLast-fLoGainFirst)
266// - Sum the content of *(ptr+fWindowSizeLoGain) and subtract *ptr
267// - Check if the sum has become bigger and store it in case yes.
268//
269void MExtractSlidingWindow::FindSignalLoGain(Byte_t *ptr, Int_t &max, Byte_t &sat) const
270{
271 const Byte_t *end = ptr + fLoGainLast - fLoGainFirst + 1;
272
273 Int_t sum=0;
274
275 //
276 // Calculate the sum of the first fWindowSize slices
277 //
278 sat = 0;
279 Byte_t *p = ptr;
280 while (p<ptr+fWindowSizeLoGain)
281 {
282 sum += *p;
283 if (*p++ >= fSaturationLimit)
284 sat++;
285 }
286
287 //
288 // Check for saturation in all other slices
289 //
290 while (p<end)
291 if (*p++ >= fSaturationLimit)
292 sat++;
293
294 //
295 // Calculate the i-th sum as
296 // sum_i+1 = sum_i + slice[i+8] - slice[i]
297 // This is fast and accurate (because we are using int's)
298 //
299 max=sum;
300 for (p=ptr; p+fWindowSizeLoGain<end; p++)
301 {
302 sum += *(p+fWindowSizeLoGain) - *p;
303 if (sum>max)
304 max = sum;
305 }
306}
307
Note: See TracBrowser for help on using the repository browser.