Logo ROOT   6.12/07
Reference Guide
THistDrawable.cxx
Go to the documentation of this file.
1 /// \file THistDrawable.cxx
2 /// \ingroup Hist ROOT7
3 /// \author Axel Naumann <axel@cern.ch>
4 /// \date 2015-09-11
5 /// \warning This is part of the ROOT 7 prototype! It will change without notice. It might trigger earthquakes. Feedback
6 /// is welcome!
7 
8 /*************************************************************************
9  * Copyright (C) 1995-2015, Rene Brun and Fons Rademakers. *
10  * All rights reserved. *
11  * *
12  * For the licensing terms see $ROOTSYS/LICENSE. *
13  * For the list of contributors see $ROOTSYS/README/CREDITS. *
14  *************************************************************************/
15 
16 #include "ROOT/THistDrawable.hxx"
17 
18 #include "ROOT/THistImpl.hxx"
19 
20 #include "TH1.h"
21 #include "TH2.h"
22 #include "TH3.h"
23 #include "TSystem.h" // TSystem::Load
24 
25 #include <cassert>
26 
27 using namespace ROOT::Experimental;
28 using namespace ROOT::Experimental::Internal;
29 
31 {
32  gSystem->Load("libHistPainter");
33 }
34 
35 template <int DIMENSION>
37 {
38  GetPainterPtr() = this;
39 }
40 
41 template <int DIMENSION>
43 {
44  GetPainterPtr() = nullptr;
45 }
46 
47 template <int DIMENSION>
49 {
50  static THistPainterBase<DIMENSION> *painter = nullptr;
51 
52  return painter;
53 }
54 
55 template <int DIMENSION>
57 {
58  // Trigger loading of the painter library within the init guard of the static:
59  static int triggerLibLoad = (LoadHistPainterLibrary(), 0);
60 
61  (void)triggerLibLoad; // unused.
62 
63  return GetPainterPtr();
64 }
65 
69 
71 
73 {
74  // here should be filling of context menu for the given object
75  // for the moment commented out, while TMenuItems is not available in libHist
76 
77  // items.PopulateObjectMenu(GetOldHist(), GetOldHist()->IsA());
78 }
79 
80 // GCC 5 needs to have that outlined - is that a compiler bug?
81 template <int DIMENSIONS>
83 
84 template <int DIMENSIONS>
86 {
87  auto implBase = fHistImpl.Get();
88  if (!implBase) {
89  fOldHist.reset();
90  return false;
91  }
92 
93  std::array<TAxisView, DIMENSIONS> axes;
94  for (int i = 0; i < DIMENSIONS; ++i)
95  axes[i] = implBase->GetAxis(i);
96 
97  TH1 *old = nullptr;
98 
99  // Create a unique name, for what it's worth.
100  std::string histName;
101  {
102  std::stringstream strm;
103  strm << "drawAdaptor" << this;
104  }
105 
106  // Create old histogram; set nbins because TH1::fNcells is not accessible.
107  switch (DIMENSIONS) {
108  case 1: old = new TH1D(histName.c_str(), implBase->GetTitle().c_str(), axes[0].GetNBins() - 2, 0., 1.); break;
109  case 2:
110  old = new TH2D(histName.c_str(), implBase->GetTitle().c_str(), axes[0].GetNBins() - 2, 0., 1.,
111  axes[1].GetNBins() - 2, 0., 1.);
112  break;
113  case 3:
114  old = new TH3D(histName.c_str(), implBase->GetTitle().c_str(), axes[0].GetNBins() - 2, 0., 1.,
115  axes[1].GetNBins() - 2, 0., 1., axes[2].GetNBins() - 2, 0., 1.);
116  break;
117  default:
118  // And anyway, this should really give a missing symbol due to the export
119  // template.
120  R__ERROR_HERE("Hist") << "Drawing of " << DIMENSIONS << " dimensional histograms not supported.";
121  return false;
122  }
123 
124  old->SetDirectory(nullptr);
125 
126  // See TH1::SetBins().
127  std::array<TAxis *, 3> oldAxes{{old->GetXaxis(), old->GetYaxis(), old->GetZaxis()}};
128  for (int i = 0; i < DIMENSIONS; ++i) {
129  oldAxes[i]->SetRange(0, 0);
130  oldAxes[i]->SetTitle(axes[i].GetTitle().c_str());
131  if (axes[i].GetAsEquidistant()) {
132  oldAxes[i]->Set(axes[i].GetNBins() - 2, axes[i].GetFrom(), axes[i].GetTo());
133  } else if (const TAxisIrregular *irr = axes[i].GetAsIrregular()) {
134  oldAxes[i]->Set(axes[i].GetNBins() - 2, &irr->GetBinBorders()[0]);
135  } else {
136  assert(0 && "Logic error; the axis is neither equidistant nor irregular.");
137  }
138  }
139 
140  int nBins = implBase->GetNBins();
141  old->SetBinsLength(nBins);
142  if (implBase->HasBinUncertainty())
143  old->Sumw2();
144 
145  // Set the bin content + uncertainty.
146  for (int binidx = 0; binidx < nBins; ++binidx) {
147  old->SetBinContent(binidx, implBase->GetBinContentAsDouble(binidx));
148  old->SetBinError(binidx, implBase->GetBinUncertainty(binidx));
149  }
150  fOldHist.reset(old);
151  return true;
152 }
153 
154 /// Paint the histogram
155 template <int DIMENSIONS>
157 {
158  Internal::THistPainterBase<DIMENSIONS>::GetPainter()->Paint(*this, fOpts, canv);
159 }
160 
161 namespace ROOT {
162 namespace Experimental {
163 
164 namespace Internal {
165 template class THistPainterBase<1>;
166 template class THistPainterBase<2>;
167 template class THistPainterBase<3>;
168 } // namespace Internal
169 
170 template class THistDrawable<1>;
171 template class THistDrawable<2>;
172 template class THistDrawable<3>;
173 } // namespace Experimental
174 } // namespace ROOT
Namespace for new ROOT classes and functions.
Definition: StringConv.hxx:21
void Paint(Internal::TVirtualCanvasPainter &canv) final
Paint the histogram.
virtual void SetDirectory(TDirectory *dir)
By default when an histogram is created, it is added to the list of histogram objects in the current ...
Definition: TH1.cxx:8194
virtual int Load(const char *module, const char *entry="", Bool_t system=kFALSE)
Load a shared library.
Definition: TSystem.cxx:1829
static THistPainterBase< DIMENSION > * GetPainter()
virtual void SetBinError(Int_t bin, Double_t error)
Set the bin Error Note that this resets the bin eror option to be of Normal Type and for the non-empt...
Definition: TH1.cxx:8461
THist< 3, double, THistStatContent, THistStatUncertainty > TH3D
Definition: THist.hxx:296
virtual void SetRange(Int_t first=0, Int_t last=0)
Set the viewing range for the axis from bin first to last.
Definition: TAxis.cxx:903
R__EXTERN TSystem * gSystem
Definition: TSystem.h:540
void PopulateMenu(TMenuItems &) final
Method can be used to provide menu items for the drawn object.
virtual void SetBinContent(Int_t bin, Double_t content)
Set bin content see convention for numbering bins in TH1::GetBin In case the bin number is greater th...
Definition: TH1.cxx:8477
TAxis * GetYaxis()
Definition: TH1.h:316
THistDrawableBase & operator=(THistDrawableBase &&)
Abstract interface for painting a canvas.
An axis with non-equidistant bins (also known as "variable binning").
Definition: TAxis.hxx:595
The TH1 histogram class.
Definition: TH1.h:56
THist< 2, double, THistStatContent, THistStatUncertainty > TH2D
Definition: THist.hxx:290
TAxis * GetZaxis()
Definition: TH1.h:317
typedef void((*Func_t)())
virtual void SetBinsLength(Int_t=-1)
Definition: TH1.h:368
virtual void Sumw2(Bool_t flag=kTRUE)
Create structure to store sum of squares of weights.
Definition: TH1.cxx:8276
THist< 1, double, THistStatContent, THistStatUncertainty > TH1D
Definition: THist.hxx:284
static THistPainterBase< DIMENSION > *& GetPainterPtr()
#define R__ERROR_HERE(GROUP)
Definition: TLogger.hxx:126
TAxis * GetXaxis()
Get the behaviour adopted by the object about the statoverflows. See EStatOverflows for more informat...
Definition: TH1.h:315