1 ///////////////////////////////////////
\r
5 #include "TofDialog.h"
\r
6 #include "resource.h"
\r
15 using namespace std;
\r
17 // Definitions for the CTofDialog class
\r
18 CTofDialog::CTofDialog(UINT nResID, CWnd* pParent)
\r
19 : CDialog(nResID, pParent)
\r
21 m_hInstRichEdit = ::LoadLibrary(_T("RICHED32.DLL"));
\r
22 if (!m_hInstRichEdit)
\r
23 ::MessageBox(NULL, _T("CTofDialog::CRichView Failed to load RICHED32.DLL"), _T(""), MB_ICONWARNING);
\r
26 CTofDialog::~CTofDialog()
\r
28 ::FreeLibrary(m_hInstRichEdit);
\r
31 INT_PTR CTofDialog::DialogProc(UINT uMsg, WPARAM wParam, LPARAM lParam)
\r
35 //Additional messages to be handled go here
\r
38 // Pass unhandled messages on to parent DialogProc
\r
39 return DialogProcDefault(uMsg, wParam, lParam);
\r
42 BOOL CTofDialog::OnCommand(WPARAM wParam, LPARAM lParam)
\r
44 UNREFERENCED_PARAMETER(lParam);
\r
46 switch (LOWORD(wParam))
\r
54 case IDC_BTN_OUTPUT:
\r
64 } //switch (LOWORD(wParam))
\r
69 BOOL CTofDialog::OnInitDialog()
\r
72 SetIconLarge(IDW_MAIN);
\r
73 SetIconSmall(IDW_MAIN);
\r
75 // Put some text in the edit boxes
\r
76 //SetDlgItemText(IDC_EDIT1, _T("Edit Control"));
\r
77 //SetDlgItemText(IDC_RICHEDIT1, _T("Rich Edit Window"));
\r
79 // Put some text in the list box
\r
80 //for (int i = 0 ; i < 8 ; i++)
\r
81 // SendDlgItemMessage(IDC_LIST1, LB_ADDSTRING, 0, (LPARAM) _T("List Box"));
\r
86 void CTofDialog::OnOK()
\r
88 int maxBufferSize = 1024;
\r
89 char inputFilename[maxBufferSize];
\r
90 char outputFilename[maxBufferSize];
\r
91 strcpy(inputFilename, LPCTSTR(GetDlgItemText(IDC_EDT_INPUT)));
\r
92 strcpy(outputFilename, LPCTSTR(GetDlgItemText(IDC_EDT_OUTPUT)));
\r
95 fstream input; fstream output;
\r
98 float peakValue = atof(GetDlgItemText(IDC_EDT_PEAK).GetBuffer(maxBufferSize));
\r
99 float binWidth = atof(GetDlgItemText(IDC_EDT_RES).GetBuffer(maxBufferSize));
\r
100 int numberSpectra = atoi(GetDlgItemText(IDC_EDT_SPECTRA).GetBuffer(maxBufferSize));
\r
101 float mass = atof(GetDlgItemText(IDC_EDT_MASS).GetBuffer(maxBufferSize));
\r
102 float length = atof(GetDlgItemText(IDC_EDT_LENGTH).GetBuffer(maxBufferSize));
\r
103 float peakCutOff = atof(GetDlgItemText(IDC_EDT_PEAK_CUTOFF).GetBuffer(maxBufferSize));
\r
106 if (peakValue <= 0.0f)
\r
108 MessageBox("Elastic peak value must be larger than zero", _T("Error"), MB_OK);
\r
112 if (binWidth <= 0.0f)
\r
114 MessageBox("Bin width must be larger than zero", _T("Error"), MB_OK);
\r
119 MessageBox("Mass must be larger than zero", _T("Error"), MB_OK);
\r
122 if (length <= 0.0f)
\r
124 MessageBox("Path length must be larger than zero", _T("Error"), MB_OK);
\r
128 if (numberSpectra <= 0)
\r
130 MessageBox("There should be at least one spectra in the data!", "Error", MB_OK);
\r
134 input.open(inputFilename);
\r
136 if (!input.is_open())
\r
138 MessageBox("Input file \"" + CString(inputFilename) + "\" Not Found", _T("Error"), MB_OK);
\r
142 output.open(outputFilename);
\r
143 if (output.is_open())
\r
146 if (MessageBox(_T("Output file already exists! Overwrite?"), _T("Warning"),MB_YESNO) == IDNO)
\r
148 MessageBox(_T("No action taken."), _T("Message"), MB_OK);
\r
153 output.open(outputFilename, ios::out);
\r
154 if (!output.is_open())
\r
156 MessageBox("Output file \"" + CString(outputFilename) + "\" Couldn't be opened", _T("Error"), MB_OK);
\r
165 while (line != "]" && input.good())
\r
172 //We are now at the data
\r
173 while (input.good())
\r
177 //MessageBox("Line reads " + CString(line.c_str()), _T("DEBUG"), MB_OK);
\r
178 int dataPoint = atoi(line.c_str());
\r
179 if (dataPoint == 0 && line != "0" && line != "")
\r
181 //MessageBox("Suspected corrupt data \"" + CString(line.c_str()) + "\"", _T("Error"), MB_OK);
\r
185 data.push_back(dataPoint);
\r
189 //Have aquired all data; perform operations
\r
190 map<float, vector<int> > outputData; //Data as energy,counts columns
\r
194 Analysis::AnalyseData(data, outputData, peakValue, binWidth, mass, length, peakCutOff, numberSpectra);
\r
196 output.precision(32);
\r
198 for (map<float, vector<int> >::iterator i = outputData.begin(); i != outputData.end(); ++i)
\r
200 if ((*i).second.size() == numberSpectra)
\r
202 output << (*i).first << " ";
\r
203 for (int ii=0; ii < (*i).second.size(); ++ii)
\r
205 output << (*i).second[ii];
\r
206 if (ii < (*i).second.size() - 1)
\r
213 MessageBox("Conversion complete!", _T("Message"), MB_OK);
\r
220 void CTofDialog::OnBrowseInput()
\r
222 string s = openfilename();
\r
223 SetDlgItemText(IDC_EDT_INPUT, _T(s.c_str()));
\r
226 void CTofDialog::OnBrowseOutput()
\r
228 string s = openfilename();
\r
229 SetDlgItemText(IDC_EDT_OUTPUT, _T(s.c_str()));
\r
232 void CTofDialog::OnExit()
\r
237 void CTofDialog::Unimplemented()
\r
239 MessageBox(_T("This feature doesn't work yet. Sorry."), _T("Button"), MB_OK);
\r
242 void CTofDialog::OnHelp()
\r
244 CString helpString = "";
\r
245 helpString += "This program converts electron Time of Flight (ToF) data to an energy spectrum.\n\n";
\r
247 helpString += "The formula is:\n E = m/2 (L/(L*sqrt(m/2Ep) + dt))^2\n";
\r
248 helpString += " where Ep is the peak energy, m is mass, L is path length, dt is time difference relative to peak, E is energy\n\n";
\r
250 helpString += "Input File:\n";
\r
251 helpString += "The expected input file should contain a single column of counts detected in successive channels.\n\n";
\r
253 helpString += "Output File:\n";
\r
254 helpString += "The output file will contain multiple columns, corresponding to computed energy values (eV) and the count values for each spectrum.\n\n";
\r
256 helpString += "Elastic Peak:\n";
\r
257 helpString += "The energy associated with each time channel is calculated after assuming the energy value corresponding to the time with the peak number of counts.";
\r
258 helpString += "This peak normally corresponds to elastic scattering processes.\n\n";
\r
260 helpString += "Bin Width:\n";
\r
261 helpString += "Each line in the input file corresponds to a time channel. The seperation in time between adjacent channels is used to calculate the absolute time difference between channels, dt.\n\n";
\r
263 helpString += "#no of spectra:\n";
\r
264 helpString += "One input file might contain multiple seperate TOF spectra, spread accross seperate channels. The program detects where one spectrum ends and the next begins after a series of zero counts in the channels.\n\n";
\r
266 helpString += "Path Length:\n";
\r
267 helpString += "Assuming the acceleration distance is negligable, the path length is how far the particles travel under constant velocity.\n\n";
\r
269 helpString += "Mass:\n";
\r
270 helpString += "The mass of the particles determines their energy.\n\n";
\r
272 helpString += "Peak Cut Off (%):\n";
\r
273 helpString += "The program locates the elastic peak by first determining the maximum count value, and then finding the median point of all points in the TOF spectrum with count levels above this percentage of the maximum value.";
\r
274 helpString += "This is useful for reducing the effects of noise on the elastic peak value.";
\r
275 helpString += "To just use the time bin with maximum counts for the elastic peak, set this to 100.\n\n";
\r
277 helpString += "About:\n";
\r
278 helpString += "Written by Sam Moore, 02/2012";
\r
280 MessageBox(helpString, "Help", MB_OK);
\r
284 void CTofDialog::OnButton()
\r
286 SetDlgItemText(IDC_STATIC3, _T("Button Pressed"));
\r
287 TRACE(_T("Button Pressed\n"));
\r