source: trunk/Mars/hawc/plot_ranger.C@ 19814

Last change on this file since 19814 was 19797, checked in by tbretz, 6 years ago
Examples how to use ranger.
File size: 5.0 KB
Line 
1// ==========================================================================
2// ============ see plot_ranger function at the end of the file =============
3// ==========================================================================
4void plot_resolution()
5{
6 // Get the profile produced by Draw from the current pad
7 TProfile *p = (TProfile*)gPad->FindObject("htemp");
8 if (!p)
9 return;
10
11 // Change errors from error of mean to sqrt(variance) ("spread")
12 p->SetErrorOption("s");
13
14 // Create a new histogram with the errors as entries
15 TH1D *h = p->ProjectionX("ptemp", "C=E");
16
17 // Remove the profile from the pad and from memory
18 delete p;
19
20 // Split the title into x- and y-axis title and set them accordingly
21 TObjArray *arr = TString(h->GetTitle()).Tokenize(":");
22 h->SetXTitle(arr->At(1)->GetName());
23 h->SetYTitle(arr->At(0)->GetName());
24 delete arr;
25
26 // Some additional configuration
27 h->SetDirectory(0);
28 h->SetStats(kFALSE);
29 h->SetMinimum(0);
30
31 // Draw the new histogram into the current pad
32 h->Draw();
33}
34
35// ==========================================================================
36//
37// Run the macro with
38//
39// root hawc/plot_ranger.C
40//
41// The default file name is "ranger.csv.root", To change this use
42//
43// root -b -q hawc/ratescan.C\(\"myfile.root\"\)
44//
45// The defaul ttree name is "Events", To change this use
46//
47// root -b -q hawc/ratescan.C\(\"myfile.root\",\"MyTree\"\)
48//
49// From within root, the escape characters can be omitted, e.g.
50//
51// root
52// [0] .x hawc/ratescan.C("myfile.root","MyTree")
53//
54// ==========================================================================
55
56void plot_ranger(const char *filename="ranger.csv.root", const char *tree="Events")
57{
58 // Crate a chian to read the trees and add the file
59 // (or all files if wildcards are used)
60 TChain c(tree);
61 c.Add(filename);
62
63 // Create the display and set a title
64 MStatusDisplay *d = new MStatusDisplay;
65 d->SetTitle(filename);
66
67 // ------------------------------------------------------------------
68
69 TCanvas &c0 = d->AddTab("Pediction");
70 gPad->SetGrid();
71 c.Draw("Predictions:True", "", "col");
72 c.Draw("Predictions:True", "", "same");
73
74 TGraph *g = (TGraph*)gPad->FindObject("Graph");
75 if (!g)
76 return;
77
78 TF1 equal("equal", "x", -1e100, 1e100);
79 equal.DrawCopy("same");
80
81 // ------------------------------------------------------------------
82
83 TF1 zero("zero", "0", -1e100, 1e100);
84 zero.SetLineStyle(kDashed);
85 zero.SetLineWidth(1);
86 zero.SetLineColor(kBlue);
87
88 TGraph *g[4];
89
90 TCanvas &c1 = d->AddTab("Bias");
91 c1.Divide(2,2);
92
93 c1.cd(1);
94 c.Draw("Predictions-True:True", "", "col");
95 c.Draw("Predictions-True:True", "", "same");
96 zero.DrawCopy("same");
97
98 c1.cd(2);
99 c.Draw("Predictions-True:Predictions", "", "col");
100 c.Draw("Predictions-True:Predictions", "", "same");
101 zero.DrawCopy("same");
102
103 c1.cd(3);
104 c.Draw("(Predictions-True)/True:True", "", "col");
105 c.Draw("(Predictions-True)/True:True", "", "same");
106 zero.DrawCopy("same");
107
108 c1.cd(4);
109 c.Draw("(Predictions-True)/Predictions:Predictions", "", "col");
110 c.Draw("(Predictions-True)/Predictions:Predictions", "", "same");
111 zero.DrawCopy("same");
112
113 // ------------------------------------------------------------------
114
115 TF1 zero("zero", "0", -1e100, 1e100);
116 zero.SetLineStyle(kDashed);
117 zero.SetLineWidth(1);
118 zero.SetLineColor(kBlue);
119
120 TCanvas &c1 = d->AddTab("Resolution");
121 c1.Divide(2,2);
122
123 c1.cd(1);
124 c.Draw("Predictions-True:True", "", "prof");
125 plot_resolution();
126
127 c1.cd(2);
128 c.Draw("Predictions-True:Predictions", "", "prof");
129 plot_resolution();
130
131 c1.cd(3);
132 c.Draw("(Predictions-True)/True:True", "", "prof");
133 plot_resolution();
134
135 c1.cd(4);
136 c.Draw("(Predictions-True)/Predictions:Predictions", "", "prof");
137 plot_resolution();
138
139 // ------------------------------------------------------------------
140
141 TObjArray *leaves = c.GetListOfLeaves();
142
143 TIter Next(leaves);
144 TObject *o=0;
145 while (o=Next())
146 {
147 TString name = o->GetName();
148 if (name=="True" || name=="Predictions")
149 continue;
150
151 TCanvas &cc = d->AddTab(name);
152 cc.Divide(2,2);
153
154 cc.cd(1);
155 c.Draw("Predictions-True:"+name, "", "col");
156 c.Draw("Predictions-True:"+name, "", "same");
157 zero.DrawCopy("same");
158
159 cc.cd(2);
160 c.Draw("(Predictions-True)/Predictions:"+name, "", "col");
161 c.Draw("(Predictions-True)/Predictions:"+name, "", "same");
162 zero.DrawCopy("same");
163
164 cc.cd(3);
165 c.Draw("Predictions-True:"+name, "", "prof");
166 plot_resolution();
167
168 cc.cd(4);
169 c.Draw("(Predictions-True)/Predictions:"+name, "", "prof");
170 plot_resolution();
171 }
172
173 // ------------------------------------------------------------------
174 // To store the display into a root or pdf file do
175
176 //d->SaveAs("display.root");
177 d->SaveAs("display.pdf");
178}
Note: See TracBrowser for help on using the repository browser.