moved tau to settings file, removed gain and offset, setting threshold loads settings file, setting threshold only sets client variable

This commit is contained in:
Dhanya Maliakal
2017-02-03 12:29:44 +01:00
parent 111856ed7b
commit 87ce1ed736
42 changed files with 583 additions and 6302 deletions

View File

@ -69,85 +69,58 @@ int energyConversion::writeCalibrationFile(string fname, double gain, double off
};
int energyConversion::readCalibrationFile(string fname, int *gain, int *offset, int64_t &tau, detectorType myDetectorType){
int energyConversion::readCalibrationFile(string fname, int *gain, int *offset){
string str;
ifstream infile;
double o,g;
int ig=0;
switch (myDetectorType) {
case EIGER:
#ifdef VERBOSE
std::cout<< "Opening file "<< fname << std::endl;
std::cout<< "Opening file "<< fname << std::endl;
#endif
infile.open(fname.c_str(), ios_base::in);
if (infile.is_open()) {
//get gain and offset
for (ig=0; ig<4; ig++) {
//while ( (getline(infile,str)) > -1) {
getline(infile,str);
infile.open(fname.c_str(), ios_base::in);
if (infile.is_open()) {
//get gain and offset
for (ig=0; ig<4; ig++) {
//while ( (getline(infile,str)) > -1) {
getline(infile,str);
#ifdef VERBOSE
std::cout<< str << std::endl;
std::cout<< str << std::endl;
#endif
istringstream ssstr(str);
ssstr >> o >> g;
offset[ig]=(int)(o*1000);
gain[ig]=(int)(g*1000);
// ig++;
if (ig>=4)
break;
}
//get tau
if (myDetectorType == EIGER) {
if(getline(infile,str)){
istringstream ssstr(str);
ssstr >> tau;
#ifdef VERBOSE
std::cout<< "tau:" << tau << std::endl;
#endif
}
}
infile.close();
cout << "Calibration file loaded: " << fname << endl;
} else {
cout << "Could not open calibration file: "<< fname << std::endl;
gain[0]=0;
offset[0]=0;
#ifndef MYROOT
return FAIL;
#endif
return -1;
istringstream ssstr(str);
ssstr >> o >> g;
offset[ig]=(int)(o*1000);
gain[ig]=(int)(g*1000);
// ig++;
if (ig>=4)
break;
}
infile.close();
cout << "Calibration file loaded: " << fname << endl;
} else {
cout << "Could not open calibration file: "<< fname << std::endl;
gain[0]=0;
offset[0]=0;
#ifndef MYROOT
return OK;
#endif
return 0;
break;
default:
std::cout<< "Writing Calibration Files for this detector not defined\n" << std::endl;
return FAIL;
#endif
return -1;
}
#ifndef MYROOT
return OK;
#endif
return 0;
};
int energyConversion::writeCalibrationFile(string fname, int *gain, int *offset, int64_t tau, detectorType myDetectorType){
int energyConversion::writeCalibrationFile(string fname, int *gain, int *offset){
//std::cout<< "Function not yet implemented " << std::endl;
ofstream outfile;
switch (myDetectorType) {
case EIGER:
outfile.open (fname.c_str());
// >> i/o operations here <<
if (outfile.is_open()) {
for (int ig=0; ig<4; ig++)
outfile << ((double)offset[ig]/1000) << " " << ((double)gain[ig]/1000) << std::endl;
outfile << tau << std::endl;
} else {
std::cout<< "Could not open calibration file "<< fname << " for writing" << std::endl;
#ifndef MYROOT
@ -161,21 +134,53 @@ int energyConversion::writeCalibrationFile(string fname, int *gain, int *offset,
return OK;
#endif
return 0;
break;
default:
std::cout<< "Writing Calibration Files for this detector not defined\n" << std::endl;
return FAIL;
}
};
slsDetectorDefs::sls_detector_module* energyConversion::interpolateTrim(detectorType myDetectorType, sls_detector_module* a, sls_detector_module* b, const int energy, const int e1, const int e2){
// only implemented for eiger currently (in terms of which dacs)
if(myDetectorType != EIGER) {
printf("Interpolation of Trim values not implemented for this detector!\n");
return NULL;
}
sls_detector_module* myMod = createModule(myDetectorType);
enum eiger_DacIndex{SVP,VTR,VRF,VRS,SVN,VTGSTV,VCMP_LL,VCMP_LR,CAL,VCMP_RL,RXB_RB,RXB_LB,VCMP_RR,VCP,VCN,VIS};
//Copy other dacs
int num_dacs_to_copy = 10;
int dacs_to_copy[] = {SVP,VTR,VRS,SVN,VTGSTV,CAL,RXB_RB,RXB_LB,VCN,VIS};
for (int i = 0; i < num_dacs_to_copy; ++i) {
if(a->dacs[dacs_to_copy[i]] != b->dacs[dacs_to_copy[i]]) {
deleteModule(myMod);
return NULL;
}
myMod->dacs[dacs_to_copy[i]] = a->dacs[dacs_to_copy[i]];
}
//Interpolate vrf, vcmp, vcp
int num_dacs_to_interpolate = 6;
int dacs_to_interpolate[] = {VRF,VCMP_LL,VCMP_LR,VCMP_RL,VCMP_RR,VCP};
for (int i = 0; i < num_dacs_to_interpolate; ++i) {
myMod->dacs[dacs_to_interpolate[i]] = linearInterpolation(energy, e1, e2,
a->dacs[dacs_to_interpolate[i]], b->dacs[dacs_to_interpolate[i]]);
}
//Interpolate all trimbits
for (int i = 0; i<myMod->nchan; i++)
myMod->chanregs[i] = linearInterpolation(energy, e1, e2, a->chanregs[i], b->chanregs[i]);
return myMod;
}
#ifndef MYROOT
/* I/O */
slsDetectorDefs::sls_detector_module* energyConversion::readSettingsFile(string fname, detectorType myDetectorType, sls_detector_module *myMod, int* iodelay){
slsDetectorDefs::sls_detector_module* energyConversion::readSettingsFile(string fname, detectorType myDetectorType, int& iodelay, int& tau, sls_detector_module* myMod){
@ -353,7 +358,7 @@ slsDetectorDefs::sls_detector_module* energyConversion::readSettingsFile(string
infile.close();
strcpy(settingsFile,fname.c_str());
cout << "Settings file loaded: " << settingsFile << endl;
printf("Settings file loaded: %s\n",settingsFile);
return myMod;
}
@ -365,12 +370,14 @@ slsDetectorDefs::sls_detector_module* energyConversion::readSettingsFile(string
infile.open(myfname.c_str(),ifstream::binary);
if (infile.is_open()) {
infile.read((char*) myMod->dacs,sizeof(dacs_t)*(myMod->ndac));
infile.read((char*) iodelay,sizeof(*iodelay));
infile.read((char*)&iodelay,sizeof(iodelay));
infile.read((char*)&tau,sizeof(tau));
infile.read((char*) myMod->chanregs,sizeof(int)*(myMod->nchan));
#ifdef VERBOSE
for(int i=0;i<myMod->ndac;i++)
std::cout << "dac " << i << ":" << myMod->dacs[i] << std::endl;
std::cout << "iodelay:" << *iodelay << std::endl;
std::cout << "iodelay:" << iodelay << std::endl;
std::cout << "tau:" << tau << std::endl;
#endif
if(infile.eof()){
cout<<endl<<"Error, could not load trimbits end of file, "<<myfname<<", reached."<<endl<<endl;
@ -381,7 +388,7 @@ slsDetectorDefs::sls_detector_module* energyConversion::readSettingsFile(string
}
infile.close();
strcpy(settingsFile,fname.c_str());
cout << "Settings file loaded: " << settingsFile << endl;
printf("Settings file loaded: %s\n",settingsFile);
return myMod;
}
@ -423,7 +430,7 @@ slsDetectorDefs::sls_detector_module* energyConversion::readSettingsFile(string
infile.close();
strcpy(settingsFile,fname.c_str());
cout << "Settings file loaded: " << settingsFile << endl;
printf("Settings file loaded: %s\n",settingsFile);
return myMod;
}
@ -439,7 +446,7 @@ slsDetectorDefs::sls_detector_module* energyConversion::readSettingsFile(string
}
std::cout<< "Error: Could not open settings file " << myfname << std::endl;
printf("Error: Could not open settings file %s\n", myfname.c_str());
if (nflag)
deleteModule(myMod);
@ -450,7 +457,7 @@ slsDetectorDefs::sls_detector_module* energyConversion::readSettingsFile(string
};
int energyConversion::writeSettingsFile(string fname, detectorType myDetectorType, sls_detector_module mod, int* iodelay){
int energyConversion::writeSettingsFile(string fname, detectorType myDetectorType, sls_detector_module mod, int& iodelay, int& tau){
ofstream outfile;
@ -526,17 +533,19 @@ int energyConversion::writeSettingsFile(string fname, detectorType myDetectorTyp
#ifdef VERBOSE
for(int i=0;i<mod.ndac;i++)
std::cout << "dac " << i << ":" << mod.dacs[i] << std::endl;
std::cout << "iodelay: " << *iodelay << std::endl;
std::cout << "iodelay: " << iodelay << std::endl;
std::cout << "tau: " << tau << std::endl;
#endif
outfile.write((char*)mod.dacs, sizeof(dacs_t)*(mod.ndac));
outfile.write((char*)iodelay, sizeof(*iodelay));
outfile.write((char*)iodelay, sizeof(iodelay));
outfile.write((char*)tau, sizeof(tau));
outfile.write((char*)mod.chanregs, sizeof(int)*(mod.nchan));
outfile.close();
return slsDetectorDefs::OK;
}
std::cout<< "could not open SETTINGS file " << fname << std::endl;
printf("Could not open Settings file %s\n", fname.c_str());
return slsDetectorDefs::FAIL;
default:

View File

@ -35,7 +35,9 @@ class energyConversion
reads a calibration file
\param fname file to be read
\param gain reference to the gain variable
\offset reference to the offset variable
\param offset reference to the offset variable
\returns OK if successful, else FAIL or -1
*/
static int readCalibrationFile(string fname, double &gain, double &offset);
@ -44,36 +46,51 @@ class energyConversion
\param fname file to be written
\param gain
\param offset
\returns OK if successful, else FAIL or -1
*/
static int writeCalibrationFile(string fname, double gain, double offset);
/**
reads a calibration file
\param fname file to be read
\param gain reference to the gain variable
\offset reference to the offset variable
\tau tau
\tau tau
\param offset reference to the offset variable
\returns OK if successful, else FAIL or -1
*/
static int readCalibrationFile(string fname, int *gain, int *offset, int64_t &tau, detectorType myDetectorType);
static int readCalibrationFile(string fname, int *gain, int *offset);
/**
writes a calibration file
\param fname file to be written
\param gain
\param offset
\param tau
\param gain reference to the gain variable
\param offset reference to the offset variable
\returns OK if successful, else FAIL or -1
*/
static int writeCalibrationFile(string fname, int *gain, int *offset, int64_t tau, detectorType myDetectorType);
static int writeCalibrationFile(string fname, int *gain, int *offset);
//Template function to do linear interpolation between two points
template <typename E, typename V>
V linearInterpolation(const E x, const E x1, const E x2, const V y1, const V y2){
double k = static_cast<double>(y2-y1)/(x2-x1);
double m = y1-k*x1;
int y = round( k*x+m );
return static_cast<V>(y);
}
/**
* interpolates dacs and trimbits between 2 trim files
\param myDetectorType detector type (needed for which dacs that neeeds to be interpolated & which kept same)
\param a first module structure
\param b second module structure
\param energy energy to trim at
\param e1 reference trim value
\param e2 reference trim value
\returns the pointer to the module structure with interpolated values or NULL if error
*/
sls_detector_module* interpolateTrim(detectorType myDetectorType, sls_detector_module* a, sls_detector_module* b, const int energy, const int e1, const int e2);
@ -83,12 +100,13 @@ class energyConversion
reads a trim/settings file
\param fname name of the file to be read
\param myDetectorType detector type (needed for number of channels, chips, dacs etc.)
\param myMod pointer to the module structure which has to be set. <BR> If it is NULL a new module structure will be created
\param iodelay io delay (detector specific)
\param tau tau (detector specific)
\param myMod pointer to the module structure which has to be set. <BR> If it is NULL a new module structure will be created
\returns the pointer to myMod or NULL if reading the file failed
*/
sls_detector_module* readSettingsFile(string fname, detectorType myDetectorType, sls_detector_module* myMod=NULL, int* iodelay=0);
sls_detector_module* readSettingsFile(string fname, detectorType myDetectorType, int& iodelay, int& tau, sls_detector_module* myMod=NULL);
/**
writes a trim/settings file
@ -96,11 +114,12 @@ class energyConversion
\param myDetectorType detector type (needed for number of channels, chips, dacs etc.)
\param mod module structure which has to be written to file
\param iodelay io delay (detector specific)
\param tau tau (detector specific)
\returns OK or FAIL if the file could not be written
\sa ::sls_detector_module mythenDetector::writeSettingsFile(string, sls_detector_module)
*/
int writeSettingsFile(string fname, detectorType myDetectorType, sls_detector_module mod, int* iodelay=0);
int writeSettingsFile(string fname, detectorType myDetectorType, sls_detector_module mod, int& iodelay, int& tau);
/** allocates the momery for a detector module structure
\param myDetectorType detector type (needed for number of channels, chips, dacs etc.)
@ -114,6 +133,7 @@ class energyConversion
*/
virtual void deleteModule(sls_detector_module *myMod)=0;
protected:
/** pointer to settings file name */
char *settingsFile;