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.11 by buchmann, Wed Jun 6 08:24:13 2012 UTC vs.
Revision 1.20 by buchmann, Wed Sep 19 09:16:24 2012 UTC

# Line 39 | Line 39
39   #include <TPaveText.h>
40   #include <TRandom.h>
41   #include <TGraphErrors.h>
42 + #include <TROOT.h>
43   #ifndef Verbosity
44   #define Verbosity 0
45   #endif
# Line 61 | Line 62 | namespace PlottingSetup {
62      int PaperMode=0; // PaperMode=true will suppress "Preliminary" in DrawPrelim()
63      int Approved=0; // Approved=true will only plot approved plots
64      bool is2012=true;
65 +    bool is53reco=true;
66 +    bool openBox = true;
67   }
68  
69   bool dopng=false;
# Line 254 | Line 257 | void ensure_directory_exists(string this
257    }
258   }
259  
260 + void DeadEnd(string message) {
261 +  dout << endl;
262 +  dout << "\033[1;31m     _                _                  _  " << endl;
263 +  dout << "\033[1;31m  __| | ___  __ _  __| |   ___ _ __   __| | " << endl;
264 +  dout << "\033[1;31m / _` |/ _ \\/ _` |/ _` |  / _ \\ '_ \\ / _` | " << endl;
265 +  dout << "\033[1;31m| (_| |  __/ (_| | (_| | |  __/ | | | (_| | " << endl;
266 +  dout << "\033[1;31m \\__,_|\\___|\\__,_|\\__,_|  \\___|_| |_|\\__,_| " << endl;
267 +  dout << "\033[1;31m                                            " << endl;
268 +  dout << "" << endl;
269 +  dout << " A totally fatal error has occurred: " << endl;
270 +  dout << " " << message << endl;
271 +  dout << endl;
272 +  dout << "If you do not know how to deal with this error message please call Customer Support Mumbai (or Zurich...) \033[0m" << endl;
273 +  dout << "   ... aborting now ... " << endl;
274 +  assert(0);
275 + }
276 +
277 +
278 + void SanityChecks() {
279 +  //this function gets called whenever samples are initialized (see ActiveSamples.C)
280 +  //whatever is necessary for the code to run should be put here.
281 +  
282 +  if(gROOT->GetVersionInt()<53200) {
283 +    DeadEnd("ROOT version outdated, you are using "+any2string(gROOT->GetVersionInt())+" but the earliest recommended version is 5.32.00");
284 +  }
285 +  
286 +  
287 + }
288 +
289   void initialize_log() {
290    if(!PlottingSetup::publicmode) {
291      dout << "____________________________________________________________" << endl;
# Line 551 | Line 583 | void DrawPrelim(float writelumi=generalt
583    string prel=" Preliminary";
584    if(PlottingSetup::PaperMode) prel="";
585    //prelimtext << "CMS Preliminary 2011 , #sqrt{s}= 7 TeV, L= O(1) fb^{-1}"; //temporary replacement
586 +  if(PlottingSetup::is53reco) prel += " 53X";
587    string energy="7 TeV";
588    if(PlottingSetup::is2012) energy="8 TeV";
589    if(writelumi == 0) {
# Line 636 | Line 669 | float computeProductError(float a, float
669      return val;
670   }
671  
672 + TGraphAsymmErrors *SysAndStatRatio(TH1F *h1,TH1F *h2, TH1F *sys, int id, vector<float>binning, bool precise=false) {
673 +    int absJZBbinsNumber = binning.size()-1;
674 +    TGraphAsymmErrors* graph = new TGraphAsymmErrors(absJZBbinsNumber);
675 +    for(int i=0;i<absJZBbinsNumber;i++)
676 +        {
677 +                float xCenter=h1->GetBinCenter(i+1);
678 +                float xWidth=(h1->GetBinWidth(i+1))*0.5;
679 +                float nominatorError = h1->GetBinError(i+1);
680 +                float nominator=h1->GetBinContent(i+1);
681 +                float denominatorError=h2->GetBinError(i+1);
682 +                float denominator=h2->GetBinContent(i+1);
683 +                float errorN = 0;
684 +                float errorP = computeRatioError(nominator,nominatorError,denominator,denominatorError);
685 +                float syserr=sys->GetBinContent(i+1);
686 +                float errorsys=0;
687 +                if(id==1) // (is data)
688 +                {
689 +                        if(!precise) errorP = computeRatioError(nominator,statErrorP(nominator),denominator,statErrorP(denominator));
690 +                        else         errorP = computeRatioError(nominator,nominatorError,denominator,denominatorError);
691 +                        errorsys=computeRatioError(nominator,syserr,denominator,0);
692 +                        errorP = TMath::Sqrt(errorsys*errorsys+errorP*errorP);
693 +                        errorN = errorP; // symmetrize using statErrorP
694 +                } else {
695 +                        errorN = computeRatioError(nominator,nominatorError,denominator,denominatorError);
696 +                        errorsys=computeRatioError(nominator,syserr,denominator,0);
697 +                        errorN = TMath::Sqrt(errorsys*errorsys+errorP*errorP);
698 +                        errorP = errorN; // symmetrize using statErrorP
699 +                }
700 +                if(denominator!=0) {
701 +                        graph->SetPoint(i, xCenter, nominator/denominator);
702 +                        graph->SetPointError(i,xWidth,xWidth,errorN,errorP);
703 +                }
704 +                else {
705 +                        graph->SetPoint(i, xCenter, -999);
706 +                        graph->SetPointError(i,xWidth,xWidth,errorN,errorP);
707 +                }
708 +        }      
709 +        return graph;
710 + }
711 +    
712 +
713   TGraphAsymmErrors *histRatio(TH1F *h1,TH1F *h2, int id, vector<float>binning, bool precise=false)
714   {
715          int absJZBbinsNumber = binning.size()-1;
# Line 872 | Line 946 | string interpret_cut(string incut, bool
946    
947      if(incut=="id1==0") return "ee";
948      if(incut=="id1==1") return "#mu#mu";
949 <    if(incut=="abs(mll-91.2)<20") return "|m_{l^{+}l^{-}}-m_{Z}|<20";
949 >    if(incut=="abs(mll-91)<10") return "|m_{l^{+}l^{-}}-m_{Z}|<10";
950      if(incut=="pfJetGoodID[0]") return "";
951      if(incut=="pfJetGoodID[1]") return "";
952      if((int)incut.find("pfJetGoodNum")>-1) {
# Line 1241 | Line 1315 | Double_t MarcosChi2TestX(const TH1* h1,
1315     return prob;
1316   }
1317  
1318 < void save_with_ratio(TH1F *nominator, TH1F *denominator, TVirtualPad *canvas, string savemeas, bool do_bpred_ratio=false, bool extendrange=false, string yaxistitle="ratio") {
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 > */
1347 > 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) {
1348    //this function saves the pad being passed as well as a new one including the ratio.
1349    CompleteSave(canvas,savemeas);
1350  
# Line 1293 | Line 1396 | void save_with_ratio(TH1F *nominator, TH
1396  
1397  
1398    TGraphAsymmErrors *eratio;
1399 +  TH1F *SystDown;
1400 +  TH1F *SystUp;
1401 +  
1402    if(!do_bpred_ratio) eratio = produce_ratio_graph(ratio);
1403    else {
1404       bool using_data=false;
1405       if((int)savemeas.find("Data")>0) using_data=true;
1406       eratio = histRatio(nominator,denominator,using_data,PlottingSetup::global_ratio_binning,false);
1407 +     if(syshisto!=0) {
1408 +       SystDown = (TH1F*)syshisto->Clone("SystDown");
1409 +       SystUp = (TH1F*)syshisto->Clone("SystUp");
1410 +       for(int i=1;i<=syshisto->GetNbinsX();i++) {
1411 +         SystDown->SetBinContent(i,1-syshisto->GetBinContent(i));
1412 +         SystUp->SetBinContent(i,1+syshisto->GetBinContent(i));
1413 +       }
1414 +       SystDown->SetLineColor(TColor::GetColor("#006DE1"));
1415 +       SystUp->SetLineColor(TColor::GetColor("#006DE1"));
1416 +     }
1417       for(int i=1;i<=ratio->GetNbinsX();i++) {
1418          ratio->SetBinContent(i,0);
1419          ratio->SetBinError(i,0);
1420       }
1421 +    
1422    }
1423    eratio->SetFillColor(TColor::GetColor("#00ADE1"));
1424 +  
1425  
1426    ratio->SetTitle("");
1427    ratio->GetYaxis()->SetRangeUser(0.5,1.5);
# Line 1323 | Line 1441 | void save_with_ratio(TH1F *nominator, TH
1441    ratio->SetFillColor(TColor::GetColor("#58D3F7"));
1442    ratio->SetMarkerSize(0);
1443    ratio->Draw("e2");
1444 <  eratio->Draw("2");
1444 >  if(syshisto!=0) {
1445 > //    sysratio->Draw("2");
1446 > //    eratio->Draw("2,same");
1447 >    eratio->Draw("2");
1448 >    SystDown->Draw("histo,same");
1449 >    SystUp->Draw("histo,same");
1450 >  } else {
1451 >    eratio->Draw("2");
1452 >  }
1453    ratio->Draw("same,axis");
1454    TLine *oneline = new TLine(ratio->GetXaxis()->GetBinLowEdge(1),1,ratio->GetXaxis()->GetBinLowEdge(ratio->GetNbinsX())+ratio->GetXaxis()->GetBinWidth(ratio->GetNbinsX()),1);
1455    oneline->SetLineStyle(2);
# Line 1374 | Line 1500 | void save_with_ratio(TH1F *nominator, TH
1500    delete main_canvas;
1501   }
1502  
1503 + void save_with_ratio_and_sys_band(TH1F *nominator, TH1F *denominator, TVirtualPad *canvas, string savemeas, float systematic, string yaxistitle="SysRatio") {
1504 +  //this function saves the pad being passed as well as a new one including the SysRatio.
1505 +  CompleteSave(canvas,savemeas);
1506 +
1507 +  float bottommargin=gStyle->GetPadBottomMargin();
1508 +  float canvas_height=gStyle->GetCanvasDefH();
1509 +  float canvas_width=gStyle->GetCanvasDefW();
1510 +  float SysRatiospace=0.25;// space the SysRatio should take up (relative to original pad)
1511 +  
1512 +  float SysRatiobottommargin=0.3;
1513 +  float SysRatiotopmargin=0.1;
1514 +  
1515 +  float xstretchfactor=((1-SysRatiospace)*(1-gStyle->GetPadTopMargin()))/((1)*SysRatiospace);
1516 +  
1517 +  TCanvas *MainCanvas = new TCanvas("MainCanvas","MainCanvas",(Int_t)canvas_width,(Int_t)(canvas_height*(1+SysRatiospace)));
1518 +  TPad *MainPad = new TPad("MainPad","MainPad",0,1-(1.0/(1+SysRatiospace)),1,1);//top (main) pad
1519 +  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
1520 +  TPad *BottomPad = new TPad("BottomPad", "Ratio Pad",0,0,1,(1-(1-bottommargin)/(1+SysRatiospace))-0.015); //bottom pad
1521 +  
1522 +  MainCanvas->Range(0,0,1,1);
1523 +  MainCanvas->SetBorderSize(0);
1524 +  MainCanvas->SetFrameFillColor(0);
1525 +  
1526 +  MainPad->Draw();
1527 +  MainPad->cd();
1528 +  MainPad->Range(0,0,1,1);
1529 +  MainPad->SetFillColor(kWhite);
1530 +  MainPad->SetBorderSize(0);
1531 +  MainPad->SetFrameFillColor(0);
1532 +  canvas->Range(0,0,1,1);
1533 +  canvas->Draw("same");
1534 +  MainPad->Modified();
1535 +  MainCanvas->cd();
1536 +  CoverPad->Draw();
1537 +  CoverPad->cd();
1538 +  CoverPad->Range(0,0,1,1);
1539 +  CoverPad->SetFillColor(kWhite);
1540 +  CoverPad->SetBorderSize(0);
1541 +  CoverPad->SetFrameFillColor(0);
1542 +  CoverPad->Modified();
1543 +  MainCanvas->cd();
1544 +  BottomPad->SetTopMargin(SysRatiotopmargin);
1545 +  BottomPad->SetBottomMargin(SysRatiobottommargin);
1546 +  BottomPad->Draw();
1547 +  BottomPad->cd();
1548 +  BottomPad->Range(0,0,1,1);
1549 +  BottomPad->SetFillColor(kWhite);
1550 +  TH1F *SysRatio = (TH1F*)nominator->Clone(GetNumericHistoName().c_str());
1551 +  SysRatio->Divide(denominator);
1552 +
1553 +
1554 +
1555 +  SysRatio->SetTitle("");
1556 +  SysRatio->GetYaxis()->SetRangeUser(0.0,2.0);
1557 +  SysRatio->GetXaxis()->SetTitle(nominator->GetXaxis()->GetTitle());
1558 +  SysRatio->GetXaxis()->CenterTitle();
1559 +  SysRatio->GetYaxis()->SetTitle(yaxistitle.c_str());
1560 +  SysRatio->GetYaxis()->SetTitleOffset(0.4);
1561 +  SysRatio->GetYaxis()->CenterTitle();
1562 +  SysRatio->SetStats(0);
1563 +  SysRatio->GetXaxis()->SetLabelSize(xstretchfactor*SysRatio->GetXaxis()->GetLabelSize());
1564 +  SysRatio->GetYaxis()->SetLabelSize(xstretchfactor*SysRatio->GetYaxis()->GetLabelSize());
1565 +  SysRatio->GetXaxis()->SetTitleSize(xstretchfactor*gStyle->GetTitleSize());
1566 +  SysRatio->GetYaxis()->SetTitleSize(xstretchfactor*gStyle->GetTitleSize());
1567 +  SysRatio->GetYaxis()->SetNdivisions(502,false);
1568 +  SysRatio->SetFillColor(TColor::GetColor("#58D3F7"));
1569 +  TBox *sysbox = new TBox(SysRatio->GetXaxis()->GetBinLowEdge(1),1-systematic,SysRatio->GetXaxis()->GetBinLowEdge(SysRatio->GetNbinsX())+SysRatio->GetXaxis()->GetBinWidth(SysRatio->GetNbinsX()),1+systematic);
1570 +  sysbox->SetFillColor(TColor::GetColor("#82FA58")); // light green
1571 +  
1572 +  SysRatio->Draw("e1");
1573 +  sysbox->Draw();
1574 +  SysRatio->Draw("e1,same");
1575 +  SysRatio->Draw("e1,axis,same");
1576 +  
1577 +  TLine *oneline = new TLine(SysRatio->GetXaxis()->GetBinLowEdge(1),1,SysRatio->GetXaxis()->GetBinLowEdge(SysRatio->GetNbinsX())+SysRatio->GetXaxis()->GetBinWidth(SysRatio->GetNbinsX()),1);
1578 +  oneline->SetLineStyle(2);
1579 +  oneline->SetLineColor(kBlue);
1580 +  oneline->Draw("same");
1581 +
1582 +  MainCanvas->cd();
1583 +  MainCanvas->Modified();
1584 +  MainCanvas->cd();
1585 +  MainCanvas->SetSelected(MainCanvas);
1586 +  
1587 +  CompleteSave(MainCanvas,savemeas+"_withSYSSysRatio");
1588 +  delete SysRatio;
1589 +  delete MainCanvas;
1590 + }
1591  
1592   TH1F* CollapseStack(THStack stack) {
1593     TH1F *bhist = ((TH1F*)((stack.GetHists())->At(0)));
# Line 1506 | Line 1720 | void process_directory(TString directory
1720                  perror ("Couldn't open the directory");
1721   }
1722  
1723 + void ClearHisto(TH1* histo) {
1724 +    for(int ix=0;ix<=histo->GetNbinsX()+1;ix++) {//also reset over- and underflow
1725 +        histo->SetBinContent(ix,0);
1726 +        histo->SetBinContent(ix,0);
1727 +    }
1728 + }
1729 +
1730 + void AddSquared(TH1F *histo, TH1F *addthis, float weight) {
1731 +    for(int ix=1;ix<=addthis->GetNbinsX()+1;ix++) {//also include over- and underflow
1732 +        float bincenter=addthis->GetBinCenter(ix);
1733 +        int nBinHisto= histo->FindBin(bincenter);
1734 +        float old_content=histo->GetBinContent(nBinHisto);
1735 +        histo->SetBinContent(nBinHisto,old_content+((addthis->GetBinContent(ix))*(addthis->GetBinContent(ix))*weight));
1736 +    }
1737 + }
1738 +
1739 + void SQRT(TH1* histo) {
1740 +    for(int ix=0;ix<=histo->GetNbinsX()+1;ix++) {//also include over- and underflow
1741 +        histo->SetBinContent(ix,TMath::Sqrt(histo->GetBinContent(ix)));
1742 +    }
1743 + }
1744 +
1745   /*string GetCompleteHostname() {
1746    struct addrinfo hints, *info, *p;
1747    int gai_result;
# Line 1604 | Line 1840 | void bug_tracker(string function, int li
1840   //TODO: Write a bug summary at the end.
1841  
1842   float pSTAR(float mglu, float mlsp, float mchi2) {
1843 <        float mz=91.2;
1843 >        float mz=91.0;
1844          float res=((mchi2*mchi2)-(mlsp+mz)*(mlsp+mz));
1845          res*=((mchi2*mchi2)-(mlsp-mz)*(mlsp-mz));
1846          res=TMath::Sqrt(res)/(2*mchi2);

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines