Logo ROOT   6.30.04
Reference Guide
 All Namespaces Files Pages
TKDEAdapter.cxx
Go to the documentation of this file.
1 // @(#)root/gl:$Id$
2 // Author: Timur Pocheptsov 28/07/2009
3 
4 /*************************************************************************
5  * Copyright (C) 1995-2009, Rene Brun and Fons Rademakers. *
6  * All rights reserved. *
7  * *
8  * For the licensing terms see $ROOTSYS/LICENSE. *
9  * For the list of contributors see $ROOTSYS/README/CREDITS. *
10  *************************************************************************/
11 
12 #include <stdexcept>
13 
14 #include "TKDEAdapter.h"
15 #include "TKDEFGT.h"
16 #include "TError.h"
17 #include "TGL5D.h"
18 #include "TAxis.h"
19 
20 namespace Rgl {
21 namespace Fgt {
22 
23 ////////////////////////////////////////////////////////////////////////////////
24 ///Constructor. "Half-baked" object.
25 
26 TKDEAdapter::TKDEAdapter()
27  : fW(0), fH(0), fD(0),
28  fSliceSize(0),
29  fXMin(0.), fXStep(0.),
30  fYMin(0.), fYStep(0.),
31  fZMin(0.), fZStep(0.),
32  fDE(0),
33  fE(10.)
34 {
35 }
36 
37 ////////////////////////////////////////////////////////////////////////////////
38 ///Set grid's dimensions.
39 
40 void TKDEAdapter::SetGeometry(const TGL5DDataSet *dataSet)
41 {
42  const TAxis *xA = dataSet->GetXAxis();
43  const Rgl::Range_t &xMinMax = dataSet->GetXRange();
44  const Double_t xRange = xMinMax.second - xMinMax.first;
45 
46  const TAxis *yA = dataSet->GetYAxis();
47  const Rgl::Range_t &yMinMax = dataSet->GetYRange();
48  const Double_t yRange = yMinMax.second - yMinMax.first;
49 
50  const TAxis *zA = dataSet->GetZAxis();
51  const Rgl::Range_t &zMinMax = dataSet->GetZRange();
52  const Double_t zRange = zMinMax.second - zMinMax.first;
53 
54  fW = xA->GetNbins();
55  fH = yA->GetNbins();
56  fD = zA->GetNbins();
57 
58  fSliceSize = fW * fH;
59 
60  fXMin = (xA->GetBinLowEdge(1) - xMinMax.first) / xRange;
61  fXStep = (xA->GetBinUpEdge(xA->GetLast()) - xA->GetBinLowEdge(xA->GetFirst())) / (fW - 1) / xRange;
62 
63  fYMin = (yA->GetBinLowEdge(1) - yMinMax.first) / yRange;
64  fYStep = (yA->GetBinUpEdge(yA->GetLast()) - yA->GetBinLowEdge(yA->GetFirst())) / (fH - 1) / yRange;
65 
66  fZMin = (zA->GetBinLowEdge(1) - zMinMax.first) / zRange;
67  fZStep = (zA->GetBinCenter(zA->GetLast()) - zA->GetBinLowEdge(zA->GetFirst())) / (fD - 1) / zRange;
68 }
69 
70 ////////////////////////////////////////////////////////////////////////////////
71 ///e for kdefgt.
72 
73 void TKDEAdapter::SetE(Double_t e)
74 {
75  fE = e;
76 }
77 
78 ////////////////////////////////////////////////////////////////////////////////
79 ///e for kdefgt.
80 
81 Double_t TKDEAdapter::GetE()const
82 {
83  return fE;
84 }
85 
86 ////////////////////////////////////////////////////////////////////////////////
87 ///Number of cells along X.
88 
89 UInt_t TKDEAdapter::GetW()const
90 {
91  return fW;
92 }
93 
94 ////////////////////////////////////////////////////////////////////////////////
95 ///Number of cells along Y.
96 
97 UInt_t TKDEAdapter::GetH()const
98 {
99  return fH;
100 }
101 
102 ////////////////////////////////////////////////////////////////////////////////
103 ///Number of cells along Z.
104 
105 UInt_t TKDEAdapter::GetD()const
106 {
107  return fD;
108 }
109 
110 ////////////////////////////////////////////////////////////////////////////////
111 ///Set density estimator as a data source.
112 
113 void TKDEAdapter::SetDataSource(const TKDEFGT *de)
114 {
115  fDE = de;
116 }
117 
118 ////////////////////////////////////////////////////////////////////////////////
119 ///Do some initialization and calculate densities.
120 
121 void TKDEAdapter::FetchDensities()const
122 {
123  if (!fDE) {
124  Error("TKDEAdapter::FetchFirstSlices", "Density estimator is a null pointer."
125  " Set it correctly first.");
126  throw std::runtime_error("No density estimator.");
127  }
128 
129  fGrid.resize(fD * fSliceSize * 3);//3 is the number of coordinates: xyz
130 
131  //1D index into fGrid array.
132  UInt_t ind = 0;
133  //The first slice.
134  for(UInt_t k = 0; k < fD; ++k) {
135  for (UInt_t i = 0; i < fH; ++i) {
136  for (UInt_t j = 0; j < fW; ++j, ind += 3) {
137  fGrid[ind] = fXMin + j * fXStep;
138  fGrid[ind + 1] = fYMin + i * fYStep;
139  fGrid[ind + 2] = fZMin + k * fZStep;
140  }
141  }
142  }
143 
144  fDensities.resize(fSliceSize * fD);
145  //Ok, now, we can estimate densities.
146  fDE->Predict(fGrid, fDensities, fE);
147 }
148 
149 ////////////////////////////////////////////////////////////////////////////////
150 /// Get data at given position.
151 
152 Float_t TKDEAdapter::GetData(UInt_t i, UInt_t j, UInt_t k)const
153 {
154  const UInt_t ind = k * fSliceSize + j * fW + i;
155  return fDensities[ind];
156 }
157 
158 ////////////////////////////////////////////////////////////////////////////////
159 /// Free grid and density vectors.
160 
161 void TKDEAdapter::FreeVectors()
162 {
163  vector_t().swap(fGrid);
164  vector_t().swap(fDensities);
165 }
166 
167 }
168 }