]> git.uio.no Git - u/mrichter/AliRoot.git/blob - PWGLF/SPECTRA/Nuclei/B2/AliLnHistoMap.cxx
Add linear to logarithmic bins
[u/mrichter/AliRoot.git] / PWGLF / SPECTRA / Nuclei / B2 / AliLnHistoMap.cxx
1 /**************************************************************************
2  * Copyright(c) 1998-1999, ALICE Experiment at CERN, All rights reserved. *
3  *                                                                        *
4  * Author: The ALICE Off-line Project.                                    *
5  * Contributors are mentioned in the code where appropriate.              *
6  *                                                                        *
7  * Permission to use, copy, modify and distribute this software and its   *
8  * documentation strictly for non-commercial purposes is hereby granted   *
9  * without fee, provided that the above copyright notice appears in all   *
10  * copies and that both the copyright notice and this permission notice   *
11  * appear in the supporting documentation. The authors make no claims     *
12  * about the suitability of this software for any purpose. It is          *
13  * provided "as is" without express or implied warranty.                  *
14  **************************************************************************/
15
16 // class for handling histograms
17 // author: Eulogio Serradilla <eulogio.serradilla@cern.ch>
18
19 #include <Riostream.h>
20 #include <TString.h>
21 #include <TMap.h>
22 #include <TH1.h>
23 #include <TAxis.h>
24 #include <TH1D.h>
25 #include <TH2D.h>
26 #include <TObjString.h>
27 #include <TMath.h>
28 #include "AliLnHistoMap.h"
29
30 ClassImp(AliLnHistoMap)
31
32 AliLnHistoMap::AliLnHistoMap()
33 : TObject()
34 , fHistoMap(0)
35 {
36 //
37 // Default constructor
38 //
39         fHistoMap = new TMap();
40         fHistoMap->SetOwner(kTRUE);
41 }
42
43 AliLnHistoMap::~AliLnHistoMap()
44 {
45 //
46 // Default destructor
47 //
48         delete fHistoMap;
49         
50 }
51
52 Int_t AliLnHistoMap::Write(const char *name, Int_t option, Int_t bsize) const
53 {
54 //
55 // write only the histograms
56 //
57         Int_t nbytes = 0;
58         
59         TObjString* key;
60         TIter hIter(fHistoMap);
61         while( (key = (TObjString*)hIter.Next()) )
62         {
63                 if(fHistoMap->GetValue(key)->InheritsFrom("TH1"))
64                 {
65                         nbytes += ((TH1*)fHistoMap->GetValue(key))->Write(name,option,bsize);
66                 }
67         }
68         
69         return nbytes;
70 }
71
72 Int_t AliLnHistoMap::Write(const char *name, Int_t option, Int_t bsize)
73 {
74 // write only the histograms
75 //
76         return ((const AliLnHistoMap*)this)->Write(name,option,bsize);
77 }
78
79 TObject* AliLnHistoMap::Add(const TString& keyname, TObject* value)
80 {
81 //
82 // Add a TObject to the histogram map
83 //
84         if(fHistoMap->Contains(keyname.Data()))
85         {
86                 fHistoMap->Warning("Add", "object %s already exists", keyname.Data());
87                 return 0;
88         }
89         
90         TObjString* key = new TObjString(keyname.Data());
91         
92         fHistoMap->Add((TObject*)key, value);
93         return value;
94 }
95
96 TH1D* AliLnHistoMap::Add(const TString& name, Int_t nbins, Double_t xmin, Double_t xmax, const TString& title, const TString& xlabel, const TString& ylabel, Bool_t logx)
97 {
98 //
99 // Add a TH1D histogram to the histogram map
100 //
101         TH1D* value = 0;
102         if(fHistoMap->Contains(name.Data()))
103         {
104                 fHistoMap->Warning("Add", "histogram %s already exists", name.Data());
105         }
106         else
107         {
108                 TObjString* key = new TObjString(name.Data());
109                 value = new TH1D(name.Data(),title.Data(),nbins,xmin,xmax);
110                 value->SetXTitle(xlabel.Data());
111                 value->SetYTitle(ylabel.Data());
112                 
113                 if(logx) this->SetLogXaxis(value);
114                 
115                 fHistoMap->Add((TObject*)key, (TObject*)value);
116         }
117         
118         return value;
119 }
120
121 TH2D* AliLnHistoMap::Add(const TString& name, Int_t xbins, Double_t xmin, Double_t xmax, Int_t ybins, Double_t ymin, Double_t ymax, const TString& title, const TString& xlabel, const TString& ylabel, Bool_t logx, Bool_t logy)
122 {
123 //
124 // Add a TH2D histogram to the output map
125 //
126         TH2D* value = 0;
127         if(fHistoMap->Contains(name.Data()))
128         {
129                 fHistoMap->Warning("Add", "histogram %s already exists", name.Data());
130         }
131         else
132         {
133                 TObjString* key = new TObjString(name.Data());
134                 
135                 value = new TH2D(name.Data(),title.Data(),xbins,xmin,xmax,ybins,ymin,ymax);
136                 value->SetXTitle(xlabel.Data());
137                 value->SetYTitle(ylabel.Data());
138                 
139                 if(logx) this->SetLogXaxis(value);
140                 if(logy) this->SetLogYaxis(value);
141                 
142                 fHistoMap->Add((TObject*)key, (TObject*)value);
143         }
144         
145         return value;
146 }
147
148 Bool_t AliLnHistoMap::SetLogXaxis(TH1* h)
149 {
150 //
151 // transform linear X-axis bins to logarithmic bins
152 //
153         return this->SetLogBins(h->GetXaxis());
154 }
155
156 Bool_t AliLnHistoMap::SetLogYaxis(TH1* h)
157 {
158 //
159 // transform linear Y-axis bins to logarithmic bins
160 //
161         return this->SetLogBins(h->GetYaxis());
162 }
163
164 Bool_t AliLnHistoMap::SetLogBins(TAxis* axis)
165 {
166 //
167 // transform linear bins to logarithmic bins
168 // (adapted from TPad)
169 //
170         if(axis == 0) return kFALSE;
171         
172         Double_t xmin = axis->GetXmin();
173         Double_t xmax = axis->GetXmax();
174         
175         if(xmin <= 0)
176         {
177                 fHistoMap->Warning("SetLogBins", "no log bins, xmin=%f is <= 0", xmin);
178                 return kFALSE;
179         }
180         
181         Int_t nbins = axis->GetNbins();
182         
183         Double_t xminl = TMath::Log(xmin);
184         Double_t xmaxl = TMath::Log(xmax);
185         Double_t dx = (xmaxl-xminl)/nbins;
186         Double_t* xbins = new Double_t[nbins+1];
187         
188         xbins[0] = xmin;
189         for (Int_t i=1; i<=nbins; ++i)
190         {
191                 xbins[i] = TMath::Exp(xminl+i*dx);
192         }
193         
194         axis->Set(nbins, xbins);
195         delete[] xbins;
196         
197         return kTRUE;
198 }