Commit before breaking everything
[matches/honours.git] / research / transmission_spectroscopy / TOF / TofDialog.cpp
diff --git a/research/transmission_spectroscopy/TOF/TofDialog.cpp b/research/transmission_spectroscopy/TOF/TofDialog.cpp
new file mode 100644 (file)
index 0000000..8a4b617
--- /dev/null
@@ -0,0 +1,290 @@
+///////////////////////////////////////\r
+// TofDialog.cpp\r
+\r
+#include "stdafx.h"\r
+#include "TofDialog.h"\r
+#include "resource.h"\r
+\r
+#include "utils.h"\r
+#include "analyse.h"\r
+#include <sstream>\r
+#include <iostream>\r
+#include <fstream>\r
+#include <vector>\r
+\r
+using namespace std;\r
+\r
+// Definitions for the CTofDialog class\r
+CTofDialog::CTofDialog(UINT nResID, CWnd* pParent)\r
+       : CDialog(nResID, pParent)\r
+{\r
+       m_hInstRichEdit = ::LoadLibrary(_T("RICHED32.DLL"));\r
+    if (!m_hInstRichEdit)\r
+               ::MessageBox(NULL, _T("CTofDialog::CRichView  Failed to load RICHED32.DLL"), _T(""), MB_ICONWARNING);\r
+}\r
+\r
+CTofDialog::~CTofDialog()\r
+{\r
+       ::FreeLibrary(m_hInstRichEdit);\r
+}\r
+\r
+INT_PTR CTofDialog::DialogProc(UINT uMsg, WPARAM wParam, LPARAM lParam)\r
+{\r
+//     switch (uMsg)\r
+//     {\r
+               //Additional messages to be handled go here\r
+//     }\r
+\r
+       // Pass unhandled messages on to parent DialogProc\r
+       return DialogProcDefault(uMsg, wParam, lParam);\r
+}\r
+\r
+BOOL CTofDialog::OnCommand(WPARAM wParam, LPARAM lParam)\r
+{\r
+       UNREFERENCED_PARAMETER(lParam);\r
+\r
+       switch (LOWORD(wParam))\r
+       {\r
+               case IDC_BTN_EXIT:\r
+                       OnExit();\r
+                       break;\r
+               case IDC_BTN_OK:\r
+                       OnOK();\r
+                       break;\r
+               case IDC_BTN_OUTPUT:\r
+                       OnBrowseOutput();\r
+                       break;\r
+               case IDC_BTN_INPUT:\r
+                       OnBrowseInput();\r
+                       break;\r
+               case IDC_BTN_HELP:\r
+                       OnHelp();\r
+                       break;\r
+\r
+    } //switch (LOWORD(wParam))\r
+\r
+       return FALSE;\r
+}\r
+\r
+BOOL CTofDialog::OnInitDialog()\r
+{\r
+       // Set the Icon\r
+       SetIconLarge(IDW_MAIN);\r
+       SetIconSmall(IDW_MAIN);\r
+\r
+       // Put some text in the edit boxes\r
+       //SetDlgItemText(IDC_EDIT1, _T("Edit Control"));\r
+       //SetDlgItemText(IDC_RICHEDIT1, _T("Rich Edit Window"));\r
+\r
+       // Put some text in the list box\r
+       //for (int i = 0 ; i < 8 ; i++)\r
+       //      SendDlgItemMessage(IDC_LIST1, LB_ADDSTRING, 0, (LPARAM) _T("List Box"));\r
+\r
+       return true;\r
+}\r
+\r
+void CTofDialog::OnOK()\r
+{\r
+       int maxBufferSize = 1024;\r
+       char inputFilename[maxBufferSize]; \r
+       char outputFilename[maxBufferSize];\r
+       strcpy(inputFilename, LPCTSTR(GetDlgItemText(IDC_EDT_INPUT)));\r
+       strcpy(outputFilename, LPCTSTR(GetDlgItemText(IDC_EDT_OUTPUT)));\r
+\r
+\r
+       fstream input; fstream output;\r
+\r
+\r
+       float peakValue = atof(GetDlgItemText(IDC_EDT_PEAK).GetBuffer(maxBufferSize));\r
+       float binWidth = atof(GetDlgItemText(IDC_EDT_RES).GetBuffer(maxBufferSize));\r
+       int numberSpectra = atoi(GetDlgItemText(IDC_EDT_SPECTRA).GetBuffer(maxBufferSize));\r
+       float mass = atof(GetDlgItemText(IDC_EDT_MASS).GetBuffer(maxBufferSize));\r
+       float length = atof(GetDlgItemText(IDC_EDT_LENGTH).GetBuffer(maxBufferSize));\r
+       float peakCutOff = atof(GetDlgItemText(IDC_EDT_PEAK_CUTOFF).GetBuffer(maxBufferSize));\r
+       \r
+\r
+       if (peakValue <= 0.0f)\r
+       {\r
+               MessageBox("Elastic peak value must be larger than zero", _T("Error"), MB_OK);\r
+               return;\r
+       }\r
+\r
+       if (binWidth <= 0.0f)\r
+       {\r
+               MessageBox("Bin width must be larger than zero", _T("Error"), MB_OK);\r
+               return;\r
+       }\r
+       if (mass <= 0.0f)\r
+       {\r
+               MessageBox("Mass must be larger than zero", _T("Error"), MB_OK);\r
+               return;\r
+       }\r
+       if (length <= 0.0f)\r
+       {\r
+               MessageBox("Path length must be larger than zero", _T("Error"), MB_OK);\r
+               return;\r
+       }\r
+\r
+       if (numberSpectra <= 0)\r
+       {\r
+               MessageBox("There should be at least one spectra in the data!", "Error", MB_OK);\r
+               return;\r
+       }\r
+\r
+       input.open(inputFilename);\r
+\r
+       if (!input.is_open())\r
+       {\r
+               MessageBox("Input file \"" + CString(inputFilename) + "\" Not Found", _T("Error"), MB_OK);\r
+               return;\r
+       }\r
+\r
+       output.open(outputFilename);\r
+       if (output.is_open())\r
+       {\r
+               output.close();\r
+               if (MessageBox(_T("Output file already exists! Overwrite?"), _T("Warning"),MB_YESNO) == IDNO)\r
+               {\r
+                       MessageBox(_T("No action taken."), _T("Message"), MB_OK);\r
+                       input.close();\r
+                       return; \r
+               }\r
+       }\r
+       output.open(outputFilename, ios::out);\r
+       if (!output.is_open())\r
+       {\r
+               MessageBox("Output file \"" + CString(outputFilename) + "\" Couldn't be opened", _T("Error"), MB_OK);\r
+               input.close();\r
+               return;\r
+       }\r
+\r
+       string line = "";\r
+       \r
+       /*\r
+       //Ignore headers\r
+       while (line != "]" && input.good())\r
+       {\r
+               line = "";\r
+               input >> line;\r
+       }\r
+       */\r
+       vector<int> data;\r
+       //We are now at the data\r
+       while (input.good())\r
+       {\r
+               line = "";\r
+               input >> line;\r
+               //MessageBox("Line reads " + CString(line.c_str()), _T("DEBUG"), MB_OK);\r
+               int dataPoint = atoi(line.c_str());\r
+               if (dataPoint == 0 && line != "0" && line != "")\r
+               {\r
+                       //MessageBox("Suspected corrupt data \"" + CString(line.c_str()) + "\"", _T("Error"), MB_OK);\r
+               }\r
+               else\r
+               {\r
+                       data.push_back(dataPoint);\r
+               }\r
+       }\r
+       \r
+       //Have aquired all data; perform operations\r
+       map<float, vector<int> > outputData; //Data as energy,counts columns\r
+\r
+\r
+\r
+       Analysis::AnalyseData(data, outputData, peakValue, binWidth, mass, length, peakCutOff, numberSpectra);\r
+       \r
+       output.precision(32);\r
+\r
+       for (map<float, vector<int> >::iterator i = outputData.begin(); i != outputData.end(); ++i)\r
+       {\r
+               if ((*i).second.size() == numberSpectra)\r
+               {\r
+                       output << (*i).first << " ";\r
+                       for (int ii=0; ii < (*i).second.size(); ++ii)\r
+                       {\r
+                               output << (*i).second[ii];\r
+                               if (ii < (*i).second.size() - 1)\r
+                                       output << " ";\r
+                       }\r
+                       output << "\n";\r
+               }\r
+       }       \r
+\r
+       MessageBox("Conversion complete!", _T("Message"), MB_OK);\r
+       input.close();\r
+       output.close();\r
+\r
+\r
+}\r
+\r
+void CTofDialog::OnBrowseInput()\r
+{\r
+       string s = openfilename();\r
+       SetDlgItemText(IDC_EDT_INPUT, _T(s.c_str()));\r
+}\r
+\r
+void CTofDialog::OnBrowseOutput()\r
+{\r
+       string s = openfilename();\r
+       SetDlgItemText(IDC_EDT_OUTPUT, _T(s.c_str()));\r
+}\r
+\r
+void CTofDialog::OnExit()\r
+{\r
+     CDialog::OnOK();\r
+}\r
+\r
+void CTofDialog::Unimplemented()\r
+{\r
+     MessageBox(_T("This feature doesn't work yet. Sorry."), _T("Button"), MB_OK); \r
+}\r
+\r
+void CTofDialog::OnHelp()\r
+{\r
+       CString helpString = "";\r
+       helpString += "This program converts electron Time of Flight (ToF) data to an energy spectrum.\n\n";\r
+\r
+       helpString += "The formula is:\n E = m/2 (L/(L*sqrt(m/2Ep) + dt))^2\n";\r
+       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
+\r
+       helpString += "Input File:\n";\r
+       helpString += "The expected input file should contain a single column of counts detected in successive channels.\n\n";\r
+\r
+       helpString += "Output File:\n";\r
+       helpString += "The output file will contain multiple columns, corresponding to computed energy values (eV) and the count values for each spectrum.\n\n";\r
+\r
+       helpString += "Elastic Peak:\n";\r
+       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
+       helpString += "This peak normally corresponds to elastic scattering processes.\n\n";\r
+       \r
+       helpString += "Bin Width:\n";\r
+       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
+\r
+       helpString += "#no of spectra:\n";\r
+       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
+\r
+       helpString += "Path Length:\n"; \r
+       helpString += "Assuming the acceleration distance is negligable, the path length is how far the particles travel under constant velocity.\n\n";\r
+\r
+       helpString += "Mass:\n";\r
+       helpString += "The mass of the particles determines their energy.\n\n";\r
+\r
+       helpString += "Peak Cut Off (%):\n";\r
+       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
+       helpString += "This is useful for reducing the effects of noise on the elastic peak value.";\r
+       helpString += "To just use the time bin with maximum counts for the elastic peak, set this to 100.\n\n";\r
+\r
+       helpString += "About:\n";\r
+       helpString += "Written by Sam Moore, 02/2012";\r
+               \r
+       MessageBox(helpString, "Help", MB_OK);\r
+}\r
+\r
+/*\r
+void CTofDialog::OnButton()\r
+{\r
+       SetDlgItemText(IDC_STATIC3, _T("Button Pressed"));\r
+       TRACE(_T("Button Pressed\n"));\r
+}\r
+*/\r
+\r

UCC git Repository :: git.ucc.asn.au