πηγαίοι κώδικες του αρχείου PDF
//------------------------------------------------------------------------- { auto c1 = new TCanvas("c","My Canvas",1200,600); auto h1 = new TH1D("h1","a 1D histo",50,-1,1); auto r = new TRandom( time(NULL) ); for (int i=0; i<5000;i++) h1->Fill( r->Gaus(0,0.2) ); h1->Draw(); c1->SaveAs("TH1-errors1.png"); h1->Draw("e"); c1->SaveAs("TH1-errors2.png"); h1->SetMarkerColor(3); h1->SetMarkerStyle(8); h1->SetMarkerSize(1); h1->Draw("e"); c1->SaveAs("TH1-errors3.png"); h1->SetLineColor(6); h1->SetLineWidth(3); h1->Draw("e"); c1->SaveAs("TH1-errors4.png"); h1->SetFillColor(kYellow-10); h1->Draw("ebar"); c1->SaveAs("TH1-errors5.png"); h1->SetFillStyle(3008); h1->Draw("ebar"); c1->SaveAs("TH1-errors6.png"); } //------------------------------------------------------------------------- { auto c1 = new TCanvas( "c", "My Canvas", 1200, 600); auto h1 = new TH1D( "h1", "a normal distribution", 50, 0, 10); auto h2 = new TH1D( "h2", "a Landau distribution", 50, 0, 10); auto r = new TRandom( time(NULL) ); for (int i=0; i<10000;i++) { h1->Fill( r->Gaus( 5, 1.5 ) ); // κανονική κατανομή h2->Fill( r->Landau( 3, 0.75 ) ); // κατανομή Landau } c1->Divide( 2, 1 ); h1->SetMarkerColor(3); h1->SetMarkerStyle(8); h1->SetMarkerSize(1); h1->SetLineColor(6); h1->SetLineWidth(3); c1->cd(1); h1->Draw("e"); h2->SetMarkerColor(3); h2->SetMarkerStyle(8); h2->SetMarkerSize(1); h2->SetLineColor(6); h2->SetLineWidth(3); c1->cd(2); h2->Draw("e"); c1->SaveAs("TH1-sbs.png"); } //------------------------------------------------------------------------- { auto c1 = new TCanvas( "c", "My Canvas", 1200, 600); auto h1 = new TH1D( "h1", "a normal distribution", 50, 0, 10); auto h2 = new TH1D( "h2", "a Landau distribution", 50, 0, 10); auto r = new TRandom( time(NULL) ); for (int i=0; i<10000;i++) { h1->Fill( r->Gaus( 5, 1.5 ) ); // κανονική κατανομή h2->Fill( r->Landau( 3, 0.75 ) ); // κατανομή Landau } c1->Divide( 2, 1 ); for ( int i : { 1, 2 } ) { char s[10]; sprintf( s, "h%d", i ); // κατασκευή ονόματος // αναζήτηση αντικειμένου βάσει ονόματος: TH1D * h = (TH1D *) gROOT->FindObject(s); h->SetMarkerColor(3); h->SetMarkerStyle(8); h->SetMarkerSize(1); h->SetLineColor(6); h->SetLineWidth(3); c1->cd(i); h->Draw("e"); } } //------------------------------------------------------------------------- { auto c1 = new TCanvas( "c", "My Canvas", 1200, 600); auto h1 = new TH1D( "h1", "normal distribution A", 100, 0, 10); auto h2 = new TH1D( "h2", "normal distribution B", 100, 0, 10); auto h3 = new TH1D( "h3", "normal distribution C", 100, 0, 10); auto h4 = new TH1D( "h4", "Landau distribution A", 100, 0, 10); auto h5 = new TH1D( "h5", "Landau distribution B", 100, 0, 10); auto h6 = new TH1D( "h6", "Landau distribution C", 100, 0, 10); auto r = new TRandom( time(NULL) ); for (int i=0; i<10000;i++) { h1->Fill( r->Gaus( 2, 0.5 ) ); // κανονική κατανομή A h2->Fill( r->Gaus( 5, 1.5 ) ); // κανονική κατανομή B h3->Fill( r->Gaus( 7.5, 0.35 ) ); // κανονική κατανομή C h4->Fill( r->Landau( 3, 0.75 ) ); // κατανομή Landau A h5->Fill( r->Landau( 4, 0.5 ) ); // κατανομή Landau B h6->Fill( r->Landau( 5, 0.2 ) ); // κατανομή Landau C } double max=0; for ( int i : { 1, 2, 3, 4, 5, 6 } ) { char s[10]; sprintf( s, "h%d", i ); TH1D * h = (TH1D *) gROOT->FindObject(s); h->SetMarkerColor(i); h->SetMarkerStyle(8); h->SetMarkerSize(1); h->SetLineColor(6); h->SetLineWidth(3); if (h->GetMaximum()>max) max=h->GetMaximum(); if (i==0) h->Draw("e"); else h->Draw("esame"); } h1->SetMaximum( max*1.05 ); h1->SetTitle("six distributions;x;events"); gStyle->SetOptStat(0); c1->Draw(); } //------------------------------------------------------------------------- { TCanvas * c1 = new TCanvas("c1","",1200,600); TH2D * h2 = new TH2D("h2","2D rotated gaussian distribution",100,-1,1,100,-1,1); for (int i=0; i<1000000;i++) { double x, y, u, v, theta=30*M_PI/180; x=gRandom->Gaus(0,0.3); y=gRandom->Gaus(0,0.1); u=x*cos(theta)-y*sin(theta); v=x*sin(theta)+y*cos(theta); h2->Fill( u , v ); } gStyle->SetOptStat(false); h2->Draw("scat"); // scatter plot h2->Draw("box"); // box plot h2->Draw("cont"); // colour contours h2->Draw("contz"); // colour contours with z-colour scale h2->Draw("lego"); // 3D LEGO plot h2->Draw("surf3"); // 3D surface with colour contours on top h2->Draw("surf3z"); // same, with z-colour scale } //------------------------------------------------------------------------- { ofstream outfile("MyData.txt"); auto r = new TRandom( time(NULL) ); for (int i=0; i<100000;i++) { outfile << r->Gaus( 2, 0.5 ) << endl; // κανονική κατανομή A outfile << r->Gaus( 5, 0.6 ) << endl; // κανονική κατανομή B outfile << r->Gaus( 8, 0.4 ) << endl; // κανονική κατανομή C outfile << r->Landau( 3, 0.7 ) << endl; // κατανομή Landau A } } //------------------------------------------------------------------------- { ifstream infile("MyData.txt"); if ( ! infile.is_open() ) { cout << "Σφάλμα κατά το άνοιγμα του αρχείου MyData.txt" << endl; gROOT->ProcessLine(".q"); // quit ROOT } auto c1 = new TCanvas( "c", "My Canvas", 1200, 800); auto h1 = new TH1D( "h1", "some distribution;x;events", 200, -5, 15); gStyle->SetOptStat(0); // απόκρυψη στατιστικών double x; int cnt=0; while ( infile >> x ) { h1->Fill(x); cnt++; } cout << "Διάβασα " << cnt << " αριθμούς." << endl; h1->SetMarkerColor(4); h1->SetMarkerStyle(8); h1->SetMarkerSize(1); h1->SetLineColor(6); h1->SetLineWidth(3); h1->Draw("e"); } //------------------------------------------------------------------------- { // Σκέδαση Rutherford: dσ/dΩ ∝ 1 / sin^4(theta/2) double pdf(double th) { // συνάρτηση κατανομής πιθανότητας, th σε ° return 1.0 / pow(sin(th*TMath::DegToRad()/2.0), 4); }; TRandom3 r( time(NULL) ); // γεννήτρια τυχαίων, αρχικοποίηση βάσει της ώρας const int N = 20000000; // αριθμός γεγονότων που θα παραχθούν string tit="Rutherford Scattering: #frac{d#sigma}{d#Omega} #propto #frac{1}{sin^{4}(#theta/2)}"; tit+=";#theta (#circ);Events"; TH1D *hTheta = new TH1D("hTheta", tit.c_str(), 180, 0, 180); // bins της 1° const double theta_min = 1; // ° , αποφυγή του μηδενός που οδηγεί σε απειρισμό double pdf_max = pdf(theta_min); // μέγιστη πιθανότητα στη μικρότερη γωνία for (int i=0; i
Fill(theta); // σε ° if ( (i+1)%(N/50)==0 ) cout << "-> παραγωγή " << i+1 << " γεγονότων από " << N << " (" << 100.*(i+1)/N << "%)" << endl; } TCanvas *c = new TCanvas("c", "Rutherford Simulation", 800, 600); c->SetLogy(); // λογαριθμική κλίμακα στον άξονα y hTheta->SetMarkerColor(4); hTheta->SetMarkerStyle(8); hTheta->SetMarkerSize(0.75); hTheta->SetLineColor(6); hTheta->SetLineWidth(2); hTheta->Draw("e"); c->Draw(); c->GetListOfPrimitives()->ls(); auto t = (TPaveText*) c->GetPrimitive("title"); t->SetTextSize(0.05); t->SetX1NDC(0.15); t->SetY1NDC(0.75); t->SetX2NDC(0.70); t->SetY2NDC(0.90); c->SaveAs("rutherford.png"); c->SaveAs("rutherford.root"); } //------------------------------------------------------------------------- { auto c1 = new TCanvas("c1","cv",1200,600); double x[31], y[31]; for ( int i=0 ; i<31 ; i++ ) { x[i] = (i-15)*0.1; // [-1.5,1.5] y[i] = 20*sin(2*x[i]) - gRandom->Gaus(0,2); } TGraph * gr = new TGraph(31,x,y); gr->Draw("A*"); c1->SaveAs("TGraph1.png"); c1->Clear(); c1->Divide(2,2,0,0); //c1->Divide(2,2); c1->cd(1); gr->DrawClone("AL"); c1->cd(2); gr->DrawClone("AC"); gr->SetFillColor(42); c1->cd(3); gr->DrawClone("AB"); c1->cd(4); gr->DrawClone("AF"); c1->SaveAs("TGraph2.png"); } //------------------------------------------------------------------------- { auto c1 = new TCanvas("c1","cv",1200,600); double x[31], y[31], sx[31]={0}, sy[31]; for ( int i=0 ; i<31 ; i++ ) { x[i] = (i-15)*0.1; // [-1.5,1.5] y[i] = 20*sin(2*x[i]) - gRandom->Gaus(0,2); sy[i] = 1 + fabs(gRandom->Gaus(0,2)); } TGraph * gr = new TGraphErrors(31,x,y,sx,sy); gr->Draw("A*"); c1->SaveAs("TGraphErrors1.png"); } //------------------------------------------------------------------------- { // 40 προσομοιωμένα πειραματικά σημεία τάσης και ρεύματος για R=4.7kΩ double V[40], I[40], sV[40]={0}, sI[40]; for ( int i=0 ; i<40 ; i++) { V[i]=(i+1)/4.0; I[i]=V[i]/4.7 + gRandom->Gaus(0, 0.5/4.7); sI[i]=0.5/4.7 + 0.1*I[i]; } TGraphErrors *gr=new TGraphErrors(40, V, I, sV, sI); auto c1 = new TCanvas("c1", "A TGraphErrors object...", 1200, 600); c1->SetFillColor(41); c1->SetGrid(); gr->SetTitle ("Ohm's Law"); gr->GetXaxis()->SetTitle("V [V]"); gr->GetYaxis()->SetTitle("I [mA]"); gr->SetMarkerStyle(21); // τετράγωνα gr->SetMarkerSize(0.7); gr->SetMarkerColor(9); gr->SetLineColor(46); gr->SetLineWidth(2); gr->Draw ("AP"); // άξονες και σημεία c1->Draw(); c1->GetFrame()->SetFillColor(17); c1->SaveAs("Ohm.png"); } //------------------------------------------------------------------------- { double phimin=0; double phimax=TMath::Pi(); double phi[1000]; double r[1000]; TF1 *fp1 = new TF1("fp1", "cos(x)", phimin, phimax) ; for(int i=0; i<1000; ++i) { phi[i] = i*(phimax-phimin)/1000 + phimin; // γωνία r[i] = fp1->Eval(phi[i]); // ακτίνα } TCanvas *c = new TCanvas("c", "TGraphPolar Example", 600,600); TGraphPolar *gr = new TGraphPolar(1000, phi, r); gr->SetTitle("cos(#varphi) in polar coordinates: a circle!"); gr->SetLineColor(kRed); gr->SetLineWidth(3); gr->Draw ("AL"); //---------------------------------------- // Προσθέστε αυτές τις εντολές για σχεδίαση με κέντρο το 0 c->Draw(); gr->SetMinRadial(0); gr->SetMaxRadial(1); // <-- βάλτε τη σωστή τιμή του ρ_max gr->SetMinPolar(0); gr->SetMaxPolar(2*TMath::Pi()); //---------------------------------------- } //-------------------------------------------------------------------------