Diana Software
QGSessionHandler.cc
Go to the documentation of this file.
1 #include "QGSessionHandler.hh"
2 #include <iostream>
3 #include <list>
4 #include <sstream>
5 #include <typeinfo>
6 #include <vector>
7 #include <utility>
8 #include "QGCalibrationWindow.hh"
9 #include "QGCanvasWindow.hh"
10 #include "QGDefaultsHandler.hh"
11 #include "QGFileHandler.hh"
12 #include "QGGraphicalCut.hh"
13 #include "QGHistogram.hh"
14 #include "QGPlotListWindow.hh"
15 #include "QGPulse.hh"
16 #include "QGScatterPlot.hh"
17 #include "QGTextFileHandler.hh"
18 #include "QGWindow.hh"
19 #include "TGFileDialog.h"
20 #include "TString.h"
21 
23 
24 using std::cout;
25 using std::endl;
26 using std::flush;
27 using std::list;
28 using std::pair;
29 using std::string;
30 using std::stringstream;
31 using std::vector;
32 
34 fCurrentCalibrationWindow(0), fCurrentCanvasWindow(0), fCurrentPlot(0)
35 {
36 }
37 
39 {
40 }
41 
43 {
44  string sessionDirectory = QGDefaultsHandler::Instance()->GetSessionDirectory();
45  TString dir(sessionDirectory.c_str());
46  TGFileInfo fi;
47  fi.fIniDir = StrDup(dir);
48  const char *filetypes[] = {"GUI session files" , "*.gui",
49  0, 0};
50  fi.fFileTypes = filetypes;
51  new TGFileDialog(gClient->GetRoot(), 0, kFDOpen, &fi);
52  if (fi.fFilename) {
53  OpenSession(fi.fFilename, window);
54  string currentDirectory = fi.fIniDir;
55  if (currentDirectory != sessionDirectory) {
57  }
58  }
59 }
60 
61 void QGSessionHandler::OpenSession(string filename, QGCanvasWindow *window)
62 {
63  QGTextFileHandler *textFileHandler = new QGTextFileHandler();
64  if (textFileHandler->ReadFile(filename)) {
65  const vector<pair<string, string> >& lines = textFileHandler->GetLines();
66  vector<pair<string, string> >::const_iterator lineIter;
67  for (lineIter = lines.begin(); lineIter != lines.end(); ++lineIter) {
68  const string& key = lineIter->first;
69  const string& value = lineIter->second;
70  if (key == CANVAS_WINDOW_KEY) {
71  if (window) {
72  fCurrentCanvasWindow = window;
73  window = 0;
74  } else {
76  }
77  fCurrentPlot = 0;
78  } else if (key == HISTOGRAM_KEY) {
79  fCurrentPlot = new QGHistogram();
83  } else if (key == SCATTER_PLOT_KEY) {
88  } else if (key == PULSE_KEY) {
89  fCurrentPlot = new QGPulse();
93  } else if (key == GRAPHICAL_CUT_KEY) {
98  } else if (key == CALIBRATION_WINDOW_KEY) {
101  fCurrentPlot = 0;
102  } else if (key == NAME_KEY) {
103  if (fCurrentCanvasWindow) {
104  fCurrentCanvasWindow->SetWindowName(value.c_str());
106  } else if (fCurrentPlot) {
107  fCurrentPlot->SetName(value.c_str());
108  }
109  } else if (key == CANVAS_WINDOW_NAME_KEY) {
110  if (fCurrentPlot) {
111  if (fCanvasWindowsMap.count(value) != 0) {
113  }
114  }
115  } else {
116  if (fCurrentCanvasWindow) {
117  fCurrentCanvasWindow->SetProperty(key, value);
118  } else if (fCurrentPlot) {
119  fCurrentPlot->SetProperty(key, value);
120  } else if (fCurrentCalibrationWindow) {
122  }
123  }
124  }
126  } else {
127  cout << "Unable to read file " << filename << endl;
128  }
129  delete textFileHandler;
130 }
131 
133 {
134  string sessionDirectory = QGDefaultsHandler::Instance()->GetSessionDirectory();
135  TString dir(sessionDirectory.c_str());
136  TGFileInfo fi;
137  fi.fIniDir = StrDup(dir);
138  const char *filetypes[] = {"GUI session files" , "*.gui",
139  0, 0};
140  fi.fFileTypes = filetypes;
141  new TGFileDialog(gClient->GetRoot(), 0, kFDSave, &fi);
142  if (fi.fFilename) {
143  TString filename(fi.fFilename);
144  TString extension(fi.fFileTypes[fi.fFileTypeIdx+1]);
145  extension.Remove(TString::kLeading, '*');
146  if (!filename.EndsWith(extension)) {
147  filename.Append(extension);
148  }
149  SaveSession(filename.Data());
150  string currentDirectory = fi.fIniDir;
151  if (currentDirectory != sessionDirectory) {
153  }
154  }
155 }
156 
157 void QGSessionHandler::SaveSession(string filename)
158 {
159  // open file for output
160  QGTextFileHandler *textFileHandler = new QGTextFileHandler();
161 
162  // add canvas windows to output
163  const list<QGWindow*>& windows = QGWindow::GetWindows();
164  list<QGWindow*>::const_iterator windowIter;
165  for (windowIter = windows.begin(); windowIter != windows.end(); ++windowIter) {
166  if (QGCanvasWindow *canvasWindow = dynamic_cast<QGCanvasWindow*>(*windowIter)) {
167  string name = canvasWindow->GetWindowName();
168  vector<string> filenames = canvasWindow->GetFileHandler()->GetFilenames();
169  textFileHandler->AppendLine(CANVAS_WINDOW_KEY);
170  textFileHandler->AppendLine(NAME_KEY, name);
171  vector<string>::const_iterator fileIter;
172  for (fileIter = filenames.begin(); fileIter != filenames.end(); ++fileIter) {
173  textFileHandler->AppendLine(FILE_KEY, *fileIter);
174  }
175  textFileHandler->AppendLine();
176  }
177  }
178 
179  // add plots to output
180  const list<QGPlot*>& plots = QGPlot::GetPlots();
181  list<QGPlot*>::const_iterator plotIter;
182  for (plotIter = plots.begin(); plotIter != plots.end(); ++plotIter) {
183  string name = (*plotIter)->GetName();
184  string xVariable = (*plotIter)->GetXVariable();
185  string xUnits = (*plotIter)->GetXUnits();
186  string yVariable = (*plotIter)->GetYVariable();
187  string yUnits = (*plotIter)->GetYUnits();
188  bool superimpose = (*plotIter)->IsSuperimposeSet();
189  const vector<string>& cuts = (*plotIter)->GetCuts();
190  vector<string>::const_iterator cutIter;
191 
192  if (QGHistogram *histogram = dynamic_cast<QGHistogram*>(*plotIter)) {
193  textFileHandler->AppendLine(HISTOGRAM_KEY);
194  textFileHandler->AppendLine(NAME_KEY, name);
195  textFileHandler->AppendLine(X_VARIABLE_KEY, xVariable);
196  textFileHandler->AppendLine(X_UNITS_KEY, xUnits);
197  if (histogram->IsXMinSet()) {
198  Double_t min = histogram->GetXMin();
199  textFileHandler->AppendLine(MIN_KEY, min);
200  }
201  if (histogram->IsXMaxSet()) {
202  Double_t max = histogram->GetXMax();
203  textFileHandler->AppendLine(MAX_KEY, max);
204  }
205  if (histogram->IsNumberOfBinsSet()) {
206  Int_t numberOfBins = histogram->GetNumberOfBins();
207  textFileHandler->AppendLine(NUMBER_OF_BINS_KEY, numberOfBins);
208  }
209  if (superimpose) {
210  textFileHandler->AppendLine(SUPERIMPOSE_KEY, "true");
211  }
212  for (cutIter = cuts.begin(); cutIter != cuts.end(); ++ cutIter) {
213  textFileHandler->AppendLine(CUT_KEY, *cutIter);
214  }
215  textFileHandler->AppendLine(FILL_COLOR_KEY, histogram->GetFillColor());
216  textFileHandler->AppendLine(FILL_STYLE_KEY, histogram->GetFillStyle());
217  textFileHandler->AppendLine(LINE_COLOR_KEY, histogram->GetLineColor());
218  textFileHandler->AppendLine(LINE_STYLE_KEY, histogram->GetLineStyle());
219  textFileHandler->AppendLine(LINE_WIDTH_KEY, histogram->GetLineWidth());
220  textFileHandler->AppendLine(MARKER_COLOR_KEY, histogram->GetMarkerColor());
221  textFileHandler->AppendLine(MARKER_SIZE_KEY, histogram->GetMarkerSize());
222  textFileHandler->AppendLine(MARKER_STYLE_KEY, histogram->GetMarkerStyle());
223  }
224  else if (QGScatterPlot *scatterPlot = dynamic_cast<QGScatterPlot*>(*plotIter)) {
225  textFileHandler->AppendLine(SCATTER_PLOT_KEY);
226  textFileHandler->AppendLine(NAME_KEY, name);
227  textFileHandler->AppendLine(X_VARIABLE_KEY, xVariable);
228  textFileHandler->AppendLine(X_UNITS_KEY, xUnits);
229  textFileHandler->AppendLine(Y_VARIABLE_KEY, yVariable);
230  textFileHandler->AppendLine(Y_UNITS_KEY, yUnits);
231  if (superimpose) {
232  textFileHandler->AppendLine(SUPERIMPOSE_KEY, "true");
233  }
234 
235  for (cutIter = cuts.begin(); cutIter != cuts.end(); ++ cutIter) {
236  textFileHandler->AppendLine(CUT_KEY, *cutIter);
237  }
238  textFileHandler->AppendLine(MARKER_COLOR_KEY, scatterPlot->GetMarkerColor());
239  textFileHandler->AppendLine(MARKER_SIZE_KEY, scatterPlot->GetMarkerSize());
240  textFileHandler->AppendLine(MARKER_STYLE_KEY, scatterPlot->GetMarkerStyle());
241  }
242  else if (QGPulse *pulse = dynamic_cast<QGPulse*>(*plotIter)) {
243  textFileHandler->AppendLine(PULSE_KEY);
244  textFileHandler->AppendLine(NAME_KEY, name);
245  Int_t eventIndex = pulse->GetEventIndex();
246  textFileHandler->AppendLine(EVENT_INDEX_KEY, eventIndex);
247  const string& orderVariable = pulse->GetOrderVariable();
248  if (orderVariable != "") {
249  textFileHandler->AppendLine(ORDER_VARIABLE_KEY, orderVariable);
250  }
251  const string& samplesVariable = pulse->GetSamplesLabel();
252  if (samplesVariable != "") {
253  textFileHandler->AppendLine(PULSE_VARIABLE_KEY, samplesVariable);
254  }
255  if (superimpose) {
256  textFileHandler->AppendLine(SUPERIMPOSE_KEY, "true");
257  }
258  for (cutIter = cuts.begin(); cutIter != cuts.end(); ++ cutIter) {
259  textFileHandler->AppendLine(CUT_KEY, *cutIter);
260  }
261  textFileHandler->AppendLine(LINE_COLOR_KEY, pulse->GetLineColor());
262  textFileHandler->AppendLine(LINE_STYLE_KEY, pulse->GetLineStyle());
263  textFileHandler->AppendLine(LINE_WIDTH_KEY, pulse->GetLineWidth());
264  textFileHandler->AppendLine(MARKER_COLOR_KEY, pulse->GetMarkerColor());
265  textFileHandler->AppendLine(MARKER_SIZE_KEY, pulse->GetMarkerSize());
266  textFileHandler->AppendLine(MARKER_STYLE_KEY, pulse->GetMarkerStyle());
267  }
268  else if (QGGraphicalCut *graphicalCut = dynamic_cast<QGGraphicalCut*>(*plotIter)) {
269  textFileHandler->AppendLine(GRAPHICAL_CUT_KEY);
270  textFileHandler->AppendLine(NAME_KEY, name);
271  textFileHandler->AppendLine(X_VARIABLE_KEY, xVariable);
272  textFileHandler->AppendLine(Y_VARIABLE_KEY, yVariable);
273  for (Int_t vertex = 0; vertex < graphicalCut->GetN() - 1; ++vertex) {
274  stringstream vertexStream;
275  vertexStream.precision(16);
276  vertexStream << graphicalCut->GetX()[vertex] << " , " << graphicalCut->GetY()[vertex] << flush;
277  textFileHandler->AppendLine(VERTEX_KEY, vertexStream.str());
278  }
279  }
280  if ((*plotIter)->GetCanvasWindow()) {
281  string canvasWindowName = (*plotIter)->GetCanvasWindow()->GetWindowName();
282  textFileHandler->AppendLine(CANVAS_WINDOW_NAME_KEY, canvasWindowName);
283  }
284  textFileHandler->AppendLine();
285  }
286 
287  // write file
288  textFileHandler->WriteFile(filename);
289  delete textFileHandler;
290 }
double max
Definition: CheckOF.C:53
#define FILE_KEY
#define VERTEX_KEY
#define FILL_COLOR_KEY
#define HISTOGRAM_KEY
#define MAX_KEY
#define NUMBER_OF_BINS_KEY
#define CUT_KEY
#define ORDER_VARIABLE_KEY
#define EVENT_INDEX_KEY
#define MARKER_SIZE_KEY
#define Y_VARIABLE_KEY
#define GRAPHICAL_CUT_KEY
#define PULSE_KEY
#define MARKER_COLOR_KEY
#define MIN_KEY
#define CANVAS_WINDOW_NAME_KEY
#define CALIBRATION_WINDOW_KEY
#define X_UNITS_KEY
#define SUPERIMPOSE_KEY
#define CANVAS_WINDOW_KEY
#define SCATTER_PLOT_KEY
#define LINE_STYLE_KEY
#define Y_UNITS_KEY
#define LINE_WIDTH_KEY
#define FILL_STYLE_KEY
#define NAME_KEY
#define PULSE_VARIABLE_KEY
#define LINE_COLOR_KEY
#define X_VARIABLE_KEY
#define MARKER_STYLE_KEY
ClassImp(QObject)
double min(const Diana::QVector &v)
Definition: QVector.cc:878
Used to select amplitude windows for calibration peaks.
void SetProperty(const std::string &key, const std::string &value)
Set property (used for opening a session)
Window containing a ROOT canvas.
void SetProperty(std::string key, std::string value)
Set property (used for opening a session)
void SetSessionDirectory(const std::string &directory)
Set session directory.
static QGDefaultsHandler * Instance()
Singleton.
const std::string & GetSessionDirectory()
Get session directory.
Class for graphical cuts.
Class for GUI histograms.
Definition: QGHistogram.hh:19
static void Update()
Update the plot list.
static const std::list< QGPlot * > & GetPlots()
Get collection of plots.
Definition: QGPlot.hh:84
virtual void SetCanvasWindow(QGCanvasWindow *window)
Set the canvas window to which the plot is associated.
Definition: QGPlot.cc:216
virtual void SetProperty(const std::string &key, const std::string &value)
Set property (used for opening a session)
Definition: QGPlot.cc:245
virtual void AutoSetStyle()
Automatically set the plot style, should be overridden by derived classes.
Definition: QGPlot.cc:67
virtual void SetName(const char *name=0)
Set plot name.
Definition: QGPlot.cc:240
Class for GUI pulse plots.
Definition: QGPulse.hh:18
Class for GUI scatter plots.
Class to handle saving and opening sessions.
QGCanvasWindow * fCurrentCanvasWindow
Current canvas window being created by OpenSession.
virtual ~QGSessionHandler()
Destructor.
QGCalibrationWindow * fCurrentCalibrationWindow
Current calibration window being created by OpenSession.
QGSessionHandler()
Constructor.
std::map< std::string, QGCanvasWindow * > fCanvasWindowsMap
Collection of canvas windows created by OpenSession.
void SaveSession()
Save session.
void OpenSession(QGCanvasWindow *window=0)
Open session.
QGPlot * fCurrentPlot
Current plot being created by OpenSession.
Class to handle input and output for text files.
const std::vector< std::pair< std::string, std::string > > & GetLines()
Get the lines of the file as key, value pairs.
bool ReadFile(std::string filename)
Read file, returns true if successful.
bool WriteFile(std::string filename)
Write file, returns true if successful.
void AppendLine(std::string key="", std::string value="")
Append a key and value to the file.
static const std::list< QGWindow * > & GetWindows()
Get collection of windows.
Definition: QGWindow.hh:27
virtual void SetWindowName(const char *name=0)
Set name of window.
Definition: QGWindow.cc:52