VEXPDetectorSimulator.cpp 3.62 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
/*
 * Nomad Instrument Control Software
 *
 * Copyright 2011 Institut Laue-Langevin
 *
 * Licensed under the EUPL, Version 1.1 only (the "License");
 * You may not use this work except in compliance with the Licence.
 * You may obtain a copy of the Licence at:
 *
 * http://joinup.ec.europa.eu/software/page/eupl
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the Licence is distributed on an "AS IS" basis,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the Licence for the specific language governing permissions and
 * limitations under the Licence.
 */

#include "VEXPDetectorSimulator.h"
#include <fstream>
#include <boost/algorithm/string.hpp>

using namespace std;
using namespace boost;

namespace vexp {

const string DetectorSimulator::TYPE = "vexp_detector_simulator";

DetectorSimulator::DetectorSimulator(const string& name) :
	ExperimentController(name), m_dataHandle(0), m_coef(1.0) {

	setFamily(family::HIDDEN);

	m_detector.init(this, "detector");
	m_scattering.init(this, "scattering");

	simulationFile.init(this, SAVE, "simulation_file");
	intensity.init(this, NOSAVE, "intensity");
40
41
	detSum.init(this, NOSAVE, "detector_sum");
	idata.init(this, NOSAVE, "idata");
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
}

DetectorSimulator::DetectorSimulator(const DetectorSimulator& controller) :
	ExperimentController(controller), m_dataHandle(0), m_coef(1.0) {
}

DetectorSimulator::~DetectorSimulator() {
}

void DetectorSimulator::postConfiguration() {

	registerUpdater(m_detector->detSum, &DetectorSimulator::updateData, this);

	loadSimulationFile();
}

void DetectorSimulator::loadSimulationFile() {

	// Open the file.
	ifstream file(simulationFile().c_str());
	if (!file) {
		cerr << "Cannot open simulation file " << simulationFile() << endl;
		return;
	}

	// Create a tree.
	m_dataHandle = m_accessor.createSqwTree();

	// Calculate the max intensity.
	float64 maxIntensity = 0.0;
	float64 mh, mk, ml, me;

	typedef tokenizer<char_separator<char> > tokenizer;
	char_separator<char> sep(" ");

	// Read the file.
	while (!file.eof()) {

		string line;
		getline(file, line);

		if (line != "") {
			try {
				tokenizer tokens(line, sep);
				tokenizer::iterator iter = tokens.begin();

				float64 qh(stod(*iter));
				++iter;
				float64 qk(stod(*iter));
				++iter;
				float64 ql(stod(*iter));
				++iter;
				float64 qe(stod(*iter));
				++iter;
				float64 qs(stod(*iter));

				//cout << "found " << qh << " " << qk << " " << ql << " " << qe << " " << qs << endl;

				// Add the point.
				m_accessor.addSqwValue(m_dataHandle, qh, qk, ql, qe, qs);

				// Update the max intensity.
				//maxIntensity = max(qs, maxIntensity);
				if (qs > maxIntensity) {
					maxIntensity = qs;
					mh = qh;
					mk = qk;
					ml = ql;
					me = qe;
				}
			}
			catch (...) {
				// Line with bad characters are excluded.
			}
		}
	}

	m_coef = 1000.0 / maxIntensity;

	cout << "Created the simulation data, max intensity is " << maxIntensity << " at " << mh << ", " << mk << ", " << ml << ", " << me << endl;
}

void DetectorSimulator::updateData() {

	// Query the s value from the data.

	float64 h, k, l, e, s;
	h = m_scattering->qh();
	k = m_scattering->qk();
	l = m_scattering->ql();
	e = m_scattering->en();

	m_accessor.querySqwTree(m_dataHandle, h, k, l, e, s);

136
137
138
139
	// Update the detector sum applying the coef.
	float64 sum = s * m_coef;
	detSum = sum;
	m_detector->detSum = sum;
140

141
142
	// Increase idata to trigger VEXPController.
	idata = idata() + 1;
143
144
145

	cout << "updateData " << m_detector->detSum()
		<< ", scattering " << m_scattering->qh() << ", " << m_scattering->qk() << ", " << m_scattering->ql() << ", " << m_scattering->en() << endl;
146
147


148
149
150
}

}