home *** CD-ROM | disk | FTP | other *** search
- #include <string.h>
- #include <stdlib.h>
- #include <math.h>
- #include "hippo.h"
- #ifdef VM
- #include "h_util.h"
- #else
- #include "hippoutil.h"
- #endif
-
- GLOB_QUAL const char hippoutil_c_rcsid[] = "$Id: hippoutil.c,v 3.3 1992/03/21 01:52:25 rensing Rel $";
-
- /*
- * Structures for the function registry.
- */
-
- struct func_strt {
- char name[FUNCNAMELEN+1];
- void *func;
- };
-
- static struct func_strt f_list1[] = {
- { "h_cut_lt", h_cut_lt },
- { "h_cut_gt", h_cut_gt },
- { "h_cut_le", h_cut_le },
- { "h_cut_ge", h_cut_ge },
- { "h_cut_inside", h_cut_inside },
- { "h_cut_outside", h_cut_outside },
- { "h_cut_in_incl", h_cut_in_incl },
- { "h_cut_out_incl", h_cut_out_incl },
- { "", (void *)NULL } }; /* must terminate with NULL */
-
- static struct func_strt *f_list2 = NULL;
-
- static int func_list_l = 0;
-
-
- /*
- * Search for the function name, return its pointer.
- * Return NULL if not found.
- */
- void *h_fNameSrch(const char *fname)
- {
- int i = 0;
-
- while( f_list1[i].func != NULL )
- {
- if (strncmp(f_list1[i].name,fname,FUNCNAMELEN) == 0)
- return f_list1[i].func;
- i++;
- }
-
- if (f_list2 != NULL)
- {
- i = 0;
- while( f_list2[i].func != NULL )
- {
- if (strncmp(f_list2[i].name,fname,FUNCNAMELEN) == 0)
- return f_list2[i].func;
- i++;
- }
- }
-
- return NULL;
- }
-
- /*
- * Search for the function pointer, return its name if found.
- */
- char *h_fPtrSrch(void *func)
- {
- int i = 0;
-
- while( f_list1[i].func != NULL )
- {
- if (f_list1[i].func == func)
- return f_list1[i].name;
- i++;
- }
-
- if (f_list2 != NULL)
- {
- i = 0;
- while( f_list2[i].func != NULL )
- {
- if (f_list2[i].func == func)
- return f_list2[i].name;
- i++;
- }
- }
-
- return NULL;
- }
-
-
- /*
- * Register the function on the registry.
- */
- int h_func_reg(const char *fn, void *func)
- {
- int i=0;
- char *sname;
- void *sfunc;
-
- if ((sname = h_fPtrSrch(func)) != NULL)
- if (sname != fn)
- {
- h_error("h_funcReg: function name is in registry, but pointer does not match.");
- return -1;
- }
- else
- return 0;
-
- if ((sfunc = h_fNameSrch(fn)) != NULL)
- if (sfunc != func)
- {
- h_error("h_funcReg: function pointer is in registry, but name does not match.");
- return -1;
- }
- else
- return 0;
-
- if (func_list_l == 0)
- {
- f_list2 = (struct func_strt *)
- malloc( (size_t)5*sizeof(struct func_strt) );
- if (f_list2 == NULL)
- {
- h_error("h_funcReg: Error allocating memory for function registry.");
- return -1;
- }
- func_list_l = 5;
- i = 0;
- }
- else
- {
- while( f_list1[i].func != NULL ) i++;
- if (i==(func_list_l-1))
- {
- f_list2 = (struct func_strt *)
- realloc( f_list2,
- (size_t)(func_list_l+5)*sizeof(struct func_strt) );
- if (f_list2 == NULL)
- {
- h_error("h_funcReg: Error reallocating memory for function registry");
- return -1;
- }
- func_list_l += 5;
- }
- }
-
- strncpy(f_list2[i].name,fn,FUNCNAMELEN);
- f_list2[i].func = func;
-
- strncpy(f_list2[i+1].name,"",FUNCNAMELEN);
- f_list2[i+1].func = (void*)NULL;
-
-
- return 0;
- }
-
- void h_adjustAxis( float *low, float *high, int nbins, int log )
- {
- int i;
- int axis = 0;
- float step, w, mag, range;
- float mylow, myhigh;
- #define N_NICE 4
- #ifndef __STDC__
- static
- #endif
- float nice[N_NICE] = { 1.0,2.0,2.5,5.0 };
-
- if (*low >= *high)
- {
- if (*low < 0.0) *high = 0.0;
- else if (*low > 0.0) *low = 0.0;
- else
- {
- *low = -1.0;
- *high = 1.0;
- return;
- }
- }
-
- if (nbins <= 0)
- {
- axis = 1;
- nbins = 10;
- }
-
- /*
- * Round the "bin width" to a nice number.
- * If this is being done for an axis (ie nbins was 0), then
- * we don't have to go > *high.
- */
- w = (*high - *low)/((float)nbins);
- mag = floor(log10(w));
- i = 0;
- do
- {
- step = nice[i] * pow(10.0,mag);
-
- mylow = floor(*low/step) * step;
- if (!axis)
- myhigh = mylow + step*nbins;
- else
- myhigh = ceil(*high/step) * step;
-
-
- i++;
- if (i>=N_NICE)
- {
- i = 0;
- mag++;
- }
- }
- while ( (axis && myhigh < *high) || (!axis && myhigh <= *high) );
-
- range = myhigh - mylow;
-
- /*
- * we now have decided on a range. Try to move low/high a little
- * to end up on a nice number.
- *
- * first check if either end is near 0.0
- */
- if (!log && *low >= 0.0 &&
- ((axis && range>=*high) || (!axis && range>*high)) )
- {
- *low = 0.0;
- *high = range;
- return;
- }
- if ( ((axis && *high<=0.0) || (!axis && *high<0.0))
- && -range<=*low)
- {
- *high = 0.0;
- *low = -range;
- return;
- }
-
- /*
- * try to round *low.
- */
- i = N_NICE-1;
- if (myhigh != 0.0)
- mag = ceil(log10(fabs(myhigh)));
- else
- mag = ceil(log10(fabs(mylow)));
-
- do
- {
- step = nice[i] * pow(10.0,mag);
-
- mylow = floor(*low/step) * step;
- myhigh = mylow + range;
-
- i--;
- if (i<0)
- {
- i = N_NICE-1;
- mag--;
- }
- }
- while ((log && mylow <= 0.0) ||
- (axis && myhigh < *high) ||
- (!axis && myhigh <= *high) );
-
- *low = mylow;
- *high = myhigh;
- return;
- }
- #undef N_NICE
-
-
- /*
- * Hippo error message facility.
- */
- void h_errmsg(const char *file, int line, const char *msg )
- {
- fprintf(stderr, "Error in hippo file %s, line %d :\n", file, line);
- fprintf(stderr, " %s\n", msg );
- }
-
-
- /*
- * Title expansion routine. Parse string for such things as
- * %t, %x, %y, etc.
- */
- char *h_expandLabel( char *dest, const char *src, int max, display disp )
- {
- int i,j=0;
- int bind;
- int l;
- int k;
- float del,del2;
- char form[15];
-
- l = strlen(src);
-
- if (disp->ntuple == NULL)
- {
- strncpy( dest, src, max );
- return dest;
- }
-
- for (i=0; i<l && j<max; i++)
- {
- if (src[i] == '%')
- {
- i++;
- switch (src[i])
- {
- case '%':
- dest[j++] = '%';
- break;
-
- case 't': case 'T':
- if (disp->ntuple->title != NULL)
- {
- strncpy(&(dest[j]),disp->ntuple->title,max-j-1);
- j = strlen(dest);
- }
- break;
-
- case 'x': case 'X':
- if ((bind = disp->binding.x) >= 0)
- {
- strncpy(&(dest[j]),disp->ntuple->label[bind],max-j-1);
- j = strlen(dest);
- }
- break;
-
- case 'y': case 'Y':
- if ((bind = disp->binding.y) >= 0)
- {
- strncpy(&(dest[j]),disp->ntuple->label[bind],max-j-1);
- j = strlen(dest);
- }
- break;
-
- case 'z': case 'Z':
- if ((bind = disp->binding.z) >= 0)
- {
- strncpy(&(dest[j]),disp->ntuple->label[bind],max-j-1);
- j = strlen(dest);
- }
- break;
-
- case 'w': case 'W':
- if ((bind = disp->binding.weight) >= 0)
- {
- strncpy(&(dest[j]),disp->ntuple->label[bind],max-j-1);
- j = strlen(dest);
- }
- break;
-
- case 'd': case 'D':
- i++;
- switch (src[i])
- {
- case 'x': case 'X':
- del = (disp->xAxis.high-disp->xAxis.low)
- /(float)disp->bins.xAxis.nBins;
- for (k=1; k<6; k++)
- {
- del2 = del*pow(10.0,(double)k);
- if (fabs((int)del2 - del2) < 1e-2) break;
- }
- sprintf(form,"%%1.%df",k);
- sprintf(&dest[j],form,del);
- j = strlen(dest);
- break;
-
- case 'y': case 'Y':
- del = (disp->yAxis.high-disp->yAxis.low)
- /(float)disp->bins.yAxis.nBins;
- for (k=1; k<6; k++)
- {
- del2 = del*pow(10.0,(double)k);
- if (fabs((int)del2 - del2) < 1e-2) break;
- }
- sprintf(form,"%%1.%df",k);
- sprintf(&dest[j],form,del);
- j = strlen(dest);
- break;
- }
- break;
-
- case 'e': case 'E':
- i++;
- switch (src[i])
- {
- case 'x': case 'X':
- if ((bind = disp->binding.xerror) >= 0)
- {
- strncpy(&(dest[j]),disp->ntuple->label[bind],
- max-j-1);
- j = strlen(dest);
- }
- break;
-
- case 'y': case 'Y':
- if ((bind = disp->binding.yerror) >= 0)
- {
- strncpy(&(dest[j]),disp->ntuple->label[bind],
- max-j-1);
- j = strlen(dest);
- }
- break;
- }
- break;
- }
- }
- else
- dest[j++] = src[i];
- }
-
- if (j<max)
- dest[j] = '\0';
- else
- dest[max-1] = '\0';
-
- return dest;
- }
-
-
- /*
- * Standard Cut functions.
- */
-
- int h_cut_lt( float nt[], double param[] )
- {
- if (nt[ (int)param[0] ] < param[1])
- return 1;
- else
- return 0;
- }
-
- int h_cut_gt( float nt[], double param[] )
- {
- if (nt[ (int) param[0]] > param[1] )
- return 1;
- else
- return 0;
- }
-
- int h_cut_le( float nt[], double param[] )
- {
- if (nt[ (int) param[0]] <= param[1] )
- return 1;
- else
- return 0;
- }
-
- int h_cut_ge( float nt[], double param[] )
- {
- if (nt[ (int) param[0]] >= param[1] )
- return 1;
- else
- return 0;
- }
-
- int h_cut_inside( float nt[], double param[] )
- {
- if (nt[(int) param[0]] > param[1] &&
- nt[(int) param[0]] < param[2])
- return 1;
- else
- return 0;
- }
-
- int h_cut_outside( float nt[], double param[] )
- {
- if (nt[(int) param[0]] < param[1] ||
- nt[(int) param[0]] > param[2])
- return 1;
- else
- return 0;
- }
-
- int h_cut_in_incl( float nt[], double param[] )
- {
- if (nt[(int) param[0]] >= param[1] &&
- nt[(int) param[0]] <= param[2])
- return 1;
- else
- return 0;
- }
-
- int h_cut_out_incl( float nt[], double param[] )
- {
- if (nt[(int) param[0]] <= param[1] ||
- nt[(int) param[0]] >= param[2])
- return 1;
- else
- return 0;
- }
-
-
- int doCuts( float nt[], func_id cutlist )
- {
- typedef int (*cutfunc)(float *,double *);
-
- func_id c = cutlist;
- int result = 0;
-
- while (c != NULL)
- {
- result += (*((cutfunc)c->funcPtr))( nt, (double *)c->paramBlk );
- c = c->next;
- }
-
- return result;
- }
-