Commit 363a6ae0 authored by LEFEBVREJP email's avatar LEFEBVREJP email
Browse files

Merge branch 'spectra' into 'master'

Spectra

See merge request !55
parents 439aa797 2513f63b
Pipeline #16242 passed with stages
in 9 minutes and 4 seconds
......@@ -23,7 +23,7 @@ std::string trim_string(const std::string &indata)
{
std::string data(indata);
data.erase(0, data.find_first_not_of(" \t\n\r")); // prefixing spaces
data.erase(data.find_last_not_of(" \t\n\r") + 1); // surfixing spaces
data.erase(data.find_last_not_of(" \t\n\r") + 1); // suffixing spaces
return data;
} // trim_string
......@@ -60,7 +60,8 @@ std::string strip_quotes(const std::string &src)
}
std::vector<std::string> split_string(const std::string &delim,
const std::string &word)
const std::string &word,
bool removeEmptyParts)
{
std::vector<std::string> tokens;
size_t pos = 0;
......@@ -78,6 +79,12 @@ std::vector<std::string> split_string(const std::string &delim,
tokens.resize(tokens.size() - 1);
}
// Remove empty parts if requested
if (removeEmptyParts)
{
tokens.erase(std::remove(tokens.begin(), tokens.end(), ""), tokens.end());
}
return tokens;
} // splitString
......
......@@ -86,8 +86,9 @@ std::string RADIX_PUBLIC join(const std::string &delim, const T &x);
/**
* @brief Split a string into pieces using a delimiter
*/
std::vector<std::string> RADIX_PUBLIC split_string(const std::string &delim,
const std::string &word);
std::vector<std::string> RADIX_PUBLIC
split_string(const std::string &delim, const std::string &word,
bool removeEmptyParts = false);
/**
* @brief Find all instances and replace them
......
......@@ -104,6 +104,20 @@ TEST(Split, NoDelim)
ASSERT_EQ(1, x.size());
EXPECT_EQ("nope", x[0]);
}
TEST(Split, RemoveEmptyEntries)
{
Vec_Str split = split_string(";", "good;;;morning");
ASSERT_EQ(4, split.size());
EXPECT_EQ("good", split[0]);
EXPECT_EQ("morning", split[3]);
split = split_string(";", "good;;;morning", true);
ASSERT_EQ(2, split.size());
EXPECT_EQ("good", split[0]);
EXPECT_EQ("morning", split[1]);
}
TEST(Radix, OrdinalItoa)
{
{
......
......@@ -11,6 +11,7 @@ SET(SOURCE
f71stream.cc
hcdump.cc
hysplitcdump.cc
spectrum.cc
)
SET(HEADERS
......@@ -25,6 +26,9 @@ SET(HEADERS
hcdump.hh
hysplitcdump.hh
hysplitcdump.i.hh
spectrum.hh
spectrumpcfstream.i.hh
spectrumspestream.i.hh
)
#
......
......@@ -566,6 +566,13 @@ void eafstream::writeString(const std::string &var)
mBytesWritten += var.size();
}
void eafstream::writeString(const std::string &var, size_t length, char filler)
{
std::string copy(var);
copy.resize(length, filler);
this->writeString(copy);
}
/**
* \brief Returns the number of bytes read since the last headers marker
* \return int
......
......@@ -94,6 +94,8 @@ class RADIX_PUBLIC eafstream : public std::fstream
*/
std::string readString(int size);
void writeString(const std::string &var);
void writeString(const std::string &var, size_t length, char filler = ' ');
/**
* \brief Returns the number of bytes read since the last header marker
* \return int
......
#include <algorithm>
#include <cmath>
#include "radixio/spectrum.hh"
namespace radix
{
short Spectrum::nrps() const
{
// Calculate the number of records per spectrum required
// Used by PCF file to allocate space in binary file structure
// Go through each SpectrumData object and find the maximum number of channels
auto comparator = [](const SpectrumData &a, const SpectrumData &b) {
return a.numberOfChannels < b.numberOfChannels;
};
int maxChannels = (*std::max_element(mSpectrumData.begin(),
mSpectrumData.end(), comparator))
.numberOfChannels;
short nrps = short(std::ceil((float(maxChannels) / 64.f) + 1.f));
return nrps;
}
bool Spectrum::dhsVersion() const { return mDhsVersion; }
void Spectrum::setDhsVersion(bool dhsType) { mDhsVersion = dhsType; }
const std::string &Spectrum::energyCalibrationLabel() const
{
return mEnergyCalibrationLabel;
}
void Spectrum::setEnergyCalibrationLabel(
const std::string &energyCalibrationLabel)
{
mEnergyCalibrationLabel = energyCalibrationLabel;
}
float Spectrum::energyCalibrationOffset() const
{
return mEnergyCalibrationOffset;
}
void Spectrum::setEnergyCalibrationOffset(float energyCalibrationOffset)
{
mEnergyCalibrationOffset = energyCalibrationOffset;
}
float Spectrum::energyCalibrationGain() const { return mEnergyCalibrationGain; }
void Spectrum::setEnergyCalibrationGain(float energyCalibrationGain)
{
mEnergyCalibrationGain = energyCalibrationGain;
}
float Spectrum::energyCalibrationQuadraticTerm() const
{
return mEnergyCalibrationQuadraticTerm;
}
void Spectrum::setEnergyCalibrationQuadraticTerm(
float energyCalibrationQuadraticTerm)
{
mEnergyCalibrationQuadraticTerm = energyCalibrationQuadraticTerm;
}
float Spectrum::energyCalibrationCubicTerm() const
{
return mEnergyCalibrationCubicTerm;
}
void Spectrum::setEnergyCalibrationCubicTerm(float energyCalibrationCubicTerm)
{
mEnergyCalibrationCubicTerm = energyCalibrationCubicTerm;
}
float Spectrum::energyCalibrationLowEnergy() const
{
return mEnergyCalibrationLowEnergy;
}
void Spectrum::setEnergyCalibrationLowEnergy(float energyCalibrationLowEnergy)
{
mEnergyCalibrationLowEnergy = energyCalibrationLowEnergy;
}
const std::string &Spectrum::lastModifiedHash() const
{
return mLastModifiedHash;
}
void Spectrum::setLastModifiedHash(const std::string &lastModifiedHash)
{
mLastModifiedHash = lastModifiedHash;
}
std::string Spectrum::uuid() const { return mUuid; }
void Spectrum::setUuid(const std::string &uuid) { mUuid = uuid; }
const std::string &Spectrum::inspection() const { return mInspection; }
void Spectrum::setInspection(const std::string &inspection)
{
mInspection = inspection;
}
short Spectrum::laneNumber() const { return mLaneNumber; }
void Spectrum::setLaneNumber(short laneNumber) { mLaneNumber = laneNumber; }
const std::string &Spectrum::measurementRemark() const
{
return mMeasurementRemark;
}
void Spectrum::setMeasurementRemark(const std::string &measurementRemark)
{
mMeasurementRemark = measurementRemark;
}
const std::string &Spectrum::instrumentType() const { return mInstrumentType; }
void Spectrum::setInstrumentType(const std::string &instrumentType)
{
mInstrumentType = instrumentType;
}
const std::string &Spectrum::manufacturer() const { return mManufacturer; }
void Spectrum::setManufacturer(const std::string &manufacturer)
{
mManufacturer = manufacturer;
}
const std::string &Spectrum::instrumentModel() const
{
return mInstrumentModel;
}
void Spectrum::setInstrumentModel(const std::string &instrumentModel)
{
mInstrumentModel = instrumentModel;
}
const std::string &Spectrum::instrumentID() const { return mInstrumentID; }
void Spectrum::setInstrumentID(const std::string &instrumentID)
{
mInstrumentID = instrumentID;
}
const std::string &Spectrum::itemDescription() const
{
return mItemDescription;
}
void Spectrum::setItemDescription(const std::string &itemDescription)
{
mItemDescription = itemDescription;
}
const std::string &Spectrum::measurementLocationName() const
{
return mMeasurementLocationName;
}
void Spectrum::setMeasurementLocationName(
const std::string &measurementLocationName)
{
mMeasurementLocationName = measurementLocationName;
}
const std::string &Spectrum::measurementLocationCoords() const
{
return mMeasurementLocationCoords;
}
void Spectrum::setMeasurementLocationCoords(
const std::string &measurementLocationCoords)
{
mMeasurementLocationCoords = measurementLocationCoords;
}
short Spectrum::itemDetectorDistance() const { return mItemDetectorDistance; }
void Spectrum::setItemDetectorDistance(short itemDetectorDistance)
{
mItemDetectorDistance = itemDetectorDistance;
}
short Spectrum::occupancyNumber() const { return mOccupancyNumber; }
void Spectrum::setOccupancyNumber(short occupancyNumber)
{
mOccupancyNumber = occupancyNumber;
}
const std::string &Spectrum::cargoType() const { return mCargoType; }
void Spectrum::setCargoType(const std::string &cargoType)
{
mCargoType = cargoType;
}
const std::string &Spectrum::deviationPairPresence() const
{
return mDeviationPairPresence;
}
void Spectrum::setDeviationPairPresence(
const std::string &deviationPairPresence)
{
mDeviationPairPresence = deviationPairPresence;
}
std::array<std::array<std::array<std::array<float, 4>, 8>, 8>, 20>
Spectrum::deviationPairEnergies() const
{
return mDeviationPairEnergies;
}
void Spectrum::setDeviationPairEnergies(
const std::array<std::array<std::array<std::array<float, 4>, 8>, 8>, 20>
&deviationPairEnergies)
{
mDeviationPairEnergies = deviationPairEnergies;
}
std::array<std::array<std::array<std::array<float, 4>, 8>, 8>, 20>
Spectrum::deviationPairOffsets() const
{
return mDeviationPairOffsets;
}
void Spectrum::setDeviationPairOffsets(
const std::array<std::array<std::array<std::array<float, 4>, 8>, 8>, 20>
&deviationPairOffsets)
{
mDeviationPairOffsets = deviationPairOffsets;
}
int Spectrum::spectrumDataCount() { return mSpectrumData.size(); }
void Spectrum::spectrumData(int i, std::string &title, std::string &source,
std::string &description, std::string &dateTime,
std::string &tag, float &liveTime, float &totalTime,
float &energyCalOffset, float &energyCalGain,
float &energyCalQuadraticTerm,
float &energyCalCubicTerm,
float &energyCalLowEnergyTerm, float &occupancyFlag,
float &totalNeutronCount, int &numberOfChannels,
std::vector<float> &countsByChannel)
{
if (i >= 0 && i < mSpectrumData.size())
{
size_t i_ = size_t(i);
SpectrumData spectrum = mSpectrumData[i_];
title = spectrum.title;
source = spectrum.source;
description = spectrum.description;
dateTime = spectrum.dateTime;
tag = spectrum.tag;
liveTime = spectrum.liveTime;
totalTime = spectrum.totalTime;
energyCalOffset = spectrum.energyCalOffset;
energyCalGain = spectrum.energyCalGain;
energyCalQuadraticTerm = spectrum.energyCalQuadraticTerm;
energyCalCubicTerm = spectrum.energyCalCubicTerm;
energyCalLowEnergyTerm = spectrum.energyCalLowEnergyTerm;
occupancyFlag = spectrum.occupancyFlag;
totalNeutronCount = spectrum.totalNeutronCount;
numberOfChannels = spectrum.numberOfChannels;
countsByChannel.clear();
for (size_t j = 0; j < spectrum.countsByChannel.size(); ++j)
{
countsByChannel.push_back(spectrum.countsByChannel[j]);
}
}
}
void Spectrum::addSpectrumData(
std::string title, std::string source, std::string description,
std::string dateTime, std::string tag, float liveTime, float totalTime,
float energyCalOffset, float energyCalGain, float energyCalQuadraticTerm,
float energyCalCubicTerm, float energyCalLowEnergyTerm, float occupancyFlag,
float totalNeutronCount, int numberOfChannels,
std::vector<float> countsByChannel)
{
SpectrumData spectrumData;
spectrumData.title = title;
spectrumData.source = source;
spectrumData.description = description;
spectrumData.dateTime = dateTime;
spectrumData.tag = tag;
spectrumData.liveTime = liveTime;
spectrumData.totalTime = totalTime;
spectrumData.energyCalOffset = energyCalOffset;
spectrumData.energyCalGain = energyCalGain;
spectrumData.energyCalQuadraticTerm = energyCalQuadraticTerm;
spectrumData.energyCalCubicTerm = energyCalCubicTerm;
spectrumData.energyCalLowEnergyTerm = energyCalLowEnergyTerm;
spectrumData.occupancyFlag = occupancyFlag;
spectrumData.totalNeutronCount = totalNeutronCount;
spectrumData.numberOfChannels = numberOfChannels;
for (size_t i = 0; i < countsByChannel.size(); ++i)
{
spectrumData.countsByChannel.push_back(countsByChannel[i]);
}
mSpectrumData.push_back(spectrumData);
}
}
#ifndef RADIX_RADIXIO_SPECTRUM_HH_
#define RADIX_RADIXIO_SPECTRUM_HH_
#include <array>
#include <memory>
#include <string>
#include <vector>
#include "radixcore/visibility.hh"
namespace radix
{
class RADIX_PUBLIC Spectrum
{
public:
typedef std::shared_ptr<Spectrum> SP;
struct SpectrumData
{
std::string title, source, description, dateTime, tag;
float liveTime, totalTime, energyCalOffset, energyCalGain,
energyCalQuadraticTerm, energyCalCubicTerm, energyCalLowEnergyTerm,
occupancyFlag, totalNeutronCount;
int numberOfChannels;
std::vector<float> countsByChannel;
}; // struct SpectrumData
short nrps() const;
bool dhsVersion() const;
void setDhsVersion(bool dhsVersion);
const std::string &energyCalibrationLabel() const;
void setEnergyCalibrationLabel(const std::string &energyCalibrationLabel);
float energyCalibrationOffset() const;
void setEnergyCalibrationOffset(float energyCalibrationOffset);
float energyCalibrationGain() const;
void setEnergyCalibrationGain(float energyCalibrationGain);
float energyCalibrationQuadraticTerm() const;
void setEnergyCalibrationQuadraticTerm(float energyCalibrationQuadraticTerm);
float energyCalibrationCubicTerm() const;
void setEnergyCalibrationCubicTerm(float energyCalibrationCubicTerm);
float energyCalibrationLowEnergy() const;
void setEnergyCalibrationLowEnergy(float energyCalibrationLowEnergy);
const std::string &lastModifiedHash() const;
void setLastModifiedHash(const std::string &lastModifiedHash);
std::string uuid() const;
void setUuid(const std::string &uuid);
const std::string &inspection() const;
void setInspection(const std::string &inspection);
short laneNumber() const;
void setLaneNumber(short laneNumber);
const std::string &measurementRemark() const;
void setMeasurementRemark(const std::string &measurementRemark);
const std::string &instrumentType() const;
void setInstrumentType(const std::string &instrumentType);
const std::string &manufacturer() const;
void setManufacturer(const std::string &manufacturer);
const std::string &instrumentModel() const;
void setInstrumentModel(const std::string &instrumentModel);
const std::string &instrumentID() const;
void setInstrumentID(const std::string &instrumentID);
const std::string &itemDescription() const;
void setItemDescription(const std::string &itemDescription);
const std::string &measurementLocationName() const;
void setMeasurementLocationName(const std::string &measurementLocationName);
const std::string &measurementLocationCoords() const;
void setMeasurementLocationCoords(
const std::string &measurementLocationCoords);
short itemDetectorDistance() const;
void setItemDetectorDistance(short itemDetectorDistance);
short occupancyNumber() const;
void setOccupancyNumber(short occupancyNumber);
const std::string &cargoType() const;
void setCargoType(const std::string &cargoType);
const std::string &deviationPairPresence() const;
void setDeviationPairPresence(const std::string &deviationPairPresence);
std::array<std::array<std::array<std::array<float, 4>, 8>, 8>, 20>
deviationPairEnergies() const;
void setDeviationPairEnergies(
const std::array<std::array<std::array<std::array<float, 4>, 8>, 8>, 20>
&deviationPairEnergies);
std::array<std::array<std::array<std::array<float, 4>, 8>, 8>, 20>
deviationPairOffsets() const;
void setDeviationPairOffsets(
const std::array<std::array<std::array<std::array<float, 4>, 8>, 8>, 20>
&deviationPairOffsets);
int spectrumDataCount();
void spectrumData(int i, std::string &title, std::string &source,
std::string &description, std::string &dateTime,
std::string &tag, float &liveTime, float &totalTime,
float &energyCalOffset, float &energyCalGain,
float &energyCalQuadraticTerm, float &energyCalCubicTerm,
float &energyCalLowEnergyTerm, float &occupancyFlag,
float &totalNeutronCount, int &numberOfChannels,
std::vector<float> &countsByChannel);
void addSpectrumData(std::string title, std::string source,
std::string description, std::string dateTime,
std::string tag, float liveTime, float totalTime,
float energyCalOffset, float energyCalGain,
float energyCalQuadraticTerm, float energyCalCubicTerm,
float energyCalLowEnergyTerm, float occupancyFlag,
float totalNeutronCount, int numberOfChannels,
std::vector<float> countsByChannel);
private:
// PCF file objects -
// TODO work out what/how to change to support SPE and PCF
// Is the file a DHS version?
bool mDhsVersion = false;
// Non-DHS header objects
std::string mEnergyCalibrationLabel;
float mEnergyCalibrationOffset;
float mEnergyCalibrationGain;
float mEnergyCalibrationQuadraticTerm;
float mEnergyCalibrationCubicTerm;
float mEnergyCalibrationLowEnergy;
// DHS header objects
std::string mLastModifiedHash;
std::string mUuid;
std::string mInspection;
short mLaneNumber;
std::string mMeasurementRemark;
std::string mInstrumentType;
std::string mManufacturer;
std::string mInstrumentModel;
std::string mInstrumentID;
std::string mItemDescription;
std::string mMeasurementLocationName;
std::string mMeasurementLocationCoords;
short mItemDetectorDistance;
short mOccupancyNumber;
std::string mCargoType;
// Deviation pair presence indicator
std::string mDeviationPairPresence;
// Deviation pairs
std::array<std::array<std::array<std::array<float, 4>, 8>, 8>, 20>
mDeviationPairEnergies;
std::array<std::array<std::array<std::array<float, 4>, 8>, 8>, 20>
mDeviationPairOffsets;
// Spectrum data
std::vector<SpectrumData> mSpectrumData;
}; // class RADIX_PUBLIC Spectrum
template <typename data_type>
class RADIX_PUBLIC SpectrumPCFStream
{
public:
SpectrumPCFStream(data_type container);
/**
* @brief read_from Read a PCF-formatted spectrum file
* @param file Path to the PCF formatted spectrum file
* @return Success (true)/failure (false) flag
*
* Requires data_type to have the following methods:
* (to support both PCF and SPE formats)
*
* setDhsVersion(bool dhsVersion);
* setEnergyCalibrationLabel(const std::string &energyCalibrationLabel);
* setEnergyCalibrationOffset(float energyCalibrationOffset);
* setEnergyCalibrationGain(float energyCalibrationGain);
* setEnergyCalibrationQuadraticTerm(float energyCalibrationQuadraticTerm);
* setEnergyCalibrationCubicTerm(float energyCalibrationCubicTerm);
* setEnergyCalibrationLowEnergy(float energyCalibrationLowEnergy);
* setLastModifiedHash(const std::string &lastModifiedHash);
* setUuid(const std::string &uuid);
* setInspection(const std::string &inspection);
* setLaneNumber(short laneNumber);
* setMeasurementRemark(const std::string &measurementRemark);
* setInstrumentType(const std::string &instrumentType);
* setManufacturer(const std::string &manufacturer);
* setInstrumentModel(const std::string &instrumentModel);
* setInstrumentID(const std::string &instrumentID);
* setItemDescription(const std::string &itemDescription);
* setMeasurementLocationName(const std::string &measurementLocationName);
* setMeasurementLocationCoords(const std::string &measurementLocationCoords);
* setItemDetectorDistance(short itemDetectorDistance);
* setOccupancyNumber(short occupancyNumber);
* setCargoType(const std::string &cargoType);
* setDeviationPairPresence(const std::string &deviationPairPresence);
* setDeviationPairEnergies(const
* std::array<std::array<std::array<std::array<float, 4>, 8>, 8>, 20>
* &deviationPairEnergies);
* setDeviationPairOffsets(const
* std::array<std::array<std::array<std::array<float, 4>, 8>, 8>, 20>
* &deviationPairOffsets);