Diana Software
QGCalibrationWindow.cc
Go to the documentation of this file.
1 #include "QGCalibrationWindow.hh"
2 #include <iostream>
3 #include <list>
4 #include <string>
5 #include <fstream>
7 #include "QGHistogram.hh"
8 #include "QGPlot.hh"
9 #include "QGSessionHandler.hh"
10 #include "TGButton.h"
11 #include "TGComboBox.h"
12 #include "TGFrame.h"
13 #include "TGLabel.h"
14 #include "TString.h"
15 
17 
18 using std::cout;
19 using std::endl;
20 using std::list;
21 using std::map;
22 using std::string;
23 using std::vector;
24 
25 QGCalibrationWindow::QGCalibrationWindow(const TGWindow *p, UInt_t w, UInt_t h) :
26 QGWindow(p, w, h), fCurrentHistogramName("")
27 {
28  SetWindowName("Calibration Window");
29 
30  vector<string> instructions_above_histograms;
31  instructions_above_histograms.push_back("Select a channel from the list to calibrate and click \"Draw\"");
32 
33  for ( vector<string>::const_iterator iter = instructions_above_histograms.begin();
34  iter != instructions_above_histograms.end();
35  ++iter ) {
36  TGLabel *instruction = new TGLabel(this, iter->c_str());
37  AddFrame(instruction, new TGLayoutHints(kLHintsCenterX, 5, 5, 1, 0));
38  }
39 
40  fHistogramFrame = new TGHorizontalFrame(this);
41 
42  fHistogramDropDownBox = new TGComboBox(fHistogramFrame);
43  fHistogramFrame->AddFrame(fHistogramDropDownBox, new TGLayoutHints(kLHintsCenterY, 0, 0, 0, 0));
45  fHistogramDropDownBox->Resize(200, 25);
46  fHistogramDropDownBox->Connect("Selected(const char*)", "QGCalibrationWindow", this, "HandleHistogramDropDownBox(const char*)");
47 
48  fDrawButton = new TGTextButton(fHistogramFrame, "Draw");
49  fDrawButton->Connect("Clicked()", "QGCalibrationWindow", this, "HandleDrawButton()");
50  fHistogramFrame->AddFrame(fDrawButton, new TGLayoutHints(kLHintsCenterY, 5, 0, 0, 0));
51 
52  AddFrame(fHistogramFrame, new TGLayoutHints(kLHintsCenterX, 0, 0, 5, 4));
53 
54  vector<string> instructions_above_peaks;
55  instructions_above_peaks.push_back("Zoom in to a window containing each peak and click\"Set\" to record the");
56  instructions_above_peaks.push_back("max and min of the window. When you are finished with");
57  instructions_above_peaks.push_back(" all the peaks and channels, click \"Save\" to produce the output file.");
58 
59  for ( vector<string>::const_iterator iter = instructions_above_peaks.begin();
60  iter != instructions_above_peaks.end();
61  ++iter ) {
62  TGLabel *instruction = new TGLabel(this, iter->c_str());
63  AddFrame(instruction, new TGLayoutHints(kLHintsCenterX, 5, 5, 1, 0));
64  }
65 
66  fSaveButton = new TGTextButton(this, "Save");
67  AddFrame(fSaveButton, new TGLayoutHints(kLHintsCenterX, 0, 0, 5, 5));
68  fSaveButton->Connect("Clicked()", "QGCalibrationWindow", this, "HandleSaveButton()");
69 
70  Resize(GetDefaultSize());
71  MapSubwindows();
72  Resize(w, h);
73  MapWindow();
74 }
75 
77 {
78 }
79 
80 void QGCalibrationWindow::AddPeak(Double_t energy)
81 {
82  RemoveFrame(fSaveButton);
83 
84  QGCalibrationPeakFrame *calibrationPeakFrame = new QGCalibrationPeakFrame(this);
85  fCalibrationPeakFrames.push_back(calibrationPeakFrame);
86  calibrationPeakFrame->SetEnergy(energy);
87  AddFrame(calibrationPeakFrame, new TGLayoutHints(kLHintsLeft, 0, 5, 5, 0));
88 
89  AddFrame(fSaveButton, new TGLayoutHints(kLHintsCenterX, 0, 0, 5, 5));
90 
91  MapSubwindows();
92  Resize();
93 }
94 
95 void QGCalibrationWindow::Clear(Option_t* opt)
96 {
97  vector<QGCalibrationPeakFrame*>::const_iterator peakIter;
98  for (peakIter = fCalibrationPeakFrames.begin(); peakIter != fCalibrationPeakFrames.end(); ++peakIter) {
99  (*peakIter)->Clear(opt);
100  }
101 }
102 
104 {
105  QGPlot *plot = 0;
106  if (fHistogramDropDownBox->GetSelectedEntry()) {
107  string name = fHistogramDropDownBox->GetSelectedEntry()->GetTitle();
108  plot = QGPlot::GetPlotByName(name);
109  }
110  return plot;
111 }
112 
114 {
115  QGPlot *plot = GetCurrentPlot();
116  if (plot) {
117  plot->Plot();
118  }
119 }
120 
122 {
123  SaveData();
124  Clear();
125  fCurrentHistogramName = text;
127 }
128 
130 {
131  // write data to file from maps
132  SaveData();
133  std::ofstream filestream;
134  filestream.open(fOutputFilename.c_str(), std::ofstream::out);
135 
136  if(!filestream.good())
137  {
138  cout << "Could not open file " << fOutputFilename << endl;
139  return;
140  }
141 
142  std::vector<int> channels;
143  std::vector<double> energies;
144  std::vector<double> mins;
145  std::vector<double> maxs;
146 
147  if(fMaxData.size() != fMinData.size())
148  {
149  cout << "Number of max values set must equal number of min values!" << endl;
150  return;
151  }
152 
153  // loop over channels
154  map<string, map<QGCalibrationPeakFrame*, Double_t> >::iterator iter;
155  for(iter=fMaxData.begin(); iter != fMaxData.end(); iter++)
156  {
157  string histname = iter->first;
158  int channel;
159  sscanf(histname.c_str(), "Channel_%d", &channel);
160 
161  if(fMaxData[histname].size() != fMinData[histname].size())
162  {
163  cout << "Number of max values set must equal number of min values!" << endl;
164  return;
165  }
166 
167  // loop over peaks
168  map<QGCalibrationPeakFrame*, Double_t>::iterator peakIter;
169  for(peakIter=fMaxData[histname].begin(); peakIter != fMaxData[histname].end(); peakIter++)
170  {
171  channels.push_back(channel);
172  energies.push_back((peakIter->first)->GetEnergy());
173  maxs.push_back(fMaxData[histname][peakIter->first]);
174  if(fMinData[histname].count(peakIter->first))
175  {
176  mins.push_back(fMinData[histname][peakIter->first]);
177  }
178  else
179  {
180  cout << "Min value missing for channel " << channel;
181  cout << ", energy " << energies.back() << endl;
182  return;
183  }
184  }
185  }
186 
187  int nCols = 4;
188  int nRows = channels.size();
189 
190  // MV FIXME: should use the QGlobalDataManager here...
191  filestream << "# begin GUI@peaks" << endl;
192  filestream << "# QMatrix" << endl;
193  filestream << "# " << nRows << ' ' << nCols << endl;
194 
195  for(int i=0; i<nRows; i++)
196  {
197  filestream << channels[i] << '\t';
198  filestream << energies[i] << '\t';
199  filestream << mins[i] << '\t';
200  filestream << maxs[i] << endl;
201  }
202  filestream << "# end peaks" << endl;
203  filestream.close();
204  return;
205 }
206 
207 void QGCalibrationWindow::LoadData(const std::string& histogramName)
208 {
209  if (fMinData.count(histogramName) != 0) {
210  map<QGCalibrationPeakFrame*, Double_t>::const_iterator peakIter;
211  for (peakIter = fMinData[histogramName].begin(); peakIter != fMinData[histogramName].end(); ++peakIter) {
212  (peakIter->first)->SetMinAmplitude(peakIter->second);
213  }
214  }
215  if (fMaxData.count(histogramName) != 0) {
216  map<QGCalibrationPeakFrame*, Double_t>::const_iterator peakIter;
217  for (peakIter = fMaxData[histogramName].begin(); peakIter != fMaxData[histogramName].end(); ++peakIter) {
218  (peakIter->first)->SetMaxAmplitude(peakIter->second);
219  }
220  }
221 }
222 
224 {
225  if (fCurrentHistogramName != "") {
226  vector<QGCalibrationPeakFrame*>::const_iterator peakIter;
227  for (peakIter = fCalibrationPeakFrames.begin(); peakIter != fCalibrationPeakFrames.end(); ++peakIter) {
228  if ((*peakIter)->IsMinAmplitudeSet()) {
229  fMinData[fCurrentHistogramName][*peakIter] = (*peakIter)->GetMinAmplitude();
230  } else {
231  fMinData[fCurrentHistogramName].erase(*peakIter);
232  }
233  if ((*peakIter)->IsMaxAmplitudeSet()) {
234  fMaxData[fCurrentHistogramName][*peakIter] = (*peakIter)->GetMaxAmplitude();
235  } else {
236  fMaxData[fCurrentHistogramName].erase(*peakIter);
237  }
238  }
239  }
240 }
241 
242 void QGCalibrationWindow::SetProperty(const string& key, const string& value)
243 {
244  if (key == PEAK_KEY) {
245  TString valueTString(value);
246  if (valueTString.IsFloat()) {
247  AddPeak(valueTString.Atof());
248  }
249  }
250  if (key == CALIBRATION_OUTPUT_FILENAME_KEY) {
251  fOutputFilename = value;
252  }
253 }
254 
256 {
257  fHistogramDropDownBox->RemoveAll();
258 
259  Int_t id = 0;
260  const list<QGHistogram*>& histograms = QGHistogram::GetHistograms();
261  list<QGHistogram*>::const_iterator histogramIter;
262  for (histogramIter = histograms.begin(); histogramIter != histograms.end(); ++histogramIter) {
263  fHistogramDropDownBox->AddEntry((*histogramIter)->GetName(), id++);
264  }
265  fHistogramDropDownBox->Layout();
266 }
const int channel
#define PEAK_KEY
#define CALIBRATION_OUTPUT_FILENAME_KEY
ClassImp(QObject)
Part of a QGCalibrationWindow for a specific peak.
void SetEnergy(Double_t energy)
Set energy.
Used to select amplitude windows for calibration peaks.
std::map< std::string, std::map< QGCalibrationPeakFrame *, Double_t > > fMaxData
Max amplitude data for each histogram and peak.
TGTextButton * fSaveButton
Save button.
QGPlot * GetCurrentPlot() const
Get current plot.
void LoadData(const std::string &histogramName)
Load data for histogram.
void HandleSaveButton()
Handle save button.
void AddPeak(Double_t energy)
Add peak.
void SetUpHistograms()
Set up histogram drop down box.
void Clear(Option_t *opt="")
Clear data.
void HandleHistogramDropDownBox(const char *text)
Handle histogram drop down box.
TGHorizontalFrame * fHistogramFrame
Histogram frame.
std::string fCurrentHistogramName
Current histogram name.
TGTextButton * fDrawButton
Draw button.
TGComboBox * fHistogramDropDownBox
Histogram drop down box.
std::string fOutputFilename
Output filename.
void HandleDrawButton()
Handle draw button.
void SaveData()
Save data for current histogram.
std::map< std::string, std::map< QGCalibrationPeakFrame *, Double_t > > fMinData
Min amplitude data for each histogram and peak.
void SetProperty(const std::string &key, const std::string &value)
Set property (used for opening a session)
QGCalibrationWindow(const TGWindow *p=0, UInt_t w=350, UInt_t h=300)
Constructor.
virtual ~QGCalibrationWindow()
Destructor.
std::vector< QGCalibrationPeakFrame * > fCalibrationPeakFrames
Collection of calibration peak frames.
static const std::list< QGHistogram * > & GetHistograms()
Get collection of histograms.
Definition: QGHistogram.hh:58
Base class for GUI plots including histograms, scatter plots, pulses, and graphical cuts.
Definition: QGPlot.hh:21
virtual void Plot()
Plot this in the associated canvas window, generating it if necessary.
Definition: QGPlot.cc:195
static QGPlot * GetPlotByName(const std::string &name)
Get plot by name.
Definition: QGPlot.cc:155
Base class for GUI windows.
Definition: QGWindow.hh:15
virtual void SetWindowName(const char *name=0)
Set name of window.
Definition: QGWindow.cc:52
std::string Resize(const std::string &s, size_t len)
resize a string to len, adding spaces if necessary