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

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