]> git.uio.no Git - u/mrichter/AliRoot.git/blob - FMD/flow/AliFMDFlowBinned2D.cxx
coverity 15108 fixed
[u/mrichter/AliRoot.git] / FMD / flow / AliFMDFlowBinned2D.cxx
1 /* Copyright (C) 2007 Christian Holm Christensen <cholm@nbi.dk>
2  *
3  * This library is free software; you can redistribute it and/or
4  * modify it under the terms of the GNU Lesser General Public License
5  * as published by the Free Software Foundation; either version 2.1 of
6  * the License, or (at your option) any later version.
7  *
8  * This library is distributed in the hope that it will be useful, but
9  * WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11  * Lesser General Public License for more details.
12  *
13  * You should have received a copy of the GNU Lesser General Public
14  * License along with this library; if not, write to the Free Software
15  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
16  * USA
17  */
18 /** @file 
19     @brief Implementation of a 2-dimensional Flow "histogram" */
20 //____________________________________________________________________ 
21 //
22 // A histogram of flow bins.  The axis can by anything
23 // (pseudo-rapidity, transvers momentum) - there's no assumption on
24 // what is the basis of the histogram.  The method Event can be used
25 // to calculate everything in one go.   Alternatively, one can use the
26 // methods AddToEventPlane and AddToHarmonic.  See also the example
27 // TestFlow.C 
28 #include "flow/AliFMDFlowBinned2D.h"
29 #include "flow/AliFMDFlowBin.h"
30 #include "flow/AliFMDFlowSplitter.h"
31 #include <cmath>
32 #include <cstdlib>
33 #include <TString.h>
34 #include <TBrowser.h>
35
36 //====================================================================
37 AliFMDFlowBinned2D::AliFMDFlowBinned2D(const char* name, 
38                                        const char* title,
39                                        UShort_t order, 
40                                        UShort_t nxbins, Double_t* xbins,
41                                        UShort_t nybins, Double_t* ybins,
42                                        AliFMDFlowSplitter* splitter) 
43   : TNamed(name, title), 
44     fXAxis(nxbins, xbins),
45     fYAxis(nybins, ybins),
46     fBins(0), 
47     fSplitter(splitter)
48 {
49   // Constructor 
50   // Parameters: 
51   //   Order    Order 
52   //   nxbins   Number of X bins 
53   //   xbins    X Bin borders 
54   //   nybins   Number of Y bins 
55   //   ybins    Y Bin borders 
56   UInt_t n = fXAxis.N() * fYAxis.N();
57   fBins   = new AliFMDFlowBin*[n];
58   for (UInt_t i = 0; i < n; i++) fBins[i]= new AliFMDFlowBin(order);
59   if (!fSplitter) fSplitter = new AliFMDFlowShuffle;
60 }
61 //____________________________________________________________________
62 AliFMDFlowBinned2D::AliFMDFlowBinned2D(const char*           name, 
63                                        const char*           title,
64                                        UShort_t              order, 
65                                        const AliFMDFlowAxis& xaxis, 
66                                        const AliFMDFlowAxis& yaxis,
67                                        AliFMDFlowSplitter*   splitter)
68   : TNamed(name, title),
69     fXAxis(xaxis), 
70     fYAxis(yaxis),
71     fBins(0),
72     fSplitter(splitter)
73 {
74   // Constructor 
75   // Parameters: 
76   //   Order    Order 
77   //   xaxis    X axis object
78   //   yaxis    Y axis object
79   UShort_t n = fXAxis.N() * fYAxis.N();
80   fBins   = new AliFMDFlowBin*[n];
81   for (UInt_t i = 0; i < n; i++) fBins[i]= new AliFMDFlowBin(order);
82   if (!fSplitter) fSplitter = new AliFMDFlowShuffle;
83 }
84 //____________________________________________________________________
85 AliFMDFlowBinned2D::AliFMDFlowBinned2D(const AliFMDFlowBinned2D& o)
86   : TNamed(o), 
87     TAttLine(o),
88     TAttFill(o),
89     TAttMarker(o),
90     fXAxis(o.fXAxis), 
91     fYAxis(o.fYAxis),
92     fBins(0),
93     fSplitter(0)
94 {
95   // Copy constructor 
96   // Parameters: 
97   //   o   Object to copy from 
98   UShort_t n = fXAxis.N() * fYAxis.N();
99   fSplitter  = new AliFMDFlowShuffle;
100   fBins      = new AliFMDFlowBin*[n];
101   for (UInt_t i = 0; i < n; i++) fBins[i]= new AliFMDFlowBin(*(o.fBins[i]));
102 }
103 //____________________________________________________________________
104 AliFMDFlowBinned2D&
105 AliFMDFlowBinned2D::operator=(const AliFMDFlowBinned2D& o)
106 {
107   // Assignment operator
108   // Parameters: 
109   //   o Object to assign from 
110   // 
111   // Returns reference to this object 
112   SetLineColor(o.GetLineColor());
113   SetLineStyle(o.GetLineStyle());
114   SetLineWidth(o.GetLineWidth());
115   SetFillColor(o.GetFillColor());
116   SetFillStyle(o.GetFillStyle());
117   SetMarkerColor(o.GetMarkerColor());
118   SetMarkerStyle(o.GetMarkerStyle());
119   SetMarkerSize(o.GetMarkerSize());
120   if (fBins) { 
121     UInt_t n = fXAxis.N() * fYAxis.N();
122     for (UInt_t i = 0; i < n; i++) delete fBins[i];
123     delete [] fBins;
124   }
125   fXAxis     = o.fXAxis;
126   UShort_t n = fXAxis.N() * fYAxis.N();
127   fSplitter  = new AliFMDFlowShuffle;
128   fBins      = new AliFMDFlowBin*[n];
129   for (UInt_t i = 0; i < n; i++) fBins[i]= new AliFMDFlowBin(*(o.fBins[i]));
130   return *this;
131 }
132
133 //____________________________________________________________________
134 AliFMDFlowBinned2D::~AliFMDFlowBinned2D()
135 {
136   // Destructor 
137   // Parameters: 
138   //    none 
139   if (fBins) { 
140     UInt_t n = fXAxis.N() * fYAxis.N();
141     for (UInt_t i = 0; i < n; i++) delete fBins[i];
142     delete [] fBins;
143   }
144   if (fSplitter) delete fSplitter;
145 }
146 //____________________________________________________________________
147 AliFMDFlowBin* 
148 AliFMDFlowBinned2D::GetBin(UShort_t i, UShort_t j) const
149 {
150   // Get the ith,jth bin 
151   // Parameters: 
152   //    i  X Bin number 
153   //    j  Y Bin number 
154   // 
155   // Return pointer to bin, or null. 
156   if (i >= fXAxis.N() || j >= fYAxis.N()) return 0;
157   return fBins[i * fYAxis.N() + j];
158 }
159 //____________________________________________________________________
160 AliFMDFlowBin* 
161 AliFMDFlowBinned2D::GetBin(Double_t x, Double_t y) const
162 {
163   // Get the bin that contains x,y
164   // Parameters: 
165   //    x  X axis value
166   //    y  X axis value
167   // 
168   // Return pointer to bin, or null. 
169   Int_t i = fXAxis.FindBin(x);
170   if (i < 0) return 0;
171   Int_t j = fYAxis.FindBin(y);
172   if (j < 0) return 0;
173   UShort_t k = i;
174   UShort_t l = j;
175   return GetBin(k, l);
176 }
177 //____________________________________________________________________
178 void 
179 AliFMDFlowBinned2D::Begin()
180 {
181   // Called at the beginning of an event
182   // Parameters: 
183   //   none
184   UInt_t n = fXAxis.N() * fYAxis.N();
185   for (UInt_t i = 0; i < n; i++) fBins[i]->Begin();
186 }
187 //____________________________________________________________________
188 void 
189 AliFMDFlowBinned2D::End()
190 {
191   // Called at the end of an event
192   // Parameters: 
193   //   none
194   UInt_t n = fXAxis.N() * fYAxis.N();
195   for (UInt_t i = 0; i < n; i++) fBins[i]->End();
196 }
197 //____________________________________________________________________
198 void 
199 AliFMDFlowBinned2D::Finish()
200 {
201   // Called at the end of an job
202   // Parameters: 
203   //   none
204   UInt_t n = fXAxis.N() * fYAxis.N();
205   for (UInt_t i = 0; i < n; i++) fBins[i]->Finish();
206 }
207 //____________________________________________________________________
208 Bool_t 
209 AliFMDFlowBinned2D::AddToEventPlane(Double_t x, Double_t y, Double_t phi, 
210                                     Double_t w, Bool_t a)
211 {
212   // Called to add a contribution to the event plane 
213   // Parameters:
214   //    x   X Bin value to fill into 
215   //    y   Y Bin value to fill into 
216   //    w   Weight
217   //    phi The angle phi in radians 
218   //    a   If true, add to sub-event A, otherwise sub-event B
219   // 
220   // Return false if (x,y) falls outside the defined range, true otherwise
221   AliFMDFlowBin* bin = GetBin(x, y);
222   if (!bin) return kFALSE;
223   bin->AddToEventPlane(phi, w, a);
224   return kTRUE;
225 }
226
227 //____________________________________________________________________
228 Bool_t 
229 AliFMDFlowBinned2D::AddToHarmonic(Double_t x, Double_t y, Double_t phi,
230                                   Double_t wp, Double_t wh)
231 {
232   // Called to add a contribution to the harmonic
233   // Parameters: 
234   //    x   X Bin value to fill into 
235   //    y   Y Bin value to fill into 
236   //    phi The angle phi in radians
237   // 
238   // Return false if (x,y) falls outside the defined range, true otherwise
239   AliFMDFlowBin* bin = GetBin(x, y);
240   if (!bin) return kFALSE;
241   bin->AddToHarmonic(phi, wp, wh);
242   return kTRUE;
243 }
244
245 //____________________________________________________________________
246 void 
247 AliFMDFlowBinned2D::Event(ULong_t   n,  Double_t* phis, 
248                           Double_t* xs, Double_t* ys, 
249                           Double_t* wp, Double_t* wh)
250 {
251   // Process a full event. 
252   // Parameters: 
253   //   phis   List of n phi=[0,2pi] angles 
254   //   xs     List of n x values. 
255   //   ys     List of n y values. 
256   //   wp     Weights for event plane
257   //   wh     Weights for harmonic
258   //   n      Size of phis and xs
259   Begin();
260   fSplitter->Event(phis, xs, n);
261   for (UInt_t i = 0; i < n; i++) 
262     AddToEventPlane(xs[i], ys[i], phis[i], (wp ? wp[i] : 1), 
263                     fSplitter->Select(i));
264   for (UInt_t i = 0; i < n; i++) 
265     AddToHarmonic(xs[i], ys[i], phis[i], (wp ? wh[i] : 1), (wp ? wh[i] : 1));
266   End();
267 }
268
269 //____________________________________________________________________
270 void 
271 AliFMDFlowBinned2D::Browse(TBrowser* b)
272 {
273   // Browse this object
274   b->Add(&fXAxis, "xaxis");
275   b->Add(&fYAxis, "yaxis");
276   for (UInt_t i = 0; i < fXAxis.N(); i++) { 
277     for (UInt_t j = 0; i < fYAxis.N(); j++) { 
278       b->Add(fBins[i*fXAxis.N()+j], Form("bin_%03d_%03d", i, j));
279     }
280   }
281 }
282
283 //____________________________________________________________________
284 //
285 // EOF
286 //