FairRoot/PandaRoot
lookupTable.cxx
Go to the documentation of this file.
1 #include <iostream>
2 #include "TStyle.h"
3 #include "TH2D.h"
4 #include "TCanvas.h"
5 #include "TMath.h"
6 
7 //
8 // Johan Messchendorp - August 2009
9 //
10 // FindTheBin - scan through a 2D spectrum to find the best bin
11 // that fit the value (value_x,value_y);
12 // The results are stored in bin_x and bin_y;
13 // Function returns 0 if successfull, -1/-2 in case
14 // not found for x/y, respectively.
15 //
16 
17 Int_t FindTheBin(TH2 *lookup_table, Float_t value_x, Float_t value_y, Int_t &bin_x, Int_t &bin_y)
18 {
19  bin_x = lookup_table->GetXaxis()->FindBin(value_x);
20  bin_y = lookup_table->GetYaxis()->FindBin(value_y);
21 
22  if ((bin_x < 1) || (bin_x > lookup_table->GetXaxis()->GetNbins()))
23  {
24  bin_x = -1; bin_y = -1;
25  return -1;
26  }
27 
28  if ((bin_y < 1) || (bin_y > lookup_table->GetYaxis()->GetNbins()))
29  {
30  bin_x = -1; bin_y = -1;
31  return -2;
32  }
33 
34  return 0; // Succes
35 }
36 
37 //
38 // GetValueInZ - Obtains the value in Z given lookup_table
39 // that fit the value (value_x,value_y);
40 // Funtion returns the value in Z.
41 //
42 
43 Double_t GetValueInZ(TH2 *lookup_table, Float_t value_x, Float_t value_y, Bool_t use_interpolation = kFALSE)
44 {
45 
46  if (use_interpolation)
47  {
48  //
49  // Use the interpolarion routine of ROOT:
50  // Interpolate approximates the value via bilinear
51  // interpolation based on the four nearest bin centers
52  // see Wikipedia, Bilinear Interpolation
53  // Andy Mastbaum 10/8/2008
54  // vaguely based on R.Raja 6-Sep-2008
55  //
56  return (lookup_table->Interpolate(value_x,value_y));
57  }
58  else
59  {
60 
61  Int_t binx, biny, retval;
62 
63  retval = FindTheBin(lookup_table, value_x, value_y, binx, biny);
64  if (retval)
65  {
66  cout << "<E> Error in FindTheBin, check your table and input values!!!!: " << retval << endl;
67  return 0;
68  }
69 
70  return (lookup_table->GetBinContent(binx,biny));
71  }
72  return 0;
73 }
74 
75 void TestIt(Bool_t use_interpolation=kFALSE)
76 {
77  //
78  // Create two 2D spectrum, not equal in binning!
79  // Try to set the interpolation flag to kTRUE and compare with kFALSE
80  //
81 
82  gStyle->SetPalette(1);
83 
84  Double_t xlowarray_1[] = {0,10,20,30,40,50,60,70,80,90,100};
85  Double_t ylowarray_1[] = {0,10,20,30,40,50,60,70,80,90,100};
86 
87  TH2D *h1 = new TH2D("h1","Input spectrum",
88  (sizeof(xlowarray_1)/sizeof(Double_t))-1,xlowarray_1,
89  (sizeof(ylowarray_1)/sizeof(Double_t))-1,ylowarray_1);
90 
91  Double_t xlowarray_2[] = {0,10,20,30,35,40,45,46,47,48,49,50,51,52,53,54,55,60,65,70,80,90,100};
92  Double_t ylowarray_2[] = {0,10,20,30,35,40,45,46,47,48,49,50,51,52,53,54,55,60,65,70,80,90,100};
93 
94  TH2D *h2 = new TH2D("h2","Check spectrum",
95  (sizeof(xlowarray_2)/sizeof(Double_t))-1,xlowarray_2,
96  (sizeof(ylowarray_2)/sizeof(Double_t))-1,ylowarray_2);
97 
98  //
99  // Fill Input spectrum with random numbers
100  //
101 
102  for (Int_t i = 1; i < (h1->GetXaxis()->GetNbins() + 1); i++)
103  {
104  for (Int_t j = 1; j < (h1->GetYaxis()->GetNbins() + 1); j++)
105  {
106  h1->SetBinContent(i,j,
107  TMath::Gaus(h1->GetXaxis()->GetBinCenter(j),50,20)+
108  TMath::Gaus(h1->GetXaxis()->GetBinCenter(i),50,10));
109  }
110  }
111 
112  //
113  // Fill check spectrum using lookup
114  //
115 
116  Double_t valx, valy, valz;
117  for (Int_t i = 1; i < (h2->GetXaxis()->GetNbins() + 1); i++)
118  {
119  valx = h2->GetXaxis()->GetBinCenter(i);
120  for (Int_t j = 1; j < (h2->GetYaxis()->GetNbins() + 1); j++)
121  {
122  valy = h2->GetYaxis()->GetBinCenter(j);
123  cout << "<I> Checking (" << valx << "," << valy << ")" << endl;
124  valz = GetValueInZ((TH2*) h1, valx, valy, use_interpolation);
125  h2->SetBinContent(i,j,valz);
126  }
127  }
128 
129 //
130 // At this point h1 should be equal to h2, check it yourself!
131 //
132 
133  TCanvas *c = new TCanvas();
134  c->Divide(1,2);
135  c->cd(1);
136  h1->Draw("zcol");
137  c->cd(2);
138  h2->Draw("zcol");
139 
140  cout << "Compare TestIt(kTRUE) with TestIt(kFALSE), interpolation really improves! Cool, heh!" << endl;
141 }
Int_t i
Definition: run_full.C:25
Int_t FindTheBin(TH2 *lookup_table, Float_t value_x, Float_t value_y, Int_t &bin_x, Int_t &bin_y)
Definition: lookupTable.cxx:17
Double_t GetValueInZ(TH2 *lookup_table, Float_t value_x, Float_t value_y, Bool_t use_interpolation=kFALSE)
Definition: lookupTable.cxx:43
void TestIt(Bool_t use_interpolation=kFALSE)
Definition: lookupTable.cxx:75
Double_t