source: trunk/MagicSoft/Mars/mhistmc/MHMcEnergy.cc@ 2017

Last change on this file since 2017 was 2017, checked in by tbretz, 21 years ago
*** empty log message ***
File size: 6.5 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 TVirtualPad *pad = gPad ? gPad : MH::MakeDefCanvas(this);
173 pad->SetBorderMode(0);
174
175 AppendPad("");
176
177 fHist->Draw(option);
178
179 DrawLegend();
180
181 pad->Modified();
182 pad->Update();
183}
184
185// --------------------------------------------------------------------------
186//
187// Set the number of bins in the histogran.
188//
189void MHMcEnergy::SetNumBins(Int_t nbins)
190{
191 fHist->SetBins(nbins, 0.5, 4.5);
192}
193// --------------------------------------------------------------------------
194//
195// Write the threshold and its error in the standard output
196//
197void MHMcEnergy::Print(Option_t*) const
198{
199 *fLog << all << "Threshold: " << fThreshold << " +- " << fThresholdErr << endl;
200}
201
202// -------------------------------------------------------------------------
203//
204// Return the threshold
205//
206Float_t MHMcEnergy::CalcThreshold(TF1 *gauss)
207{
208 const Float_t p1 = gauss->GetParameter(1);
209
210 return pow(10, p1);
211}
212
213// -------------------------------------------------------------------------
214//
215// Return the error of the threshold.
216//
217Float_t MHMcEnergy::CalcThresholdErr(TF1 *gauss)
218{
219 const Float_t lg10 = log(10);
220 const Float_t p1 = gauss->GetParameter(1);
221 const Float_t p1err = gauss->GetParError(1);
222
223 // The error has into accuont the error in the fit
224 return pow(10, p1) * p1err * lg10;
225}
226
227// -------------------------------------------------------------------------
228//
229// Return the peak of the fitted gaussan function.
230//
231Float_t MHMcEnergy::CalcGaussPeak(TF1 *gauss)
232{
233 return gauss->GetParameter(1);
234}
235
236// -------------------------------------------------------------------------
237//
238// Return the sigma of the fitted gaussan function.
239//
240Float_t MHMcEnergy::CalcGaussSigma(TF1 *gauss)
241{
242 return gauss->GetParameter(2);
243}
244
245TH1 *MHMcEnergy::GetHistByName(const TString name)
246{
247 return fHist;
248}
Note: See TracBrowser for help on using the repository browser.