1 |
/*************************************************
|
2 |
Drawing plots directly fron TTree objects can
|
3 |
give you unexpected axis endpoints and
|
4 |
binning. So I wrote this to make histograms
|
5 |
that will be easier to compare.
|
6 |
|
7 |
In current directory,
|
8 |
rootlogon.C must contain, for example:
|
9 |
|
10 |
G__loadfile("/afs/hep.wisc.edu/home/mbanderson/ROOT_Macros/PlotFromTTree.C");
|
11 |
TFile *file1 = new TFile("aFile.root");
|
12 |
TCanvas *c1 = new TCanvas("c1","testCanvas",640,480);
|
13 |
TH1F *h1 = new TH1F("h1","Blah",20,-5,5);
|
14 |
|
15 |
|
16 |
|
17 |
Example commands typed
|
18 |
at ROOT prompt:
|
19 |
PlotFromTTree("TreeRec", "recPhtn.phi", "", -4.0, 4.0, 25)
|
20 |
PlotFromTTree("TreeRec","abs(recPhtn.phi-recJet.phi)","",2.0,4.0,50,"#Delta#phi")
|
21 |
|
22 |
with this format:
|
23 |
PlotFromTTree("Tree Name","What To Plot","Cuts to Make", xMin, xMax, bins, "x-axis title", "y-axis title")
|
24 |
|
25 |
|
26 |
Example to draw two things
|
27 |
onto same plot:
|
28 |
|
29 |
root [0] PlotFromTTree("TreePhotonAndJet","recJet2.pt","",0,300,100)
|
30 |
root [1] PlotFromTTree("TreePhotonAndJet","recJet1.pt","",0,300,100,"","","same")
|
31 |
|
32 |
OR
|
33 |
|
34 |
root [0] PlotFromTTree("TreePhotonAndJet","recJet1.pt","recJet2.pt","","",0,300,100)
|
35 |
|
36 |
Michael B. Anderson
|
37 |
May 27, 2008
|
38 |
*************************************************/
|
39 |
|
40 |
void PlotFromTTree( TString treeName,
|
41 |
TString plotVar,
|
42 |
TString cuts,
|
43 |
Double_t xMin,
|
44 |
Double_t xMax,
|
45 |
int numOfBins,
|
46 |
TString XaxisTitle = "",
|
47 |
TString YaxisTitle = "",
|
48 |
TString drawOption = "")
|
49 |
{
|
50 |
// *******************************************
|
51 |
// Variables
|
52 |
|
53 |
// cutNames will be put into the image filename
|
54 |
// so certain characters must be removed
|
55 |
TString cutNames = filenameFilter(cuts);
|
56 |
|
57 |
// ASSUMES file1 is opened in the rootlogon.C
|
58 |
TTree* treeAny = file1->Get(treeName); // Grab the ntuple from the ROOT file
|
59 |
// see if the TTree was found
|
60 |
if ( !treeAny ) {
|
61 |
cout << "Error: " << file1->GetName() << " does not contain TTree named " << treeName << endl;
|
62 |
return;
|
63 |
}
|
64 |
|
65 |
// This will be the filename for the output image
|
66 |
gSystem->MakeDirectory("Images");
|
67 |
TString outputName("Images/"+treeName+"-"+plotVar+cutNames+".gif");
|
68 |
|
69 |
Int_t LogY = 0; // Y-axis, No log = 0, Log = 1
|
70 |
Double_t YaxisTitleOffset = 1.4; // Set to 1.4 if y-axis title is overlapping with labels
|
71 |
// 0 otherwise
|
72 |
|
73 |
// End of Variables
|
74 |
// *******************************************
|
75 |
|
76 |
|
77 |
// Created in rootlogon.C
|
78 |
TString histName = h1->GetName(); // Grab the name of the histogram
|
79 |
TString histName2 = h2->GetName();
|
80 |
|
81 |
if (drawOption.CompareTo("same") == 0 ) {
|
82 |
h2->SetBins(numOfBins,xMin,xMax); // Set the Number of Bins, Xmin, and Xmax
|
83 |
treeAny->Draw(plotVar+" >> "+histName2,cuts,"goff"); // Draw to it
|
84 |
h2->SetLineColor(kRed); // Set color for line.
|
85 |
h2->Draw("same"); // Draw the histogram.
|
86 |
} else {
|
87 |
|
88 |
h1->SetBins(numOfBins,xMin,xMax); // Set the Number of Bins, Xmin, and Xmax
|
89 |
h1->SetNameTitle(histName,treeName+"-"+plotVar); // Set the Name and Title
|
90 |
|
91 |
treeAny->Draw(plotVar+" >> "+histName,cuts); // Draw to it
|
92 |
|
93 |
//h1->GetYaxis()->SetRangeUser(ymin,ymax); // Rescale Y-Axis.
|
94 |
|
95 |
h1->SetLineColor(kBlue); // Set color for line.
|
96 |
//h1->SetStats(kFALSE); // Turn the stats box off.
|
97 |
|
98 |
h1->GetXaxis()->SetTitle(XaxisTitle); // Give titles to the
|
99 |
h1->GetYaxis()->SetTitle(YaxisTitle); // X and Y axis.
|
100 |
|
101 |
h1->Draw(); // Draw the histogram.
|
102 |
|
103 |
}
|
104 |
h1->GetYaxis()->SetTitleOffset(YaxisTitleOffset);
|
105 |
|
106 |
c1->SetLogy(LogY); // Set the axis to a log or not-log scale
|
107 |
c1->Print(outputName); // Save the histogram to a file
|
108 |
}
|
109 |
|
110 |
|
111 |
// Plot 2 things onto the same plot
|
112 |
void PlotFromTTree( TString treeName,
|
113 |
TString plotVar1,
|
114 |
TString plotVar2,
|
115 |
TString cuts1="",
|
116 |
TString cuts2="",
|
117 |
Double_t xMin = 0,
|
118 |
Double_t xMax = 0,
|
119 |
int numOfBins = 1,
|
120 |
TString XaxisTitle = "",
|
121 |
TString YaxisTitle = "")
|
122 |
{
|
123 |
// *******************************************
|
124 |
// Variables
|
125 |
|
126 |
// cutNames will be put into the image filename
|
127 |
// so certain characters must be removed
|
128 |
TString cutNames1 = filenameFilter(cuts1);
|
129 |
TString cutNames2 = filenameFilter(cuts2);
|
130 |
|
131 |
// ASSUMES file1 is opened in the rootlogon.C
|
132 |
TTree* treeAny = file1->Get(treeName); // Grab the ntuple from the ROOT file
|
133 |
// see if the TTree was found
|
134 |
if ( !treeAny ) {
|
135 |
cout << "Error: " << file1->GetName() << " does not contain TTree named " << treeName << endl;
|
136 |
return;
|
137 |
}
|
138 |
|
139 |
// This will be the filename for the output image
|
140 |
gSystem->MakeDirectory("Images");
|
141 |
TString outputName("Images/"+treeName+"-"+plotVar1+"-"+plotVar2+cutNames1+cutNames2+".gif");
|
142 |
|
143 |
Int_t LogY = 0; // Y-axis, No log = 0, Log = 1
|
144 |
Double_t YaxisTitleOffset = 1.4; // Set to 1.4 if y-axis title is overlapping with labels
|
145 |
// 0 otherwise
|
146 |
|
147 |
// End of Variables
|
148 |
// *******************************************
|
149 |
|
150 |
|
151 |
|
152 |
if (xMin == xMax) {
|
153 |
echo << xMin << " " << xMax << endl;
|
154 |
// User did not give us a value, so determine one
|
155 |
|
156 |
// Draw to a temp histogram to see default bins and xlo and xhi
|
157 |
treeAny->Draw(plotVar1+" >> htemp",cuts1,"goff"); // Draw to it
|
158 |
|
159 |
numOfBins = htemp->GetNbinsX();
|
160 |
xMin = htemp->GetXaxis()->GetXmin();
|
161 |
XMax = htemp->GetXaxis()->GetXmax();
|
162 |
|
163 |
htemp->Delete();
|
164 |
}
|
165 |
|
166 |
// Set the bin information
|
167 |
h1->SetBins(numOfBins,xMin,xMax); // Set the Number of Bins, Xmin, and Xmax
|
168 |
TString histName = h1->GetName(); // Grab the name of the histogram
|
169 |
h1->SetNameTitle(histName,treeName); // Set the Name and Title
|
170 |
|
171 |
// Draw variable 1 to histogram 1
|
172 |
treeAny->Draw(plotVar1+" >> "+histName,cuts1,"goff"); // Draw to it
|
173 |
|
174 |
// Draw variable 2 to histogram 2
|
175 |
h2->SetBins(numOfBins,xMin,xMax);
|
176 |
|
177 |
h2->SetLineColor(kBlue);
|
178 |
treeAny->Draw(plotVar2+" >> h2",cuts2,"goff"); // Draw to it
|
179 |
|
180 |
// Set the color
|
181 |
h1->SetLineColor(kRed);
|
182 |
h2->SetLineColor(kBlue);
|
183 |
|
184 |
// Get rid of Stats box
|
185 |
h1->SetStats(kFALSE);
|
186 |
|
187 |
h1->GetXaxis()->SetTitle(XaxisTitle); // Give titles to the
|
188 |
h1->GetYaxis()->SetTitle(YaxisTitle); // X and Y axis.
|
189 |
|
190 |
h1->Draw();
|
191 |
h2->Draw("same");
|
192 |
|
193 |
// ****************
|
194 |
// Draw Info Label
|
195 |
TString hist1Label = plotVar1;
|
196 |
TString hist2Label = plotVar2;
|
197 |
TLegend *infoBox = new TLegend(0.74, 0.74, 0.98, 0.98, "");
|
198 |
if ( (hist1Label=="") || (hist2Label=="") ) {
|
199 |
infoBox->AddEntry(h1,hist1Name,"L");
|
200 |
infoBox->AddEntry(h2,hist2Name,"L");
|
201 |
} else {
|
202 |
// If the plot variables were the same,
|
203 |
// then the user is probably comparing before & after a cut
|
204 |
if ( hist1Label.CompareTo(hist2Label) == 0 ) {
|
205 |
hist1Label = "before";
|
206 |
hist2Label = "after";
|
207 |
h1->SetNameTitle(histName,"New cut(s) = "+cuts2); // Set the Name and Title
|
208 |
cout << h1->GetEntries() << " entries before with cuts = " << cuts1 << endl;
|
209 |
cout << h2->GetEntries() << " entries after with cuts = " << cuts2 << endl;
|
210 |
}
|
211 |
infoBox->AddEntry(h1,hist1Label,"L");
|
212 |
infoBox->AddEntry(h2,hist2Label,"L");
|
213 |
}
|
214 |
infoBox->Draw();
|
215 |
// ****************
|
216 |
|
217 |
c1->Print(outputName); // Save the histogram to a file
|
218 |
}
|
219 |
|
220 |
|
221 |
// This is for 2D Plots
|
222 |
void PlotFromTTree( TString treeName,
|
223 |
TString plotVarX,
|
224 |
TString plotVarY,
|
225 |
TString cuts,
|
226 |
Double_t xMin,
|
227 |
Double_t xMax,
|
228 |
Double_t yMin,
|
229 |
Double_t yMax,
|
230 |
int numOfBinsX,
|
231 |
int numOfBinsY,
|
232 |
TString XaxisTitle = "",
|
233 |
TString YaxisTitle = "")
|
234 |
{
|
235 |
// *******************************************
|
236 |
// Variables
|
237 |
|
238 |
// cutNames will be put into the image filename
|
239 |
// so certain characters must be removed
|
240 |
TString cutNames = filenameFilter(cuts);
|
241 |
|
242 |
// ASSUMES file1 is opened in the rootlogon.C
|
243 |
TTree* treeAny = file1->Get(treeName); // Grab the ntuple from the ROOT file
|
244 |
// see if the TTree was found
|
245 |
if ( !treeAny ) {
|
246 |
cout << "Error: " << file1->GetName() << " does not contain TTree named " << treeName << endl;
|
247 |
return;
|
248 |
}
|
249 |
|
250 |
// This will be the filename for the output image
|
251 |
gSystem->MakeDirectory("Images");
|
252 |
TString outputName("Images/"+treeName+"-"+plotVarX+"-"+plotVarY+cutNames+".gif");
|
253 |
|
254 |
Double_t YaxisTitleOffset = 1.4; // Set to 1.4 if y-axis title is overlapping with labels
|
255 |
// 0 otherwise
|
256 |
|
257 |
// End of Variables
|
258 |
// *******************************************
|
259 |
|
260 |
h2d->SetBins(numOfBinsX,xMin,xMax,numOfBinsY,yMin,yMax); // Set the Number of Bins, Xmin, and Xmax
|
261 |
TString histName = h2d->GetName(); // Grab the name of the histogram
|
262 |
h2d->SetNameTitle(histName,treeName+"-"+plotVarX+"-VS-"+plotVarY); // Set the Name and Title
|
263 |
|
264 |
h2d->SetOption("BOX");
|
265 |
treeAny->Draw(plotVarY+":"+plotVarX+" >> "+histName,cuts); // Draw to it
|
266 |
|
267 |
|
268 |
h2d->SetLineColor(kBlue); // Set color for line.
|
269 |
//h2d->SetStats(kFALSE); // Turn the stats box off.
|
270 |
|
271 |
h2d->GetXaxis()->SetTitle(XaxisTitle); // Give titles to the
|
272 |
h2d->GetYaxis()->SetTitle(YaxisTitle); // X and Y axis.
|
273 |
|
274 |
h2d->Draw(); // Draw the histogram.
|
275 |
h2d->GetYaxis()->SetTitleOffset(YaxisTitleOffset);
|
276 |
|
277 |
c1->Print(outputName); // Save the histogram to a file
|
278 |
}
|
279 |
|
280 |
|
281 |
TString filenameFilter(TString originalString) {
|
282 |
|
283 |
if (originalString == "" ) return originalString;
|
284 |
|
285 |
TString newString = originalString;
|
286 |
newString.ReplaceAll("-","S"); // Subtraction
|
287 |
newString.ReplaceAll("||","-"); // OR
|
288 |
newString.ReplaceAll("&&","-"); // AND
|
289 |
newString.ReplaceAll("<","LT"); // Less Than
|
290 |
newString.ReplaceAll(">","GT"); // Greater Than
|
291 |
|
292 |
newString = "-"+newString;
|
293 |
|
294 |
return newString;
|
295 |
}
|