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

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