source: HiSusy/trunk/Delphes-3.0.0/external/ExRootAnalysis/ExRootResult.cc @ 1

Last change on this file since 1 was 1, checked in by zerwas, 11 years ago

first import of structure, PYTHIA8 and DELPHES

File size: 11.0 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    if(object->IsA()->InheritsFrom(THStack::Class()))
269    {
270      object->Draw("nostack");
271    }
272    else
273    {
274      object->Draw();
275    }
276    canvas->Update();
277
278    if(histogram)
279    {
280      stats = static_cast<TPaveStats*>(histogram->GetListOfFunctions()->FindObject("stats"));
281      if(stats)
282      {
283        stats->SetX1NDC(0.67);
284        stats->SetX2NDC(0.99);
285        stats->SetY1NDC(0.77);
286        stats->SetY2NDC(0.99);
287        stats->SetTextFont(kExRootFont);
288        stats->SetTextSize(kExRootFontSize);
289        canvas->Draw();
290      }
291    }
292    if(attachment)
293    {
294      TIter iterator(attachment);
295      while((object = iterator()))
296      {
297        object->Draw();
298      }
299    }
300    canvas->Print(name + "." + format);
301  }
302}
303
304//------------------------------------------------------------------------------
305
306TH1 *ExRootResult::AddHist1D(const char *name, const char *title,
307                             const char *xlabel, const char *ylabel,
308                             Int_t nxbins, Axis_t xmin, Axis_t xmax,
309                             Int_t logx, Int_t logy)
310{
311  TH1F *hist = new TH1F(name, title, nxbins, xmin, xmax);
312  PlotSettings settings;
313  settings.logx = logx;
314  settings.logy = logy;
315 
316  fPool.insert(hist);
317  hist->GetXaxis()->SetTitle(xlabel);
318  hist->GetYaxis()->SetTitle(ylabel);
319  fPlots[hist] = 0;
320  fSettings[hist] = settings;
321  HistStyle(hist, kFALSE);
322  return hist;
323}
324
325//------------------------------------------------------------------------------
326
327TH1 *ExRootResult::AddHist1D(const char *name, const char *title,
328                             const char *xlabel, const char *ylabel,
329                             Int_t nxbins, const Float_t *bins,
330                             Int_t logx, Int_t logy)
331{
332  TH1F *hist = new TH1F(name, title, nxbins, bins);
333  PlotSettings settings;
334  settings.logx = logx;
335  settings.logy = logy;
336
337  fPool.insert(hist);
338  hist->GetXaxis()->SetTitle(xlabel);
339  hist->GetYaxis()->SetTitle(ylabel);
340  fPlots[hist] = 0;
341  fSettings[hist] = settings;
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
359  fPool.insert(profile);
360  profile->GetXaxis()->SetTitle(xlabel);
361  profile->GetYaxis()->SetTitle(ylabel);
362  fPlots[profile] = 0;
363  fSettings[profile] = settings;
364  HistStyle(profile, kFALSE);
365  if(fFolder) fFolder->Add(profile);
366  return profile;
367}
368
369//------------------------------------------------------------------------------
370
371TH2 *ExRootResult::AddHist2D(const char *name, const char *title,
372                             const char *xlabel, const char *ylabel,
373                             Int_t nxbins, Axis_t xmin, Axis_t xmax,
374                             Int_t nybins, Axis_t ymin, Axis_t ymax,
375                             Int_t logx, Int_t logy)
376{
377  TH2F *hist = new TH2F(name, title, nxbins, xmin, xmax, nybins, ymin, ymax);
378  PlotSettings settings;
379  settings.logx = logx;
380  settings.logy = logy;
381
382  fPool.insert(hist);
383  hist->GetXaxis()->SetTitle(xlabel);
384  hist->GetYaxis()->SetTitle(ylabel);
385  fPlots[hist] = 0;
386  fSettings[hist] = settings;
387  HistStyle(hist, kFALSE);
388  if(fFolder) fFolder->Add(hist);
389  return hist;
390}
391
392//------------------------------------------------------------------------------
393
394THStack *ExRootResult::AddHistStack(const char *name, const char *title)
395{
396  THStack *stack = new THStack(name, title);
397//  segmentaion violation when deleting stack in ~ExRootResult()
398//  fPool.insert(stack);
399  fPlots[stack] = 0;
400  if(fFolder) fFolder->Add(stack);
401  return stack;
402}
403
404//------------------------------------------------------------------------------
405
406TPaveText *ExRootResult::AddComment(Double_t x1, Double_t y1, Double_t x2, Double_t y2)
407{
408  TPaveText *comment = new TPaveText(x1, y1, x2, y2, "brNDC");
409  fPool.insert(comment);
410  comment->SetTextSize(kExRootFontSize);
411  comment->SetTextFont(kExRootFont);
412  comment->SetTextAlign(22);
413  comment->SetFillColor(kExRootBackgroundColor);
414  comment->SetBorderSize(2);
415  return comment;
416}
417
418//------------------------------------------------------------------------------
419
420TLegend *ExRootResult::AddLegend(Double_t x1, Double_t y1, Double_t x2, Double_t y2)
421{
422  TLegend *legend = new TLegend(x1, y1, x2, y2);
423  fPool.insert(legend);
424  legend->SetTextSize(kExRootFontSize);
425  legend->SetTextFont(kExRootFont);
426  legend->SetFillColor(kExRootBackgroundColor);
427  legend->SetBorderSize(2);
428  return legend;
429}
430
431//------------------------------------------------------------------------------
432
433
Note: See TracBrowser for help on using the repository browser.