forked from UVaCompPhys/plotting_examples
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcpp_example2.cpp
More file actions
26 lines (25 loc) · 1.4 KB
/
Copy pathcpp_example2.cpp
File metadata and controls
26 lines (25 loc) · 1.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
#include "TApplication.h"
#include "TROOT.h"
#include "TH2F.h"
#include "TCanvas.h"
#include "TRandom3.h"
#include "TMath.h"
int main(int argc,char**argv){
int n=10000; TApplication app("App",&argc,argv); if(argc>1) n=atoi(argv[1]);
auto H=[](const char* a,const char* b){return new TH2F(a,b,100,50,150,100,50,150);};
TRandom3 tr;
TH2F* h1=H("h1","2D Gaussian;x;y");
for(int i=0;i<n;++i){ double x=tr.Gaus(100,6),y=tr.Gaus(100,6); if(50<=x&&x<=150&&50<=y&&y<=150) h1->Fill(x,y);}
TH2F* h2=(TH2F*)h1->Clone("h2"); h2->SetTitle("2D Gaussian + uniform;x;y");
for(int i=0;i<n/3;++i) h2->Fill(tr.Uniform(50,150),tr.Uniform(50,150));
TH2F* h3=(TH2F*)h1->Clone("h3"); h3->SetTitle("2D Gaussian + 1/r^2;x;y");
for(int i=0;i<n*10;++i){ double u=tr.Uniform(0,1-1.0/11.0),r=1.0/(1.0-u),th=tr.Uniform(0,2*TMath::Pi());
double x=100+10*r*cos(th),y=100+10*r*sin(th); if(50<=x&&x<=150&&50<=y&&y<=150) h3->Fill(x,y);}
TH2F* h4=(TH2F*)h1->Clone("h4"); h4->SetTitle("Mixture of 2D Gaussians;x;y");
for(int i=0;i<n/2;++i){ double x=tr.Gaus(100,20),y=tr.Gaus(100,20); if(50<=x&&x<=150&&50<=y&&y<=150) h4->Fill(x,y);}
TCanvas* c=new TCanvas("c2d_cpp","Canvas2D_cpp",900,900); c->Divide(2,2);
c->cd(1); h1->Draw("COLZ"); c->cd(2); h2->Draw("COLZ"); c->cd(3); h3->Draw("COLZ"); c->cd(4); h4->Draw("COLZ");
c->SaveAs("canvas2d_cpp.png");
if(!gROOT->IsBatch()){ app.SetIdleTimer(30,".q"); app.Run(true); }
return 0;
}