source: tags/Mars-V0.8.6/mhbase/MBinning.cc

Last change on this file was 4966, checked in by tbretz, 20 years ago
*** empty log message ***
File size: 8.4 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-2004
21!
22!
23\* ======================================================================== */
24
25//////////////////////////////////////////////////////////////////////////////
26//
27// MBinning
28//
29// This is a MParCOntainer storing a binning for a histogram. Doing this
30// you are able to distribute a single binning to several histograms
31// in your parameter list.
32//
33// In some classes the title of the container is used to set the
34// axis-title of the corresponding axis in your histogram.
35//
36// For all the features supported see the function descriptions in
37// MBinning and MH
38//
39//////////////////////////////////////////////////////////////////////////////
40#include "MBinning.h"
41
42#include <ctype.h> // tolower
43#include <fstream>
44
45#include <TH1.h> // InheritsFrom
46
47#include "MLog.h"
48#include "MLogManip.h"
49
50#include "MH.h"
51
52ClassImp(MBinning);
53
54using namespace std;
55
56const TString MBinning::gsDefName = "MBinning";
57const TString MBinning::gsDefTitle = "Container describing the binning of an axis";
58
59// --------------------------------------------------------------------------
60//
61// Default Constructor. It sets name and title only. Typically you won't
62// need to change this.
63//
64MBinning::MBinning(const char *name, const char *title)
65{
66 //
67 // set the name and title of this object
68 //
69 fName = name ? name : gsDefName.Data();
70 fTitle = title ? title : gsDefTitle.Data();
71
72 SetEdges(10, 0, 1);
73 fType = kIsDefault;
74}
75
76// --------------------------------------------------------------------------
77//
78// Instantiate MBinning with nbins number of bins between lo (lower edge)
79// and hi (upper edge), name name and title title.
80//
81MBinning::MBinning(Int_t nbins, Axis_t lo, Axis_t hi, const char *name, const char *opt, const char *title)
82{
83 fName = name ? name : gsDefName.Data();
84 fTitle = title ? title : gsDefTitle.Data();
85
86 SetEdges(nbins, lo, hi, opt);
87}
88
89// --------------------------------------------------------------------------
90//
91// Setup the edges stored in MBinning from the TAxis axe
92//
93void MBinning::SetEdges(const TAxis &axe)
94{
95 const TArrayD &arr = *axe.GetXbins();
96 if (arr.GetSize()>0)
97 {
98 SetEdges(arr);
99 return;
100 }
101
102 SetEdges(axe.GetNbins(), axe.GetXmin(), axe.GetXmax());
103}
104
105// --------------------------------------------------------------------------
106//
107// Add a new upper edge to the edges stored in MBinning. The new upper
108// edge must be greater than the current greatest. Using this you can
109// enhance a histogram bin-by-bin, eg:
110// TH1F h("", "", 2, 0, 1);
111// MBinning b;
112// b.SetEdges(h);
113// b.AddEdge(2);
114// b.Apply(h);
115// b.AddEdge(3);
116// b.Apply(h);
117// [...]
118//
119void MBinning::AddEdge(Axis_t up)
120{
121 const Int_t n = fEdges.GetSize();
122
123 if (up<=fEdges[n-1])
124 {
125 *fLog << warn << dbginf << "WARNING - New upper edge not greater than old upper edge... ignored." << endl;
126 return;
127 }
128
129 fEdges.Set(n+1);
130 fEdges[n] = up;
131
132 fType = kIsUserArray;
133}
134
135void MBinning::RemoveFirstEdge()
136{
137 const Int_t n = fEdges.GetSize();
138 for (int i=0; i<n-1; i++)
139 fEdges[i] = fEdges[i+1];
140 fEdges.Set(n-1);
141}
142
143void MBinning::RemoveLastEdge()
144{
145 fEdges.Set(fEdges.GetSize()-1);
146}
147
148// --------------------------------------------------------------------------
149//
150// Set the edges in MBinning from a histogram-axis. Which axis is
151// specified by axis ('x', 'y', 'z')
152//
153void MBinning::SetEdges(const TH1 &h, const Char_t axis)
154{
155 switch (tolower(axis))
156 {
157 case 'x':
158 SetEdges(*h.GetXaxis());
159 return;
160 case 'y':
161 SetEdges(*h.GetYaxis());
162 return;
163 case 'z':
164 SetEdges(*h.GetZaxis());
165 return;
166 default:
167 *fLog << warn << "MBinning::SetEdges: Axis '" << axis << "' unknown... using x." << endl;
168 SetEdges(*h.GetXaxis());
169 }
170}
171
172// --------------------------------------------------------------------------
173//
174// Specify the number of bins <nbins> (not the number of edges), the
175// lowest <lo> and highest <up> Edge (of your histogram)
176//
177void MBinning::SetEdges(const Int_t nbins, const Axis_t lo, Axis_t up)
178{
179 const Double_t binsize = nbins<=0 ? 0 : (up-lo)/nbins;
180 fEdges.Set(nbins+1);
181 for (int i=0; i<=nbins; i++)
182 fEdges[i] = binsize*i + lo;
183
184 fType = kIsLinear;
185}
186
187// --------------------------------------------------------------------------
188//
189// Calls SetEdgesLog if opt contains "log"
190// Calls SetEdgesCos if opt contains "cos"
191// Calls SetEdges in all other cases
192//
193void MBinning::SetEdges(const Int_t nbins, const Axis_t lo, Axis_t up, const char *opt)
194{
195 const TString o(opt);
196 if (o.Contains("log", TString::kIgnoreCase))
197 {
198 SetEdgesLog(nbins, lo, up);
199 return;
200 }
201 if (o.Contains("cos", TString::kIgnoreCase))
202 {
203 SetEdgesCos(nbins, lo, up);
204 return;
205 }
206 SetEdges(nbins, lo, up);
207}
208
209// --------------------------------------------------------------------------
210//
211// Specify the number of bins <nbins> (not the number of edges), the
212// lowest <lo> and highest <up> Edge (of your histogram)
213//
214void MBinning::SetEdgesLog(const Int_t nbins, const Axis_t lo, Axis_t up)
215{
216 // if (lo==0) ...
217
218 const Double_t binsize = log10(up/lo)/nbins;
219 fEdges.Set(nbins+1);
220 for (int i=0; i<=nbins; i++)
221 fEdges[i] = pow(10, binsize*i) * lo;
222
223 fType = kIsLogarithmic;
224}
225
226// --------------------------------------------------------------------------
227//
228// Specify the number of bins <nbins> (not the number of edges), the
229// lowest [deg] <lo> and highest [deg] <up> Edge (of your histogram)
230//
231void MBinning::SetEdgesCos(const Int_t nbins, const Axis_t lo, Axis_t up)
232{
233 // if (lo==0) ...
234 const Axis_t ld = lo/kRad2Deg;
235 const Axis_t ud = up/kRad2Deg;
236
237 const Double_t binsize = (cos(ld)-cos(ud))/nbins;
238 fEdges.Set(nbins+1);
239 for (int i=0; i<=nbins; i++)
240 fEdges[i] = acos(cos(ld)-binsize*i)*kRad2Deg;
241
242 fType = kIsCosinic;
243}
244
245// --------------------------------------------------------------------------
246//
247// Apply this binning to the given histogram.
248// (By definition this works only for 1D-histograms. For 2D- and 3D-
249// histograms use MH::SetBinning directly)
250//
251void MBinning::Apply(TH1 &h) const
252{
253 if (h.InheritsFrom("TH2") || h.InheritsFrom("TH3"))
254 {
255 *fLog << warn << "MBinning::Apply: '" << h.GetName() << "' is not a basic TH1 object... no binning applied." << endl;
256 return;
257 }
258
259 MH::SetBinning(&h, this);
260}
261
262// --------------------------------------------------------------------------
263//
264// Implementation of SavePrimitive. Used to write the call to a constructor
265// to a macro. In the original root implementation it is used to write
266// gui elements to a macro-file.
267//
268void MBinning::StreamPrimitive(ofstream &out) const
269{
270 out << " MBinning " << GetUniqueName();
271 if (fName!=gsDefName || fTitle!=gsDefTitle)
272 {
273 out << "(\"" << fName << "\"";
274 if (fTitle!=gsDefTitle)
275 out << ", \"" << fTitle << "\"";
276 out <<")";
277 }
278 out << ";" << endl;
279
280 if (IsDefault())
281 return;
282
283 if (IsLinear() || IsLogarithmic() || IsCosinic())
284 {
285 out << " " << GetUniqueName() << ".SetEdges";
286 if (IsLogarithmic())
287 out << "Log";
288 if (IsCosinic())
289 out << "Cos";
290 out << "(" << GetNumBins() << ", " << GetEdgeLo() << ", " << GetEdgeHi() << ");" << endl;
291 return;
292 }
293
294 out << " {" << endl;
295 out << " TArrayD dummy;" << endl;
296 for (int i=0; i<GetNumEdges(); i++)
297 out << " dummy[" << i << "]=" << GetEdges()[i] << ";" << endl;
298 out << " " << GetUniqueName() << ".SetEdges(dummy);" << endl;
299 out << " }" << endl;
300}
Note: See TracBrowser for help on using the repository browser.