File indexing completed on 2024-04-06 12:32:01
0001
0002
0003
0004
0005
0006
0007 #include <sstream>
0008 #include <iomanip>
0009
0010 #if !defined(__CINT__) || defined(__MAKECINT__)
0011 #include "TProfile.h"
0012 #include "TLegend.h"
0013 #include "TROOT.h"
0014 #include "TVirtualPad.h"
0015 #include "TLine.h"
0016 #include "TCanvas.h"
0017 #include "TPostScript.h"
0018 #include "TH1.h"
0019 #include "TH2.h"
0020 #include "TF1.h"
0021 #include "TAxis.h"
0022 #include "TMath.h"
0023 #include "TROOT.h"
0024 #include "TStyle.h"
0025
0026 #include <iostream>
0027
0028 using namespace std;
0029
0030 #endif
0031
0032
0033
0034 TString getPitchString(TH1 *histo, int prec = 5);
0035
0036
0037 TLegend * getLegend(float x1=0.48, float y1=0.81, float x2=0.98, float y2=0.995);
0038 void setStyle(TH1 *histo);
0039 void setStyle(TH2 *histo);
0040
0041
0042 void setStyle(TH1 *histo) {
0043 histo->GetXaxis()->SetTitleFont(gStyle->GetTitleFont());
0044 histo->GetXaxis()->SetTitleSize(gStyle->GetTitleFontSize());
0045 histo->GetXaxis()->SetLabelFont(gStyle->GetLabelFont());
0046 histo->GetXaxis()->SetLabelSize(gStyle->GetLabelSize());
0047
0048 histo->GetYaxis()->SetTitleFont(gStyle->GetTitleFont());
0049 histo->GetYaxis()->SetTitleSize(gStyle->GetTitleFontSize());
0050 histo->GetYaxis()->SetLabelFont(gStyle->GetLabelFont());
0051 histo->GetYaxis()->SetLabelSize(gStyle->GetLabelSize());
0052 }
0053
0054 void setStyle(TH2 *histo) {
0055 histo->GetXaxis()->SetTitleFont(gStyle->GetTitleFont());
0056 histo->GetXaxis()->SetTitleSize(gStyle->GetTitleFontSize());
0057 histo->GetXaxis()->SetLabelFont(gStyle->GetLabelFont());
0058 histo->GetXaxis()->SetLabelSize(gStyle->GetLabelSize());
0059
0060 histo->GetYaxis()->SetTitleFont(gStyle->GetTitleFont());
0061 histo->GetYaxis()->SetTitleSize(gStyle->GetTitleFontSize());
0062 histo->GetYaxis()->SetLabelFont(gStyle->GetLabelFont());
0063 histo->GetYaxis()->SetLabelSize(gStyle->GetLabelSize());
0064 }
0065
0066
0067 bool addProfile=false;
0068 bool addSlice=true;
0069 bool addMedian = false;
0070 TString opt2Dplot = "col";
0071
0072
0073
0074
0075 TH1F* plotAndProfileX (TH2* theh, int rebinX, int rebinY, int rebinProfile, float minY, float maxY, float minX=0, float maxX=0) {
0076 TH2* h2=(TH2*)theh->Clone();
0077
0078
0079 if (h2==0) {
0080 cout << "plotAndProfileX: null histo ptr" << endl;
0081 return 0;
0082 }
0083
0084 gPad->SetGrid(1,1);
0085 gStyle->SetGridColor(15);
0086 h2->Rebin2D(rebinX,rebinY);
0087
0088
0089 TLine * l = new TLine(h2->GetXaxis()->GetXmin(),0,h2->GetXaxis()->GetXmax(),0);
0090 if (maxX>minX) {
0091 h2->GetXaxis()->SetRangeUser(minX,maxX);
0092 l->SetX1(minX);
0093 l->SetX2(maxX);
0094 }
0095
0096 h2->SetMarkerStyle(1);
0097 h2->Draw(opt2Dplot);
0098 l->SetLineColor(3);
0099 l->SetLineWidth(2);
0100 l->Draw();
0101 if (addProfile) {
0102 TAxis* yaxis = h2->GetYaxis();
0103
0104 TProfile* prof = h2->ProfileX("_pfx",
0105 TMath::Max(1,yaxis->FindBin(minY)),
0106 TMath::Min(yaxis->GetNbins(),yaxis->FindBin(maxY)));
0107
0108
0109
0110 prof->SetMarkerColor(2);
0111 prof->SetMarkerStyle(20);
0112 prof->SetMarkerSize(0.4);
0113 prof->SetLineColor(2);
0114 prof->Rebin(rebinProfile);
0115 prof->Draw("same");
0116 }
0117
0118 TH1F* ht=0;
0119
0120 if (addSlice) {
0121 TObjArray aSlices;
0122
0123 h2->FitSlicesY(0, 0, -1, 0, "QNR", &aSlices);
0124
0125 TH1F* ht = (TH1F*) aSlices[1]->Clone();
0126
0127 float thr = (maxY-minY)/4.;
0128 for (int bin=1; bin<=ht->GetNbinsX();++bin){
0129 if (ht->GetBinError(bin)>thr) {
0130 ht->SetBinContent(bin,0);
0131 ht->SetBinError(bin,0);
0132 }
0133 }
0134 ht->SetMarkerColor(4);
0135 ht->Draw("same");
0136 }
0137
0138 if (addMedian) {
0139 double xq[1] = {0.5};
0140 double median[1];
0141
0142 TAxis* axis = h2->GetXaxis();
0143 TH1F* medprof = new TH1F(h2->GetName()+TString("medians"),"medians", axis->GetNbins(), axis->GetXmin(), axis->GetXmax());
0144 float bw = h2->GetYaxis()->GetBinLowEdge(2)-h2->GetYaxis()->GetBinLowEdge(1);
0145
0146
0147 TString projname = h2->GetName()+TString("_pmedian");
0148 for (int bin=1; bin<=h2->GetNbinsX(); ++bin){
0149 TH1D * proj = h2->ProjectionY(projname, bin, bin);
0150 double integral = proj->Integral();
0151 if (integral==0) continue;
0152
0153 int nbins = proj->GetNbinsX();
0154 proj->SetBinContent(1, proj->GetBinContent(0)+proj->GetBinContent(1));
0155 proj->SetBinContent(0,0);
0156 proj->SetBinContent(nbins, proj->GetBinContent(nbins)+proj->GetBinContent(nbins+1));
0157 proj->SetBinContent(nbins+1,0);
0158 proj->GetQuantiles(1,median,xq);
0159 medprof->SetBinContent(bin,median[0]);
0160
0161 medprof->SetBinError(bin,bw*sqrt(integral/2.)/2./TMath::Max(1.,proj->GetBinContent(proj->FindBin(median[0]))));
0162 }
0163 medprof->SetMarkerColor(2);
0164 medprof->SetMarkerStyle(20);
0165 medprof->SetMarkerSize(0.4);
0166 medprof->Draw("Esame");
0167 }
0168
0169 h2->GetYaxis()->SetRangeUser(minY,maxY);
0170
0171 return ht;
0172 }
0173
0174
0175
0176
0177
0178
0179
0180
0181
0182
0183
0184
0185
0186
0187
0188
0189
0190
0191
0192
0193
0194
0195 void plotAndProfileXSpread (TH2* h2, float min, float max, bool profile=false, float ymin=-5., float ymax=5.) {
0196 setStyle(h2);
0197 gPad->SetGrid(1,1);
0198 gStyle->SetGridColor(15);
0199 gStyle->SetOptStat(0);
0200
0201
0202
0203
0204 h2->SetMarkerColor(2);
0205 h2->SetLineColor(2);
0206 h2->GetYaxis()->SetTitleOffset(1.4);
0207 h2->GetXaxis()->SetRangeUser(min,max);
0208 h2->GetYaxis()->SetRangeUser(ymin,ymax);
0209 h2->DrawCopy("box");
0210 if (profile) {
0211 TProfile* prof = h2->ProfileX("profile",-1,-1,"s");
0212 prof->SetMarkerStyle(20);
0213 prof->SetMarkerSize(1.2);
0214 prof->SetMarkerColor(1);
0215 prof->SetLineColor(1);
0216 prof->SetLineWidth(2);
0217 prof->DrawCopy("same e1");
0218 delete prof;
0219 }
0220 TLine * l = new TLine(h2->GetXaxis()->GetXmin(),0,h2->GetXaxis()->GetXmax(),0);
0221 l->SetLineColor(3);
0222 l->Draw();
0223 }
0224
0225
0226
0227
0228 TF1* drawGFit(TH1 * h1, float nsigmas, float min, float max){
0229
0230 gPad->SetGrid(1,1);
0231 gStyle->SetGridColor(15);
0232 h1->GetXaxis()->SetRangeUser(min,max);
0233 float minfit = h1->GetMean() - h1->GetRMS();
0234 float maxfit = h1->GetMean() + h1->GetRMS();
0235
0236 TLine * l = new TLine(0,0,0,h1->GetMaximum()*1.05);
0237
0238 l->SetLineColor(3);
0239 l->SetLineWidth(2);
0240
0241 static int i = 0;
0242 TString nameF1 = TString("g") + (Long_t)i;
0243 i++;
0244 TF1* g1 = new TF1(nameF1,"gaus",minfit,maxfit);
0245
0246 g1->SetLineColor(2);
0247 g1->SetLineWidth(2);
0248 h1->Fit(g1,"RQ");
0249
0250 minfit = g1->GetParameter("Mean") - nsigmas*g1->GetParameter("Sigma");
0251 maxfit = g1->GetParameter("Mean") + nsigmas*g1->GetParameter("Sigma");
0252 g1->SetRange(minfit,maxfit);
0253
0254 h1->Fit(g1,"RQ");
0255 TF1* fh=h1->GetFunction(nameF1);
0256 if (fh) fh->FixParameter(0,g1->GetParameter(0));
0257
0258 gPad->Draw();
0259 l->Draw();
0260 h1->Draw("same");
0261 return g1;
0262 }
0263
0264
0265
0266
0267
0268
0269
0270
0271
0272 TCanvas * newCanvas(TString name="", TString title="",
0273 Int_t xdiv=0, Int_t ydiv=0, Int_t form = 1, Int_t w=-1){
0274 static char i = '1';
0275 if (name == "") {
0276 name = TString("Canvas ") + i;
0277 i++;
0278 }
0279 if (title == "") title = name;
0280 TCanvas * c = 0;
0281 if (w<0) {
0282 c = new TCanvas(name,title, form);
0283 } else {
0284 c = new TCanvas(name,title,form,w);
0285 }
0286 if (xdiv*ydiv!=0) c->Divide(xdiv,ydiv);
0287 c->cd(1);
0288 return c;
0289 }
0290
0291
0292 TCanvas * newCanvas(Int_t xdiv, Int_t ydiv, Int_t form = 1) {
0293 return newCanvas("","",xdiv,ydiv,form);
0294 }
0295
0296
0297 TCanvas * newCanvas(Int_t form = 1)
0298 {
0299 return newCanvas(0,0,form);
0300 }
0301
0302 TCanvas * newCanvas(TString name, Int_t xdiv, Int_t ydiv, Int_t form,
0303 Int_t w) {
0304 return newCanvas(name, name,xdiv,ydiv,form,w);
0305 }
0306
0307 TCanvas * newCanvas(TString name, Int_t form, Int_t w=-1)
0308 {
0309 return newCanvas(name, name, 0,0,form,w);
0310 }
0311
0312
0313
0314
0315
0316
0317 void printCanvasesPS(TString name){
0318 TPostScript * ps = new TPostScript(name,112);
0319 TIter iter(gROOT->GetListOfCanvases());
0320 TCanvas *c;
0321 while( (c = (TCanvas *)iter()) )
0322 {
0323 c->cd();
0324 cout << "Printing " << c->GetName() << endl;
0325 ps->NewPage();
0326 c->Draw();
0327 }
0328 cout << " File " << name << " was created" << endl;
0329 ps->Close();
0330 }
0331
0332 void printCanvasesEps(){
0333 TIter iter(gROOT->GetListOfCanvases());
0334 TCanvas *c;
0335 while( (c = (TCanvas *)iter()) ) {
0336 c->Print(0,".eps");
0337 }
0338 }
0339
0340 void printCanvasesEps2() {
0341 gROOT->GetListOfCanvases()->Print(".eps");
0342 }
0343
0344
0345 void printCanvases(TString type="png", TString path="."){
0346 TIter iter(gROOT->GetListOfCanvases());
0347 TCanvas *c;
0348 while( (c = (TCanvas *)iter()) ) {
0349 TString name = c->GetTitle();
0350 c->Print(path+"/"+name+"."+type,type);
0351 }
0352 }
0353
0354
0355
0356
0357
0358
0359
0360 TStyle * getStyle(TString name="myStyle")
0361 {
0362 TStyle *theStyle;
0363
0364 gROOT->ForceStyle();
0365
0366 if ( name == "myStyle" ) {
0367 theStyle = new TStyle("myStyle", "myStyle");
0368
0369 theStyle->SetPadBorderMode(0);
0370 theStyle->SetCanvasBorderMode(0);
0371 theStyle->SetPadColor(0);
0372 theStyle->SetCanvasColor(0);
0373 theStyle->SetMarkerStyle(8);
0374 theStyle->SetMarkerSize(0.7);
0375 theStyle->SetStatH(0.3);
0376 theStyle->SetStatW(0.15);
0377
0378
0379 theStyle->SetTitleBorderSize(1);
0380 theStyle->SetPalette(1);
0381
0382 } else if( name == "tdr" ) {
0383 theStyle = new TStyle("tdrStyle","Style for P-TDR");
0384
0385
0386 theStyle->SetCanvasBorderMode(0);
0387 theStyle->SetCanvasColor(kWhite);
0388
0389
0390 theStyle->SetCanvasDefH(750);
0391 theStyle->SetCanvasDefW(1000);
0392
0393 theStyle->SetCanvasDefX(0);
0394 theStyle->SetCanvasDefY(0);
0395
0396
0397 theStyle->SetPadBorderMode(0);
0398
0399 theStyle->SetPadColor(kWhite);
0400 theStyle->SetPadGridX(true);
0401 theStyle->SetPadGridY(true);
0402 theStyle->SetGridColor(0);
0403 theStyle->SetGridStyle(3);
0404 theStyle->SetGridWidth(1);
0405
0406
0407 theStyle->SetFrameBorderMode(0);
0408 theStyle->SetFrameBorderSize(1);
0409 theStyle->SetFrameFillColor(0);
0410 theStyle->SetFrameFillStyle(0);
0411 theStyle->SetFrameLineColor(1);
0412 theStyle->SetFrameLineStyle(1);
0413 theStyle->SetFrameLineWidth(1);
0414
0415
0416
0417
0418 theStyle->SetHistLineColor(kBlue);
0419 theStyle->SetMarkerColor(kBlue);
0420
0421
0422
0423
0424
0425
0426 theStyle->SetEndErrorSize(2);
0427
0428
0429
0430 theStyle->SetMarkerStyle(20);
0431 theStyle->SetMarkerSize(0.5);
0432
0433
0434
0435 theStyle->SetOptFit(1);
0436 theStyle->SetFitFormat("5.4g");
0437 theStyle->SetFuncColor(2);
0438 theStyle->SetFuncStyle(1);
0439 theStyle->SetFuncWidth(1);
0440
0441
0442 theStyle->SetOptDate(0);
0443
0444
0445
0446
0447 theStyle->SetOptFile(0);
0448
0449
0450 theStyle->SetOptStat("e");
0451 theStyle->SetStatColor(kWhite);
0452
0453
0454 theStyle->SetStatTextColor(1);
0455 theStyle->SetStatFormat("6.4g");
0456 theStyle->SetStatBorderSize(1);
0457
0458
0459
0460 theStyle->SetStatX(0.94);
0461 theStyle->SetStatY(0.96);
0462
0463
0464
0465 theStyle->SetPadBottomMargin(0.11);
0466
0467
0468 theStyle->SetPadLeftMargin(0.15);
0469
0470
0471
0472
0473
0474
0475
0476 theStyle->SetTitleFillColor(0);
0477
0478
0479
0480
0481 theStyle->SetTitleY(0.96);
0482 theStyle->SetTitleStyle(0);
0483 theStyle->SetTitleBorderSize(0);
0484
0485
0486
0487
0488
0489
0490
0491
0492
0493
0494
0495
0496
0497
0498
0499
0500
0501
0502
0503
0504
0505
0506 theStyle->SetAxisColor(1, "XYZ");
0507 theStyle->SetStripDecimals(kTRUE);
0508 theStyle->SetTickLength(0.03, "XYZ");
0509 theStyle->SetNdivisions(510, "XYZ");
0510 theStyle->SetPadTickX(1);
0511 theStyle->SetPadTickY(1);
0512
0513
0514 theStyle->SetOptLogx(0);
0515 theStyle->SetOptLogy(0);
0516 theStyle->SetOptLogz(0);
0517
0518
0519 theStyle->SetPaperSize(20.,20.);
0520
0521
0522
0523
0524
0525
0526
0527
0528
0529
0530
0531 theStyle->SetTextSize(0.045);
0532
0533
0534
0535
0536
0537 } else {
0538
0539 theStyle = gStyle;
0540 }
0541 return theStyle;
0542 }
0543
0544
0545 void
0546 setPalette()
0547 {
0548 const Int_t NRGBs = 5;
0549 const Int_t NCont = 255;
0550
0551
0552
0553
0554
0555
0556
0557
0558
0559
0560
0561
0562
0563
0564
0565
0566
0567
0568
0569
0570
0571
0572
0573
0574 {
0575
0576 float max = 0.3;
0577 float step=(1-max)/(NRGBs-1);
0578 Double_t stops[NRGBs] = { 0.00, 0.34, 0.61, 0.84, 1.00 };
0579 Double_t red[NRGBs] = { 1.00, 1-step, 1-2*step, 1-3*step, 1-4*step };
0580 Double_t green[NRGBs] = { 1.00, 1-step, 1-2*step, 1-3*step, 1-4*step };
0581 Double_t blue[NRGBs] = { 1.00, 1-step, 1-2*step, 1-3*step, 1-4*step };
0582 TColor::CreateGradientColorTable(NRGBs, stops, red, green, blue, NCont);
0583 }
0584
0585
0586 gStyle->SetNumberContours(NCont);
0587 }
0588
0589
0590
0591 void plotEff(TH1* h1, TH1* h2=0, TH1* h3=0) {
0592 float minY=0.6;
0593 float maxY=1.05;
0594 h1->GetYaxis()->SetRangeUser(minY,maxY);
0595 h1->GetXaxis()->SetRangeUser(0.,2.1);
0596 h1->SetMarkerStyle(20);
0597 h1->SetMarkerSize(0.5);
0598 h1->SetStats(0);
0599 h1->Draw();
0600
0601 if (h2) {
0602 h2->SetLineColor(kRed);
0603 h2->SetMarkerColor(kRed);
0604 h2->SetMarkerStyle(20);
0605 h2->SetMarkerSize(0.5);
0606 h2->SetStats(0);
0607 h2->Draw("same");
0608 }
0609
0610 if (h3) {
0611 h3->SetLineColor(kBlack);
0612 h3->SetMarkerColor(kBlack);
0613 h3->SetMarkerStyle(20);
0614 h3->SetMarkerSize(0.5);
0615 h3->SetStats(0);
0616 h3->Draw("same");
0617 }
0618 }
0619
0620 TH1F* getEffPlot(TH1* hnum, TH1* hden, int rebin=1) {
0621 hnum->Rebin(rebin);
0622 hden->Rebin(rebin);
0623 TH1F* h = (TH1F*)hnum->Clone();
0624 h->Sumw2();
0625 h->Divide(hnum,hden,1.,1.,"B");
0626 h->SetStats(0);
0627 return h;
0628 }