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

Last change on this file since 1388 was 1388, checked in by tbretz, 22 years ago
*** empty log message ***
File size: 10.1 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 "MLog.h"
57
58#include "MBinning.h"
59
60ClassImp(MH);
61
62// --------------------------------------------------------------------------
63//
64// Default Constructor. It sets name and title only. Typically you won't
65// need to change this.
66//
67MH::MH(const char *name, const char *title)
68{
69 //
70 // set the name and title of this object
71 //
72 fName = name ? name : "MH";
73 fTitle = title ? title : "Base class for Mars histograms";
74}
75
76// --------------------------------------------------------------------------
77//
78// If you want to use the automatic filling of your derived class you
79// must overload this function. If it is not overloaded you cannot use
80// FillH with this class. The argument is a pointer to a container
81// in your paremeter list which is specified in the MFillH constructor
82//
83Bool_t MH::Fill(const MParContainer *par)
84{
85 *fLog << GetDescriptor() << ": Fill not overloaded! Can't be used!" << endl;
86 return kFALSE;
87}
88
89// --------------------------------------------------------------------------
90//
91// This is a function which should replace the creation of default
92// canvases like root does. Because this is inconvinient in some aspects.
93// need to change this.
94// You can specify a name for the default canvas and a title. Also
95// width and height can be given.
96// MakeDefCanvas looks for a canvas with the given name. If now name is
97// given the DefCanvasName of root is used. If no such canvas is existing
98// it is created and returned. If such a canvas already exists a new canvas
99// with a name plus anumber is created (the number is calculated by the
100// number of all existing canvases plus one)
101//
102TCanvas *MH::MakeDefCanvas(TString name, const char *title,
103 const UInt_t w, const UInt_t h)
104{
105 const TList *list = (TList*)gROOT->GetListOfCanvases();
106
107 if (name.IsNull())
108 name = gROOT->GetDefCanvasName();
109
110 if (list->FindObject(name))
111 name += Form(" <%d>", list->GetSize()+1);
112
113 return new TCanvas(name, title, w, h);
114}
115
116// --------------------------------------------------------------------------
117//
118// This function works like MakeDefCanvas(name, title, w, h) but name
119// and title are retrieved from the given TObject.
120//
121TCanvas *MH::MakeDefCanvas(const TObject *obj,
122 const UInt_t w, const UInt_t h)
123{
124 return MakeDefCanvas(obj->GetName(), obj->GetTitle(), w, h);
125}
126
127void MH::SetBinning(TH1 *h, const MBinning *binsx)
128{
129 //
130 // Another strange behaviour: TAxis::Set deletes the axis title!
131 //
132 TAxis &x = *h->GetXaxis();
133
134#if ROOT_VERSION_CODE < ROOT_VERSION(3,03,03)
135 TString xtitle = x.GetTitle();
136#endif
137
138 //
139 // This is a necessary workaround if one wants to set
140 // non-equidistant bins after the initialization
141 // TH1D::fNcells must be set correctly.
142 //
143 h->SetBins(binsx->GetNumBins(), 0, 1);
144
145 //
146 // Set the binning of the current histogram to the binning
147 // in one of the two given histograms
148 //
149 x.Set(binsx->GetNumBins(), binsx->GetEdges());
150#if ROOT_VERSION_CODE < ROOT_VERSION(3,03,03)
151 x.SetTitle(xtitle);
152#endif
153}
154
155void MH::SetBinning(TH2 *h, const MBinning *binsx, const MBinning *binsy)
156{
157 TAxis &x = *h->GetXaxis();
158 TAxis &y = *h->GetYaxis();
159
160 //
161 // Another strange behaviour: TAxis::Set deletes the axis title!
162 //
163#if ROOT_VERSION_CODE < ROOT_VERSION(3,03,03)
164 TString xtitle = x.GetTitle();
165 TString ytitle = y.GetTitle();
166#endif
167
168 //
169 // This is a necessary workaround if one wants to set
170 // non-equidistant bins after the initialization
171 // TH1D::fNcells must be set correctly.
172 //
173 h->SetBins(binsx->GetNumBins(), 0, 1,
174 binsy->GetNumBins(), 0, 1);
175
176 //
177 // Set the binning of the current histogram to the binning
178 // in one of the two given histograms
179 //
180 x.Set(binsx->GetNumBins(), binsx->GetEdges());
181 y.Set(binsy->GetNumBins(), binsy->GetEdges());
182#if ROOT_VERSION_CODE < ROOT_VERSION(3,03,03)
183 x.SetTitle(xtitle);
184 y.SetTitle(ytitle);
185#endif
186}
187
188void MH::SetBinning(TH3 *h, const MBinning *binsx, const MBinning *binsy, const MBinning *binsz)
189{
190 //
191 // Another strange behaviour: TAxis::Set deletes the axis title!
192 //
193 TAxis &x = *h->GetXaxis();
194 TAxis &y = *h->GetYaxis();
195 TAxis &z = *h->GetZaxis();
196
197#if ROOT_VERSION_CODE < ROOT_VERSION(3,03,03)
198 TString xtitle = x.GetTitle();
199 TString ytitle = y.GetTitle();
200 TString ztitle = z.GetTitle();
201#endif
202
203 //
204 // This is a necessary workaround if one wants to set
205 // non-equidistant bins after the initialization
206 // TH1D::fNcells must be set correctly.
207 //
208 h->SetBins(binsx->GetNumBins(), 0, 1,
209 binsy->GetNumBins(), 0, 1,
210 binsz->GetNumBins(), 0, 1);
211
212 //
213 // Set the binning of the current histogram to the binning
214 // in one of the two given histograms
215 //
216 x.Set(binsx->GetNumBins(), binsx->GetEdges());
217 y.Set(binsy->GetNumBins(), binsy->GetEdges());
218 z.Set(binsz->GetNumBins(), binsz->GetEdges());
219#if ROOT_VERSION_CODE < ROOT_VERSION(3,03,03)
220 x.SetTitle(xtitle);
221 y.SetTitle(ytitle);
222 z.SetTitle(ztitle);
223#endif
224}
225
226void MH::SetBinning(TH1 *h, const TArrayD *binsx)
227{
228 MBinning bx;
229 bx.SetEdges(*binsx);
230 SetBinning(h, &bx);
231}
232
233void MH::SetBinning(TH2 *h, const TArrayD *binsx, const TArrayD *binsy)
234{
235 MBinning bx;
236 MBinning by;
237 bx.SetEdges(*binsx);
238 by.SetEdges(*binsy);
239 SetBinning(h, &bx, &by);
240}
241
242void MH::SetBinning(TH3 *h, const TArrayD *binsx, const TArrayD *binsy, const TArrayD *binsz)
243{
244 MBinning bx;
245 MBinning by;
246 MBinning bz;
247 bx.SetEdges(*binsx);
248 by.SetEdges(*binsy);
249 bz.SetEdges(*binsz);
250 SetBinning(h, &bx, &by, &bz);
251}
252
253void MH::SetBinning(TH1 *h, const TAxis *binsx)
254{
255 const Int_t nx = binsx->GetNbins();
256
257 TArrayD bx(nx+1);
258 for (int i=0; i<nx; i++) bx[i] = binsx->GetBinLowEdge(i+1);
259 bx[nx] = binsx->GetXmax();
260
261 SetBinning(h, &bx);
262}
263
264void MH::SetBinning(TH2 *h, const TAxis *binsx, const TAxis *binsy)
265{
266 const Int_t nx = binsx->GetNbins();
267 const Int_t ny = binsy->GetNbins();
268
269 TArrayD bx(nx+1);
270 TArrayD by(ny+1);
271 for (int i=0; i<nx; i++) bx[i] = binsx->GetBinLowEdge(i+1);
272 for (int i=0; i<ny; i++) by[i] = binsy->GetBinLowEdge(i+1);
273 bx[nx] = binsx->GetXmax();
274 by[ny] = binsy->GetXmax();
275
276 SetBinning(h, &bx, &by);
277}
278
279void MH::SetBinning(TH3 *h, const TAxis *binsx, const TAxis *binsy, const TAxis *binsz)
280{
281 const Int_t nx = binsx->GetNbins();
282 const Int_t ny = binsy->GetNbins();
283 const Int_t nz = binsz->GetNbins();
284
285 TArrayD bx(nx+1);
286 TArrayD by(ny+1);
287 TArrayD bz(nz+1);
288 for (int i=0; i<nx; i++) bx[i] = binsx->GetBinLowEdge(i+1);
289 for (int i=0; i<ny; i++) by[i] = binsy->GetBinLowEdge(i+1);
290 for (int i=0; i<nz; i++) bz[i] = binsz->GetBinLowEdge(i+1);
291 bx[nx] = binsx->GetXmax();
292 by[ny] = binsy->GetXmax();
293 bz[nz] = binsz->GetXmax();
294
295 SetBinning(h, &bx, &by, &bz);
296}
297
298void MH::SetBinning(TH1 *h, TH1 *x)
299{
300 if (h->InheritsFrom(TH3::Class()) && x->InheritsFrom(TH3::Class()))
301 {
302 SetBinning((TH3*)h, x->GetXaxis(), x->GetYaxis(), x->GetZaxis());
303 return;
304 }
305 if (h->InheritsFrom(TH2::Class()) && x->InheritsFrom(TH2::Class()))
306 {
307 SetBinning((TH2*)h, x->GetXaxis(), x->GetYaxis());
308 return;
309 }
310 if (h->InheritsFrom(TH1::Class()) && x->InheritsFrom(TH1::Class()))
311 {
312 SetBinning(h, x->GetXaxis());
313 return;
314 }
315}
Note: See TracBrowser for help on using the repository browser.