| 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): Markus Gaug 02/2004 <mailto:markus@ifae.es> | 
|---|
| 19 | ! | 
|---|
| 20 | !   Copyright: MAGIC Software Development, 2000-2004 | 
|---|
| 21 | ! | 
|---|
| 22 | ! | 
|---|
| 23 | \* ======================================================================== */ | 
|---|
| 24 |  | 
|---|
| 25 | ////////////////////////////////////////////////////////////////////////////// | 
|---|
| 26 | // | 
|---|
| 27 | //  MHCalibrationPix | 
|---|
| 28 | // | 
|---|
| 29 | //  A base class for events which are believed to follow a Gaussian distribution | 
|---|
| 30 | //  with time, e.g. calibration events, observables containing white noise, ... | 
|---|
| 31 | // | 
|---|
| 32 | //  MHCalibrationPix derives from MHGausEvents, thus all features of | 
|---|
| 33 | //  MHGausEvents can be used by a class deriving from MHCalibrationPix | 
|---|
| 34 | // | 
|---|
| 35 | //  See also: MHGausEvents | 
|---|
| 36 | // | 
|---|
| 37 | ////////////////////////////////////////////////////////////////////////////// | 
|---|
| 38 | #include "MHCalibrationPix.h" | 
|---|
| 39 |  | 
|---|
| 40 | #include <TH1.h> | 
|---|
| 41 | #include <TF1.h> | 
|---|
| 42 | #include <TGraph.h> | 
|---|
| 43 |  | 
|---|
| 44 | #include "MLog.h" | 
|---|
| 45 | #include "MLogManip.h" | 
|---|
| 46 |  | 
|---|
| 47 | ClassImp(MHCalibrationPix); | 
|---|
| 48 |  | 
|---|
| 49 | using namespace std; | 
|---|
| 50 |  | 
|---|
| 51 | const Float_t  MHCalibrationPix::fgBlackoutLimit        = 5.; | 
|---|
| 52 | const Float_t  MHCalibrationPix::fgPickupLimit          = 5.; | 
|---|
| 53 | // -------------------------------------------------------------------------- | 
|---|
| 54 | // | 
|---|
| 55 | // Default Constructor. | 
|---|
| 56 | // Sets: | 
|---|
| 57 | // - the default number for fPickupLimit           (fgPickupLimit) | 
|---|
| 58 | // - the default number for fBlackoutLimit         (fgBlackoutLimit) | 
|---|
| 59 | // | 
|---|
| 60 | // Initializes: | 
|---|
| 61 | // - all variables to 0. | 
|---|
| 62 | // | 
|---|
| 63 | MHCalibrationPix::MHCalibrationPix(const char *name, const char *title) | 
|---|
| 64 | : fEventFrequency(0), fPixId(-1) | 
|---|
| 65 | { | 
|---|
| 66 |  | 
|---|
| 67 | fName  = name  ? name  : "MHCalibrationPix"; | 
|---|
| 68 | fTitle = title ? title : "Calibration histogram events"; | 
|---|
| 69 |  | 
|---|
| 70 | Clear(); | 
|---|
| 71 |  | 
|---|
| 72 | SetBlackoutLimit(); | 
|---|
| 73 | SetPickupLimit(); | 
|---|
| 74 | } | 
|---|
| 75 |  | 
|---|
| 76 |  | 
|---|
| 77 |  | 
|---|
| 78 | // -------------------------------------------------------------------------- | 
|---|
| 79 | // | 
|---|
| 80 | // Default Clear(), can be overloaded. | 
|---|
| 81 | // | 
|---|
| 82 | // Sets: | 
|---|
| 83 | // - all other pointers to NULL | 
|---|
| 84 | // - all variables to 0., except fPixId to -1 and keep fEventFrequency | 
|---|
| 85 | // - all flags to kFALSE | 
|---|
| 86 | // | 
|---|
| 87 | // Deletes (if not NULL): | 
|---|
| 88 | // - all pointers | 
|---|
| 89 | // | 
|---|
| 90 | void MHCalibrationPix::Clear(Option_t *o) | 
|---|
| 91 | { | 
|---|
| 92 |  | 
|---|
| 93 | MHGausEvents::Clear(); | 
|---|
| 94 | fSaturated         = 0; | 
|---|
| 95 | } | 
|---|
| 96 |  | 
|---|
| 97 |  | 
|---|
| 98 | #if 0 | 
|---|
| 99 | // -------------------------------------------------------------------------- | 
|---|
| 100 | // | 
|---|
| 101 | // ATTENTION: This nasty Clone function is necessary since the ROOT cloning | 
|---|
| 102 | //            lead to crashes on SOME machines (unfortunately not mine...). | 
|---|
| 103 | //            This function is a workaround in order to achieve the correct | 
|---|
| 104 | //            DrawClone() behaviour. | 
|---|
| 105 | // | 
|---|
| 106 | TObject *MHCalibrationPix::Clone(const char *name) const | 
|---|
| 107 | { | 
|---|
| 108 |  | 
|---|
| 109 | MHCalibrationPix &pix = *new MHCalibrationPix(name ? name : fName.Data(),fTitle.Data()); | 
|---|
| 110 |  | 
|---|
| 111 | // | 
|---|
| 112 | // Copy MHGausEvents data members | 
|---|
| 113 | // | 
|---|
| 114 | pix.fBinsAfterStripping   = fBinsAfterStripping; | 
|---|
| 115 | pix.fCurrentSize          = fCurrentSize; | 
|---|
| 116 | pix.fFlags                = fFlags; | 
|---|
| 117 | pix.fPowerProbabilityBins = fPowerProbabilityBins; | 
|---|
| 118 |  | 
|---|
| 119 | if (fHPowerProbability) | 
|---|
| 120 | pix.fHPowerProbability=(TH1I*)fHPowerProbability->Clone(); | 
|---|
| 121 |  | 
|---|
| 122 | if (fPowerSpectrum) | 
|---|
| 123 | pix.fPowerSpectrum = new TArrayF(*fPowerSpectrum); | 
|---|
| 124 |  | 
|---|
| 125 | pix.fEvents = fEvents; | 
|---|
| 126 |  | 
|---|
| 127 | if (fFGausFit) | 
|---|
| 128 | pix.fFGausFit=(TF1*)fFGausFit->Clone(); | 
|---|
| 129 | if (fFExpFit) | 
|---|
| 130 | pix.fFExpFit=(TF1*)fFExpFit->Clone(); | 
|---|
| 131 |  | 
|---|
| 132 | pix.fFirst = fFirst; | 
|---|
| 133 |  | 
|---|
| 134 | if (fGraphEvents) | 
|---|
| 135 | pix.fGraphEvents=(TGraph*)fGraphEvents->Clone(); | 
|---|
| 136 | if (fGraphPowerSpectrum) | 
|---|
| 137 | pix.fGraphPowerSpectrum=(TGraph*)fGraphPowerSpectrum->Clone(); | 
|---|
| 138 |  | 
|---|
| 139 | pix.fHGausHist = fHGausHist; | 
|---|
| 140 |  | 
|---|
| 141 | pix.fLast      = fLast; | 
|---|
| 142 | pix.fMean      = fMean; | 
|---|
| 143 | pix.fMeanErr   = fMeanErr; | 
|---|
| 144 | pix.fNbins     = fNbins; | 
|---|
| 145 | pix.fNDFLimit  = fNDFLimit; | 
|---|
| 146 | pix.fSigma     = fSigma; | 
|---|
| 147 | pix.fSigmaErr  = fSigmaErr; | 
|---|
| 148 | pix.fProb      = fProb; | 
|---|
| 149 | pix.fProbLimit = fProbLimit; | 
|---|
| 150 |  | 
|---|
| 151 | // | 
|---|
| 152 | // Copy data members | 
|---|
| 153 | // | 
|---|
| 154 | pix.fEventFrequency       = fEventFrequency; | 
|---|
| 155 | pix.fBlackoutLimit        = fBlackoutLimit; | 
|---|
| 156 | pix.fSaturated            = fSaturated; | 
|---|
| 157 | pix.fPickupLimit          = fPickupLimit; | 
|---|
| 158 | pix.fPixId                = fPixId; | 
|---|
| 159 |  | 
|---|
| 160 | return &pix; | 
|---|
| 161 | } | 
|---|
| 162 | #endif | 
|---|
| 163 |  | 
|---|
| 164 | // ----------------------------------------------------------------------------- | 
|---|
| 165 | // | 
|---|
| 166 | // Bypasses the Gauss fit by taking mean and RMS from the histogram | 
|---|
| 167 | // | 
|---|
| 168 | // Errors are determined in the following way: | 
|---|
| 169 | // MeanErr  = RMS / Sqrt(entries) | 
|---|
| 170 | // SigmaErr = RMS / (2.*Sqrt(entries) ) | 
|---|
| 171 | // | 
|---|
| 172 | void MHCalibrationPix::BypassFit() | 
|---|
| 173 | { | 
|---|
| 174 |  | 
|---|
| 175 | const Stat_t entries = fHGausHist.GetEntries(); | 
|---|
| 176 |  | 
|---|
| 177 | if (entries <= 0.) | 
|---|
| 178 | { | 
|---|
| 179 | *fLog << warn << GetDescriptor() | 
|---|
| 180 | << ": Cannot bypass fit. Number of entries smaller or equal 0 in pixel: " << fPixId << endl; | 
|---|
| 181 | return; | 
|---|
| 182 | } | 
|---|
| 183 |  | 
|---|
| 184 | fMean     = fHGausHist.GetMean(); | 
|---|
| 185 | fMeanErr  = fHGausHist.GetRMS() / TMath::Sqrt(entries); | 
|---|
| 186 | fSigma    = fHGausHist.GetRMS() ; | 
|---|
| 187 | fSigmaErr = fHGausHist.GetRMS() / TMath::Sqrt(entries) / 2.; | 
|---|
| 188 | } | 
|---|
| 189 |  | 
|---|
| 190 | // -------------------------------------------------------------------------- | 
|---|
| 191 | // | 
|---|
| 192 | // - Set fPixId to id | 
|---|
| 193 | // | 
|---|
| 194 | // Add id to names and titles of: | 
|---|
| 195 | // - fHGausHist | 
|---|
| 196 | // | 
|---|
| 197 | void MHCalibrationPix::ChangeHistId(const Int_t id) | 
|---|
| 198 | { | 
|---|
| 199 |  | 
|---|
| 200 | fPixId = id; | 
|---|
| 201 |  | 
|---|
| 202 | fHGausHist.SetName(  Form("%s%d", fHGausHist.GetName(),  id)); | 
|---|
| 203 | fHGausHist.SetTitle( Form("%s%d", fHGausHist.GetTitle(), id)); | 
|---|
| 204 |  | 
|---|
| 205 | fName  = Form("%s%d", fName.Data(),  id); | 
|---|
| 206 | fTitle = Form("%s%d", fTitle.Data(), id); | 
|---|
| 207 |  | 
|---|
| 208 | } | 
|---|
| 209 |  | 
|---|
| 210 |  | 
|---|
| 211 | // ----------------------------------------------------------------------------- | 
|---|
| 212 | // | 
|---|
| 213 | // Create the x-axis for the event graph | 
|---|
| 214 | // | 
|---|
| 215 | Float_t *MHCalibrationPix::CreateEventXaxis(Int_t n) | 
|---|
| 216 | { | 
|---|
| 217 |  | 
|---|
| 218 | Float_t *xaxis = new Float_t[n]; | 
|---|
| 219 |  | 
|---|
| 220 | if (fEventFrequency) | 
|---|
| 221 | for (Int_t i=0;i<n;i++) | 
|---|
| 222 | xaxis[i] = (Float_t)i/fEventFrequency; | 
|---|
| 223 | else | 
|---|
| 224 | for (Int_t i=0;i<n;i++) | 
|---|
| 225 | xaxis[i] = (Float_t)i; | 
|---|
| 226 |  | 
|---|
| 227 | return xaxis; | 
|---|
| 228 |  | 
|---|
| 229 | } | 
|---|
| 230 |  | 
|---|
| 231 | // ----------------------------------------------------------------------------- | 
|---|
| 232 | // | 
|---|
| 233 | // Create the x-axis for the event graph | 
|---|
| 234 | // | 
|---|
| 235 | Float_t *MHCalibrationPix::CreatePSDXaxis(Int_t n) | 
|---|
| 236 | { | 
|---|
| 237 |  | 
|---|
| 238 | Float_t *xaxis = new Float_t[n]; | 
|---|
| 239 |  | 
|---|
| 240 | if (fEventFrequency) | 
|---|
| 241 | for (Int_t i=0;i<n;i++) | 
|---|
| 242 | xaxis[i] = 0.5*(Float_t)i*fEventFrequency/n; | 
|---|
| 243 | else | 
|---|
| 244 | for (Int_t i=0;i<n;i++) | 
|---|
| 245 | xaxis[i] = 0.5*(Float_t)i/n; | 
|---|
| 246 |  | 
|---|
| 247 | return xaxis; | 
|---|
| 248 |  | 
|---|
| 249 | } | 
|---|
| 250 |  | 
|---|
| 251 | // ---------------------------------------------------------------------------------- | 
|---|
| 252 | // | 
|---|
| 253 | // Create a graph to display the array fEvents | 
|---|
| 254 | // If the variable fEventFrequency is set, the x-axis is transformed into real time. | 
|---|
| 255 | // | 
|---|
| 256 | void MHCalibrationPix::CreateGraphEvents() | 
|---|
| 257 | { | 
|---|
| 258 |  | 
|---|
| 259 | MHGausEvents::CreateGraphEvents(); | 
|---|
| 260 | fGraphEvents->GetXaxis()->SetTitle((fEventFrequency) ? "Time [s]" : "Event Nr."); | 
|---|
| 261 | } | 
|---|
| 262 |  | 
|---|
| 263 | // ---------------------------------------------------------------------------------- | 
|---|
| 264 | // | 
|---|
| 265 | // Create a graph to display the array fPowerSpectrum | 
|---|
| 266 | // If the variable fEventFrequency is set, the x-axis is transformed into real frequency. | 
|---|
| 267 | // | 
|---|
| 268 | void MHCalibrationPix::CreateGraphPowerSpectrum() | 
|---|
| 269 | { | 
|---|
| 270 |  | 
|---|
| 271 | MHGausEvents::CreateGraphPowerSpectrum(); | 
|---|
| 272 |  | 
|---|
| 273 | fGraphPowerSpectrum->GetXaxis()->SetTitle((fEventFrequency) ? "Frequency [Hz]" : "Frequency"); | 
|---|
| 274 | } | 
|---|
| 275 |  | 
|---|
| 276 | // ------------------------------------------------------------------------------- | 
|---|
| 277 | // | 
|---|
| 278 | // Return the number of "blackout" events, which are events with values higher | 
|---|
| 279 | // than fBlackoutLimit sigmas from the mean | 
|---|
| 280 | // | 
|---|
| 281 | // | 
|---|
| 282 | const Double_t MHCalibrationPix::GetBlackout() const | 
|---|
| 283 | { | 
|---|
| 284 |  | 
|---|
| 285 | if ((fMean == 0.) && (fSigma == 0.)) | 
|---|
| 286 | return -1.; | 
|---|
| 287 |  | 
|---|
| 288 | const Int_t first = fHGausHist.GetXaxis()->GetFirst(); | 
|---|
| 289 | const Int_t last  = fHGausHist.GetXaxis()->FindBin(fMean-fBlackoutLimit*fSigma); | 
|---|
| 290 |  | 
|---|
| 291 | if (first >= last) | 
|---|
| 292 | return 0.; | 
|---|
| 293 |  | 
|---|
| 294 | return fHGausHist.Integral(first, last, "width"); | 
|---|
| 295 | } | 
|---|
| 296 |  | 
|---|
| 297 |  | 
|---|
| 298 | // ------------------------------------------------------------------------------- | 
|---|
| 299 | // | 
|---|
| 300 | // Return the number of "pickup" events, which are events with values higher | 
|---|
| 301 | // than fPickupLimit sigmas from the mean | 
|---|
| 302 | // | 
|---|
| 303 | // | 
|---|
| 304 | const Double_t MHCalibrationPix::GetPickup() const | 
|---|
| 305 | { | 
|---|
| 306 |  | 
|---|
| 307 | if ((fMean == 0.) && (fSigma == 0.)) | 
|---|
| 308 | return -1.; | 
|---|
| 309 |  | 
|---|
| 310 | const Int_t first = fHGausHist.GetXaxis()->FindBin(fMean+fPickupLimit*fSigma); | 
|---|
| 311 | const Int_t last  = fHGausHist.GetXaxis()->GetLast(); | 
|---|
| 312 |  | 
|---|
| 313 | if (first >= last) | 
|---|
| 314 | return 0.; | 
|---|
| 315 |  | 
|---|
| 316 | return fHGausHist.Integral(first, last, "width"); | 
|---|
| 317 | } | 
|---|
| 318 |  | 
|---|
| 319 |  | 
|---|
| 320 | // -------------------------------------------------------------------------- | 
|---|
| 321 | // | 
|---|
| 322 | // Re-normalize the results, has to be overloaded | 
|---|
| 323 | // | 
|---|
| 324 | void  MHCalibrationPix::Renorm() | 
|---|
| 325 | { | 
|---|
| 326 | } | 
|---|
| 327 |  | 
|---|
| 328 | // ----------------------------------------------------------------------------- | 
|---|
| 329 | // | 
|---|
| 330 | // If flag IsGausFitOK() is set (histogram already successfully fitted), | 
|---|
| 331 | // returns kTRUE | 
|---|
| 332 | // | 
|---|
| 333 | // If both fMean and fSigma are still zero, call FitGaus() | 
|---|
| 334 | // | 
|---|
| 335 | // Repeats the Gauss fit in a smaller range, defined by: | 
|---|
| 336 | // | 
|---|
| 337 | // min = GetMean() - fBlackoutLimit * GetSigma(); | 
|---|
| 338 | // max = GetMean() + fPickupLimit   * GetSigma(); | 
|---|
| 339 | // | 
|---|
| 340 | // The fit results are retrieved and stored in class-own variables. | 
|---|
| 341 | // | 
|---|
| 342 | // A flag IsGausFitOK() is set according to whether the fit probability | 
|---|
| 343 | // is smaller or bigger than fProbLimit, whether the NDF is bigger than | 
|---|
| 344 | // fNDFLimit and whether results are NaNs. | 
|---|
| 345 | // | 
|---|
| 346 | Bool_t MHCalibrationPix::RepeatFit(const Option_t *option) | 
|---|
| 347 | { | 
|---|
| 348 |  | 
|---|
| 349 | if (IsGausFitOK()) | 
|---|
| 350 | return kTRUE; | 
|---|
| 351 |  | 
|---|
| 352 | if ((fMean == 0.) && (fSigma == 0.)) | 
|---|
| 353 | return FitGaus(); | 
|---|
| 354 |  | 
|---|
| 355 | // | 
|---|
| 356 | // Get new fitting ranges | 
|---|
| 357 | // | 
|---|
| 358 | Axis_t rmin = fMean - fBlackoutLimit * fSigma; | 
|---|
| 359 | Axis_t rmax = fMean + fPickupLimit   * fSigma; | 
|---|
| 360 |  | 
|---|
| 361 | Axis_t hmin = fHGausHist.GetBinCenter(fHGausHist.GetXaxis()->GetFirst()); | 
|---|
| 362 | Axis_t hmax = fHGausHist.GetBinCenter(fHGausHist.GetXaxis()->GetLast()) ; | 
|---|
| 363 |  | 
|---|
| 364 | fFGausFit->SetRange(hmin < rmin ? rmin : hmin , hmax > rmax ? rmax : hmax); | 
|---|
| 365 |  | 
|---|
| 366 | fHGausHist.Fit(fFGausFit,option); | 
|---|
| 367 |  | 
|---|
| 368 | fMean     = fFGausFit->GetParameter(1); | 
|---|
| 369 | fSigma    = fFGausFit->GetParameter(2); | 
|---|
| 370 | fMeanErr  = fFGausFit->GetParError(1) ; | 
|---|
| 371 | fSigmaErr = fFGausFit->GetParError(2) ; | 
|---|
| 372 | fProb     = fFGausFit->GetProb()      ; | 
|---|
| 373 |  | 
|---|
| 374 | // | 
|---|
| 375 | // The fit result is accepted under condition: | 
|---|
| 376 | // 1) The results are not nan's | 
|---|
| 377 | // 2) The NDF is not smaller than fNDFLimit (default: fgNDFLimit) | 
|---|
| 378 | // 3) The Probability is greater than fProbLimit (default: fgProbLimit) | 
|---|
| 379 | // | 
|---|
| 380 | if (   TMath::IsNaN ( fMean     ) | 
|---|
| 381 | || TMath::IsNaN ( fMeanErr  ) | 
|---|
| 382 | || TMath::IsNaN ( fProb     ) | 
|---|
| 383 | || TMath::IsNaN ( fSigma    ) | 
|---|
| 384 | || TMath::IsNaN ( fSigmaErr ) | 
|---|
| 385 | || fFGausFit->GetNDF() < fNDFLimit | 
|---|
| 386 | || fProb < fProbLimit ) | 
|---|
| 387 | return kFALSE; | 
|---|
| 388 |  | 
|---|
| 389 | SetGausFitOK(kTRUE); | 
|---|
| 390 | return kTRUE; | 
|---|
| 391 |  | 
|---|
| 392 | } | 
|---|
| 393 |  | 
|---|