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

Last change on this file since 1522 was 1487, checked in by tbretz, 22 years ago
*** empty log message ***
File size: 4.6 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 (not the number of edges), the lowest and
67// highest Edge.
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 (not the number of edges), the lowest and
82// highest Edge.
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// Apply this binning to the given histogram.
99// (By definition this works only for 1D-histograms. For 2D- and 3D-
100// histograms use MH::SetBinning directly)
101//
102void MBinning::Apply(TH1 &h)
103{
104 if (h.InheritsFrom("TH2") || h.InheritsFrom("TH3"))
105 {
106 *fLog << warn << "MBinning::Apply: '" << h.GetName() << "' is not a basic TH1 object... no binning applied." << endl;
107 return;
108 }
109
110 MH::SetBinning(&h, this);
111}
112
113// --------------------------------------------------------------------------
114//
115// Implementation of SavePrimitive. Used to write the call to a constructor
116// to a macro. In the original root implementation it is used to write
117// gui elements to a macro-file.
118//
119void MBinning::StreamPrimitive(ofstream &out) const
120{
121 out << " MBinning " << GetUniqueName();
122 if (fName!=gsDefName || fTitle!=gsDefTitle)
123 {
124 out << "(\"" << fName << "\"";
125 if (fTitle!=gsDefTitle)
126 out << ", \"" << fTitle << "\"";
127 out <<")";
128 }
129 out << ";" << endl;
130
131 if (IsDefault())
132 return;
133
134 if (IsLinear() || IsLogarithmic())
135 {
136 out << " " << GetUniqueName() << ".SetEdges";
137 if (IsLogarithmic())
138 out << "Log";
139 out << "(" << GetNumBins() << ", " << GetEdgeLo() << ", " << GetEdgeHi() << ");" << endl;
140 return;
141 }
142
143 out << " {" << endl;
144 out << " TArrayD dummy;" << endl;
145 for (int i=0; i<GetNumEdges(); i++)
146 out << " dummy[" << i << "]=" << GetEdges()[i] << ";" << endl;
147 out << " " << GetUniqueName() << ".SetEdges(dummy);" << endl;
148 out << " }" << endl;
149}
Note: See TracBrowser for help on using the repository browser.