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

Last change on this file since 3924 was 3886, checked in by gaug, 21 years ago
*** empty log message ***
File size: 8.8 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, 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 while (p<ptr+fWindowSizeHiGain)
208 {
209 sum += *p;
210 if (*p++ >= fSaturationLimit)
211 sat++;
212 }
213
214 //
215 // Check for saturation in all other slices
216 //
217 while (p<end)
218 if (*p++ >= fSaturationLimit)
219 sat++;
220
221 //
222 // Calculate the i-th sum as
223 // sum_i+1 = sum_i + slice[i+8] - slice[i]
224 // This is fast and accurate (because we are using int's)
225 //
226 max=sum;
227 for (p=ptr; p+fWindowSizeHiGain<end; p++)
228 {
229 sum += *(p+fWindowSizeHiGain) - *p;
230 if (sum>max)
231 max = sum;
232 }
233}
234
235
236// --------------------------------------------------------------------------
237//
238// FindSignalLoGain:
239//
240// - Loop from ptr to (ptr+fWindowSizeLoGain)
241// - Sum up contents of *ptr
242// - If *ptr is greater than fSaturationLimit, raise sat by 1
243// - Loop from (ptr+fWindowSizeLoGain) to (ptr+fLoGainLast-fLoGainFirst)
244// - Sum the content of *(ptr+fWindowSizeLoGain) and subtract *ptr
245// - Check if the sum has become bigger and store it in case yes.
246//
247void MExtractSlidingWindow::FindSignalLoGain(Byte_t *ptr, Int_t &max, Byte_t &sat) const
248{
249 const Byte_t *end = ptr + fLoGainLast - fLoGainFirst + 1;
250
251 Int_t sum=0;
252
253 //
254 // Calculate the sum of the first fWindowSize slices
255 //
256 sat = 0;
257 Byte_t *p = ptr;
258 while (p<ptr+fWindowSizeLoGain)
259 {
260 sum += *p;
261 if (*p++ >= fSaturationLimit)
262 sat++;
263 }
264
265 //
266 // Check for saturation in all other slices
267 //
268 while (p<end)
269 if (*p++ >= fSaturationLimit)
270 sat++;
271
272 //
273 // Calculate the i-th sum as
274 // sum_i+1 = sum_i + slice[i+8] - slice[i]
275 // This is fast and accurate (because we are using int's)
276 //
277 max=sum;
278 for (p=ptr; p+fWindowSizeLoGain<end; p++)
279 {
280 sum += *(p+fWindowSizeLoGain) - *p;
281 if (sum>max)
282 max = sum;
283 }
284}
285
Note: See TracBrowser for help on using the repository browser.