Subversion Repositories f9daq

Rev

Blame | Last modification | View Log | RSS feed

#ifdef _CVI_
#  include <ansi_c.h>
# else _CVI_
#  include <stdlib.h>
#  include <stdio.h>
#  include <string.h>
#endif _CVI_

#include "H1D.h"

#define H1DMAX 500
H1D *h1[H1DMAX];
//int Printf(char *format, ...);

int H1DClear(int h1d) {
  if (!h1[h1d]) return -1;
  memset(h1[h1d]->data, 0,h1[h1d]->size );
  h1[h1d]->min=0;
  h1[h1d]->max=0;
  h1[h1d]->nentries=0;
  return  0;

}

int H1DPrint(int h1d) {
  if (!h1[h1d]) return -1;
//Printf("PrintH1D nx=%d minx=%f stepx=%f ny=%d miny=%f stepy=%f size=%d\n", h1[h1d]->nx, h1[h1d]->minx, h1[h1d]->stepx, h1[h1d]->ny, h1[h1d]->miny, h1[h1d]->stepy, h1[h1d]->size ) ;
  return  0;

}

int H1DExist(int h) {
  if (h1[h]) return 1;
  else return 0;
}


int H1DGetBin(int h, double value) {
  int nx,xmin,dx;
  int bin;

  nx = H1DGetNbinsX(h);
  xmin= H1DGetMinX(h);
  dx = H1DGetStepX(h);

  if (dx==0) return -1;
  if (value<xmin) return -1;
  bin = (int)((value-xmin)/dx);
  if (bin>=nx) return -1;
  else return bin;
}

int H1DFill(int h1d,double x, double val) {

  int ix;
  if (!h1[h1d]) return -1;

  ix = H1DGetBin(h1d,x);
  if (ix<0) return ix;

  h1[h1d]->data[ix]+=val;
//Printf("%d %d data %f %f\n",x,y,val, h1[h1d]->data[idx]);
  if (h1[h1d]->data[ix]>h1[h1d]->max) h1[h1d]->max= h1[h1d]->data[ix];
  if (h1[h1d]->data[ix]<h1[h1d]->min) h1[h1d]->min= h1[h1d]->data[ix];
  h1[h1d]->nentries++;
  return 0;
}


int H1DFillBin(int h1d,int x, double val) {

  if (!h1[h1d]) {
//Printf("FillH1D error h1d is not initialized\n");
    return -1;
  }

  h1[h1d]->data[x]+=val;
//Printf("%d %d data %f %f\n",x,y,val, h1[h1d]->data[idx]);
  if (h1[h1d]->data[x]>h1[h1d]->max) h1[h1d]->max= h1[h1d]->data[x];
  if (h1[h1d]->data[x]<h1[h1d]->min) h1[h1d]->min= h1[h1d]->data[x];
  h1[h1d]->nentries++;
  return 0;
}

int H1DSetBinContent(int h1d,int x, double val) {

  if (!h1[h1d]) {
//Printf("FillH1D error h1d is not initialized\n");
    return -1;
  }

  h1[h1d]->data[x]=val;
//Printf("%d %d data %f %f\n",x,y,val, h1[h1d]->data[idx]);
  if (h1[h1d]->data[x]>h1[h1d]->max) h1[h1d]->max= h1[h1d]->data[x];
  if (h1[h1d]->data[x]<h1[h1d]->min) h1[h1d]->min= h1[h1d]->data[x];
  h1[h1d]->nentries++;
  return 0;
}



double H1DGetBinContent(int h1d,int atx) {

  int idx;
  if (!h1[h1d]) return 0;
  if (h1[h1d]->nx <= atx)  return 0;
  if (atx<0)  return 0;
  if (atx*sizeof(double) < h1[h1d]->size ) return h1[h1d]->data[atx];


  return 0;
}


int H1DInit(int h1d,char *name, char *title,int nx, double minx, double stepx) {

  if (h1[h1d]) {

    free(h1[h1d]->data);
    free(h1[h1d]);
    h1[h1d] = NULL;
  }
  // if (h1d!=H1DMAX-1) printf("InitH1D hID=%d\n",h1d);
  h1[h1d] = (H1D *) malloc(sizeof(H1D));
//h2  =h1d;

  H1DSetTitle(h1d,title);
  H1DSetName(h1d,name);
  H1DSetTitleX(h1d,"x");
  ;
  h1[h1d]->id=H1D_ID;
  h1[h1d]->nx = nx;


  h1[h1d]->minx = minx;

  h1[h1d]->stepx = stepx;

  h1[h1d]->size = h1[h1d]->nx*sizeof(double);
  h1[h1d]->data = (double *) malloc(h1[h1d]->size);
  h1[h1d]->len=sizeof(H1D)-sizeof(double *)+h1[h1d]->size;
  H1DClear(h1d);
  H1DPrint(h1d);
//Printf("InitH1D 0x%x\n", h1d );
  return  0;

}


double H1DGetXBinCenter(int h1d,int xbin) {
  return h1[h1d]->minx+xbin*h1[h1d]->stepx;
}


int H1DWrite2File(int h1d,FILE *fp) {

  if (!fp) return -1;
//printf("H1D sizeof(H1D)=%lu len-datasize=%d len=%lu datasize=%d\t",sizeof(H1D)-sizeof(double *),h1[h1d]->len-h1[h1d]->size,h1[h1d]->len,h1[h1d]->size);
//printf("H1D sz=%d %d\n",sizeof(double),sizeof(double *));
  if (!H1DExist(h1d)){
    printf("Histogram H1D=%d is not initialized\n",h1d);
    return -1;
  }
  fwrite (h1[h1d], 1, sizeof(H1D)-sizeof(double *), fp);
  fwrite (h1[h1d]->data, 1, h1[h1d]->size, fp);
  return  0;
}

int H1DWrite(int h1d,const char *fname,const char *opt) {
  FILE *fp=fopen(fname,opt);
  H1DWrite2File(h1d,fp);
  fclose(fp);
  return  0;
}




int H1DSetTitle(int h1d,char *title) {
  if (!h1[h1d]) {
    printf("h1d %d does not exist %s\n",h1d, title);
    return 0;
  }
  sprintf(h1[h1d]->title,"%s",title);
  return 0;
}


int H1DSetTitleX(int h1d,char *title) {
  sprintf(h1[h1d]->titlex,"%s",title);
  return 0;
}


int H1DSetTitleY(int h1d,char *title) {
  sprintf(h1[h1d]->titley,"%s",title);
  return 0;
}


char * H1DGetTitleX(int h1d) {
  return h1[h1d]->titlex;
}

char * H1DGetTitleY(int h1d) {
  return h1[h1d]->titley;
}

char * H1DGetTitle(int h1d) {
  return h1[h1d]->title;
}



int H1DSetName(int h1d,char *title) {
  sprintf(h1[h1d]->name,"%s",title);
  return 0;
}

int H1DGetNbinsX(int h) {
  if (h1[h]) return h1[h]->nx;
  else return 0;
}



double H1DGetMinX(int h) {
  if (h1[h]) return h1[h]->minx;
  else return 0;
}



double H1DGetStepX(int h) {
  if (h1[h]) return h1[h]->stepx;
  else return 0;
}


double H1DGetMin(int h) {
  if (h1[h]) return h1[h]->min;
  else return 0;
}

double H1DGetMax(int h) {
  if (h1[h]) return h1[h]->max;
  else return 0;
}

double * H1DGetData(int h) {
  if (h1[h]) return h1[h]->data;
  else return NULL;
}