source: trunk/MagicSoft/Mars/mhbase/MBinning.cc@ 4698

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