| 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 | !   Author(s): Hendrik Bartko, 01/2004 <mailto:hbartko@mppmu.mpg.de> | 
|---|
| 20 | !   Author(s): 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 |  | 
|---|
| 62 | ClassImp(MExtractSlidingWindow); | 
|---|
| 63 |  | 
|---|
| 64 | using namespace std; | 
|---|
| 65 |  | 
|---|
| 66 | const Byte_t MExtractSlidingWindow::fgHiGainFirst      = 0; | 
|---|
| 67 | const Byte_t MExtractSlidingWindow::fgHiGainLast       = 14; | 
|---|
| 68 | const Byte_t MExtractSlidingWindow::fgLoGainFirst      = 3; | 
|---|
| 69 | const Byte_t MExtractSlidingWindow::fgLoGainLast       = 14; | 
|---|
| 70 | const Byte_t MExtractSlidingWindow::fgHiGainWindowSize = 6; | 
|---|
| 71 | const 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 | // | 
|---|
| 83 | MExtractSlidingWindow::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 | // | 
|---|
| 102 | void 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 | // | 
|---|
| 123 | Bool_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 |  | 
|---|
| 132 | *fLog << dec << endl; | 
|---|
| 133 | *fLog << inf << "Taking " << fNumHiGainSamples | 
|---|
| 134 | << " HiGain samples starting with slice " << (Int_t)fHiGainFirst | 
|---|
| 135 | << " to " << (Int_t)(fHiGainLast+fHiLoLast) << " incl" << endl; | 
|---|
| 136 | *fLog << inf << "Taking " << fNumLoGainSamples | 
|---|
| 137 | << " LoGain samples starting with slice " << (Int_t)fLoGainFirst | 
|---|
| 138 | << " to " << (Int_t)fLoGainLast << " incl" << endl; | 
|---|
| 139 |  | 
|---|
| 140 | return kTRUE; | 
|---|
| 141 |  | 
|---|
| 142 | } | 
|---|
| 143 |  | 
|---|
| 144 | // -------------------------------------------------------------------------- | 
|---|
| 145 | // | 
|---|
| 146 | // Checks: | 
|---|
| 147 | // - if a window is odd, subtract one | 
|---|
| 148 | // - if a window is bigger than the one defined by the ranges, set it to the available range | 
|---|
| 149 | // - if a window is smaller than 2, set it to 2 | 
|---|
| 150 | // | 
|---|
| 151 | // Sets: | 
|---|
| 152 | // - fNumHiGainSamples to: (Float_t)fWindowSizeHiGain | 
|---|
| 153 | // - fNumLoGainSamples to: (Float_t)fWindowSizeLoGain | 
|---|
| 154 | // - fSqrtHiGainSamples to: TMath::Sqrt(fNumHiGainSamples) | 
|---|
| 155 | // - fSqrtLoGainSamples to: TMath::Sqrt(fNumLoGainSamples) | 
|---|
| 156 | // | 
|---|
| 157 | void MExtractSlidingWindow::SetWindowSize(Byte_t windowh, Byte_t windowl) | 
|---|
| 158 | { | 
|---|
| 159 |  | 
|---|
| 160 | fWindowSizeHiGain = windowh & ~1; | 
|---|
| 161 | fWindowSizeLoGain = windowl & ~1; | 
|---|
| 162 |  | 
|---|
| 163 | if (fWindowSizeHiGain != windowh) | 
|---|
| 164 | *fLog << warn << GetDescriptor() << ": Hi Gain window size has to be even, set to: " | 
|---|
| 165 | << int(fWindowSizeHiGain) << " samples " << endl; | 
|---|
| 166 |  | 
|---|
| 167 | if (fWindowSizeLoGain != windowl) | 
|---|
| 168 | *fLog << warn << GetDescriptor() << ": Lo Gain window size has to be even, set to: " | 
|---|
| 169 | << int(fWindowSizeLoGain) << " samples " << endl; | 
|---|
| 170 |  | 
|---|
| 171 | const Byte_t availhirange = (fHiGainLast-fHiGainFirst+1) & ~1; | 
|---|
| 172 |  | 
|---|
| 173 | if (fWindowSizeHiGain > availhirange) | 
|---|
| 174 | { | 
|---|
| 175 | *fLog << warn << GetDescriptor() | 
|---|
| 176 | << Form("%s%2i%s%2i%s%2i%s",": Hi Gain window size: ",(int)fWindowSizeHiGain, | 
|---|
| 177 | " is bigger than available range: [",(int)fHiGainFirst,",",(int)fHiGainLast,"]") << endl; | 
|---|
| 178 | *fLog << warn << GetDescriptor() | 
|---|
| 179 | << ": Will set window size to: " << (int)availhirange << endl; | 
|---|
| 180 | fWindowSizeHiGain = availhirange; | 
|---|
| 181 | } | 
|---|
| 182 |  | 
|---|
| 183 | if (fWindowSizeHiGain<2) | 
|---|
| 184 | { | 
|---|
| 185 | fWindowSizeHiGain = 2; | 
|---|
| 186 | *fLog << warn << GetDescriptor() << ": Hi Gain window size set to two samples" << endl; | 
|---|
| 187 | } | 
|---|
| 188 |  | 
|---|
| 189 | if (fLoGainLast != 0 && fWindowSizeLoGain != 0) | 
|---|
| 190 | { | 
|---|
| 191 | const Byte_t availlorange = (fLoGainLast-fLoGainFirst+1) & ~1; | 
|---|
| 192 |  | 
|---|
| 193 | if (fWindowSizeLoGain > availlorange) | 
|---|
| 194 | { | 
|---|
| 195 | *fLog << warn << GetDescriptor() | 
|---|
| 196 | << Form("%s%2i%s%2i%s%2i%s",": Lo Gain window size: ",(int)fWindowSizeLoGain, | 
|---|
| 197 | " is bigger than available range: [",(int)fLoGainFirst,",",(int)fLoGainLast,"]") << endl; | 
|---|
| 198 | *fLog << warn << GetDescriptor() | 
|---|
| 199 | << ": Will set window size to: " << (int)availlorange << endl; | 
|---|
| 200 | fWindowSizeLoGain = availlorange; | 
|---|
| 201 | } | 
|---|
| 202 |  | 
|---|
| 203 | if (fWindowSizeLoGain<2) | 
|---|
| 204 | { | 
|---|
| 205 | fWindowSizeLoGain = 2; | 
|---|
| 206 | *fLog << warn << GetDescriptor() << ": Lo Gain window size set to two samples" << endl; | 
|---|
| 207 | } | 
|---|
| 208 | } | 
|---|
| 209 |  | 
|---|
| 210 | fNumHiGainSamples = (Float_t)fWindowSizeHiGain; | 
|---|
| 211 | fNumLoGainSamples = (Float_t)fWindowSizeLoGain; | 
|---|
| 212 |  | 
|---|
| 213 | fSqrtHiGainSamples = TMath::Sqrt(fNumHiGainSamples); | 
|---|
| 214 | fSqrtLoGainSamples = TMath::Sqrt(fNumLoGainSamples); | 
|---|
| 215 |  | 
|---|
| 216 | } | 
|---|
| 217 |  | 
|---|
| 218 |  | 
|---|
| 219 | // -------------------------------------------------------------------------- | 
|---|
| 220 | // | 
|---|
| 221 | // FindSignalHiGain: | 
|---|
| 222 | // | 
|---|
| 223 | // - Loop from ptr to (ptr+fWindowSizeHiGain) | 
|---|
| 224 | // - Sum up contents of *ptr | 
|---|
| 225 | // - If *ptr is greater than fSaturationLimit, raise sat by 1 | 
|---|
| 226 | // - Loop from (ptr+fWindowSizeHiGain) to (ptr+fHiGainLast-fHiGainFirst) | 
|---|
| 227 | // - Sum the content of *(ptr+fWindowSizeHiGain) and subtract *ptr | 
|---|
| 228 | // - Check if the sum has become bigger and store it in case yes. | 
|---|
| 229 | // | 
|---|
| 230 | void MExtractSlidingWindow::FindSignalHiGain(Byte_t *ptr, Byte_t *logain, Float_t &max, Byte_t &sat) const | 
|---|
| 231 | { | 
|---|
| 232 | const Byte_t *end = ptr + fHiGainLast - fHiGainFirst + 1; | 
|---|
| 233 |  | 
|---|
| 234 | Int_t sum=0; | 
|---|
| 235 | Int_t sumtot =0; | 
|---|
| 236 |  | 
|---|
| 237 | // | 
|---|
| 238 | // Calculate the sum of the first fWindowSize slices | 
|---|
| 239 | // | 
|---|
| 240 | sat = 0; | 
|---|
| 241 | Byte_t *p = ptr; | 
|---|
| 242 |  | 
|---|
| 243 | while (p<ptr+fWindowSizeHiGain-fHiLoLast) | 
|---|
| 244 | { | 
|---|
| 245 | sum += *p; | 
|---|
| 246 | if (*p++ >= fSaturationLimit) | 
|---|
| 247 | sat++; | 
|---|
| 248 | } | 
|---|
| 249 |  | 
|---|
| 250 | // | 
|---|
| 251 | // Check for saturation in all other slices | 
|---|
| 252 | // | 
|---|
| 253 | while (p<end) | 
|---|
| 254 | if (*p++ >= fSaturationLimit) | 
|---|
| 255 | sat++; | 
|---|
| 256 |  | 
|---|
| 257 | // | 
|---|
| 258 | // If part of the "low-Gain" slices are used, | 
|---|
| 259 | // repeat steps one and two for the logain part until fHiLoLast | 
|---|
| 260 | // | 
|---|
| 261 | Byte_t *l = logain; | 
|---|
| 262 | while (l<logain+fHiLoLast) | 
|---|
| 263 | { | 
|---|
| 264 | sum += *l; | 
|---|
| 265 | if (*l++ >= fSaturationLimit) | 
|---|
| 266 | sat++; | 
|---|
| 267 | } | 
|---|
| 268 |  | 
|---|
| 269 |  | 
|---|
| 270 | // | 
|---|
| 271 | // Calculate the i-th sum as | 
|---|
| 272 | //    sum_i+1 = sum_i + slice[i+8] - slice[i] | 
|---|
| 273 | // This is fast and accurate (because we are using int's) | 
|---|
| 274 | // | 
|---|
| 275 | sumtot=sum; | 
|---|
| 276 | for (p=ptr; p+fWindowSizeHiGain-fHiLoLast<end; p++) | 
|---|
| 277 | { | 
|---|
| 278 | sum += *(p+fWindowSizeHiGain-fHiLoLast) - *p; | 
|---|
| 279 | if (sum>sumtot) | 
|---|
| 280 | sumtot = sum; | 
|---|
| 281 | } | 
|---|
| 282 |  | 
|---|
| 283 | for (l=logain; l<logain+fHiLoLast; l++) | 
|---|
| 284 | { | 
|---|
| 285 | sum += *l - *p++; | 
|---|
| 286 | if (sum>sumtot) | 
|---|
| 287 | sumtot = sum; | 
|---|
| 288 | } | 
|---|
| 289 | max = (Float_t)sumtot; | 
|---|
| 290 | } | 
|---|
| 291 |  | 
|---|
| 292 |  | 
|---|
| 293 | // -------------------------------------------------------------------------- | 
|---|
| 294 | // | 
|---|
| 295 | // FindSignalLoGain: | 
|---|
| 296 | // | 
|---|
| 297 | // - Loop from ptr to (ptr+fWindowSizeLoGain) | 
|---|
| 298 | // - Sum up contents of *ptr | 
|---|
| 299 | // - If *ptr is greater than fSaturationLimit, raise sat by 1 | 
|---|
| 300 | // - Loop from (ptr+fWindowSizeLoGain) to (ptr+fLoGainLast-fLoGainFirst) | 
|---|
| 301 | // - Sum the content of *(ptr+fWindowSizeLoGain) and subtract *ptr | 
|---|
| 302 | // - Check if the sum has become bigger and store it in case yes. | 
|---|
| 303 | // | 
|---|
| 304 | void MExtractSlidingWindow::FindSignalLoGain(Byte_t *ptr, Float_t &max, Byte_t &sat) const | 
|---|
| 305 | { | 
|---|
| 306 | const Byte_t *end = ptr + fLoGainLast - fLoGainFirst + 1; | 
|---|
| 307 |  | 
|---|
| 308 | Int_t sum=0; | 
|---|
| 309 | Int_t sumtot=0; | 
|---|
| 310 | // | 
|---|
| 311 | // Calculate the sum of the first fWindowSize slices | 
|---|
| 312 | // | 
|---|
| 313 | sat = 0; | 
|---|
| 314 | Byte_t *p = ptr; | 
|---|
| 315 | while (p<ptr+fWindowSizeLoGain) | 
|---|
| 316 | { | 
|---|
| 317 | sum += *p; | 
|---|
| 318 | if (*p++ >= fSaturationLimit) | 
|---|
| 319 | sat++; | 
|---|
| 320 | } | 
|---|
| 321 |  | 
|---|
| 322 | // | 
|---|
| 323 | // Check for saturation in all other slices | 
|---|
| 324 | // | 
|---|
| 325 | while (p<end) | 
|---|
| 326 | if (*p++ >= fSaturationLimit) | 
|---|
| 327 | sat++; | 
|---|
| 328 |  | 
|---|
| 329 | // | 
|---|
| 330 | // Calculate the i-th sum as | 
|---|
| 331 | //    sum_i+1 = sum_i + slice[i+8] - slice[i] | 
|---|
| 332 | // This is fast and accurate (because we are using int's) | 
|---|
| 333 | // | 
|---|
| 334 | sumtot=sum; | 
|---|
| 335 | for (p=ptr; p+fWindowSizeLoGain<end; p++) | 
|---|
| 336 | { | 
|---|
| 337 | sum += *(p+fWindowSizeLoGain) - *p; | 
|---|
| 338 | if (sum>sumtot) | 
|---|
| 339 | sumtot = sum; | 
|---|
| 340 | } | 
|---|
| 341 | max = (Float_t)sumtot; | 
|---|
| 342 | } | 
|---|
| 343 |  | 
|---|
| 344 |  | 
|---|