ViewVC Help
View File | Revision Log | Show Annotations | Root Listing
root/cvsroot/UserCode/cbrown/Development/Plotting/Modules/GeneralToolBox.C
(Generate patch)

Comparing UserCode/cbrown/Development/Plotting/Modules/GeneralToolBox.C (file contents):
Revision 1.22 by fronga, Tue Nov 13 13:27:37 2012 UTC vs.
Revision 1.25 by buchmann, Mon Dec 10 16:40:13 2012 UTC

# Line 40 | Line 40
40   #include <TRandom.h>
41   #include <TGraphErrors.h>
42   #include <TROOT.h>
43 + #include <TPolyLine.h>
44 +
45   #ifndef Verbosity
46   #define Verbosity 0
47   #endif
# Line 1315 | Line 1317 | Double_t MarcosChi2TestX(const TH1* h1,
1317     return prob;
1318   }
1319  
1318 /*void save_with_difference(TH1F *obs, vector<TH1F*> predictions, vector<float> prediction_weights, vector<float> prediction_uncerts, TVirtualPad *canvas, string savemeas) {
1319  if(predictions.size()<1) {
1320    write_warning(__FUNCTION__,"Cannot make a pred-obs prediction plot because the prediction vector is empty. aborting!");
1321    return;
1322  }
1323  TH1F *comppred = predictions[0]->Clone("comppred");
1324  comppred->Scale(prediction_weights[0]);
1325  for(int i=1;i<predictions.size();i++) comppred->Add(predictions[i],prediction_weights[i]);
1326  
1327  TH1F *statpred = (TH1F*)comppred->Clone("statpred");
1328  for(int i=1;i<=statpred->GetNbinsX();i++) statpred->SetBinContent(0);
1329  
1330  TH1F *syspred = predictions[0];
1331  for(int i=1;i<=syspred->GetNbinsX();i++) syspred->SetBinContent(i,prediction_weights[0]*prediction_weights[0]*prediction_uncerts[0]*prediction_uncerts[0]*syspred->GetBinContent(i)*syspred->GetBinContent(i));
1332  for(int i=1;i<predictions.size();i++) {
1333    for(int ibin=1;ibin<=predictions[i]->GetNbinsX();ibin++) {
1334      float newentry=syspred->GetBinContent(ibin);
1335      newentry+=(predictions[i]->GetBinContent(ibin))*(predictions[i]->GetBinContent(ibin))*prediction_weights[i]*prediction_weights[i]*prediction_uncerts[i]*prediction_uncerts[i];
1336      syspred->SetBinContent(ibin,newentry);
1337    }
1338  }
1339  for(int ibin=1;ibin<=syspred->GetNbinsX();ibin++) {
1340    syspred->SetBinError(ibin,syspred->GetBinContent(i)+statpred->GetBinError());
1341    syspred->SetBinContent(ibin,0);
1342  }
1343  
1344  
1345 }
1346 */
1320   void save_with_ratio(TH1F *nominator, TH1F *denominator, TVirtualPad *canvas, string savemeas, bool do_bpred_ratio=false, bool extendrange=false, string yaxistitle="ratio",TH1F *syshisto=NULL) {
1321    //this function saves the pad being passed as well as a new one including the ratio.
1322    CompleteSave(canvas,savemeas);
# Line 1420 | Line 1393 | void save_with_ratio(TH1F *nominator, TH
1393       }
1394      
1395    }
1423  eratio->SetFillColor(TColor::GetColor("#00ADE1"));
1396    
1397 +  if(syshisto && !do_bpred_ratio) {
1398 +       SystDown = (TH1F*)syshisto->Clone("SystDown");
1399 +       SystUp = (TH1F*)syshisto->Clone("SystUp");
1400 +       for(int i=1;i<=syshisto->GetNbinsX();i++) {
1401 +         SystDown->SetBinContent(i,1-syshisto->GetBinContent(i));
1402 +         SystUp->SetBinContent(i,1+syshisto->GetBinContent(i));
1403 +       }
1404 +       SystDown->SetLineColor(TColor::GetColor("#006DE1"));
1405 +       SystUp->SetLineColor(TColor::GetColor("#006DE1"));
1406 +  }
1407 +  eratio->SetFillColor(TColor::GetColor("#00ADE1"));
1408  
1409    ratio->SetTitle("");
1410    ratio->GetYaxis()->SetRangeUser(0.0,2.0);
# Line 1500 | Line 1483 | void save_with_ratio(TH1F *nominator, TH
1483    delete main_canvas;
1484   }
1485  
1486 < void save_with_ratio_and_sys_band(TH1F *nominator, TH1F *denominator, TVirtualPad *canvas, string savemeas, float systematic, string yaxistitle="SysRatio") {
1486 > void save_with_ratio_and_sys_band(TH1F *nominator, TH1F *denominator, TVirtualPad *canvas, string savemeas, bool do_bpred_ratio=false, bool extendrange=false, string yaxistitle="ratio",TH1F *syshisto=NULL) {
1487    //this function saves the pad being passed as well as a new one including the SysRatio.
1488    CompleteSave(canvas,savemeas);
1489  
1490    float bottommargin=gStyle->GetPadBottomMargin();
1491    float canvas_height=gStyle->GetCanvasDefH();
1492    float canvas_width=gStyle->GetCanvasDefW();
1493 <  float SysRatiospace=0.25;// space the SysRatio should take up (relative to original pad)
1493 >  float ratiospace=0.25;// space the ratio should take up (relative to original pad)
1494    
1495 <  float SysRatiobottommargin=0.3;
1496 <  float SysRatiotopmargin=0.1;
1495 >  float ratiobottommargin=0.3;
1496 >  float ratiotopmargin=0.1;
1497 >  
1498 >  float xstretchfactor=((1-ratiospace)*(1-gStyle->GetPadTopMargin()))/((1)*ratiospace);
1499    
1500 <  float xstretchfactor=((1-SysRatiospace)*(1-gStyle->GetPadTopMargin()))/((1)*SysRatiospace);
1500 >  TCanvas *main_canvas = new TCanvas("main_canvas","main_canvas",(Int_t)canvas_width,(Int_t)(canvas_height*(1+ratiospace)));
1501 >  TPad *mainpad = new TPad("mainpad","mainpad",0,1-(1.0/(1+ratiospace)),1,1);//top (main) pad
1502 >  TPad *coverpad = new TPad("coverpad","coverpad",gStyle->GetPadLeftMargin()-0.008,1-(1.0/(1+ratiospace))-0.04,1,1-(1.0/(1+ratiospace))+0.103);//pad covering up the x scale
1503 >  TPad *bottompad = new TPad("bottompad", "Ratio Pad",0,0,1,(1-(1-bottommargin)/(1+ratiospace))-0.015); //bottom pad
1504    
1505 <  TCanvas *MainCanvas = new TCanvas("MainCanvas","MainCanvas",(Int_t)canvas_width,(Int_t)(canvas_height*(1+SysRatiospace)));
1506 <  TPad *MainPad = new TPad("MainPad","MainPad",0,1-(1.0/(1+SysRatiospace)),1,1);//top (main) pad
1507 <  TPad *CoverPad = new TPad("CoverPad","CoverPad",gStyle->GetPadLeftMargin()-0.008,1-(1.0/(1+SysRatiospace))-0.04,1,1-(1.0/(1+SysRatiospace))+0.103);//pad covering up the x scale
1508 <  TPad *BottomPad = new TPad("BottomPad", "Ratio Pad",0,0,1,(1-(1-bottommargin)/(1+SysRatiospace))-0.015); //bottom pad
1509 <  
1510 <  MainCanvas->Range(0,0,1,1);
1511 <  MainCanvas->SetBorderSize(0);
1512 <  MainCanvas->SetFrameFillColor(0);
1513 <  
1514 <  MainPad->Draw();
1527 <  MainPad->cd();
1528 <  MainPad->Range(0,0,1,1);
1529 <  MainPad->SetFillColor(kWhite);
1530 <  MainPad->SetBorderSize(0);
1531 <  MainPad->SetFrameFillColor(0);
1505 >  main_canvas->Range(0,0,1,1);
1506 >  main_canvas->SetBorderSize(0);
1507 >  main_canvas->SetFrameFillColor(0);
1508 >  
1509 >  mainpad->Draw();
1510 >  mainpad->cd();
1511 >  mainpad->Range(0,0,1,1);
1512 >  mainpad->SetFillColor(kWhite);
1513 >  mainpad->SetBorderSize(0);
1514 >  mainpad->SetFrameFillColor(0);
1515    canvas->Range(0,0,1,1);
1516    canvas->Draw("same");
1517 <  MainPad->Modified();
1518 <  MainCanvas->cd();
1519 <  CoverPad->Draw();
1520 <  CoverPad->cd();
1521 <  CoverPad->Range(0,0,1,1);
1522 <  CoverPad->SetFillColor(kWhite);
1523 <  CoverPad->SetBorderSize(0);
1524 <  CoverPad->SetFrameFillColor(0);
1525 <  CoverPad->Modified();
1526 <  MainCanvas->cd();
1527 <  BottomPad->SetTopMargin(SysRatiotopmargin);
1528 <  BottomPad->SetBottomMargin(SysRatiobottommargin);
1529 <  BottomPad->Draw();
1530 <  BottomPad->cd();
1531 <  BottomPad->Range(0,0,1,1);
1532 <  BottomPad->SetFillColor(kWhite);
1533 <  TH1F *SysRatio = (TH1F*)nominator->Clone(GetNumericHistoName().c_str());
1534 <  SysRatio->Divide(denominator);
1517 >  mainpad->Modified();
1518 >  main_canvas->cd();
1519 >  coverpad->Draw();
1520 >  coverpad->cd();
1521 >  coverpad->Range(0,0,1,1);
1522 >  coverpad->SetFillColor(kWhite);
1523 >  coverpad->SetBorderSize(0);
1524 >  coverpad->SetFrameFillColor(0);
1525 >  coverpad->Modified();
1526 >  main_canvas->cd();
1527 >  bottompad->SetTopMargin(ratiotopmargin);
1528 >  bottompad->SetBottomMargin(ratiobottommargin);
1529 >  bottompad->Draw();
1530 >  bottompad->cd();
1531 >  bottompad->Range(0,0,1,1);
1532 >  bottompad->SetFillColor(kWhite);
1533 >  TH1F *ratio = (TH1F*)nominator->Clone(GetNumericHistoName().c_str());
1534 >  ratio->Divide(denominator);
1535  
1536  
1537 +  TGraphAsymmErrors *eratio;
1538 +  TGraphAsymmErrors *SysEnvelope = new TGraphAsymmErrors();
1539 +  
1540 +  if(!do_bpred_ratio) eratio = produce_ratio_graph(ratio);
1541 +  else {
1542 +     bool using_data=false;
1543 +     if((int)savemeas.find("Data")>0) using_data=true;
1544 +     eratio = histRatio(nominator,denominator,using_data,PlottingSetup::global_ratio_binning,false);
1545 +     if(syshisto!=0) {
1546 +       for(int i=1;i<=syshisto->GetNbinsX();i++) {
1547 +         SysEnvelope->SetPoint(i-1,syshisto->GetBinCenter(i),1.0);
1548 +         SysEnvelope->SetPointError(i-1,syshisto->GetBinWidth(i),syshisto->GetBinWidth(i),syshisto->GetBinContent(i),syshisto->GetBinContent(i));
1549 +       }
1550 +       SysEnvelope->SetFillColor(TColor::GetColor("#006DE1"));
1551 +     }
1552 +     for(int i=1;i<=ratio->GetNbinsX();i++) {
1553 +        ratio->SetBinContent(i,0);
1554 +        ratio->SetBinError(i,0);
1555 +     }
1556 +    
1557 +  }
1558 +  
1559 +  if(syshisto && !do_bpred_ratio) {
1560 +       for(int i=1;i<=syshisto->GetNbinsX();i++) {
1561 +         SysEnvelope->SetPoint(i-1,syshisto->GetBinCenter(i),1.0);
1562 +         SysEnvelope->SetPointError(i-1,syshisto->GetBinWidth(i),syshisto->GetBinWidth(i),syshisto->GetBinContent(i),syshisto->GetBinContent(i));
1563 +       }
1564 +       SysEnvelope->SetFillColor(TColor::GetColor("#006DE1"));
1565 +  }
1566  
1567 <  SysRatio->SetTitle("");
1568 <  SysRatio->GetYaxis()->SetRangeUser(0.0,2.0);
1569 <  SysRatio->GetXaxis()->SetTitle(nominator->GetXaxis()->GetTitle());
1570 <  SysRatio->GetXaxis()->CenterTitle();
1571 <  SysRatio->GetYaxis()->SetTitle(yaxistitle.c_str());
1572 <  SysRatio->GetYaxis()->SetTitleOffset(0.4);
1573 <  SysRatio->GetYaxis()->CenterTitle();
1574 <  SysRatio->SetStats(0);
1575 <  SysRatio->GetXaxis()->SetLabelSize(xstretchfactor*SysRatio->GetXaxis()->GetLabelSize());
1576 <  SysRatio->GetYaxis()->SetLabelSize(xstretchfactor*SysRatio->GetYaxis()->GetLabelSize());
1577 <  SysRatio->GetXaxis()->SetTitleSize(xstretchfactor*gStyle->GetTitleSize());
1578 <  SysRatio->GetYaxis()->SetTitleSize(xstretchfactor*gStyle->GetTitleSize());
1579 <  SysRatio->GetYaxis()->SetNdivisions(502,false);
1580 <  SysRatio->SetFillColor(TColor::GetColor("#58D3F7"));
1581 <  TBox *sysbox = new TBox(SysRatio->GetXaxis()->GetBinLowEdge(1),1-systematic,SysRatio->GetXaxis()->GetBinLowEdge(SysRatio->GetNbinsX())+SysRatio->GetXaxis()->GetBinWidth(SysRatio->GetNbinsX()),1+systematic);
1582 <  sysbox->SetFillColor(TColor::GetColor("#82FA58")); // light green
1583 <  
1572 <  SysRatio->Draw("e1");
1573 <  sysbox->Draw();
1574 <  SysRatio->Draw("e1,same");
1575 <  SysRatio->Draw("e1,axis,same");
1567 >  ratio->SetTitle("");
1568 >  ratio->GetYaxis()->SetRangeUser(0.0,2.0);
1569 >  if(do_bpred_ratio) ratio->GetYaxis()->SetRangeUser(0.0,2.0);
1570 >  if(extendrange) ratio->GetYaxis()->SetRangeUser(0.0,4.0);
1571 >  ratio->GetXaxis()->SetTitle(nominator->GetXaxis()->GetTitle());
1572 >  ratio->GetXaxis()->CenterTitle();
1573 >  ratio->GetYaxis()->SetTitle(yaxistitle.c_str());
1574 >  ratio->GetYaxis()->SetTitleOffset(0.4);
1575 >  ratio->GetYaxis()->CenterTitle();
1576 >  ratio->SetStats(0);
1577 >  ratio->GetXaxis()->SetLabelSize(xstretchfactor*ratio->GetXaxis()->GetLabelSize());
1578 >  ratio->GetYaxis()->SetLabelSize(xstretchfactor*ratio->GetYaxis()->GetLabelSize());
1579 >  ratio->GetXaxis()->SetTitleSize(xstretchfactor*gStyle->GetTitleSize());
1580 >  ratio->GetYaxis()->SetTitleSize(xstretchfactor*gStyle->GetTitleSize());
1581 >  ratio->GetYaxis()->SetNdivisions(502,false);
1582 >  ratio->SetFillColor(TColor::GetColor("#58D3F7"));
1583 >  ratio->Draw("e1");
1584    
1585 <  TLine *oneline = new TLine(SysRatio->GetXaxis()->GetBinLowEdge(1),1,SysRatio->GetXaxis()->GetBinLowEdge(SysRatio->GetNbinsX())+SysRatio->GetXaxis()->GetBinWidth(SysRatio->GetNbinsX()),1);
1585 >  if(syshisto!=0) {
1586 >    ratio->Draw("");
1587 >    SysEnvelope->SetFillColor(TColor::GetColor("#FE9A2E"));
1588 >    SysEnvelope->Draw("2,same");
1589 >    ratio->Draw("e1,same");
1590 >  } else {
1591 >    eratio->Draw("1");
1592 >  }
1593 >  ratio->Draw("same,axis");
1594 >  TLine *oneline = new TLine(ratio->GetXaxis()->GetBinLowEdge(1),1,ratio->GetXaxis()->GetBinLowEdge(ratio->GetNbinsX())+ratio->GetXaxis()->GetBinWidth(ratio->GetNbinsX()),1);
1595    oneline->SetLineStyle(2);
1596    oneline->SetLineColor(kBlue);
1597    oneline->Draw("same");
1598  
1599 <  MainCanvas->cd();
1600 <  MainCanvas->Modified();
1601 <  MainCanvas->cd();
1602 <  MainCanvas->SetSelected(MainCanvas);
1603 <  
1604 <  CompleteSave(MainCanvas,savemeas+"_withSYSSysRatio");
1605 <  delete SysRatio;
1606 <  delete MainCanvas;
1599 >  main_canvas->cd();
1600 >  main_canvas->Modified();
1601 >  main_canvas->cd();
1602 >  main_canvas->SetSelected(main_canvas);
1603 >  
1604 >  CompleteSave(main_canvas,savemeas+"_withSysRatio");
1605 >  bottompad->cd();
1606 >  
1607 >  Double_t chi2;
1608 >  Int_t ndf,igood;
1609 >  Double_t chi2prob = MarcosChi2TestX(nominator,denominator,chi2,ndf,igood,"UW");
1610 >
1611 >  stringstream Chi2text;
1612 >  Chi2text << "#chi^{2} / ndf: " << chi2 << " / " << ndf;
1613 >  stringstream Chi2probtext;
1614 >  Chi2probtext << "#chi^{2} prob: " << chi2prob;
1615 >  TText* chi2box = write_text(0.02,0.11,Chi2text.str());
1616 >  chi2box->SetTextSize(0.08);
1617 >  chi2box->SetTextAlign(11);
1618 >  chi2box->Draw();
1619 >  TText* chi2probbox = write_text(0.02,0.04,Chi2probtext.str());
1620 >  chi2probbox->SetTextSize(0.08);
1621 >  chi2probbox->SetTextAlign(11);
1622 >  chi2probbox->Draw();
1623 >  
1624 >  stringstream CompRatio;
1625 >  CompRatio << "Ratio: " << nominator->Integral() << " / " << denominator->Integral() << " \n " << nominator->Integral()/denominator->Integral();
1626 >  TText *CompleteRatio = write_text(0.98,0.04,CompRatio.str());
1627 >  CompleteRatio->SetTextSize(0.08);
1628 >  CompleteRatio->SetTextAlign(31);
1629 >  CompleteRatio->Draw();
1630 >  
1631 >  CompleteSave(main_canvas,savemeas+"_withSysRatio_and_Chi2");
1632 >
1633 >  delete main_canvas;
1634   }
1635  
1636   TH1F* CollapseStack(THStack stack,TString hname="base") {
# Line 1803 | Line 1847 | string removefunnystring(string name) {
1847    return name;
1848   }
1849  
1850 + TPolyLine* GetFitUncertaintyShape(TF1 *fit, float low, float high,float x0,float x1) {
1851 +  int nPoints=1000;
1852 +  double x[nPoints+1];
1853 +  double y[nPoints];
1854 +  
1855 +  float par1=fit->GetParameter(0);
1856 +  float dpar1=fit->GetParError(0);
1857 +  float par2=fit->GetParameter(1);
1858 +  float dpar2=fit->GetParError(1);
1859 +    
1860 +  float step=(x1-x0)/(nPoints/2.0 -1);
1861 +  for(int i=0;i<nPoints/2.0;i++) {
1862 +    x[i]=x0+i*step;
1863 +    y[i]=(par1+x[i]*par2)    -    sqrt(dpar1*dpar1+dpar2*dpar2*x[i]*x[i]);
1864 +    x[nPoints-1-i]=x[i];
1865 +    y[nPoints-1-i]=(par1+x[i]*par2)    +    sqrt(dpar1*dpar1+dpar2*dpar2*x[i]*x[i]);
1866 +    if(y[i]<low) y[i]=low;
1867 +    if(y[i]>high) y[i]=high;
1868 +    if(y[nPoints-1-i]<low) y[nPoints-1-i]=low;
1869 +    if(y[nPoints-1-i]>high) y[nPoints-1-i]=high;
1870 +  }
1871 +  
1872 +  x[nPoints]=x[0];
1873 +  y[nPoints]=y[0];
1874 +  
1875 +  TPolyLine *l = new TPolyLine(nPoints+1,x,y);
1876 +  l->SetFillColor(TColor::GetColor("#5858FA"));
1877 +  l->SetLineColor(TColor::GetColor("#5858FA"));
1878 +  l->SetLineWidth(1);
1879 +  return l;
1880 + }  
1881 +
1882 +
1883 + TPolyLine* GetFitUncertaintyShape(TH1 *histo, string fitname, float low, float high) {
1884 +  TF1 *fit = (TF1*)histo->GetFunction(fitname.c_str());
1885 +  if(!fit) {
1886 +    cout << "CANNOT PRODUCE FIT UNCERTAINTY AS THERE IS NO FIT CALLED " << fitname << " IN " << histo->GetName() << endl;
1887 +    return NULL;
1888 +  }
1889 +  float x0=histo->GetBinLowEdge(1);
1890 +  float x1=histo->GetBinLowEdge(histo->GetNbinsX())+histo->GetBinWidth(histo->GetNbinsX());
1891 +  return GetFitUncertaintyShape(fit, low, high,x0,x1);
1892 + }
1893 +
1894 + TPolyLine* GetFitUncertaintyShape(TGraphErrors *gr, string fitname, float low, float high, float minx=-999, float maxx=-999) {
1895 +  TF1 *fit = (TF1*)gr->GetFunction(fitname.c_str());
1896 +  if(!fit) {
1897 +    cout << "CANNOT PRODUCE FIT UNCERTAINTY AS THERE IS NO FIT CALLED " << fitname << " IN " << gr->GetName() << endl;
1898 +    return NULL;
1899 +  }
1900 +  double x,y;
1901 +  gr->GetPoint(0,x,y);
1902 +  float min=x;
1903 +  gr->GetPoint(gr->GetN()-1,x,y);
1904 +  float max=x;
1905 +  if(minx>-999 && maxx>-999 && minx<maxx) {
1906 +    max=maxx;
1907 +    min=minx;
1908 +  }
1909 +  return GetFitUncertaintyShape(fit, low, high,min,max);
1910 + }
1911 +
1912   stringstream all_bugs;
1913  
1914   void bug_tracker(string function, int line, string description) {
# Line 1853 | Line 1959 | float generalizedpSTAR(float massmother,
1959          res=TMath::Sqrt(res)/(2*massmother);
1960          return res;
1961   }
1962 +
1963 + void FindMinMax(TGraphErrors *gr, float &min, float &max) {
1964 +  double x,y;
1965 +  gr->GetPoint(0,x,y);
1966 +  min=500*y;
1967 +  max=0.0002*y;
1968 +  for(int i=0;i<gr->GetN();i++) {
1969 +    gr->GetPoint(i,x,y);
1970 +    cout << "Point at " << x << "/" << y << " with error " << gr->GetErrorY(i) << endl;
1971 +    float potmin=y-1.5*gr->GetErrorY(i); // taking 1.5 times the error to allow for enough space to present the plot
1972 +    float potmax=y+1.5*gr->GetErrorY(i); // taking 1.5 times the error to allow for enough space to present the plot
1973 +    if(potmin<min) min=potmin;
1974 +    if(potmax>max) max=potmax;
1975 +  }
1976 + }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines