Logo ROOT  
Reference Guide
Loading...
Searching...
No Matches
hist056_TPolyMarker_contour.C
Go to the documentation of this file.
1/// \file
2/// \ingroup tutorial_hist
3/// \notebook
4/// Make a contour plot and get the first contour in a TPolyMarker.
5/// This macro generates a color contour plot by selecting entries
6/// from an ntuple file.
7/// The TGraph object corresponding to the first contour line is
8/// accessed and displayed into a separate canvas.
9///
10/// \macro_code
11///
12/// \date January 2017
13/// \author Rene Brun
14
15void hist056_TPolyMarker_contour()
16{
17 TString dir = gROOT->GetTutorialDir();
18 dir.Append("/hsimple.C");
19 dir.ReplaceAll("/./", "/");
20 if (!gInterpreter->IsLoaded(dir.Data()))
21 gInterpreter->LoadMacro(dir.Data());
22 TFile *file = (TFile *)gROOT->ProcessLineFast("hsimple(1)");
23 if (!file)
24 return;
25 TTree *ntuple = (TTree *)file->Get("ntuple");
26
27 TCanvas *c1 = new TCanvas("c1", "Contours", 10, 10, 800, 600);
28 ntuple->Draw("py:px", "px*px+py*py < 20", "contz,list");
29
30 // we must call Update to force the canvas to be painted. When
31 // painting the contour plot, the list of contours is generated
32 // and a reference to it added to the Root list of special objects
33 c1->Update();
34
35 TCanvas *c2 = new TCanvas("c2", "First contour", 100, 100, 800, 600);
36
37 TObjArray *contours = (TObjArray *)gROOT->GetListOfSpecials()->FindObject("contours");
38 if (!contours)
39 return;
40 TList *lcontour1 = (TList *)contours->At(0);
41 if (!lcontour1)
42 return;
43 TGraph *gc1 = (TGraph *)lcontour1->First();
44 if (!gc1)
45 return;
46 if (gc1->GetN() < 10)
47 return;
48 gc1->SetMarkerStyle(21);
49 gc1->Draw("alp");
50
51 // We make a TCutG object with the array obtained from this graph
52 TCutG *cutg = new TCutG("cutg", gc1->GetN(), gc1->GetX(), gc1->GetY());
53
54 // We create a polymarker object with npmax points.
55 const Int_t npmax = 50000;
56 TPolyMarker *pm = new TPolyMarker(npmax);
57 Int_t np = 0;
58 while (1) {
59 Double_t x = -4 + 8 * gRandom->Rndm();
60 Double_t y = -4 + 8 * gRandom->Rndm();
61 if (cutg->IsInside(x, y)) {
62 pm->SetPoint(np, x, y);
63 np++;
64 if (np == npmax)
65 break;
66 }
67 }
68 pm->Draw();
69}
int Int_t
Signed integer 4 bytes (int).
Definition RtypesCore.h:59
double Double_t
Double 8 bytes.
Definition RtypesCore.h:73
#define gInterpreter
#define gROOT
Definition TROOT.h:417
externTRandom * gRandom
Definition TRandom.h:62
virtual void SetMarkerStyle(Style_t mstyle=1)
Set the marker style.
Definition TAttMarker.h:43
The Canvas class.
Definition TCanvas.h:23
Graphical cut class.
Definition TCutG.h:20
TObject * Get(const char *namecycle) override
Return pointer to object identified by namecycle.
A file, usually with extension .root, that stores data and code in the form of serialized objects in ...
Definition TFile.h:130
Double_t * GetY() const
Definition TGraph.h:139
Int_t GetN() const
Definition TGraph.h:131
virtual Int_t IsInside(Double_t x, Double_t y) const
Double_t * GetX() const
Definition TGraph.h:138
void Draw(Option_t *chopt="") override
Default Draw method for all objects.
A doubly linked list.
Definition TList.h:38
TObject * First() const override
Return the first object in the list. Returns 0 when list is empty.
Definition TList.cxx:789
An array of TObjects.
Definition TObjArray.h:31
TObject * At(Int_t idx) const override
Definition TObjArray.h:170
A PolyMarker is defined by an array on N points in a 2-D space.
Definition TPolyMarker.h:31
void Draw(Option_t *option="") override
Draw.
virtual void SetPoint(Int_t point, Double_t x, Double_t y)
Set point number n.
Basic string class.
Definition TString.h:138
const char * Data() const
Definition TString.h:384
TString & ReplaceAll(const TString &s1, const TString &s2)
Definition TString.h:713
TString & Append(const char *cs)
Definition TString.h:581
A TTree represents a columnar dataset.
Definition TTree.h:89
return c1
Definition legend1.C:41
Double_t y[n]
Definition legend1.C:17
Double_t x[n]
Definition legend1.C:17
return c2
Definition legend2.C:14