PlotView.cxx
Go to the documentation of this file.
1 /// \file PlotView.cxx
2 /// \brief Display a collection of plots for this event
3 /// \author messier@indiana.edu
4 /// \version $Id: PlotView.cxx,v 1.5 2012-02-22 01:41:58 bckhouse Exp $
5 ///
7 #include <iostream>
8 #include <string>
9 #include <vector>
12 #include "TCanvas.h"
13 #include "TH1F.h"
14 #include "TH2F.h"
15 #include "TH1D.h"
16 #include "TH2D.h"
17 #include "TProfile.h"
18 #include "TGraph.h"
19 #include "TGraphErrors.h"
22 
23 //
24 // Split up a string based on tokens.
25 //
26 static void tokenize(const std::string& str,
27  std::vector<std::string>& tokens,
28  const std::string& delimiters)
29 {
30  std::string::size_type lastPos = str.find_first_not_of(delimiters, 0);
31  std::string::size_type pos = str.find_first_of(delimiters, lastPos);
32 
33  while (std::string::npos != pos || std::string::npos != lastPos) {
34  tokens.push_back(str.substr(lastPos, pos-lastPos));
35  lastPos = str.find_first_not_of(delimiters, pos);
36  pos = str.find_first_of(delimiters, lastPos);
37  }
38 }
39 
40 //......................................................................
41 
42 static TObject* find_by_name(const std::string& n,
43  art::Handle< std::vector<TH1F> >& th1f,
44  art::Handle< std::vector<TH2F> >& th2f,
45  art::Handle< std::vector<TH1D> >& th1d,
46  art::Handle< std::vector<TH2D> >& th2d,
47  art::Handle< std::vector<TProfile> >& tprofile,
48  art::Handle< std::vector<TGraph> >& tgraph,
49  art::Handle< std::vector<TGraphErrors> >& tgrapherrors)
50 {
51  unsigned int i;
52 #define CHECK(V) \
53  { if (V.failedToGet()==false) {\
54  for (i=0; i<V->size(); ++i) {\
55  if (n==(*V)[i].GetName()) return (TObject*)&(*V)[i];\
56  }\
57  }\
58  }
59  CHECK(th1f);
60  CHECK(th2f);
61  CHECK(th1d);
62  CHECK(th2d);
63  CHECK(tprofile);
64  CHECK(tgraph);
65  CHECK(tgrapherrors);
66 #undef CHECK
67  return 0;
68 }
69 
70 //......................................................................
71 
72 namespace evd
73 {
74  //...................................................................
75 
76  PlotView::PlotView(TGMainFrame* mf) : evdb::Canvas(mf) { }
77 
78  //......................................................................
79 
81 
82  //......................................................................
83 
84  ///
85  /// Parse out the configuration strings into a collection of
86  /// modules, plots, and drawing options
87  ///
88  void PlotView::Parse(std::vector<PlotPad>& pads)
89  {
91 
92  unsigned int i, j;
93 
95  for (i=0; i<plot->fPadDescription.size(); ++i) {
96  PlotPad pad;
97 
98  // Remove white space
99  str = plot->fPadDescription[i];
100  if (str.find(" ")!=std::string::npos) {
101  str.erase(remove_if(str.begin(), str.end(), isspace));
102  }
103 
104  // Split into plots separated by "+"'s
105  std::vector<std::string> plots;
106  tokenize(str, plots, "+");
107  for (j=0; j<plots.size(); ++j) {
108  Plot plot;
109 
110  // module, object and plot options are separted by "/"
111  std::vector<std::string> fields;
112  tokenize(plots[j], fields, "/");
113 
114  // Get the module, object and plot options
115 
116  if (fields.size()>0) plot.fModule = fields[0];
117  if (fields.size()>1) plot.fObject = fields[1];
118  if (fields.size()>2) {
119  tokenize(fields[2], plot.fOptions, ",");
120  }
121  pad.fPlot.push_back(plot);
122  }
123  pads.push_back(pad);
124  }
125  }
126 
127  //......................................................................
128 
129  void PlotView::Draw(const char* opt)
130  {
131  evdb::Canvas::fCanvas->Clear();
133  if (evt==0) return;
134 
135  evdb::Canvas::fCanvas->cd();
136 
137  // Get the pad descriptions
138  std::vector<PlotPad> pads;
139  this->Parse(pads);
140 
141  // Lay out the main canvas to hold the pads
142  unsigned int nx=1, ny=1;
143  while (nx*ny < pads.size()) {
144  if (nx-ny<2) ++nx;
145  else { --nx; ++ny;}
146  }
147  evdb::Canvas::fCanvas->Divide(nx,ny);
148 
149  //
150  // Loop over the pads and draw the plots
151  //
152  unsigned int i, j, k;
153  for (i=0; i<pads.size(); ++i) {
154  evdb::Canvas::fCanvas->cd(i+1);
155  // nuevdb/EventDisplayBase/RootEnv.cxx sets some huge margins on gStyle. Just
156  // reset our pad to some more sensible margins here.
157  gPad->SetBottomMargin(.1);
158  gPad->SetLeftMargin(.1);
159  gPad->SetTopMargin(.05);
160  gPad->SetRightMargin(.05);
161 
162  const PlotPad& pad = pads[i];
163  for (j=0; j<pad.fPlot.size(); ++j) {
164  const Plot& plot = pad.fPlot[j];
165 
166  // Find the plot object we've been requested to plot...
167  TObject* obj=0;
175 
176  evt->getByLabel(plot.fModule, th1f);
177  evt->getByLabel(plot.fModule, th2f);
178  evt->getByLabel(plot.fModule, th1d);
179  evt->getByLabel(plot.fModule, th2d);
180  evt->getByLabel(plot.fModule, tprofile);
181  evt->getByLabel(plot.fModule, tgraph);
182  evt->getByLabel(plot.fModule, tgrapherrors);
183 
184  obj = find_by_name(plot.fObject,
185  th1f,
186  th2f,
187  th1d,
188  th2d,
189  tprofile,
190  tgraph,
191  tgrapherrors);
192  if (obj==0) {
193  std::cerr << "find_by_name failed to find "
194  << plot.fModule << "/" << plot.fObject
195  << " in event." << std::endl;
196  continue;
197  }
198 
199  TAttLine* l = dynamic_cast<TAttLine*>(obj);
200  TAttMarker* m = dynamic_cast<TAttMarker*>(obj);
201 
202  // Tease out the plotting options
204  if (j>0) options += "same";
205  for (k=0; k<plot.fOptions.size(); ++k) {
206  const std::string& opt = plot.fOptions[k];
207 
208  bool r = false;
209  if (opt=="logx") { gPad->SetLogx(); r=true; }
210  if (opt=="logy") { gPad->SetLogy(); r=true; }
211  if (opt=="logz") { gPad->SetLogz(); r=true; }
212  if (opt=="gridx") { gPad->SetGridx(); r=true; }
213  if (opt=="gridy") { gPad->SetGridy(); r=true; }
214  if (r==false && opt.find("=")!=std::string::npos) {
215  std::vector<std::string> kv;
216  tokenize(opt, kv, "=");
217  if (kv.size()!=2) abort();
218  if (kv[0]=="lstyle") { if (l) l->SetLineStyle(atoi(kv[1].c_str())); r=true; }
219  if (kv[0]=="lwidth") { if (l) l->SetLineWidth(atoi(kv[1].c_str())); r=true; }
220  if (kv[0]=="lcolor") { if (l) l->SetLineColor(atoi(kv[1].c_str())); r=true; }
221  if (kv[0]=="mstyle") { if (m) m->SetMarkerStyle(atoi(kv[1].c_str())); r=true; }
222  if (kv[0]=="msize") { if (m) m->SetMarkerSize(atof(kv[1].c_str())); r=true; }
223  if (kv[0]=="mcolor") { if (m) m->SetMarkerColor(atoi(kv[1].c_str())); r=true; }
224  }
225  if (r==false) { options += ","; options += opt; }
226  } // Loop on options k
227  obj->Draw(options.c_str());
228  } // Loop on plots j
229  } // Loop on pads i
230  evdb::Canvas::fCanvas->Update();
231  }
232 
233 } // end namespace evd
234 ////////////////////////////////////////////////////////////////////////
#define CHECK(V)
Options for which plots to draw on the PlotsView.
const art::Event * GetEvent() const
Definition: EventHolder.cxx:45
Definition: Canvas.py:1
static void tokenize(const std::string &str, std::vector< std::string > &tokens, const std::string &delimiters)
Definition: PlotView.cxx:26
A view showing collections of plots related to this event.
Data for a single plotting pad.
Definition: PlotView.h:24
OStream cerr
Definition: OStream.cxx:7
static TObject * find_by_name(const std::string &n, art::Handle< std::vector< TH1F > > &th1f, art::Handle< std::vector< TH2F > > &th2f, art::Handle< std::vector< TH1D > > &th1d, art::Handle< std::vector< TH2D > > &th2d, art::Handle< std::vector< TProfile > > &tprofile, art::Handle< std::vector< TGraph > > &tgraph, art::Handle< std::vector< TGraphErrors > > &tgrapherrors)
Definition: PlotView.cxx:42
TCanvas * fCanvas
The ROOT drawing canvas.
Definition: Canvas.h:42
Manage all things related to colors for the event display.
Definition: Display3DPad.h:11
Singleton to hold the current art::Event for the event display.
std::string fObject
Definition: PlotView.h:19
Eigen::Matrix< double, Eigen::Dynamic, Eigen::Dynamic >::Index size_type
Definition: typedefs.hpp:11
if(dump)
nova event display
PlotView(TGMainFrame *mf)
Definition: PlotView.cxx:76
static EventHolder * Instance()
Definition: EventHolder.cxx:15
std::vector< Plot > fPlot
Definition: PlotView.h:26
const std::vector< Plot > plots
int evt
const double j
Definition: BetheBloch.cxx:29
std::vector< std::string > fOptions
Definition: PlotView.h:20
void Draw(const char *opt="")
Definition: PlotView.cxx:129
Data for a single plot appearing in the view.
Definition: PlotView.h:16
bool getByLabel(std::string const &label, std::string const &productInstanceName, Handle< PROD > &result) const
Definition: DataViewImpl.h:344
std::string fModule
Definition: PlotView.h:18
TRandom3 r(0)
void Parse(std::vector< PlotPad > &pads)
Definition: PlotView.cxx:88
void plot(std::string label, std::map< std::string, std::map< std::string, Spectrum * >> &plots, bool log)
std::vector< std::string > fPadDescription
What&#39;s in a pad?
enum BeamMode string