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

Last change on this file since 4921 was 4920, checked in by tbretz, 20 years ago
*** empty log message ***
File size: 7.2 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
46const TString MBinning::gsDefName = "MBinning";
47const TString MBinning::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}
74
75void MBinning::SetEdges(const TAxis &axe)
76{
77 const TArrayD &arr = *axe.GetXbins();
78 if (arr.GetSize()>0)
79 {
80 SetEdges(arr);
81 return;
82 }
83
84 SetEdges(axe.GetNbins(), axe.GetXmin(), axe.GetXmax());
85}
86
87void MBinning::AddEdge(Axis_t up)
88{
89 const Int_t n = fEdges.GetSize();
90
91 if (up<=fEdges[n-1])
92 {
93 *fLog << warn << dbginf << "WARNING - New upper edge not greater than old upper edge... ignored." << endl;
94 return;
95 }
96
97 fEdges.Set(n+1);
98 fEdges[n] = up;
99
100 fType = kIsUserArray;
101}
102
103void MBinning::SetEdges(const TH1 &h, const Char_t axis)
104{
105 TH1 &hist = (TH1&)h; // get rid of const qualifier
106 switch (tolower(axis))
107 {
108 case 'x':
109 SetEdges(*hist.GetXaxis());
110 return;
111 case 'y':
112 SetEdges(*hist.GetYaxis());
113 return;
114 case 'z':
115 SetEdges(*hist.GetZaxis());
116 return;
117 default:
118 *fLog << warn << "MBinning::SetEdges: Axis '" << axis << "' unknown... using x." << endl;
119 SetEdges(*hist.GetXaxis());
120 }
121}
122
123// --------------------------------------------------------------------------
124//
125// Specify the number of bins <nbins> (not the number of edges), the
126// lowest <lo> and highest <up> Edge (of your histogram)
127//
128void MBinning::SetEdges(const Int_t nbins, const Axis_t lo, Axis_t up)
129{
130 const Double_t binsize = nbins<=0 ? 0 : (up-lo)/nbins;
131 fEdges.Set(nbins+1);
132 for (int i=0; i<=nbins; i++)
133 fEdges[i] = binsize*i + lo;
134
135 fType = kIsLinear;
136}
137
138// --------------------------------------------------------------------------
139//
140// Calls SetEdgesLog if opt contains "log"
141// Calls SetEdgesCos if opt contains "cos"
142// Calls SetEdges in all other cases
143//
144void MBinning::SetEdges(const Int_t nbins, const Axis_t lo, Axis_t up, const char *opt)
145{
146 const TString o(opt);
147 if (o.Contains("log", TString::kIgnoreCase))
148 {
149 SetEdgesLog(nbins, lo, up);
150 return;
151 }
152 if (o.Contains("cos", TString::kIgnoreCase))
153 {
154 SetEdgesCos(nbins, lo, up);
155 return;
156 }
157 SetEdges(nbins, lo, up);
158}
159
160// --------------------------------------------------------------------------
161//
162// Specify the number of bins <nbins> (not the number of edges), the
163// lowest <lo> and highest <up> Edge (of your histogram)
164//
165void MBinning::SetEdgesLog(const Int_t nbins, const Axis_t lo, Axis_t up)
166{
167 // if (lo==0) ...
168
169 const Double_t binsize = log10(up/lo)/nbins;
170 fEdges.Set(nbins+1);
171 for (int i=0; i<=nbins; i++)
172 fEdges[i] = pow(10, binsize*i) * lo;
173
174 fType = kIsLogarithmic;
175}
176
177// --------------------------------------------------------------------------
178//
179// Specify the number of bins <nbins> (not the number of edges), the
180// lowest [deg] <lo> and highest [deg] <up> Edge (of your histogram)
181//
182void MBinning::SetEdgesCos(const Int_t nbins, const Axis_t lo, Axis_t up)
183{
184 // if (lo==0) ...
185 const Axis_t ld = lo/kRad2Deg;
186 const Axis_t ud = up/kRad2Deg;
187
188 const Double_t binsize = (cos(ld)-cos(ud))/nbins;
189 fEdges.Set(nbins+1);
190 for (int i=0; i<=nbins; i++)
191 fEdges[i] = acos(cos(ld)-binsize*i)*kRad2Deg;
192
193 fType = kIsCosinic;
194}
195
196// --------------------------------------------------------------------------
197//
198// Apply this binning to the given histogram.
199// (By definition this works only for 1D-histograms. For 2D- and 3D-
200// histograms use MH::SetBinning directly)
201//
202void MBinning::Apply(TH1 &h) const
203{
204 if (h.InheritsFrom("TH2") || h.InheritsFrom("TH3"))
205 {
206 *fLog << warn << "MBinning::Apply: '" << h.GetName() << "' is not a basic TH1 object... no binning applied." << endl;
207 return;
208 }
209
210 MH::SetBinning(&h, this);
211}
212
213// --------------------------------------------------------------------------
214//
215// Implementation of SavePrimitive. Used to write the call to a constructor
216// to a macro. In the original root implementation it is used to write
217// gui elements to a macro-file.
218//
219void MBinning::StreamPrimitive(ofstream &out) const
220{
221 out << " MBinning " << GetUniqueName();
222 if (fName!=gsDefName || fTitle!=gsDefTitle)
223 {
224 out << "(\"" << fName << "\"";
225 if (fTitle!=gsDefTitle)
226 out << ", \"" << fTitle << "\"";
227 out <<")";
228 }
229 out << ";" << endl;
230
231 if (IsDefault())
232 return;
233
234 if (IsLinear() || IsLogarithmic() || IsCosinic())
235 {
236 out << " " << GetUniqueName() << ".SetEdges";
237 if (IsLogarithmic())
238 out << "Log";
239 if (IsCosinic())
240 out << "Cos";
241 out << "(" << GetNumBins() << ", " << GetEdgeLo() << ", " << GetEdgeHi() << ");" << endl;
242 return;
243 }
244
245 out << " {" << endl;
246 out << " TArrayD dummy;" << endl;
247 for (int i=0; i<GetNumEdges(); i++)
248 out << " dummy[" << i << "]=" << GetEdges()[i] << ";" << endl;
249 out << " " << GetUniqueName() << ".SetEdges(dummy);" << endl;
250 out << " }" << endl;
251}
Note: See TracBrowser for help on using the repository browser.