source: trunk/MagicSoft/Mars/mhist/MH.cc@ 1336

Last change on this file since 1336 was 1330, checked in by tbretz, 23 years ago
*** empty log message ***
File size: 9.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): Thomas Bretz 07/2001 <mailto:tbretz@astro.uni-wuerzburg.de>
19!
20! Copyright: MAGIC Software Development, 2000-2002
21!
22!
23\* ======================================================================== */
24
25//////////////////////////////////////////////////////////////////////////////
26// //
27// MH //
28// //
29// This is a base tasks for mars histograms. It defines a common interface //
30// for filling the histograms with events (MH::Fill) which is used by a //
31// common 'filler' And a SetupFill member function which may be used //
32// by MFillH. The idea is: //
33// 1) If your Histogram can become filled by one single container //
34// (like MHHillas) you overload MH::Fill and it gets called with //
35// a pointer to the container with which it should be filled. //
36// //
37// 2) You histogram needs several containers to get filled. Than you //
38// have to overload MH::SetupFill and get the necessary objects from //
39// the parameter list. Use this objects in Fill to fill your //
40// histogram. //
41// //
42// If you want to create your own histogram class the new class must be //
43// derived from MH (instead of the base MParContainer) and you must //
44// the fill function of MH. This is the function which is called to fill //
45// the histogram(s) by the data of a corresponding parameter container. //
46// //
47//////////////////////////////////////////////////////////////////////////////
48
49#include "MH.h"
50
51#include <TH1.h>
52#include <TH2.h>
53#include <TH3.h>
54#include <TCanvas.h>
55
56#include "MBinning.h"
57
58ClassImp(MH);
59
60// --------------------------------------------------------------------------
61//
62// Default Constructor. It sets name and title only. Typically you won't
63// need to change this.
64//
65MH::MH(const char *name, const char *title)
66{
67 //
68 // set the name and title of this object
69 //
70 fName = name ? name : "MH";
71 fTitle = title ? title : "Base class for Mars histograms";
72}
73
74// --------------------------------------------------------------------------
75//
76// This is a function which should replace the creation of default
77// canvases like root does. Because this is inconvinient in some aspects.
78// need to change this.
79// You can specify a name for the default canvas and a title. Also
80// width and height can be given.
81// MakeDefCanvas looks for a canvas with the given name. If now name is
82// given the DefCanvasName of root is used. If no such canvas is existing
83// it is created and returned. If such a canvas already exists a new canvas
84// with a name plus anumber is created (the number is calculated by the
85// number of all existing canvases plus one)
86//
87TCanvas *MH::MakeDefCanvas(TString name, const char *title,
88 const UInt_t w, const UInt_t h)
89{
90 const TList *list = (TList*)gROOT->GetListOfCanvases();
91
92 if (name.IsNull())
93 name = gROOT->GetDefCanvasName();
94
95 if (list->FindObject(name))
96 name += Form(" <%d>", list->GetSize()+1);
97
98 return new TCanvas(name, title, w, h);
99}
100
101// --------------------------------------------------------------------------
102//
103// This function works like MakeDefCanvas(name, title, w, h) but name
104// and title are retrieved from the given TObject.
105//
106TCanvas *MH::MakeDefCanvas(const TObject *obj,
107 const UInt_t w, const UInt_t h)
108{
109 return MakeDefCanvas(obj->GetName(), obj->GetTitle(), w, h);
110}
111
112void MH::SetBinning(TH1 *h, const MBinning *binsx)
113{
114 //
115 // Another strange behaviour: TAxis::Set deletes the axis title!
116 //
117 TAxis &x = *h->GetXaxis();
118
119#if ROOT_VERSION_CODE < ROOT_VERSION(3,03,03)
120 TString xtitle = x.GetTitle();
121#endif
122
123 //
124 // This is a necessary workaround if one wants to set
125 // non-equidistant bins after the initialization
126 // TH1D::fNcells must be set correctly.
127 //
128 h->SetBins(binsx->GetNumBins(), 0, 1);
129
130 //
131 // Set the binning of the current histogram to the binning
132 // in one of the two given histograms
133 //
134 x.Set(binsx->GetNumBins(), binsx->GetEdges());
135#if ROOT_VERSION_CODE < ROOT_VERSION(3,03,03)
136 x.SetTitle(xtitle);
137#endif
138}
139
140void MH::SetBinning(TH2 *h, const MBinning *binsx, const MBinning *binsy)
141{
142 TAxis &x = *h->GetXaxis();
143 TAxis &y = *h->GetYaxis();
144
145 //
146 // Another strange behaviour: TAxis::Set deletes the axis title!
147 //
148#if ROOT_VERSION_CODE < ROOT_VERSION(3,03,03)
149 TString xtitle = x.GetTitle();
150 TString ytitle = y.GetTitle();
151#endif
152
153 //
154 // This is a necessary workaround if one wants to set
155 // non-equidistant bins after the initialization
156 // TH1D::fNcells must be set correctly.
157 //
158 h->SetBins(binsx->GetNumBins(), 0, 1,
159 binsy->GetNumBins(), 0, 1);
160
161 //
162 // Set the binning of the current histogram to the binning
163 // in one of the two given histograms
164 //
165 x.Set(binsx->GetNumBins(), binsx->GetEdges());
166 y.Set(binsy->GetNumBins(), binsy->GetEdges());
167#if ROOT_VERSION_CODE < ROOT_VERSION(3,03,03)
168 x.SetTitle(xtitle);
169 y.SetTitle(ytitle);
170#endif
171}
172
173void MH::SetBinning(TH3 *h, const MBinning *binsx, const MBinning *binsy, const MBinning *binsz)
174{
175 //
176 // Another strange behaviour: TAxis::Set deletes the axis title!
177 //
178 TAxis &x = *h->GetXaxis();
179 TAxis &y = *h->GetYaxis();
180 TAxis &z = *h->GetZaxis();
181
182#if ROOT_VERSION_CODE < ROOT_VERSION(3,03,03)
183 TString xtitle = x.GetTitle();
184 TString ytitle = y.GetTitle();
185 TString ztitle = z.GetTitle();
186#endif
187
188 //
189 // This is a necessary workaround if one wants to set
190 // non-equidistant bins after the initialization
191 // TH1D::fNcells must be set correctly.
192 //
193 h->SetBins(binsx->GetNumBins(), 0, 1,
194 binsy->GetNumBins(), 0, 1,
195 binsz->GetNumBins(), 0, 1);
196
197 //
198 // Set the binning of the current histogram to the binning
199 // in one of the two given histograms
200 //
201 x.Set(binsx->GetNumBins(), binsx->GetEdges());
202 y.Set(binsy->GetNumBins(), binsy->GetEdges());
203 z.Set(binsz->GetNumBins(), binsz->GetEdges());
204#if ROOT_VERSION_CODE < ROOT_VERSION(3,03,03)
205 x.SetTitle(xtitle);
206 y.SetTitle(ytitle);
207 z.SetTitle(ztitle);
208#endif
209}
210
211void MH::SetBinning(TH1 *h, const TArrayD *binsx)
212{
213 MBinning bx;
214 bx.SetEdges(*binsx);
215 SetBinning(h, &bx);
216}
217
218void MH::SetBinning(TH2 *h, const TArrayD *binsx, const TArrayD *binsy)
219{
220 MBinning bx;
221 MBinning by;
222 bx.SetEdges(*binsx);
223 by.SetEdges(*binsy);
224 SetBinning(h, &bx, &by);
225}
226
227void MH::SetBinning(TH3 *h, const TArrayD *binsx, const TArrayD *binsy, const TArrayD *binsz)
228{
229 MBinning bx;
230 MBinning by;
231 MBinning bz;
232 bx.SetEdges(*binsx);
233 by.SetEdges(*binsy);
234 bz.SetEdges(*binsz);
235 SetBinning(h, &bx, &by, &bz);
236}
237
238void MH::SetBinning(TH1 *h, const TAxis *binsx)
239{
240 const Int_t nx = binsx->GetNbins();
241
242 TArrayD bx(nx+1);
243 for (int i=0; i<nx; i++) bx[i] = binsx->GetBinLowEdge(i+1);
244 bx[nx] = binsx->GetXmax();
245
246 SetBinning(h, &bx);
247}
248
249void MH::SetBinning(TH2 *h, const TAxis *binsx, const TAxis *binsy)
250{
251 const Int_t nx = binsx->GetNbins();
252 const Int_t ny = binsy->GetNbins();
253
254 TArrayD bx(nx+1);
255 TArrayD by(ny+1);
256 for (int i=0; i<nx; i++) bx[i] = binsx->GetBinLowEdge(i+1);
257 for (int i=0; i<ny; i++) by[i] = binsy->GetBinLowEdge(i+1);
258 bx[nx] = binsx->GetXmax();
259 by[ny] = binsy->GetXmax();
260
261 SetBinning(h, &bx, &by);
262}
263
264void MH::SetBinning(TH3 *h, const TAxis *binsx, const TAxis *binsy, const TAxis *binsz)
265{
266 const Int_t nx = binsx->GetNbins();
267 const Int_t ny = binsy->GetNbins();
268 const Int_t nz = binsz->GetNbins();
269
270 TArrayD bx(nx+1);
271 TArrayD by(ny+1);
272 TArrayD bz(nz+1);
273 for (int i=0; i<nx; i++) bx[i] = binsx->GetBinLowEdge(i+1);
274 for (int i=0; i<ny; i++) by[i] = binsy->GetBinLowEdge(i+1);
275 for (int i=0; i<nz; i++) bz[i] = binsz->GetBinLowEdge(i+1);
276 bx[nx] = binsx->GetXmax();
277 by[ny] = binsy->GetXmax();
278 bz[nz] = binsz->GetXmax();
279
280 SetBinning(h, &bx, &by, &bz);
281}
282
283void MH::SetBinning(TH1 *h, TH1 *x)
284{
285 if (h->InheritsFrom(TH3::Class()) && x->InheritsFrom(TH3::Class()))
286 {
287 SetBinning((TH3*)h, x->GetXaxis(), x->GetYaxis(), x->GetZaxis());
288 return;
289 }
290 if (h->InheritsFrom(TH2::Class()) && x->InheritsFrom(TH2::Class()))
291 {
292 SetBinning((TH2*)h, x->GetXaxis(), x->GetYaxis());
293 return;
294 }
295 if (h->InheritsFrom(TH1::Class()) && x->InheritsFrom(TH1::Class()))
296 {
297 SetBinning(h, x->GetXaxis());
298 return;
299 }
300}
Note: See TracBrowser for help on using the repository browser.