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 | // Remark: the static member function (eg MakeDefCanvas) can be called //
|
---|
48 | // from everywhere using: MH::MakeDefCanvas(...) //
|
---|
49 | // //
|
---|
50 | //////////////////////////////////////////////////////////////////////////////
|
---|
51 |
|
---|
52 | #include "MH.h"
|
---|
53 |
|
---|
54 | #include <TH1.h>
|
---|
55 | #include <TH2.h>
|
---|
56 | #include <TH3.h>
|
---|
57 | #include <TGaxis.h>
|
---|
58 | #include <TCanvas.h>
|
---|
59 | #include <TLegend.h>
|
---|
60 | #include <TPaveStats.h>
|
---|
61 | #if ROOT_VERSION_CODE > ROOT_VERSION(3,04,01)
|
---|
62 | #include <THLimitsFinder.h>
|
---|
63 | #endif
|
---|
64 |
|
---|
65 | #include "MLog.h"
|
---|
66 | #include "MLogManip.h"
|
---|
67 |
|
---|
68 | #include "MParList.h"
|
---|
69 | #include "MParContainer.h"
|
---|
70 |
|
---|
71 | #include "MBinning.h"
|
---|
72 |
|
---|
73 | ClassImp(MH);
|
---|
74 |
|
---|
75 | // --------------------------------------------------------------------------
|
---|
76 | //
|
---|
77 | // Default Constructor. It sets name and title only. Typically you won't
|
---|
78 | // need to change this.
|
---|
79 | //
|
---|
80 | MH::MH(const char *name, const char *title)
|
---|
81 | {
|
---|
82 | //
|
---|
83 | // set the name and title of this object
|
---|
84 | //
|
---|
85 | fName = name ? name : "MH";
|
---|
86 | fTitle = title ? title : "Base class for Mars histograms";
|
---|
87 | }
|
---|
88 |
|
---|
89 | // --------------------------------------------------------------------------
|
---|
90 | //
|
---|
91 | // If you want to use the automatic filling of your derived class you
|
---|
92 | // must overload this function. If it is not overloaded you cannot use
|
---|
93 | // FillH with this class. The argument is a pointer to a container
|
---|
94 | // in your paremeter list which is specified in the MFillH constructor.
|
---|
95 | // If you are not going to use it you should at least add
|
---|
96 | // Bool_t MH::Fill(const MParContainer *) { return kTRUE; }
|
---|
97 | // to your class definition.
|
---|
98 | //
|
---|
99 | Bool_t MH::Fill(const MParContainer *par)
|
---|
100 | {
|
---|
101 | *fLog << warn << GetDescriptor() << ": Fill not overloaded! Can't be used!" << endl;
|
---|
102 | return kFALSE;
|
---|
103 | }
|
---|
104 |
|
---|
105 | // --------------------------------------------------------------------------
|
---|
106 | //
|
---|
107 | // This virtual function is ment as a generalized interface to retrieve
|
---|
108 | // a pointer to a root histogram from the MH-derived class.
|
---|
109 | //
|
---|
110 | TH1 *MH::GetHistByName(const TString name)
|
---|
111 | {
|
---|
112 | *fLog << warn << GetDescriptor() << ": GetHistByName not overloaded! Can't be used!" << endl;
|
---|
113 | return NULL;
|
---|
114 | }
|
---|
115 |
|
---|
116 | // --------------------------------------------------------------------------
|
---|
117 | //
|
---|
118 | // This is a function which should replace the creation of default
|
---|
119 | // canvases like root does. Because this is inconvinient in some aspects.
|
---|
120 | // need to change this.
|
---|
121 | // You can specify a name for the default canvas and a title. Also
|
---|
122 | // width and height can be given.
|
---|
123 | // MakeDefCanvas looks for a canvas with the given name. If now name is
|
---|
124 | // given the DefCanvasName of root is used. If no such canvas is existing
|
---|
125 | // it is created and returned. If such a canvas already exists a new canvas
|
---|
126 | // with a name plus anumber is created (the number is calculated by the
|
---|
127 | // number of all existing canvases plus one)
|
---|
128 | //
|
---|
129 | TCanvas *MH::MakeDefCanvas(TString name, const char *title,
|
---|
130 | const UInt_t w, const UInt_t h)
|
---|
131 | {
|
---|
132 | const TList *list = (TList*)gROOT->GetListOfCanvases();
|
---|
133 |
|
---|
134 | if (name.IsNull())
|
---|
135 | name = gROOT->GetDefCanvasName();
|
---|
136 |
|
---|
137 | if (list->FindObject(name))
|
---|
138 | name += Form(" <%d>", list->GetSize()+1);
|
---|
139 |
|
---|
140 | return new TCanvas(name, title, w, h);
|
---|
141 | }
|
---|
142 |
|
---|
143 | // --------------------------------------------------------------------------
|
---|
144 | //
|
---|
145 | // This function works like MakeDefCanvas(name, title, w, h) but name
|
---|
146 | // and title are retrieved from the given TObject.
|
---|
147 | //
|
---|
148 | TCanvas *MH::MakeDefCanvas(const TObject *obj,
|
---|
149 | const UInt_t w, const UInt_t h)
|
---|
150 | {
|
---|
151 | return MakeDefCanvas(obj->GetName(), obj->GetTitle(), w, h);
|
---|
152 | }
|
---|
153 |
|
---|
154 | // --------------------------------------------------------------------------
|
---|
155 | //
|
---|
156 | // Applies a given binning to a 1D-histogram
|
---|
157 | //
|
---|
158 | void MH::SetBinning(TH1 *h, const MBinning *binsx)
|
---|
159 | {
|
---|
160 | //
|
---|
161 | // Another strange behaviour: TAxis::Set deletes the axis title!
|
---|
162 | //
|
---|
163 | TAxis &x = *h->GetXaxis();
|
---|
164 |
|
---|
165 | #if ROOT_VERSION_CODE < ROOT_VERSION(3,03,03)
|
---|
166 | TString xtitle = x.GetTitle();
|
---|
167 | #endif
|
---|
168 |
|
---|
169 | //
|
---|
170 | // This is a necessary workaround if one wants to set
|
---|
171 | // non-equidistant bins after the initialization
|
---|
172 | // TH1D::fNcells must be set correctly.
|
---|
173 | //
|
---|
174 | h->SetBins(binsx->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 | #if ROOT_VERSION_CODE < ROOT_VERSION(3,03,03)
|
---|
182 | x.SetTitle(xtitle);
|
---|
183 | #endif
|
---|
184 | }
|
---|
185 |
|
---|
186 | // --------------------------------------------------------------------------
|
---|
187 | //
|
---|
188 | // Applies given binnings to the two axis of a 2D-histogram
|
---|
189 | //
|
---|
190 | void MH::SetBinning(TH2 *h, const MBinning *binsx, const MBinning *binsy)
|
---|
191 | {
|
---|
192 | TAxis &x = *h->GetXaxis();
|
---|
193 | TAxis &y = *h->GetYaxis();
|
---|
194 |
|
---|
195 | //
|
---|
196 | // Another strange behaviour: TAxis::Set deletes the axis title!
|
---|
197 | //
|
---|
198 | #if ROOT_VERSION_CODE < ROOT_VERSION(3,03,03)
|
---|
199 | TString xtitle = x.GetTitle();
|
---|
200 | TString ytitle = y.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 |
|
---|
211 | //
|
---|
212 | // Set the binning of the current histogram to the binning
|
---|
213 | // in one of the two given histograms
|
---|
214 | //
|
---|
215 | x.Set(binsx->GetNumBins(), binsx->GetEdges());
|
---|
216 | y.Set(binsy->GetNumBins(), binsy->GetEdges());
|
---|
217 | #if ROOT_VERSION_CODE < ROOT_VERSION(3,03,03)
|
---|
218 | x.SetTitle(xtitle);
|
---|
219 | y.SetTitle(ytitle);
|
---|
220 | #endif
|
---|
221 | }
|
---|
222 |
|
---|
223 | // --------------------------------------------------------------------------
|
---|
224 | //
|
---|
225 | // Applies given binnings to the three axis of a 3D-histogram
|
---|
226 | //
|
---|
227 | void MH::SetBinning(TH3 *h, const MBinning *binsx, const MBinning *binsy, const MBinning *binsz)
|
---|
228 | {
|
---|
229 | //
|
---|
230 | // Another strange behaviour: TAxis::Set deletes the axis title!
|
---|
231 | //
|
---|
232 | TAxis &x = *h->GetXaxis();
|
---|
233 | TAxis &y = *h->GetYaxis();
|
---|
234 | TAxis &z = *h->GetZaxis();
|
---|
235 |
|
---|
236 | #if ROOT_VERSION_CODE < ROOT_VERSION(3,03,03)
|
---|
237 | TString xtitle = x.GetTitle();
|
---|
238 | TString ytitle = y.GetTitle();
|
---|
239 | TString ztitle = z.GetTitle();
|
---|
240 | #endif
|
---|
241 |
|
---|
242 | //
|
---|
243 | // This is a necessary workaround if one wants to set
|
---|
244 | // non-equidistant bins after the initialization
|
---|
245 | // TH1D::fNcells must be set correctly.
|
---|
246 | //
|
---|
247 | h->SetBins(binsx->GetNumBins(), 0, 1,
|
---|
248 | binsy->GetNumBins(), 0, 1,
|
---|
249 | binsz->GetNumBins(), 0, 1);
|
---|
250 |
|
---|
251 | //
|
---|
252 | // Set the binning of the current histogram to the binning
|
---|
253 | // in one of the two given histograms
|
---|
254 | //
|
---|
255 | x.Set(binsx->GetNumBins(), binsx->GetEdges());
|
---|
256 | y.Set(binsy->GetNumBins(), binsy->GetEdges());
|
---|
257 | z.Set(binsz->GetNumBins(), binsz->GetEdges());
|
---|
258 | #if ROOT_VERSION_CODE < ROOT_VERSION(3,03,03)
|
---|
259 | x.SetTitle(xtitle);
|
---|
260 | y.SetTitle(ytitle);
|
---|
261 | z.SetTitle(ztitle);
|
---|
262 | #endif
|
---|
263 | }
|
---|
264 |
|
---|
265 | // --------------------------------------------------------------------------
|
---|
266 | //
|
---|
267 | // Applies given binning (the n+1 edges) to the axis of a 1D-histogram
|
---|
268 | //
|
---|
269 | void MH::SetBinning(TH1 *h, const TArrayD &binsx)
|
---|
270 | {
|
---|
271 | MBinning bx;
|
---|
272 | bx.SetEdges(binsx);
|
---|
273 | SetBinning(h, &bx);
|
---|
274 | }
|
---|
275 |
|
---|
276 | // --------------------------------------------------------------------------
|
---|
277 | //
|
---|
278 | // Applies given binning (the n+1 edges) to the two axis of a
|
---|
279 | // 2D-histogram
|
---|
280 | //
|
---|
281 | void MH::SetBinning(TH2 *h, const TArrayD &binsx, const TArrayD &binsy)
|
---|
282 | {
|
---|
283 | MBinning bx;
|
---|
284 | MBinning by;
|
---|
285 | bx.SetEdges(binsx);
|
---|
286 | by.SetEdges(binsy);
|
---|
287 | SetBinning(h, &bx, &by);
|
---|
288 | }
|
---|
289 |
|
---|
290 | // --------------------------------------------------------------------------
|
---|
291 | //
|
---|
292 | // Applies given binning (the n+1 edges) to the three axis of a
|
---|
293 | // 3D-histogram
|
---|
294 | //
|
---|
295 | void MH::SetBinning(TH3 *h, const TArrayD &binsx, const TArrayD &binsy, const TArrayD &binsz)
|
---|
296 | {
|
---|
297 | MBinning bx;
|
---|
298 | MBinning by;
|
---|
299 | MBinning bz;
|
---|
300 | bx.SetEdges(binsx);
|
---|
301 | by.SetEdges(binsy);
|
---|
302 | bz.SetEdges(binsz);
|
---|
303 | SetBinning(h, &bx, &by, &bz);
|
---|
304 | }
|
---|
305 |
|
---|
306 | // --------------------------------------------------------------------------
|
---|
307 | //
|
---|
308 | // Applies the binning of a TAxis (eg from a root histogram) to the axis
|
---|
309 | // of a 1D-histogram
|
---|
310 | //
|
---|
311 | void MH::SetBinning(TH1 *h, const TAxis *binsx)
|
---|
312 | {
|
---|
313 | const Int_t nx = binsx->GetNbins();
|
---|
314 |
|
---|
315 | TArrayD bx(nx+1);
|
---|
316 | for (int i=0; i<nx; i++) bx[i] = binsx->GetBinLowEdge(i+1);
|
---|
317 | bx[nx] = binsx->GetXmax();
|
---|
318 |
|
---|
319 | SetBinning(h, bx);
|
---|
320 | }
|
---|
321 |
|
---|
322 | // --------------------------------------------------------------------------
|
---|
323 | //
|
---|
324 | // Applies the binnings of the TAxis' (eg from a root histogram) to the
|
---|
325 | // two axis' of a 2D-histogram
|
---|
326 | //
|
---|
327 | void MH::SetBinning(TH2 *h, const TAxis *binsx, const TAxis *binsy)
|
---|
328 | {
|
---|
329 | const Int_t nx = binsx->GetNbins();
|
---|
330 | const Int_t ny = binsy->GetNbins();
|
---|
331 |
|
---|
332 | TArrayD bx(nx+1);
|
---|
333 | TArrayD by(ny+1);
|
---|
334 | for (int i=0; i<nx; i++) bx[i] = binsx->GetBinLowEdge(i+1);
|
---|
335 | for (int i=0; i<ny; i++) by[i] = binsy->GetBinLowEdge(i+1);
|
---|
336 | bx[nx] = binsx->GetXmax();
|
---|
337 | by[ny] = binsy->GetXmax();
|
---|
338 |
|
---|
339 | SetBinning(h, bx, by);
|
---|
340 | }
|
---|
341 |
|
---|
342 | // --------------------------------------------------------------------------
|
---|
343 | //
|
---|
344 | // Applies the binnings of the TAxis' (eg from a root histogram) to the
|
---|
345 | // three axis' of a 3D-histogram
|
---|
346 | //
|
---|
347 | void MH::SetBinning(TH3 *h, const TAxis *binsx, const TAxis *binsy, const TAxis *binsz)
|
---|
348 | {
|
---|
349 | const Int_t nx = binsx->GetNbins();
|
---|
350 | const Int_t ny = binsy->GetNbins();
|
---|
351 | const Int_t nz = binsz->GetNbins();
|
---|
352 |
|
---|
353 | TArrayD bx(nx+1);
|
---|
354 | TArrayD by(ny+1);
|
---|
355 | TArrayD bz(nz+1);
|
---|
356 | for (int i=0; i<nx; i++) bx[i] = binsx->GetBinLowEdge(i+1);
|
---|
357 | for (int i=0; i<ny; i++) by[i] = binsy->GetBinLowEdge(i+1);
|
---|
358 | for (int i=0; i<nz; i++) bz[i] = binsz->GetBinLowEdge(i+1);
|
---|
359 | bx[nx] = binsx->GetXmax();
|
---|
360 | by[ny] = binsy->GetXmax();
|
---|
361 | bz[nz] = binsz->GetXmax();
|
---|
362 |
|
---|
363 | SetBinning(h, bx, by, bz);
|
---|
364 | }
|
---|
365 |
|
---|
366 | // --------------------------------------------------------------------------
|
---|
367 | //
|
---|
368 | // Applies the binnings of one root-histogram x to another one h
|
---|
369 | // Both histograms must be of the same type: TH1, TH2 or TH3
|
---|
370 | //
|
---|
371 | void MH::SetBinning(TH1 *h, const TH1 *x)
|
---|
372 | {
|
---|
373 | if (h->InheritsFrom(TH3::Class()) && x->InheritsFrom(TH3::Class()))
|
---|
374 | {
|
---|
375 | SetBinning((TH3*)h, ((TH1*)x)->GetXaxis(), ((TH1*)x)->GetYaxis(), ((TH1*)x)->GetZaxis());
|
---|
376 | return;
|
---|
377 | }
|
---|
378 | if (h->InheritsFrom(TH3::Class()) || x->InheritsFrom(TH3::Class()))
|
---|
379 | return;
|
---|
380 | if (h->InheritsFrom(TH2::Class()) && x->InheritsFrom(TH2::Class()))
|
---|
381 | {
|
---|
382 | SetBinning((TH2*)h, ((TH1*)x)->GetXaxis(), ((TH1*)x)->GetYaxis());
|
---|
383 | return;
|
---|
384 | }
|
---|
385 | if (h->InheritsFrom(TH2::Class()) || x->InheritsFrom(TH2::Class()))
|
---|
386 | return;
|
---|
387 | if (h->InheritsFrom(TH1::Class()) && x->InheritsFrom(TH1::Class()))
|
---|
388 | {
|
---|
389 | SetBinning(h, ((TH1*)x)->GetXaxis());
|
---|
390 | return;
|
---|
391 | }
|
---|
392 | }
|
---|
393 |
|
---|
394 | // --------------------------------------------------------------------------
|
---|
395 | //
|
---|
396 | // Multiplies all entries in a TArrayD by a float f
|
---|
397 | //
|
---|
398 | void MH::ScaleArray(TArrayD &bins, Double_t f)
|
---|
399 | {
|
---|
400 | for (int i=0; i<bins.GetSize(); i++)
|
---|
401 | bins[i] *= f;
|
---|
402 | }
|
---|
403 |
|
---|
404 | // --------------------------------------------------------------------------
|
---|
405 | //
|
---|
406 | // Scales the binning of a TAxis by a float f
|
---|
407 | //
|
---|
408 | TArrayD MH::ScaleAxis(TAxis &axe, Double_t f)
|
---|
409 | {
|
---|
410 | TArrayD arr(axe.GetNbins()+1);
|
---|
411 |
|
---|
412 | for (int i=1; i<=axe.GetNbins()+1; i++)
|
---|
413 | arr[i-1] = axe.GetBinLowEdge(i);
|
---|
414 |
|
---|
415 | ScaleArray(arr, f);
|
---|
416 |
|
---|
417 | return arr;
|
---|
418 | }
|
---|
419 |
|
---|
420 | // --------------------------------------------------------------------------
|
---|
421 | //
|
---|
422 | // Scales the binning of one, two or three axis of a histogram by a float f
|
---|
423 | //
|
---|
424 | void MH::ScaleAxis(TH1 *h, Double_t fx, Double_t fy, Double_t fz)
|
---|
425 | {
|
---|
426 | if (h->InheritsFrom(TH3::Class()))
|
---|
427 | {
|
---|
428 | SetBinning((TH3*)h,
|
---|
429 | ScaleAxis(*h->GetXaxis(), fx),
|
---|
430 | ScaleAxis(*h->GetYaxis(), fy),
|
---|
431 | ScaleAxis(*h->GetZaxis(), fz));
|
---|
432 | return;
|
---|
433 | }
|
---|
434 |
|
---|
435 | if (h->InheritsFrom(TH2::Class()))
|
---|
436 | {
|
---|
437 | SetBinning((TH2*)h,
|
---|
438 | ScaleAxis(*h->GetXaxis(), fx),
|
---|
439 | ScaleAxis(*h->GetYaxis(), fy));
|
---|
440 | return;
|
---|
441 | }
|
---|
442 |
|
---|
443 | if (h->InheritsFrom(TH1::Class()))
|
---|
444 | SetBinning(h, ScaleAxis(*h->GetXaxis(), fx));
|
---|
445 | }
|
---|
446 |
|
---|
447 | // --------------------------------------------------------------------------
|
---|
448 | //
|
---|
449 | // Tries to find a MBinning container with the name "Binning"+name
|
---|
450 | // in the given parameter list. If it was found it is applied to the
|
---|
451 | // given histogram. This is only valid for 1D-histograms
|
---|
452 | //
|
---|
453 | Bool_t MH::ApplyBinning(const MParList &plist, TString name, TH1 *h)
|
---|
454 | {
|
---|
455 | if (h->InheritsFrom(TH2::Class()) || h->InheritsFrom(TH3::Class()))
|
---|
456 | {
|
---|
457 | gLog << warn << "MH::ApplyBinning: '" << h->GetName() << "' is not a basic TH1 object... no binning applied." << endl;
|
---|
458 | return kFALSE;
|
---|
459 | }
|
---|
460 |
|
---|
461 | const MBinning *bins = (MBinning*)plist.FindObject("Binning"+name);
|
---|
462 | if (!bins)
|
---|
463 | {
|
---|
464 | gLog << warn << "Object 'Binning" << name << "' [MBinning] not found... no binning applied." << endl;
|
---|
465 | return kFALSE;
|
---|
466 | }
|
---|
467 |
|
---|
468 | SetBinning(h, bins);
|
---|
469 | return kTRUE;
|
---|
470 | }
|
---|
471 |
|
---|
472 | void MH::FindGoodLimits(Int_t nbins, Int_t &newbins, Double_t &xmin, Double_t &xmax, Bool_t isInteger)
|
---|
473 | {
|
---|
474 | #if ROOT_VERSION_CODE > ROOT_VERSION(3,04,01)
|
---|
475 | THLimitsFinder::OptimizeLimits(nbins, newbins, xmin, xmax, isInteger);
|
---|
476 | #else
|
---|
477 | //*-*-*-*-*-*-*-*-*Find reasonable bin values*-*-*-*-*-*-*-*-*-*-*-*-*-*-*
|
---|
478 | //*-* ==========================
|
---|
479 |
|
---|
480 | Double_t dx = 0.1*(xmax-xmin);
|
---|
481 | Double_t umin = xmin - dx;
|
---|
482 | Double_t umax = xmax + dx;
|
---|
483 |
|
---|
484 | if (umin < 0 && xmin >= 0)
|
---|
485 | umin = 0;
|
---|
486 |
|
---|
487 | if (umax > 0 && xmax <= 0)
|
---|
488 | umax = 0;
|
---|
489 |
|
---|
490 | Double_t binlow =0;
|
---|
491 | Double_t binhigh =0;
|
---|
492 | Double_t binwidth=0;
|
---|
493 |
|
---|
494 | TGaxis::Optimize(umin, umax, nbins, binlow, binhigh, nbins, binwidth, "");
|
---|
495 |
|
---|
496 | if (binwidth <= 0 || binwidth > 1.e+39)
|
---|
497 | {
|
---|
498 | xmin = -1;
|
---|
499 | xmax = 1;
|
---|
500 | }
|
---|
501 | else
|
---|
502 | {
|
---|
503 | xmin = binlow;
|
---|
504 | xmax = binhigh;
|
---|
505 | }
|
---|
506 |
|
---|
507 | if (isInteger)
|
---|
508 | {
|
---|
509 | Int_t ixmin = (Int_t)xmin;
|
---|
510 | Int_t ixmax = (Int_t)xmax;
|
---|
511 | Double_t dxmin = (Double_t)ixmin;
|
---|
512 | Double_t dxmax = (Double_t)ixmax;
|
---|
513 |
|
---|
514 | xmin = xmin<0 && xmin!=dxmin ? dxmin - 1 : dxmin;
|
---|
515 | xmax = xmax>0 && xmax!=dxmax ? dxmax + 1 : dxmax;
|
---|
516 |
|
---|
517 | if (xmin>=xmax)
|
---|
518 | xmax = xmin+1;
|
---|
519 |
|
---|
520 | Int_t bw = 1 + (Int_t)((xmax-xmin)/nbins);
|
---|
521 |
|
---|
522 | nbins = (Int_t)((xmax-xmin)/bw);
|
---|
523 |
|
---|
524 | if (xmin+nbins*bw < xmax)
|
---|
525 | {
|
---|
526 | nbins++;
|
---|
527 | xmax = xmin +nbins*bw;
|
---|
528 | }
|
---|
529 | }
|
---|
530 |
|
---|
531 | newbins = nbins;
|
---|
532 | #endif
|
---|
533 | }
|
---|
534 |
|
---|
535 | // --------------------------------------------------------------------------
|
---|
536 | //
|
---|
537 | // Returns the lowest entry in a histogram which is greater than gt (eg >0)
|
---|
538 | //
|
---|
539 | Double_t MH::GetMinimumGT(const TH1 &h, Double_t gt)
|
---|
540 | {
|
---|
541 | Double_t min = FLT_MAX;
|
---|
542 |
|
---|
543 | const TAxis &axe = *((TH1&)h).GetXaxis();
|
---|
544 |
|
---|
545 | for (int i=1; i<=axe.GetNbins(); i++)
|
---|
546 | {
|
---|
547 | Double_t x = h.GetBinContent(i);
|
---|
548 | if (gt<x && x<min)
|
---|
549 | min = x;
|
---|
550 | }
|
---|
551 | return min;
|
---|
552 | }
|
---|
553 |
|
---|
554 | // --------------------------------------------------------------------------
|
---|
555 | //
|
---|
556 | // Returns the bin center in a logarithmic scale. If the given bin
|
---|
557 | // number is <1 it is set to 1. If it is =GetNbins() it is set to
|
---|
558 | // GetNbins()
|
---|
559 | //
|
---|
560 | Double_t MH::GetBinCenterLog(const TAxis &axe, Int_t nbin)
|
---|
561 | {
|
---|
562 | if (nbin>axe.GetNbins())
|
---|
563 | nbin = axe.GetNbins();
|
---|
564 |
|
---|
565 | if (nbin<1)
|
---|
566 | nbin = 1;
|
---|
567 |
|
---|
568 | const Double_t lo = axe.GetBinLowEdge(nbin);
|
---|
569 | const Double_t hi = axe.GetBinUpEdge(nbin);
|
---|
570 |
|
---|
571 | const Double_t val = log10(lo) + log10(hi);
|
---|
572 |
|
---|
573 | return pow(10, val/2);
|
---|
574 | }
|
---|
575 |
|
---|
576 | // --------------------------------------------------------------------------
|
---|
577 | //
|
---|
578 | // Draws a copy of the two given histograms. Uses title as the pad title.
|
---|
579 | // Also layout the two statistic boxes and a legend.
|
---|
580 | //
|
---|
581 | void MH::DrawCopy(const TH1 &hist1, const TH1 &hist2, const TString title)
|
---|
582 | {
|
---|
583 | //
|
---|
584 | // Draw first histogram
|
---|
585 | //
|
---|
586 | TH1 *h1 = (TH1*)((TH1&)hist1).DrawCopy();
|
---|
587 | gPad->Update();
|
---|
588 |
|
---|
589 | TPaveText *t = (TPaveText*)gPad->FindObject("title");
|
---|
590 | if (t)
|
---|
591 | {
|
---|
592 | t->SetName((TString)"MHTitle"); // rename object
|
---|
593 | t->Clear(); // clear old lines
|
---|
594 | t->AddText((TString)" "+title+" "); // add the new title
|
---|
595 | t->SetBit(kCanDelete); // make sure object is deleted
|
---|
596 |
|
---|
597 | //
|
---|
598 | // FIXME: This is a stupid workaround to hide the redrawn
|
---|
599 | // (see THistPainter::PaintTitle) title
|
---|
600 | //
|
---|
601 | gPad->Modified(); // indicates a change
|
---|
602 | gPad->Update(); // recreates the original title
|
---|
603 | t->Pop(); // bring our title on top
|
---|
604 | }
|
---|
605 |
|
---|
606 | //
|
---|
607 | // Rename first statistics box
|
---|
608 | //
|
---|
609 | TPaveStats &s1 = *(TPaveStats*)gPad->FindObject("stats");
|
---|
610 | s1.SetX1NDC(s1.GetX1NDC()-0.01);
|
---|
611 | s1.SetName("MHStat");
|
---|
612 |
|
---|
613 | //
|
---|
614 | // Draw second histogram
|
---|
615 | //
|
---|
616 | TH1 *h2 = (TH1*)((TH1&)hist2).DrawCopy("sames");
|
---|
617 | gPad->Update();
|
---|
618 |
|
---|
619 | //
|
---|
620 | // Set new position of second statistics box
|
---|
621 | //
|
---|
622 | TPaveStats &s2 = *(TPaveStats*)gPad->FindObject("stats");
|
---|
623 | s2.SetX1NDC(s1.GetX1NDC()-(s2.GetX2NDC()-s2.GetX1NDC())-0.01);
|
---|
624 | s2.SetX2NDC(s1.GetX1NDC()-0.01);
|
---|
625 |
|
---|
626 | //
|
---|
627 | // Draw Legend
|
---|
628 | //
|
---|
629 | const Int_t n = s1.GetListOfLines()->GetSize();
|
---|
630 | const Double_t h = s1.GetY2NDC()-s1.GetY1NDC();
|
---|
631 | TLegend &l = *new TLegend(s1.GetX1NDC(), s1.GetY1NDC()-0.015-h*2/n,
|
---|
632 | s1.GetX2NDC(), s1.GetY1NDC()-0.01
|
---|
633 | );
|
---|
634 | l.AddEntry(h1, h1->GetTitle());
|
---|
635 | l.AddEntry(h2, h2->GetTitle());
|
---|
636 | l.SetTextSize(s2.GetTextSize());
|
---|
637 | l.SetTextFont(s2.GetTextFont());
|
---|
638 | l.SetBorderSize(s2.GetBorderSize());
|
---|
639 | l.SetBit(kCanDelete);
|
---|
640 |
|
---|
641 | l.Draw();
|
---|
642 | }
|
---|
643 |
|
---|
644 | // --------------------------------------------------------------------------
|
---|
645 | //
|
---|
646 | // Draws the two given histograms. Uses title as the pad title.
|
---|
647 | // Also layout the two statistic boxes and a legend.
|
---|
648 | //
|
---|
649 | void MH::Draw(TH1 &hist1, TH1 &hist2, const TString title)
|
---|
650 | {
|
---|
651 | //
|
---|
652 | // Draw first histogram
|
---|
653 | //
|
---|
654 | hist1.Draw();
|
---|
655 | gPad->Update();
|
---|
656 |
|
---|
657 | TPaveText *t = (TPaveText*)gPad->FindObject("title");
|
---|
658 | if (t)
|
---|
659 | {
|
---|
660 | t->SetName((TString)"MHTitle"); // rename object
|
---|
661 | t->Clear(); // clear old lines
|
---|
662 | t->AddText((TString)" "+title+" "); // add the new title
|
---|
663 | t->SetBit(kCanDelete); // make sure object is deleted
|
---|
664 |
|
---|
665 | //
|
---|
666 | // FIXME: This is a stupid workaround to hide the redrawn
|
---|
667 | // (see THistPainter::PaintTitle) title
|
---|
668 | //
|
---|
669 | gPad->Modified(); // indicates a change
|
---|
670 | gPad->Update(); // recreates the original title
|
---|
671 | t->Pop(); // bring our title on top
|
---|
672 | }
|
---|
673 |
|
---|
674 | //
|
---|
675 | // Rename first statistics box
|
---|
676 | //
|
---|
677 | TPaveStats &s1 = *(TPaveStats*)gPad->FindObject("stats");
|
---|
678 | s1.SetX1NDC(s1.GetX1NDC()-0.01);
|
---|
679 | s1.SetName((TString)"Stat"+hist1.GetTitle());
|
---|
680 |
|
---|
681 | //
|
---|
682 | // Draw second histogram
|
---|
683 | //
|
---|
684 | hist2.Draw("sames");
|
---|
685 |
|
---|
686 | gPad->Update();
|
---|
687 |
|
---|
688 | //
|
---|
689 | // Set new position of second statistics box
|
---|
690 | //
|
---|
691 | TPaveStats &s2 = *(TPaveStats*)gPad->FindObject("stats");
|
---|
692 | s2.SetX1NDC(s1.GetX1NDC()-(s2.GetX2NDC()-s2.GetX1NDC())-0.01);
|
---|
693 | s2.SetX2NDC(s1.GetX1NDC()-0.01);
|
---|
694 |
|
---|
695 | //
|
---|
696 | // Draw Legend
|
---|
697 | //
|
---|
698 | TLegend &l = *new TLegend(s1.GetX1NDC(),
|
---|
699 | s1.GetY1NDC()-0.015-(s1.GetY2NDC()-s1.GetY1NDC())/2,
|
---|
700 | s1.GetX2NDC(),
|
---|
701 | s1.GetY1NDC()-0.01
|
---|
702 | );
|
---|
703 | l.AddEntry(&hist1, hist1.GetTitle());
|
---|
704 | l.AddEntry(&hist2, hist2.GetTitle());
|
---|
705 | l.SetTextSize(s2.GetTextSize());
|
---|
706 | l.SetTextFont(s2.GetTextFont());
|
---|
707 | l.SetBorderSize(s2.GetBorderSize());
|
---|
708 | l.SetBit(kCanDelete);
|
---|
709 | l.Draw();
|
---|
710 | }
|
---|
711 |
|
---|