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