| 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, 02/2004 <mailto:hbartko@mppmu.mpg.de> | 
|---|
| 20 | ! | 
|---|
| 21 | !   Copyright: MAGIC Software Development, 2000-2004 | 
|---|
| 22 | ! | 
|---|
| 23 | ! | 
|---|
| 24 | \* ======================================================================== */ | 
|---|
| 25 |  | 
|---|
| 26 | ////////////////////////////////////////////////////////////////////////////// | 
|---|
| 27 | // | 
|---|
| 28 | //   MExtractTimeHighestIntegral | 
|---|
| 29 | // | 
|---|
| 30 | //  Calculates the arrival time as the mean time of the fWindowSize time slices | 
|---|
| 31 | //  which have the highest integral content. | 
|---|
| 32 | // | 
|---|
| 33 | // | 
|---|
| 34 | ////////////////////////////////////////////////////////////////////////////// | 
|---|
| 35 | #include "MExtractTimeHighestIntegral.h" | 
|---|
| 36 |  | 
|---|
| 37 | #include "MLog.h" | 
|---|
| 38 | #include "MLogManip.h" | 
|---|
| 39 | #include "MPedestalPix.h" | 
|---|
| 40 |  | 
|---|
| 41 | ClassImp(MExtractTimeHighestIntegral); | 
|---|
| 42 |  | 
|---|
| 43 | using namespace std; | 
|---|
| 44 |  | 
|---|
| 45 | const Byte_t MExtractTimeHighestIntegral::fgHiGainFirst  =  0; | 
|---|
| 46 | const Byte_t MExtractTimeHighestIntegral::fgHiGainLast   = 14; | 
|---|
| 47 | const Byte_t MExtractTimeHighestIntegral::fgLoGainFirst  =  3; | 
|---|
| 48 | const Byte_t MExtractTimeHighestIntegral::fgLoGainLast   = 14; | 
|---|
| 49 | const Byte_t MExtractTimeHighestIntegral::fgHiGainWindowSize = 6; | 
|---|
| 50 | const Byte_t MExtractTimeHighestIntegral::fgLoGainWindowSize = 6; | 
|---|
| 51 | // -------------------------------------------------------------------------- | 
|---|
| 52 | // | 
|---|
| 53 | // Default constructor. | 
|---|
| 54 | // | 
|---|
| 55 | // Calls: | 
|---|
| 56 | // - SetRange(fgHiGainFirst, fgHiGainLast, fgLoGainFirst, fgLoGainLast) | 
|---|
| 57 | // | 
|---|
| 58 | MExtractTimeHighestIntegral::MExtractTimeHighestIntegral(const char *name, const char *title) | 
|---|
| 59 | : fHiGainWindowSize(fgHiGainWindowSize), | 
|---|
| 60 | fLoGainWindowSize(fgLoGainWindowSize) | 
|---|
| 61 | { | 
|---|
| 62 |  | 
|---|
| 63 | fName  = name  ? name  : "MExtractTimeHighestIntegral"; | 
|---|
| 64 | fTitle = title ? title : "Task to extract the signal from the FADC slices"; | 
|---|
| 65 |  | 
|---|
| 66 | SetRange(fgHiGainFirst, fgHiGainLast, fgLoGainFirst, fgLoGainLast); | 
|---|
| 67 | } | 
|---|
| 68 |  | 
|---|
| 69 | // -------------------------------------------------------------------------- | 
|---|
| 70 | // | 
|---|
| 71 | // SetRange: | 
|---|
| 72 | // | 
|---|
| 73 | // Calls: | 
|---|
| 74 | // - MExtractor::SetRange(hifirst,hilast,lofirst,lolast); | 
|---|
| 75 | // | 
|---|
| 76 | void MExtractTimeHighestIntegral::SetRange(Byte_t hifirst, Byte_t hilast, Byte_t lofirst, Byte_t lolast) | 
|---|
| 77 | { | 
|---|
| 78 |  | 
|---|
| 79 | MExtractor::SetRange(hifirst,hilast,lofirst,lolast); | 
|---|
| 80 |  | 
|---|
| 81 | Int_t range = fHiGainLast - fHiGainFirst + 1; | 
|---|
| 82 |  | 
|---|
| 83 | if (range < 2) | 
|---|
| 84 | { | 
|---|
| 85 | *fLog << warn << GetDescriptor() | 
|---|
| 86 | << Form("%s%2i%s%2i%s",": Hi-Gain Extraction range [",(int)fHiGainFirst,"," | 
|---|
| 87 | ,fHiGainLast,"] too small, ") << endl; | 
|---|
| 88 | *fLog << warn << GetDescriptor() | 
|---|
| 89 | << " will move higher limit to obtain 4 slices " << endl; | 
|---|
| 90 | SetRange(fHiGainFirst, fHiGainLast+4-range,fLoGainFirst,fLoGainLast); | 
|---|
| 91 | } | 
|---|
| 92 |  | 
|---|
| 93 | if (fLoGainLast != 0) | 
|---|
| 94 | { | 
|---|
| 95 | range = fLoGainLast - fLoGainFirst + 1; | 
|---|
| 96 |  | 
|---|
| 97 | if (range < 2) | 
|---|
| 98 | { | 
|---|
| 99 | *fLog << warn << GetDescriptor() | 
|---|
| 100 | << Form("%s%2i%s%2i%s",": Lo-Gain Extraction range [",(int)fLoGainFirst,"," | 
|---|
| 101 | ,fLoGainLast,"] too small, ") << endl; | 
|---|
| 102 | *fLog << warn << GetDescriptor() | 
|---|
| 103 | << " will move lower limit to obtain 4 slices " << endl; | 
|---|
| 104 | SetRange(fHiGainFirst, fHiGainLast,fLoGainFirst,fLoGainLast+4-range); | 
|---|
| 105 | } | 
|---|
| 106 | } | 
|---|
| 107 |  | 
|---|
| 108 | SetWindowSize(fHiGainWindowSize,fLoGainWindowSize); | 
|---|
| 109 |  | 
|---|
| 110 | fNumHiGainSamples = fHiGainLast-fHiGainFirst+1; | 
|---|
| 111 | fNumLoGainSamples = fLoGainLast ? fLoGainLast-fLoGainFirst+1 : 0; | 
|---|
| 112 |  | 
|---|
| 113 | } | 
|---|
| 114 |  | 
|---|
| 115 | // -------------------------------------------------------------------------- | 
|---|
| 116 | // | 
|---|
| 117 | // Checks: | 
|---|
| 118 | // - if a window is odd, subtract one | 
|---|
| 119 | // - if a window is bigger than the one defined by the ranges, set it to the available range | 
|---|
| 120 | // - if a window is smaller than 2, set it to 2 | 
|---|
| 121 | // | 
|---|
| 122 | void MExtractTimeHighestIntegral::SetWindowSize(Byte_t windowh, Byte_t windowl) | 
|---|
| 123 | { | 
|---|
| 124 |  | 
|---|
| 125 | fHiGainWindowSize = windowh & ~1; | 
|---|
| 126 | fLoGainWindowSize = windowl & ~1; | 
|---|
| 127 |  | 
|---|
| 128 | if (fHiGainWindowSize != windowh) | 
|---|
| 129 | *fLog << warn << GetDescriptor() << ": Hi Gain window size has to be even, set to: " | 
|---|
| 130 | << int(fHiGainWindowSize) << " samples " << endl; | 
|---|
| 131 |  | 
|---|
| 132 | if (fLoGainWindowSize != windowl) | 
|---|
| 133 | *fLog << warn << GetDescriptor() << ": Lo Gain window size has to be even, set to: " | 
|---|
| 134 | << int(fLoGainWindowSize) << " samples " << endl; | 
|---|
| 135 |  | 
|---|
| 136 | const Byte_t availhirange = (fHiGainLast-fHiGainFirst+1) & ~1; | 
|---|
| 137 | const Byte_t availlorange = (fLoGainLast-fLoGainFirst+1) & ~1; | 
|---|
| 138 |  | 
|---|
| 139 | if (fHiGainWindowSize > availhirange) | 
|---|
| 140 | { | 
|---|
| 141 | *fLog << warn << GetDescriptor() | 
|---|
| 142 | << Form("%s%2i%s%2i%s%2i%s",": Hi Gain window size: ",(int)fHiGainWindowSize, | 
|---|
| 143 | " is bigger than available range: [",(int)fHiGainFirst,",",(int)fHiGainLast,"]") << endl; | 
|---|
| 144 | *fLog << warn << GetDescriptor() | 
|---|
| 145 | << ": Will set window size to: " << (int)availhirange << endl; | 
|---|
| 146 | fHiGainWindowSize = availhirange; | 
|---|
| 147 | } | 
|---|
| 148 |  | 
|---|
| 149 | if (fLoGainWindowSize > availlorange) | 
|---|
| 150 | { | 
|---|
| 151 | *fLog << warn << GetDescriptor() | 
|---|
| 152 | << Form("%s%2i%s%2i%s%2i%s",": Lo Gain window size: ",(int)fLoGainWindowSize, | 
|---|
| 153 | " is bigger than available range: [",(int)fLoGainFirst,",",(int)fLoGainLast,"]") << endl; | 
|---|
| 154 | *fLog << warn << GetDescriptor() | 
|---|
| 155 | << ": Will set window size to: " << (int)availlorange << endl; | 
|---|
| 156 | fLoGainWindowSize= availlorange; | 
|---|
| 157 | } | 
|---|
| 158 |  | 
|---|
| 159 | fHiGainWindowSizeSqrt = TMath::Sqrt((Float_t)fHiGainWindowSize); | 
|---|
| 160 | fLoGainWindowSizeSqrt = TMath::Sqrt((Float_t)fLoGainWindowSize); | 
|---|
| 161 | } | 
|---|
| 162 |  | 
|---|
| 163 |  | 
|---|
| 164 | void MExtractTimeHighestIntegral::FindTimeHiGain(Byte_t *ptr, Float_t &time, Float_t &deltatime, Byte_t &sat, const MPedestalPix &ped) const | 
|---|
| 165 | { | 
|---|
| 166 |  | 
|---|
| 167 | const Byte_t *end = ptr + fHiGainLast - fHiGainFirst +1 ; | 
|---|
| 168 |  | 
|---|
| 169 | Int_t sum=0;    // integral content of the actual window | 
|---|
| 170 | Int_t max = 0;  // highest integral content of all windows | 
|---|
| 171 |  | 
|---|
| 172 | // | 
|---|
| 173 | // Calculate the sum of the first fWindowSize slices | 
|---|
| 174 | // | 
|---|
| 175 | sat = 0; | 
|---|
| 176 | Byte_t *p = ptr; | 
|---|
| 177 |  | 
|---|
| 178 | while (p<ptr+fHiGainWindowSize) | 
|---|
| 179 | { | 
|---|
| 180 | sum += *p; | 
|---|
| 181 | if (*p++ >= fSaturationLimit) | 
|---|
| 182 | sat++; | 
|---|
| 183 | } | 
|---|
| 184 |  | 
|---|
| 185 | // | 
|---|
| 186 | // Check for saturation in all other slices | 
|---|
| 187 | // | 
|---|
| 188 | while (p<end) | 
|---|
| 189 | if (*p++ >= fSaturationLimit) | 
|---|
| 190 | sat++; | 
|---|
| 191 |  | 
|---|
| 192 | // | 
|---|
| 193 | // Calculate the i-th sum as | 
|---|
| 194 | //    sum_i+1 = sum_i + slice[i+8] - slice[i] | 
|---|
| 195 | // This is fast and accurate (because we are using int's) | 
|---|
| 196 | // | 
|---|
| 197 | max=sum; | 
|---|
| 198 | Byte_t *ptrmax=ptr;  // pointer to the first slice of the maximum window | 
|---|
| 199 |  | 
|---|
| 200 | for (p=ptr; p+fHiGainWindowSize<end; p++) | 
|---|
| 201 | { | 
|---|
| 202 | sum += *(p+fHiGainWindowSize) - *p; | 
|---|
| 203 |  | 
|---|
| 204 | if (sum>max) | 
|---|
| 205 | { | 
|---|
| 206 | max = sum; | 
|---|
| 207 | ptrmax = p+1; | 
|---|
| 208 | } | 
|---|
| 209 | } | 
|---|
| 210 |  | 
|---|
| 211 | // now calculate the time for the maximum window | 
|---|
| 212 | Int_t timesignalsum = 0; | 
|---|
| 213 | Int_t timesquaredsum =0; | 
|---|
| 214 | Int_t timesum =0; | 
|---|
| 215 |  | 
|---|
| 216 | for (p=ptrmax; p < ptrmax + fHiGainWindowSize; p++) | 
|---|
| 217 | { | 
|---|
| 218 | timesignalsum += *p*(p-ptr); | 
|---|
| 219 | timesum += p-ptr; | 
|---|
| 220 | timesquaredsum  += (p-ptr)*(p-ptr); | 
|---|
| 221 | } | 
|---|
| 222 |  | 
|---|
| 223 | const Float_t pedes  = ped.GetPedestal(); | 
|---|
| 224 | const Float_t pedrms = ped.GetPedestalRms(); | 
|---|
| 225 | const Float_t pedsubsum = max - fHiGainWindowSize*pedes; | 
|---|
| 226 | const Float_t pedsubtimesignalsum = timesignalsum - timesum*pedes; | 
|---|
| 227 |  | 
|---|
| 228 | time      = pedsubsum != 0 ? pedsubtimesignalsum / pedsubsum  + Float_t(fHiGainFirst): 1; | 
|---|
| 229 | deltatime = pedsubsum != 0 ? pedrms / pedsubsum * sqrt(timesquaredsum - fHiGainWindowSize*time) : 1; | 
|---|
| 230 | } | 
|---|
| 231 |  | 
|---|
| 232 | void MExtractTimeHighestIntegral::FindTimeLoGain(Byte_t *ptr, Float_t &time, Float_t &deltatime, Byte_t &sat, const MPedestalPix &ped) const | 
|---|
| 233 | { | 
|---|
| 234 |  | 
|---|
| 235 | const Byte_t *end = ptr + fLoGainLast - fLoGainFirst +1 ; | 
|---|
| 236 |  | 
|---|
| 237 | Int_t sum=0;    // integral content of the actual window | 
|---|
| 238 | Int_t max = 0;  // highest integral content of all windows | 
|---|
| 239 |  | 
|---|
| 240 | // | 
|---|
| 241 | // Calculate the sum of the first fWindowSize slices | 
|---|
| 242 | // | 
|---|
| 243 | sat = 0; | 
|---|
| 244 | Byte_t *p = ptr; | 
|---|
| 245 |  | 
|---|
| 246 | while (p<ptr+fLoGainWindowSize) | 
|---|
| 247 | { | 
|---|
| 248 | sum += *p; | 
|---|
| 249 | if (*p++ >= fSaturationLimit) | 
|---|
| 250 | sat++; | 
|---|
| 251 | } | 
|---|
| 252 |  | 
|---|
| 253 | // | 
|---|
| 254 | // Check for saturation in all other slices | 
|---|
| 255 | // | 
|---|
| 256 | while (p<end) | 
|---|
| 257 | if (*p++ >= fSaturationLimit) | 
|---|
| 258 | sat++; | 
|---|
| 259 |  | 
|---|
| 260 | // | 
|---|
| 261 | // Calculate the i-th sum as | 
|---|
| 262 | //    sum_i+1 = sum_i + slice[i+8] - slice[i] | 
|---|
| 263 | // This is fast and accurate (because we are using int's) | 
|---|
| 264 | // | 
|---|
| 265 | max=sum; | 
|---|
| 266 | Byte_t *ptrmax=ptr;  // pointer to the first slice of the maximum window | 
|---|
| 267 |  | 
|---|
| 268 | for (p=ptr; p+fLoGainWindowSize<end; p++) | 
|---|
| 269 | { | 
|---|
| 270 | sum += *(p+fLoGainWindowSize) - *p; | 
|---|
| 271 |  | 
|---|
| 272 | if (sum>max) | 
|---|
| 273 | { | 
|---|
| 274 | max = sum; | 
|---|
| 275 | ptrmax = p+1; | 
|---|
| 276 | } | 
|---|
| 277 | } | 
|---|
| 278 |  | 
|---|
| 279 | // now calculate the time for the maximum window | 
|---|
| 280 | Int_t timesignalsum = 0; | 
|---|
| 281 | Int_t timesquaredsum =0; | 
|---|
| 282 | Int_t timesum =0; | 
|---|
| 283 |  | 
|---|
| 284 | for (p=ptrmax; p < ptrmax + fLoGainWindowSize; p++) | 
|---|
| 285 | { | 
|---|
| 286 | timesignalsum += *p*(p-ptr); | 
|---|
| 287 | timesum += p-ptr; | 
|---|
| 288 | timesquaredsum  += (p-ptr)*(p-ptr); | 
|---|
| 289 | } | 
|---|
| 290 |  | 
|---|
| 291 | const Float_t pedes  = ped.GetPedestal(); | 
|---|
| 292 | const Float_t pedrms = ped.GetPedestalRms(); | 
|---|
| 293 | const Float_t pedsubsum = max - fLoGainWindowSize*pedes; | 
|---|
| 294 | const Float_t pedsubtimesignalsum = timesignalsum - timesum*pedes; | 
|---|
| 295 |  | 
|---|
| 296 | time      = pedsubsum != 0 ? pedsubtimesignalsum / pedsubsum  + Float_t(fLoGainFirst) : 1; | 
|---|
| 297 | deltatime = pedsubsum != 0 ? pedrms / pedsubsum * sqrt(timesquaredsum - fLoGainWindowSize*time) : 1; | 
|---|
| 298 | } | 
|---|
| 299 |  | 
|---|
| 300 | // -------------------------------------------------------------------------- | 
|---|
| 301 | // | 
|---|
| 302 | // In addition to the resources of the base-class MExtractor: | 
|---|
| 303 | //   MJPedestal.MExtractor.WindowSizeHiGain: 6 | 
|---|
| 304 | //   MJPedestal.MExtractor.WindowSizeLoGain: 6 | 
|---|
| 305 | // | 
|---|
| 306 | Int_t MExtractTimeHighestIntegral::ReadEnv(const TEnv &env, TString prefix, Bool_t print) | 
|---|
| 307 | { | 
|---|
| 308 | Byte_t hw = fHiGainWindowSize; | 
|---|
| 309 | Byte_t lw = fLoGainWindowSize; | 
|---|
| 310 |  | 
|---|
| 311 | Bool_t rc = kFALSE; | 
|---|
| 312 |  | 
|---|
| 313 | if (IsEnvDefined(env, prefix, "HiGainWindowSize", print)) | 
|---|
| 314 | { | 
|---|
| 315 | hw = GetEnvValue(env, prefix, "HiGainWindowSize", hw); | 
|---|
| 316 | rc = kTRUE; | 
|---|
| 317 | } | 
|---|
| 318 | if (IsEnvDefined(env, prefix, "LoGainWindowSize", print)) | 
|---|
| 319 | { | 
|---|
| 320 | lw = GetEnvValue(env, prefix, "LoGainWindowSize", lw); | 
|---|
| 321 | rc = kTRUE; | 
|---|
| 322 | } | 
|---|
| 323 |  | 
|---|
| 324 | if (rc) | 
|---|
| 325 | SetWindowSize(hw, lw); | 
|---|
| 326 |  | 
|---|
| 327 | return MExtractTime::ReadEnv(env, prefix, print) ? kTRUE : rc; | 
|---|
| 328 | } | 
|---|
| 329 |  | 
|---|
| 330 | void MExtractTimeHighestIntegral::Print(Option_t *o) const | 
|---|
| 331 | { | 
|---|
| 332 | *fLog << all; | 
|---|
| 333 | *fLog << GetDescriptor() << ":" << endl; | 
|---|
| 334 | *fLog << " Windows: Hi-Gain=" << (int)fHiGainWindowSize << "  Lo-Gain=" << (int)fLoGainWindowSize << endl; | 
|---|
| 335 | MExtractTime::Print(o); | 
|---|
| 336 | } | 
|---|