Logo ROOT   6.10/09
Reference Guide
ContourList.C
Go to the documentation of this file.
1 /// \file
2 /// \ingroup tutorial_hist
3 /// \notebook
4 /// Getting Contours From TH2D.
5 ///
6 /// #### Image produced by `.x ContourList.C`
7 /// The contours values are drawn next to each contour.
8 /// \macro_image
9 ///
10 /// #### Output produced by `.x ContourList.C`
11 /// It shows that 6 contours and 12 graphs were found.
12 /// \macro_output
13 ///
14 /// #### `ContourList.C`
15 /// \macro_code
16 ///
17 /// \authors Josh de Bever (CSI Medical Physics Group, The University of Western Ontario, London, Ontario, Canada), Olivier Couet
18 
19 Double_t SawTooth(Double_t x, Double_t WaveLen);
20 
21 TCanvas *ContourList(){
22 
23  const Double_t PI = TMath::Pi();
24 
25  TCanvas* c = new TCanvas("c","Contour List",0,0,600,600);
26  c->SetRightMargin(0.15);
27  c->SetTopMargin(0.15);
28 
29  Int_t i, j;
30 
31  Int_t nZsamples = 80;
32  Int_t nPhiSamples = 80;
33 
34  Double_t HofZwavelength = 4.0; // 4 meters
35  Double_t dZ = HofZwavelength/(Double_t)(nZsamples - 1);
36  Double_t dPhi = 2*PI/(Double_t)(nPhiSamples - 1);
37 
38  TArrayD z(nZsamples);
39  TArrayD HofZ(nZsamples);
40  TArrayD phi(nPhiSamples);
41  TArrayD FofPhi(nPhiSamples);
42 
43  // Discretized Z and Phi Values
44  for ( i = 0; i < nZsamples; i++) {
45  z[i] = (i)*dZ - HofZwavelength/2.0;
46  HofZ[i] = SawTooth(z[i], HofZwavelength);
47  }
48 
49  for(Int_t i=0; i < nPhiSamples; i++){
50  phi[i] = (i)*dPhi;
51  FofPhi[i] = sin(phi[i]);
52  }
53 
54  // Create Histogram
55  TH2D *HistStreamFn = new TH2D("HstreamFn",
56  "#splitline{Histogram with negative and positive contents. Six contours are defined.}{It is plotted with options CONT LIST to retrieve the contours points in TGraphs}",
57  nZsamples, z[0], z[nZsamples-1], nPhiSamples, phi[0], phi[nPhiSamples-1]);
58 
59  // Load Histogram Data
60  for (Int_t i = 0; i < nZsamples; i++) {
61  for(Int_t j = 0; j < nPhiSamples; j++){
62  HistStreamFn->SetBinContent(i,j, HofZ[i]*FofPhi[j]);
63  }
64  }
65 
66  gStyle->SetOptStat(0);
67  gStyle->SetTitleW(0.99);
68  gStyle->SetTitleH(0.08);
69 
70  Double_t contours[6];
71  contours[0] = -0.7;
72  contours[1] = -0.5;
73  contours[2] = -0.1;
74  contours[3] = 0.1;
75  contours[4] = 0.4;
76  contours[5] = 0.8;
77 
78  HistStreamFn->SetContour(6, contours);
79 
80  // Draw contours as filled regions, and Save points
81  HistStreamFn->Draw("CONT Z LIST");
82  c->Update(); // Needed to force the plotting and retrieve the contours in TGraphs
83 
84  // Get Contours
85  TObjArray *conts = (TObjArray*)gROOT->GetListOfSpecials()->FindObject("contours");
86  TList* contLevel = NULL;
87  TGraph* curv = NULL;
88  TGraph* gc = NULL;
89 
90  Int_t nGraphs = 0;
91  Int_t TotalConts = 0;
92 
93  if (conts == NULL){
94  printf("*** No Contours Were Extracted!\n");
95  TotalConts = 0;
96  return 0;
97  } else {
98  TotalConts = conts->GetSize();
99  }
100 
101  printf("TotalConts = %d\n", TotalConts);
102 
103  for(i = 0; i < TotalConts; i++){
104  contLevel = (TList*)conts->At(i);
105  printf("Contour %d has %d Graphs\n", i, contLevel->GetSize());
106  nGraphs += contLevel->GetSize();
107  }
108 
109  nGraphs = 0;
110 
111  TCanvas* c1 = new TCanvas("c1","Contour List",610,0,600,600);
112  c1->SetTopMargin(0.15);
113  TH2F *hr = new TH2F("hr",
114  "#splitline{Negative contours are returned first (highest to lowest). Positive contours are returned from}{lowest to highest. On this plot Negative contours are drawn in red and positive contours in blue.}",
115  2, -2, 2, 2, 0, 6.5);
116 
117  hr->Draw();
118  Double_t xval0, yval0, zval0;
119  TLatex l;
120  l.SetTextSize(0.03);
121  char val[20];
122 
123  for(i = 0; i < TotalConts; i++){
124  contLevel = (TList*)conts->At(i);
125  if (i<3) zval0 = contours[2-i];
126  else zval0 = contours[i];
127  printf("Z-Level Passed in as: Z = %f\n", zval0);
128 
129  // Get first graph from list on curves on this level
130  curv = (TGraph*)contLevel->First();
131  for(j = 0; j < contLevel->GetSize(); j++){
132  curv->GetPoint(0, xval0, yval0);
133  if (zval0<0) curv->SetLineColor(kRed);
134  if (zval0>0) curv->SetLineColor(kBlue);
135  nGraphs ++;
136  printf("\tGraph: %d -- %d Elements\n", nGraphs,curv->GetN());
137 
138  // Draw clones of the graphs to avoid deletions in case the 1st
139  // pad is redrawn.
140  gc = (TGraph*)curv->Clone();
141  gc->Draw("C");
142 
143  sprintf(val,"%g",zval0);
144  l.DrawLatex(xval0,yval0,val);
145  curv = (TGraph*)contLevel->After(curv); // Get Next graph
146  }
147  }
148  c1->Update();
149  printf("\n\n\tExtracted %d Contours and %d Graphs \n", TotalConts, nGraphs );
150  gStyle->SetTitleW(0.);
151  gStyle->SetTitleH(0.);
152  return c1;
153 }
154 
155 
156 Double_t SawTooth(Double_t x, Double_t WaveLen){
157 
158 // This function is specific to a sawtooth function with period
159 // WaveLen, symmetric about x = 0, and with amplitude = 1. Each segment
160 // is 1/4 of the wavelength.
161 //
162 // |
163 // /\ |
164 // / \ |
165 // / \ |
166 // / \
167 // /--------\--------/------------
168 // |\ /
169 // | \ /
170 // | \ /
171 // | \/
172 //
173 
174  Double_t y;
175  if ( (x < -WaveLen/2) || (x > WaveLen/2)) y = -99999999; // Error X out of bounds
176  if (x <= -WaveLen/4) {
177  y = x + 2.0;
178  } else if ((x > -WaveLen/4) && (x <= WaveLen/4)) {
179  y = -x ;
180  } else if (( x > WaveLen/4) && (x <= WaveLen/2)) {
181  y = x - 2.0;
182  }
183  return y;
184 }
An array of TObjects.
Definition: TObjArray.h:37
return c1
Definition: legend1.C:41
Definition: Rtypes.h:56
void SetTitleW(Float_t w=0)
Definition: TStyle.h:388
virtual void SetContour(Int_t nlevels, const Double_t *levels=0)
Set the number and values of contour levels.
Definition: TH1.cxx:7607
R__EXTERN TStyle * gStyle
Definition: TStyle.h:402
#define gROOT
Definition: TROOT.h:375
#define PI
int Int_t
Definition: RtypesCore.h:41
virtual void Draw(Option_t *option="")
Default Draw method for all objects.
Definition: TObject.cxx:202
#define NULL
Definition: RtypesCore.h:88
TObject * At(Int_t idx) const
Definition: TObjArray.h:165
virtual void SetTopMargin(Float_t topmargin)
Set Pad top margin in fraction of the pad height.
Definition: TAttPad.cxx:130
Double_t x[n]
Definition: legend1.C:17
To draw Mathematical Formula.
Definition: TLatex.h:18
TLatex * DrawLatex(Double_t x, Double_t y, const char *text)
Make a copy of this object with the new parameters And copy object attributes.
Definition: TLatex.cxx:1918
constexpr Double_t Pi()
Definition: TMath.h:40
double sin(double)
A doubly linked list.
Definition: TList.h:43
virtual void SetLineColor(Color_t lcolor)
Set the line color.
Definition: TAttLine.h:40
virtual Int_t GetPoint(Int_t i, Double_t &x, Double_t &y) const
Get x and y values for point number i.
Definition: TGraph.cxx:1580
virtual TObject * First() const
Return the first object in the list. Returns 0 when list is empty.
Definition: TList.cxx:561
virtual void Draw(Option_t *option="")
Draw this histogram with options.
Definition: TH1.cxx:2851
tomato 2-D histogram with a float per channel (see TH1 documentation)}
Definition: TH2.h:249
Int_t GetN() const
Definition: TGraph.h:122
TLine * l
Definition: textangle.C:4
virtual TObject * After(const TObject *obj) const
Returns the object after object obj.
Definition: TList.cxx:293
void SetTitleH(Float_t h=0)
Definition: TStyle.h:389
The Canvas class.
Definition: TCanvas.h:31
double Double_t
Definition: RtypesCore.h:55
Double_t y[n]
Definition: legend1.C:17
THist< 2, double, THistStatContent, THistStatUncertainty > TH2D
Definition: THist.hxx:316
Array of doubles (64 bits per element).
Definition: TArrayD.h:27
virtual TObject * Clone(const char *newname="") const
Make a clone of an object using the Streamer facility.
Definition: TNamed.cxx:65
virtual void SetRightMargin(Float_t rightmargin)
Set Pad right margin in fraction of the pad width.
Definition: TAttPad.cxx:120
you should not use this method at all Int_t Int_t z
Definition: TRolke.cxx:630
A Graph is a graphics object made of two arrays X and Y with npoints each.
Definition: TGraph.h:41
void SetOptStat(Int_t stat=1)
The type of information printed in the histogram statistics box can be selected via the parameter mod...
Definition: TStyle.cxx:1267
Definition: Rtypes.h:56
virtual void SetBinContent(Int_t bin, Double_t content)
Set bin content.
Definition: TH2.cxx:2471
virtual void SetTextSize(Float_t tsize=1)
Set the text size.
Definition: TAttText.h:46
virtual void Update()
Update canvas pad buffers.
Definition: TCanvas.cxx:2208
virtual Int_t GetSize() const
Definition: TCollection.h:89
THist< 2, float, THistStatContent, THistStatUncertainty > TH2F
Definition: THist.hxx:317
tomato 2-D histogram with a double per channel (see TH1 documentation)}
Definition: TH2.h:290