Diana Software
QGScatterPlot.cc
Go to the documentation of this file.
1 #include "QGScatterPlot.hh"
2 #include <algorithm>
3 #include <iostream>
4 #include <map>
5 #include <set>
6 #include <sstream>
7 #include <string>
8 #include <typeinfo>
9 #include <vector>
10 #include "QChain.hh"
11 #include "QGCanvasWindow.hh"
12 #include "QGFileHandler.hh"
13 #include "QGPlotEditWindow.hh"
16 #include "QGPlotFrame.hh"
17 #include "QGPlotStyleFrame.hh"
18 #include "QGTextFileKeys.hh"
19 #include "TAxis.h"
20 #include "TCanvas.h"
21 #include "TH1F.h"
22 #include "TRootEmbeddedCanvas.h"
23 #include "TString.h"
24 #include "TStyle.h"
25 
27 
28 using std::cout;
29 using std::endl;
30 using std::list;
31 using std::map;
32 using std::set;
33 using std::string;
34 using std::stringstream;
35 using std::vector;
36 
37 list<QGScatterPlot*> QGScatterPlot::fScatterPlots;
38 
40 QGGraph(), fStyleScatterPlot(new TGraph())
41 {
42  fScatterPlots.push_back(this);
43  SetName();
44  SetBit(kCanDelete, kFALSE);
45  SetEditable(kFALSE);
46  MakeStylePlot();
47 }
48 
50 QGGraph(scatterPlot), fStyleScatterPlot(new TGraph())
51 {
52  fScatterPlots.push_back(this);
53  SetName();
54  SetBit(kCanDelete, kFALSE);
55  SetEditable(kFALSE);
56  MakeStylePlot();
57 }
58 
60 {
61  if (fPlotEditWindow) {
62  fPlotEditWindow->CloseWindow();
63  fPlotEditWindow = 0;
64  }
65  if (find(fScatterPlots.begin(), fScatterPlots.end(), this) != fScatterPlots.end()) {
66  fScatterPlots.erase(find(fScatterPlots.begin(), fScatterPlots.end(), this));
67  }
68  delete fStyleScatterPlot;
69 }
70 
72 {
73  SetMarkerStyle(20); // point
74  SetMarkerSize(0.4);
75 
76  // set standard plot color
77  vector<Color_t> standardColors;
78  standardColors.push_back(kRed - 3);
79  standardColors.push_back(kBlue + 1);
80  standardColors.push_back(kGreen + 2);
81  standardColors.push_back(kMagenta + 2);
82  standardColors.push_back(kCyan + 3);
83 
84  // count how many existing scatter plots are using each standard color
85  map<Color_t, int> colorCount;
86  // map index works like colorCount[color] = number of plots with color
87  for ( list<QGScatterPlot*>::const_iterator plotIter
88  = fScatterPlots.begin();
89  plotIter != fScatterPlots.end();
90  ++plotIter ) {
91  Color_t otherColor = (*plotIter)->GetMarkerColor();
92  vector<Color_t>::iterator colorIter
93  = find( standardColors.begin(), standardColors.end(), otherColor );
94  if ( colorIter != standardColors.end() ) {
95  ++colorCount[*colorIter];
96  }
97  }
98 
99  // find the least used color among the existing scatter plots
100  Color_t leastUsedColor = standardColors.front();
101  int leastUsedNumber = colorCount[leastUsedColor];
102  for ( vector<Color_t>::const_iterator colorIter = standardColors.begin();
103  colorIter != standardColors.end();
104  ++colorIter ) {
105  if (colorCount[*colorIter] < leastUsedNumber) {
106  leastUsedColor = *colorIter;
107  leastUsedNumber = colorCount[*colorIter];
108  }
109  }
110 
111  // assign the least used color to this plot
112  SetMarkerColor(leastUsedColor);
113 
114  MakeStylePlot();
115 }
116 
118 {
119  delete fHistogram;
120  fHistogram = 0;
121  if (fStyleScatterPlot) {
122  fStyleScatterPlot->TAttFill::Copy(*this);
123  fStyleScatterPlot->TAttLine::Copy(*this);
124  fStyleScatterPlot->TAttMarker::Copy(*this);
125  }
126  string xAxisLabel = GetXVariable();
127  string yAxisLabel = GetYVariable();
128  if (GetXUnits() != "") {
129  xAxisLabel += " (" + GetXUnits() + ")";
130  }
131  if (GetYUnits() != "") {
132  yAxisLabel += " (" + GetYUnits() + ")";
133  }
134  GetXaxis()->SetTitle(xAxisLabel.c_str());
135  GetYaxis()->SetTitle(yAxisLabel.c_str());
136  GetYaxis()->SetTitleOffset(1.35);
137  if (fCanvasWindow) {
138  bool drawSuperimpose = false;
139  if (fSuperimpose) {
140  if (fCanvasWindow->GetDrawnPlots().size() != 0) {
141  if (typeid(*fCanvasWindow->GetDrawnPlots().back()) == typeid(*this)) {
142  drawSuperimpose = true;
143  }
144  }
145  }
146  if (drawSuperimpose) {
147  fCanvasWindow->GetPlotFrame()->GetEmbeddedCanvas()->GetCanvas()->RecursiveRemove(this);
148  if (GetN() > 0) {
149  Draw("P"); //Draw("P same");
150  }
153  } else {
154  fCanvasWindow->GetPlotFrame()->GetEmbeddedCanvas()->GetCanvas()->Clear();
155  if (GetN() > 0) {
156  Draw("AP");
157  }
160  }
161  if (gPad) {
162  gPad->Update();
163  }
164  }
165 }
166 
168 {
169  if (!fPlotEditWindow) {
171  fPlotEditWindow->SetPlot(this);
172 
173  fPlotEditWindow->GetPlotStyleFrame()->GetEmbeddedCanvas()->GetCanvas()->SetEditable(kTRUE);
174  fPlotEditWindow->GetPlotStyleFrame()->GetEmbeddedCanvas()->GetCanvas()->cd();
176  fPlotEditWindow->GetPlotStyleFrame()->GetEmbeddedCanvas()->GetCanvas()->SetEditable(kFALSE);
177  }
178 }
179 
181 {
182  MakeStylePlot();
183  TH1F *supportHistogram = new TH1F();
184  supportHistogram->SetBit(kCanDelete, kTRUE);
185  supportHistogram->SetStats(kFALSE);
186  supportHistogram->SetBins(1, 0, 5);
187  supportHistogram->GetYaxis()->SetRangeUser(0, 20);
188  supportHistogram->Draw("AH");
189  fStyleScatterPlot->Draw("P");
190  if (gPad) {
191  gPad->Update();
192  }
193 }
194 
196 {
197  QGScatterPlot *scatterPlot = new QGScatterPlot(*this);
198  scatterPlot->AutoSetStyle();
199  scatterPlot->DisplayEditWindow();
200 }
201 
203 {
204  if (fCanvasWindow) {
205  fCanvasWindow->GetPlotFrame()->GetEmbeddedCanvas()->GetCanvas()->cd();
206  fCanvasWindow->GetPlotFrame()->GetEmbeddedCanvas()->GetCanvas()->RecursiveRemove(this);
207  fCanvasWindow->GetPlotFrame()->GetEmbeddedCanvas()->GetCanvas()->Update();
209  // to do: check whether this was the first drawn plot and if so redraw remaining plots in order to have axes
210  }
211 }
212 
214 {
215  Reset();
216  if (chain == 0) {
219  chain = fCanvasWindow->GetFileHandler()->GetQChain();
220  } else {
221  cout << "Cannot generate the scatter plot because there is no file open" << endl;
222  }
223  }
224  }
225  if (chain) {
226  if (fXVariable == "") {
227  cout << "Enter an x-axis variable for the scatter plot" << endl;
228  } else if (fYVariable == "") {
229  cout << "Enter a y-axis variable for the scatter plot" << endl;
230  } else {
231  chain->SetEstimate(chain->GetEntries());
232  string drawArgument = fXVariable + " : " + fYVariable;
233  chain->Draw(drawArgument.c_str(), fCutsString.c_str(), "goff");
234  Long64_t N = chain->GetSelectedRows();
235  if (N > 0) {
236  SetPoints(N, chain->GetV1(), chain->GetV2());
237  }
238  }
239  }
240 }
241 
243 {
244  if (fPlotEditWindow) {
245  if (QGScatterPlotEditWindow *scatterPlotEditWindow = dynamic_cast<QGScatterPlotEditWindow*>(fPlotEditWindow)) {
246  SetXVariable(scatterPlotEditWindow->GetScatterPlotParametersFrame()->GetXVariable());
247  SetYVariable(scatterPlotEditWindow->GetScatterPlotParametersFrame()->GetYVariable());
248  SetXUnits(scatterPlotEditWindow->GetScatterPlotParametersFrame()->GetXUnits());
249  SetYUnits(scatterPlotEditWindow->GetScatterPlotParametersFrame()->GetYUnits());
250  SetSuperimpose(scatterPlotEditWindow->GetScatterPlotParametersFrame()->IsSuperimposeSet());
251  } else {
252  cout << "QGScatterPlot::GetParametersFromEditWindow() Warning: dynamic_cast<QGScatterPlotEditWindow*> failed" << endl;
253  }
254  }
255 }
256 
258 {
259  fStyleScatterPlot->Set(5);
260  fStyleScatterPlot->SetPoint(0, 0.5, 2);
261  fStyleScatterPlot->SetPoint(1, 1.5, 3);
262  fStyleScatterPlot->SetPoint(2, 2.5, 6);
263  fStyleScatterPlot->SetPoint(3, 3.5, 11);
264  fStyleScatterPlot->SetPoint(4, 4.5, 18);
265  TAttFill::Copy(*fStyleScatterPlot);
266  TAttLine::Copy(*fStyleScatterPlot);
267  TAttMarker::Copy(*fStyleScatterPlot);
268 }
269 
270 void QGScatterPlot::SetName(const char *name)
271 {
272  if (name) {
273  string oldName = TGraph::GetName();
274  string newName = name;
275  if (newName != oldName) {
276  if (IsNameInUse(name)) {
277  TGraph::SetName(GeneratePlotName(newName).c_str());
278  } else {
279  TGraph::SetName(name);
280  }
281  }
282  } else {
283  TGraph::SetName(GeneratePlotName("ScatterPlot").c_str());
284  }
285 }
286 
287 void QGScatterPlot::SetProperty(const string& key, const string& value)
288 {
289  QGPlot::SetProperty(key, value);
290  TString valueTString(value);
291  if (key == MARKER_COLOR_KEY) {
292  if (valueTString.IsFloat()) {
293  SetMarkerColor(valueTString.Atoi());
294  }
295  }
296  else if (key == MARKER_SIZE_KEY) {
297  if (valueTString.IsFloat()) {
298  SetMarkerSize(valueTString.Atof());
299  }
300  }
301  else if (key == MARKER_STYLE_KEY) {
302  if (valueTString.IsFloat()) {
303  SetMarkerStyle(valueTString.Atoi());
304  }
305  }
306 }
int N
Definition: CheckOF.C:24
ahisto2 Draw()
#define MARKER_SIZE_KEY
#define MARKER_COLOR_KEY
#define MARKER_STYLE_KEY
ClassImp(QObject)
TChain used in diana.
Definition: QChain.hh:23
void RemoveFromDrawnPlots(QGPlot *plot)
Remove pointer to a plot from the list of drawn plots.
void AddToDrawnPlots(QGPlot *plot)
Add pointer to a plot to the list of drawn plots.
QGPlotFrame * GetPlotFrame()
Get pointer to the plot frame.
QGFileHandler * GetFileHandler() const
Get pointer to file handler.
void ClearDrawnPlots()
Clears the collection of drawn plots.
std::list< QGPlot * > & GetDrawnPlots()
Get collection of drawn plots.
bool IsFileOpen()
Check whether a file is open.
QChain * GetQChain()
Get a pointer to the chian.
Base class for scatter plots and pulses.
Definition: QGGraph.hh:14
virtual void Reset()
Reset the graph to an empty state.
Definition: QGGraph.cc:26
virtual void SetPoints(Int_t n, const Double_t *x, const Double_t *y)
Set the points of the graph.
Definition: QGGraph.cc:31
QGPlotStyleFrame * GetPlotStyleFrame()
Get pointer to the plot style frame.
virtual void SetPlot(QGPlot *plot)=0
Set plot, pure virtual function must be overriden by derived classes.
TRootEmbeddedCanvas * GetEmbeddedCanvas()
Get pointer to canvas.
Definition: QGPlotFrame.hh:26
TRootEmbeddedCanvas * GetEmbeddedCanvas()
Get pointer to embedded canvas.
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 const std::string & GetYUnits() const
Get y-axis units.
Definition: QGPlot.hh:93
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
QGPlotEditWindow * fPlotEditWindow
Pointer to plot edit window.
Definition: QGPlot.hh:163
virtual const std::string & GetXUnits() const
Get x-axis units.
Definition: QGPlot.hh:87
virtual void SetProperty(const std::string &key, const std::string &value)
Set property (used for opening a session)
Definition: QGPlot.cc:245
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 SetYVariable(const std::string &y)
Set Y-axis variable.
Definition: QGPlot.cc:278
std::string fYVariable
Y-axis variable.
Definition: QGPlot.hh:178
virtual const std::string & GetXVariable() const
Get x-axis variable.
Definition: QGPlot.hh:90
virtual const std::string & GetYVariable() const
Get y-axis variable.
Definition: QGPlot.hh:96
bool fSuperimpose
Indicates whether plot should be superimposed on existing plot.
Definition: QGPlot.hh:169
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
Class for scatter plot edit window.
Class for GUI scatter plots.
void DisplayEditWindow()
Display edit window.
void Display()
Display scatter plot.
void SetName(const char *name=0)
Set name of the scatter plot.
void SetProperty(const std::string &key, const std::string &value)
Set property (used for opening a session)
void Generate(QChain *chain=0)
Generate the scatter plot.
void Duplicate()
Duplicate the scatter plot.
void GetParametersFromEditWindow()
Get parameters from edit window.
void AutoSetStyle()
Automatically set the scatter plot style.
QGScatterPlot()
Default constructor.
void Erase()
Erase the scatter plot.
TGraph * fStyleScatterPlot
Mini-scatter plot to show style of this scatter plot.
void MakeStylePlot()
Make style plot.
static std::list< QGScatterPlot * > fScatterPlots
Collection of pointers to all QGScatterPlots.
void DrawStyleScatterPlot()
Draw a mini-scatter plot showing the style of the scatter plot.
virtual ~QGScatterPlot()
Destructor.