HistogramBrowser.cxx
Go to the documentation of this file.
2 #include <list>
3 #include <string>
4 #include <iostream>
5 #include "TGPicture.h"
6 #include "TGListTree.h"
7 #include "TGButton.h"
12 using namespace om;
13 
15  unsigned int w,
16  unsigned int h,
17  unsigned int opt) :
18  TGCompositeFrame(p, w, h, opt),
20  fCanvas(0),
21  fListTree(0)
22 {
23  fCanvas = new TGCanvas(this, w, h);
24  TGLayoutHints* hints = new TGLayoutHints(kLHintsExpandX|kLHintsExpandY,
25  2, 2, 2, 2);
26  fListTree = new TGListTree(fCanvas, kHorizontalFrame);
27  this->AddFrame(fCanvas, hints);
28 
29  fListTree->Connect("DoubleClicked(TGListTreeItem*,Int_t)",
30  "om::HistogramBrowser",
31  this,
32  "DoubleClicked(TGListTreeItem*,Int_t)");
33 
34  TGLayoutHints* xx = new TGLayoutHints(kLHintsExpandX);
35  fRefresh = new TGTextButton(this, "Refresh Histogram List");
36  fRefresh->SetToolTipText("Request the current histogram list from the histogram source and refresh the browser.");
37  fRefresh->Connect("Clicked()",
38  "om::HistogramBrowser",
39  this,
40  "HandleRefresh()");
41  this->AddFrame(fRefresh, xx);
42 }
43 
44 //......................................................................
45 
46 //
47 // Populate the browser tree with histograms from the producer source
48 //
50 {
51  HistoSource hs(GUIModel::Instance().Data().fHistogramSource.c_str());
52  const HistoTable& ht = HistoTable::Instance();
53 
54  //
55  // Pull out all the histograms from the producer
56  //
57  std::list<std::string> obj;
58  hs.FindAllMatches("*","*_prev_*",obj);
59  obj.sort();
60 
61  //
62  // Loop over histograms and place them in the correct categories
63  //
64  const TGPicture* pic1d = gClient->GetPicture("h1_t.xpm");
65  const TGPicture* pic2d = gClient->GetPicture("h2_t.xpm");
66  const TGPicture* pic = 0;
67  TGListTreeItem* h;
68  std::list<std::string>::const_iterator itr(obj.begin());
69  std::list<std::string>::const_iterator end(obj.end());
70  for (; itr!=end; ++itr) {
71 
72  // Skip the header object (for now...)
73  if((*itr) == "Header") continue;
74 
75  const HistoData* hd = ht.LookUp(itr->c_str());
76  if (hd==0) {
77  std::cerr << " Histogram " << (*itr) << " not found." << std::endl;
78  continue;
79  }
80  if (hd->fType==kTH1F) pic = pic1d;
81  if (hd->fType==kTH2F) pic = pic2d;
82  if (pic==0) pic = pic1d;
83  for (unsigned int i=0; i<hd->fCategory.size(); ++i) {
84  TGListTreeItem* p = this->FindCategory(hd->fCategory[i].c_str());
85  h = fListTree->AddItem(p, itr->c_str(), pic, pic);
86  if (h) {
87  h->SetTipText(hd->fCaption.c_str());
88  fHistograms[*itr] = h;
89  }
90  }
91  }
92 }
93 
94 //......................................................................
95 
97 {
98  this->ClearBrowser();
99  this->MakeCategories();
100  this->Populate();
101 }
102 
103 //......................................................................
104 
106 {
107  if (fListTree==0) return;
108 
109  //
110  // How to completely delete the browser tree? Try traversing all the
111  // top items and deleting all their children. Then delete all the
112  // top level items
113  //
114  std::vector<TGListTreeItem*> item;
115  TGListTreeItem* i1 = fListTree->GetFirstItem();
116  if (i1!=0) {
117  for (; i1!=0; i1=i1->GetNextSibling()) item.push_back(i1);
118  for (unsigned int i=0; i<item.size(); ++i) {
119  fListTree->DeleteItem(item[i]);
120  }
121  }
122  fCategories.clear();
123  fHistograms.clear();
124 }
125 
126 //......................................................................
127 
128 //
129 // Make all the top-level categories
130 //
132 {
133  //
134  // Load the table of histograms so we can extract the list of
135  // histogram categories
136  //
138  HistoTable& ht = HistoTable::Instance(model.fHistoCSVFile.c_str(),
139  model.fDetector);
140 
141  std::map<std::string,HistoData>::iterator itr (ht.fTable.begin());
142  std::map<std::string,HistoData>::iterator itrEnd(ht.fTable.end());
143  std::list<std::string> cats;
144  for (; itr!=itrEnd; ++itr) {
145  const HistoData& hd = itr->second;
146  for (unsigned int i=0; i<hd.fCategory.size(); ++i) {
147  cats.push_back(hd.fCategory[i]);
148  }
149  }
150 
151  //
152  // Sort the list and make the category. We count on "MakeCategory"
153  // to be smart enough to avoid duplicate categories.
154  //
155  cats.sort();
156  std::list<std::string>::iterator iCat(cats.begin());
157  std::list<std::string>::iterator iCatEnd(cats.end());
158  for (; iCat!=iCatEnd; ++iCat) {
159  this->MakeCategory(iCat->c_str());
160  }
161 }
162 
163 //......................................................................
164 
165 TGListTreeItem* HistogramBrowser::FindCategory(const char* nm)
166 {
167  std::map<std::string, TGListTreeItem*>::iterator itr;
168  itr = fCategories.find(nm);
169  if (itr==fCategories.end()) return 0;
170  return itr->second;
171 }
172 
173 //......................................................................
174 
175 TGListTreeItem* HistogramBrowser::MakeCategory(const char* nm)
176 {
177  TGListTreeItem* lti = 0;
178 
179  //
180  // Test if this category already exists - if it does, we're done
181  //
182  lti = this->FindCategory(nm);
183  if (lti!=0) return lti;
184 
185  // Split the category into parent and child pieces.
186  std::string n(nm);
187  std::string p;
188  std::string c;
189  TGListTreeItem* parent;
190 
191  size_t pos = n.find_last_of("/");
192  if (pos<n.length()) {
193  //
194  // Found a "/". Everything before is parent, everything after is child
195  //
196  c = n.substr(pos+1, n.length());
197  p = n.substr(0, pos);
198  parent = this->FindCategory(p.c_str());
199  if (parent==0) parent = this->MakeCategory(p.c_str());
200  }
201  else {
202  //
203  // No "/" found. Must be a top-level category.
204  //
205  c = n;
206  p = "";
207  parent = 0;
208  }
209  //
210  // Add the item to the tree and category list
211  //
212  lti = fListTree->AddItem(parent, c.c_str());
213  fCategories[n] = lti;
214 
215  return lti;
216 }
217 
218 //......................................................................
219 
220 void HistogramBrowser::DoubleClicked(TGListTreeItem* item, Int_t i)
221 {
222  //
223  // Check if clicked item is an end of a branch. If it is, then it
224  // must be a plot object and not a container.
225  //
226  if (item->GetFirstChild()==0) {
227  std::string nm = item->GetText();
228 
229  //
230  // Wildcarded names are never displayable histograms. Skip those.
231  //
232  bool iswildcard = (nm.find('*')!=nm.npos);
233  if (iswildcard) return;
234 
236  }
237 }
238 
239 //......................................................................
240 
242 {
243  this->BuildTree();
244 }
245 
246 //......................................................................
247 
249  unsigned int which)
250 {
251  if (this->GetLock()==false) return;
252 
253  if ( ((which&kCurrentHistogramID)==0) &&
254  ((which&kHistogramSourceID) ==0) &&
255  ((which&kWatchListUpdateID) ==0) ) {
256  this->ReleaseLock();
257  return;
258  }
259 
260  if ( (which&kHistogramSourceID) || (which&kWatchListUpdateID) ) {
261  this->BuildTree();
262  }
263 
264  //
265  // When the histogram changes, change the state of the browser to
266  // avoid presenting conflicting information to the user
267  //
268  if ( (which&kCurrentHistogramID) ) {
269  //
270  // If there is no current histogram, there is nothing to do
271  //
272  static const std::string empty("");
273  if (empty == d.fCurrentHistogram.Current()) {
274  this->ReleaseLock();
275  return;
276  }
277 
278  //
279  // Try to find the currently selected histogram in the collection
280  // of histograms in the browser. If we fail for some reason
281  // (shouldn't ever happen), bail.
282  //
283  std::map<std::string,TGListTreeItem*>::iterator itr;
284  itr = fHistograms.find(d.fCurrentHistogram.Current());
285  if (itr==fHistograms.end()) {
286  this->ReleaseLock();
287  return;
288  }
289 
290  //
291  // If the current histogram is the one already selected, there is
292  // nothing to do
293  //
294  if (itr->second == fListTree->GetSelected()) {
295  this->ReleaseLock();
296  return;
297  }
298 
299  //
300  // Clear whatever is currently highlighted and try to highlight the new
301  //
302  fListTree->ClearHighlighted();
303  if ( (itr->second->IsOpen()==1) && (itr->second->IsActive()==0) ) {
304  fListTree->HighlightItem(itr->second);
305  }
306  }
307  this->ReleaseLock();
308 }
309 
310 //......................................................................
311 
313 {
314 
315  if (fCanvas) {
316  delete fCanvas;
317  fCanvas = 0;
318  }
319  if (fListTree) {
320  delete fListTree;
321  fListTree = 0;
322  }
323  if (fRefresh) {
324  delete fRefresh;
325  fRefresh = 0;
326  }
327 
328 }
329 
330 ////////////////////////////////////////////////////////////////////////
TGListTreeItem * MakeCategory(const char *nm)
std::string fHistoCSVFile
Definition: GUIModelData.h:40
std::map< std::string, TGListTreeItem * > fCategories
std::vector< std::string > fCategory
Categories this hist. belongs to.
Definition: HistoData.h:41
Double_t xx
Definition: macro.C:12
static const unsigned int kWatchListUpdateID
Definition: GUIModelData.h:26
static const unsigned int kCurrentHistogramID
Definition: GUIModelData.h:15
Detector_t fDetector
Definition: GUIModelData.h:34
const char * p
Definition: xmltok.h:285
static constexpr Double_t nm
Definition: Munits.h:133
OStream cerr
Definition: OStream.cxx:7
std::string fCaption
What does this histogram show?
Definition: HistoData.h:56
void DoubleClicked(TGListTreeItem *, Int_t i)
TGListTreeItem * FindCategory(const char *nm)
TGTextButton * fRefresh
Class to read, hold, and deliver histogram data.
static GUIModel & Instance()
Definition: GUIModel.cxx:11
void GUIModelDataIssue(const GUIModelData &d, unsigned int which)
std::map< std::string, TGListTreeItem * > fHistograms
Float_t d
Definition: plot.C:236
const char * Current() const
static const unsigned int kHistogramSourceID
Definition: GUIModelData.h:22
void SetCurrentHistogram(const char *h)
Definition: GUIModel.cxx:102
CurrentHistogram fCurrentHistogram
Definition: GUIModelData.h:37
Histo_t fType
What kind of histogram is this?
Definition: HistoData.h:46
std::map< std::string, HistoData > fTable
Histogram data by C++ name.
Definition: HistoTable.h:34
HistogramBrowser(const TGWindow *p, unsigned int w, unsigned int h, unsigned int opt)
static HistoTable & Instance(const char *f=0, Detector_t d=kALLDET)
Definition: HistoTable.cxx:21
TH1F * hd
Definition: Xdiff_gwt.C:57
Float_t w
Definition: plot.C:20
const XML_Char XML_Content * model
Definition: expat.h:151
const GUIModelData & Data() const
Definition: GUIModel.h:15
Online Monitoring package header.
enum BeamMode string