source: trunk/MagicSoft/Mars/mhist/MHMcEnergy.cc@ 971

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