ROOT  6.07/01
Reference Guide
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
ContourList.C
Go to the documentation of this file.
1 /// \file
2 /// \ingroup tutorial_hist
3 /// Getting Contours From TH2D.
4 ///
5 /// #### Image produced by `.x ContourList.C`
6 /// The contours values are drawn next to each contour.
7 /// \macro_image
8 ///
9 /// #### Output produced by `.x ContourList.C`
10 /// It shows that 6 contours and 12 graphs were found.
11 /// \macro_output
12 ///
13 /// #### `ContourList.C`
14 /// \macro_code
15 ///
16 /// \authors Josh de Bever (CSI Medical Physics Group, The University of Western Ontario, London, Ontario, Canada), Olivier Couet
17 
18 Double_t SawTooth(Double_t x, Double_t WaveLen);
19 
20 TCanvas *ContourList(){
21 
22  const Double_t PI = TMath::Pi();
23 
24  TCanvas* c = new TCanvas("c","Contour List",0,0,600,600);
25  c->SetRightMargin(0.15);
26  c->SetTopMargin(0.15);
27 
28  Int_t i, j;
29 
30  Int_t nZsamples = 80;
31  Int_t nPhiSamples = 80;
32 
33  Double_t HofZwavelength = 4.0; // 4 meters
34  Double_t dZ = HofZwavelength/(Double_t)(nZsamples - 1);
35  Double_t dPhi = 2*PI/(Double_t)(nPhiSamples - 1);
36 
37  TArrayD z(nZsamples);
38  TArrayD HofZ(nZsamples);
39  TArrayD phi(nPhiSamples);
40  TArrayD FofPhi(nPhiSamples);
41 
42  // Discretized Z and Phi Values
43  for ( i = 0; i < nZsamples; i++) {
44  z[i] = (i)*dZ - HofZwavelength/2.0;
45  HofZ[i] = SawTooth(z[i], HofZwavelength);
46  }
47 
48  for(Int_t i=0; i < nPhiSamples; i++){
49  phi[i] = (i)*dPhi;
50  FofPhi[i] = sin(phi[i]);
51  }
52 
53  // Create Histogram
54  TH2D *HistStreamFn = new TH2D("HstreamFn",
55  "#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}",
56  nZsamples, z[0], z[nZsamples-1], nPhiSamples, phi[0], phi[nPhiSamples-1]);
57 
58  // Load Histogram Data
59  for (Int_t i = 0; i < nZsamples; i++) {
60  for(Int_t j = 0; j < nPhiSamples; j++){
61  HistStreamFn->SetBinContent(i,j, HofZ[i]*FofPhi[j]);
62  }
63  }
64 
65  gStyle->SetPalette(1);
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 x0, y0, z0;
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) z0 = contours[2-i];
126  else z0 = contours[i];
127  printf("Z-Level Passed in as: Z = %f\n", z0);
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, x0, y0);
133  if (z0<0) curv->SetLineColor(kRed);
134  if (z0>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",z0);
144  l.DrawLatex(x0,y0,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:39
TCanvas * c1
Definition: legend1.C:2
Definition: Rtypes.h:61
void SetTitleW(Float_t w=0)
Definition: TStyle.h:409
virtual void SetContour(Int_t nlevels, const Double_t *levels=0)
Set the number and values of contour levels.
Definition: TH1.cxx:7863
R__EXTERN TStyle * gStyle
Definition: TStyle.h:423
#define gROOT
Definition: TROOT.h:344
int Int_t
Definition: RtypesCore.h:41
virtual void Draw(Option_t *option="")
Default Draw method for all objects.
Definition: TObject.cxx:254
Int_t GetN() const
Definition: TGraph.h:132
virtual void SetTopMargin(Float_t topmargin)
Set Pad top margin in fraction of the pad height.
Definition: TAttPad.cxx:127
Double_t x[n]
Definition: legend1.C:17
virtual TObject * After(const TObject *obj) const
Returns the object after object obj.
Definition: TList.cxx:289
To draw Mathematical Formula.
Definition: TLatex.h:33
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:1901
virtual TObject * Clone(const char *newname="") const
Make a clone of an object using the Streamer facility.
Definition: TNamed.cxx:63
double sin(double)
Float_t z[5]
Definition: Ifit.C:16
A doubly linked list.
Definition: TList.h:47
virtual void SetLineColor(Color_t lcolor)
Definition: TAttLine.h:54
virtual void Draw(Option_t *option="")
Draw this histogram with options.
Definition: TH1.cxx:2878
2-D histogram with a float per channel (see TH1 documentation)}
Definition: TH2.h:256
TLine * l
Definition: textangle.C:4
void SetTitleH(Float_t h=0)
Definition: TStyle.h:410
Double_t Pi()
Definition: TMath.h:44
Float_t phi
Definition: shapesAnim.C:6
The Canvas class.
Definition: TCanvas.h:48
virtual Int_t GetSize() const
Definition: TCollection.h:95
double Double_t
Definition: RtypesCore.h:55
ClassImp(TMCParticle) void TMCParticle printf(": p=(%7.3f,%7.3f,%9.3f) ;", fPx, fPy, fPz)
Double_t y[n]
Definition: legend1.C:17
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:1551
Array of doubles (64 bits per element).
Definition: TArrayD.h:29
virtual void SetRightMargin(Float_t rightmargin)
Set Pad right margin in fraction of the pad width.
Definition: TAttPad.cxx:117
virtual TObject * First() const
Return the first object in the list. Returns 0 when list is empty.
Definition: TList.cxx:557
A Graph is a graphics object made of two arrays X and Y with npoints each.
Definition: TGraph.h:53
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:1252
#define NULL
Definition: Rtypes.h:82
Definition: Rtypes.h:61
virtual void SetBinContent(Int_t bin, Double_t content)
Set bin content.
Definition: TH2.cxx:2699
virtual void SetTextSize(Float_t tsize=1)
Definition: TAttText.h:60
TObject * At(Int_t idx) const
Definition: TObjArray.h:167
virtual void Update()
Update canvas pad buffers.
Definition: TCanvas.cxx:2179
void SetPalette(Int_t ncolors=kBird, Int_t *colors=0, Float_t alpha=1.)
See TColor::SetPalette.
Definition: TStyle.cxx:1445
2-D histogram with a double per channel (see TH1 documentation)}
Definition: TH2.h:297