| 1 | /* ======================================================================== *\ | 
|---|
| 2 | ! $Name: not supported by cvs2svn $:$Id: MPedCalcPedRun.cc,v 1.49 2006-10-24 07:58:13 tbretz Exp $ | 
|---|
| 3 | ! -------------------------------------------------------------------------- | 
|---|
| 4 | ! | 
|---|
| 5 | ! * | 
|---|
| 6 | ! * This file is part of MARS, the MAGIC Analysis and Reconstruction | 
|---|
| 7 | ! * Software. It is distributed to you in the hope that it can be a useful | 
|---|
| 8 | ! * and timesaving tool in analysing Data of imaging Cerenkov telescopes. | 
|---|
| 9 | ! * It is distributed WITHOUT ANY WARRANTY. | 
|---|
| 10 | ! * | 
|---|
| 11 | ! * Permission to use, copy, modify and distribute this software and its | 
|---|
| 12 | ! * documentation for any purpose is hereby granted without fee, | 
|---|
| 13 | ! * provided that the above copyright notice appear in all copies and | 
|---|
| 14 | ! * that both that copyright notice and this permission notice appear | 
|---|
| 15 | ! * in supporting documentation. It is provided "as is" without express | 
|---|
| 16 | ! * or implied warranty. | 
|---|
| 17 | ! * | 
|---|
| 18 | ! | 
|---|
| 19 | ! | 
|---|
| 20 | !   Author(s): Josep Flix 04/2001 <mailto:jflix@ifae.es> | 
|---|
| 21 | !   Author(s): Thomas Bretz 05/2001 <mailto:tbretz@astro.uni-wuerzburg.de> | 
|---|
| 22 | !   Author(s): Sebastian Commichau 12/2003 | 
|---|
| 23 | !   Author(s): Javier Rico 01/2004 <mailto:jrico@ifae.es> | 
|---|
| 24 | !   Author(s): Markus Gaug 01/2004 <mailto:markus@ifae.es> | 
|---|
| 25 | ! | 
|---|
| 26 | !   Copyright: MAGIC Software Development, 2000-2006 | 
|---|
| 27 | ! | 
|---|
| 28 | ! | 
|---|
| 29 | \* ======================================================================== */ | 
|---|
| 30 | ///////////////////////////////////////////////////////////////////////////// | 
|---|
| 31 | // | 
|---|
| 32 | //   MPedCalcPedRun | 
|---|
| 33 | // | 
|---|
| 34 | //  This task takes a pedestal run file and fills MPedestalCam during | 
|---|
| 35 | //  the Process() with the pedestal and rms computed in an event basis. | 
|---|
| 36 | //  In the PostProcess() MPedestalCam is finally filled with the pedestal | 
|---|
| 37 | //  mean and rms computed in a run basis. | 
|---|
| 38 | //  More than one run (file) can be merged | 
|---|
| 39 | // | 
|---|
| 40 | //  MPedCalcPedRun applies the following formula (1): | 
|---|
| 41 | // | 
|---|
| 42 | //  Pedestal per slice = sum(x_i) / n / slices | 
|---|
| 43 | //  PedRMS per slice   = Sqrt(  ( sum(x_i^2) - sum(x_i)^2/n ) / n-1 / slices ) | 
|---|
| 44 | // | 
|---|
| 45 | //  where x_i is the sum of "slices" FADC slices and sum means the sum over all | 
|---|
| 46 | //  events. "n" is the number of events, "slices" is the number of summed FADC samples. | 
|---|
| 47 | // | 
|---|
| 48 | //  Note that the slice-to-slice fluctuations are not Gaussian, but Poissonian, thus | 
|---|
| 49 | //  asymmetric and they are correlated. | 
|---|
| 50 | // | 
|---|
| 51 | //  It is important to know that the Pedestal per slice and PedRMS per slice depend | 
|---|
| 52 | //  on the number of used FADC slices, as seen in the following plots: | 
|---|
| 53 | // | 
|---|
| 54 | //Begin_Html | 
|---|
| 55 | /* | 
|---|
| 56 | <img src="images/PedestalStudyInner.gif"> | 
|---|
| 57 | */ | 
|---|
| 58 | //End_Html | 
|---|
| 59 | // | 
|---|
| 60 | //Begin_Html | 
|---|
| 61 | /* | 
|---|
| 62 | <img src="images/PedestalStudyOuter.gif"> | 
|---|
| 63 | */ | 
|---|
| 64 | // | 
|---|
| 65 | // The plots show the inner and outer pixels, respectivly and have the following meaning: | 
|---|
| 66 | // | 
|---|
| 67 | // 1) The calculated mean pedestal per slice (from MPedCalcPedRun) | 
|---|
| 68 | // 2) The fitted mean pedestal per slice     (from MHPedestalCam) | 
|---|
| 69 | // 3) The calculated pedestal RMS per slice  (from MPedCalcPedRun) | 
|---|
| 70 | // 4) The fitted sigma of the pedestal distribution per slice | 
|---|
| 71 | //                                           (from MHPedestalCam) | 
|---|
| 72 | // 5) The relative difference between calculation and histogram fit | 
|---|
| 73 | //    for the mean | 
|---|
| 74 | // 6) The relative difference between calculation and histogram fit | 
|---|
| 75 | //    for the sigma or RMS, respectively. | 
|---|
| 76 | // | 
|---|
| 77 | // The calculated means do not change significantly except for the case of 2 slices, | 
|---|
| 78 | // however the RMS changes from 5.7 per slice in the case of 2 extracted slices | 
|---|
| 79 | // to 8.3 per slice in the case of 26 extracted slices. This change is very significant. | 
|---|
| 80 | // | 
|---|
| 81 | // The plots have been produced on run 20123. You can reproduce them using | 
|---|
| 82 | // the macro pedestalstudies.C | 
|---|
| 83 | // | 
|---|
| 84 | //  Usage of this class: | 
|---|
| 85 | //  ==================== | 
|---|
| 86 | // | 
|---|
| 87 | //  Call: SetRange(higainfirst, higainlast, logainfirst, logainlast) | 
|---|
| 88 | //  to modify the ranges in which the window is allowed to move. | 
|---|
| 89 | //  Defaults are: | 
|---|
| 90 | // | 
|---|
| 91 | //   fHiGainFirst =  fgHiGainFirst =  0 | 
|---|
| 92 | //   fHiGainLast  =  fgHiGainLast  =  29 | 
|---|
| 93 | //   fLoGainFirst =  fgLoGainFirst =  0 | 
|---|
| 94 | //   fLoGainLast  =  fgLoGainLast  =  14 | 
|---|
| 95 | // | 
|---|
| 96 | //  Call: SetWindowSize(windowhigain, windowlogain) | 
|---|
| 97 | //  to modify the sliding window widths. Windows have to be an even number. | 
|---|
| 98 | //  In case of odd numbers, the window will be modified. | 
|---|
| 99 | // | 
|---|
| 100 | //  Defaults are: | 
|---|
| 101 | // | 
|---|
| 102 | //   fHiGainWindowSize = fgHiGainWindowSize = 14 | 
|---|
| 103 | //   fLoGainWindowSize = fgLoGainWindowSize = 0 | 
|---|
| 104 | // | 
|---|
| 105 | // | 
|---|
| 106 | // ToDo: | 
|---|
| 107 | //   - Take a setup file (ReadEnv-implementation) as input | 
|---|
| 108 | // | 
|---|
| 109 | // | 
|---|
| 110 | //  Input Containers: | 
|---|
| 111 | //   MRawEvtData | 
|---|
| 112 | //   MRawRunHeader | 
|---|
| 113 | //   MRawEvtHeader | 
|---|
| 114 | //   MGeomCam | 
|---|
| 115 | // | 
|---|
| 116 | //  Output Containers: | 
|---|
| 117 | //   MPedestalCam | 
|---|
| 118 | // | 
|---|
| 119 | //  See also: MPedestalCam, MPedestalPix, MHPedestalCam, MExtractor | 
|---|
| 120 | // | 
|---|
| 121 | ///////////////////////////////////////////////////////////////////////////// | 
|---|
| 122 | #include "MPedCalcPedRun.h" | 
|---|
| 123 |  | 
|---|
| 124 | #include "MParList.h" | 
|---|
| 125 |  | 
|---|
| 126 | #include "MLog.h" | 
|---|
| 127 | #include "MLogManip.h" | 
|---|
| 128 |  | 
|---|
| 129 | #include "MRawRunHeader.h" | 
|---|
| 130 | #include "MRawEvtHeader.h" | 
|---|
| 131 | #include "MRawEvtPixelIter.h" | 
|---|
| 132 | #include "MRawEvtData.h" | 
|---|
| 133 |  | 
|---|
| 134 | #include "MPedestalPix.h" | 
|---|
| 135 | #include "MPedestalCam.h" | 
|---|
| 136 |  | 
|---|
| 137 | #include "MGeomPix.h" | 
|---|
| 138 | #include "MGeomCam.h" | 
|---|
| 139 |  | 
|---|
| 140 | #include "MExtractPedestal.h" | 
|---|
| 141 | #include "MPedestalSubtractedEvt.h" | 
|---|
| 142 |  | 
|---|
| 143 | #include "MTriggerPattern.h" | 
|---|
| 144 |  | 
|---|
| 145 | ClassImp(MPedCalcPedRun); | 
|---|
| 146 |  | 
|---|
| 147 | using namespace std; | 
|---|
| 148 |  | 
|---|
| 149 | const UShort_t MPedCalcPedRun::fgExtractWinFirst       = 0; | 
|---|
| 150 | const UShort_t MPedCalcPedRun::fgExtractWinSize        = 6; | 
|---|
| 151 |  | 
|---|
| 152 | const UInt_t   MPedCalcPedRun::gkFirstRunWithFinalBits = 45605; | 
|---|
| 153 |  | 
|---|
| 154 | // -------------------------------------------------------------------------- | 
|---|
| 155 | // | 
|---|
| 156 | // Default constructor: | 
|---|
| 157 | // | 
|---|
| 158 | // Calls: | 
|---|
| 159 | // - SetExtractWindow(fgExtractWinFirst, fgExtractWinSize) | 
|---|
| 160 | // | 
|---|
| 161 | MPedCalcPedRun::MPedCalcPedRun(const char *name, const char *title) | 
|---|
| 162 | : fIsFirstPedRun(kFALSE), fUsedEvents(0), fTrigPattern(NULL) | 
|---|
| 163 | { | 
|---|
| 164 | fName  = name  ? name  : "MPedCalcPedRun"; | 
|---|
| 165 | fTitle = title ? title : "Task to calculate pedestals from pedestal runs raw data"; | 
|---|
| 166 |  | 
|---|
| 167 | SetExtractWindow(fgExtractWinFirst, fgExtractWinSize); | 
|---|
| 168 | } | 
|---|
| 169 |  | 
|---|
| 170 | // -------------------------------------------------------------------------- | 
|---|
| 171 | // | 
|---|
| 172 | // Call MExtractPedestal::reset and set fUsedEvents to 0. | 
|---|
| 173 | // | 
|---|
| 174 | void MPedCalcPedRun::Reset() | 
|---|
| 175 | { | 
|---|
| 176 | MExtractPedestal::ResetArrays(); | 
|---|
| 177 | fUsedEvents = 0; | 
|---|
| 178 | } | 
|---|
| 179 |  | 
|---|
| 180 | // -------------------------------------------------------------------------- | 
|---|
| 181 | // | 
|---|
| 182 | // Set fIsFirstPedRun=kTRUE | 
|---|
| 183 | // | 
|---|
| 184 | Int_t MPedCalcPedRun::PreProcess(MParList *pList) | 
|---|
| 185 | { | 
|---|
| 186 | fUsedEvents    = 0; | 
|---|
| 187 | fIsFirstPedRun = kTRUE; | 
|---|
| 188 | fIsNotPedRun   = kFALSE; | 
|---|
| 189 |  | 
|---|
| 190 | fTrigPattern = (MTriggerPattern*)pList->FindObject("MTriggerPattern"); | 
|---|
| 191 | if (!fTrigPattern) | 
|---|
| 192 | *fLog << inf << "MTriggerPattern not found... Cannot use interlaced pedestal events." << endl; | 
|---|
| 193 |  | 
|---|
| 194 | return MExtractPedestal::PreProcess(pList); | 
|---|
| 195 | } | 
|---|
| 196 |  | 
|---|
| 197 | // -------------------------------------------------------------------------- | 
|---|
| 198 | // | 
|---|
| 199 | // The run type is checked for "kRTPedestal" | 
|---|
| 200 | // and the variable fIsNotPedRun is set in that case | 
|---|
| 201 | // | 
|---|
| 202 | Bool_t MPedCalcPedRun::ReInit(MParList *pList) | 
|---|
| 203 | { | 
|---|
| 204 | if (!MExtractPedestal::ReInit(pList)) | 
|---|
| 205 | return kFALSE; | 
|---|
| 206 |  | 
|---|
| 207 | // | 
|---|
| 208 | // If this is the first ped run, the next run (call to ReInit) | 
|---|
| 209 | // is not the first anymore | 
|---|
| 210 | // | 
|---|
| 211 | switch (fRunHeader->GetRunType()) | 
|---|
| 212 | { | 
|---|
| 213 | case MRawRunHeader::kRTPedestal: | 
|---|
| 214 | case MRawRunHeader::kRTMonteCarlo: | 
|---|
| 215 | fIsFirstPedRun = kFALSE; | 
|---|
| 216 | fIsNotPedRun   = kFALSE; | 
|---|
| 217 | return kTRUE; | 
|---|
| 218 |  | 
|---|
| 219 | case MRawRunHeader::kRTCalibration: | 
|---|
| 220 | { | 
|---|
| 221 | TString proj(fRunHeader->GetProjectName()); | 
|---|
| 222 | proj.ToLower(); | 
|---|
| 223 |  | 
|---|
| 224 | // test if a continuous light run has been declared as calibration... | 
|---|
| 225 | if (proj.Contains("cl")) | 
|---|
| 226 | { | 
|---|
| 227 | fIsFirstPedRun = kFALSE; | 
|---|
| 228 | fIsNotPedRun   = kFALSE; | 
|---|
| 229 | return kTRUE; | 
|---|
| 230 | } | 
|---|
| 231 | } | 
|---|
| 232 | } | 
|---|
| 233 |  | 
|---|
| 234 | fIsNotPedRun = kTRUE; | 
|---|
| 235 |  | 
|---|
| 236 | // | 
|---|
| 237 | // If this is the first call to ReInit (before reading the first file) | 
|---|
| 238 | // nothing should be done. It occurs for the case that the first treated | 
|---|
| 239 | // file is not of pedestal type. | 
|---|
| 240 | // | 
|---|
| 241 | if (fIsFirstPedRun) | 
|---|
| 242 | return kTRUE; | 
|---|
| 243 |  | 
|---|
| 244 | // | 
|---|
| 245 | // In the other case, produce the MPedestalCam to be use the subsequent | 
|---|
| 246 | // (non-pedestal) events | 
|---|
| 247 | // | 
|---|
| 248 | *fLog << inf << "Finalizing pedestal calculations..." << flush; | 
|---|
| 249 |  | 
|---|
| 250 | if (!Finalize()) | 
|---|
| 251 | return kFALSE; | 
|---|
| 252 |  | 
|---|
| 253 | Reset(); | 
|---|
| 254 |  | 
|---|
| 255 | return kTRUE; | 
|---|
| 256 | } | 
|---|
| 257 |  | 
|---|
| 258 | // -------------------------------------------------------------------------- | 
|---|
| 259 | // | 
|---|
| 260 | // Return kTRUE (without doing anything) in case that the run type is not | 
|---|
| 261 | // equal to 1 (pedestal run) | 
|---|
| 262 | // | 
|---|
| 263 | // Fill the MPedestalCam container with the signal mean and rms for the event. | 
|---|
| 264 | // Store the measured signal in arrays fSumx and fSumx2 so that we can | 
|---|
| 265 | // calculate the overall mean and rms in the PostProcess() | 
|---|
| 266 | // | 
|---|
| 267 | Int_t MPedCalcPedRun::Calc() | 
|---|
| 268 | { | 
|---|
| 269 | if (fIsNotPedRun && !IsPedBitSet()) | 
|---|
| 270 | return kTRUE; | 
|---|
| 271 |  | 
|---|
| 272 | fUsedEvents++; | 
|---|
| 273 |  | 
|---|
| 274 | MRawEvtPixelIter pixel(fRawEvt); | 
|---|
| 275 | while (pixel.Next()) | 
|---|
| 276 | { | 
|---|
| 277 | const UInt_t idx = pixel.GetPixelId(); | 
|---|
| 278 |  | 
|---|
| 279 | if (!CheckVariation(idx)) | 
|---|
| 280 | continue; | 
|---|
| 281 |  | 
|---|
| 282 | //extract pedestal | 
|---|
| 283 | UInt_t ab[2]; | 
|---|
| 284 | const Float_t sum = fExtractor ? | 
|---|
| 285 | CalcExtractor(pixel, 0) : | 
|---|
| 286 | CalcSums(pixel, 0, ab[0], ab[1]); | 
|---|
| 287 |  | 
|---|
| 288 | if (fIntermediateStorage) | 
|---|
| 289 | (*fPedestalsInter)[idx].Set(sum, 0, 0, fUsedEvents); | 
|---|
| 290 |  | 
|---|
| 291 | const Float_t sqrsum = sum*sum; | 
|---|
| 292 |  | 
|---|
| 293 | fSumx[idx]   += sum; | 
|---|
| 294 | fSumx2[idx]  += sqrsum; | 
|---|
| 295 |  | 
|---|
| 296 | fSumAB0[idx] += ab[0]; | 
|---|
| 297 | fSumAB1[idx] += ab[1]; | 
|---|
| 298 |  | 
|---|
| 299 | if (fUseSpecialPixels) | 
|---|
| 300 | continue; | 
|---|
| 301 |  | 
|---|
| 302 | const UInt_t aidx   = (*fGeom)[idx].GetAidx(); | 
|---|
| 303 | const UInt_t sector = (*fGeom)[idx].GetSector(); | 
|---|
| 304 |  | 
|---|
| 305 | fAreaSumx[aidx]      += sum; | 
|---|
| 306 | fAreaSumx2[aidx]     += sqrsum; | 
|---|
| 307 | fSectorSumx[sector]  += sum; | 
|---|
| 308 | fSectorSumx2[sector] += sqrsum; | 
|---|
| 309 |  | 
|---|
| 310 | fAreaSumAB0[aidx]    += ab[0]; | 
|---|
| 311 | fAreaSumAB1[aidx]    += ab[1]; | 
|---|
| 312 | fSectorSumAB0[aidx]  += ab[0]; | 
|---|
| 313 | fSectorSumAB1[aidx]  += ab[1]; | 
|---|
| 314 | } | 
|---|
| 315 |  | 
|---|
| 316 | fPedestalsOut->SetReadyToSave(); | 
|---|
| 317 |  | 
|---|
| 318 | return kTRUE; | 
|---|
| 319 | } | 
|---|
| 320 |  | 
|---|
| 321 | // -------------------------------------------------------------------------- | 
|---|
| 322 | // | 
|---|
| 323 | // Compute signal mean and rms in the whole run and store it in MPedestalCam | 
|---|
| 324 | // | 
|---|
| 325 | Int_t MPedCalcPedRun::Finalize() | 
|---|
| 326 | { | 
|---|
| 327 | if (fUsedEvents == 0) | 
|---|
| 328 | return kTRUE; | 
|---|
| 329 |  | 
|---|
| 330 | // | 
|---|
| 331 | // Necessary check for extraction of special pixels | 
|---|
| 332 | // together with data which does not yet have them | 
|---|
| 333 | // | 
|---|
| 334 | if (fSumx.GetSize()==0) | 
|---|
| 335 | return kTRUE; | 
|---|
| 336 |  | 
|---|
| 337 | MRawEvtPixelIter pixel(fRawEvt); | 
|---|
| 338 | while (pixel.Next()) | 
|---|
| 339 | CalcPixResults(fUsedEvents, pixel.GetPixelId()); | 
|---|
| 340 |  | 
|---|
| 341 | if (!fUseSpecialPixels) | 
|---|
| 342 | { | 
|---|
| 343 |  | 
|---|
| 344 | // | 
|---|
| 345 | // Loop over the (two) area indices to get the averaged pedestal per aidx | 
|---|
| 346 | // | 
|---|
| 347 | for (UInt_t aidx=0; aidx<fAreaValid.GetSize(); aidx++) | 
|---|
| 348 | if (fAreaValid[aidx]>0) | 
|---|
| 349 | CalcAreaResults(fUsedEvents, fAreaValid[aidx], aidx); | 
|---|
| 350 |  | 
|---|
| 351 | // | 
|---|
| 352 | // Loop over the (six) sector indices to get the averaged pedestal per sector | 
|---|
| 353 | // | 
|---|
| 354 | for (UInt_t sector=0; sector<fSectorValid.GetSize(); sector++) | 
|---|
| 355 | if (fSectorValid[sector]>0) | 
|---|
| 356 | CalcSectorResults(fUsedEvents, fSectorValid[sector], sector); | 
|---|
| 357 | } | 
|---|
| 358 |  | 
|---|
| 359 | fPedestalsOut->SetTotalEntries(fUsedEvents*fExtractWinSize); | 
|---|
| 360 | fPedestalsOut->SetReadyToSave(); | 
|---|
| 361 |  | 
|---|
| 362 | return kTRUE; | 
|---|
| 363 | } | 
|---|
| 364 |  | 
|---|
| 365 | //----------------------------------------------------------------------- | 
|---|
| 366 | // | 
|---|
| 367 | // PostProcess MExtractPedestal and call Finalize | 
|---|
| 368 | // | 
|---|
| 369 | Int_t MPedCalcPedRun::PostProcess() | 
|---|
| 370 | { | 
|---|
| 371 | if (!fRawEvt) | 
|---|
| 372 | return kTRUE; | 
|---|
| 373 |  | 
|---|
| 374 | if (!Finalize()) | 
|---|
| 375 | return kFALSE; | 
|---|
| 376 |  | 
|---|
| 377 | return MExtractPedestal::PostProcess(); | 
|---|
| 378 | } | 
|---|
| 379 |  | 
|---|
| 380 | //----------------------------------------------------------------------- | 
|---|
| 381 | // | 
|---|
| 382 | // Return if the pedestal bit was set from the calibration trigger box. | 
|---|
| 383 | // The last but one bit is used for the "pedestal-bit". | 
|---|
| 384 | // | 
|---|
| 385 | // This bit is set since run gkFirstRunWithFinalBits | 
|---|
| 386 | // | 
|---|
| 387 | Bool_t MPedCalcPedRun::IsPedBitSet() | 
|---|
| 388 | { | 
|---|
| 389 | if (fRunHeader->GetRunNumber()<gkFirstRunWithFinalBits) | 
|---|
| 390 | return kFALSE; | 
|---|
| 391 |  | 
|---|
| 392 | if (!fTrigPattern) | 
|---|
| 393 | return kFALSE; | 
|---|
| 394 |  | 
|---|
| 395 | return (fTrigPattern->GetPrescaled() & MTriggerPattern::kPedestal) ? kTRUE : kFALSE; | 
|---|
| 396 | } | 
|---|
| 397 |  | 
|---|
| 398 | //----------------------------------------------------------------------- | 
|---|
| 399 | // | 
|---|
| 400 | void MPedCalcPedRun::Print(Option_t *o) const | 
|---|
| 401 | { | 
|---|
| 402 | MExtractPedestal::Print(o); | 
|---|
| 403 |  | 
|---|
| 404 | *fLog << "First pedrun out of sequence: " << (fIsFirstPedRun?"yes":"no") << endl; | 
|---|
| 405 | *fLog << "Number of used events so far: " << fUsedEvents << endl; | 
|---|
| 406 | } | 
|---|