MINT2
HyperBinningPainter2D.cpp
Go to the documentation of this file.
2 
8  _binning(binning),
9  _hyperPoints(hyperPoints)
10 {
11  if (getBinning().getDimension() != 2){
12  ERROR_LOG << "You have given a 2D painter a different dimensionality of binning and/or data. I'll probably crash soon";
13  }
14  if (hyperPoints != 0){
15  if(hyperPoints->getDimension() !=2){
16  ERROR_LOG << "You have given a 2D painter a different dimensionality of binning and/or data. I'll probably crash soon";
17  }
18  }
19  WELCOME_LOG << "Good day from the HyperBinningPainter2D() Constructor";
20 }
21 
24  HyperBinningPainter(histogram),
25  _binning(0),
26  _hyperPoints(0)
27 {
28  if (getBinning().getDimension() != 2){
29  ERROR_LOG << "You have given a 2D painter a different dimensionality of binning and/or data. I'll probably crash soon";
30  }
31  WELCOME_LOG << "Good day from the HyperBinningPainter2D() Constructor";
32 }
33 
36 
37  if (_binning != 0) return *_binning;
38  return _histogram->getBinning();
39 
40 }
41 
44  for(int i = 0; i < getBinning().getNumBins(); i++) drawBinEdge(plotter, i);
45 }
46 
49 
50  for (int i = 0; i < getBinning().getBinHyperVolume(bin).size(); i++){
51  HyperCuboid temp (getBinning().getBinHyperVolume(bin).getHyperCuboid(i));
52  drawBinEdge(plotter, &temp);
53  }
54 }
55 
58 
59  TLine* leftLine = new TLine(bin->getLowCorner().at(0) , bin->getLowCorner().at(1), bin->getLowCorner().at(0) , bin->getHighCorner().at(1));
60  TLine* rightLine = new TLine(bin->getHighCorner().at(0), bin->getLowCorner().at(1), bin->getHighCorner().at(0), bin->getHighCorner().at(1));
61  TLine* topLine = new TLine(bin->getLowCorner().at(0) , bin->getHighCorner().at(1), bin->getHighCorner().at(0) , bin->getHighCorner().at(1));
62  TLine* bottomLine = new TLine(bin->getLowCorner().at(0) , bin->getLowCorner().at(1), bin->getHighCorner().at(0) , bin->getLowCorner().at(1));
63 
64  leftLine ->SetLineWidth(1);
65  rightLine ->SetLineWidth(1);
66  topLine ->SetLineWidth(1);
67  bottomLine->SetLineWidth(1);
68 
69  leftLine ->SetLineColor(kBlack);
70  rightLine ->SetLineColor(kBlack);
71  topLine ->SetLineColor(kBlack);
72  bottomLine->SetLineColor(kBlack);
73 
74  //
75  leftLine ->SetLineStyle(3);
76  //rightLine ->SetLineStyle(3);
77  //topLine ->SetLineStyle(3);
78  bottomLine->SetLineStyle(3);
79 
80  plotter->addObject(leftLine);
81  //plotter->addObject(rightLine);
82  //plotter->addObject(topLine);
83  plotter->addObject(bottomLine);
84 
85 }
86 
87 
90 
91  double minBinWidthX = 10e50;
92  double minBinWidthY = 10e50;
93 
94  for(int i = 0; i < getBinning().getNumBins(); i++){
96  double binWidthX = vol.getWidth(0);
97  double binWidthY = vol.getWidth(1);
98  if (binWidthX < minBinWidthX) minBinWidthX = binWidthX;
99  if (binWidthY < minBinWidthY) minBinWidthY = binWidthY;
100  }
101 
102  for(int i = 0; i < getBinning().getNumBins(); i++) drawBinEdge2(plotter, i, minBinWidthX, minBinWidthY);
103 }
104 
106 void HyperBinningPainter2D::drawBinEdge2(RootPlotter2D* plotter, int bin, double minWidX, double minWidY){
107 
108  for (int i = 0; i < getBinning().getBinHyperVolume(bin).size(); i++){
109  HyperCuboid temp (getBinning().getBinHyperVolume(bin).getHyperCuboid(i));
110  drawBinEdge2(plotter, &temp, minWidX, minWidY);
111  }
112 }
113 
115 void HyperBinningPainter2D::drawBinEdge2(RootPlotter2D* plotter, HyperCuboid* bin, double minWidX, double minWidY){
116 
117  drawBinEdge2(plotter, bin, 0 , minWidX, minWidY);
118  drawBinEdge2(plotter, bin, 1 , minWidX, minWidY);
119  drawBinEdge2(plotter, bin, 2 , minWidX, minWidY);
120  drawBinEdge2(plotter, bin, 3 , minWidX, minWidY);
121 
122 }
123 
125 void HyperBinningPainter2D::drawBinEdge2(RootPlotter2D* plotter, HyperCuboid* bin, int edge, double minWidX, double minWidY){
126 
127  double binContent = _histogram->getVal( bin->getCenter() );
128  TLine* line = 0;
129 
130  if (edge == 0) { //left edge
131 
132  HyperPoint cornerLow (bin->getLowCorner().at(0)-minWidX*0.5, bin->getLowCorner ().at(1)+minWidY*0.05);
133  HyperPoint cornerHigh(bin->getLowCorner().at(0)-minWidX*0.5, bin->getHighCorner().at(1)-minWidY*0.05);
134  int binNumLow = getBinning().getBinNum( cornerLow );
135  int binNumHigh = getBinning().getBinNum( cornerHigh );
136 
137  if (binNumLow == -1 ) return;
138  if (binNumHigh == -1 ) return;
139 
140  double binConLow = _histogram->getBinContent(binNumLow );
141  double binConHigh = _histogram->getBinContent(binNumHigh);
142 
143  HyperCuboid binLow = getBinning().getBinHyperVolume(binNumLow ).at(0);
144  HyperCuboid binHigh = getBinning().getBinHyperVolume(binNumHigh).at(0);
145 
146  if (binNumLow == binNumHigh && binContent == binConLow) return;
147  else if (binNumLow == binNumHigh && binContent != binConLow) {
148  line = new TLine( bin->getLowCorner().at(0), bin->getLowCorner ().at(1), bin->getLowCorner().at(0), bin->getHighCorner().at(1) );
149  }
150  else if (binConLow != binContent){
151  line = new TLine( bin->getLowCorner().at(0), bin->getLowCorner ().at(1), bin->getLowCorner().at(0), binLow.getHighCorner().at(1) );
152  }
153  else if (binConHigh != binContent){
154  line = new TLine( bin->getLowCorner().at(0), binHigh.getLowCorner ().at(1), bin->getLowCorner().at(0), bin->getHighCorner().at(1) );
155  }
156 
157  }
158 
159  if (edge == 1) { //right edge
160 
161  HyperPoint cornerLow (bin->getHighCorner().at(0)+minWidX*0.5, bin->getLowCorner ().at(1)+minWidY*0.05);
162  HyperPoint cornerHigh(bin->getHighCorner().at(0)+minWidX*0.5, bin->getHighCorner().at(1)-minWidY*0.05);
163  int binNumLow = getBinning().getBinNum( cornerLow );
164  int binNumHigh = getBinning().getBinNum( cornerHigh );
165 
166  if (binNumLow == -1 ) return;
167  if (binNumHigh == -1 ) return;
168 
169  double binConLow = _histogram->getBinContent(binNumLow );
170  double binConHigh = _histogram->getBinContent(binNumHigh);
171 
172  HyperCuboid binLow = getBinning().getBinHyperVolume(binNumLow ).at(0);
173  HyperCuboid binHigh = getBinning().getBinHyperVolume(binNumHigh).at(0);
174 
175  if (binNumLow == binNumHigh && binContent == binConLow) return;
176  else if (binNumLow == binNumHigh && binContent != binConLow) {
177  line = new TLine( bin->getHighCorner().at(0), bin->getLowCorner ().at(1), bin->getHighCorner().at(0), bin->getHighCorner().at(1) );
178  }
179  else if (binConLow != binContent){
180  line = new TLine( bin->getHighCorner().at(0), bin->getLowCorner ().at(1), bin->getHighCorner().at(0), binLow.getHighCorner().at(1) );
181  }
182  else if (binConHigh != binContent){
183  line = new TLine( bin->getHighCorner().at(0), binHigh.getLowCorner ().at(1), bin->getHighCorner().at(0), bin->getHighCorner().at(1) );
184  }
185 
186  }
187 
188  if (edge == 2) { //top edge
189 
190  HyperPoint cornerLow (bin->getLowCorner ().at(0)+minWidX*0.05, bin->getHighCorner().at(1)+minWidY*0.5);
191  HyperPoint cornerHigh(bin->getHighCorner().at(0)-minWidX*0.05, bin->getHighCorner().at(1)+minWidY*0.5);
192  int binNumLow = getBinning().getBinNum( cornerLow );
193  int binNumHigh = getBinning().getBinNum( cornerHigh );
194 
195  if (binNumLow == -1 ) return;
196  if (binNumHigh == -1 ) return;
197 
198  double binConLow = _histogram->getBinContent(binNumLow );
199  double binConHigh = _histogram->getBinContent(binNumHigh);
200 
201  HyperCuboid binLow = getBinning().getBinHyperVolume(binNumLow ).at(0);
202  HyperCuboid binHigh = getBinning().getBinHyperVolume(binNumHigh).at(0);
203 
204  if (binNumLow == binNumHigh && binContent == binConLow) return;
205  else if (binNumLow == binNumHigh && binContent != binConLow) {
206  line = new TLine( bin->getLowCorner ().at(0), bin->getHighCorner().at(1), bin->getHighCorner().at(0), bin->getHighCorner().at(1) );
207  }
208  else if (binConLow != binContent){
209  line = new TLine( bin->getLowCorner ().at(0), bin->getHighCorner().at(1), binLow.getHighCorner().at(0), bin->getHighCorner().at(1) );
210  }
211  else if (binConHigh != binContent){
212  line = new TLine( binHigh.getLowCorner ().at(0), bin->getHighCorner().at(1), bin->getHighCorner().at(0), bin->getHighCorner().at(1) );
213  }
214 
215  }
216 
217  if (edge == 3) { //bottom edge
218 
219  HyperPoint cornerLow (bin->getLowCorner ().at(0)+minWidX*0.05, bin->getLowCorner().at(1)-minWidY*0.5);
220  HyperPoint cornerHigh(bin->getHighCorner().at(0)-minWidX*0.05, bin->getLowCorner().at(1)-minWidY*0.5);
221  int binNumLow = getBinning().getBinNum( cornerLow );
222  int binNumHigh = getBinning().getBinNum( cornerHigh );
223 
224  if (binNumLow == -1 ) return;
225  if (binNumHigh == -1 ) return;
226  double binConLow = _histogram->getBinContent(binNumLow );
227  double binConHigh = _histogram->getBinContent(binNumHigh);
228 
229  HyperCuboid binLow = getBinning().getBinHyperVolume(binNumLow ).at(0);
230  HyperCuboid binHigh = getBinning().getBinHyperVolume(binNumHigh).at(0);
231 
232  if (binNumLow == binNumHigh && binContent == binConLow) return;
233  else if (binNumLow == binNumHigh && binContent != binConLow) {
234  line = new TLine( bin->getLowCorner ().at(0), bin->getLowCorner().at(1), bin->getHighCorner().at(0), bin->getLowCorner().at(1) );
235  }
236  else if (binConLow != binContent){
237  line = new TLine( bin->getLowCorner ().at(0), bin->getLowCorner().at(1), binLow.getHighCorner().at(0), bin->getLowCorner().at(1) );
238  }
239  else if (binConHigh != binContent){
240  line = new TLine( binHigh.getLowCorner ().at(0), bin->getLowCorner().at(1), bin->getHighCorner().at(0), bin->getLowCorner().at(1) );
241  }
242 
243  }
244 
245  if (line == 0) return;
246 
247  line ->SetLineWidth(1);
248  line ->SetLineColor(kBlack);
249  line ->SetLineStyle(1);
250 
251  plotter->addObject(line);
252 
253 
254 }
255 
256 
259 int HyperBinningPainter2D::getFillColour(double binContents){
260  double min = _histogram->getMin();
261  double max = _histogram->getMax();
262  if (_density == true){
263  min = _histogram->getMinDensity();
264  max = _histogram->getMaxDensity();
265  }
266 
267  if (binContents < min || binContents > max) return kWhite;
268 
269  int ndivz = gStyle->GetNumberContours();
270  double dz = max - min;
271  double scale = ndivz/dz;
272  int ncolors = gStyle->GetNumberOfColors();
273 
274  int color = int(0.01+(binContents-min)*scale);
275  int theColor = int((color+0.99)*double(ncolors)/double(ndivz));
276  if (theColor > ncolors-1) theColor = ncolors-1;
277 
278  return gStyle->GetColorPalette(theColor);
279 }
280 
283  for(int i = 0; i < getBinning().getNumBins(); i++) drawBinNumbers(plotter, i);
284 }
285 
286 
290  TString label = ""; label += bin;
291  plotter->addText( label ,center.at(0), center.at(1), 2, 2, 0.02, 0);
292 
293 }
294 
297  for(int i = 0; i < getBinning().getNumBins(); i++) drawBinCont(plotter, i);
298 }
299 
300 
304  double binCont = _histogram->getBinContent(bin );
305  TString label = ""; label += binCont;
306  plotter->addText( label ,center.at(0), center.at(1), 2, 2, 0.02, 0);
307 
308 }
309 
310 
313  for(int i = 0; i < getBinning().getNumBins(); i++) drawFilledBin(plotter, i, hashNeg);
314 }
315 
317 void HyperBinningPainter2D::drawFilledBin(RootPlotter2D* plotter, int bin, bool hashNeg){
318 
319  double binContent = _histogram->getBinContent(bin);
320  if (_density == true ) binContent = _histogram->getFrequencyDensity(bin);
321 
322  bool neg = false;
323  if (hashNeg == true && binContent < 0.0) {
324  binContent = -binContent;
325  neg = true;
326  }
327 
328  for (int i = 0; i < getBinning().getBinHyperVolume(bin).size(); i++){
329  HyperCuboid temp (getBinning().getBinHyperVolume(bin).getHyperCuboid(i));
330  drawFilledBin(plotter, &temp, binContent);
331  if (neg){
332  drawFilledBin(plotter, &temp, 1, 3344);
333  }
334  }
335 
336 }
337 
338 
340 void HyperBinningPainter2D::drawFilledBin(RootPlotter2D* plotter, HyperCuboid* bin, double binContents){
341 
342  int fillColor = getFillColour(binContents);
343  int fillStyle = 1001;
344 
345  drawFilledBin(plotter, bin, fillColor, fillStyle);
346 
347 }
348 
350 void HyperBinningPainter2D::drawFilledBin(RootPlotter2D* plotter, HyperCuboid* bin, int fillColor, int fillStyle){
351 
352  TBox* box = new TBox(bin->getLowCorner().at(0) , bin->getLowCorner().at(1), bin->getHighCorner().at(0) , bin->getHighCorner().at(1));
353  box->SetFillColor( fillColor );
354  box->SetLineWidth(0.0);
355  box->SetFillStyle( fillStyle );
356  box->SetLineColor( fillColor );
357  plotter->addObject(box);
358 
359 }
360 
361 
364  for(unsigned int i = 0; i < _hyperPoints->size(); i++){
365  histogram->Fill(_hyperPoints->at(i).at(0), _hyperPoints->at(i).at(1));
366  }
367 }
368 
370 void HyperBinningPainter2D::draw(TString path, TString option){
371 
372  bool drawBinEd1 = option.Contains("Edges1" );
373  bool drawBinEd2 = option.Contains("Edges2" );
374  bool drawBinNums = option.Contains("BinNums");
375  bool drawHashedNeg = option.Contains("HashNeg");
376  bool drawBinContOp = option.Contains("Text");
377 
378  double x_min = getBinning().getMin(0);
379  double x_max = getBinning().getMax(0);
380  double y_min = getBinning().getMin(1);
381  double y_max = getBinning().getMax(1);
382 
383  TString xtitle = _histogram->getNames().getAxisString(0);
384  TString ytitle = _histogram->getNames().getAxisString(1);
385 
386  TH2D* histogram = new TH2D("2DHyperBinningPlot", "2DHyperBinningPlot", 100, x_min, x_max, 100, y_min, y_max);
387  histogram->GetXaxis()->SetTitle( xtitle );
388  histogram->GetYaxis()->SetTitle( ytitle );
389 
390  double min = _histogram->getMin();
391  double max = _histogram->getMax();
392  if (_density == true){
393  min = _histogram->getMinDensity();
394  max = _histogram->getMaxDensity();
395  }
396 
397  if (drawHashedNeg){
398  min = min<0.0?0.0:min;
399  max = fabs(max)>fabs(min)?fabs(max):fabs(min);
400  }
401 
402  if (_histogram != 0){
403  //if you want the scale to be correct on the zaxis, we need to fill the
404  //histogram with events between min-max (annoying)
405  for(int i = 1; i <= 100; i++) for (int j = 0; j <= 100; ++j) histogram->SetBinContent(i, j, max);
406  histogram->SetBinContent(2, 2, min);
407  histogram->SetMaximum(max);
408  histogram->SetMinimum(min);
409  }
410 
411  histogram->SetContour(gStyle->GetNumberContours());
412  //std::cout << "Am setting number of contours to " << gStyle->GetNumberContours() << std::endl;
413  RootPlotter2D plotter(histogram);
414 
415  if (_hyperPoints != 0) addHyperPoints(histogram);
416  if (_binning != 0) drawBinEdges (&plotter);
417  if (_histogram != 0) {
418  TBox* box = new TBox(getBinning().getMin(0) , getBinning().getMin(1), getBinning().getMax(0) , getBinning().getMax(1));
419  box->SetFillColor( 0 );
420  box->SetLineWidth(0.0);
421  plotter.addObject(box) ;
422  drawFilledBins(&plotter, drawHashedNeg);
423  if (drawBinEd1 ) drawBinEdges (&plotter);
424  if (drawBinEd2 ) drawBinEdges2 (&plotter);
425  if (drawBinNums ) drawBinNumbers(&plotter);
426  if (drawBinContOp ) drawBinCont (&plotter);
427 
428  }
429 
430  plotter.plot(path, "COLZ");
431 
432  delete histogram;
433 }
434 
437 
438 }
double getMax(int dimension) const
Definition: BinningBase.cpp:46
double getMaxDensity() const
void addObject(TObject *obj)
Definition: Plotter.cpp:168
TString getAxisString(int dim)
Definition: HyperName.cpp:96
double getMin(int dimension) const
Definition: BinningBase.cpp:42
const HyperCuboid & at(int i) const
Definition: HyperVolume.h:55
int size() const
Definition: HyperVolume.h:60
#define ERROR_LOG
double getBinContent(int bin) const
double getMin() const
void drawBinEdges2(RootPlotter2D *plotter)
HyperBinningPainter2D(BinningBase *binning, HyperPointSet *hyperPoints=0)
virtual int getNumBins() const =0
void drawFilledBin(RootPlotter2D *plotter, int bin, bool hashNeg=false)
HyperCuboid getLimits() const
void drawBinNumbers(RootPlotter2D *plotter)
double getFrequencyDensity(int bin) const
void addHyperPoints(TH2D *histogram)
const HyperPoint & at(int i) const
virtual int getBinNum(const HyperPoint &coords) const =0
HyperPoint getCenter() const
Definition: HyperCuboid.cpp:93
double getMax() const
const HyperPoint & getHighCorner() const
Definition: HyperCuboid.h:89
void drawBinEdge(RootPlotter2D *plotter, int bin)
virtual void draw(TString path="", TString option="")
virtual void plot(TString plotDirectory, TString plotOptions="", TPad *pad=0, double scaleFactor=1.0)
HyperPoint getWidth() const
HyperHistogram * _histogram
const double & at(int i) const
Definition: HyperPoint.cpp:433
void drawBinEdges(RootPlotter2D *plotter)
const int & getDimension() const
Definition: HyperPointSet.h:40
virtual double getVal(const HyperPoint &point) const
void drawFilledBins(RootPlotter2D *plotter, bool hashNeg=false)
const HyperPoint & getLowCorner() const
Definition: HyperCuboid.h:87
unsigned int size() const
#define WELCOME_LOG
void addText(TString text, double x, double y, int alignh=1, int alignv=2, double size=0.06, int ndc=true, int color=kBlack)
Definition: RootPlotter.cpp:33
const BinningBase & getBinning() const
void drawBinCont(RootPlotter2D *plotter)
HyperName getNames() const
HyperPoint getAverageCenter() const
const BinningBase & getBinning()
void drawBinEdge2(RootPlotter2D *plotter, int bin, double minWidX, double minWidY)
int getFillColour(double binContents)
double getMinDensity() const
virtual HyperVolume getBinHyperVolume(int binNumber) const =0