| 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): Javier Lopez 05/2001 (jlopez@ifae.es)
|
|---|
| 19 | !
|
|---|
| 20 | ! Copyright: MAGIC Software Development, 2000-2001
|
|---|
| 21 | !
|
|---|
| 22 | !
|
|---|
| 23 | \* ======================================================================== */
|
|---|
| 24 |
|
|---|
| 25 | /////////////////////////////////////////////////////////////////////////////
|
|---|
| 26 | //
|
|---|
| 27 | // MHMcEnergy
|
|---|
| 28 | //
|
|---|
| 29 | // This class holds the information (histogram and fit function)
|
|---|
| 30 | // about the energy threshold for a particular trigger condition.
|
|---|
| 31 | //
|
|---|
| 32 | ////////////////////////////////////////////////////////////////////////////
|
|---|
| 33 | #include "MHMcEnergy.h"
|
|---|
| 34 |
|
|---|
| 35 | #include <stdlib.h>
|
|---|
| 36 | #include <iostream.h>
|
|---|
| 37 |
|
|---|
| 38 | #include <TH1.h>
|
|---|
| 39 | #include <TF1.h>
|
|---|
| 40 | #include <TCanvas.h>
|
|---|
| 41 | #include <TPaveLabel.h>
|
|---|
| 42 |
|
|---|
| 43 | #include "MH.h"
|
|---|
| 44 |
|
|---|
| 45 | ClassImp(MHMcEnergy);
|
|---|
| 46 |
|
|---|
| 47 | // -------------------------------------------------------------------------
|
|---|
| 48 | //
|
|---|
| 49 | // Default Constructor.
|
|---|
| 50 | //
|
|---|
| 51 | MHMcEnergy::MHMcEnergy(const char *name, const char *title)
|
|---|
| 52 | {
|
|---|
| 53 | fTitle = title ? title : "Container for an energy distribution histogram";
|
|---|
| 54 |
|
|---|
| 55 | // - we initialize the histogram
|
|---|
| 56 | // - we have to give diferent names for the diferent histograms because
|
|---|
| 57 | // root don't allow us to have diferent histograms with the same name
|
|---|
| 58 |
|
|---|
| 59 | fHist = new TH1F("", "", 20, 0.5, 4.5);
|
|---|
| 60 |
|
|---|
| 61 | fHist->SetDirectory(NULL);
|
|---|
| 62 | fHist->SetXTitle("log(E/GeV)");
|
|---|
| 63 | fHist->SetYTitle("dN/dE");
|
|---|
| 64 |
|
|---|
| 65 | SetName(name ? name : "MHMcEnergy");
|
|---|
| 66 | }
|
|---|
| 67 |
|
|---|
| 68 | // -------------------------------------------------------------------------
|
|---|
| 69 | //
|
|---|
| 70 | // This doesn't only set the name. It tries to get the number from the
|
|---|
| 71 | // name and creates also name and title of the histogram.
|
|---|
| 72 | //
|
|---|
| 73 | // This is necessary for example if a list of such MHMcEnergy histograms
|
|---|
| 74 | // is created (s. MParList::CreateObjList)
|
|---|
| 75 | //
|
|---|
| 76 | void MHMcEnergy::SetName(const char *name)
|
|---|
| 77 | {
|
|---|
| 78 | TString cname(name);
|
|---|
| 79 | const char *semicolon = strrchr(cname, ';');
|
|---|
| 80 |
|
|---|
| 81 | UInt_t idx = semicolon ? atoi(semicolon+1) : 0;
|
|---|
| 82 |
|
|---|
| 83 | fName = cname;
|
|---|
| 84 |
|
|---|
| 85 | char text[256];
|
|---|
| 86 | if (idx>0)
|
|---|
| 87 | sprintf(text, "Energy Distribution for trigger condition #%i", idx);
|
|---|
| 88 | else
|
|---|
| 89 | sprintf(text, "Energy Distribution");
|
|---|
| 90 |
|
|---|
| 91 | char aux[256];
|
|---|
| 92 | strcpy(aux, "Threshold");
|
|---|
| 93 |
|
|---|
| 94 | if (idx>0)
|
|---|
| 95 | sprintf(aux+strlen(aux), " #%i", idx);
|
|---|
| 96 |
|
|---|
| 97 | fHist->SetName(aux);
|
|---|
| 98 | fHist->SetTitle(text);
|
|---|
| 99 | }
|
|---|
| 100 |
|
|---|
| 101 | //-------------------------------------------------------------------------
|
|---|
| 102 | //
|
|---|
| 103 | // Defualt Destructor
|
|---|
| 104 | //
|
|---|
| 105 | MHMcEnergy::~MHMcEnergy()
|
|---|
| 106 | {
|
|---|
| 107 | delete fHist;
|
|---|
| 108 | }
|
|---|
| 109 |
|
|---|
| 110 | //--------------------------------------------------------------------------
|
|---|
| 111 | //
|
|---|
| 112 | // Fill the histogram with the log10 of the energy for triggered events.
|
|---|
| 113 | //
|
|---|
| 114 | void MHMcEnergy::Fill(Float_t log10E, Float_t w)
|
|---|
| 115 | {
|
|---|
| 116 | fHist->Fill(log10E, w);
|
|---|
| 117 | }
|
|---|
| 118 |
|
|---|
| 119 | // -------------------------------------------------------------------------
|
|---|
| 120 | //
|
|---|
| 121 | // Fitting function
|
|---|
| 122 | //
|
|---|
| 123 | void MHMcEnergy::Fit(Axis_t xxmin, Axis_t xxmax)
|
|---|
| 124 | {
|
|---|
| 125 | //
|
|---|
| 126 | // 0: don't draw the function (it is drawn together with the histogram)
|
|---|
| 127 | // Q: quiet mode
|
|---|
| 128 | //
|
|---|
| 129 | fHist->Fit("gaus", "Q0", "", xxmin, xxmax);
|
|---|
| 130 |
|
|---|
| 131 | TF1 *result = fHist->GetFunction("gaus");
|
|---|
| 132 |
|
|---|
| 133 | fThreshold = CalcThreshold(result);
|
|---|
| 134 | fThresholdErr = CalcThresholdErr(result);
|
|---|
| 135 | fGaussPeak = CalcGaussPeak(result);
|
|---|
| 136 | fGaussSigma = CalcGaussSigma(result);
|
|---|
| 137 | }
|
|---|
| 138 |
|
|---|
| 139 | // ------------------------------------------------------------------------
|
|---|
| 140 | //
|
|---|
| 141 | // Helper function for Draw() and DrawClone() which adds some useful
|
|---|
| 142 | // information to the plot.
|
|---|
| 143 | //
|
|---|
| 144 | void MHMcEnergy::DrawLegend() const
|
|---|
| 145 | {
|
|---|
| 146 | char text[256];
|
|---|
| 147 |
|
|---|
| 148 | const Float_t min = fHist->GetMinimum();
|
|---|
| 149 | const Float_t max = fHist->GetMaximum();
|
|---|
| 150 | const Float_t sum = min+max;
|
|---|
| 151 |
|
|---|
| 152 | sprintf(text, "Energy Threshold = %4.1f +- %4.1f GeV",
|
|---|
| 153 | fThreshold, fThresholdErr);
|
|---|
| 154 |
|
|---|
| 155 | TPaveLabel* label = new TPaveLabel(2.2, 0.75*sum, 4.4, 0.90*sum, text);
|
|---|
| 156 | label->SetFillColor(10);
|
|---|
| 157 | label->SetTextSize(0.3);
|
|---|
| 158 | label->SetBit(kCanDelete);
|
|---|
| 159 | label->Draw();
|
|---|
| 160 | }
|
|---|
| 161 |
|
|---|
| 162 | // ------------------------------------------------------------------------
|
|---|
| 163 | //
|
|---|
| 164 | // Drawing function. It creates its own canvas.
|
|---|
| 165 | //
|
|---|
| 166 | void MHMcEnergy::Draw(Option_t *option)
|
|---|
| 167 | {
|
|---|
| 168 | if (!gPad)
|
|---|
| 169 | MH::MakeDefCanvas(fHist);
|
|---|
| 170 |
|
|---|
| 171 | fHist->Draw(option);
|
|---|
| 172 |
|
|---|
| 173 | DrawLegend();
|
|---|
| 174 |
|
|---|
| 175 | gPad->Modified();
|
|---|
| 176 | gPad->Update();
|
|---|
| 177 | }
|
|---|
| 178 |
|
|---|
| 179 | TObject *MHMcEnergy::DrawClone(Option_t *option) const
|
|---|
| 180 | {
|
|---|
| 181 | TCanvas *c = MH::MakeDefCanvas(fHist);
|
|---|
| 182 |
|
|---|
| 183 | //
|
|---|
| 184 | // This is necessary to get the expected bahviour of DrawClone
|
|---|
| 185 | //
|
|---|
| 186 | gROOT->SetSelectedPad(NULL);
|
|---|
| 187 |
|
|---|
| 188 | fHist->DrawClone(option);
|
|---|
| 189 |
|
|---|
| 190 | DrawLegend();
|
|---|
| 191 |
|
|---|
| 192 | c->Modified();
|
|---|
| 193 | c->Update();
|
|---|
| 194 |
|
|---|
| 195 | return c;
|
|---|
| 196 | }
|
|---|
| 197 |
|
|---|
| 198 | // --------------------------------------------------------------------------
|
|---|
| 199 | //
|
|---|
| 200 | // Set the number of bins in the histogran.
|
|---|
| 201 | //
|
|---|
| 202 | void MHMcEnergy::SetNumBins(Int_t nbins)
|
|---|
| 203 | {
|
|---|
| 204 | fHist->SetBins(nbins, 0.5, 4.5);
|
|---|
| 205 | }
|
|---|
| 206 | // --------------------------------------------------------------------------
|
|---|
| 207 | //
|
|---|
| 208 | // Write the threshold and its error in the standard output
|
|---|
| 209 | //
|
|---|
| 210 | void MHMcEnergy::Print(Option_t*) const
|
|---|
| 211 | {
|
|---|
| 212 | cout << "Threshold: " << fThreshold << " +- " << fThresholdErr << endl;
|
|---|
| 213 | }
|
|---|
| 214 |
|
|---|
| 215 | // -------------------------------------------------------------------------
|
|---|
| 216 | //
|
|---|
| 217 | // Return the threshold
|
|---|
| 218 | //
|
|---|
| 219 | Float_t MHMcEnergy::CalcThreshold(TF1 *gauss)
|
|---|
| 220 | {
|
|---|
| 221 | const Float_t p1 = gauss->GetParameter(1);
|
|---|
| 222 |
|
|---|
| 223 | return pow(10, p1);
|
|---|
| 224 | }
|
|---|
| 225 |
|
|---|
| 226 | // -------------------------------------------------------------------------
|
|---|
| 227 | //
|
|---|
| 228 | // Return the error of the threshold.
|
|---|
| 229 | //
|
|---|
| 230 | Float_t MHMcEnergy::CalcThresholdErr(TF1 *gauss)
|
|---|
| 231 | {
|
|---|
| 232 | const Float_t lg10 = log(10);
|
|---|
| 233 | const Float_t p1 = gauss->GetParameter(1);
|
|---|
| 234 | const Float_t p1err = gauss->GetParError(1);
|
|---|
| 235 |
|
|---|
| 236 | // The error has into accuont the error in the fit
|
|---|
| 237 | return pow(10, p1) * p1err * lg10;
|
|---|
| 238 | }
|
|---|
| 239 |
|
|---|
| 240 | // -------------------------------------------------------------------------
|
|---|
| 241 | //
|
|---|
| 242 | // Return the peak of the fitted gaussan function.
|
|---|
| 243 | //
|
|---|
| 244 | Float_t MHMcEnergy::CalcGaussPeak(TF1 *gauss)
|
|---|
| 245 | {
|
|---|
| 246 | return gauss->GetParameter(1);
|
|---|
| 247 | }
|
|---|
| 248 |
|
|---|
| 249 | // -------------------------------------------------------------------------
|
|---|
| 250 | //
|
|---|
| 251 | // Return the sigma of the fitted gaussan function.
|
|---|
| 252 | //
|
|---|
| 253 | Float_t MHMcEnergy::CalcGaussSigma(TF1 *gauss)
|
|---|
| 254 | {
|
|---|
| 255 | return gauss->GetParameter(2);
|
|---|
| 256 | }
|
|---|
| 257 |
|
|---|