Skip to content

Instantly share code, notes, and snippets.

@JeremyMcCormick
Last active November 10, 2015 23:06
Show Gist options
  • Save JeremyMcCormick/5a1a9787894e3739d003 to your computer and use it in GitHub Desktop.
Save JeremyMcCormick/5a1a9787894e3739d003 to your computer and use it in GitHub Desktop.
HPS - calculate Q from EvioMetadataReader (unused)
/**
* Calculate charge in nano coulomb for a file using the EPICS FCup current measurements.
* <p>
* The accuracy of this calculation is limited by the time interval of the EPICS data (2s),
* and SVT livetime is not included so the value should not be used for analysis.
*
* @param epicsData the list of EPICS data with the FCup values
* @param firstTimestamp the first timestamp in the file
* @param lastTimestamp the last timestamp in the file
* @return the charge for the file
*/
private double calculateCharge(List<EpicsData> epicsData, int firstTimestamp, int lastTimestamp) {
LOGGER.fine("calculating charge from " + epicsData.size() + " EPICS events between " + firstTimestamp + " and " + lastTimestamp + " event timestamps");
double charge = 0.;
for (int i = 0; i < epicsData.size(); i++) {
// Get EPICS data.
EpicsData epicsEvent = epicsData.get(i);
EpicsHeader epicsHeader = epicsEvent.getEpicsHeader();
int epicsTimestamp = epicsHeader.getTimestamp();
// Calculate the time interval to use.
int interval = 0;
if (i == 0) {
// For first record, use the difference between EPICS and first timestamp in the file.
if (epicsTimestamp > firstTimestamp) {
interval = epicsTimestamp - firstTimestamp;
} else {
interval = firstTimestamp - epicsTimestamp;
}
} else {
// For all other records, use the difference between EPICS timestamps.
interval = epicsTimestamp - epicsData.get(i - 1).getEpicsHeader().getTimestamp();
}
// Calculate the current which will use the average value between EPICS masurements if available.
double current;
if (i != 0) {
current = (epicsEvent.getValue("scaler_calc1") + epicsData.get(i - 1).getValue("scaler_calc1")) / 2.;
} else {
current = epicsEvent.getValue("scaler_calc1");
}
LOGGER.fine(interval + "s * " + current + " nc = " + (interval * current));
// Add integration of current and time.
charge += interval * current;
LOGGER.fine("charge to " + charge);
}
// Add charge for last EPICS event.
EpicsData lastEpicsEvent = epicsData.get(epicsData.size() - 1);
charge += (double) (lastTimestamp - lastEpicsEvent.getEpicsHeader().getTimestamp()) * (double) lastEpicsEvent.getValue("scaler_calc1");
LOGGER.fine("final charge " + charge);
return charge;
}
private double calculateAverageCharge(List<EpicsData> epicsData, int firstTimestamp, int lastTimestamp) {
double charge = 0;
int nEpicsData = 0;
for (EpicsData epicsEvent : epicsData) {
int epicsTimestamp = epicsEvent.getEpicsHeader().getTimestamp();
if (epicsTimestamp > firstTimestamp && epicsTimestamp < lastTimestamp) {
charge += epicsEvent.getValue("scaler_calc1");
++nEpicsData;
}
}
charge /= nEpicsData;
charge *= (double) (lastTimestamp - firstTimestamp);
return charge;
}
/**
* Calculate gated charge value.
*
* @param charge the ungated charge
* @param scalerData the last scaler data in the event
* @return the gated charge value
*/
private double calculateGatedCharge(double charge, List<ScalerData> scalerData, ScalerDataIndex gatedIndex, ScalerDataIndex ungatedIndex) {
ScalerData scalerEvent = null;
LOGGER.fine("looking for livetime measurements in " + scalerData.size() + " scaler records");
for (int i = scalerData.size() - 1; i >= 0; i--) {
if (scalerData.get(i).getValue(gatedIndex) != 0. && scalerData.get(i).getValue(ungatedIndex) != 0.) {
LOGGER.fine("valid livetime measurement found in scaler record " + i);
scalerEvent = scalerData.get(i);
break;
}
}
if (scalerEvent != null) {
return charge * ((double) scalerEvent.getValue(gatedIndex) / (double) scalerEvent.getValue(ungatedIndex));
} else {
LOGGER.warning("no scaler data measurements available for calculating gated charge");
return 0;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment