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

Last change on this file since 2929 was 2735, checked in by tbretz, 21 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>
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
64 fType = kIsDefault;
65}
66
67void MBinning::SetEdges(const TAxis &axe)
68{
69 const TArrayD &arr = *((TAxis&)axe).GetXbins();
70 if (arr.GetSize()>0)
71 {
72 SetEdges(arr);
73 return;
74 }
75
76 SetEdges(axe.GetNbins(), axe.GetXmin(), axe.GetXmax());
77}
78
79void MBinning::SetEdges(const TH1 &h, const Char_t axis)
80{
81 TH1 &hist = (TH1&)h; // get rid of const qualifier
82 switch (tolower(axis))
83 {
84 case 'x':
85 SetEdges(*hist.GetXaxis());
86 return;
87 case 'y':
88 SetEdges(*hist.GetYaxis());
89 return;
90 case 'z':
91 SetEdges(*hist.GetZaxis());
92 return;
93 default:
94 *fLog << warn << "MBinning::SetEdges: Axis '" << axis << "' unknown... using x." << endl;
95 SetEdges(*hist.GetXaxis());
96 }
97}
98
99// --------------------------------------------------------------------------
100//
101// Specify the number of bins <nbins> (not the number of edges), the
102// lowest <lo> and highest <up> Edge (of your histogram)
103//
104void MBinning::SetEdges(const Int_t nbins, const Axis_t lo, Axis_t up)
105{
106 const Double_t binsize = (up-lo)/nbins;
107 fEdges.Set(nbins+1);
108 for (int i=0; i<=nbins; i++)
109 fEdges[i] = binsize*i + lo;
110
111 fType = kIsLinear;
112}
113
114// --------------------------------------------------------------------------
115//
116// Specify the number of bins <nbins> (not the number of edges), the
117// lowest <lo> and highest <up> Edge (of your histogram)
118//
119void MBinning::SetEdgesLog(const Int_t nbins, const Axis_t lo, Axis_t up)
120{
121 // if (lo==0) ...
122
123 const Double_t binsize = log10(up/lo)/nbins;
124 fEdges.Set(nbins+1);
125 for (int i=0; i<=nbins; i++)
126 fEdges[i] = pow(10, binsize*i) * lo;
127
128 fType = kIsLogarithmic;
129}
130
131// --------------------------------------------------------------------------
132//
133// Specify the number of bins <nbins> (not the number of edges), the
134// lowest [deg] <lo> and highest [deg] <up> Edge (of your histogram)
135//
136void MBinning::SetEdgesCos(const Int_t nbins, const Axis_t lo, Axis_t up)
137{
138 // if (lo==0) ...
139 const Axis_t ld = lo/kRad2Deg;
140 const Axis_t ud = up/kRad2Deg;
141
142 const Double_t binsize = (cos(ld)-cos(ud))/nbins;
143 fEdges.Set(nbins+1);
144 for (int i=0; i<=nbins; i++)
145 fEdges[i] = acos(cos(ld)-binsize*i)*kRad2Deg;
146
147 fType = kIsCosinic;
148}
149
150// --------------------------------------------------------------------------
151//
152// Apply this binning to the given histogram.
153// (By definition this works only for 1D-histograms. For 2D- and 3D-
154// histograms use MH::SetBinning directly)
155//
156void MBinning::Apply(TH1 &h)
157{
158 if (h.InheritsFrom("TH2") || h.InheritsFrom("TH3"))
159 {
160 *fLog << warn << "MBinning::Apply: '" << h.GetName() << "' is not a basic TH1 object... no binning applied." << endl;
161 return;
162 }
163
164 MH::SetBinning(&h, this);
165}
166
167// --------------------------------------------------------------------------
168//
169// Implementation of SavePrimitive. Used to write the call to a constructor
170// to a macro. In the original root implementation it is used to write
171// gui elements to a macro-file.
172//
173void MBinning::StreamPrimitive(ofstream &out) const
174{
175 out << " MBinning " << GetUniqueName();
176 if (fName!=gsDefName || fTitle!=gsDefTitle)
177 {
178 out << "(\"" << fName << "\"";
179 if (fTitle!=gsDefTitle)
180 out << ", \"" << fTitle << "\"";
181 out <<")";
182 }
183 out << ";" << endl;
184
185 if (IsDefault())
186 return;
187
188 if (IsLinear() || IsLogarithmic() || IsCosinic())
189 {
190 out << " " << GetUniqueName() << ".SetEdges";
191 if (IsLogarithmic())
192 out << "Log";
193 if (IsCosinic())
194 out << "Cos";
195 out << "(" << GetNumBins() << ", " << GetEdgeLo() << ", " << GetEdgeHi() << ");" << endl;
196 return;
197 }
198
199 out << " {" << endl;
200 out << " TArrayD dummy;" << endl;
201 for (int i=0; i<GetNumEdges(); i++)
202 out << " dummy[" << i << "]=" << GetEdges()[i] << ";" << endl;
203 out << " " << GetUniqueName() << ".SetEdges(dummy);" << endl;
204 out << " }" << endl;
205}
Note: See TracBrowser for help on using the repository browser.