source: trunk/MagicSoft/Mars/mhist/MBinning.cc@ 2166

Last change on this file since 2166 was 2117, checked in by tbretz, 22 years ago
*** empty log message ***
File size: 6.0 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, 01/2002 <mailto:tbretz@astro.uni-wuerzburg.de>
19!
20! Copyright: MAGIC Software Development, 2000-2003
21!
22!
23\* ======================================================================== */
24
25//////////////////////////////////////////////////////////////////////////////
26// //
27// MBinning //
28// //
29//////////////////////////////////////////////////////////////////////////////
30#include "MBinning.h"
31
32#include <ctype.h> // tolower
33#include <fstream.h>
34
35#include <TH1.h> // InheritsFrom
36
37#include "MLog.h"
38#include "MLogManip.h"
39
40#include "MH.h"
41
42ClassImp(MBinning);
43
44static const TString gsDefName = "MBinning";
45static const TString gsDefTitle = "Container describing the binning of an axis";
46
47// --------------------------------------------------------------------------
48//
49// Default Constructor. It sets name and title only. Typically you won't
50// need to change this.
51//
52MBinning::MBinning(const char *name, const char *title)
53{
54 //
55 // set the name and title of this object
56 //
57 fName = name ? name : gsDefName.Data();
58 fTitle = title ? title : gsDefTitle.Data();
59
60 SetEdges(10, 0, 1);
61
62 fType = kIsDefault;
63}
64
65void MBinning::SetEdges(const TAxis &axe)
66{
67 const TArrayD &arr = *((TAxis&)axe).GetXbins();
68 if (arr.GetSize()>0)
69 {
70 SetEdges(arr);
71 return;
72 }
73
74 SetEdges(axe.GetNbins(), axe.GetXmin(), axe.GetXmax());
75}
76
77void MBinning::SetEdges(const TH1 &h, const Char_t axis='x')
78{
79 TH1 &hist = (TH1&)h; // get rid of const qualifier
80 switch (tolower(axis))
81 {
82 case 'x':
83 SetEdges(*hist.GetXaxis());
84 return;
85 case 'y':
86 SetEdges(*hist.GetYaxis());
87 return;
88 case 'z':
89 SetEdges(*hist.GetZaxis());
90 return;
91 default:
92 *fLog << warn << "MBinning::SetEdges: Axis '" << axis << "' unknown... using x." << endl;
93 SetEdges(*hist.GetXaxis());
94 }
95}
96
97// --------------------------------------------------------------------------
98//
99// Specify the number of bins <nbins> (not the number of edges), the
100// lowest <lo> and highest <up> Edge (of your histogram)
101//
102void MBinning::SetEdges(const Int_t nbins, const Axis_t lo, Axis_t up)
103{
104 const Double_t binsize = (up-lo)/nbins;
105 fEdges.Set(nbins+1);
106 for (int i=0; i<=nbins; i++)
107 fEdges[i] = binsize*i + lo;
108
109 fType = kIsLinear;
110}
111
112// --------------------------------------------------------------------------
113//
114// Specify the number of bins <nbins> (not the number of edges), the
115// lowest <lo> and highest <up> Edge (of your histogram)
116//
117void MBinning::SetEdgesLog(const Int_t nbins, const Axis_t lo, Axis_t up)
118{
119 // if (lo==0) ...
120
121 const Double_t binsize = log10(up/lo)/nbins;
122 fEdges.Set(nbins+1);
123 for (int i=0; i<=nbins; i++)
124 fEdges[i] = pow(10, binsize*i) * lo;
125
126 fType = kIsLogarithmic;
127}
128
129// --------------------------------------------------------------------------
130//
131// Specify the number of bins <nbins> (not the number of edges), the
132// lowest [deg] <lo> and highest [deg] <up> Edge (of your histogram)
133//
134void MBinning::SetEdgesCos(const Int_t nbins, const Axis_t lo, Axis_t up)
135{
136 // if (lo==0) ...
137 const Axis_t ld = lo/kRad2Deg;
138 const Axis_t ud = up/kRad2Deg;
139
140 const Double_t binsize = (cos(ld)-cos(ud))/nbins;
141 fEdges.Set(nbins+1);
142 for (int i=0; i<=nbins; i++)
143 fEdges[i] = acos(cos(ld)-binsize*i)*kRad2Deg;
144
145 fType = kIsCosinic;
146}
147
148// --------------------------------------------------------------------------
149//
150// Apply this binning to the given histogram.
151// (By definition this works only for 1D-histograms. For 2D- and 3D-
152// histograms use MH::SetBinning directly)
153//
154void MBinning::Apply(TH1 &h)
155{
156 if (h.InheritsFrom("TH2") || h.InheritsFrom("TH3"))
157 {
158 *fLog << warn << "MBinning::Apply: '" << h.GetName() << "' is not a basic TH1 object... no binning applied." << endl;
159 return;
160 }
161
162 MH::SetBinning(&h, this);
163}
164
165// --------------------------------------------------------------------------
166//
167// Implementation of SavePrimitive. Used to write the call to a constructor
168// to a macro. In the original root implementation it is used to write
169// gui elements to a macro-file.
170//
171void MBinning::StreamPrimitive(ofstream &out) const
172{
173 out << " MBinning " << GetUniqueName();
174 if (fName!=gsDefName || fTitle!=gsDefTitle)
175 {
176 out << "(\"" << fName << "\"";
177 if (fTitle!=gsDefTitle)
178 out << ", \"" << fTitle << "\"";
179 out <<")";
180 }
181 out << ";" << endl;
182
183 if (IsDefault())
184 return;
185
186 if (IsLinear() || IsLogarithmic() || IsCosinic())
187 {
188 out << " " << GetUniqueName() << ".SetEdges";
189 if (IsLogarithmic())
190 out << "Log";
191 if (IsCosinic())
192 out << "Cos";
193 out << "(" << GetNumBins() << ", " << GetEdgeLo() << ", " << GetEdgeHi() << ");" << endl;
194 return;
195 }
196
197 out << " {" << endl;
198 out << " TArrayD dummy;" << endl;
199 for (int i=0; i<GetNumEdges(); i++)
200 out << " dummy[" << i << "]=" << GetEdges()[i] << ";" << endl;
201 out << " " << GetUniqueName() << ".SetEdges(dummy);" << endl;
202 out << " }" << endl;
203}
Note: See TracBrowser for help on using the repository browser.