#include "TScatTable.h" #include #include #include ClassImp(TScatTable); //double interpvals[3][3][3][3][3][3]; TScatTable::TScatTable(){ } TScatTable::TScatTable(TScatTable* tst){ // std::cout << "Inside copy constructor, (nbs,abs) have size (" << nbs.size() << "," << abs.size() << ")" << std::endl; TString name = tst->GetName(); TString title = tst->GetTitle(); name += "_2"; title += "_2"; Initialize(name,title, tst->GetNBins(0),tst->GetAxisBound(0,0),tst->GetAxisBound(0,1), tst->GetNBins(1),tst->GetAxisBound(1,0),tst->GetAxisBound(1,1), tst->GetNBins(2),tst->GetAxisBound(2,0),tst->GetAxisBound(2,1), tst->GetNBins(3),tst->GetAxisBound(3,0),tst->GetAxisBound(3,1), tst->GetNBins(4),tst->GetAxisBound(4,0),tst->GetAxisBound(4,1), tst->GetNBins(5),tst->GetAxisBound(5,0),tst->GetAxisBound(5,1)); for (int iele=0; ieleGetElement(iele); } } TScatTable::TScatTable(const char *name, const char *title, int nzsbins, double minzs, double maxzs, int nrsbins, double minrs, double maxrs, int ntbins, double mint, double maxt, int nastbins, double minast, double maxast, int nctbins, double minct, double maxct, int nphibins, double minphi, double maxphi){ Initialize(name, title, nzsbins, minzs, maxzs, nrsbins, minrs, maxrs, ntbins, mint, maxt, nastbins, minast, maxast, nctbins, minct, maxct, nphibins, minphi, maxphi); } TScatTable::~TScatTable(){ } bool TScatTable::Initialize(const char *name, const char *title, int nzsbins, double minzs, double maxzs, int nrsbins, double minrs, double maxrs, int ntbins, double mint, double maxt, int nastbins, double minast, double maxast, int nctbins, double minct, double maxct, int nphibins, double minphi, double maxphi){ SetName(name); SetTitle(title); bool badentries = false; if (nzsbins<=0) { std::cerr << "ERROR in TScatTable::Initialize: nzsbins = " << nzsbins << ", which is <= 0" << std::endl; badentries = true; } if (nrsbins<=0) { std::cerr << "ERROR in TScatTable::Initialize: nrsbins = " << nrsbins << ", which is <= 0" << std::endl; badentries = true; } if (ntbins<=0) { std::cerr << "ERROR in TScatTable::Initialize: ntbins = " << ntbins << ", which is <= 0" << std::endl; badentries = true; } if (nastbins<=0) { std::cerr << "ERROR in TScatTable::Initialize: nastbins = " << nastbins << ", which is <= 0" << std::endl; badentries = true; } if (nctbins<=0) { std::cerr << "ERROR in TScatTable::Initialize: nctbins = " << nctbins << ", which is <= 0" << std::endl; badentries = true; } if (nphibins<=0) { std::cerr << "ERROR in TScatTable::Initialize: nphibins = " << nphibins << ", which is <= 0" << std::endl; badentries = true; } if (badentries) { return badentries; } int idim = 0; nbins[idim] = nzsbins; axisbounds[idim][0] = minzs; axisbounds[idim][1] = maxzs; idim = 1; nbins[idim] = nrsbins; axisbounds[idim][0] = minrs; axisbounds[idim][1] = maxrs; idim = 2; nbins[idim] = ntbins; axisbounds[idim][0] = mint; axisbounds[idim][1] = maxt; idim = 3; nbins[idim] = nastbins; axisbounds[idim][0] = minast; axisbounds[idim][1] = maxast; idim = 4; nbins[idim] = nctbins; axisbounds[idim][0] = minct; axisbounds[idim][1] = maxct; idim = 5; nbins[idim] = nphibins; axisbounds[idim][0] = minphi; axisbounds[idim][1] = maxphi; nbinstot = 1; for (idim=0; idim 0) { nbinstot *= nbins[idim]; } } std::cout << "Scattering table has " << nbinstot << " elements." << std::endl; fillbininfo(); table.clear(); table.resize(nbinstot,0.); for (int iele=0; iele<=nbinstot; iele++) { table[iele] = 0.; } return badentries; } void TScatTable::fillbininfo(){ gbinincr[0]=1; gbinincr[1]=nbins[0]; gbinincr[2]=nbins[0]*nbins[1]; gbinincr[3]=nbins[0]*nbins[1]*nbins[2]; gbinincr[4]=nbins[0]*nbins[1]*nbins[2]*nbins[3]; gbinincr[5]=nbins[0]*nbins[1]*nbins[2]*nbins[3]*nbins[4]; for (int idim=0; idimmaxnbins) { std::cout << "nbins exceeds maxnbins!" << std::endl; exit(-1); } bnwdth[idim]=(axisbounds[idim][1]-axisbounds[idim][0])/nbins[idim]; for (int j=0; j=0; idim--) { if (subindices[idim] >= nbins[idim]) { std::cerr << "ERROR in TScatTable::GetIndex: subindex " << idim << " (" << subindices[idim] << ") is out of bounds (range = [0," << nbins[idim]-1 << "])" << std::endl; char a[10]; std::cin >> a; return -1; } if (idim < maxdimensions-1) { index *= nbins[idim]; } index += subindices[idim]; } return index; } int TScatTable::GetIndexFast(int* subindices){ int index=0; for (int idim=maxdimensions-1; idim>=0; idim--) { index *= nbins[idim]; index += subindices[idim]; } return index; } int TScatTable::GetIndex(int izs, int irs, int it, int iast, int ict, int iphi){ int subindices[maxdimensions] = {izs, irs, it, iast, ict, iphi}; return GetIndex(subindices); } void TScatTable::GetSubIndices(int* subindices, int iglobalindex){ for (int idim=0; idim 0) { prevnbins *= nbins[idim-1]; } subindices[idim] = ( (iglobalindex - GetIndex(subindices)) / prevnbins ) % nbins[idim]; } } bool TScatTable::IsInABin(double zs, double rs, double t, double ast, double ct, double phi){ double inputs[maxdimensions] = {zs, rs, t, ast, ct, phi}; for (int idim=0; idim axisbounds[idim][1])||(inputs[idim] < axisbounds[idim][0])) { return false; } } return true; } double TScatTable::GetBinCenter(int idimension, int ibin) { if (nbins[idimension] <= 1) { return 0.; } return axisbounds[idimension][0] + (ibin + 0.5)*(axisbounds[idimension][1] - axisbounds[idimension][0]); } void TScatTable::FindClosestSubBins(int* ibinarray, double zs, double rs, double t, double ast, double ct, double phi){ double val[maxdimensions] = {zs, rs, t, ast, ct, phi}; for (int idim=0; idim= axisbounds[idim][1]){ ibinarray[idim] = nbins[idim] - 1; continue; } else if (val[idim] < axisbounds[idim][0]) { ibinarray[idim] = 0; continue; } double binwidth = (axisbounds[idim][1] - axisbounds[idim][0])/nbins[idim]; for (ibinarray[idim]=0; ibinarray[idim]= maxdimensions) { std::cerr << "ERROR in TScatTable::FindSurroundingBins: idim (=" << idim << ") > maxdimensions (=" << maxdimensions << ")" << std::endl; min = 0; max = 0; return; } if (nbins[idim] <= 1) { min = 0; min = 0; // std::cout << "!!!!! FSB: <= 1 bin, returning zeros" << std::endl; return; } else if (val < axisbounds[idim][0]) { min = 0; max = 1; // std::cout << "!!!!! FSB: val (" << val << ") < low bound (" << axisbounds[idim][0] << "), returning (0,1)" << std::endl; return; } else if (val > axisbounds[idim][1]) { min = nbins[idim]-2; max = nbins[idim]-1; // std::cout << "!!!!! FSB: val (" << val << ") > high bound (" << axisbounds[idim][1] << "), returning (" << min << "," << max << ")" << std::endl; return; } double binwidth = (axisbounds[idim][1] - axisbounds[idim][0])/nbins[idim]; for (int ibin=1; ibin 0) { int index = GetIndex(izs, irs, it, iast, ict, iphi); if (index >= 0) { table[index] = val; } } else { std::cerr << "ERROR in TScatTable::SetElement: table has not yet been initialized" << std::endl; } } void TScatTable::SetElement(int izs, int irs, int it, int iast, double val){ SetElement(izs, irs, it, iast, 0, 0, val); } double TScatTable::GetElement(int iglobalindex){ return table[iglobalindex]; } double TScatTable::GetElement(int izs, int irs, int it, int iast, int ict, int iphi){ int subindices[maxdimensions] = {izs, irs, it, iast, ict, iphi}; return GetElement(subindices); } double TScatTable::GetElement(int* subindices){ if (table.size() > 0) { int index = GetIndex(subindices); if (index >= 0) { return table[index]; } } else { std::cerr << "ERROR in TScatTable::GetElement: table has not yet been initialized" << std::endl; } return -1.; } double TScatTable::GetElement6DFast(int izs, int irs, int it, int iast, int ict, int iphi){ int index = izs + nbins[0]*(irs + nbins[1]*(it + nbins[2]*(iast + nbins[3]*(ict + nbins[4]*(iphi))))); return table[index]; } double TScatTable::GetElement6DFast(int* subindices){ int index = subindices[0] + nbins[0]*(subindices[1] + nbins[1]*(subindices[2] + nbins[2]*(subindices[3] + nbins[3]*(subindices[4] + nbins[4]*(subindices[5]))))); return table[index]; } double TScatTable::GetInterpVal(double zs, double rs, double t, double ast, double ct, double phi){ double vals[maxdimensions] = {zs, rs, t, ast, ct, phi}; // for (int idim=0; idim axisbounds[idim][1])) { // std::cout << "For idim " << idim << ", val," << vals[idim] << ", is out of bounds [" << axisbounds[idim][0]<< "," << axisbounds[idim][1] << "]" << std::endl; // } // } int ibins[maxdimensions]; double binwidths[maxdimensions]; double highedges[maxdimensions]; for (int idim=0; idim highcenter) { vals[idim] = highcenter; ibins[idim] = nbins[idim] - 1; highedges[idim] = highbound; continue; } for (ibins[idim]=0; ibins[idim] x2) { return y2; } else { return (y2 - y1) / (x2 - x1) * (x - x1) + y1; } } void TScatTable::Fill(double zs, double rs, double t, double ast, double ct, double phi, double wgt){ double vals[maxdimensions] = {zs, rs, t, ast, ct, phi}; for (int idim=0; idim axisbounds[idim][1])||(vals[idim] < axisbounds[idim][0])) { if (idim >= 4) { std::cout << "value (" << vals[idim] << ") outside boundaries [" << axisbounds[idim][0] << "," << axisbounds[idim][1] << "] in dimension " << idim << std::endl; } return; } } int ibins[maxdimensions]; FindClosestSubBins(ibins, zs, rs, t, ast, ct, phi); int index = GetIndex(ibins); // std::cout << "index = " << index << std::endl; if (index >= 0) { table[index] += wgt; } } void TScatTable::Fill(double zs, double rs, double t, double ast, double wgt){ Fill(zs, rs, t, ast, 0, 0, wgt); } TH1F* TScatTable::MakeTH1F(const char *name, const char *title, int subindex){ if ((subindex >= maxdimensions)||(subindex < 0)) { std::cerr << "ERROR in TScatTable::MakeTH1F: subindex (" << subindex << ") is out of bounds" << std::endl; return NULL; } TH1F* histo = new TH1F(name, title, nbins[subindex], axisbounds[subindex][0], axisbounds[subindex][1]); for (int igbin=0; igbinSetBinContent(subindices[subindex]+1,histo->GetBinContent(subindices[subindex]+1)+table[igbin]); } return histo; } TH2F* TScatTable::MakeTH2F(const char *name, const char *title, int subindex1, int subindex2){ if ((subindex1 >= maxdimensions)||(subindex1 < 0)) { std::cerr << "ERROR in TScatTable::MakeTH2F: subindex1 (" << subindex1 << ") is out of bounds" << std::endl; return NULL; } if ((subindex2 >= maxdimensions)||(subindex2 < 0)) { std::cerr << "ERROR in TScatTable::MakeTH2F: subindex2 (" << subindex2 << ") is out of bounds" << std::endl; return NULL; } TH2F* histo = new TH2F(name, title, nbins[subindex1], axisbounds[subindex1][0], axisbounds[subindex1][1], nbins[subindex2], axisbounds[subindex2][0], axisbounds[subindex2][1]); for (int igbin=0; igbinSetBinContent(subindices[subindex1]+1,subindices[subindex2]+1,histo->GetBinContent(subindices[subindex1]+1,subindices[subindex2]+1)+table[igbin]); } return histo; } TScatTable* TScatTable::Make4DScatTable(){ TString tname = GetName(); tname += "4d"; TString ttitle = GetTitle(); ttitle += " 4D Projection"; TScatTable* tst = new TScatTable(tname,ttitle, nbins[0],axisbounds[0][0],axisbounds[0][1], nbins[1],axisbounds[1][0],axisbounds[1][1], nbins[2],axisbounds[2][0],axisbounds[2][1], nbins[3],axisbounds[3][0],axisbounds[3][1]); for (int igbin=0; igbinGetElement(sis[0],sis[1],sis[2],sis[3]); tst->SetElement(sis[0],sis[1],sis[2],sis[3],currentval+fillval); } return tst; } TScatTable* TScatTable::MakeNormalized4DScatTable(){ // int nconsolidatedbins = nbins[4] + nbins[5]; should be multiplication int nconsolidatedbins = nbins[4] * nbins[5]; TScatTable* t4d = Make4DScatTable(); for (int ibin=0; ibinGetNBinsTot(); ibin++) { t4d->SetElement(ibin,t4d->GetElement(ibin)/nconsolidatedbins); } return t4d; } int TScatTable::GetNBinsTot(){ return nbinstot; } int TScatTable::GetNBins(int idim){ return nbins[idim]; } double TScatTable::GetAxisBound(int idim, int ibound){ return axisbounds[idim][ibound]; } double TScatTable::GetIntegral(){ double sum = 0.; for (int ibin=0; ibinGetNBinsTot()) { std::cerr << "ERROR in TScatTable::Add. Both scattering tables must be the same length" << std::endl; return; } for (int ibin=0; ibinGetElement(ibin); } } void TScatTable::Subtract(TScatTable* tst, int cutValue){ if (nbinstot != tst->GetNBinsTot()) { std::cerr << "ERROR in TScatTable::Subtract. Both scattering tables must be the same length" << std::endl; return; } int nmoredirect = 0; for (int ibin=0; ibinGetElement(ibin) < cutValue)) { table[ibin] = 0.; continue; } if (table[ibin] < tst->GetElement(ibin)) { // std::cout << "Strange: In TScatTable::Subtract (a - b), a = " << table[ibin] << " and b = " << tst->GetElement(ibin) << std::endl; nmoredirect++; table[ibin] = 0.; continue; } table[ibin] -= tst->GetElement(ibin); } std::cout << "In TScatTable::Subtract, found more direct light than scattered light " << nmoredirect << " times." << std::endl; } void TScatTable::Divide4D(TScatTable* tst){ for (int ibin=0; ibinGetElement(sis[0],sis[1],sis[2],sis[3]); if (denom <= 0.) { table[ibin] = 0.; continue; } table[ibin] /= denom; } } void TScatTable::DivideUnnormalized4D(TScatTable* tst){ int nconsolidatedbins = nbins[4] * nbins[5]; for (int ibin=0; ibinGetElement(sis[0],sis[1],sis[2],sis[3])/nconsolidatedbins; if (denom <= 0.) { table[ibin] = 0.; continue; } table[ibin] /= denom; } } template < typename T > void TScatTable::PrintVector(std::vector vec){ std::cout << "("; int vsize = vec.size(); for (int iele=0; ieleFill(table[ibin]); } return hnphot; } void TScatTable::CheckDiff(TScatTable* tst){ if (nbinstot != tst->GetNBinsTot()){ std::cout << "These 2 tables have a different number of bins" << std::endl; return; } for (int ibin=0; ibinGetElement(ibin)) > 1.e-5) { int sis[maxdimensions]; GetSubIndices(sis,ibin); std::cout << "Index " << ibin << " (" << sis[0] << "," << sis[1] << "," << sis[2] << "," << sis[3] << "," << sis[4] << "," << sis[5] << ") has values of " << table[ibin] << " and " << tst->GetElement(ibin) << std::endl; } } }