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.43 by buchmann, Sun May 12 20:22:53 2013 UTC

# Line 40 | Line 40
40   #include <TRandom.h>
41   #include <TGraphErrors.h>
42   #include <TROOT.h>
43 + #include <TPolyLine.h>
44 +
45 + #include "ValueClass.C"
46   #ifndef Verbosity
47   #define Verbosity 0
48   #endif
# Line 64 | Line 67 | namespace PlottingSetup {
67      bool is2012=true;
68      bool is53reco=true;
69      bool openBox = true;
70 +    vector<TH1F*> FakeHistoHeap;
71 +    bool DrawMetSignalRegionMllLines=false; // whether to draw the lines in mll plots for the MET signal region
72 +    float ConsideredZWidth=20;
73   }
74  
75   bool dopng=false;
# Line 90 | Line 96 | void write_error(string funcname, string
96   void write_info(string funcname, string text);
97   string get_directory();
98   bool Contains(string wholestring, string findme);
99 + TH1F* CollapseStack(THStack stack,TString hname);
100 + TH1F* CollapseStack(THStack* stack,TString hname);
101   //-------------------------------------------------------------------------------------
102  
103   template<typename U>
# Line 257 | Line 265 | void ensure_directory_exists(string this
265    }
266   }
267  
268 + void DeleteStack(THStack mcstack) {
269 +  TH1F* h;
270 +  TIter nextHist(mcstack.GetHists());
271 +  while ( h = (TH1F*)nextHist() ) {
272 +    h->Delete();
273 +  }
274 +  delete h;
275 + }
276 +
277 +
278   void DeadEnd(string message) {
279    dout << endl;
280    dout << "\033[1;31m     _                _                  _  " << endl;
# Line 425 | Line 443 | void CompleteSave(TVirtualPad *can, stri
443  
444  
445   void setlumi(float l) {
446 < generaltoolboxlumi=l;
446 >  generaltoolboxlumi=l;
447   }
448  
449   int write_first_line(vector<vector<string> > &entries) {
# Line 583 | Line 601 | void DrawPrelim(float writelumi=generalt
601    string prel=" Preliminary";
602    if(PlottingSetup::PaperMode) prel="";
603    //prelimtext << "CMS Preliminary 2011 , #sqrt{s}= 7 TeV, L= O(1) fb^{-1}"; //temporary replacement
604 <  if(PlottingSetup::is53reco) prel += " 53X";
604 > //  if(PlottingSetup::is53reco) prel += " 53X";
605    string energy="7 TeV";
606    if(PlottingSetup::is2012) energy="8 TeV";
607    if(writelumi == 0) {
608      if(isMC) prelimtext << "CMS Simulation, #sqrt{s} = " << energy;
609      else prelimtext << "CMS" << prel << ", #sqrt{s} = " << energy;
610    } else {
611 <    if(isMC) prelimtext << "CMS Simulation, #sqrt{s} = " << energy << ", L_{int} = " << std::setprecision(3) <<writelumi<<" "<<barn<<"^{-1}";
612 <     else prelimtext << "CMS" << prel << ", #sqrt{s} = " << energy << ", L_{int} = " << std::setprecision(3) <<writelumi<<" "<<barn<<"^{-1}";
611 >    if(isMC) prelimtext << "CMS Simulation, #sqrt{s} = " << energy << ", #scale[0.6]{#int}L dt = " << std::setprecision(3) <<writelumi<<" "<<barn<<"^{-1}";
612 >     else prelimtext << "CMS" << prel << ", #sqrt{s} = " << energy << ", #scale[0.6]{#int}L dt = " << std::setprecision(3) <<writelumi<<" "<<barn<<"^{-1}";
613    }
614    TPaveText *eventSelectionPaveText = new TPaveText(0.27, 0.93,0.77, 1.0,"blNDC");
615    eventSelectionPaveText->SetFillStyle(4000);
# Line 1222 | Line 1240 | bool Contains(string wholestring, string
1240    else return false;
1241   }
1242  
1243 +
1244   //////////////////////////////////////////////////////////////////////////////
1245   //
1246   // http://stackoverflow.com/questions/669438/how-to-get-memory-usage-at-run-time-in-c
# Line 1234 | Line 1253 | bool Contains(string wholestring, string
1253   /* usage:
1254   double vm2, rss2;
1255   process_mem_usage(vm2, rss2);
1256 < cout << "Memory usage: VM: " << vm << "; RSS: " << rss << endl;
1256 > cout << "Memory usage: VM: " << vm2 << "; RSS: " << rss2 << endl;
1257   */
1258  
1259   void process_mem_usage(double& vm_usage, double& resident_set)
# Line 1293 | Line 1312 | TGraphAsymmErrors* produce_ratio_graph(T
1312   }
1313  
1314  
1315 + TLatex* WriteSelection(int njets) {
1316 +  string sel="Loose selection";
1317 +  if(njets==3) sel="Low E_{T}^{miss} selection";
1318 +  assert(njets==2||njets==3);
1319 +  TLatex *sele = new TLatex(0.97,0.135,sel.c_str());
1320 +  sele->SetNDC(true);
1321 +  sele->SetTextColor(TColor::GetColor("#848484"));
1322 +  sele->SetTextFont(42);
1323 +  sele->SetTextAlign(32);
1324 +  sele->SetTextSize(0.03);
1325 +  sele->SetTextAngle(270);
1326 +  return sele;
1327 + }
1328 +
1329   Double_t MarcosChi2TestX(const TH1* h1, const TH1* h2, Double_t &chi2, Int_t &ndf, Int_t &igood, Option_t *option)
1330   {
1331  
# Line 1315 | Line 1348 | Double_t MarcosChi2TestX(const TH1* h1,
1348     return prob;
1349   }
1350  
1351 < /*void save_with_difference(TH1F *obs, vector<TH1F*> predictions, vector<float> prediction_weights, vector<float> prediction_uncerts, TVirtualPad *canvas, string savemeas) {
1352 <  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 <  }
1351 > void Save_With_Ratio(TH1F *nominator, TH1F *denominator, TVirtualPad *orig_canvas, string savemeas, bool do_bpred_ratio=false, bool extendrange=false, string yaxistitle="ratio",TH1F *syshisto=NULL) {
1352 >  //this function saves the pad being passed as well as a new one including the ratio.
1353    
1354 +  orig_canvas->cd();
1355 +  orig_canvas->Update();
1356 +  CompleteSave(orig_canvas,savemeas);
1357 +  TVirtualPad *canvas = (TVirtualPad*) orig_canvas->Clone("TempCanvas");//otherwise the Ratio_main_canvas will own our pad and destroy it upon deletion
1358    
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
1359    float bottommargin=gStyle->GetPadBottomMargin();
1360    float canvas_height=gStyle->GetCanvasDefH();
1361    float canvas_width=gStyle->GetCanvasDefW();
# Line 1358 | Line 1366 | void save_with_ratio(TH1F *nominator, TH
1366    
1367    float xstretchfactor=((1-ratiospace)*(1-gStyle->GetPadTopMargin()))/((1)*ratiospace);
1368    
1369 <  TCanvas *main_canvas = new TCanvas("main_canvas","main_canvas",(Int_t)canvas_width,(Int_t)(canvas_height*(1+ratiospace)));
1369 >  TCanvas *Ratio_main_canvas = new TCanvas("Ratio_main_canvas","Ratio_main_canvas",(Int_t)canvas_width,(Int_t)(canvas_height*(1+ratiospace)));
1370    TPad *mainpad = new TPad("mainpad","mainpad",0,1-(1.0/(1+ratiospace)),1,1);//top (main) pad
1371    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
1372    TPad *bottompad = new TPad("bottompad", "Ratio Pad",0,0,1,(1-(1-bottommargin)/(1+ratiospace))-0.015); //bottom pad
1373    
1374 <  main_canvas->Range(0,0,1,1);
1375 <  main_canvas->SetBorderSize(0);
1376 <  main_canvas->SetFrameFillColor(0);
1369 <  
1374 >  Ratio_main_canvas->Range(0,0,1,1);
1375 >  Ratio_main_canvas->SetBorderSize(0);
1376 >  Ratio_main_canvas->SetFrameFillColor(0);
1377    mainpad->Draw();
1378    mainpad->cd();
1379    mainpad->Range(0,0,1,1);
# Line 1376 | Line 1383 | void save_with_ratio(TH1F *nominator, TH
1383    canvas->Range(0,0,1,1);
1384    canvas->Draw("same");
1385    mainpad->Modified();
1386 <  main_canvas->cd();
1386 >  Ratio_main_canvas->cd();
1387    coverpad->Draw();
1388    coverpad->cd();
1389    coverpad->Range(0,0,1,1);
# Line 1384 | Line 1391 | void save_with_ratio(TH1F *nominator, TH
1391    coverpad->SetBorderSize(0);
1392    coverpad->SetFrameFillColor(0);
1393    coverpad->Modified();
1394 <  main_canvas->cd();
1394 >  Ratio_main_canvas->cd();
1395    bottompad->SetTopMargin(ratiotopmargin);
1396    bottompad->SetBottomMargin(ratiobottommargin);
1397    bottompad->Draw();
# Line 1393 | Line 1400 | void save_with_ratio(TH1F *nominator, TH
1400    bottompad->SetFillColor(kWhite);
1401    TH1F *ratio = (TH1F*)nominator->Clone(GetNumericHistoName().c_str());
1402    ratio->Divide(denominator);
1396
1397
1403    TGraphAsymmErrors *eratio;
1404    TH1F *SystDown;
1405    TH1F *SystUp;
# Line 1420 | Line 1425 | void save_with_ratio(TH1F *nominator, TH
1425       }
1426      
1427    }
1423  eratio->SetFillColor(TColor::GetColor("#00ADE1"));
1428    
1429 <
1429 >  if(syshisto && !do_bpred_ratio) {
1430 >       SystDown = (TH1F*)syshisto->Clone("SystDown");
1431 >       SystUp = (TH1F*)syshisto->Clone("SystUp");
1432 >       for(int i=1;i<=syshisto->GetNbinsX();i++) {
1433 >         SystDown->SetBinContent(i,1-syshisto->GetBinContent(i));
1434 >         SystUp->SetBinContent(i,1+syshisto->GetBinContent(i));
1435 >       }
1436 >       SystDown->SetLineColor(TColor::GetColor("#006DE1"));
1437 >       SystUp->SetLineColor(TColor::GetColor("#006DE1"));
1438 >  }
1439 >  eratio->SetFillColor(TColor::GetColor("#00ADE1"));
1440    ratio->SetTitle("");
1441    ratio->GetYaxis()->SetRangeUser(0.0,2.0);
1442    if(do_bpred_ratio) ratio->GetYaxis()->SetRangeUser(0.0,2.0);
# Line 1440 | Line 1454 | void save_with_ratio(TH1F *nominator, TH
1454    ratio->GetYaxis()->SetNdivisions(502,false);
1455    ratio->SetFillColor(TColor::GetColor("#58D3F7"));
1456    ratio->SetMarkerSize(0);
1457 <  ratio->Draw("e2");
1457 >  ratio->Draw("e20");
1458 >  
1459 >  TGraphAsymmErrors *ratio_center = (TGraphAsymmErrors*)eratio->Clone("ratio_center");
1460 >  for(int i=0;i<ratio_center->GetN();i++) {
1461 >    ratio_center->SetPointError(i,ratio_center->GetErrorXlow(i),ratio_center->GetErrorXhigh(i),0.005,0.005);
1462 >  }
1463 >  ratio_center->SetFillColor(TColor::GetColor("#006381"));
1464 >  
1465    if(syshisto!=0) {
1466   //    sysratio->Draw("2");
1467   //    eratio->Draw("2,same");
1468 <    eratio->Draw("2");
1468 >    eratio->Draw("20");
1469      SystDown->Draw("histo,same");
1470      SystUp->Draw("histo,same");
1471    } else {
1472 <    eratio->Draw("2");
1472 >    eratio->Draw("20");
1473    }
1474 +  ratio_center->Draw("20");
1475    ratio->Draw("same,axis");
1476    TLine *oneline = new TLine(ratio->GetXaxis()->GetBinLowEdge(1),1,ratio->GetXaxis()->GetBinLowEdge(ratio->GetNbinsX())+ratio->GetXaxis()->GetBinWidth(ratio->GetNbinsX()),1);
1477    oneline->SetLineStyle(2);
1478    oneline->SetLineColor(kBlue);
1479    oneline->Draw("same");
1480 <
1481 <  main_canvas->cd();
1482 <  main_canvas->Modified();
1483 <  main_canvas->cd();
1484 <  main_canvas->SetSelected(main_canvas);
1480 >  if(PlottingSetup::DrawMetSignalRegionMllLines) {
1481 >    cout << "Drawing extra lines in ratio this time around ... " << endl;
1482 >    float RatioYMax=2.0;
1483 >    if(extendrange) RatioYMax=4.0;
1484 >    
1485 >    TLine *SRline   = new TLine(70,0,70,RatioYMax);
1486 >    TLine *ZLowLine = new TLine(91.2-PlottingSetup::ConsideredZWidth,0,91.2-PlottingSetup::ConsideredZWidth,RatioYMax);
1487 >    TLine *ZHiLine  = new TLine(91.2+PlottingSetup::ConsideredZWidth,0,91.2+PlottingSetup::ConsideredZWidth,RatioYMax);
1488 >  
1489 >    SRline->SetLineStyle(2);
1490 >    ZLowLine->SetLineStyle(2);
1491 >    ZHiLine->SetLineStyle(2);
1492 >    SRline->SetLineColor(kGray+2);
1493 >    ZLowLine->SetLineColor(kGray+2);
1494 >    ZHiLine->SetLineColor(kGray+2);
1495 >    SRline->Draw();
1496 >    ZLowLine->Draw();
1497 >    ZHiLine->Draw();
1498 >  }
1499 >      
1500 >  Ratio_main_canvas->cd();
1501 >  Ratio_main_canvas->Modified();
1502 >  Ratio_main_canvas->cd();
1503 >  Ratio_main_canvas->SetSelected(Ratio_main_canvas);
1504    
1505 <  CompleteSave(main_canvas,savemeas+"_withratio");
1505 >  CompleteSave(Ratio_main_canvas,savemeas+"_withratio");
1506    bottompad->cd();
1507    
1508    Double_t chi2;
# Line 1489 | Line 1530 | void save_with_ratio(TH1F *nominator, TH
1530    CompleteRatio->SetTextAlign(31);
1531    CompleteRatio->Draw();
1532    
1533 <  CompleteSave(main_canvas,savemeas+"_withratio_and_Chi2");
1533 >  CompleteSave(Ratio_main_canvas,savemeas+"_withratio_and_Chi2");
1534  
1535   //  float KS   = nominator->KolmogorovTest(denominator);
1536   //  stringstream KStext;
1537   //  Chi2text << "KS = " << KS << endl;
1538   //cout << "Found : " << KStext.str() << endl;
1539  
1540 +  delete eratio;
1541 +  delete ratio_center;
1542 +  delete ratio;
1543 +  delete Ratio_main_canvas;
1544 + }
1545  
1546 <  delete main_canvas;
1546 > void Save_With_Ratio_And_Line(TH1F *nominator, TH1F *denominator, TVirtualPad *orig_canvas, string savemeas, bool do_bpred_ratio=false, bool extendrange=false, string yaxistitle="ratio",TH1F *syshisto=NULL) {
1547 >  PlottingSetup::ConsideredZWidth=10.0;
1548 >  PlottingSetup::DrawMetSignalRegionMllLines=true;
1549 >  Save_With_Ratio(nominator, denominator, orig_canvas, savemeas, do_bpred_ratio, extendrange, yaxistitle,syshisto);
1550 >  PlottingSetup::DrawMetSignalRegionMllLines=false;
1551 > }
1552 >  
1553 > void Save_With_Ratio_And_Line(TH1F *nominator, THStack denominator, TVirtualPad *canvas, string savemeas, bool do_bpred_ratio=false) {
1554 >  TH1F *denominator_histo = (TH1F*) CollapseStack(denominator,"TemporaryStack");
1555 >  Save_With_Ratio_And_Line(nominator, denominator_histo, canvas, savemeas, do_bpred_ratio);
1556 >  delete denominator_histo;
1557   }
1558  
1559 < void save_with_ratio_and_sys_band(TH1F *nominator, TH1F *denominator, TVirtualPad *canvas, string savemeas, float systematic, string yaxistitle="SysRatio") {
1559 > void save_with_ratio_and_sys_band(float ConsideredZWidth, TH1F *nominator, TH1F *denominator, TVirtualPad *orig_canvas, string savemeas, bool do_bpred_ratio=false, bool extendrange=false, string yaxistitle="ratio",TH1F *syshisto=NULL) {
1560    //this function saves the pad being passed as well as a new one including the SysRatio.
1561 <  CompleteSave(canvas,savemeas);
1561 >  orig_canvas->cd();
1562 >  orig_canvas->Update();
1563 >  CompleteSave(orig_canvas,savemeas);
1564 >  
1565 >  TVirtualPad *canvas = (TVirtualPad*) orig_canvas->Clone("TempCanvas");//otherwise the Ratio_main_canvas will own our pad and destroy it upon deletion
1566  
1567    float bottommargin=gStyle->GetPadBottomMargin();
1568    float canvas_height=gStyle->GetCanvasDefH();
1569    float canvas_width=gStyle->GetCanvasDefW();
1570 <  float SysRatiospace=0.25;// space the SysRatio should take up (relative to original pad)
1570 >  float ratiospace=0.25;// space the ratio should take up (relative to original pad)
1571    
1572 <  float SysRatiobottommargin=0.3;
1573 <  float SysRatiotopmargin=0.1;
1572 >  float ratiobottommargin=0.3;
1573 >  float ratiotopmargin=0.1;
1574 >  
1575 >  float xstretchfactor=((1-ratiospace)*(1-gStyle->GetPadTopMargin()))/((1)*ratiospace);
1576 >  
1577 >  TCanvas *Ratio_main_canvas = new TCanvas("Ratio_main_canvas","Ratio_main_canvas",(Int_t)canvas_width,(Int_t)(canvas_height*(1+ratiospace)));
1578 >  TPad *mainpad = new TPad("mainpad","mainpad",0,1-(1.0/(1+ratiospace)),1,1);//top (main) pad
1579 >  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
1580 >  TPad *bottompad = new TPad("bottompad", "Ratio Pad",0,0,1,(1-(1-bottommargin)/(1+ratiospace))-0.015); //bottom pad
1581    
1582 <  float xstretchfactor=((1-SysRatiospace)*(1-gStyle->GetPadTopMargin()))/((1)*SysRatiospace);
1582 >  Ratio_main_canvas->Range(0,0,1,1);
1583 >  Ratio_main_canvas->SetBorderSize(0);
1584 >  Ratio_main_canvas->SetFrameFillColor(0);
1585    
1586 <  TCanvas *MainCanvas = new TCanvas("MainCanvas","MainCanvas",(Int_t)canvas_width,(Int_t)(canvas_height*(1+SysRatiospace)));
1587 <  TPad *MainPad = new TPad("MainPad","MainPad",0,1-(1.0/(1+SysRatiospace)),1,1);//top (main) pad
1588 <  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
1589 <  TPad *BottomPad = new TPad("BottomPad", "Ratio Pad",0,0,1,(1-(1-bottommargin)/(1+SysRatiospace))-0.015); //bottom pad
1590 <  
1591 <  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);
1586 >  mainpad->Draw();
1587 >  mainpad->cd();
1588 >  mainpad->Range(0,0,1,1);
1589 >  mainpad->SetFillColor(kWhite);
1590 >  mainpad->SetBorderSize(0);
1591 >  mainpad->SetFrameFillColor(0);
1592    canvas->Range(0,0,1,1);
1593    canvas->Draw("same");
1594 <  MainPad->Modified();
1595 <  MainCanvas->cd();
1596 <  CoverPad->Draw();
1597 <  CoverPad->cd();
1598 <  CoverPad->Range(0,0,1,1);
1599 <  CoverPad->SetFillColor(kWhite);
1600 <  CoverPad->SetBorderSize(0);
1601 <  CoverPad->SetFrameFillColor(0);
1602 <  CoverPad->Modified();
1603 <  MainCanvas->cd();
1604 <  BottomPad->SetTopMargin(SysRatiotopmargin);
1605 <  BottomPad->SetBottomMargin(SysRatiobottommargin);
1606 <  BottomPad->Draw();
1607 <  BottomPad->cd();
1608 <  BottomPad->Range(0,0,1,1);
1609 <  BottomPad->SetFillColor(kWhite);
1610 <  TH1F *SysRatio = (TH1F*)nominator->Clone(GetNumericHistoName().c_str());
1611 <  SysRatio->Divide(denominator);
1594 >  mainpad->Modified();
1595 >  Ratio_main_canvas->cd();
1596 >  coverpad->Draw();
1597 >  coverpad->cd();
1598 >  coverpad->Range(0,0,1,1);
1599 >  coverpad->SetFillColor(kWhite);
1600 >  coverpad->SetBorderSize(0);
1601 >  coverpad->SetFrameFillColor(0);
1602 >  coverpad->Modified();
1603 >  Ratio_main_canvas->cd();
1604 >  bottompad->SetTopMargin(ratiotopmargin);
1605 >  bottompad->SetBottomMargin(ratiobottommargin);
1606 >  bottompad->Draw();
1607 >  bottompad->cd();
1608 >  bottompad->Range(0,0,1,1);
1609 >  bottompad->SetFillColor(kWhite);
1610 >  TH1F *ratio = (TH1F*)nominator->Clone(GetNumericHistoName().c_str());
1611 >  ratio->Divide(denominator);
1612  
1613  
1614 +  TGraphAsymmErrors *eratio;
1615 +  TGraphAsymmErrors *SysEnvelope = new TGraphAsymmErrors();
1616 +  
1617 +  if(syshisto && !do_bpred_ratio) {
1618 +       for(int i=1;i<=syshisto->GetNbinsX();i++) {
1619 +         float dx=syshisto->GetBinWidth(i)/2.0;
1620 +         float dy=syshisto->GetBinContent(i);
1621 +         if(dy<0.01) dy=0.07;
1622 +         SysEnvelope->SetPoint(i-1,syshisto->GetBinCenter(i),1.0);
1623 +         SysEnvelope->SetPointError(i-1,dx,dx,dy,dy);
1624 +       }
1625 +       SysEnvelope->SetFillColor(TColor::GetColor("#006DE1"));
1626 +  }
1627  
1628 <  SysRatio->SetTitle("");
1629 <  SysRatio->GetYaxis()->SetRangeUser(0.0,2.0);
1630 <  SysRatio->GetXaxis()->SetTitle(nominator->GetXaxis()->GetTitle());
1631 <  SysRatio->GetXaxis()->CenterTitle();
1632 <  SysRatio->GetYaxis()->SetTitle(yaxistitle.c_str());
1633 <  SysRatio->GetYaxis()->SetTitleOffset(0.4);
1634 <  SysRatio->GetYaxis()->CenterTitle();
1635 <  SysRatio->SetStats(0);
1636 <  SysRatio->GetXaxis()->SetLabelSize(xstretchfactor*SysRatio->GetXaxis()->GetLabelSize());
1637 <  SysRatio->GetYaxis()->SetLabelSize(xstretchfactor*SysRatio->GetYaxis()->GetLabelSize());
1638 <  SysRatio->GetXaxis()->SetTitleSize(xstretchfactor*gStyle->GetTitleSize());
1639 <  SysRatio->GetYaxis()->SetTitleSize(xstretchfactor*gStyle->GetTitleSize());
1640 <  SysRatio->GetYaxis()->SetNdivisions(502,false);
1641 <  SysRatio->SetFillColor(TColor::GetColor("#58D3F7"));
1642 <  TBox *sysbox = new TBox(SysRatio->GetXaxis()->GetBinLowEdge(1),1-systematic,SysRatio->GetXaxis()->GetBinLowEdge(SysRatio->GetNbinsX())+SysRatio->GetXaxis()->GetBinWidth(SysRatio->GetNbinsX()),1+systematic);
1643 <  sysbox->SetFillColor(TColor::GetColor("#82FA58")); // light green
1644 <  
1572 <  SysRatio->Draw("e1");
1573 <  sysbox->Draw();
1574 <  SysRatio->Draw("e1,same");
1575 <  SysRatio->Draw("e1,axis,same");
1628 >  ratio->SetTitle("");
1629 >  ratio->GetYaxis()->SetRangeUser(0.0,2.0);
1630 >  if(do_bpred_ratio) ratio->GetYaxis()->SetRangeUser(0.0,2.0);
1631 >  if(extendrange) ratio->GetYaxis()->SetRangeUser(0.0,4.0);
1632 >  ratio->GetXaxis()->SetTitle(nominator->GetXaxis()->GetTitle());
1633 >  ratio->GetXaxis()->CenterTitle();
1634 >  ratio->GetYaxis()->SetTitle(yaxistitle.c_str());
1635 >  ratio->GetYaxis()->SetTitleOffset(0.4);
1636 >  ratio->GetYaxis()->CenterTitle();
1637 >  ratio->SetStats(0);
1638 >  ratio->GetXaxis()->SetLabelSize(xstretchfactor*ratio->GetXaxis()->GetLabelSize());
1639 >  ratio->GetYaxis()->SetLabelSize(xstretchfactor*ratio->GetYaxis()->GetLabelSize());
1640 >  ratio->GetXaxis()->SetTitleSize(xstretchfactor*gStyle->GetTitleSize());
1641 >  ratio->GetYaxis()->SetTitleSize(xstretchfactor*gStyle->GetTitleSize());
1642 >  ratio->GetYaxis()->SetNdivisions(502,false);
1643 >  ratio->SetFillColor(TColor::GetColor("#58D3F7"));
1644 >  ratio->Draw("e0");
1645    
1646 <  TLine *oneline = new TLine(SysRatio->GetXaxis()->GetBinLowEdge(1),1,SysRatio->GetXaxis()->GetBinLowEdge(SysRatio->GetNbinsX())+SysRatio->GetXaxis()->GetBinWidth(SysRatio->GetNbinsX()),1);
1646 >  if(syshisto!=0) {
1647 >    SysEnvelope->SetFillColor(TColor::GetColor("#FE9A2E"));
1648 >    SysEnvelope->Draw("2,0,same");
1649 >    ratio->Draw("e0,same");
1650 >  } else {
1651 >    eratio->Draw("0");
1652 >  }
1653 >  ratio->Draw("same,axis");
1654 >  TLine *oneline = new TLine(ratio->GetXaxis()->GetBinLowEdge(1),1,ratio->GetXaxis()->GetBinLowEdge(ratio->GetNbinsX())+ratio->GetXaxis()->GetBinWidth(ratio->GetNbinsX()),1);
1655    oneline->SetLineStyle(2);
1656    oneline->SetLineColor(kBlue);
1657    oneline->Draw("same");
1658 +  
1659 +  
1660 +  
1661 +  if(ConsideredZWidth>0.1) {
1662 +    cout << "Drawing extra lines in ratio this time around ... " << endl;
1663 +    float RatioYMax=2.0;
1664 +    if(extendrange) RatioYMax=4.0;
1665 +    
1666 +    TLine *SRline   = new TLine(70,0,70,RatioYMax);
1667 +    TLine *ZLowLine = new TLine(91.2-ConsideredZWidth,0,91.2-ConsideredZWidth,RatioYMax);
1668 +    TLine *ZHiLine  = new TLine(91.2+ConsideredZWidth,0,91.2+ConsideredZWidth,RatioYMax);
1669 +  
1670 +    SRline->SetLineStyle(2);
1671 +    ZLowLine->SetLineStyle(2);
1672 +    ZHiLine->SetLineStyle(2);
1673 +    SRline->SetLineColor(kGray+2);
1674 +    ZLowLine->SetLineColor(kGray+2);
1675 +    ZHiLine->SetLineColor(kGray+2);
1676 +    SRline->Draw();
1677 +    ZLowLine->Draw();
1678 +    ZHiLine->Draw();
1679 +  }
1680 +
1681 +  Ratio_main_canvas->cd();
1682 +  Ratio_main_canvas->Modified();
1683 +  Ratio_main_canvas->cd();
1684 +  Ratio_main_canvas->SetSelected(Ratio_main_canvas);
1685 +  
1686 +  CompleteSave(Ratio_main_canvas,savemeas+"_withSysRatio");
1687 +  bottompad->cd();
1688 +  
1689 +  Double_t chi2;
1690 +  Int_t ndf,igood;
1691 +  Double_t chi2prob = MarcosChi2TestX(nominator,denominator,chi2,ndf,igood,"UW");
1692  
1693 <  MainCanvas->cd();
1694 <  MainCanvas->Modified();
1695 <  MainCanvas->cd();
1696 <  MainCanvas->SetSelected(MainCanvas);
1693 >  stringstream Chi2text;
1694 >  Chi2text << "#chi^{2} / ndf: " << chi2 << " / " << ndf;
1695 >  stringstream Chi2probtext;
1696 >  Chi2probtext << "#chi^{2} prob: " << chi2prob;
1697 >  TText* chi2box = write_text(0.02,0.11,Chi2text.str());
1698 >  chi2box->SetTextSize(0.08);
1699 >  chi2box->SetTextAlign(11);
1700 >  chi2box->Draw();
1701 >  TText* chi2probbox = write_text(0.02,0.04,Chi2probtext.str());
1702 >  chi2probbox->SetTextSize(0.08);
1703 >  chi2probbox->SetTextAlign(11);
1704 >  chi2probbox->Draw();
1705    
1706 <  CompleteSave(MainCanvas,savemeas+"_withSYSSysRatio");
1707 <  delete SysRatio;
1708 <  delete MainCanvas;
1706 >  stringstream CompRatio;
1707 >  CompRatio << "Ratio: " << nominator->Integral() << " / " << denominator->Integral() << " \n " << nominator->Integral()/denominator->Integral();
1708 >  TText *CompleteRatio = write_text(0.98,0.04,CompRatio.str());
1709 >  CompleteRatio->SetTextSize(0.08);
1710 >  CompleteRatio->SetTextAlign(31);
1711 >  CompleteRatio->Draw();
1712 >  
1713 >  CompleteSave(Ratio_main_canvas,savemeas+"_withSysRatio_and_Chi2");
1714 >  delete Ratio_main_canvas;
1715 >  delete ratio;
1716   }
1717  
1718 < TH1F* CollapseStack(THStack stack,TString hname="base") {
1718 > TH1F* CollapseStack(THStack stack,TString hname="CollapsedStack") {
1719     TH1F *bhist = ((TH1F*)((stack.GetHists())->At(0)));
1720     TH1F *basehisto = (TH1F*)bhist->Clone(hname);
1721     TIter next(stack.GetHists());
# Line 1603 | Line 1729 | TH1F* CollapseStack(THStack stack,TStrin
1729     return basehisto;
1730   }
1731  
1732 < void save_with_ratio(TH1F *nominator, THStack denominator, TVirtualPad *canvas, string savemeas, bool do_bpred_ratio=false) {
1733 <  save_with_ratio(nominator, CollapseStack(denominator), canvas, savemeas, do_bpred_ratio);
1732 > TH1F* CollapseStack(THStack *stack,TString hname="CollapsedStack") {
1733 >   TH1F *bhist = ((TH1F*)((stack->GetHists())->At(0)));
1734 >   TH1F *basehisto = (TH1F*)bhist->Clone(hname);
1735 >   TIter next(stack->GetHists());
1736 >   TH1F *h;
1737 >   int counter=0;
1738 >   while ((h=(TH1F*)next())) {
1739 >     counter++;
1740 >     if(counter==1) continue;
1741 >     basehisto->Add(h);
1742 >   }
1743 >   return basehisto;
1744 > }
1745 >
1746 > void Save_With_Ratio(TH1F *nominator, THStack denominator, TVirtualPad *canvas, string savemeas, bool do_bpred_ratio=false) {
1747 >  TH1F *denominator_histo = (TH1F*) CollapseStack(denominator);
1748 >  Save_With_Ratio(nominator, denominator_histo, canvas, savemeas, do_bpred_ratio);
1749 >  delete denominator_histo;
1750   }
1751  
1752   void flag_this_change(string function, int line, int checked=0) {
# Line 1789 | Line 1931 | string removefunnystring(string name) {
1931    name=ReplaceCharacter(name,"]","_");
1932    name=ReplaceCharacter(name,"{","_");
1933    name=ReplaceCharacter(name,"}","_");
1934 +  name=ReplaceCharacter(name,"(","_");
1935 +  name=ReplaceCharacter(name,")","_");
1936 +  name=ReplaceCharacter(name," ","_");
1937    name=ReplaceCharacter(name,".","_");
1938    name=ReplaceCharacter(name,",","_");
1939    name=ReplaceCharacter(name,";","_");
# Line 1803 | Line 1948 | string removefunnystring(string name) {
1948    return name;
1949   }
1950  
1951 + TPolyLine* GetFitUncertaintyShape(TF1 *fit, float low, float high,float x0,float x1) {
1952 +  int nPoints=1000;
1953 +  double x[nPoints+1];
1954 +  double y[nPoints];
1955 +  
1956 +  float par1=fit->GetParameter(0);
1957 +  float dpar1=fit->GetParError(0);
1958 +  float par2=fit->GetParameter(1);
1959 +  float dpar2=fit->GetParError(1);
1960 +    
1961 +  float step=(x1-x0)/(nPoints/2.0 -1);
1962 +  for(int i=0;i<nPoints/2.0;i++) {
1963 +    x[i]=x0+i*step;
1964 +    y[i]=(par1+x[i]*par2)    -    sqrt(dpar1*dpar1+dpar2*dpar2*x[i]*x[i]);
1965 +    x[nPoints-1-i]=x[i];
1966 +    y[nPoints-1-i]=(par1+x[i]*par2)    +    sqrt(dpar1*dpar1+dpar2*dpar2*x[i]*x[i]);
1967 +    if(y[i]<low) y[i]=low;
1968 +    if(y[i]>high) y[i]=high;
1969 +    if(y[nPoints-1-i]<low) y[nPoints-1-i]=low;
1970 +    if(y[nPoints-1-i]>high) y[nPoints-1-i]=high;
1971 +  }
1972 +  
1973 +  x[nPoints]=x[0];
1974 +  y[nPoints]=y[0];
1975 +  
1976 +  TPolyLine *l = new TPolyLine(nPoints+1,x,y);
1977 +  l->SetFillColor(TColor::GetColor("#A2A2FA"));
1978 +  l->SetLineColor(TColor::GetColor("#A2A2FA"));
1979 +  l->SetLineWidth(1);
1980 +  return l;
1981 + }  
1982 +
1983 + //code for ReplaceAll copied from
1984 + //http://stackoverflow.com/questions/5343190/c-how-do-i-replace-all-instances-of-of-a-string-with-another-string
1985 + string ReplaceAll(string str, string from, string to) {
1986 +    size_t start_pos = 0;
1987 +    while((start_pos = str.find(from, start_pos)) != std::string::npos) {
1988 +         str.replace(start_pos, from.length(), to);
1989 +         start_pos += to.length(); // ...
1990 +    }
1991 +    return str;
1992 + }
1993 +
1994 + TPolyLine* GetFitUncertaintyShape(TH1 *histo, string fitname, float low, float high) {
1995 +  TF1 *fit = (TF1*)histo->GetFunction(fitname.c_str());
1996 +  if(!fit) {
1997 +    cout << "CANNOT PRODUCE FIT UNCERTAINTY AS THERE IS NO FIT CALLED " << fitname << " IN " << histo->GetName() << endl;
1998 +    return NULL;
1999 +  }
2000 +  float x0=histo->GetBinLowEdge(1);
2001 +  float x1=histo->GetBinLowEdge(histo->GetNbinsX())+histo->GetBinWidth(histo->GetNbinsX());
2002 +  return GetFitUncertaintyShape(fit, low, high,x0,x1);
2003 + }
2004 +
2005 + TPolyLine* GetFitUncertaintyShape(TGraphErrors *gr, string fitname, float low, float high, float minx=-999, float maxx=-999) {
2006 +  TF1 *fit = (TF1*)gr->GetFunction(fitname.c_str());
2007 +  if(!fit) {
2008 +    cout << "CANNOT PRODUCE FIT UNCERTAINTY AS THERE IS NO FIT CALLED " << fitname << " IN " << gr->GetName() << endl;
2009 +    return NULL;
2010 +  }
2011 +  double x,y;
2012 +  gr->GetPoint(0,x,y);
2013 +  float min=x;
2014 +  gr->GetPoint(gr->GetN()-1,x,y);
2015 +  float max=x;
2016 +  if(minx>-999 && maxx>-999 && minx<maxx) {
2017 +    max=maxx;
2018 +    min=minx;
2019 +  }
2020 +  return GetFitUncertaintyShape(fit, low, high,min,max);
2021 + }
2022 +
2023   stringstream all_bugs;
2024  
2025   void bug_tracker(string function, int line, string description) {
# Line 1853 | Line 2070 | float generalizedpSTAR(float massmother,
2070          res=TMath::Sqrt(res)/(2*massmother);
2071          return res;
2072   }
2073 +
2074 + void FindMinMax(TGraphErrors *gr, float &min, float &max) {
2075 +  double x,y;
2076 +  gr->GetPoint(0,x,y);
2077 +  min=500*y;
2078 +  max=0.0002*y;
2079 +  for(int i=0;i<gr->GetN();i++) {
2080 +    gr->GetPoint(i,x,y);
2081 +    cout << "Point at " << x << "/" << y << " with error " << gr->GetErrorY(i) << endl;
2082 +    float potmin=y-1.5*gr->GetErrorY(i); // taking 1.5 times the error to allow for enough space to present the plot
2083 +    float potmax=y+1.5*gr->GetErrorY(i); // taking 1.5 times the error to allow for enough space to present the plot
2084 +    if(potmin<min) min=potmin;
2085 +    if(potmax>max) max=potmax;
2086 +  }
2087 + }
2088 +
2089 +
2090 + void CleanLegends() {
2091 +  for(int ihist=(int)PlottingSetup::FakeHistoHeap.size()-1;ihist>=0;ihist--) {
2092 +    PlottingSetup::FakeHistoHeap[ihist]->Delete();
2093 +    PlottingSetup::FakeHistoHeap.pop_back();
2094 +  }
2095 + }
2096 +
2097 + string DigitsAfterComma(float number, int digits) {
2098 +  float temp=number*pow(10.0,digits);
2099 +  temp=int(temp)/pow(10.0,digits);
2100 +  return any2string(temp);
2101 + }
2102 +
2103 +
2104 + Value GetYield(TH1F *histo, float min, float max) {
2105 +  float res=0.0;
2106 +  float reserr=0.0;
2107 +  for(int i=1;i<=histo->GetNbinsX();i++) {
2108 +      if(histo->GetBinLowEdge(i)+histo->GetBinWidth(i)<=min) continue;// considered in the next bin
2109 +      if(histo->GetBinLowEdge(i)>=max) continue;//above the threshold, out!
2110 + //      cout << "         added yield for bin [" << histo->GetBinLowEdge(i) << "," << histo->GetBinLowEdge(i)+histo->GetBinWidth(i) << "] which is " << histo->GetBinContent(i) << "   in GetYield" << endl;
2111 +      res+=histo->GetBinContent(i);
2112 +      reserr=sqrt(reserr*reserr+histo->GetBinError(i)*histo->GetBinError(i));
2113 +  }
2114 +  return Value(res,reserr);
2115 + }
2116 +
2117 + void ProduceYields(float min, float max, TH1F *data, THStack *stack) {
2118 +  dout << "   *************** <MC YIELDS> ********* " << endl;
2119 +  dout << "   Considering " << min << " < mll < " << max << "           " << endl;
2120 +  dout << "   Data : " << GetYield(data,min,max)  << endl;
2121 +  TIter nextSF(stack->GetHists());
2122 +  TH1F* h;
2123 +  while ( h = (TH1F*)nextSF() ) {
2124 +    dout << "   " << h->GetName() << " : " << GetYield(h,min,max)  << endl;
2125 +  }
2126 +  dout << "   *************** </MC YIELDS> ********* " << endl;
2127 + }
2128 +
2129 +  
2130 + void WriteYield(THStack *mc, float low, float high) {
2131 +  TH1F *h;
2132 +  TIter NextHisto(mc->GetHists());
2133 +  while ( h = (TH1F*)NextHisto() ) {
2134 +    float CurrBKG=0;
2135 +    float CurrBKGErr=0;
2136 +    for(int i=1;i<=h->GetNbinsX()+1;i++) {
2137 +      if(h->GetBinLowEdge(i)+h->GetBinWidth(i)<=low) continue;// considered in the next bin
2138 +      if(h->GetBinLowEdge(i)>=high) continue;//above the threshold, out!
2139 +      CurrBKG+=h->GetBinContent(i);
2140 +      CurrBKGErr=sqrt(CurrBKGErr*CurrBKGErr+h->GetBinError(i)*h->GetBinError(i));
2141 +    }
2142 +    dout << "      " << h->GetName() << " : " << CurrBKG << " +/- " << CurrBKGErr << endl;
2143 +  }
2144 + }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines