PSI sics-cvs-psi-2008-10-02

This commit is contained in:
2008-10-02 00:00:00 +00:00
committed by Douglas Clowes
parent 6e926b813f
commit 4baffb9b7a
304 changed files with 77527 additions and 3612 deletions

285
hmdata.c
View File

@@ -5,17 +5,23 @@
copyright: see file COPYRIGHT
Mark Koennecke, January 2003
Added loading HM data from file, Mark Koennecke, November 2006
-------------------------------------------------------------------------*/
#include <stdlib.h>
#include <stdio.h>
#include <assert.h>
#include <math.h>
#include <time.h>
#include "splitter.h"
#include "fortify.h"
#include "hmdata.h"
#include <nxdataset.h>
#include "HistMem.h"
#include "HistMem.i"
#include "HistDriv.i"
#include "countdriv.h"
#include "stptok.h"
/*----------------------------------------------------------------------*/
pHMdata makeHMData(void) {
pHMdata self = NULL;
@@ -46,12 +52,12 @@ void clearHMData(pHMdata self){
size *= self->iDim[i];
}
if(self->tofMode){
size *= self->nTimeChan;
size *= getNoOfTimebins(self);
}
memset(self->localBuffer,0,size*sizeof(HistInt));
}
/*----------------------------------------------------------------------*/
static int resizeBuffer(pHMdata self){
int resizeBuffer(pHMdata self){
long size;
int i;
@@ -60,7 +66,7 @@ static int resizeBuffer(pHMdata self){
size *= self->iDim[i];
}
if(self->tofMode){
size *= self->nTimeChan;
size *= getNoOfTimebins(self);
}
if(self->localBuffer != NULL){
free(self->localBuffer);
@@ -80,6 +86,7 @@ int configureHMdata(pHMdata self, pStringDict pOpt,
int status, i;
float fVal;
char pValue[80];
pHistMem master = NULL;
if(self->nTimeChan > 2) {
self->tofMode = 1;
@@ -111,6 +118,18 @@ int configureHMdata(pHMdata self, pStringDict pOpt,
self->updateIntervall = (int)rint(fVal);
}
status = StringDictGet(pOpt,"timeslave",pValue, 79);
if(status == 1) {
master = (pHistMem)FindCommandData(pServ->pSics,pValue,"HistMem");
if(master == NULL){
SCWrite(pCon,"ERROR: timeslave requested, but master HM not found",
eError);
} else {
self->timeslave = master->pDriv->data;
self->tofMode = 1;
}
}
/*
invalidate buffer
*/
@@ -138,7 +157,7 @@ int configureHMdata(pHMdata self, pStringDict pOpt,
int genTimeBinning(pHMdata self, float start, float step, int noSteps){
int i;
if(noSteps >= MAXCHAN){
if(noSteps >= MAXCHAN || self->timeslave != NULL){
return 0;
}
for(i = 0; i < noSteps; i++){
@@ -150,6 +169,10 @@ int genTimeBinning(pHMdata self, float start, float step, int noSteps){
}
/*----------------------------------------------------------------------*/
int setTimeBin(pHMdata self, int index, float value){
if(self->timeslave != NULL){
return 0;
}
if(index >= 0 && index < MAXCHAN){
self->timeBinning[index] = value;
} else {
@@ -157,7 +180,7 @@ int setTimeBin(pHMdata self, int index, float value){
}
self->tofMode = 1;
if(index > self->nTimeChan){
self->nTimeChan = index;
self->nTimeChan = index+1;
return resizeBuffer(self);
}
return 1;
@@ -168,17 +191,27 @@ int isInTOFMode(pHMdata self){
}
/*---------------------------------------------------------------------*/
int getNoOfTimebins(pHMdata self){
return self->nTimeChan;
if(self->timeslave != NULL){
return getNoOfTimebins(self->timeslave);
} else {
return self->nTimeChan;
}
}
/*---------------------------------------------------------------------*/
float *getTimeBinning(pHMdata self){
return self->timeBinning;
if(self->timeslave != NULL){
return getTimeBinning(self->timeslave);
} else {
return self->timeBinning;
}
}
/*-------------------------------------------------------------------*/
void clearTimeBinning(pHMdata self){
self->nTimeChan = 1;
self->tofMode = 0;
resizeBuffer(self);
if(self->timeslave == NULL){
self->nTimeChan = 1;
self->tofMode = 0;
resizeBuffer(self);
}
}
/*--------------------------------------------------------------------*/
void getHMDataDim(pHMdata self, int iDim[MAXDIM], int *rank){
@@ -193,7 +226,7 @@ long getHMDataLength(pHMdata self){
length *= self->iDim[i];
}
if(self->tofMode){
length *= self->nTimeChan;
length *= getNoOfTimebins(self);
}
return length;
}
@@ -227,6 +260,10 @@ static int updateHMbuffer(pHistMem hist, int bank, SConnection *pCon){
assert(self);
if(self->timeslave != NULL){
resizeBuffer(self);
}
for(i = 0; i < 3; i++){
status = hist->pDriv->GetHistogram(hist->pDriv,pCon,
bank,0,getHMDataLength(self),
@@ -292,7 +329,7 @@ HistInt *getHMDataBufferPointer(pHistMem hist,SConnection *pCon){
assert(self);
if(self->localBuffer == NULL){
if(self->localBuffer == NULL || self->timeslave != NULL){
resizeBuffer(self);
}
/*
@@ -327,7 +364,7 @@ long sumHMDataRectangle(pHistMem hist, SConnection *pCon,
int iStart[MAXDIM], int iEnd[MAXDIM]) {
HistInt *iData;
pHMdata self = hist->pDriv->data;
int i, iHistLength, status, iIndex;
int i, iHistLength, status, iIndex, myrank;
char pBueffel[256];
unsigned long lSum, lRowSum;
@@ -336,7 +373,12 @@ long sumHMDataRectangle(pHistMem hist, SConnection *pCon,
/*
error checking
*/
for(i = 0; i < self->rank; i++){
myrank = self->rank;
if(isInTOFMode(self)){
self->iDim[self->rank] = getNoOfTimebins(self);
myrank++;
}
for(i = 0; i < myrank; i++){
if( (iStart[i] < 0) || (iStart[i] > self->iDim[i]) ) {
sprintf(pBueffel,"ERROR: %d is out of data dimension range",
iStart[i]);
@@ -367,25 +409,35 @@ long sumHMDataRectangle(pHistMem hist, SConnection *pCon,
iHistLength = getHMDataLength(self);
/* actually sum */
switch(self->rank)
switch(myrank)
{
case 1:
lSum = SumRow(self->localBuffer, iHistLength,
iStart[0], iEnd[0]);
break;
case 2:
lSum = 0;
for(i = iStart[0]; i < iEnd[0]; i++){
iIndex = i*self->iDim[1];
lRowSum = SumRow(self->localBuffer,iHistLength,
if(isInTOFMode(self)){
lSum = 0;
for(i = iStart[0]; i < iEnd[0]; i++){
iIndex = i*self->iDim[1];
lRowSum = SumRow(self->localBuffer,iHistLength,
iIndex+iStart[1], iIndex+iEnd[1]);
lSum += lRowSum;
lSum += lRowSum;
}
} else {
lSum = 0;
for(i = iStart[1]; i < iEnd[1]; i++){
iIndex = i*self->iDim[0];
lRowSum = SumRow(self->localBuffer,iHistLength,
iIndex+iStart[0], iIndex+iEnd[0]);
lSum += lRowSum;
}
}
break;
default:
sprintf(pBueffel,
"ERROR: summing in %d dimensions not yet implemented",
self->rank);
myrank);
SCWrite(pCon,pBueffel,eError);
return -1;
break;
@@ -395,3 +447,194 @@ long sumHMDataRectangle(pHistMem hist, SConnection *pCon,
}
return lSum;
}
/*--------------------------------------------------------------------------*/
int loadHMData(pHMdata self, SConnection *pCon, char *filename){
FILE *fd = NULL;
char buffer[1024], pNumber[80], *pPtr;
long i = 0, length;
HistInt *data = NULL;
fd = fopen(filename,"r");
if(fd == NULL){
snprintf(buffer,1023,"ERROR: failed to open file %s", filename);
SCWrite(pCon,buffer,eError);
return 0;
}
length = getHMDataLength(self);
if(self->localBuffer == NULL || self->timeslave != NULL){
resizeBuffer(self);
}
data = self->localBuffer;
if(data == NULL){
SCWrite(pCon,"ERROR: failed to allocate HM", eError);
fclose(fd);
return 0;
}
while(i < length && fgets(buffer,1024,fd) != NULL){
pPtr = buffer;
while(pPtr != NULL){
pPtr = sicsNextNumber(pPtr,pNumber);
if(pPtr != NULL){
data[i] = atoi(pNumber);
i++;
}
}
}
if(i < length-1){
SCWrite(pCon,"WARNING: not enough data in file to fill HM",eWarning);
}
fclose(fd);
return 1;
}
/*==========================================================================
* subsampling was stolen from the SinqHTTP histogram memory code and
* thus contains some additional indirections.
* =========================================================================*/
static pNXDS hmDataToNXDataset(pHMdata self){
pNXDS result = NULL;
int i;
result = malloc(sizeof(NXDS));
if(result == NULL){
return NULL;
}
memset(result,0,sizeof(NXDS));
result->magic = MAGIC;
result->type = NX_INT32;
result->rank = self->rank;
if(isInTOFMode(self)){
result->rank++;
}
result->dim = malloc(self->rank*sizeof(int));
if(result->dim == NULL){
free(result);
return NULL;
}
for(i = 0; i < self->rank; i++){
result->dim[i] = self->iDim[i];
}
if(isInTOFMode(self)){
result->dim[result->rank-1] = getNoOfTimebins(self);
}
if(self->localBuffer == NULL){
resizeBuffer(self);
}
result->u.iPtr = self->localBuffer;
return result;
}
/*---------------------------------------------------------------------------*/
static pNXDS subSampleCommand(pNXDS source, char *command,
char *error, int errLen){
int startDim[NX_MAXRANK], endDim[NX_MAXRANK];
int dim = 0, start = 0, i;
char *pPtr = NULL, token[80];
pPtr = stptok(command,token,79,":\0");
while((pPtr = stptok(pPtr,token,79,":\0")) != NULL){
if(start == 0){
startDim[dim] = atoi(token);
start = 1;
} else {
endDim[dim] = atoi(token);
start = 0;
dim++;
}
}
if(dim < source->rank - 1){
strncpy(error,"ERROR: Not enough border values specified for subsampling",errLen);
return NULL;
}
for(i = 0; i < source->rank; i++){
if(startDim[i] < 0 || startDim[i] >= source->dim[i]){
snprintf(error,errLen,"ERROR: invalid start value %d for dimension %d", startDim[1], i);
return NULL;
}
if(endDim[i] < startDim[i] || endDim[i] >= source->dim[i]){
snprintf(error,errLen,"ERROR: invalid end value %d for dimension %d", endDim[1], i);
return NULL;
}
}
return cutNXDataset(source,startDim,endDim);
}
/*-----------------------------------------------------------------------------*/
static pNXDS sumCommand(pNXDS source, char *command, char *error, int errlen){
int dimNo = -1, start = -1, end = -1;
char *pPtr = NULL;
char token[80];
pPtr = stptok(command,token,79,":\0");
pPtr = stptok(pPtr,token,79,":\0");
if(pPtr != NULL){
dimNo = atoi(token);
}
pPtr = stptok(pPtr,token,79,":\0");
if(pPtr != NULL){
start = atoi(token);
}
pPtr = stptok(pPtr,token,79,":\0");
if(pPtr != NULL){
end = atoi(token);
}
if(dimNo < 0 || dimNo > source->rank - 1){
snprintf(error,errlen,"ERROR: invalid dimension %d requestd to sum", dimNo);
return NULL;
}
if(end < 0 || end > source->dim[dimNo] || start < 0 || start > end){
snprintf(error,errlen,"ERROR: invalid summing limits %d to %d requested", start,end);
return NULL;
}
return sumNXDataset(source,dimNo, start, end);
}
/*--------------------------------------------------------------------------*/
HistInt *subSample(pHMdata self, char *command,
char *error, int errLen){
pNXDS source = NULL, start = NULL;
pNXDS result = NULL;
char *pPtr = NULL;
char subCommand[132];
HistInt *data = NULL;
int length;
start = hmDataToNXDataset(self);
if(start == NULL){
strncpy(error,"Out-Of-Memory or no data while subsampling ",
errLen);
return NULL;
}
source = start;
pPtr = command;
while((pPtr = stptok(pPtr,subCommand,131,";\0\r\n")) != NULL){
if(strstr(subCommand,"sample") != NULL){
result = subSampleCommand(source,subCommand,error, errLen);
} else if(strstr(subCommand,"sum") != NULL){
result = sumCommand(source,subCommand,error, errLen);
} else {
strncpy(error,"ERROR: invalid subcommand to process requested",errLen);
return NULL;
}
if(result == NULL){
return NULL;
}
if(source != start){
dropNXDataset(source);
}
source = result;
}
length = getNXDatasetLength(result);
data = malloc((length+1)*sizeof(int));
if(data == NULL){
strncpy(error,"Out-Of-Mmeory in Subsample", errLen);
dropNXDataset(result);
return NULL;
}
data[0] = length;
memcpy(data+1,result->u.iPtr, length*sizeof(int));
dropNXDataset(result);
return data;
}