001
002
003
004
005
006
007
008
009
010
011 #include "JetTagTools/HistoHelperRoot.h"
012 #include "GaudiKernel/ITHistSvc.h"
013 #include "TH1.h"
014 #include "TH2.h"
015 #include "TH3.h"
016 #include <iostream>
017 #include <string>
018 #include <utility>
019 #include <vector>
020
021 namespace Analysis
022 {
023
024 HistoHelperRoot::HistoHelperRoot(ITHistSvc* histoSvc) :
025 m_histoSvc(histoSvc),
026 m_histoMap1D(std::map<std::string, TH1*>()),
027 m_histoMap2D(std::map<std::string, TH2*>()),
028 m_histoMap3D(std::map<std::string, TH3*>()),
029 m_histoLimitsMap1D(std::map<std::string, HistoLimits>()),
030 m_histoLimitsMap2D(std::map<std::string, HistoLimits>()),
031 m_histoLimitsMap3D(std::map<std::string, HistoLimits>()),
032 m_checkOverflows(true)
033 {}
034
035
036 HistoHelperRoot::~HistoHelperRoot() {}
037
038 std::string HistoHelperRoot::baseName(const std::string& fullHistoName) {
039 const std::string delim("/");
040 std::vector<std::string> words;
041 std::string::size_type sPos, sEnd, sLen;
042 sPos = fullHistoName.find_first_not_of(delim);
043 while ( sPos != std::string::npos ) {
044 sEnd = fullHistoName.find_first_of(delim, sPos);
045 if(sEnd==std::string::npos) sEnd = fullHistoName.length();
046 sLen = sEnd - sPos;
047 std::string word = fullHistoName.substr(sPos,sLen);
048 words.push_back(word);
049 sPos = fullHistoName.find_first_not_of(delim, sEnd);
050 }
051 std::string base = "";
052 if(words.size()>0) base = words[words.size()-1];
053
054 return base;
055 }
056
057 void HistoHelperRoot::bookHisto(const std::string& histoName, const std::string& histoTitle, unsigned int bins, double minx, double maxx)
058 {
059 m_histoMap1D[histoName] = new TH1F(this->baseName(histoName).c_str(),histoTitle.c_str(),bins,minx,maxx);
060
061 if (!m_histoSvc->regHist(histoName,m_histoMap1D[histoName]).isSuccess()) {
062 std::cout <<"Cannot book histo " << histoName << " with Title " << histoTitle <<std::endl;
063 }
064 m_histoLimitsMap1D[histoName] = HistoLimits(bins, minx, maxx);
065 }
066
067 void HistoHelperRoot::bookHisto(const std::string& histoName, const std::string& histoTitle, unsigned int bins, double* edge)
068 {
069 m_histoMap1D[histoName] = new TH1F(this->baseName(histoName).c_str(),histoTitle.c_str(),bins,edge);
070 if (!m_histoSvc->regHist(histoName,m_histoMap1D[histoName]).isSuccess()) {
071 std::cout <<"Cannot book histo " << histoName << " with Title " << histoTitle <<std::endl;
072 }
073 m_histoLimitsMap1D[histoName] = HistoLimits(bins, edge[0], edge[bins]);
074 }
075
076 void HistoHelperRoot::bookHisto(const std::string& histoName, const std::string& histoTitle, unsigned int binsx, double minx, double maxx, unsigned int binsy, double miny, double maxy)
077 {
078 m_histoMap2D[histoName] = new TH2F(this->baseName(histoName).c_str(),histoTitle.c_str(),binsx,minx,maxx,binsy,miny,maxy);
079 if (!m_histoSvc->regHist(histoName,m_histoMap2D[histoName]).isSuccess()) {
080 std::cout <<"Cannot book histo " << histoName << " with Title " << histoTitle <<std::endl;
081 }
082 m_histoLimitsMap2D[histoName] = HistoLimits(binsx, minx, maxx, binsy, miny, maxy);
083 }
084
085 void HistoHelperRoot::bookHisto(const std::string& histoName, const std::string& histoTitle, unsigned int binsx, double* edgex, unsigned int binsy, double* edgey)
086 {
087 m_histoMap2D[histoName] = new TH2F(this->baseName(histoName).c_str(),histoTitle.c_str(),binsx,edgex,binsy,edgey);
088 if (!m_histoSvc->regHist(histoName,m_histoMap2D[histoName]).isSuccess()) {
089 std::cout <<"Cannot book histo " << histoName << " with Title " << histoTitle <<std::endl;
090 }
091 m_histoLimitsMap2D[histoName] = HistoLimits(binsx, edgex[0], edgex[binsx], binsy, edgey[0], edgey[binsy]);
092 }
093
094 void HistoHelperRoot::bookHisto(const std::string& histoName, const std::string& histoTitle, unsigned int binsx, double minx, double maxx, unsigned int binsy, double miny, double maxy, unsigned int binsz, double minz, double maxz)
095 {
096 m_histoMap3D[histoName] = new TH3F(this->baseName(histoName).c_str(),histoTitle.c_str(),binsx,minx,maxx,binsy,miny,maxy,binsz,minz,maxz);
097 if (!m_histoSvc->regHist(histoName,m_histoMap3D[histoName]).isSuccess()) {
098 std::cout <<"Cannot book histo " << histoName << " with Title " << histoTitle <<std::endl;
099 }
100 m_histoLimitsMap3D[histoName] = HistoLimits(binsx, minx, maxx, binsy, miny, maxy, binsz, minz, maxz);
101 }
102
103
104
105 void HistoHelperRoot::fillHisto(const std::string& histoName, double value)
106 {
107
108
109 if (m_checkOverflows) {
110 const HistoLimits& l = (*(m_histoLimitsMap1D.find(histoName))).second;
111 if ( value < l.xmin ) value = l.xmin+0.0001;
112 else if ( value > l.xmax ) value = l.xmax-0.0001;
113 }
114 m_histoMap1D[histoName]->Fill(value);
115 }
116
117 void HistoHelperRoot::fillHistoWithWeight(const std::string& histoName, double value, double weight)
118 {
119
120
121 if (m_checkOverflows) {
122 const HistoLimits& l = (*(m_histoLimitsMap1D.find(histoName))).second;
123 if ( value < l.xmin ) value = l.xmin+0.0001;
124 else if ( value > l.xmax ) value = l.xmax-0.0001;
125 }
126 m_histoMap1D[histoName]->Fill(value,weight);
127 }
128
129 void HistoHelperRoot::fillHisto(const std::string& histoName, double valuex, double valuey)
130 {
131
132
133 if (m_checkOverflows) {
134 const HistoLimits& l = (*(m_histoLimitsMap2D.find(histoName))).second;
135 if ( valuex < l.xmin ) valuex = l.xmin+0.0001;
136 else if ( valuex > l.xmax ) valuex = l.xmax-0.0001;
137 if ( valuey < l.ymin ) valuey = l.ymin+0.0001;
138 else if ( valuey > l.ymax ) valuey = l.ymax-0.0001;
139 }
140 m_histoMap2D[histoName]->Fill(valuex,valuey);
141 }
142 void HistoHelperRoot::fillHisto(const std::string& histoName, double valuex, double valuey, double valuez)
143 {
144
145
146 if (m_checkOverflows) {
147 const HistoLimits& l = (*(m_histoLimitsMap3D.find(histoName))).second;
148 if ( valuex < l.xmin ) valuex = l.xmin+0.0001;
149 else if ( valuex > l.xmax ) valuex = l.xmax-0.0001;
150 if ( valuey < l.ymin ) valuey = l.ymin+0.0001;
151 else if ( valuey > l.ymax ) valuey = l.ymax-0.0001;
152 if ( valuez < l.zmin ) valuez = l.zmin+0.0001;
153 else if ( valuez > l.zmax ) valuez = l.zmax-0.0001;
154 }
155 m_histoMap3D[histoName]->Fill(valuex,valuey,valuez);
156 }
157
158 TH1* HistoHelperRoot::getHisto1D(const std::string& histoName)
159 {
160 return m_histoMap1D[histoName];
161 }
162 TH2* HistoHelperRoot::getHisto2D(const std::string& histoName)
163 {
164 return m_histoMap2D[histoName];
165 }
166 TH3* HistoHelperRoot::getHisto3D(const std::string& histoName)
167 {
168 return m_histoMap3D[histoName];
169 }
170
171 void HistoHelperRoot::normalizeHistos()
172 {
173
174 for (std::map<std::string, TH1*>::iterator mapItr = m_histoMap1D.begin();
175 mapItr != m_histoMap1D.end(); ++mapItr ) {
176 const std::string& name = (*mapItr).first;
177
178 TH1* histo = (*mapItr).second;
179 double norm(histo->Integral());
180 if (norm!=1. && norm != 0.) {
181
182 histo->Scale(1./norm);
183 } else if (norm == 0.) {
184 std::cout << "Empty histogram ! "<< name <<std::endl;
185 }
186 }
187 for (std::map<std::string, TH2*>::iterator mapItr = m_histoMap2D.begin();
188 mapItr != m_histoMap2D.end(); ++mapItr ) {
189 const std::string& name = (*mapItr).first;
190
191 TH2* histo = (*mapItr).second;
192 double norm(histo->Integral());
193 if (norm!=1. && norm != 0.) {
194
195 histo->Scale(1./norm);
196 } else if (norm == 0.) {
197 std::cout << "Empty histogram ! "<< name <<std::endl;
198 }
199 }
200 for (std::map<std::string, TH3*>::iterator mapItr = m_histoMap3D.begin();
201 mapItr != m_histoMap3D.end(); ++mapItr ) {
202 const std::string& name = (*mapItr).first;
203
204 TH3* histo = (*mapItr).second;
205 double norm(histo->Integral());
206 if (norm!=1. && norm != 0.) {
207
208 histo->Scale(1./norm);
209 } else if (norm == 0.) {
210 std::cout << "Empty histogram ! "<< name <<std::endl;
211 }
212 }
213
214
215 }
216
217 void HistoHelperRoot::normalizeHistos(const std::string histname)
218 {
219 double norm = 0.;
220
221 std::map<std::string,TH1*>::const_iterator pos = m_histoMap1D.find(histname);
222 if (pos != m_histoMap1D.end()) {
223 TH1* histo = pos->second;
224 norm = histo->Integral();
225 if (norm!=1. && norm != 0.) {
226 histo->Scale(1./norm);
227 } else if (norm == 0.) {
228 std::cout << "Empty histogram ! "<< (*pos).first <<std::endl;
229 }
230 } else {
231 std::map<std::string,TH2*>::const_iterator pos = m_histoMap2D.find(histname);
232 if (pos != m_histoMap2D.end()) {
233 TH2* histo = pos->second;
234 norm = histo->Integral();
235 if (norm!=1. && norm != 0.) {
236 histo->Scale(1./norm);
237 } else if (norm == 0.) {
238 std::cout << "Empty histogram ! "<< (*pos).first <<std::endl;
239 }
240 } else {
241 std::map<std::string,TH3*>::const_iterator pos = m_histoMap3D.find(histname);
242 if (pos != m_histoMap3D.end()) {
243 TH3* histo = pos->second;
244 norm = histo->Integral();
245 if (norm!=1. && norm != 0.) {
246 histo->Scale(1./norm);
247 } else if (norm == 0.) {
248 std::cout << "Empty histogram ! "<< (*pos).first <<std::endl;
249 }
250 } else {
251 std::cout << "Histo "<< histname << " does not exist ? Nothing to normalise !" << std::endl;
252 }
253 }
254 }
255 }
256
257 void HistoHelperRoot::smoothHistos(const std::string histname)
258 {
259 std::map<std::string,TH1*>::const_iterator pos = m_histoMap1D.find(histname);
260 if (pos != m_histoMap1D.end()) {
261 (pos->second)->Smooth();
262 } else {
263 std::cout << "Histo "<< histname << " does not exist ? Nothing to normalise !" << std::endl;
264 }
265 }
266
267 void HistoHelperRoot::print() {
268 for (std::map<std::string, TH1*>::iterator mapItr = m_histoMap1D.begin();
269 mapItr != m_histoMap1D.end(); ++mapItr ) {
270 const std::string& name = (*mapItr).first;
271 std::cout <<"The name of the 1D Histo " << name << std::endl;
272 }
273 for (std::map<std::string, TH2*>::iterator mapItr = m_histoMap2D.begin();
274 mapItr != m_histoMap2D.end(); ++mapItr ) {
275 const std::string& name = (*mapItr).first;
276 std::cout <<"The name of the 2D Histo " << name << std::endl;
277 }
278 for (std::map<std::string, TH3*>::iterator mapItr = m_histoMap3D.begin();
279 mapItr != m_histoMap3D.end(); ++mapItr ) {
280 const std::string& name = (*mapItr).first;
281 std::cout <<"The name of the 3D Histo " << name << std::endl;
282 }
283 }
284
285 void HistoHelperRoot::smoothASH2D(TH2* input2D, int m1, int m2, bool debug) {
286 if(debug) {
287 std::cout << "Smoothing a two dimensional histogram "<< input2D->GetName()
288 << " " << m1 << " " << m2 << std::endl;
289 std::cout << "First (1-3, 1-3) 3x3 bins before smoothing: " << std::endl;
290 for(int i=1;i<4;i++) {
291 for(int j=1;j<4;j++) {
292 std::cout<<i<<" "<<j<<" : "<<input2D->GetBinContent(i,j)<< " / " ;
293 }
294 }
295 std::cout << std::endl;
296 int ioffset = input2D->GetNbinsX() / 2 ;
297 int joffset = input2D->GetNbinsY() / 2 ;
298 std::cout << "Middle (" << ioffset+1 << "-" << ioffset+4 << ", ("
299 << joffset+1 << "-" << joffset+4 << ") 3x3 bins before smoothing: " << std::endl;
300 for(int i=1;i<4;i++) {
301 for(int j=1;j<4;j++) {
302 std::cout<<i<<" "<<j<<" : "<<input2D->GetBinContent(i+ioffset,j+joffset)<< " / " ;
303 }
304 }
305 std::cout << std::endl;
306 }
307
308 const int lsup = 20;
309 if (m1 > lsup || m2 > lsup) {
310 std::cout <<"HistoHelperRoot::smoothASH2D: m1 or m2 too big !"<<std::endl;
311 return;
312 } else {
313 int nx = input2D->GetNbinsX()+1;
314 int ny = input2D->GetNbinsY()+1;
315 float **h, **res;
316 h = new float*[nx-1];
317 res = new float*[nx-1];
318 for (int i = 0;i < nx-1;i++) {
319 h[i] = new float[ny-1];
320 res[i] = new float[ny-1];
321 }
322 for (int iy = 1;iy<ny;iy++) {
323 for (int ix = 1;ix<nx;ix++) {
324 h[ix-1][iy-1] = (float) input2D->GetBinContent(ix,iy);
325 }
326 }
327
328 int i,j,k,l;
329 float wk1[41],wk2[41],wgt[100][100];
330 double wk[41][41],wks = 0.;
331 float ai,am1 = float(m1), am2 = float(m2);
332 float am12 = am1*am1, am22 = am2*am2;
333
334 for (k = 0;k<nx-1;k++) {
335 for (l = 0;l<ny-1;l++) {
336 res[k][l] = 0.; wgt[k][l] = 0.;
337 }
338 }
339
340 for (i = lsup+1-m1;i<lsup+m1;i++) {
341 ai = float(i-lsup)*float(i-lsup);
342 wk1[i] = 15./16.*(1.-ai/am12)*(1.-ai/am12);
343 wks = wks + wk1[i];
344 }
345 for (i = lsup+1-m1;i<lsup+m1;i++) {
346 wk1[i] = wk1[i]*am1/wks;
347 }
348 wks = 0.;
349 for (i = lsup+1-m2;i<lsup+m2;i++) {
350 ai = float(i-lsup)*float(i-lsup);
351 wk2[i] = 15./16.*(1.-ai/am22)*(1.-ai/am22);
352 wks = wks + wk2[i];
353 }
354 for (i = lsup+1-m2;i<lsup+m2;i++) {
355 wk2[i] = wk2[i]*am2/wks;
356 }
357 for (i = lsup+1-m1;i<lsup+m1;i++) {
358 for (j = lsup+1-m2;j<lsup+m2;j++) {
359 wk[i][j] = wk1[i]*wk2[j];
360 }
361 }
362
363 for (k = 0;k<nx-1;k++) {
364 for (l = 0;l<ny-1;l++) {
365 for (i = std::max(0,k-m1+1);i<std::min(nx-1,k+m1);i++) {
366 for (j = std::max(0,l-m2+1);j<std::min(ny-1,l+m2);j++) {
367 res[i][j] = res[i][j] + wk[lsup+i-k][lsup+j-l]*h[k][l];
368 wgt[i][j] = wgt[i][j] + wk[lsup+i-k][lsup+j-l];
369 }
370 }
371 }
372 }
373 for (k = 0;k<nx-1;k++) {
374 for (l = 0;l<ny-1;l++) {
375 res[k][l] = res[k][l]/am1/am2;
376 if (wgt[k][l] != 0.) {res[k][l] = res[k][l]/wgt[k][l];}
377 }
378 }
379
380 input2D->Reset();
381 for (int iy = 1;iy<ny;iy++) {
382 for (int ix = 1;ix<nx;ix++) {
383 input2D->SetBinContent(ix,iy,res[ix-1][iy-1]);
384 }
385 }
386 for (i = 0; i < nx-1; i++){
387 delete[] h[i];
388 delete[] res[i];
389 }
390 delete[] h;
391 delete[] res;
392 }
393 if(debug) {
394 std::cout << "First (1-3, 1-3) 3x3 bins after smoothing: " << std::endl;
395 for(int i=1;i<4;i++) {
396 for(int j=1;j<4;j++) {
397 std::cout<<i<<" "<<j<<" : "<<input2D->GetBinContent(i,j)<< " / " ;
398 }
399 }
400 std::cout << std::endl;
401 int ioffset = input2D->GetNbinsX() / 2 ;
402 int joffset = input2D->GetNbinsY() / 2 ;
403 std::cout << "Middle (" << ioffset+1 << "-" << ioffset+4 << ", ("
404 << joffset+1 << "-" << joffset+4 << ") 3x3 bins after smoothing: " << std::endl;
405 for(int i=1;i<4;i++) {
406 for(int j=1;j<4;j++) {
407 std::cout<<i<<" "<<j<<" : "<<input2D->GetBinContent(i+ioffset,j+joffset)<< " / " ;
408 }
409 }
410 std::cout << std::endl;
411 }
412 }
413
414 void HistoHelperRoot::smoothASH3D(TH3* input3D, int m1, int m2, int m3, bool debug) {
415 if(debug) {
416 std::cout << "Smoothing a three dimensional histogram "<< input3D->GetName()
417 << " " << m1 << " " << m2 << " " << m3 << std::endl;
418 std::cout << "First 2x2x2 bins before smoothing: " << std::endl;
419 for(int i=1;i<3;i++) {
420 for(int j=1;j<3;j++) {
421 for(int k=1;k<3;k++) {
422 std::cout<<i<<" "<<j<<" "<<k<<" : "<<input3D->GetBinContent(i,j,k)<< " / " ;
423 }
424 }
425 }
426 std::cout << std::endl;
427 int ioffset = input3D->GetNbinsX() / 2 ;
428 int joffset = input3D->GetNbinsY() / 2 ;
429 int koffset = input3D->GetNbinsZ() / 2 ;
430 std::cout << "Middle (" << ioffset+1 << "-" << ioffset+3 << ", "
431 << joffset+1 << "-" << joffset+3 << ", "
432 << koffset+1 << "-" << koffset+3 << ") 2x2 bins before smoothing: " << std::endl;
433 for(int i=1;i<3;i++) {
434 for(int j=1;j<3;j++) {
435 for(int k=1;k<3;k++) {
436 std::cout<<i<<" "<<j<<" "<<k<<" : "<<input3D->GetBinContent(i+ioffset,j+joffset,k+koffset)<< " / " ;
437 }
438 }
439 }
440 std::cout << std::endl;
441 }
442
443 const int lsup = 20;
444 if (m1 > lsup || m2 > lsup || m3 > lsup) {
445 std::cout <<"HistoHelperRoot::smoothASH3D: m1, m2 or m3 too big !"<<std::endl;
446 return;
447 } else {
448 int nx = input3D->GetNbinsX()+1;
449 int ny = input3D->GetNbinsY()+1;
450 int nz = input3D->GetNbinsZ()+1;
451 float ***h, ***res;
452 h = new float**[nx-1];
453 res = new float**[nx-1];
454 for (int i = 0;i < nx-1; i++) {
455 h[i] = new float*[ny-1];
456 res[i] = new float*[ny-1];
457 for (int j = 0; j < ny-1; j++) {
458 h[i][j] = new float[nz-1];
459 res[i][j] = new float[nz-1];
460 }
461 }
462 for (int iz = 1;iz<nz;iz++) {
463 for (int iy = 1;iy<ny;iy++) {
464 for (int ix = 1;ix<nx;ix++) {
465 h[ix-1][iy-1][iz-1] = (float) input3D->GetBinContent(ix,iy,iz);
466 }
467 }
468 }
469
470 int i,j,k,l,m,n;
471 float wk1[41],wk2[41],wk3[41];
472
473 double wk[41][41][41],wks = 0.;
474 float ai,am1 = float(m1), am2 = float(m2), am3 = float(m3);
475 float am12 = am1*am1, am22 = am2*am2, am32 = am3*am3;
476
477 for (k = 0;k<nx-1;k++) {
478 for (l = 0;l<ny-1;l++) {
479 for (m = 0;m<nz-1;m++) {
480 res[k][l][m] = 0.;
481
482 }
483 }
484 }
485
486
487
488 float ***wgt;
489 int dimension = 100; int x, y, z = 0;
490 wgt = new float**[dimension];
491 if (!wgt) {
492 std::cout <<"HistoHelperRoot::smoothASH3D: problem to allocate memory, exiting"<<std::endl;
493 exit(-1);
494 }
495 for(x = 0; x < dimension; x++) {
496 wgt[x] = new float*[dimension];
497 if (!wgt) {
498 std::cout <<"HistoHelperRoot::smoothASH3D: problem to allocate memory, exiting"<<std::endl;
499 exit(-1);
500 }
501 for(y = 0; y < dimension; y++) {
502 wgt[x][y] = new float[dimension];
503 if (!wgt) {
504 std::cout <<"HistoHelperRoot::smoothASH3D: problem to allocate memory, exiting"<<std::endl;
505 exit(-1);
506 }
507 for(z = 0; z < dimension; z++)
508 wgt[x][y][z] = 0;
509 }
510 }
511
512 for (i = lsup+1-m1;i<lsup+m1;i++) {
513 ai = float(i-lsup)*float(i-lsup);
514 wk1[i] = 15./16.*(1.-ai/am12)*(1.-ai/am12);
515 wks = wks + wk1[i];
516 }
517 for (i = lsup+1-m1;i<lsup+m1;i++) {
518 wk1[i] = wk1[i]*am1/wks;
519 }
520 wks = 0.;
521 for (i = lsup+1-m2;i<lsup+m2;i++) {
522 ai = float(i-lsup)*float(i-lsup);
523 wk2[i] = 15./16.*(1.-ai/am22)*(1.-ai/am22);
524 wks = wks + wk2[i];
525 }
526 for (i = lsup+1-m2;i<lsup+m2;i++) {
527 wk2[i] = wk2[i]*am2/wks;
528 }
529 wks = 0.;
530 for (i = lsup+1-m3;i<lsup+m3;i++) {
531 ai = float(i-lsup)*float(i-lsup);
532 wk3[i] = 15./16.*(1.-ai/am32)*(1.-ai/am32);
533 wks = wks + wk3[i];
534 }
535 for (i = lsup+1-m3;i<lsup+m3;i++) {
536 wk3[i] = wk3[i]*am3/wks;
537 }
538
539 for (i = lsup+1-m1;i<lsup+m1;i++) {
540 for (j = lsup+1-m2;j<lsup+m2;j++) {
541 for (k = lsup+1-m3;k<lsup+m3;k++) {
542 wk[i][j][k] = wk1[i]*wk2[j]*wk3[k];
543 }
544 }
545 }
546
547 for (k = 0;k<nx-1;k++) {
548 for (l = 0;l<ny-1;l++) {
549 for (m = 0;m<nz-1;m++) {
550 for (i = std::max(0,k-m1+1);i<std::min(nx-1,k+m1);i++) {
551 for (j = std::max(0,l-m2+1);j<std::min(ny-1,l+m2);j++) {
552 for (n = std::max(0,m-m3+1);n<std::min(nz-1,m+m3);n++) {
553 res[i][j][n] = res[i][j][n] + wk[lsup+i-k][lsup+j-l][lsup+n-m]*h[k][l][m];
554 wgt[i][j][n] = wgt[i][j][n] + wk[lsup+i-k][lsup+j-l][lsup+n-m];
555 }
556 }
557 }
558 }
559 }
560 }
561 for (k = 0;k<nx-1;k++) {
562 for (l = 0;l<ny-1;l++) {
563 for (m = 0;m<nz-1;m++) {
564 res[k][l][m] = res[k][l][m]/am1/am2;
565 if (wgt[k][l][m] != 0.) {res[k][l][m] = res[k][l][m]/wgt[k][l][m];}
566 }
567 }
568 }
569
570 input3D->Reset();
571 for (int iz = 1;iz<nz;iz++) {
572 for (int iy = 1;iy<ny;iy++) {
573 for (int ix = 1;ix<nx;ix++) {
574 input3D->SetBinContent(ix,iy,iz,res[ix-1][iy-1][iz-1]);
575 }
576 }
577 }
578 for (i = 0; i < nx-1; i++){
579 for (j = 0; j < ny-1; j++) {
580 delete[] h[i][j];
581 delete[] res[i][j];
582 }
583 delete[] h[i];
584 delete[] res[i];
585 }
586 delete[] h;
587 delete[] res;
588
589 for (i = 0; i < dimension; i++){
590 for (j = 0; j < dimension; j++)
591 delete[] wgt[i][j];
592 delete[] wgt[i];
593 }
594 delete[] wgt;
595 }
596 if(debug) {
597 std::cout << "First 2x2x2 bins after smoothing: " << std::endl;
598 for(int i=1;i<3;i++) {
599 for(int j=1;j<3;j++) {
600 for(int k=1;k<3;k++) {
601 std::cout<<i<<" "<<j<<" "<<k<<" : "<<input3D->GetBinContent(i,j,k)<< " / " ;
602 }
603 }
604 }
605 std::cout << std::endl;
606 int ioffset = input3D->GetNbinsX() / 2 ;
607 int joffset = input3D->GetNbinsY() / 2 ;
608 int koffset = input3D->GetNbinsZ() / 2 ;
609 std::cout << "Middle (" << ioffset+1 << "-" << ioffset+3 << ", "
610 << joffset+1 << "-" << joffset+3 << ", "
611 << koffset+1 << "-" << koffset+3 << ") 2x2 bins after smoothing: " << std::endl;
612 for(int i=1;i<3;i++) {
613 for(int j=1;j<3;j++) {
614 for(int k=1;k<3;k++) {
615 std::cout<<i<<" "<<j<<" "<<k<<" : "<<input3D->GetBinContent(i+ioffset,j+joffset,k+koffset)<< " / " ;
616 }
617 }
618 }
619 std::cout << std::endl;
620 }
621 }
622
623
624
625
626
627 double HistoHelperRoot::Interpol1d(double x, TH1* h)
628 {
629 int bin((h->GetXaxis())->FindBin(x));
630 double bincenter(h->GetBinCenter(bin));
631 double nextbincenter(bincenter);
632 double nextbincontent(0);
633 if(x>bincenter && bin < h->GetNbinsX())
634 {
635 nextbincenter = h->GetBinCenter(bin+1);
636 nextbincontent = h->GetBinContent(bin+1);
637 }
638 else if(x<bincenter && bin > 1)
639 {
640 nextbincenter = h->GetBinCenter(bin-1);
641 nextbincontent = h->GetBinContent(bin-1);
642 }
643 double tmp(h->GetBinContent(bin));
644 if(nextbincenter != bincenter) tmp = ( nextbincontent*(x-bincenter) + tmp*(nextbincenter-x) ) / (nextbincenter - bincenter);
645 return tmp;
646 }
647
648
649
650
651 double HistoHelperRoot::Interpol2d(double x, double y, TH2* h)
652 {
653 int nx = h->GetNbinsX(), ny = h->GetNbinsY();
654 int ibx = h->GetXaxis()->FindBin(x), iby = h->GetYaxis()->FindBin(y);
655 int ibx2,iby2;
656 double z00,z11,z01,z10,xc,yc,xc2,yc2,u,t,r;
657 if (ibx > nx) ibx = nx; if (iby > ny) iby = ny;
658 xc = h->GetXaxis()->GetBinCenter(ibx);
659 yc = h->GetYaxis()->GetBinCenter(iby);
660 z11 = h->GetBinContent(ibx,iby);
661 if ( (ibx > 1 || (ibx == 1 && x > xc) ) &&
662 (ibx < nx || (ibx == nx && x < xc) ) ) {
663 if (x > xc) {ibx2 = ibx + 1;} else {ibx2 = ibx - 1;}
664 xc2 = h->GetXaxis()->GetBinCenter(ibx2);
665 if ( (iby > 1 || (iby == 1 && y > yc) ) &&
666 (iby < ny || (iby == ny && y < yc) ) ) {
667 if (y > yc) {iby2 = iby + 1;} else {iby2 = iby - 1;}
668 yc2 = h->GetYaxis()->GetBinCenter(iby2);
669 z00 = h->GetBinContent(ibx2,iby2);
670 z10 = h->GetBinContent(ibx,iby2);
671 z01 = h->GetBinContent(ibx2,iby);
672 t = (x - xc2)/(xc-xc2);
673 u = (y - yc2)/(yc-yc2);
674 r = z11*t*u+z00*(1.-t)*(1.-u)+z01*(1.-t)*u+z10*t*(1.-u);
675 } else {
676 z01 = h->GetBinContent(ibx2,iby);
677 t = (x - xc2)/(xc-xc2);
678 r = z11*t + z01*(1.-t);
679 }
680 } else if ((iby > 1 || (iby == 1 && y > yc) ) &&
681 (iby < ny || (iby == ny && y < yc) ) ) {
682 if (y > yc) {iby2 = iby + 1;} else {iby2 = iby - 1;}
683 z10 = h->GetBinContent(ibx,iby2);
684 yc2 = h->GetYaxis()->GetBinCenter(iby2);
685 u = (y - yc2)/(yc-yc2);
686 r = z11*u + z10*(1.-u);
687 } else {
688 r = z11;
689 }
690 return r;
691 }
692
693
694
695
696 double HistoHelperRoot::Interpol3d(double x, double y, double z, TH3* h)
697 {
698 int nx = h->GetNbinsX(), ny = h->GetNbinsY(), nz = h->GetNbinsZ();
699 int ibx = h->GetXaxis()->FindBin(x), iby = h->GetYaxis()->FindBin(y), ibz = h->GetZaxis()->FindBin(z);
700 int ibx2,iby2,ibz2;
701 double z000,z010,z110,z100,z001,z011,z111,z101,xc,yc,zc,xc2,yc2,zc2,u,t,v,r;
702 if (ibx > nx) ibx = nx; if (iby > ny) iby = ny; if (ibz > nz) ibz = nz;
703 xc = h->GetXaxis()->GetBinCenter(ibx);
704 yc = h->GetYaxis()->GetBinCenter(iby);
705 zc = h->GetZaxis()->GetBinCenter(ibz);
706
707 z111 = h->GetBinContent(ibx,iby,ibz);
708 if ( (ibx > 1 || (ibx == 1 && x > xc) ) &&
709 (ibx < nx || (ibx == nx && x < xc) ) ) {
710 if (x > xc) {ibx2 = ibx + 1;} else {ibx2 = ibx - 1;}
711 xc2 = h->GetXaxis()->GetBinCenter(ibx2);
712 if ( (iby > 1 || (iby == 1 && y > yc) ) &&
713 (iby < ny || (iby == ny && y < yc) ) ) {
714 if (y > yc) {iby2 = iby + 1;} else {iby2 = iby - 1;}
715 yc2 = h->GetYaxis()->GetBinCenter(iby2);
716 if ( (ibz > 1 || (ibz == 1 && z > zc) ) &&
717 (ibz < nz || (ibz == nz && z < zc) ) ) {
718 if (z > zc) {ibz2 = ibz + 1;} else {ibz2 = ibz - 1;}
719 zc2 = h->GetZaxis()->GetBinCenter(ibz2);
720
721 z000 = h->GetBinContent(ibx2,iby2,ibz2);
722 z100 = h->GetBinContent(ibx,iby2,ibz2);
723 z010 = h->GetBinContent(ibx2,iby,ibz2);
724 z110 = h->GetBinContent(ibx,iby,ibz2);
725 z001 = h->GetBinContent(ibx2,iby2,ibz);
726 z101 = h->GetBinContent(ibx,iby2,ibz);
727 z011 = h->GetBinContent(ibx2,iby,ibz);
728 t = (x - xc2)/(xc-xc2);
729 u = (y - yc2)/(yc-yc2);
730 v = (z - zc2)/(zc-zc2);
731 r = z111*t*u*v + z001*(1.-t)*(1.-u)*v
732 + z011*(1.-t)*u*v + z101*t*(1.-u)*v
733 + z110*t*u*(1.-v) + z000*(1.-t)*(1.-u)*(1.-v)
734 + z010*(1.-t)*u*(1.-v) + z100*t*(1.-u)*(1.-v);
735 } else {
736 z011 = h->GetBinContent(ibx2,iby,ibz);
737 z001 = h->GetBinContent(ibx2,iby2,ibz);
738 z101 = h->GetBinContent(ibx,iby2,ibz);
739 t = (x - xc2)/(xc-xc2);
740 u = (y - yc2)/(yc-yc2);
741 r = z111*t*u + z011*(1.-t)*u + z101*t*(1.-u) + z001*(1.-t)*(1.-u) ;
742 }
743 } else if ((ibz > 1 || (ibz == 1 && z > zc) ) &&
744 (ibz < nz || (ibz == nz && z < zc) ) ) {
745 if (z > zc) {ibz2 = ibz + 1;} else {ibz2 = ibz - 1;}
746 z110 = h->GetBinContent(ibx,iby,ibz2);
747 z010 = h->GetBinContent(ibx2,iby,ibz2);
748 z011 = h->GetBinContent(ibx2,iby,ibz);
749 zc2 = h->GetYaxis()->GetBinCenter(ibz2);
750 t = (x - xc2)/(xc-xc2);
751 v = (z - zc2)/(zc-zc2);
752 r = z111*t*v + z011*(1.-t)*v + z110*t*(1.-v) +z010*(1.-t)*(1.-v);
753 } else {
754 z011 = h->GetBinContent(ibx2,iby,ibz);
755 t = (x - xc2)/(xc-xc2);
756 r = z111*t + z011*(1.-t);
757 }
758 } else {
759 if ( (iby > 1 || (iby == 1 && y > yc) ) &&
760 (iby < ny || (iby == ny && y < yc) ) ) {
761 if (y > yc) {iby2 = iby + 1;} else {iby2 = iby - 1;}
762 yc2 = h->GetYaxis()->GetBinCenter(iby2);
763 if ( (ibz > 1 || (ibz == 1 && z > zc) ) &&
764 (ibz < nz || (ibz == nz && z < zc) ) ) {
765 if (z > zc) {ibz2 = ibz + 1;} else {ibz2 = ibz - 1;}
766 zc2 = h->GetZaxis()->GetBinCenter(ibz2);
767
768 z100 = h->GetBinContent(ibx,iby2,ibz2);
769 z110 = h->GetBinContent(ibx,iby,ibz2);
770 z101 = h->GetBinContent(ibx,iby2,ibz);
771 u = (y - yc2)/(yc-yc2);
772 v = (z - zc2)/(zc-zc2);
773 r = z111*u*v + z101*(1.-u)*v
774 + z110*u*(1.-v) + z100*(1.-u)*(1.-v);
775 } else {
776 z101 = h->GetBinContent(ibx,iby2,ibz);
777 u = (y - yc2)/(yc-yc2);
778 r = z111*u + z101*(1.-u);
779 }
780 } else if ((ibz > 1 || (ibz == 1 && z > zc) ) &&
781 (ibz < nz || (ibz == nz && z < zc) ) ) {
782 if (z > zc) {ibz2 = ibz + 1;} else {ibz2 = ibz - 1;}
783 z110 = h->GetBinContent(ibx,iby,ibz2);
784 zc2 = h->GetYaxis()->GetBinCenter(ibz2);
785 v = (z - zc2)/(zc-zc2);
786 r = z111*v + z110*(1.-v);
787 } else {
788 r = z111;
789 }
790 }
791 return r;
792 }
793
794 }
| Due to the LXR bug, the updates fail sometimes to remove references to deleted files. The Saturday's full rebuilds fix these problems |
|
This page was automatically generated by the
LXR engine.
|
|