112#include "Minuit2/FCNBase.h"
288 double operator()(
const std::vector<double> &par)
const;
313 std::vector<uint32_t>
fDims = {0, 0, 0};
541 PNeXus(
const std::string fln,
const bool printDebug=
false);
610 template <
typename T>
623 template <
typename T>
625 return std::any_cast<PNXdata<T>>(
fDataMap.at(path));
636 template <
typename T>
638 return std::any_cast<PNXdata<T>&>(
fDataMap.at(path));
673 template <
typename T>
677 auto& dataset = std::any_cast<PNXdata<T>&>(
fDataMap.at(path));
678 dataset.SetData(newData);
680 }
catch (
const std::bad_any_cast&) {
692 template <
typename T>
696 auto& dataset = std::any_cast<PNXdata<T>&>(
fDataMap.at(path));
697 dataset.SetDimensions(newDimensions);
699 }
catch (
const std::bad_any_cast&) {
712 template <
typename T>
714 const std::any& attrValue) {
717 auto& dataset = std::any_cast<PNXdata<T>&>(
fDataMap.at(path));
718 dataset.AddAttribute(attrName, attrValue);
720 }
catch (
const std::bad_any_cast&) {
732 template <
typename T>
736 auto& dataset = std::any_cast<PNXdata<T>&>(
fDataMap.at(path));
737 auto& attrs = dataset.GetAttributes();
738 auto it = attrs.find(attrName);
739 if (it != attrs.end()) {
744 }
catch (
const std::bad_any_cast&) {
758 template <
typename T>
759 bool AddDataset(
const std::string& path,
const std::vector<T>& data,
760 const std::vector<uint32_t>& dimensions,
778 template <
typename T>
780 const std::vector<uint32_t>& newDimensions) {
783 auto& dataset = std::any_cast<PNXdata<T>&>(
fDataMap.at(path));
784 dataset.SetData(newData);
785 dataset.SetDimensions(newDimensions);
787 }
catch (
const std::bad_any_cast&) {
817 int WriteNexusFile(
const std::string& filename,
int idfVersion = 2);
830 bool AddGroupAttribute(
const std::string& groupPath,
const std::string& attrName,
831 const std::any& attrValue);
847 bool HasGroupAttribute(
const std::string& groupPath,
const std::string& attrName)
const;
856 std::any
GetGroupAttribute(
const std::string& groupPath,
const std::string& attrName)
const;
863 const std::map<std::string, std::any>&
GetGroupAttributes(
const std::string& groupPath)
const;
885 bool AddRootAttribute(
const std::string& attrName,
const std::any& attrValue);
943 template <
typename T>
952 const std::map<std::string, std::any>& attributes);
967 std::map<std::string, int32>& vgroupCache);
1029 static std::vector<std::string>
SplitPath(
const std::string& path);
1092 template <
typename T>
1233 double operator()(
const std::vector<double> &par)
const;
1508 PNeXus(
const std::string fln,
const bool printDebug=
false);
1574 template <
typename T>
1587 template <
typename T>
1589 return std::any_cast<PNXdata<T>>(
fDataMap.at(path));
1600 template <
typename T>
1602 return std::any_cast<PNXdata<T>&>(
fDataMap.at(path));
1637 template <
typename T>
1641 auto& dataset = std::any_cast<PNXdata<T>&>(
fDataMap.at(path));
1642 dataset.SetData(newData);
1644 }
catch (
const std::bad_any_cast&) {
1656 template <
typename T>
1660 auto& dataset = std::any_cast<PNXdata<T>&>(
fDataMap.at(path));
1661 dataset.SetDimensions(newDimensions);
1663 }
catch (
const std::bad_any_cast&) {
1676 template <
typename T>
1678 const std::any& attrValue) {
1681 auto& dataset = std::any_cast<PNXdata<T>&>(
fDataMap.at(path));
1682 dataset.AddAttribute(attrName, attrValue);
1684 }
catch (
const std::bad_any_cast&) {
1696 template <
typename T>
1700 auto& dataset = std::any_cast<PNXdata<T>&>(
fDataMap.at(path));
1701 auto& attrs = dataset.GetAttributes();
1702 auto it = attrs.find(attrName);
1703 if (it != attrs.end()) {
1708 }
catch (
const std::bad_any_cast&) {
1722 template <
typename T>
1723 bool AddDataset(
const std::string& path,
const std::vector<T>& data,
1724 const std::vector<hsize_t>& dimensions,
1725 const H5::DataType& dataType = H5::PredType::NATIVE_INT) {
1742 template <
typename T>
1744 const std::vector<hsize_t>& newDimensions) {
1747 auto& dataset = std::any_cast<PNXdata<T>&>(
fDataMap.at(path));
1748 dataset.SetData(newData);
1749 dataset.SetDimensions(newDimensions);
1751 }
catch (
const std::bad_any_cast&) {
1803 int WriteNexusFile(
const std::string& filename,
int idfVersion = 2);
1816 bool AddGroupAttribute(
const std::string& groupPath,
const std::string& attrName,
1817 const std::any& attrValue);
1833 bool HasGroupAttribute(
const std::string& groupPath,
const std::string& attrName)
const;
1842 std::any
GetGroupAttribute(
const std::string& groupPath,
const std::string& attrName)
const;
1849 const std::map<std::string, std::any>&
GetGroupAttributes(
const std::string& groupPath)
const;
1871 bool AddRootAttribute(
const std::string& attrName,
const std::any& attrValue);
1936 template <
typename T>
1944 void WriteGroupAttributes(H5::Group& group,
const std::map<std::string, std::any>& attributes);
2003 static std::vector<std::string>
SplitPath(
const std::string& path);
2013 std::string
FindAttributeName(H5::H5File& obj,
const std::string& requestedName);
2023 std::string
FindGroupPath(H5::H5File& parent,
const std::string& requestedPath);
2032 std::string
FindGroupPath(H5::Group& parent,
const std::string& requestedPath);
2043 std::string
FindDatasetPath(H5::H5File& parent,
const std::string& requestedPath);
2053 std::string
FindDatasetPath(H5::Group& parent,
const std::string& requestedPath);
2089 template <
typename T>
Template class for storing HDF4 dataset content with attributes.
const std::vector< T > & GetData() const
Get the data as a vector.
std::vector< uint32_t > fDimensions
Dimensions of the dataset.
std::vector< T > & GetData()
Get mutable reference to the data vector.
PNXdata()
Default constructor.
std::vector< T > fData
Data storage (flattened multi-dimensional array)
void SetDataType(const H4DataType &dataType)
Set the HDF4 DataType.
std::map< std::string, std::any > & GetAttributes()
Get mutable reference to all attributes.
void SetData(const std::vector< T > &data)
Set the data vector.
void SetDimensions(const std::vector< uint32_t > &dims)
Set the dimensions of the dataset.
std::map< std::string, std::any > fAttributes
Attributes associated with this dataset.
const std::map< std::string, std::any > & GetAttributes() const
Get all attributes.
bool HasAttribute(const std::string &name) const
Check if an attribute exists.
H4DataType fDataType
HDF4 datatype of the dataset.
void AddAttribute(const std::string &name, const std::any &value)
Add an attribute to this dataset.
H4DataType GetDataType() const
Get the HDF4 DataType.
const std::vector< uint32_t > & GetDimensions() const
Get the dimensions of the dataset.
size_t GetNumElements() const
Get total number of elements.
size_t GetRank() const
Get the number of dimensions.
std::any GetAttribute(const std::string &name) const
Get an attribute by name.
PNXdata(const H4DataType &dataType)
Constructor with datatype.
std::vector< int > fCounts
Count data for minimization.
double Up() const
Get the error definition for the minimizer (FCNBase requirement)
int fLgbBin
Last good bin for analysis.
std::vector< uint32_t > fDims
Dimensions of count data [periods, spectra, bins].
float fTimeResolution
Time resolution in picoseconds.
int fT0Bin
T0 bin number (time zero reference)
PNeXusDeadTime(const PNeXus *nxs, bool debug=false)
Constructor - initializes dead time calculator from NeXus data.
int fGoodFrames
Number of good time frames for analysis.
bool IsValid()
Check if the dead time calculator was initialized successfully.
unsigned int fIdx
Current spectrum index being minimized.
const std::vector< uint32_t > & GetDimensions() const
Get the dimensions of the count dataset.
double fUp
UP parameter for Minuit2 (0.5 for chi-square)
bool fDebug
Debug flag - if true, print additional diagnostic information.
double operator()(const std::vector< double > &par) const
Function call operator - calculates chi-square for given parameters.
std::vector< float > fDeadTime
Dead time values per detector (microseconds)
bool fValid
Validity flag - true if required data was loaded successfully.
int fFgbBin
First good bin for analysis.
void Minimize(const int i)
Minimize dead time for a specific detector spectrum.
NeXus HDF4 file reader with case-insensitive path lookup.
bool AddRootAttribute(const std::string &attrName, const std::any &attrValue)
bool RemoveDataset(const std::string &path)
Remove a dataset from the data map.
bool RemoveDatasetAttribute(const std::string &path, const std::string &attrName)
Remove an attribute from a dataset.
std::string fHdf4LibVersion
HDF4 library version used when calling PNeXus.
int32 WriteStringDataset(int32 sd_id, const std::string &path, const PNXdata< std::string > &data)
Write a string dataset with attributes and return its SDS reference.
bool RemoveGroupAttribute(const std::string &groupPath, const std::string &attrName)
Remove an attribute from a group.
bool AddGroupAttribute(const std::string &groupPath, const std::string &attrName, const std::any &attrValue)
Add or update an attribute for a group.
int32 FindDatasetIndex(int32 sd_id, const std::string &requestedName)
Find dataset index with case-insensitive matching.
void SetDataset(const std::string &path, const PNXdata< T > &data)
Add or update a dataset in the data map.
bool AddDataset(const std::string &path, const std::vector< T > &data, const std::vector< uint32_t > &dimensions, const H4DataType &dataType=H4DataType::kINT32)
Create and add a new dataset with data, dimensions, and optional attributes.
const std::map< std::string, std::any > & GetGroupAttributes(const std::string &groupPath) const
Get all attributes for a group.
~PNeXus()
Destructor - closes HDF4 file if open.
bool UpdateDatasetData(const std::string &path, const std::vector< T > &newData)
Update the data of an existing dataset.
bool UpdateDatasetDimensions(const std::string &path, const std::vector< uint32_t > &newDimensions)
Update the dimensions of an existing dataset.
static H4DataType convertHdf4Type(int32 hdf4_type)
Convert HDF4 data type to H4DataType enum.
void WriteDatasetAttributes(int32 sds_id, const PNXdata< T > &data)
Write dataset attributes from PNXdata object.
std::string fFileName
NeXus HDF4 filename.
std::string GetHdf4Version() const
Get the hdf4 version of the NeXus file.
bool HasGroupAttribute(const std::string &groupPath, const std::string &attrName) const
Check if a group has a specific attribute.
static bool CaseInsensitiveEquals(const std::string &a, const std::string &b)
Compare two strings case-insensitively.
int32 fSdId
HDF4 SD interface identifier.
std::string fNeXusVersion
NeXus version of the file.
void HandleIdfV1(int32 sd_id)
Read datasets for IDF version 1 file structure.
std::map< std::string, std::map< std::string, std::any > > fGroupAttributes
Map of group paths to their attributes.
const std::map< std::string, std::any > & GetDataMap() const
Get the data map containing all datasets.
int32 FindDatasetRefByPath(const std::string &path)
Find dataset reference by navigating VGroup hierarchy.
std::string GetHdf4LibVersion() const
Get the hdf4 library version.
std::string GetNeXusVersion() const
Get the NeXus version of the file.
void WriteVGroupAttributes(int32 vgroup_id, const std::map< std::string, std::any > &attributes)
Write attributes to a Vgroup.
PNXdata< T > GetDataset(const std::string &path) const
Get a dataset from the data map.
std::string fHdf4Version
HDF4 version of the file.
void ReadStringDataset(int32 sd_id, const std::string &path)
Read a string dataset and store in data map.
std::map< std::string, std::any > fDataMap
Map of HDF4 paths to PNXdata objects.
std::map< std::string, std::any > & GetDataMap()
Get mutable reference to the data map.
int WriteNexusFile(const std::string &filename, int idfVersion=2)
Write the data map contents to a NeXus HDF4 file.
bool fPrintDebug
if true print additional debug information
int32 CreateVGroupHierarchy(int32 file_id, const std::string &path, std::map< std::string, int32 > &vgroupCache)
Create the Vgroup hierarchy for a given path.
bool AddDatasetAttribute(const std::string &path, const std::string &attrName, const std::any &attrValue)
Add or update an attribute for a dataset.
int GetIdfVersion() const
Get the Idf version of the file.
static int32 convertToHdf4Type(H4DataType dataType)
Convert H4DataType enum to HDF4 data type.
void ReadIntDataset(int32 sd_id, const std::string &path)
Read an integer dataset and store in data map.
bool HasDataset(const std::string &path) const
Check if a dataset path exists in the data map.
std::string FindAttributeName(int32 sd_id, const std::string &requestedName)
std::any GetGroupAttribute(const std::string &groupPath, const std::string &attrName) const
Get an attribute value from a group.
int32 WriteIntDataset(int32 sd_id, const std::string &path, const PNXdata< int > &data)
Write an integer dataset with attributes and return its SDS reference.
void HandleIdfV2(int32 sd_id)
Read datasets for IDF version 2 file structure.
bool ModifyDataset(const std::string &path, const std::vector< T > &newData, const std::vector< uint32_t > &newDimensions)
Modify an existing dataset's data and dimensions.
static std::vector< std::string > SplitPath(const std::string &path)
bool ClearGroupAttributes(const std::string &groupPath)
Clear all attributes from a group.
void WriteFileAttributes(int32 sd_id)
Write root-level file attributes.
void ReadFloatDataset(int32 sd_id, const std::string &path)
Read a float dataset and store in data map.
void ClearDataMap()
Clear all datasets from the data map.
void Dump()
Print a human-readable dump of the NeXus file contents.
PNXdata< T > & GetDatasetRef(const std::string &path)
Get a mutable reference to a dataset in the data map.
size_t GetNumDatasets() const
Get the number of datasets in the data map.
int32 WriteFloatDataset(int32 sd_id, const std::string &path, const PNXdata< float > &data)
Write a float dataset with attributes and return its SDS reference.
void ReadDatasetAttributes(int32 sds_id, PNXdata< T > &data)
Read dataset attributes and add to PNXdata object.
std::string GetFileName() const
Get the filename of the NeXus file.
int32 fFileId
HDF4 file identifier.
int fIdfVersion
IDF version of the NeXus file.
int ReadNexusFile()
Read and parse the NeXus HDF4 file.
Template class for storing HDF5 dataset content with attributes.
const std::vector< T > & GetData() const
Get the data as a vector.
std::vector< T > & GetData()
Get mutable reference to the data vector.
std::map< std::string, std::any > & GetAttributes()
Get mutable reference to all attributes.
H5::DataType fDataType
HDF5 datatype of the dataset.
PNXdata(const H5::DataType &dataType)
Constructor with datatype.
H5::DataType GetDataType() const
Get the HDF5 DataType.
void SetDimensions(const std::vector< hsize_t > &dims)
Set the dimensions of the dataset.
void SetDataType(const H5::DataType &dataType)
Set the HDF5 DataType.
std::map< std::string, std::any > fAttributes
Attributes associated with this dataset.
std::any GetAttribute(const std::string &name) const
Get an attribute by name.
const std::map< std::string, std::any > & GetAttributes() const
Get all attributes.
void AddAttribute(const std::string &name, const std::any &value)
Add an attribute to this dataset.
std::vector< T > fData
Data storage (flattened multi-dimensional array)
PNXdata()
Default constructor.
const std::vector< hsize_t > & GetDimensions() const
Get the dimensions of the dataset.
size_t GetNumElements() const
Get total number of elements.
std::vector< hsize_t > fDimensions
Dimensions of the dataset.
size_t GetRank() const
Get the number of dimensions.
void SetData(const std::vector< T > &data)
Set the data vector.
bool HasAttribute(const std::string &name) const
Check if an attribute exists.
bool fValid
Validity flag - true if required data was loaded successfully.
unsigned int fIdx
Current spectrum index being minimized.
std::vector< int > fCounts
Count data for minimization.
double fUp
UP parameter for Minuit2 (0.5 for chi-square)
int fGoodFrames
Number of good time frames for analysis.
bool IsValid()
Check if the dead time calculator was initialized successfully.
PNeXusDeadTime(const PNeXus *nxs, bool debug=false)
Constructor - initializes dead time calculator from NeXus data.
std::vector< hsize_t > fDims
Dimensions of count data [periods, spectra, bins].
const std::vector< hsize_t > & GetDimensions() const
Get the dimensions of the count dataset.
int fT0Bin
T0 bin number (time zero reference)
double operator()(const std::vector< double > &par) const
Function call operator - calculates chi-square for given parameters.
void Minimize(const int i)
Minimize dead time for a specific detector spectrum.
bool fDebug
Debug flag - if true, print additional diagnostic information.
float fTimeResolution
Time resolution in picoseconds.
std::vector< float > fDeadTimeEstimated
Dead time values per detector (microseconds) as estimated from the data.
double Up() const
Get the error definition for the minimizer (FCNBase requirement)
int fFgbBin
First good bin for analysis.
std::vector< float > GetDeadTimeEstimated()
Get the estimated dead time values from minimization.
std::vector< float > fDeadTime
Dead time values per detector (microseconds) from the file.
int fLgbBin
Last good bin for analysis.
NeXus HDF5 file reader with case-insensitive path lookup.
std::string GetFileName() const
Get the filename of the NeXus file.
void WriteFloatDataset(H5::H5File &file, const std::string &path, const PNXdata< float > &data)
Write a float dataset with attributes.
void HandleIdfV2(H5::H5File &file)
Read datasets for IDF version 2 file structure.
std::string GetNeXusVersion() const
Get the NeXus version of the file.
std::map< std::string, std::any > & GetDataMap()
Get mutable reference to the data map.
std::string FindGroupPath(H5::H5File &parent, const std::string &requestedPath)
void HandleIdfV1(H5::H5File &file)
Read datasets for IDF version 1 file structure.
bool HasDataset(const std::string &path) const
Check if a dataset path exists in the data map.
bool RemoveDatasetAttribute(const std::string &path, const std::string &attrName)
Remove an attribute from a dataset.
std::string FindAttributeName(H5::H5File &obj, const std::string &requestedName)
bool RemoveGroupAttribute(const std::string &groupPath, const std::string &attrName)
Remove an attribute from a group.
void WriteFileAttributes(H5::H5File &file)
Write root-level file attributes.
bool AddRootAttribute(const std::string &attrName, const std::any &attrValue)
void ReadFloatDataset(H5::H5File &file, const std::string &path)
Read a float dataset and store in data map.
std::string fFileName
NeXus HDF5 filename.
bool ModifyDataset(const std::string &path, const std::vector< T > &newData, const std::vector< hsize_t > &newDimensions)
Modify an existing dataset's data and dimensions.
int ReadNexusFile()
Read and parse the NeXus HDF5 file.
std::string fHdf5Version
HDF5 version of the file.
std::map< std::string, std::map< std::string, std::any > > fGroupAttributes
Map of group paths to their attributes.
void ReadIntDataset(H5::H5File &file, const std::string &path)
Read an integer dataset and store in data map.
const std::map< std::string, std::any > & GetDataMap() const
Get the data map containing all datasets.
void WriteGroupAttributes(H5::Group &group, const std::map< std::string, std::any > &attributes)
Write attributes to a group.
void WriteDatasetAttributes(H5::DataSet &dataset, const PNXdata< T > &data)
Write dataset attributes from PNXdata object.
static std::vector< std::string > SplitPath(const std::string &path)
bool AddGroupAttribute(const std::string &groupPath, const std::string &attrName, const std::any &attrValue)
int GetIdfVersion() const
Get the Idf version of the file.
void ReadDatasetAttributes(H5::DataSet &dataset, PNXdata< T > &data)
Read dataset attributes and add to PNXdata object.
H5::Group CreateGroupHierarchy(H5::H5File &file, const std::string &path)
Create nested group hierarchy for a given path.
void WriteIntDataset(H5::H5File &file, const std::string &path, const PNXdata< int > &data)
Write an integer dataset with attributes.
bool HasGroupAttribute(const std::string &groupPath, const std::string &attrName) const
Check if a group has a specific attribute.
std::map< std::string, std::any > fDataMap
Map of HDF5 paths to PNXdata objects.
bool ClearGroupAttributes(const std::string &groupPath)
Clear all attributes from a group.
void ReadStringDataset(H5::H5File &file, const std::string &path)
Read a string dataset and store in data map.
size_t GetNumDatasets() const
Get the number of datasets in the data map.
bool AddDataset(const std::string &path, const std::vector< T > &data, const std::vector< hsize_t > &dimensions, const H5::DataType &dataType=H5::PredType::NATIVE_INT)
Create and add a new dataset with data, dimensions, and optional attributes.
std::string fHdf5LibVersion
HDF5 library version used when calling PNeXus.
void SetDataset(const std::string &path, const PNXdata< T > &data)
Add or update a dataset in the data map.
const std::map< std::string, std::any > & GetGroupAttributes(const std::string &groupPath) const
Get all attributes for a group.
bool UpdateDatasetData(const std::string &path, const std::vector< T > &newData)
Update the data of an existing dataset.
bool fPrintDebug
if true print additional debug information
std::any GetGroupAttribute(const std::string &groupPath, const std::string &attrName) const
Get an attribute value from a group.
PNXdata< T > GetDataset(const std::string &path) const
Get a dataset from the data map.
bool UpdateDatasetDimensions(const std::string &path, const std::vector< hsize_t > &newDimensions)
Update the dimensions of an existing dataset.
int fIdfVersion
IDF version of the NeXus file.
std::string GetHdf5LibVersion() const
Get the HDF5 library version string.
std::string GetHdf5Version() const
Get the HDF5 version string from the file.
std::string FindDatasetPath(H5::H5File &parent, const std::string &requestedPath)
void ClearDataMap()
Clear all datasets from the data map.
PNXdata< T > & GetDatasetRef(const std::string &path)
Get a mutable reference to a dataset in the data map.
int WriteNexusFile(const std::string &filename, int idfVersion=2)
void WriteStringDataset(H5::H5File &file, const std::string &path, const PNXdata< std::string > &data)
Write a string dataset with attributes.
std::string fNeXusVersion
NeXus version of the file.
static bool CaseInsensitiveEquals(const std::string &a, const std::string &b)
Compare two strings case-insensitively.
bool RemoveDataset(const std::string &path)
Remove a dataset from the data map.
bool AddDatasetAttribute(const std::string &path, const std::string &attrName, const std::any &attrValue)
Add or update an attribute for a dataset.
HDF4 implementation of the NeXus file reader/writer.
H4DataType
HDF4 data type enumeration.
@ kUINT8
8-bit unsigned integer (DFNT_UINT8)
@ kFLOAT32
32-bit floating point (DFNT_FLOAT32)
@ kUINT16
16-bit unsigned integer (DFNT_UINT16)
@ kINT16
16-bit signed integer (DFNT_INT16)
@ kUINT32
32-bit unsigned integer (DFNT_UINT32)
@ kINT8
8-bit signed integer (DFNT_INT8)
@ kCHAR8
8-bit character (DFNT_CHAR8)
@ kFLOAT64
64-bit floating point (DFNT_FLOAT64)
@ kINT32
32-bit signed integer (DFNT_INT32)
HDF5 implementation of the NeXus file reader/writer.
Common utilities for NeXus file handling.
HDFType
Enumeration of supported HDF file types.
@ HDF5
HDF5 format (magic: 0x89 'H' 'D' 'F' 0x0d 0x0a 0x1a 0x0a)
@ Unknown
Unrecognized file format.
@ HDF4
HDF4 format (magic: 0x0e 0x03 0x13 0x01)
HDFType checkHDFType(const std::string &filename)
Determine the HDF format type of a file by reading its header.
std::string getIso8601TimestampLocal()
get the current time and return it as na IOS8601 time stamp.