51{
52 if (xAxis == 0 || xAxis > histo->GetDimension())
53 {
54 histo->Error("Interpolate2D","Invalid x-axis specified");
55 return 0;
56 }
57 if (yAxis == 0 || yAxis > histo->GetDimension())
58 {
59 histo->Error("Interpolate2D","Invalid y-axis specified");
60 return 0;
61 }
62 if (xAxis == yAxis)
63 {
64 histo->Error(
"Interpolate2D",
"Requested x-axis == y-axis, invalid for 2D interpolation");
65 return 0;
66 }
67
68
69
70 const TAxis* fXaxis = xAxis == 1 ?
histo->GetXaxis() : xAxis == 2 ?
histo->GetYaxis() : xAxis == 3 ?
histo->GetZaxis() :
nullptr;
71 const TAxis* fYaxis = yAxis == 1 ?
histo->GetXaxis() : yAxis == 2 ?
histo->GetYaxis() : yAxis == 3 ?
histo->GetZaxis() :
nullptr;
72
73 if (!fXaxis || !fYaxis)
74 {
75 histo->Error(
"Interpolate2D",
"Failed to parse axes from inputs");
76 return 0;
77 }
78
82 int bin_x = fXaxis->FindBin(
x);
83 int bin_y = fYaxis->FindBin(
y);
84 if(bin_x<1 || bin_x>fXaxis->GetNbins() || bin_y<1 || bin_y>fYaxis->GetNbins()) {
85 histo->Error(
"Interpolate",
"Cannot interpolate outside histogram domain. (x: %f vs [%f,%f], y: %f vs [%f,%f])",
x,fXaxis->GetBinLowEdge(1),fXaxis->GetBinLowEdge(fXaxis->GetNbins()+1),
y,fYaxis->GetBinLowEdge(1),fYaxis->GetBinLowEdge(fYaxis->GetNbins()+1));
86 return 0;
87 }
88 int quadrant = 0;
89
90 dx = fXaxis->GetBinUpEdge(bin_x)-
x;
91 dy = fYaxis->GetBinUpEdge(bin_y)-
y;
92 if (dx<=fXaxis->GetBinWidth(bin_x)/2 && dy<=fYaxis->GetBinWidth(bin_y)/2)
93 quadrant = 1;
94 if (dx>fXaxis->GetBinWidth(bin_x)/2 && dy<=fYaxis->GetBinWidth(bin_y)/2)
95 quadrant = 2;
96 if (dx>fXaxis->GetBinWidth(bin_x)/2 && dy>fYaxis->GetBinWidth(bin_y)/2)
97 quadrant = 3;
98 if (dx<=fXaxis->GetBinWidth(bin_x)/2 && dy>fYaxis->GetBinWidth(bin_y)/2)
99 quadrant = 4;
100 switch(quadrant) {
101 case 1:
102 x1 = fXaxis->GetBinCenter(bin_x);
103 y1 = fYaxis->GetBinCenter(bin_y);
104 x2 = fXaxis->GetBinCenter(bin_x+1);
105 y2 = fYaxis->GetBinCenter(bin_y+1);
106 break;
107 case 2:
108 x1 = fXaxis->GetBinCenter(bin_x-1);
109 y1 = fYaxis->GetBinCenter(bin_y);
110 x2 = fXaxis->GetBinCenter(bin_x);
111 y2 = fYaxis->GetBinCenter(bin_y+1);
112 break;
113 case 3:
114 x1 = fXaxis->GetBinCenter(bin_x-1);
115 y1 = fYaxis->GetBinCenter(bin_y-1);
116 x2 = fXaxis->GetBinCenter(bin_x);
117 y2 = fYaxis->GetBinCenter(bin_y);
118 break;
119 case 4:
120 x1 = fXaxis->GetBinCenter(bin_x);
121 y1 = fYaxis->GetBinCenter(bin_y-1);
122 x2 = fXaxis->GetBinCenter(bin_x+1);
123 y2 = fYaxis->GetBinCenter(bin_y);
124 break;
125 }
126 int bin_x1 = fXaxis->FindBin(x1);
127 if(bin_x1<1) bin_x1=1;
128 int bin_x2 = fXaxis->FindBin(x2);
129 if(bin_x2>fXaxis->GetNbins()) bin_x2=fXaxis->GetNbins();
130 int bin_y1 = fYaxis->FindBin(y1);
131 if(bin_y1<1) bin_y1=1;
132 int bin_y2 = fYaxis->FindBin(y2);
133 if(bin_y2>fYaxis->GetNbins()) bin_y2=fYaxis->GetNbins();
134
135 double q11;
136 double q12;
137 double q21;
138 double q22;
139 if (otherDimBin > 0)
140 {
141
142 if (xAxis == 1 && yAxis == 2)
143 {
144 q11 =
histo->GetBinContent(
histo->GetBin(bin_x1,bin_y1,otherDimBin));
145 q12 =
histo->GetBinContent(
histo->GetBin(bin_x1,bin_y2,otherDimBin));
146 q21 =
histo->GetBinContent(
histo->GetBin(bin_x2,bin_y1,otherDimBin));
147 q22 =
histo->GetBinContent(
histo->GetBin(bin_x2,bin_y2,otherDimBin));
148 }
149
150 else if (xAxis == 1 && yAxis == 3)
151 {
152 q11 =
histo->GetBinContent(
histo->GetBin(bin_x1,otherDimBin,bin_y1));
153 q12 =
histo->GetBinContent(
histo->GetBin(bin_x1,otherDimBin,bin_y2));
154 q21 =
histo->GetBinContent(
histo->GetBin(bin_x2,otherDimBin,bin_y1));
155 q22 =
histo->GetBinContent(
histo->GetBin(bin_x2,otherDimBin,bin_y2));
156 }
157
158 else if (xAxis == 2 && yAxis == 3)
159 {
160 q11 =
histo->GetBinContent(
histo->GetBin(otherDimBin,bin_x1,bin_y1));
161 q12 =
histo->GetBinContent(
histo->GetBin(otherDimBin,bin_x1,bin_y2));
162 q21 =
histo->GetBinContent(
histo->GetBin(otherDimBin,bin_x2,bin_y1));
163 q22 =
histo->GetBinContent(
histo->GetBin(otherDimBin,bin_x2,bin_y2));
164 }
165
166 else if (xAxis == 2 && yAxis == 1)
167 {
168 q11 =
histo->GetBinContent(
histo->GetBin(bin_y1,bin_x1,otherDimBin));
169 q12 =
histo->GetBinContent(
histo->GetBin(bin_y1,bin_x2,otherDimBin));
170 q21 =
histo->GetBinContent(
histo->GetBin(bin_y2,bin_x1,otherDimBin));
171 q22 =
histo->GetBinContent(
histo->GetBin(bin_y2,bin_x2,otherDimBin));
172 }
173
174 else if (xAxis == 3 && yAxis == 1)
175 {
176 q11 =
histo->GetBinContent(
histo->GetBin(bin_y1,otherDimBin,bin_x1));
177 q12 =
histo->GetBinContent(
histo->GetBin(bin_y1,otherDimBin,bin_x2));
178 q21 =
histo->GetBinContent(
histo->GetBin(bin_y2,otherDimBin,bin_x1));
179 q22 =
histo->GetBinContent(
histo->GetBin(bin_y2,otherDimBin,bin_x2));
180 }
181
182 else if (xAxis == 3 && yAxis == 2)
183 {
184 q11 =
histo->GetBinContent(
histo->GetBin(otherDimBin,bin_y1,bin_x1));
185 q12 =
histo->GetBinContent(
histo->GetBin(otherDimBin,bin_y1,bin_x2));
186 q21 =
histo->GetBinContent(
histo->GetBin(otherDimBin,bin_y2,bin_x1));
187 q22 =
histo->GetBinContent(
histo->GetBin(otherDimBin,bin_y2,bin_x2));
188 }
189 else
190 {
191 histo->Error(
"Interpolate2D",
"Unsupported axis combination: (x,y)=(%d,%d) with one bin fixed",xAxis,yAxis);
192 return 0;
193 }
194 }
195 else
196 {
197
198 if (xAxis == 1 && yAxis == 2)
199 {
200 q11 =
histo->GetBinContent(
histo->GetBin(bin_x1,bin_y1));
201 q12 =
histo->GetBinContent(
histo->GetBin(bin_x1,bin_y2));
202 q21 =
histo->GetBinContent(
histo->GetBin(bin_x2,bin_y1));
203 q22 =
histo->GetBinContent(
histo->GetBin(bin_x2,bin_y2));
204 }
205
206 else if (xAxis == 2 && yAxis == 1)
207 {
208 q11 =
histo->GetBinContent(
histo->GetBin(bin_y1,bin_x1));
209 q12 =
histo->GetBinContent(
histo->GetBin(bin_y1,bin_x2));
210 q21 =
histo->GetBinContent(
histo->GetBin(bin_y2,bin_x1));
211 q22 =
histo->GetBinContent(
histo->GetBin(bin_y2,bin_x2));
212 }
213 else
214 {
215 histo->Error(
"Interpolate2D",
"Unsupported axis combination: (x,y)=(%d,%d)",xAxis,yAxis);
216 return 0;
217 }
218 }
219
220 double d = 1.0*(
x2-
x1)*(y2-y1);
223}