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.25 by buchmann, Mon Dec 10 16:40:13 2012 UTC vs.
Revision 1.35 by buchmann, Mon Feb 25 15:36:39 2013 UTC

# Line 66 | Line 66 | namespace PlottingSetup {
66      bool is2012=true;
67      bool is53reco=true;
68      bool openBox = true;
69 +    vector<TH1F*> FakeHistoHeap;
70 +
71   }
72  
73   bool dopng=false;
# Line 259 | Line 261 | void ensure_directory_exists(string this
261    }
262   }
263  
264 + void DeleteStack(THStack mcstack) {
265 +  TH1F* h;
266 +  TIter nextHist(mcstack.GetHists());
267 +  while ( h = (TH1F*)nextHist() ) {
268 +    h->Delete();
269 +  }
270 +  delete h;
271 + }
272 +
273 +
274   void DeadEnd(string message) {
275    dout << endl;
276    dout << "\033[1;31m     _                _                  _  " << endl;
# Line 427 | Line 439 | void CompleteSave(TVirtualPad *can, stri
439  
440  
441   void setlumi(float l) {
442 < generaltoolboxlumi=l;
442 >  generaltoolboxlumi=l;
443   }
444  
445   int write_first_line(vector<vector<string> > &entries) {
# Line 1224 | Line 1236 | bool Contains(string wholestring, string
1236    else return false;
1237   }
1238  
1239 +
1240   //////////////////////////////////////////////////////////////////////////////
1241   //
1242   // http://stackoverflow.com/questions/669438/how-to-get-memory-usage-at-run-time-in-c
# Line 1236 | Line 1249 | bool Contains(string wholestring, string
1249   /* usage:
1250   double vm2, rss2;
1251   process_mem_usage(vm2, rss2);
1252 < cout << "Memory usage: VM: " << vm << "; RSS: " << rss << endl;
1252 > cout << "Memory usage: VM: " << vm2 << "; RSS: " << rss2 << endl;
1253   */
1254  
1255   void process_mem_usage(double& vm_usage, double& resident_set)
# Line 1295 | Line 1308 | TGraphAsymmErrors* produce_ratio_graph(T
1308   }
1309  
1310  
1311 + TText* WriteSelection(int njets) {
1312 +  string sel="Loose selection";
1313 +  if(njets==3) sel="Tight selection";
1314 +  assert(njets==2||njets==3);
1315 +  TText *sele = new TText(0.97,0.135,sel.c_str());
1316 +  sele->SetNDC(true);
1317 +  sele->SetTextColor(TColor::GetColor("#848484"));
1318 +  sele->SetTextFont(42);
1319 +  sele->SetTextAlign(32);
1320 +  sele->SetTextSize(0.03);
1321 +  sele->SetTextAngle(270);
1322 +  return sele;
1323 + }
1324 +
1325   Double_t MarcosChi2TestX(const TH1* h1, const TH1* h2, Double_t &chi2, Int_t &ndf, Int_t &igood, Option_t *option)
1326   {
1327  
# Line 1317 | Line 1344 | Double_t MarcosChi2TestX(const TH1* h1,
1344     return prob;
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) {
1347 > 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) {
1348    //this function saves the pad being passed as well as a new one including the ratio.
1349 <  CompleteSave(canvas,savemeas);
1350 <
1349 >  
1350 >  orig_canvas->cd();
1351 >  orig_canvas->Update();
1352 >  CompleteSave(orig_canvas,savemeas);
1353 >  TVirtualPad *canvas = (TVirtualPad*) orig_canvas->Clone("TempCanvas");//otherwise the Ratio_main_canvas will own our pad and destroy it upon deletion
1354 >  
1355    float bottommargin=gStyle->GetPadBottomMargin();
1356    float canvas_height=gStyle->GetCanvasDefH();
1357    float canvas_width=gStyle->GetCanvasDefW();
# Line 1331 | Line 1362 | void save_with_ratio(TH1F *nominator, TH
1362    
1363    float xstretchfactor=((1-ratiospace)*(1-gStyle->GetPadTopMargin()))/((1)*ratiospace);
1364    
1365 <  TCanvas *main_canvas = new TCanvas("main_canvas","main_canvas",(Int_t)canvas_width,(Int_t)(canvas_height*(1+ratiospace)));
1365 >  TCanvas *Ratio_main_canvas = new TCanvas("Ratio_main_canvas","Ratio_main_canvas",(Int_t)canvas_width,(Int_t)(canvas_height*(1+ratiospace)));
1366    TPad *mainpad = new TPad("mainpad","mainpad",0,1-(1.0/(1+ratiospace)),1,1);//top (main) pad
1367    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
1368    TPad *bottompad = new TPad("bottompad", "Ratio Pad",0,0,1,(1-(1-bottommargin)/(1+ratiospace))-0.015); //bottom pad
1369    
1370 <  main_canvas->Range(0,0,1,1);
1371 <  main_canvas->SetBorderSize(0);
1372 <  main_canvas->SetFrameFillColor(0);
1370 >  Ratio_main_canvas->Range(0,0,1,1);
1371 >  Ratio_main_canvas->SetBorderSize(0);
1372 >  Ratio_main_canvas->SetFrameFillColor(0);
1373    
1374    mainpad->Draw();
1375    mainpad->cd();
# Line 1349 | Line 1380 | void save_with_ratio(TH1F *nominator, TH
1380    canvas->Range(0,0,1,1);
1381    canvas->Draw("same");
1382    mainpad->Modified();
1383 <  main_canvas->cd();
1383 >  Ratio_main_canvas->cd();
1384    coverpad->Draw();
1385    coverpad->cd();
1386    coverpad->Range(0,0,1,1);
# Line 1357 | Line 1388 | void save_with_ratio(TH1F *nominator, TH
1388    coverpad->SetBorderSize(0);
1389    coverpad->SetFrameFillColor(0);
1390    coverpad->Modified();
1391 <  main_canvas->cd();
1391 >  Ratio_main_canvas->cd();
1392    bottompad->SetTopMargin(ratiotopmargin);
1393    bottompad->SetBottomMargin(ratiobottommargin);
1394    bottompad->Draw();
# Line 1367 | Line 1398 | void save_with_ratio(TH1F *nominator, TH
1398    TH1F *ratio = (TH1F*)nominator->Clone(GetNumericHistoName().c_str());
1399    ratio->Divide(denominator);
1400  
1370
1401    TGraphAsymmErrors *eratio;
1402    TH1F *SystDown;
1403    TH1F *SystUp;
# Line 1424 | Line 1454 | void save_with_ratio(TH1F *nominator, TH
1454    ratio->SetFillColor(TColor::GetColor("#58D3F7"));
1455    ratio->SetMarkerSize(0);
1456    ratio->Draw("e2");
1457 +  
1458 +  TGraphAsymmErrors *ratio_center = (TGraphAsymmErrors*)eratio->Clone("ratio_center");
1459 +  for(int i=0;i<ratio_center->GetN();i++) {
1460 +    ratio_center->SetPointError(i,ratio_center->GetErrorXlow(i),ratio_center->GetErrorXhigh(i),0.005,0.005);
1461 +  }
1462 +  
1463 +  ratio_center->SetFillColor(TColor::GetColor("#006381"));
1464 +  
1465    if(syshisto!=0) {
1466   //    sysratio->Draw("2");
1467   //    eratio->Draw("2,same");
# Line 1433 | Line 1471 | void save_with_ratio(TH1F *nominator, TH
1471    } else {
1472      eratio->Draw("2");
1473    }
1474 +  ratio_center->Draw("2");
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);
1481 >  Ratio_main_canvas->cd();
1482 >  Ratio_main_canvas->Modified();
1483 >  Ratio_main_canvas->cd();
1484 >  Ratio_main_canvas->SetSelected(Ratio_main_canvas);
1485    
1486 <  CompleteSave(main_canvas,savemeas+"_withratio");
1486 >  CompleteSave(Ratio_main_canvas,savemeas+"_withratio");
1487    bottompad->cd();
1488    
1489    Double_t chi2;
# Line 1472 | Line 1511 | void save_with_ratio(TH1F *nominator, TH
1511    CompleteRatio->SetTextAlign(31);
1512    CompleteRatio->Draw();
1513    
1514 <  CompleteSave(main_canvas,savemeas+"_withratio_and_Chi2");
1514 >  CompleteSave(Ratio_main_canvas,savemeas+"_withratio_and_Chi2");
1515  
1516   //  float KS   = nominator->KolmogorovTest(denominator);
1517   //  stringstream KStext;
1518   //  Chi2text << "KS = " << KS << endl;
1519   //cout << "Found : " << KStext.str() << endl;
1520  
1521 <
1522 <  delete main_canvas;
1521 >  delete eratio;
1522 >  delete ratio_center;
1523 >  delete ratio;
1524 >  delete Ratio_main_canvas;
1525   }
1526  
1527 < void save_with_ratio_and_sys_band(TH1F *nominator, TH1F *denominator, TVirtualPad *canvas, string savemeas, bool do_bpred_ratio=false, bool extendrange=false, string yaxistitle="ratio",TH1F *syshisto=NULL) {
1527 > void save_with_ratio_and_sys_band(TH1F *nominator, TH1F *denominator, TVirtualPad *orig_canvas, string savemeas, bool do_bpred_ratio=false, bool extendrange=false, string yaxistitle="ratio",TH1F *syshisto=NULL) {
1528    //this function saves the pad being passed as well as a new one including the SysRatio.
1529 <  CompleteSave(canvas,savemeas);
1529 >  orig_canvas->cd();
1530 >  orig_canvas->Update();
1531 >  CompleteSave(orig_canvas,savemeas);
1532 >  
1533 >  TVirtualPad *canvas = (TVirtualPad*) orig_canvas->Clone("TempCanvas");//otherwise the Ratio_main_canvas will own our pad and destroy it upon deletion
1534  
1535    float bottommargin=gStyle->GetPadBottomMargin();
1536    float canvas_height=gStyle->GetCanvasDefH();
# Line 1497 | Line 1542 | void save_with_ratio_and_sys_band(TH1F *
1542    
1543    float xstretchfactor=((1-ratiospace)*(1-gStyle->GetPadTopMargin()))/((1)*ratiospace);
1544    
1545 <  TCanvas *main_canvas = new TCanvas("main_canvas","main_canvas",(Int_t)canvas_width,(Int_t)(canvas_height*(1+ratiospace)));
1545 >  TCanvas *Ratio_main_canvas = new TCanvas("Ratio_main_canvas","Ratio_main_canvas",(Int_t)canvas_width,(Int_t)(canvas_height*(1+ratiospace)));
1546    TPad *mainpad = new TPad("mainpad","mainpad",0,1-(1.0/(1+ratiospace)),1,1);//top (main) pad
1547    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
1548    TPad *bottompad = new TPad("bottompad", "Ratio Pad",0,0,1,(1-(1-bottommargin)/(1+ratiospace))-0.015); //bottom pad
1549    
1550 <  main_canvas->Range(0,0,1,1);
1551 <  main_canvas->SetBorderSize(0);
1552 <  main_canvas->SetFrameFillColor(0);
1550 >  Ratio_main_canvas->Range(0,0,1,1);
1551 >  Ratio_main_canvas->SetBorderSize(0);
1552 >  Ratio_main_canvas->SetFrameFillColor(0);
1553    
1554    mainpad->Draw();
1555    mainpad->cd();
# Line 1515 | Line 1560 | void save_with_ratio_and_sys_band(TH1F *
1560    canvas->Range(0,0,1,1);
1561    canvas->Draw("same");
1562    mainpad->Modified();
1563 <  main_canvas->cd();
1563 >  Ratio_main_canvas->cd();
1564    coverpad->Draw();
1565    coverpad->cd();
1566    coverpad->Range(0,0,1,1);
# Line 1523 | Line 1568 | void save_with_ratio_and_sys_band(TH1F *
1568    coverpad->SetBorderSize(0);
1569    coverpad->SetFrameFillColor(0);
1570    coverpad->Modified();
1571 <  main_canvas->cd();
1571 >  Ratio_main_canvas->cd();
1572    bottompad->SetTopMargin(ratiotopmargin);
1573    bottompad->SetBottomMargin(ratiobottommargin);
1574    bottompad->Draw();
# Line 1537 | Line 1582 | void save_with_ratio_and_sys_band(TH1F *
1582    TGraphAsymmErrors *eratio;
1583    TGraphAsymmErrors *SysEnvelope = new TGraphAsymmErrors();
1584    
1540  if(!do_bpred_ratio) eratio = produce_ratio_graph(ratio);
1541  else {
1542     bool using_data=false;
1543     if((int)savemeas.find("Data")>0) using_data=true;
1544     eratio = histRatio(nominator,denominator,using_data,PlottingSetup::global_ratio_binning,false);
1545     if(syshisto!=0) {
1546       for(int i=1;i<=syshisto->GetNbinsX();i++) {
1547         SysEnvelope->SetPoint(i-1,syshisto->GetBinCenter(i),1.0);
1548         SysEnvelope->SetPointError(i-1,syshisto->GetBinWidth(i),syshisto->GetBinWidth(i),syshisto->GetBinContent(i),syshisto->GetBinContent(i));
1549       }
1550       SysEnvelope->SetFillColor(TColor::GetColor("#006DE1"));
1551     }
1552     for(int i=1;i<=ratio->GetNbinsX();i++) {
1553        ratio->SetBinContent(i,0);
1554        ratio->SetBinError(i,0);
1555     }
1556    
1557  }
1558  
1585    if(syshisto && !do_bpred_ratio) {
1586         for(int i=1;i<=syshisto->GetNbinsX();i++) {
1587 +         float dx=syshisto->GetBinWidth(i)/2.0;
1588 +         float dy=syshisto->GetBinContent(i);
1589 +         if(dy<0.01) dy=0.07;
1590           SysEnvelope->SetPoint(i-1,syshisto->GetBinCenter(i),1.0);
1591 <         SysEnvelope->SetPointError(i-1,syshisto->GetBinWidth(i),syshisto->GetBinWidth(i),syshisto->GetBinContent(i),syshisto->GetBinContent(i));
1591 >         SysEnvelope->SetPointError(i-1,dx,dx,dy,dy);
1592         }
1593         SysEnvelope->SetFillColor(TColor::GetColor("#006DE1"));
1594    }
# Line 1583 | Line 1612 | void save_with_ratio_and_sys_band(TH1F *
1612    ratio->Draw("e1");
1613    
1614    if(syshisto!=0) {
1586    ratio->Draw("");
1615      SysEnvelope->SetFillColor(TColor::GetColor("#FE9A2E"));
1616      SysEnvelope->Draw("2,same");
1617      ratio->Draw("e1,same");
# Line 1596 | Line 1624 | void save_with_ratio_and_sys_band(TH1F *
1624    oneline->SetLineColor(kBlue);
1625    oneline->Draw("same");
1626  
1627 <  main_canvas->cd();
1628 <  main_canvas->Modified();
1629 <  main_canvas->cd();
1630 <  main_canvas->SetSelected(main_canvas);
1627 >  Ratio_main_canvas->cd();
1628 >  Ratio_main_canvas->Modified();
1629 >  Ratio_main_canvas->cd();
1630 >  Ratio_main_canvas->SetSelected(Ratio_main_canvas);
1631    
1632 <  CompleteSave(main_canvas,savemeas+"_withSysRatio");
1632 >  CompleteSave(Ratio_main_canvas,savemeas+"_withSysRatio");
1633    bottompad->cd();
1634    
1635    Double_t chi2;
# Line 1628 | Line 1656 | void save_with_ratio_and_sys_band(TH1F *
1656    CompleteRatio->SetTextAlign(31);
1657    CompleteRatio->Draw();
1658    
1659 <  CompleteSave(main_canvas,savemeas+"_withSysRatio_and_Chi2");
1660 <
1661 <  delete main_canvas;
1659 >  CompleteSave(Ratio_main_canvas,savemeas+"_withSysRatio_and_Chi2");
1660 >  delete Ratio_main_canvas;
1661 >  delete ratio;
1662   }
1663  
1664 < TH1F* CollapseStack(THStack stack,TString hname="base") {
1664 > TH1F* CollapseStack(THStack stack,TString hname="CollapsedStack") {
1665     TH1F *bhist = ((TH1F*)((stack.GetHists())->At(0)));
1666     TH1F *basehisto = (TH1F*)bhist->Clone(hname);
1667     TIter next(stack.GetHists());
# Line 1647 | Line 1675 | TH1F* CollapseStack(THStack stack,TStrin
1675     return basehisto;
1676   }
1677  
1678 < void save_with_ratio(TH1F *nominator, THStack denominator, TVirtualPad *canvas, string savemeas, bool do_bpred_ratio=false) {
1679 <  save_with_ratio(nominator, CollapseStack(denominator), canvas, savemeas, do_bpred_ratio);
1678 > void Save_With_Ratio(TH1F *nominator, THStack denominator, TVirtualPad *canvas, string savemeas, bool do_bpred_ratio=false) {
1679 >  TH1F *denominator_histo = (TH1F*) CollapseStack(denominator);
1680 >  Save_With_Ratio(nominator, denominator_histo, canvas, savemeas, do_bpred_ratio);
1681 >  delete denominator_histo;
1682   }
1683  
1684   void flag_this_change(string function, int line, int checked=0) {
# Line 1833 | Line 1863 | string removefunnystring(string name) {
1863    name=ReplaceCharacter(name,"]","_");
1864    name=ReplaceCharacter(name,"{","_");
1865    name=ReplaceCharacter(name,"}","_");
1866 +  name=ReplaceCharacter(name,"(","_");
1867 +  name=ReplaceCharacter(name,")","_");
1868 +  name=ReplaceCharacter(name," ","_");
1869    name=ReplaceCharacter(name,".","_");
1870    name=ReplaceCharacter(name,",","_");
1871    name=ReplaceCharacter(name,";","_");
# Line 1879 | Line 1912 | TPolyLine* GetFitUncertaintyShape(TF1 *f
1912    return l;
1913   }  
1914  
1915 + //code for ReplaceAll copied from
1916 + //http://stackoverflow.com/questions/5343190/c-how-do-i-replace-all-instances-of-of-a-string-with-another-string
1917 + string ReplaceAll(string str, string from, string to) {
1918 +    size_t start_pos = 0;
1919 +    while((start_pos = str.find(from, start_pos)) != std::string::npos) {
1920 +         str.replace(start_pos, from.length(), to);
1921 +         start_pos += to.length(); // ...
1922 +    }
1923 +    return str;
1924 + }
1925  
1926   TPolyLine* GetFitUncertaintyShape(TH1 *histo, string fitname, float low, float high) {
1927    TF1 *fit = (TF1*)histo->GetFunction(fitname.c_str());
# Line 1974 | Line 2017 | void FindMinMax(TGraphErrors *gr, float
2017      if(potmax>max) max=potmax;
2018    }
2019   }
2020 +
2021 +
2022 + void CleanLegends() {
2023 +  for(int ihist=(int)PlottingSetup::FakeHistoHeap.size()-1;ihist>=0;ihist--) {
2024 +    PlottingSetup::FakeHistoHeap[ihist]->Delete();
2025 +    PlottingSetup::FakeHistoHeap.pop_back();
2026 +  }
2027 + }
2028 +
2029 + string DigitsAfterComma(float number, int digits) {
2030 +  float temp=number*pow(10.0,digits);
2031 +  temp=int(temp)/pow(10.0,digits);
2032 +  return any2string(temp);
2033 + }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines