| 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 | // -----------------------------------------------------------------------------
|
|---|
| 99 | //
|
|---|
| 100 | // Bypasses the Gauss fit by taking mean and RMS from the histogram
|
|---|
| 101 | //
|
|---|
| 102 | // Errors are determined in the following way:
|
|---|
| 103 | // MeanErr = RMS / Sqrt(entries)
|
|---|
| 104 | // SigmaErr = RMS / (2.*Sqrt(entries) )
|
|---|
| 105 | //
|
|---|
| 106 | void MHCalibrationPix::BypassFit()
|
|---|
| 107 | {
|
|---|
| 108 |
|
|---|
| 109 | const Stat_t entries = fHGausHist.GetEntries();
|
|---|
| 110 |
|
|---|
| 111 | if (entries <= 0.)
|
|---|
| 112 | {
|
|---|
| 113 | *fLog << warn << GetDescriptor()
|
|---|
| 114 | << ": Cannot bypass fit. Number of entries smaller or equal 0 in pixel: " << fPixId << endl;
|
|---|
| 115 | return;
|
|---|
| 116 | }
|
|---|
| 117 |
|
|---|
| 118 | fMean = fHGausHist.GetMean();
|
|---|
| 119 | fMeanErr = fHGausHist.GetRMS() / TMath::Sqrt(entries);
|
|---|
| 120 | fSigma = fHGausHist.GetRMS() ;
|
|---|
| 121 | fSigmaErr = fHGausHist.GetRMS() / TMath::Sqrt(entries) / 2.;
|
|---|
| 122 | }
|
|---|
| 123 |
|
|---|
| 124 | // --------------------------------------------------------------------------
|
|---|
| 125 | //
|
|---|
| 126 | // - Set fPixId to id
|
|---|
| 127 | //
|
|---|
| 128 | // Add id to names and titles of:
|
|---|
| 129 | // - fHGausHist
|
|---|
| 130 | //
|
|---|
| 131 | void MHCalibrationPix::ChangeHistId(const Int_t id)
|
|---|
| 132 | {
|
|---|
| 133 |
|
|---|
| 134 | fPixId = id;
|
|---|
| 135 |
|
|---|
| 136 | fHGausHist.SetName( Form("%s%d", fHGausHist.GetName(), id));
|
|---|
| 137 | fHGausHist.SetTitle( Form("%s%d", fHGausHist.GetTitle(), id));
|
|---|
| 138 |
|
|---|
| 139 | fName = Form("%s%d", fName.Data(), id);
|
|---|
| 140 | fTitle = Form("%s%d", fTitle.Data(), id);
|
|---|
| 141 |
|
|---|
| 142 | }
|
|---|
| 143 |
|
|---|
| 144 |
|
|---|
| 145 | // -----------------------------------------------------------------------------
|
|---|
| 146 | //
|
|---|
| 147 | // Create the x-axis for the event graph
|
|---|
| 148 | //
|
|---|
| 149 | Float_t *MHCalibrationPix::CreateEventXaxis(Int_t n)
|
|---|
| 150 | {
|
|---|
| 151 |
|
|---|
| 152 | Float_t *xaxis = new Float_t[n];
|
|---|
| 153 |
|
|---|
| 154 | if (fEventFrequency)
|
|---|
| 155 | for (Int_t i=0;i<n;i++)
|
|---|
| 156 | xaxis[i] = (Float_t)i/fEventFrequency;
|
|---|
| 157 | else
|
|---|
| 158 | for (Int_t i=0;i<n;i++)
|
|---|
| 159 | xaxis[i] = (Float_t)i;
|
|---|
| 160 |
|
|---|
| 161 | return xaxis;
|
|---|
| 162 |
|
|---|
| 163 | }
|
|---|
| 164 |
|
|---|
| 165 | // -----------------------------------------------------------------------------
|
|---|
| 166 | //
|
|---|
| 167 | // Create the x-axis for the event graph
|
|---|
| 168 | //
|
|---|
| 169 | Float_t *MHCalibrationPix::CreatePSDXaxis(Int_t n)
|
|---|
| 170 | {
|
|---|
| 171 |
|
|---|
| 172 | Float_t *xaxis = new Float_t[n];
|
|---|
| 173 |
|
|---|
| 174 | if (fEventFrequency)
|
|---|
| 175 | for (Int_t i=0;i<n;i++)
|
|---|
| 176 | xaxis[i] = 0.5*(Float_t)i*fEventFrequency/n;
|
|---|
| 177 | else
|
|---|
| 178 | for (Int_t i=0;i<n;i++)
|
|---|
| 179 | xaxis[i] = 0.5*(Float_t)i/n;
|
|---|
| 180 |
|
|---|
| 181 | return xaxis;
|
|---|
| 182 |
|
|---|
| 183 | }
|
|---|
| 184 |
|
|---|
| 185 | // ----------------------------------------------------------------------------------
|
|---|
| 186 | //
|
|---|
| 187 | // Create a graph to display the array fEvents
|
|---|
| 188 | // If the variable fEventFrequency is set, the x-axis is transformed into real time.
|
|---|
| 189 | //
|
|---|
| 190 | void MHCalibrationPix::CreateGraphEvents()
|
|---|
| 191 | {
|
|---|
| 192 |
|
|---|
| 193 | MHGausEvents::CreateGraphEvents();
|
|---|
| 194 | fGraphEvents->GetXaxis()->SetTitle((fEventFrequency) ? "Time [s]" : "Event Nr.");
|
|---|
| 195 | }
|
|---|
| 196 |
|
|---|
| 197 | // ----------------------------------------------------------------------------------
|
|---|
| 198 | //
|
|---|
| 199 | // Create a graph to display the array fPowerSpectrum
|
|---|
| 200 | // If the variable fEventFrequency is set, the x-axis is transformed into real frequency.
|
|---|
| 201 | //
|
|---|
| 202 | void MHCalibrationPix::CreateGraphPowerSpectrum()
|
|---|
| 203 | {
|
|---|
| 204 |
|
|---|
| 205 | MHGausEvents::CreateGraphPowerSpectrum();
|
|---|
| 206 |
|
|---|
| 207 | fGraphPowerSpectrum->GetXaxis()->SetTitle((fEventFrequency) ? "Frequency [Hz]" : "Frequency");
|
|---|
| 208 | }
|
|---|
| 209 |
|
|---|
| 210 | // -------------------------------------------------------------------------------
|
|---|
| 211 | //
|
|---|
| 212 | // Return the number of "blackout" events, which are events with values higher
|
|---|
| 213 | // than fBlackoutLimit sigmas from the mean
|
|---|
| 214 | //
|
|---|
| 215 | //
|
|---|
| 216 | const Double_t MHCalibrationPix::GetBlackout() const
|
|---|
| 217 | {
|
|---|
| 218 |
|
|---|
| 219 | if ((fMean == 0.) && (fSigma == 0.))
|
|---|
| 220 | return -1.;
|
|---|
| 221 |
|
|---|
| 222 | const Int_t first = fHGausHist.GetXaxis()->GetFirst();
|
|---|
| 223 | const Int_t last = fHGausHist.GetXaxis()->FindBin(fMean-fBlackoutLimit*fSigma);
|
|---|
| 224 |
|
|---|
| 225 | if (first >= last)
|
|---|
| 226 | return 0.;
|
|---|
| 227 |
|
|---|
| 228 | return fHGausHist.Integral(first, last, "width");
|
|---|
| 229 | }
|
|---|
| 230 |
|
|---|
| 231 |
|
|---|
| 232 | // -------------------------------------------------------------------------------
|
|---|
| 233 | //
|
|---|
| 234 | // Return the number of "pickup" events, which are events with values higher
|
|---|
| 235 | // than fPickupLimit sigmas from the mean
|
|---|
| 236 | //
|
|---|
| 237 | //
|
|---|
| 238 | const Double_t MHCalibrationPix::GetPickup() const
|
|---|
| 239 | {
|
|---|
| 240 |
|
|---|
| 241 | if ((fMean == 0.) && (fSigma == 0.))
|
|---|
| 242 | return -1.;
|
|---|
| 243 |
|
|---|
| 244 | const Int_t first = fHGausHist.GetXaxis()->FindBin(fMean+fPickupLimit*fSigma);
|
|---|
| 245 | const Int_t last = fHGausHist.GetXaxis()->GetLast();
|
|---|
| 246 |
|
|---|
| 247 | if (first >= last)
|
|---|
| 248 | return 0.;
|
|---|
| 249 |
|
|---|
| 250 | return fHGausHist.Integral(first, last, "width");
|
|---|
| 251 | }
|
|---|
| 252 |
|
|---|
| 253 |
|
|---|
| 254 | // --------------------------------------------------------------------------
|
|---|
| 255 | //
|
|---|
| 256 | // Re-normalize the results, has to be overloaded
|
|---|
| 257 | //
|
|---|
| 258 | void MHCalibrationPix::Renorm()
|
|---|
| 259 | {
|
|---|
| 260 | }
|
|---|
| 261 |
|
|---|
| 262 | // -----------------------------------------------------------------------------
|
|---|
| 263 | //
|
|---|
| 264 | // If flag IsGausFitOK() is set (histogram already successfully fitted),
|
|---|
| 265 | // returns kTRUE
|
|---|
| 266 | //
|
|---|
| 267 | // If both fMean and fSigma are still zero, call FitGaus()
|
|---|
| 268 | //
|
|---|
| 269 | // Repeats the Gauss fit in a smaller range, defined by:
|
|---|
| 270 | //
|
|---|
| 271 | // min = GetMean() - fBlackoutLimit * GetSigma();
|
|---|
| 272 | // max = GetMean() + fPickupLimit * GetSigma();
|
|---|
| 273 | //
|
|---|
| 274 | // The fit results are retrieved and stored in class-own variables.
|
|---|
| 275 | //
|
|---|
| 276 | // A flag IsGausFitOK() is set according to whether the fit probability
|
|---|
| 277 | // is smaller or bigger than fProbLimit, whether the NDF is bigger than
|
|---|
| 278 | // fNDFLimit and whether results are NaNs.
|
|---|
| 279 | //
|
|---|
| 280 | Bool_t MHCalibrationPix::RepeatFit(const Option_t *option)
|
|---|
| 281 | {
|
|---|
| 282 |
|
|---|
| 283 | if (IsGausFitOK())
|
|---|
| 284 | return kTRUE;
|
|---|
| 285 |
|
|---|
| 286 | if ((fMean == 0.) && (fSigma == 0.))
|
|---|
| 287 | return FitGaus();
|
|---|
| 288 |
|
|---|
| 289 | //
|
|---|
| 290 | // Get new fitting ranges
|
|---|
| 291 | //
|
|---|
| 292 | Axis_t rmin = fMean - fBlackoutLimit * fSigma;
|
|---|
| 293 | Axis_t rmax = fMean + fPickupLimit * fSigma;
|
|---|
| 294 |
|
|---|
| 295 | Axis_t hmin = fHGausHist.GetBinCenter(fHGausHist.GetXaxis()->GetFirst());
|
|---|
| 296 | Axis_t hmax = fHGausHist.GetBinCenter(fHGausHist.GetXaxis()->GetLast()) ;
|
|---|
| 297 |
|
|---|
| 298 | fFGausFit->SetRange(hmin < rmin ? rmin : hmin , hmax > rmax ? rmax : hmax);
|
|---|
| 299 |
|
|---|
| 300 | fHGausHist.Fit(fFGausFit,option);
|
|---|
| 301 |
|
|---|
| 302 | fMean = fFGausFit->GetParameter(1);
|
|---|
| 303 | fSigma = fFGausFit->GetParameter(2);
|
|---|
| 304 | fMeanErr = fFGausFit->GetParError(1) ;
|
|---|
| 305 | fSigmaErr = fFGausFit->GetParError(2) ;
|
|---|
| 306 | fProb = fFGausFit->GetProb() ;
|
|---|
| 307 |
|
|---|
| 308 | //
|
|---|
| 309 | // The fit result is accepted under condition:
|
|---|
| 310 | // 1) The results are not nan's
|
|---|
| 311 | // 2) The NDF is not smaller than fNDFLimit (default: fgNDFLimit)
|
|---|
| 312 | // 3) The Probability is greater than fProbLimit (default: fgProbLimit)
|
|---|
| 313 | //
|
|---|
| 314 | if ( TMath::IsNaN ( fMean )
|
|---|
| 315 | || TMath::IsNaN ( fMeanErr )
|
|---|
| 316 | || TMath::IsNaN ( fProb )
|
|---|
| 317 | || TMath::IsNaN ( fSigma )
|
|---|
| 318 | || TMath::IsNaN ( fSigmaErr )
|
|---|
| 319 | || fFGausFit->GetNDF() < fNDFLimit
|
|---|
| 320 | || fProb < fProbLimit )
|
|---|
| 321 | return kFALSE;
|
|---|
| 322 |
|
|---|
| 323 | SetGausFitOK(kTRUE);
|
|---|
| 324 | return kTRUE;
|
|---|
| 325 |
|
|---|
| 326 | }
|
|---|
| 327 |
|
|---|