Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
hlHisto2.C
Go to the documentation of this file.
1/// \file
2/// \ingroup tutorial_hist
3///
4/// This tutorial demonstrates how the highlight mechanism can be used on an histogram.
5/// A 2D histogram is booked an filled with a random gaussian distribution and
6/// drawn with the "col" option.
7/// Then an highlight method is connected to the histogram. Moving the mouse
8/// on the histogram open a new canvas displaying the two X and Y projections
9/// at the highlighted bin.
10///
11/// \macro_code
12///
13/// \date March 2018
14/// \author Jan Musinsky
15
16TText *info = nullptr;
17
18void Highlight2(TVirtualPad *pad, TObject *obj, Int_t xhb, Int_t yhb)
19{
20 auto h2 = dynamic_cast<TH2F *>(obj);
21 if(!h2) return;
22 auto CanvasProj = (TCanvas *) gROOT->GetListOfCanvases()->FindObject("CanvasProj");
23 if (!h2->IsHighlight()) { // after highlight disabled
24 if (CanvasProj) delete CanvasProj;
25 h2->SetTitle("Disable highlight");
26 return;
27 }
28
29 if (info) info->SetTitle("");
30
31 auto px = h2->ProjectionX("_px", yhb, yhb);
32 auto py = h2->ProjectionY("_py", xhb, xhb);
33 px->SetTitle(TString::Format("ProjectionX of biny[%02d]", yhb));
34 py->SetTitle(TString::Format("ProjectionY of binx[%02d]", xhb));
35
36 if (!CanvasProj) {
37 CanvasProj = new TCanvas("CanvasProj", "CanvasProj", 505, 0, 600, 600);
38 CanvasProj->Divide(1, 2);
39 CanvasProj->cd(1);
40 px->Draw();
41 CanvasProj->cd(2);
42 py->Draw();
43 }
44
45 h2->SetTitle(TString::Format("Highlight bin [%02d, %02d]", xhb, yhb).Data());
46 pad->Modified();
47 pad->Update();
48
49 CanvasProj->GetPad(1)->Modified();
50 CanvasProj->GetPad(2)->Modified();
51 CanvasProj->Update();
52}
53
54void hlHisto2()
55{
56 auto c1 = new TCanvas("Canvas", "Canvas", 0, 0, 500, 500);
57 c1->HighlightConnect("Highlight2(TVirtualPad*,TObject*,Int_t,Int_t)");
58
59 auto h2 = new TH2F("h2", "", 50, -5.0, 5.0, 50, -5.0, 5.0);
60 for (Int_t i = 0; i < 10000; i++) h2->Fill(gRandom->Gaus(), gRandom->Gaus());
61 h2->Draw("col");
62
63 info = new TText(0.0, -4.0, "please move the mouse over the frame");
64 info->SetTextAlign(22);
65 info->SetTextSize(0.04);
66 info->SetTextColor(kRed+1);
67 info->SetBit(kCannotPick);
68 info->Draw();
69 c1->Update();
70
71 h2->SetHighlight();
72}
73
int Int_t
Definition RtypesCore.h:45
@ kRed
Definition Rtypes.h:66
@ kCannotPick
Definition TObject.h:372
#define gROOT
Definition TROOT.h:407
R__EXTERN TRandom * gRandom
Definition TRandom.h:62
virtual void SetTextAlign(Short_t align=11)
Set the text alignment.
Definition TAttText.h:42
virtual void SetTextColor(Color_t tcolor=1)
Set the text color.
Definition TAttText.h:44
virtual void SetTextSize(Float_t tsize=1)
Set the text size.
Definition TAttText.h:47
The Canvas class.
Definition TCanvas.h:23
TObject * FindObject(const char *name) const override
Search object named name in the list of functions.
Definition TH1.cxx:3860
2-D histogram with a float per channel (see TH1 documentation)}
Definition TH2.h:258
virtual void SetTitle(const char *title="")
Set the title of the TNamed.
Definition TNamed.cxx:164
Mother of all ROOT objects.
Definition TObject.h:41
void SetBit(UInt_t f, Bool_t set)
Set or unset the user status bits as specified in f.
Definition TObject.cxx:780
virtual void Draw(Option_t *option="")
Default Draw method for all objects.
Definition TObject.cxx:274
virtual Double_t Gaus(Double_t mean=0, Double_t sigma=1)
Samples a random number from the standard Normal (Gaussian) Distribution with the given mean and sigm...
Definition TRandom.cxx:274
static TString Format(const char *fmt,...)
Static method which formats a string using a printf style format descriptor and return a TString.
Definition TString.cxx:2356
Base class for several text objects.
Definition TText.h:22
TVirtualPad is an abstract base class for the Pad and Canvas classes.
Definition TVirtualPad.h:51
virtual void Modified(Bool_t flag=1)=0
virtual void Update()=0
return c1
Definition legend1.C:41