Logo ROOT   6.30.04
Reference Guide
 All Namespaces Files Pages
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 }