Diana Software
QGPlot.cc
Go to the documentation of this file.
1 #include "QGPlot.hh"
2 #include <algorithm>
3 #include <iostream>
4 #include <sstream>
5 #include "QGCanvasWindow.hh"
6 #include "QGCutsFrame.hh"
7 #include "QGFileHandler.hh"
9 #include "QGPlotEditWindow.hh"
10 #include "QGPlotFrame.hh"
11 #include "QGPlotListWindow.hh"
12 #include "QGSessionHandler.hh"
13 #include "TCanvas.h"
14 #include "TRootEmbeddedCanvas.h"
15 
17 
18 using std::cout;
19 using std::endl;
20 using std::flush;
21 using std::list;
22 using std::string;
23 using std::stringstream;
24 using std::vector;
25 
26 list<QGPlot*> QGPlot::fPlots;
27 
29 fCanvasWindow(0), fPlotEditWindow(0), fSuperimpose(false)
30 {
31  fPlots.push_back(this);
32  SetRegenerate();
33 }
34 
35 QGPlot::QGPlot(const QGPlot& plot) :
36 fCanvasWindow(plot.fCanvasWindow), fCuts(plot.fCuts), fCutsString(plot.fCutsString),
37 fNeedsRegeneration(plot.fNeedsRegeneration), fPlotEditWindow(0), fSuperimpose(plot.fSuperimpose),
38 fXVariable(plot.fXVariable), fXUnits(plot.fXUnits), fYVariable(plot.fYVariable), fYUnits(plot.fYUnits)
39 {
40  fPlots.push_back(this);
41  if (fCanvasWindow) {
42  fCanvasWindow->AddPlot(this);
43  }
44 }
45 
47 {
48  if (find(fPlots.begin(), fPlots.end(), this) != fPlots.end()) {
49  fPlots.erase(find(fPlots.begin(), fPlots.end(), this));
50  }
51  if (fCanvasWindow) {
53  }
57  }
58 }
59 
60 void QGPlot::AddCut(const string& cut)
61 {
62  fCuts.push_back(cut);
64  SetRegenerate();
65 }
66 
68 {
69  // Should be overridden by derived classes
70 }
71 
73 {
74  // Should be overridden by derived classes
75 }
76 
78 {
79  // Should be overridden by derived classes
80 }
81 
83 {
84  cout << "Duplicate not implemented for this type" << endl;
85  // Should be overridden by derived classes
86 }
87 
89 {
90  // Should be overridden by derived classes
91 }
92 
94 {
95  // Should be overridden by derived classes
96 }
97 
98 string QGPlot::GeneratePlotName(const string& baseName) const
99 {
100  string name;
101  int i = 0;
102  do {
103  ++i;
104  stringstream index;
105  index << i << flush;
106  name = baseName + index.str();
107  } while (IsNameInUse(name));
108  return name;
109 }
110 
112 {
113  return fCanvasWindow->GetPlotFrame()->GetEmbeddedCanvas()->GetCanvas();
114 }
115 
117 {
119  string canvasWindow = fPlotEditWindow->GetButtonsFrame()->GetCanvasWindowName();
120  if (canvasWindow != "") {
121  QGWindow *window = QGWindow::GetWindowByName(canvasWindow);
122  if (QGCanvasWindow *newCanvasWindow = dynamic_cast<QGCanvasWindow*>(window)) {
123  //QGCanvasWindow *oldCanvasWindow = GetCanvasWindow();
124  if (newCanvasWindow != fCanvasWindow) { //oldCanvasWindow) {
125  //Erase();
126  //if (oldCanvasWindow) {
127  // oldCanvasWindow->RemovePlot(this);
128  //}
129  SetCanvasWindow(newCanvasWindow);
130  //newCanvasWindow->AddPlot(this);
131  }
132  }
133  }
134  }
135 }
136 
138 {
141  }
142 }
143 
144 const char *QGPlot::GetName() const
145 {
146  // Should be overridden by derived class
147  return "";
148 }
149 
151 {
152  // Should be overridden by derived class
153 }
154 
155 QGPlot *QGPlot::GetPlotByName(const string& name)
156 {
157  QGPlot *plot = 0;
158  list<QGPlot*>::const_iterator plotIter;
159  for (plotIter = fPlots.begin(); plotIter != fPlots.end(); ++plotIter) {
160  if ((*plotIter)->GetName() == name) {
161  plot = *plotIter;
162  break;
163  }
164  }
165  return plot;
166 }
167 
168 bool QGPlot::IsNameInUse(const string& name) const
169 {
170  bool inUse = false;
171  list<QGPlot*>::const_iterator plotIter;
172  for (plotIter = fPlots.begin(); plotIter != fPlots.end(); ++plotIter) {
173  if ((*plotIter)->GetName() == name) {
174  inUse = true;
175  break;
176  }
177  }
178  return inUse;
179 }
180 
182 {
183  fCutsString.clear();
184  if (fCuts.size() != 0) {
185  vector<string>::const_iterator cutIter = fCuts.begin();
186  fCutsString = "(" + *cutIter + ")";
187  ++cutIter;
188  while (cutIter != fCuts.end()) {
189  fCutsString += " && (" + *cutIter + ")";
190  ++cutIter;
191  }
192  }
193 }
194 
196 {
204  }
205  }
206  if (NeedsRegeneration()) {
207  Generate();
208  SetRegenerate(false);
209  }
210  if (fCanvasWindow) {
211  fCanvasWindow->GetPlotFrame()->GetEmbeddedCanvas()->GetCanvas()->cd();
212  }
213  Display();
214 }
215 
217 {
218  if (fCanvasWindow != window) {
219  if (fCanvasWindow) {
220  Erase();
221  fCanvasWindow->RemovePlot(this);
222  }
223  if (window) {
224  window->AddPlot(this);
225  }
226  fCanvasWindow = window;
227  SetRegenerate();
228  }
229 }
230 
231 void QGPlot::SetCuts(const vector<string>& cuts)
232 {
233  if (fCuts != cuts) {
234  fCuts = cuts;
235  MakeCutsString();
236  SetRegenerate();
237  }
238 }
239 
240 void QGPlot::SetName(const char *name)
241 {
242  // Should be overridden by derived class
243 }
244 
245 void QGPlot::SetProperty(const string& key, const string& value)
246 {
247  if (key == NAME_KEY) {
248  SetName(value.c_str());
249  }
250  else if (key == CUT_KEY) {
251  AddCut(value);
252  }
253  else if (key == X_VARIABLE_KEY) {
254  SetXVariable(value);
255  }
256  else if (key == X_UNITS_KEY) {
257  SetXUnits(value);
258  }
259  else if (key == Y_VARIABLE_KEY) {
260  SetYVariable(value);
261  }
262  else if (key == Y_UNITS_KEY) {
263  SetYUnits(value);
264  }
265  else if (key == SUPERIMPOSE_KEY) {
266  SetSuperimpose(value == "true");
267  }
268 }
269 
270 void QGPlot::SetXVariable(const string& x)
271 {
272  if (fXVariable != x) {
273  fXVariable = x;
274  SetRegenerate();
275  }
276 }
277 
278 void QGPlot::SetYVariable(const string& y)
279 {
280  if (fYVariable != y) {
281  fYVariable = y;
282  SetRegenerate();
283  }
284 }
285 
287 {
289 }
#define CUT_KEY
#define Y_VARIABLE_KEY
#define X_UNITS_KEY
#define SUPERIMPOSE_KEY
#define Y_UNITS_KEY
#define NAME_KEY
#define X_VARIABLE_KEY
ClassImp(QObject)
TChain used in diana.
Definition: QChain.hh:23
Window containing a ROOT canvas.
void RemovePlot(QGPlot *plot)
Remove pointer to a plot from the list of plots associated with this window.
QGPlotFrame * GetPlotFrame()
Get pointer to the plot frame.
void AddPlot(QGPlot *plot)
Add pointer to a plot to the list of plots associated with this window.
void SetPlotsToRegenerate()
Set regenerate for each plot associated with this window.
QGFileHandler * GetFileHandler() const
Get pointer to file handler.
void UpdateVariablesDropDownBoxes()
Update variables drop down boxes for plots associated with this window.
std::vector< std::string > GetCuts()
Get cuts.
Definition: QGCutsFrame.cc:101
bool CheckForModifiedFiles()
Check whether any open file has been modified.
std::string GetCanvasWindowName()
Get name of selected canvas window.
QGPlotEditButtonsFrame * GetButtonsFrame()
Get pointer to the buttons frame.
QGCutsFrame * GetCutsFrame()
Get pointer to the cuts frame.
TRootEmbeddedCanvas * GetEmbeddedCanvas()
Get pointer to canvas.
Definition: QGPlotFrame.hh:26
void RemoveEntry(QGPlot *plot)
Remove entry for plot.
static bool IsVisible()
Returns true if the plot list window exists.
void Refresh()
Refresh the contents of the window.
static QGPlotListWindow * Instance()
Singleton.
Base class for GUI plots including histograms, scatter plots, pulses, and graphical cuts.
Definition: QGPlot.hh:21
virtual void SetCuts(const std::vector< std::string > &cuts)
Set cuts to apply in generating the plot.
Definition: QGPlot.cc:231
virtual void SetSuperimpose(bool flag=false)
Set whether the plot should be superimposed.
Definition: QGPlot.hh:132
virtual std::string GeneratePlotName(const std::string &baseName) const
Generate a name for the plot.
Definition: QGPlot.cc:98
virtual bool IsNameInUse(const std::string &name) const
Check whether string is the name of any plot.
Definition: QGPlot.cc:168
std::string fXVariable
X-axis variable.
Definition: QGPlot.hh:172
virtual void GetCutsFromEditWindow()
Get cuts from edit window.
Definition: QGPlot.cc:137
virtual ~QGPlot()
Destructor.
Definition: QGPlot.cc:46
QGPlot()
Default constructor.
Definition: QGPlot.cc:28
virtual const char * GetName() const
Get plot name, should be overridden by derived classes.
Definition: QGPlot.cc:144
virtual void Duplicate()
Duplicate the plot, should be overridden by derived classes.
Definition: QGPlot.cc:82
virtual void GetCanvasWindowFromEditWindow()
Get canvas window from edit window.
Definition: QGPlot.cc:116
static void ShowPlotsList()
Show plots list.
Definition: QGPlot.cc:286
virtual TCanvas * GetCanvas()
Get pointer to the associated canvas.
Definition: QGPlot.cc:111
virtual void Generate(QChain *chain=0)
Generate plot from chain of data files, should be overridden by derived classes.
Definition: QGPlot.cc:93
virtual void Plot()
Plot this in the associated canvas window, generating it if necessary.
Definition: QGPlot.cc:195
static std::list< QGPlot * > fPlots
Collection of pointers to all QGPlots.
Definition: QGPlot.hh:166
QGPlotEditWindow * fPlotEditWindow
Pointer to plot edit window.
Definition: QGPlot.hh:163
static QGPlot * GetPlotByName(const std::string &name)
Get plot by name.
Definition: QGPlot.cc:155
virtual void SetRegenerate(bool flag=true)
Set whether the plot needs to be regenerated.
Definition: QGPlot.hh:129
virtual void SetCanvasWindow(QGCanvasWindow *window)
Set the canvas window to which the plot is associated.
Definition: QGPlot.cc:216
virtual bool NeedsRegeneration() const
Check whether plot needs to be regenerated.
Definition: QGPlot.hh:108
std::vector< std::string > fCuts
Cuts applied to make the plot.
Definition: QGPlot.hh:154
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 AddCut(const std::string &cut)
Add cut.
Definition: QGPlot.cc:60
virtual void SetName(const char *name=0)
Set plot name.
Definition: QGPlot.cc:240
std::string fCutsString
Cuts in a single string.
Definition: QGPlot.hh:157
virtual void SetXVariable(const std::string &x)
Set X-axis variable.
Definition: QGPlot.cc:270
virtual void DisplayEditWindow()
Display edit window for the plot, should be overridden by derived classes.
Definition: QGPlot.cc:77
virtual void SetYVariable(const std::string &y)
Set Y-axis variable.
Definition: QGPlot.cc:278
virtual void GetParametersFromEditWindow()
Get parameters from edit window, should be overridden by derived classes.
Definition: QGPlot.cc:150
std::string fYVariable
Y-axis variable.
Definition: QGPlot.hh:178
virtual void Erase()
Erase plot, should be overridden by derived classes.
Definition: QGPlot.cc:88
virtual void MakeCutsString()
Make cuts string.
Definition: QGPlot.cc:181
QGCanvasWindow * fCanvasWindow
Canvas window to which plot is associated.
Definition: QGPlot.hh:151
virtual void SetXUnits(const std::string &units)
Set X-axis units.
Definition: QGPlot.hh:138
virtual void SetYUnits(const std::string &units)
Set Y-axis units.
Definition: QGPlot.hh:144
virtual void Display()
Display plot, should be overridden by derived classes.
Definition: QGPlot.cc:72
Base class for GUI windows.
Definition: QGWindow.hh:15
static QGWindow * GetWindowByName(const std::string &name)
Get pointer to window with a given name.
Definition: QGWindow.cc:39