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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines