-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCFitButton.cpp
277 lines (224 loc) · 7.42 KB
/
CFitButton.cpp
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
40
41
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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
/*
This software is Copyright by the Board of Trustees of Michigan
State University (c) Copyright 2005.
You may use this software under the terms of the GNU public license
(GPL). The terms of this license are described at:
http://www.gnu.org/licenses/gpl.txt
Author:
Ron Fox
NSCL
Michigan State University
East Lansing, MI 48824-1321
*/
#include <config.h>
#include "CFitButton.h"
#include "ButtonEvent.h"
#include <Xamine.h>
#include <CGaussianFit.h>
#include <CSpectrumFit.h>
#include <CFitDictionary.h>
#include <Histogrammer.h>
#include <SpecTcl.h>
#include <Spectrum.h>
#include <CFitCommand.h>
#include <TCLInterpreter.h>
#include <clientops.h>
#include <string>
#ifdef HAVE_STD_NAMESPACE
using namespace std;
#endif
#include <iostream> // For debugging.
/*!
For now we assume we have the button box all to ourselves.
\param pEventHandler : CXamineEventHandler*
Pointer to the xamine event handler (we register ourselvse on this).
*/
CFitButton::CFitButton(CXamineEventHandler* pEventHandler)
{
// Generic Gauss fit button.
m_FitButtonId = 1;
ButtonDescription myButton;
myButton.button_code = m_FitButtonId;
strcpy(myButton.label, "Gauss Fit");
myButton.type = Push;
myButton.sensitive = T;
myButton.prompter = Points;
myButton.whenavailable = In1dSpectrum;
strcpy(myButton.promptstr, "Fit limits"); // Ingored?
myButton.spectype = Oned; // I think this is ignored for this prompter
myButton.minpts = 2;
myButton.maxpts = 2; // need exactly 2 pts.
// Xamine_DefineButton(0,0, &myButton); // Old button in case Tim needs to see.
// The Fit Gammas button is about the same as the GaussFit button.. just
// different Id and label:
m_GammaFitButtonId = 2;
myButton.button_code = m_GammaFitButtonId;
strcpy(myButton.label, "Fit Gammas");
Xamine_DefineButton(0,0, &myButton);
// The fit Neutrons button is also about the same as the guass fit button:
m_NeutronFitButtonId = 3;
myButton.button_code = m_NeutronFitButtonId;
strcpy(myButton.label, "Fit Neutrons");
Xamine_DefineButton(0,2, &myButton);
// The SHOW FOM button has no prompter, and requires
// we be in a 1-d spectrum...
m_FOMButtonId = 4;
myButton.button_code = m_FOMButtonId;
strcpy(myButton.label, "FOM");
myButton.prompter = NoPrompt;
Xamine_DefineButton(2,0, &myButton);
// Show all FOM button has no prompter, and no requirements
// about the current spectrum.
m_FOMAllButtonId = 5;
myButton.button_code = m_FOMAllButtonId;
strcpy(myButton.label, "FOM All");
myButton.whenavailable = Anytime;
Xamine_DefineButton(2,1, &myButton);
// Project button:
m_projectButtonId = 6;
myButton.button_code = m_projectButtonId;
strcpy(myButton.label, "Project");
myButton.whenavailable = In2dSpectrum;
Xamine_DefineButton(2,2, &myButton);
pEventHandler->addButtonHandler(*this);
}
/*!
The destructor does nothing for now, however note that a call to the
destructor is really really bad since at present, there's no way to
unregister us as a button event handler!!
*/
CFitButton::~CFitButton() {}
/*
The button handler is called when \em any button in the button box
sends its message back to SpecTcl. We need to determine if we can
successfully process the message. If so return true if not,
false so that other handlers can try.
\param event : CButtonEvent&
Reference to an object that describes the button event.
\return Bool_t
\retval kfTRUE - we processed the event, no need for other handlers to fire.
\retval kfFALSE - we did not process the event, continue checking handlers.
*/
Bool_t
CFitButton::operator()(CButtonEvent& event)
{
// If this is not our button, return false right away:
int buttonId = event.getId();
// What we do depends a bit on the button that has been clicked:
if (buttonId == m_FitButtonId |
buttonId == m_GammaFitButtonId |
buttonId == m_NeutronFitButtonId) {
// Fits are all the same except for where the fit name comes from.
// Before we can fit, we need to get:
// - The name of the fit.
// - The name of the spectrum.
// - The fit points.
PointArray pts = event.getPoints();
string spectrum = spectrumName(event);
// Figure out the fit source depending on the actual button
// id:
string fitName;
if (buttonId == m_FitButtonId) {
fitName = event.getm_sPromptedString();
}
if (buttonId == m_GammaFitButtonId) {
fitName = spectrum;
fitName += "-gammas";
}
if (buttonId == m_NeutronFitButtonId) {
fitName = spectrum;
fitName += "-neutrons";
}
// Now we can create the fit:
CGaussianFit* pFit = new CGaussianFit(fitName, CFitCommand::id());
int low = pts[0].X();
int high = pts[1].X();
// Danger Will Robinson.. if low==high, gsl will exit our
// program.. not likely but need to catch it because the
// game theoretcial payoff is soo sooo bad.
//
if (low == high) {
invokeScript(string("tk_messageBox -icon error -message {Both fit points have the same X positions}"));
return kfTRUE;
}
if (low > high) {
int temp = low;
low = high;
high = temp;
}
// This is in a try/catch block in case the user did the truly
// pathalogical thing of deleting the spectrum just as they
// accepted the fit ... kids these days.
//
CSpectrumFit* pSpectrumFit;
try {
pSpectrumFit = new CSpectrumFit(spectrum,
pFit->getNumber(),
low, high, *pFit);
}
catch (...) {
delete pFit; // Just abort the operation silently.
return kfTRUE;
}
// we use the addOrReplace function of the fit dictionary:
CFitDictionary& dict(CFitDictionary::getInstance());
dict.addOrReplace(*pSpectrumFit);
}
else if (buttonId == m_FOMButtonId) {
//
// We're going to invoke ShowFOM spectrum name:
// construc the script:
string script = "ShowFOM ";
script += spectrumName(event);
invokeScript(script);
}
else if (buttonId == m_FOMAllButtonId) {
// Report all the FOM's.
//
invokeScript(string("ShowFOMAll"));
}
else if (buttonId == m_projectButtonId) {
//
// Invoke the projection GUI:
string script = "Project2D ";
script += spectrumName(event);
invokeScript(script);
}
else {
return kfFALSE; // Not one of our buttons.
}
return kfTRUE;
}
// Return the spectrum name associated with a button click.
// an empty string if spectrum is not defined.
//
string
CFitButton::spectrumName(CButtonEvent& event)
{
int bindId = event.getPromptedSpectrum();
SpecTcl* pApi = SpecTcl::getInstance();
CHistogrammer* pHistogrammer = pApi->GetHistogrammer();
CSpectrum* pSpectrum = pHistogrammer->DisplayBinding(bindId-1);
string spectrumName;
if (pSpectrum) {
spectrumName = pSpectrum->getName();
}
else {
spectrumName = "";
}
return spectrumName;
}
//
// Invoke a Tcl script ignoring any errors that may occur in it:
//
void
CFitButton::invokeScript(string script)
{
SpecTcl* api = SpecTcl::getInstance();
CTCLInterpreter* pInterp = api->getInterpreter();
try {
pInterp->GlobalEval(script);
}
catch (...) {
}
}