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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines