#include "THealPixCube.h"
#include "THealUtil.h"
ClassImp(THealPixCube)
THealPixCube::THealPixCube(): TNamed()
{
fN = 0;
fHeals = 0;
fMaximum = -1111;
fMinimum = -1111;
fWaxis.SetName("waxis");
fWaxis.SetParent(this);
}
THealPixCube::THealPixCube(const char* name, const char* title,
Int_t nbins, Double_t wlow, Double_t wup)
: TNamed(name, title)
{
fN = (nbins <= 0) ? 1 : nbins;
fWaxis.Set(fN, wlow, wup);
fHeals = new THealPix*[fN];
for(Int_t i = 0; i < fN; i++){
fHeals[i] = 0;
}
fMaximum = -1111;
fMinimum = -1111;
}
THealPixCube::~THealPixCube()
{
for(Int_t i = 0; i < fN; i++){
SafeDelete(fHeals[i]);
}
delete [] fHeals;
fHeals = 0;
}
THealPix* THealPixCube::GetSlice(Int_t n) const
{
if(n < 0 || n >= fN){
return 0;
}
return fHeals[n];
}
THealPix* THealPixCube::operator[](Int_t n) const
{
if(n < 0 || n >= fN){
return 0;
}
return fHeals[n];
}
ClassImp(THealPixCubeF)
THealPixCubeF::THealPixCubeF(): THealPixCube()
{
}
THealPixCubeF::~THealPixCubeF()
{
}
THealPixCubeF::THealPixCubeF(const char* name, const char* title, Int_t order,
Int_t nbins, Double_t wlow, Double_t wup,
Bool_t nested)
: THealPixCube(name, title, nbins, wlow, wup)
{
for(Int_t i = 0; i < fN; i++){
fHeals[i] = new THealPixF(Form("%s%d", name, i), title, order, nested);
}
}
Int_t THealPixCubeF::GetType() const
{
return TFLOAT;
}
std::string THealPixCubeF::GetTypeString() const
{
return "E";
}
THealPixCubeF* THealPixCubeF::ReadFits(const char* fname, const char* colname)
{
THealPix::HealHeader_t head;
fitsfile* fptr = 0;
if(!THealPix::ReadFitsHeader(&fptr, fname, colname, head)){
return 0;
}
THealPixCubeF* hpcf;
if(head.nrows*head.repeat == head.npix){
hpcf = new THealPixCubeF(fname, colname, head.order, 1, 0, 0, head.isNested);
} else if(head.nrows == head.npix){
hpcf = new THealPixCubeF(fname, colname, head.order, head.repeat, 0, 0, head.isNested);
} else {
return 0;
}
for(Int_t i = 0; i < hpcf->GetN(); i++){
(*hpcf)[i]->SetUnit(head.tunit);
}
Int_t status = 0;
if(head.nrows*head.repeat == head.npix){
for(Int_t i = 0; i < head.nrows; i++){
fits_read_col(fptr, TFLOAT, head.colnum, i + 1, 1, head.repeat, 0,
&(((THealPixF*)((*hpcf)[0]))->GetArray()[i*head.repeat]),
0, &status);
if(!THealUtil::FitsReportError(status)){
delete hpcf;
return 0;
}
}
} else if(head.nrows == head.npix){
for(Int_t i = 0; i < head.nrows; i++){
for(Int_t j = 0; j < hpcf->GetN(); j++){
fits_read_col(fptr, TFLOAT, head.colnum, i + 1, j + 1, 1, 0,
&(((THealPixF*)((*hpcf)[j]))->GetArray()[i]), 0, &status);
if(!THealUtil::FitsReportError(status)){
delete hpcf;
return 0;
}
}
}
}
for(Int_t i = 0; i < hpcf->GetN(); i++){
Double_t entries = 0;
for(Int_t j = 0; j < (*hpcf)[i]->GetNpix(); j++){
if((*hpcf)[i]->GetBinContent(j) != 0){
entries++;
}
}
(*hpcf)[i]->SetEntries(entries);
}
return hpcf;
}
ClassImp(THealPixCubeD)
THealPixCubeD::THealPixCubeD(): THealPixCube()
{
}
THealPixCubeD::~THealPixCubeD()
{
}
THealPixCubeD::THealPixCubeD(const char* name, const char* title, Int_t order,
Int_t nbins, Double_t wlow, Double_t wup,
Bool_t nested)
: THealPixCube(name, title, nbins, wlow, wup)
{
for(Int_t i = 0; i < fN; i++){
fHeals[i] = new THealPixD(Form("%s%d", name, i), title, order, nested);
}
}
Int_t THealPixCubeD::GetType() const
{
return TDOUBLE;
}
std::string THealPixCubeD::GetTypeString() const
{
return "D";
}
THealPixCubeD* THealPixCubeD::ReadFits(const char* fname, const char* colname)
{
THealPix::HealHeader_t head;
fitsfile* fptr = 0;
if(!THealPix::ReadFitsHeader(&fptr, fname, colname, head)){
return 0;
}
THealPixCubeD* hpcd;
if(head.nrows*head.repeat == head.npix){
hpcd = new THealPixCubeD(fname, colname, head.order, 1, 0, 0, head.isNested);
} else if(head.nrows == head.npix){
hpcd = new THealPixCubeD(fname, colname, head.order, head.repeat, 0, 0, head.isNested);
} else {
return 0;
}
for(Int_t i = 0; i < hpcd->GetN(); i++){
(*hpcd)[i]->SetUnit(head.tunit);
}
Int_t status = 0;
if(head.nrows*head.repeat == head.npix){
for(Int_t i = 0; i < head.nrows; i++){
fits_read_col(fptr, TDOUBLE, head.colnum, i + 1, 1, head.repeat, 0,
&(((THealPixD*)((*hpcd)[0]))->GetArray()[i*head.repeat]),
0, &status);
if(!THealUtil::FitsReportError(status)){
delete hpcd;
return 0;
}
}
} else if(head.nrows == head.npix){
for(Int_t i = 0; i < head.nrows; i++){
for(Int_t j = 0; j < hpcd->GetN(); j++){
fits_read_col(fptr, TDOUBLE, head.colnum, i + 1, j + 1, 1, 0,
&(((THealPixD*)((*hpcd)[j]))->GetArray()[i]), 0, &status);
if(!THealUtil::FitsReportError(status)){
delete hpcd;
return 0;
}
}
}
}
for(Int_t i = 0; i < hpcd->GetN(); i++){
Double_t entries = 0;
for(Int_t j = 0; j < (*hpcd)[i]->GetNpix(); j++){
if((*hpcd)[i]->GetBinContent(j) != 0){
entries++;
}
}
(*hpcd)[i]->SetEntries(entries);
}
return hpcd;
}