source: trunk/src/ExRootResult.cc@ 20

Last change on this file since 20 was 2, checked in by Pavel Demin, 16 years ago

first commit

File size: 10.9 KB
Line 
1
2/** \class ExRootResult
3 *
4 * Class simplifying work with histograms
5 *
6 * $Date: 2008-06-04 13:57:56 $
7 * $Revision: 1.1 $
8 *
9 *
10 * \author P. Demin - UCL, Louvain-la-Neuve
11 *
12 */
13
14#include "ExRootAnalysis/ExRootResult.h"
15
16#include "ExRootAnalysis/ExRootUtilities.h"
17
18#include "TROOT.h"
19#include "TFile.h"
20#include "TClass.h"
21#include "TStyle.h"
22#include "TCanvas.h"
23#include "TLegend.h"
24#include "TPaveText.h"
25#include "TPaveStats.h"
26#include "TList.h"
27#include "TH2.h"
28#include "THStack.h"
29#include "TProfile.h"
30#include "TObjArray.h"
31#include "TFolder.h"
32
33#include <algorithm>
34#include <iostream>
35
36using namespace std;
37
38const Font_t kExRootFont = 42;
39const Float_t kExRootFontSize = 0.04;
40const Color_t kExRootBackgroundColor = 10;
41
42//------------------------------------------------------------------------------
43
44static void DeleteTObjectPtr(TObject *x)
45{
46 delete x;
47}
48
49//------------------------------------------------------------------------------
50
51ExRootResult::ExRootResult() : fCanvas(0), fFolder(0)
52{
53
54}
55
56//------------------------------------------------------------------------------
57
58ExRootResult::~ExRootResult()
59{
60 for_each(fPool.begin(), fPool.end(), DeleteTObjectPtr);
61
62 if(fCanvas) delete fCanvas;
63}
64
65//------------------------------------------------------------------------------
66
67void ExRootResult::Reset()
68{
69
70}
71
72//------------------------------------------------------------------------------
73
74void ExRootResult::Write(const char *fileName)
75{
76 TObject *object;
77 TDirectory *currentDirectory = gDirectory;
78 TFile *file = new TFile(fileName, "RECREATE");
79 file->cd();
80 map<TObject*, TObjArray*>::iterator it_plots;
81 for(it_plots = fPlots.begin(); it_plots != fPlots.end(); ++it_plots)
82 {
83 object = it_plots->first;
84 object->Write();
85 }
86 currentDirectory->cd();
87 delete file;
88}
89
90//------------------------------------------------------------------------------
91
92void ExRootResult::CreateCanvas()
93{
94 TDirectory *currentDirectory = gDirectory;
95
96 // Graphics style parameters to avoid grey background on figures
97 gStyle->SetCanvasColor(kExRootBackgroundColor);
98 gStyle->SetStatColor(kExRootBackgroundColor);
99 // gStyle->SetTitleColor(kExRootBackgroundColor);
100 gStyle->SetPadColor(kExRootBackgroundColor);
101
102 gStyle->SetPadTopMargin(0.10);
103 gStyle->SetPadRightMargin(0.05);
104 gStyle->SetPadBottomMargin(0.15);
105 gStyle->SetPadLeftMargin(0.15);
106
107 gStyle->SetStatFont(kExRootFont);
108 gStyle->SetStatFontSize(kExRootFontSize);
109
110 gStyle->SetTitleFont(kExRootFont, "");
111 gStyle->SetTitleFont(kExRootFont, "X");
112 gStyle->SetTitleFont(kExRootFont, "Y");
113 gStyle->SetTitleFont(kExRootFont, "Z");
114 gStyle->SetTitleSize(kExRootFontSize, "");
115 gStyle->SetTitleSize(kExRootFontSize, "X");
116 gStyle->SetTitleSize(kExRootFontSize, "Y");
117 gStyle->SetTitleSize(kExRootFontSize, "Z");
118
119 gStyle->SetLabelFont(kExRootFont, "X");
120 gStyle->SetLabelFont(kExRootFont, "Y");
121 gStyle->SetLabelFont(kExRootFont, "Z");
122 gStyle->SetLabelSize(kExRootFontSize, "X");
123 gStyle->SetLabelSize(kExRootFontSize, "Y");
124 gStyle->SetLabelSize(kExRootFontSize, "Z");
125
126 gStyle->SetPadTickX(1);
127 gStyle->SetPadTickY(1);
128
129 gStyle->SetTextFont(kExRootFont);
130 gStyle->SetTextSize(kExRootFontSize);
131
132 gStyle->SetOptStat(111110);
133 // gStyle->SetOptFit(101);
134
135 fCanvas = static_cast<TCanvas*>(gROOT->FindObject("c1"));
136 if(fCanvas)
137 {
138 fCanvas->Clear();
139 fCanvas->UseCurrentStyle();
140 fCanvas->SetWindowSize(800, 650);
141 }
142 else
143 {
144 fCanvas = new TCanvas("c1", "c1", 800, 650);
145 }
146 fCanvas->SetLogy(0);
147 fCanvas->SetHighLightColor(kExRootBackgroundColor);
148
149 currentDirectory->cd();
150}
151
152//------------------------------------------------------------------------------
153
154TCanvas *ExRootResult::GetCanvas()
155{
156 if(!fCanvas) CreateCanvas();
157 return fCanvas;
158}
159
160//------------------------------------------------------------------------------
161
162void ExRootResult::Attach(TObject *plot, TObject *object)
163{
164 if(!plot) return;
165
166 map<TObject*, TObjArray*>::iterator it_plots = fPlots.find(plot);
167 if(it_plots != fPlots.end())
168 {
169 TObjArray *attachment = it_plots->second;
170 if(!attachment)
171 {
172 attachment = new TObjArray();
173 it_plots->second = attachment;
174 }
175 attachment->Add(object);
176 }
177}
178
179
180//------------------------------------------------------------------------------
181
182void ExRootResult::PrintPlot(TObject *plot, const char *sufix, const char *format)
183{
184 if(!plot) return;
185
186 TCanvas *canvas = GetCanvas();
187 TH1 *histogram = 0;
188
189 if(plot->IsA()->InheritsFrom(TH1::Class()))
190 {
191 histogram = static_cast<TH1*>(plot);
192 }
193
194 map<TObject*, PlotSettings>::iterator it_settings = fSettings.find(plot);
195 if(it_settings != fSettings.end())
196 {
197 canvas->SetLogx(it_settings->second.logx);
198 if(histogram == 0 || histogram->Integral() > 0.0)
199 {
200 canvas->SetLogy(it_settings->second.logy);
201 }
202 else
203 {
204 canvas->SetLogy(0);
205 }
206 }
207
208 map<TObject*, TObjArray*>::iterator it_plots = fPlots.find(plot);
209 if(it_plots != fPlots.end())
210 {
211 TObjArray *attachment = it_plots->second;
212 if(attachment)
213 {
214 TIter iterator(attachment);
215 TObject *object;
216 while((object = iterator()))
217 {
218 object->Draw();
219 }
220 }
221 }
222
223 TString name = plot->GetName();
224 canvas->Print(name + sufix + "." + format);
225}
226
227//------------------------------------------------------------------------------
228
229void ExRootResult::Print(const char *format)
230{
231 TObjArray *attachment;
232 TObject *object;
233 TH1 *histogram;
234 TPaveStats *stats;
235 TString name;
236
237 TCanvas *canvas = GetCanvas();
238
239 map<TObject*, TObjArray*>::iterator it_plots;
240 map<TObject*, PlotSettings>::iterator it_settings;
241
242 for(it_plots = fPlots.begin(); it_plots != fPlots.end(); ++it_plots)
243 {
244 object = it_plots->first;
245 attachment = it_plots->second;
246 name = object->GetName();
247 histogram = 0;
248
249 if(object->IsA()->InheritsFrom(TH1::Class()))
250 {
251 histogram = static_cast<TH1*>(object);
252 }
253
254 it_settings = fSettings.find(object);
255 if(it_settings != fSettings.end())
256 {
257 canvas->SetLogx(it_settings->second.logx);
258 if(histogram == 0 || histogram->Integral() > 0.0)
259 {
260 canvas->SetLogy(it_settings->second.logy);
261 }
262 else
263 {
264 canvas->SetLogy(0);
265 }
266 }
267
268 object->Draw();
269 canvas->Update();
270
271 if(histogram)
272 {
273 stats = static_cast<TPaveStats*>(histogram->GetListOfFunctions()->FindObject("stats"));
274 if(stats)
275 {
276 stats->SetX1NDC(0.67);
277 stats->SetX2NDC(0.99);
278 stats->SetY1NDC(0.77);
279 stats->SetY2NDC(0.99);
280 stats->SetTextFont(kExRootFont);
281 stats->SetTextSize(kExRootFontSize);
282 canvas->Draw();
283 }
284 }
285 if(attachment)
286 {
287 TIter iterator(attachment);
288 while((object = iterator()))
289 {
290 object->Draw();
291 }
292 }
293 canvas->Print(name + "." + format);
294 }
295}
296
297//------------------------------------------------------------------------------
298
299TH1 *ExRootResult::AddHist1D(const char *name, const char *title,
300 const char *xlabel, const char *ylabel,
301 Int_t nxbins, Axis_t xmin, Axis_t xmax,
302 Int_t logx, Int_t logy)
303{
304 TH1F *hist = new TH1F(name, title, nxbins, xmin, xmax);
305 PlotSettings settings;
306 settings.logx = logx;
307 settings.logy = logy;
308
309 fPool.insert(hist);
310 hist->GetXaxis()->SetTitle(xlabel);
311 hist->GetYaxis()->SetTitle(ylabel);
312 fPlots[hist] = 0;
313 fSettings[hist] = settings;
314 HistStyle(hist, kFALSE);
315 return hist;
316}
317
318//------------------------------------------------------------------------------
319
320TH1 *ExRootResult::AddHist1D(const char *name, const char *title,
321 const char *xlabel, const char *ylabel,
322 Int_t nxbins, const Float_t *bins,
323 Int_t logx, Int_t logy)
324{
325 TH1F *hist = new TH1F(name, title, nxbins, bins);
326 PlotSettings settings;
327 settings.logx = logx;
328 settings.logy = logy;
329
330 fPool.insert(hist);
331 hist->GetXaxis()->SetTitle(xlabel);
332 hist->GetYaxis()->SetTitle(ylabel);
333 fPlots[hist] = 0;
334 fSettings[hist] = settings;
335 HistStyle(hist, kFALSE);
336 if(fFolder) fFolder->Add(hist);
337 return hist;
338}
339
340//------------------------------------------------------------------------------
341
342TProfile *ExRootResult::AddProfile(const char *name, const char *title,
343 const char *xlabel, const char *ylabel,
344 Int_t nxbins, Axis_t xmin, Axis_t xmax,
345 Int_t logx, Int_t logy)
346{
347 TProfile *profile = new TProfile(name, title, nxbins, xmin, xmax);
348 PlotSettings settings;
349 settings.logx = logx;
350 settings.logy = logy;
351
352 fPool.insert(profile);
353 profile->GetXaxis()->SetTitle(xlabel);
354 profile->GetYaxis()->SetTitle(ylabel);
355 fPlots[profile] = 0;
356 fSettings[profile] = settings;
357 HistStyle(profile, kFALSE);
358 if(fFolder) fFolder->Add(profile);
359 return profile;
360}
361
362//------------------------------------------------------------------------------
363
364TH2 *ExRootResult::AddHist2D(const char *name, const char *title,
365 const char *xlabel, const char *ylabel,
366 Int_t nxbins, Axis_t xmin, Axis_t xmax,
367 Int_t nybins, Axis_t ymin, Axis_t ymax,
368 Int_t logx, Int_t logy)
369{
370 TH2F *hist = new TH2F(name, title, nxbins, xmin, xmax, nybins, ymin, ymax);
371 PlotSettings settings;
372 settings.logx = logx;
373 settings.logy = logy;
374
375 fPool.insert(hist);
376 hist->GetXaxis()->SetTitle(xlabel);
377 hist->GetYaxis()->SetTitle(ylabel);
378 fPlots[hist] = 0;
379 fSettings[hist] = settings;
380 HistStyle(hist, kFALSE);
381 if(fFolder) fFolder->Add(hist);
382 return hist;
383}
384
385//------------------------------------------------------------------------------
386
387THStack *ExRootResult::AddHistStack(const char *name, const char *title)
388{
389 THStack *stack = new THStack(name, title);
390// segmentaion violation when deleting stack in ~ExRootResult()
391// fPool.insert(stack);
392 fPlots[stack] = 0;
393 if(fFolder) fFolder->Add(stack);
394 return stack;
395}
396
397//------------------------------------------------------------------------------
398
399TPaveText *ExRootResult::AddComment(Double_t x1, Double_t y1, Double_t x2, Double_t y2)
400{
401 TPaveText *comment = new TPaveText(x1, y1, x2, y2, "brNDC");
402 fPool.insert(comment);
403 comment->SetTextSize(kExRootFontSize);
404 comment->SetTextFont(kExRootFont);
405 comment->SetTextAlign(22);
406 comment->SetFillColor(kExRootBackgroundColor);
407 comment->SetBorderSize(2);
408 return comment;
409}
410
411//------------------------------------------------------------------------------
412
413TLegend *ExRootResult::AddLegend(Double_t x1, Double_t y1, Double_t x2, Double_t y2)
414{
415 TLegend *legend = new TLegend(x1, y1, x2, y2);
416 fPool.insert(legend);
417 legend->SetTextSize(kExRootFontSize);
418 legend->SetTextFont(kExRootFont);
419 legend->SetFillColor(kExRootBackgroundColor);
420 legend->SetBorderSize(2);
421 return legend;
422}
423
424//------------------------------------------------------------------------------
425
426
Note: See TracBrowser for help on using the repository browser.