Fork me on GitHub

source: git/external/ExRootAnalysis/ExRootResult.cc@ 87ae892

ImprovedOutputFile Timing dual_readout llp
Last change on this file since 87ae892 was cab38f6, checked in by Pavel Demin <pavel.demin@…>, 10 years ago

remove svn tags and fix formatting

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