ViewVC Help
View File | Revision Log | Show Annotations | Root Listing
root/cvsroot/UserCode/YiChenHelperCode/PlotHelper.h
Revision: 1.1
Committed: Sun Aug 22 12:10:22 2010 UTC (14 years, 8 months ago) by chenyi
Content type: text/plain
Branch: MAIN
CVS Tags: HEAD
Log Message:
First version of various helper files

File Contents

# Content
1 #ifndef PlotHelper6125
2 #define PlotHelper6125
3
4 // Plot helper, version 0 - 6125 version
5 // Intended to help organizing plots and to avoid cluttering
6 // The main idea is to write a plot both to a pdf and to a TFile at the same time,
7 // so that we can check quickly using pdf and come back to TFile later if needed
8 // the helper functions here will be mostly on *.ps files
9 // Developer: Yi Chen, 6125
10
11 #include <iostream>
12 #include <string>
13 #include <vector>
14 #include <ctime>
15 using namespace std;
16
17 #include "TCanvas.h"
18 #include "TLatex.h"
19 #include "TROOT.h"
20 #include "TFile.h"
21 #include "TH1.h"
22 #include "TGraph.h"
23 #include "TVirtualPad.h"
24
25 class PsFileHelper
26 {
27 private:
28 string FileName;
29 string Option;
30 bool Status; // false - file not opened; true - file good and ongoing
31 public:
32 PsFileHelper();
33 PsFileHelper(string filename);
34 PsFileHelper(string filename, string option);
35 ~PsFileHelper();
36 void Open(string filename);
37 void Close();
38 string GetFileName();
39 void SetOption(string option);
40 string GetOption();
41 template <class PlotType> void AddPlot(PlotType *Histogram, string PlotOption = "",
42 bool LogY = false, bool LogZ = false, bool Grid = false);
43 template <class PlotType> void Add4PanelPlot(PlotType *Histogram1, PlotType *Histogram2,
44 PlotType *Histogram3, PlotType *Histogram4, string PlotOption = "",
45 bool LogY = false, bool LogZ = false, bool Grid = false);
46 template <class PlotType> void AddPlot(PlotType &Histogram, string PlotOption = "",
47 bool LogY = false, bool LogZ = false, bool Grid = false);
48 template <class PlotType> void Add4PanelPlot(PlotType &Histogram1, PlotType &Histogram2,
49 PlotType &Histogram3, PlotType &Histogram4, string PlotOption = "",
50 bool LogY = false, bool LogZ = false, bool Grid = false);
51 template <class PlotType> void AddNormalizedPlot(PlotType *Histogram, string PlotOption = "",
52 bool LogY = false, bool LogZ = false, bool Grid = false);
53 template <class PlotType> void AddNormalizedPlot(PlotType &Histogram, string PlotOption = "",
54 bool LogY = false, bool LogZ = false, bool Grid = false);
55 template <class PlotType> void AddPlotWithText(PlotType *Histogram, string Text,
56 string PlotOption = "", double X = 0.1, double Y = 0.9, double TextSize = 0.03);
57 template <class PlotType> void AddPlotWithText(PlotType &Histogram, string Text,
58 string PlotOption = "", double X = 0.1, double Y = 0.9, double TextSize = 0.03);
59 void AddHistogramFromFile(TFile &File, string HistogramName,
60 string PlotOption = "", bool LogY = false, bool LogZ = false, bool Grid = false);
61 void Add4PanelHistogramFromFile(TFile &File, string HistogramName1, string HistogramName2,
62 string HistogramName3, string HistogramName4,
63 string PlotOption = "", bool LogY = false, bool LogZ = false, bool Grid = false);
64 void AddGraphFromFile(TFile &File, string GraphName,
65 string PlotOption = "", bool LogY = false, bool LogZ = false, bool Grid = false);
66 void AddCanvas(TCanvas *Canvas);
67 void AddCanvas(TCanvas &Canvas);
68 void AddCanvasWithText(TCanvas *Canvas, string Text, double X = 0.1, double Y = 0.9, double TextSize = 0.03);
69 void AddCanvasWithText(TCanvas &Canvas, string Text, double X = 0.1, double Y = 0.9, double TextSize = 0.03);
70 void AddTextPage(string Text, double X = 0.15, double Y = 0.5, double TextSize = 0.05);
71 void AddTextPage(vector<string> Text, double X = 0.1, double Y = 0.9, double TextSize = 0.04);
72 void AddTimeStampPage();
73 };
74
75 PsFileHelper::PsFileHelper()
76 {
77 Status = false;
78 FileName = "";
79 Option = "Landscape";
80 }
81
82 PsFileHelper::PsFileHelper(string filename)
83 {
84 Option = "Landscape";
85
86 Open(filename);
87 }
88
89 PsFileHelper::PsFileHelper(string filename, string option)
90 {
91 Option = option;
92
93 Open(filename);
94 }
95
96 PsFileHelper::~PsFileHelper()
97 {
98 if(Status == true)
99 Close();
100 }
101
102 void PsFileHelper::Open(string filename)
103 {
104 FileName = filename;
105
106 Status = true;
107
108 TCanvas canvas;
109 canvas.Print((FileName + "[").c_str(), Option.c_str());
110 }
111
112 void PsFileHelper::Close()
113 {
114 TCanvas canvas;
115 canvas.Print((FileName + "]").c_str(), Option.c_str());
116
117 gROOT->ProcessLine((".! ps2pdf " + FileName).c_str());
118 gROOT->ProcessLine((".! rm " + FileName).c_str());
119
120 Status = false;
121 FileName = "";
122 }
123
124 string PsFileHelper::GetFileName()
125 {
126 return FileName;
127 }
128
129 void PsFileHelper::SetOption(string option)
130 {
131 Option = option;
132 }
133
134 string PsFileHelper::GetOption()
135 {
136 return Option;
137 }
138
139 template <class PlotType> void PsFileHelper::AddPlot(PlotType *Histogram, string PlotOption, bool LogY, bool LogZ, bool Grid)
140 {
141 if(Histogram == NULL)
142 return;
143
144 TCanvas canvas;
145
146 Histogram->Draw(PlotOption.c_str());
147
148 if(LogY == true)
149 canvas.SetLogy();
150 if(LogZ == true)
151 canvas.SetLogz();
152
153 if(Grid == true)
154 {
155 canvas.SetGridx();
156 canvas.SetGridy();
157 }
158
159 AddCanvas(canvas);
160 }
161
162 template <class PlotType> void PsFileHelper::Add4PanelPlot(PlotType *Histogram1, PlotType *Histogram2,
163 PlotType *Histogram3, PlotType *Histogram4, string PlotOption, bool LogY, bool LogZ, bool Grid)
164 {
165 TCanvas canvas;
166
167 canvas.Divide(2, 2);
168
169 PlotType *Histograms[4] = {Histogram1, Histogram2, Histogram3, Histogram4};
170
171 for(int i = 0; i < 4; i++)
172 {
173 if(Histograms[i] == NULL)
174 continue;
175
176 TVirtualPad *Pad = canvas.cd(i + 1);
177
178 Histograms[i]->Draw(PlotOption.c_str());
179
180 if(LogY == true)
181 Pad->SetLogy();
182 if(LogZ == true)
183 Pad->SetLogz();
184
185 if(Grid == true)
186 {
187 Pad->SetGridx();
188 Pad->SetGridy();
189 }
190 }
191
192 AddCanvas(canvas);
193 }
194
195 template <class PlotType> void PsFileHelper::AddPlot(PlotType &Histogram, string PlotOption, bool LogY, bool LogZ, bool Grid)
196 {
197 AddPlot(&Histogram, PlotOption, LogY, LogZ, Grid);
198 }
199
200 template <class PlotType> void Add4PanelPlot(PlotType &Histogram1, PlotType &Histogram2,
201 PlotType &Histogram3, PlotType &Histogram4, string PlotOption, bool LogY, bool LogZ, bool Grid)
202 {
203 Add4PanelPlot(&Histogram1, &Histogram2, &Histogram3, &Histogram4, PlotOption, LogY, LogZ, Grid);
204 }
205
206 template <class PlotType> void PsFileHelper::AddNormalizedPlot(PlotType *Histogram, string PlotOption,
207 bool LogY, bool LogZ, bool Grid)
208 {
209 if(Histogram == NULL)
210 return;
211
212 TCanvas canvas;
213
214 Histogram->DrawNormalized(PlotOption.c_str());
215
216 if(Histogram->GetMaximum() > 1e-10)
217 {
218 if(LogY == true)
219 canvas.SetLogy();
220 if(LogZ == true)
221 canvas.SetLogz();
222 }
223 if(Grid == true)
224 {
225 canvas.SetGridx();
226 canvas.SetGridy();
227 }
228
229 AddCanvas(canvas);
230 }
231
232 template <class PlotType> void PsFileHelper::AddNormalizedPlot(PlotType &Histogram, string PlotOption,
233 bool LogY, bool LogZ, bool Grid)
234 {
235 AddNormalizedPlot(&Histogram, PlotOption, LogY, LogZ, Grid);
236 }
237
238 template <class PlotType> void PsFileHelper::AddPlotWithText(PlotType *Histogram, string Text,
239 string PlotOption, double X, double Y, double TextSize)
240 {
241 TCanvas canvas;
242
243 Histogram->Draw(PlotOption.c_str());
244
245 AddCanvasWithText(canvas, Text, X, Y, TextSize);
246 }
247
248 template <class PlotType> void PsFileHelper::AddPlotWithText(PlotType &Histogram, string Text,
249 string PlotOption, double X, double Y, double TextSize)
250 {
251 AddPlotWithText(&Histogram, Text, PlotOption, X, Y, TextSize);
252 }
253
254 void PsFileHelper::AddHistogramFromFile(TFile &File, string HistogramName, string PlotOption, bool LogY, bool LogZ, bool Grid)
255 {
256 TH1 *Histogram = (TH1 *)File.Get(HistogramName.c_str());
257 if(Histogram == NULL)
258 return;
259
260 AddPlot(Histogram, PlotOption, LogY, LogZ, Grid);
261 }
262
263 void PsFileHelper::Add4PanelHistogramFromFile(TFile &File, string HistogramName1, string HistogramName2,
264 string HistogramName3, string HistogramName4, string PlotOption, bool LogY, bool LogZ, bool Grid)
265 {
266 TH1 *Histograms[4];
267
268 Histograms[0] = (TH1 *)File.Get(HistogramName1.c_str());
269 Histograms[1] = (TH1 *)File.Get(HistogramName2.c_str());
270 Histograms[2] = (TH1 *)File.Get(HistogramName3.c_str());
271 Histograms[3] = (TH1 *)File.Get(HistogramName4.c_str());
272
273 Add4PanelPlot(Histograms[0], Histograms[1], Histograms[2], Histograms[3], PlotOption, LogY, LogZ, Grid);
274 }
275
276 void PsFileHelper::AddGraphFromFile(TFile &File, string GraphName, string PlotOption, bool LogY, bool LogZ, bool Grid)
277 {
278 TGraph *Graph = (TGraph *)File.Get(GraphName.c_str());
279 if(Graph == NULL)
280 return;
281
282 AddPlot(Graph, PlotOption, LogY, LogZ, Grid);
283 }
284
285 void PsFileHelper::AddCanvas(TCanvas *Canvas)
286 {
287 if(Canvas == NULL)
288 return;
289
290 Canvas->Print(FileName.c_str(), Option.c_str());
291 }
292
293 void PsFileHelper::AddCanvas(TCanvas &Canvas)
294 {
295 AddCanvas(&Canvas);
296 }
297
298 void PsFileHelper::AddCanvasWithText(TCanvas *Canvas, string Text, double X, double Y, double TextSize)
299 {
300 Canvas->cd();
301
302 TLatex text(X, Y, Text.c_str());
303 text.SetTextSize(TextSize);
304 text.Draw();
305
306 Canvas->Print(FileName.c_str(), Option.c_str());
307 }
308
309 void PsFileHelper::AddCanvasWithText(TCanvas &Canvas, string Text, double X, double Y, double TextSize)
310 {
311 AddCanvasWithText(&Canvas, Text, X, Y, TextSize);
312 }
313
314 void PsFileHelper::AddTextPage(string Text, double X, double Y, double TextSize)
315 {
316 TCanvas canvas;
317
318 TLatex text(X, Y, Text.c_str());
319 text.SetTextSize(TextSize);
320 text.Draw();
321
322 canvas.Print(FileName.c_str(), Option.c_str());
323 }
324
325 void PsFileHelper::AddTextPage(vector<string> Text, double X, double Y, double TextSize)
326 {
327 TCanvas canvas;
328
329 vector<TLatex> texts;
330
331 for(unsigned int i = 0; i < Text.size(); i++)
332 {
333 texts.push_back(TLatex(X, Y - i * 0.07, Text[i].c_str()));
334 texts[i].SetName(Form("TextLine%d", i));
335 texts[i].SetTextSize(TextSize);
336 }
337
338 for(unsigned int i = 0; i < Text.size(); i++)
339 texts[i].Draw();
340
341 canvas.Print(FileName.c_str(), Option.c_str());
342 }
343
344 void PsFileHelper::AddTimeStampPage()
345 {
346 time_t CurrentTime = time(NULL);
347
348 string str = "Generated at ";
349 str = str + ctime(&CurrentTime);
350
351 AddTextPage(str, 0.03);
352 }
353
354 #endif
355
356