StRoot  1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
TMDFParameters.cxx
1 #include "Riostream.h"
2 #include "TMDFParameters.h"
3 #include "TMath.h"
4 #include <assert.h>
5 TMDFParameters* TMDFParameters::fgTMDFParameters = 0;
6 using namespace std;
7 ClassImp(TMDFParameters);
8 //________________________________________________________________________________
9 TArrayD *TMDFParameters::GetTerms(Double_t *x) {
10  TArrayD *TV = new TArrayD[fNvar];
11  for (Int_t i = 0; i < fNvar; i++) {
12  Int_t N = fMaxPower[i];
13  Double_t xx = x[i];
14  if (xx < fVmin[i]) xx = fVmin[i];
15  if (xx > fVmax[i]) xx = fVmax[i];
16  Double_t X = 1 + 2./(fVmax[i] - fVmin[i])*(xx - fVmax[i]);
17  Int_t nn = N+1;
18  if (nn < 3) nn = 3;
19  TV[i] = TArrayD(nn);
20  Tcheb(X,N,TV[i].GetArray());
21  }
22  return TV;
23 }
24 //________________________________________________________________________________
25 Double_t TMDFParameters::Eval(Double_t *x) {
26  TArrayD *TV = GetTerms(x);
27  Double_t value = fMean;
28  for (Int_t i = 0; i < fNcoef; i++) {
29  Double_t term = fCoef[i];
30  Int_t p = fCode[i];
31  for (Int_t j = fNvar - 1; j >= 0; j--) {
32  Int_t k = p%10; assert(k >= 0);
33  term *= TV[j][k-1];
34  p /= 10;
35  }
36  value += term;
37  }
38  delete [] TV;
39  return value;
40 }
41 //________________________________________________________________________________
42 Double_t TMDFParameters::dEval(Double_t *x) {
43  TArrayD *TV = GetTerms(x);
44  Double_t value = 0;
45  for (Int_t i = 0; i < fNcoef; i++) {
46  Double_t term = fdCoef[i];
47  Int_t p = fCode[i];
48  for (Int_t j = fNvar - 1; j >= 0; j--) {
49  Int_t k = p%10; assert(k >= 0);
50  term *= TV[j][k-1];
51  p /= 10;
52  }
53  value += term*term;
54  }
55  delete [] TV;
56  return TMath::Sqrt(value);
57 }
58 //________________________________________________________________________________
59 Double_t *TMDFParameters::Tcheb(Double_t x, Int_t N, Double_t *T) {
60  T[0] = 1; T[1] = T[2] = 0;
61  for (Int_t j = 1; j <= N; j++) {
62  if (j == 1) T[j] = x;
63  else T[j] = 2 * x * T[j-1] - T[j-2];
64  }
65  return &T[0];
66 }
67 //________________________________________________________________________________
68 Double_t TMDFParameters::Func(Double_t *x, Double_t *p) {
69  if (! p) return Instance()->Eval(x);
70  TArrayD X(Instance()->Nvar(),x);
71  Int_t j = 0;
72  for (Int_t i = 0; i < Instance()->Nvar(); i++) {
73  if (p[i] > -999.0) X[i] = p[i];
74  else X[i] = x[j++];
75  }
76  return Instance()->Eval(X.GetArray());
77 }
78 //________________________________________________________________________________
79 TF1 *TMDFParameters::ProjectionX(Int_t code) {
80  if (code < 0 || code >= Instance()->Nvar()) return 0;
81  TF1 *f = new TF1(Form("Func%i",code),Func,Vmin(code),Vmax(code),4);
82  Double_t params[4];
83  for (Int_t i = 0; i < Instance()->Nvar(); i++) {
84  params[i] = -999.;
85  if (i != code) params[i] = Vmax(i) - (Vmean(i) - 1)*(Vmin(i) - Vmax(i))/2.;
86  }
87  f->SetParameters(params);
88  return f;
89 }
90 //________________________________________________________________________________
91 TF2 *TMDFParameters::ProjectionXY(Int_t code1, Int_t code2) {
92  if (code1 == code2) return 0;
93  if (code1 < 0 || code1 >= Instance()->Nvar()) return 0;
94  if (code2 < 0 || code2 >= Instance()->Nvar()) return 0;
95  TF2 *f = new TF2(Form("Func%i_%i",code1,code2),Func,Vmin(code1),Vmax(code1),Vmin(code2),Vmax(code2),4);
96  Double_t params[4];
97  for (Int_t i = 0; i < Instance()->Nvar(); i++) {
98  params[i] = -999.;
99  if (i != code1 && i != code2) params[i] = Vmax(i) - (Vmean(i) - 1)*(Vmin(i) - Vmax(i))/2.;
100  }
101  f->SetParameters(params);
102  return f;
103 }
104 //________________________________________________________________________________
105 void TMDFParameters::Print(Option_t */* option */) const {
106  cout << "Sample statistics:" << "\n"
107  << "------------------" << "\n"
108  << " ";
109  for (Int_t i = 0; i < fNvar; i++)
110  cout << " " << setw(10) << i+1 << "\n";
111  cout << "\n" << " Max: " << "\n";
112  for (Int_t i = 0; i < fNvar; i++)
113  cout << " " << setw(10) << setprecision(4)
114  << fVmax[i] << "\n";
115  cout << "\n" << " Min: " << "\n";
116  for (Int_t i = 0; i < fNvar; i++)
117  cout << " " << setw(10) << setprecision(4)
118  << fVmin[i] << "\n";
119  cout << "\n" << " Mean: " << "\n";
120  for (Int_t i = 0; i < fNvar; i++)
121  cout << " " << setw(10) << setprecision(4)
122  << fVmean[i] << "\n";
123  cout << "\n";
124  cout << "Coefficients:" << "\n"
125  << "-------------" << "\n"
126  << " # Value Error Powers" << "\n"
127  << " ---------------------------------------" << "\n";
128  for (Int_t i = 0; i < fNcoef; i++) {
129  cout << " " << setw(3) << i << " "
130  << setw(12) << fCoef[i] << " "
131  << setw(12) << fdCoef[i] << " " << "\n";
132  Int_t p = fCode[i];
133  TArrayI Power(fNvar);
134  for (Int_t j = fNvar - 1; j >= 0; j--) {Power[j] = p%10; p /= 10;}
135  for (Int_t j = 0; j < fNvar; j++) cout << " " << setw(3) << Power[j] - 1 << "\n";
136  cout << endl;
137  }
138 }