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