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

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