13template <
class T> std::string
getType() {
14 if (std::is_same<T, std::int64_t>::value)
16 if (std::is_same<T, std::int32_t>::value)
18 if (std::is_same<T, double>::value)
20 if (std::is_same<T, float>::value)
26template <
class T> Json::Value convertToJsonValue(
const T
value) {
27 if (std::is_same<T, int64_t>::value)
28 return Json::Value(
static_cast<Json::Int64
>(
value));
29 else if (std::is_same<T, int32_t>::value)
30 return Json::Value(
static_cast<Json::Int
>(
value));
31 else if (std::is_same<T, double>::value || std::is_same<T, float>::value)
32 return Json::Value(
static_cast<double>(
value));
35Json::Value createNXAttributes(
const std::string &NXClass) {
36 Json::Value attributes;
37 attributes[0][
"name"] =
"NX_class";
38 attributes[0][
"values"] = NXClass;
42Json::Value createAttribute(
const std::string &name,
const std::string &values) {
43 Json::Value attribute;
44 attribute[
"name"] = name;
45 attribute[
"values"] = values;
49template <
class T> Json::Value createAttribute(
const std::string &name,
const std::vector<T> &values) {
50 Json::Value attribute;
51 attribute[
"name"] = name;
52 attribute[
"type"] = getType<T>();
53 for (
size_t i = 0; i < values.size(); ++i)
54 attribute[
"values"][
static_cast<int>(i)] = values[i];
59Json::Value createEmptyDataset(
const std::string &name,
const std::string &type) {
61 dataset[
"type"] =
"dataset";
62 dataset[
"name"] = name;
64 Json::Value datasetType;
65 datasetType[
"type"] = type;
67 dataset[
"dataset"] = datasetType;
72Json::Value createNX(
const std::string &name,
const std::string &NXClass) {
76 nx[
"children"].resize(0);
77 nx[
"attributes"] = createNXAttributes(NXClass);
82void appendToChildren(Json::Value &parent,
const Json::Value &child) {
83 Json::Value &children = parent[
"children"];
84 children[children.size()] = child;
87void resizeValues(Json::Value &values,
size_t size) {
89 values.resize(
static_cast<Json::ArrayIndex
>(size));
90 for (Json::ArrayIndex i = 0; i < values.size(); ++i)
93 for (
auto &val : values) {
95 for (
auto &child : val)
96 resizeValues(child, size);
98 resizeValues(val, size);
103template <
class T>
void fillValues(Json::Value &values,
const std::vector<T> &fillArray,
size_t &start,
size_t size) {
104 if (!values.isNull() && !values.empty()) {
105 for (
auto &val : values)
106 fillValues<T>(val, fillArray, start, size);
108 for (
size_t i = 0; i < size; ++i) {
109 values[
static_cast<int>(i)] = convertToJsonValue<T>(fillArray[start + i]);
116void addDataset(Json::Value &parent,
const std::string &name,
const std::vector<int> &arrayShape,
117 const std::vector<T> &data,
const std::string &attributesName =
"",
118 const std::string &attributesValues =
"") {
119 auto dataset = createEmptyDataset(name, getType<T>());
121 for (; i < static_cast<int>(arrayShape.size() - 1); ++i) {
122 auto s = arrayShape[i];
123 dataset[
"dataset"][
"size"][i] = s;
124 resizeValues(dataset[
"values"], s);
127 auto leafSize =
static_cast<size_t>(arrayShape[arrayShape.size() - 1]);
128 dataset[
"dataset"][
"size"][i] = convertToJsonValue<int64_t>(leafSize);
130 fillValues<T>(dataset[
"values"], data, start, leafSize);
132 if (!attributesName.empty())
133 dataset[
"attributes"][0] = createAttribute(attributesName, attributesValues);
135 appendToChildren(parent, dataset);
138void addTransformationChild(Json::Value &transformation,
const std::string &name,
const std::string &transformationType,
139 const std::string &dependency,
const std::string &units,
const std::vector<int> &arrayShape,
140 const std::vector<double> &values,
const std::vector<double> &vec) {
141 addDataset<double>(transformation, name, arrayShape, values,
"units", units);
142 auto index = transformation[
"children"].size() - 1;
143 Json::Value &child = transformation[
"children"][
index];
144 child[
"attributes"][1] = createAttribute(
"vector", vec);
145 child[
"attributes"][2] = createAttribute(
"depends_on", dependency);
146 child[
"attributes"][3] = createAttribute(
"transformation_type", transformationType);
149Json::Value &addNX(Json::Value &parent,
const std::string &name,
const std::string &NXClass) {
150 auto &children = parent[
"children"];
151 auto &child = children[children.size()];
152 child = createNX(name, NXClass);
156void addStream(Json::Value &parent,
const std::string &name,
const std::string &topic,
const std::string &source,
157 const std::string &writerModule) {
158 Json::Value streamGroup;
159 streamGroup[
"type"] =
"group";
160 streamGroup[
"name"] = name;
162 stream[
"type"] =
"stream";
163 stream[
"stream"][
"topic"] = topic;
164 stream[
"stream"][
"source"] = source;
165 stream[
"stream"][
"writer_module"] = writerModule;
166 appendToChildren(streamGroup, stream);
167 appendToChildren(parent, streamGroup);
172namespace JSONTestInstrumentBuilder {
174void initialiseRoot(
const Json::Value &root,
const std::string &name) { root[name]; }
176Json::Value &
addNXEntry(Json::Value &root,
const std::string &name) {
177 return addNX(root[
"nexus_structure"], name,
"NXentry");
180Json::Value &
addNXSample(Json::Value &entry,
const std::string &name) {
return addNX(entry, name,
"NXsample"); }
182Json::Value &
addNXInstrument(Json::Value &entry,
const std::string &name) {
return addNX(entry, name,
"NXinstrument"); }
185 auto instrumentName = createEmptyDataset(
"name",
"string");
186 instrumentName[
"values"] = name;
187 appendToChildren(instrument, instrumentName);
190Json::Value &
addNXSource(Json::Value &instrument,
const std::string &name) {
191 return addNX(instrument, name,
"NXsource");
194Json::Value &
addNXMonitor(Json::Value &entry,
const std::string &name) {
return addNX(entry, name,
"NXmonitor"); }
196 auto monitorName = createEmptyDataset(
"name",
"string");
197 monitorName[
"values"] = name;
198 appendToChildren(monitor, monitorName);
202 auto monitorDetID = createEmptyDataset(
"detector_id",
"int32");
203 monitorDetID[
"values"] = convertToJsonValue<int32_t>(detectorID);
204 appendToChildren(monitor, monitorDetID);
208 const std::string &writerModule) {
209 addStream(monitor,
"events", topic, source, writerModule);
213 const std::string &writerModule) {
214 addStream(monitor,
"waveforms", topic, source, writerModule);
217Json::Value &
addNXChopper(Json::Value &instrument,
const std::string &name) {
218 return addNX(instrument, name,
"NXdisk_chopper");
222 auto chopperFullName = createEmptyDataset(
"name",
"string");
223 chopperFullName[
"values"] = chopperName;
224 appendToChildren(chopper, chopperFullName);
228 auto chopperRadius = createEmptyDataset(
"radius",
"double");
229 chopperRadius[
"values"] =
radius;
230 chopperRadius[
"attributes"][0] = createAttribute(
"units",
"mm");
231 appendToChildren(chopper, chopperRadius);
235 addDataset<double>(chopper,
"slit_edges", {2}, edges,
"units",
"mm");
239 auto chopperSlitHeight = createEmptyDataset(
"slit_height",
"double");
240 chopperSlitHeight[
"values"] = slitHeight;
241 chopperSlitHeight[
"attributes"][0] = createAttribute(
"units",
"mm");
242 appendToChildren(chopper, chopperSlitHeight);
246 auto chopperSlits = createEmptyDataset(
"slits",
"int32");
247 chopperSlits[
"values"] = convertToJsonValue<int32_t>(
value);
248 appendToChildren(chopper, chopperSlits);
252 const std::string &writerModule) {
253 addStream(chopper,
"top_dead_center", topic, source, writerModule);
256Json::Value &
addNXDetector(Json::Value &instrument,
const std::string &name) {
257 return addNX(instrument, name,
"NXdetector");
261 auto transDep = createEmptyDataset(
"depends_on",
"string");
262 transDep[
"values"] = dependencyPath;
263 appendToChildren(nxDetector, transDep);
267 return addNX(nxDetector, name,
"NXtransformations");
271 const std::vector<double> &values,
const std::vector<double> &vec) {
272 addTransformationChild(nxTransformation,
"beam_direction_offset",
"translation",
273 "/entry/instrument/detector_1/transformations/orientation",
"m", arrayShape, values, vec);
277 const std::vector<double> &values,
const std::vector<double> &vec) {
278 addTransformationChild(nxTransformation,
"location",
"translation",
279 "/entry/instrument/detector_1/transformations/beam_direction_offset",
"m", arrayShape, values,
284 const std::vector<double> &values,
const std::vector<double> &vec) {
285 addTransformationChild(nxTransformation,
"orientation",
"translation",
".",
"degrees", arrayShape, values, vec);
289 const std::vector<int32_t> &values) {
290 addDataset<int32_t>(nxDetector,
"detector_number", arrayShape, values);
294 const std::vector<double> &values) {
295 addDataset<double>(nxDetector,
"x_pixel_offset", arrayShape, values,
"units",
"m");
299 const std::vector<double> &values) {
300 addDataset<double>(nxDetector,
"y_pixel_offset", arrayShape, values,
"units",
"m");
304 const std::vector<double> &values) {
305 addDataset<double>(nxDetector,
"z_pixel_offset", arrayShape, values,
"units",
"m");
308Json::Value &
addOffShape(Json::Value &nxDetector,
const std::string &name) {
309 return addNX(nxDetector, name,
"NXoff_geometry");
312void addOffShapeFaces(Json::Value &shape,
const std::vector<int> &arrayShape,
const std::vector<int> &indices) {
313 addDataset<int>(shape,
"faces", arrayShape, indices,
"",
"");
316void addOffShapeVertices(Json::Value &shape,
const std::vector<int> &arrayShape,
const std::vector<double> &vertices) {
317 addDataset<double>(shape,
"vertices", arrayShape, vertices,
"units",
"m");
321 const std::vector<int> &windingOrder) {
322 addDataset<int>(shape,
"winding_order", arrayShape, windingOrder,
"",
"");
326 return addNX(nxDetector, name,
"NXcylindrical_geometry");
330 const std::vector<int> &indices) {
331 addDataset<int>(shape,
"cylinders", arrayShape, indices);
335 const std::vector<double> &vertices) {
336 addDataset<double>(shape,
"vertices", arrayShape, vertices,
"units",
"m");
507 "/entry/instrument/detector_1/transformations/location");
530 "/entry/instrument/detector_1/transformations/location");
555 "/entry/instrument/detector_1/transformations/location");
582 "/entry/instrument/detector_1/transformations/location");
610 "/entry/instrument/detector_1/transformations/location");
638 "/entry/instrument/detector_1/transformations/location");
672 "/entry/instrument/detector_1/transformations/location");
699 "/entry/instrument/detector_1/transformations/location");
736 "/entry/instrument/detector_1/transformations/location");
775 "/entry/instrument/detector_1/transformations/location");
818 "/entry/instrument/detector_1/transformations/location");
847 "/entry/instrument/detector_1/transformations/location");
double value
The value of the point.
std::map< DeltaEMode::Type, std::string > index
H5::DataType getType()
Convert a primitive type to the appropriate H5::DataType.
void addYPixelOffset(Json::Value &nxDetector, const std::vector< int > &arrayShape, const std::vector< double > &values)
void addNXMonitorName(Json::Value &monitor, const std::string &name)
void addNXInstrumentName(Json::Value &instrument, const std::string &name)
Json::Value & addNXSource(Json::Value &instrument, const std::string &name)
void addNXTransformationDependency(Json::Value &nxDetector, const std::string &dependencyPath)
void addNXMonitorWaveformStreamInfo(Json::Value &monitor, const std::string &topic, const std::string &source, const std::string &writerModule)
Json::Value & addNXTransformation(Json::Value &nxDetector, const std::string &name)
void addOffShapeWindingOrder(Json::Value &shape, const std::vector< int > &arrayShape={4}, const std::vector< int > &windingOrder={0, 1, 2, 3})
void addOffShapeFaces(Json::Value &shape, const std::vector< int > &arrayShape={1}, const std::vector< int > &faces={0})
void addNXChopperSlitEdges(Json::Value &chopper, const std::vector< double > &edges={0.0, 23.0})
void addNXTransformationBeamDirectionOffset(Json::Value &nxTransformation, const std::vector< int > &arrayShape={1}, const std::vector< double > &values={0.049}, const std::vector< double > &vec={0, 0, -1})
Json::Value & addNXDetector(Json::Value &instrument, const std::string &name)
Json::Value & addOffShape(Json::Value &nxDetector, const std::string &name="pixel_shape")
Json::Value & addCylindricalShape(Json::Value &nxDetector, const std::string &name="pixel_shape")
void addOffShapeVertices(Json::Value &shape, const std::vector< int > &arrayShape={4, 3}, const std::vector< double > &vertices={-0.001, -0.001, 0, 0.001, -0.001, 0, 0.001, 0.001, 0, -0.001, 0.001, 0})
const std::string convertToString(const Json::Value &value)
void addNXMonitorEventStreamInfo(Json::Value &monitor, const std::string &topic, const std::string &source, const std::string &writerModule)
void addXPixelOffset(Json::Value &nxDetector, const std::vector< int > &arrayShape, const std::vector< double > &values)
Json::Value & addNXInstrument(Json::Value &entry, const std::string &name)
void addNXChopperSlitHeight(Json::Value &chopper, const double slitHeight=150)
void addNXChopperSlits(Json::Value &chopper, const int32_t value)
Json::Value & addNXSample(Json::Value &entry, const std::string &name)
void addDetectorNumbers(Json::Value &nxDetector, const std::vector< int > &arrayShape, const std::vector< int32_t > &values)
void addCylindricalShapeCylinders(Json::Value &shape, const std::vector< int > &arrayShape={3}, const std::vector< int > &indices={0, 1, 2})
Json::Value & addNXMonitor(Json::Value &entry, const std::string &name)
void initialiseRoot(const Json::Value &root, const std::string &name)
void addNXMonitorDetectorID(Json::Value &monitor, const int32_t detectorID)
void addNXTransformationLocation(Json::Value &nxTransformation, const std::vector< int > &arrayShape={1}, const std::vector< double > &values={0.971}, const std::vector< double > &vec={1, 0, 0})
void addNXChopperName(Json::Value &chopper, const std::string &chopperName)
Json::Value & addNXChopper(Json::Value &instrument, const std::string &name)
void addZPixelOffset(Json::Value &nxDetector, const std::vector< int > &arrayShape, const std::vector< double > &values)
void addNXChopperRadius(Json::Value &chopper, const double radius=350)
void addCylindricalShapeVertices(Json::Value &shape, const std::vector< int > &arrayShape={3, 3}, const std::vector< double > &vertices={-0.001, 0, 0, 0.001, 0.00405, 0, 0.001, 0, 0})
Json::Value & addNXEntry(Json::Value &root, const std::string &name)
void addNXChopperTopDeadCenter(Json::Value &chopper, const std::string &topic, const std::string &source, const std::string &writerModule)
void addNXTransformationOrientation(Json::Value &nxTransformation, const std::vector< int > &arrayShape={1}, const std::vector< double > &values={90}, const std::vector< double > &vec={0, 1, 0})
std::string getJSONGeometryMissingChopperInformation()
std::string getJSONGeometryNoDetectorIDs()
std::string getFullJSONInstrumentSimpleCylindrical()
std::string getJSONGeometryEmptyCylindricalGeometry()
std::string getJSONGeometryMissingBeamDirectionOffset()
std::string getJSONGeometryNoYPixelOffset()
std::string getJSONGeometryInvalidOffGeometry()
std::string getJSONGeometryNoPixelShape()
std::string getFullJSONInstrumentSimpleWithChopper()
std::string getJSONGeometryNoInstrument()
std::string getFullJSONInstrumentSimpleOFF()
std::string getJSONGeometryMissingOrientation()
std::string getFullJSONInstrumentSimpleWithMonitor()
std::string getJSONGeometryNoXPixelOffset()
std::string getJSONGeometryMissingTransformations()
std::string getJSONGeometryNoDetectors()
std::string getJSONGeometryEmptyOffGeometry()
std::string getJSONGeometryMissingMonitorInformation()
std::string getJSONGeometryInvalidCylindricalGeometry()
std::string getFullJSONInstrumentSimpleWithMonitorNoShape()
std::string getJSONGeometryNoSample()
std::string getFullJSONInstrumentSimpleWithSource()
std::string getFullJSONInstrumentSimpleWithZPixelOffset()