//* // Macro: osc_graph.C // // Crea grafici per le soluzioni numeriche dell'oscillatore armonico //* #include "TROOT.h" #include "TStyle.h" #include "TCanvas.h" #include "TH2F.h" #include "TGraph.h" #include "TLegend.h" #include #include #include #include #include using namespace std; //-- // Questa funzione ha lo stesso nome del file. // Viene eseguita tutte le volte che il file viene caricato dal prompt // di root con .L osc_graph.C oppure .L osc_graph.C+ //-- void osc_graph() { gROOT->SetStyle("Plain"); gStyle->SetOptStat(0); gStyle->SetOptTitle(kFALSE); gStyle->SetLabelOffset(0.01,"x"); gStyle->SetLabelOffset(0.005,"y"); gStyle->SetTitleOffset(1.4,"y"); gStyle->SetTitleOffset(1.4,"x"); } int read_datafile(const char *filename, vector & vtime, vector & vx, vector & vv, vector & venergy) { double value; if (filename == NULL) { cerr << "read_datafile() filename is NULL" << endl; return 1; } ifstream in(filename); if (! in.is_open() ) { cerr << "Cannot open data file \"" << filename << "\"\n"; return 2; } while ( ! in.eof() ) { string line_buffer; getline(in, line_buffer); // cout << " buffer: " << line_buffer << endl; // Skip all lines starting with # if (line_buffer.size() && line_buffer[0] != '#') { stringstream extr(line_buffer); extr >> value; vtime.push_back(value); // cout << "Time: " << value << " "; extr >> value; vx.push_back(value); // cout << "x: " << value << endl; extr >> value; vv.push_back(value); extr >> value; venergy.push_back(value); } } in.close(); return 0; } //-- // Plot the data //-- int plot_data(const char * f1="osc_euler_0.25.dat", const char * txt_f1="Eulero 0.25", const char * f2="osc_euler_cromer_0.25.dat", const char * txt_f2="Eulero Cromer 0.25", const char * f3="osc_euler_richardson_0.25.dat", const char * txt_f3="Eulero Richardson 0.25") { int ret = 0; vector v1_time, v1_x, v1_v, v1_energy; TGraph * g1 = NULL; vector v2_time, v2_x, v2_v, v2_energy; TGraph * g2 = NULL; vector v3_time, v3_x, v3_v, v3_energy; TGraph * g3 = NULL; if (f1) { ret = read_datafile(f1, v1_time, v1_x, v1_v, v1_energy); if (ret) { cerr << "read_datafile() error: " << ret << endl; return 1; } g1 = new TGraph( v1_time.size(), & v1_time[0], & v1_x[0]); // g1->GetXaxis()->SetTitle("time [s]"); // g1->GetYaxis()->SetTitle("position [a.u.]"); g1->SetMarkerColor(2); g1->SetLineColor(2); g1->SetMarkerStyle(28); g1->SetMarkerSize(0.9); } if (f2) { ret = read_datafile(f2, v2_time, v2_x, v2_v, v2_energy); if (ret) { cerr << "read_datafile() error: " << ret << endl; return 1; } g2 = new TGraph( v2_time.size(), & v2_time[0], & v2_x[0]); // g2->GetXaxis()->SetTitle("time [s]"); // g2->GetYaxis()->SetTitle("position [a.u.]"); g2->SetMarkerColor(8); g2->SetLineColor(8); g2->SetMarkerStyle(20); g2->SetMarkerSize(0.9); } if (f3) { ret = read_datafile(f3, v3_time, v3_x, v3_v, v3_energy); if (ret) { cerr << "read_datafile() error: " << ret << endl; return 1; } g3 = new TGraph( v3_time.size(), & v3_time[0], & v3_x[0]); // g3->GetXaxis()->SetTitle("time [s]"); // g3->GetYaxis()->SetTitle("position [a.u.]"); g3->SetMarkerColor(60); g3->SetLineColor(60); g3->SetMarkerStyle(3); g3->SetMarkerSize(0.9); } TCanvas * c3 = new TCanvas("c3","test"); gPad->SetTopMargin(0.01); gPad->SetBottomMargin(0.12); gPad->SetRightMargin(0.01); gPad->SetLeftMargin(0.12); TH2F * frame_1 = new TH2F("frame_1", "", 10, -0.2, 10.2, 10, -4., 4.); frame_1->Draw(); frame_1->GetXaxis()->SetTitle("time [s]"); frame_1->GetXaxis()->SetLabelSize(0.05); frame_1->GetYaxis()->SetTitle("position [a.u.]"); if (g1) g1->Draw("psame"); if (g2) g2->Draw("psame"); if (g3) g3->Draw("psame"); TLegend * leg = new TLegend(0.17,0.17,0.57,0.33); leg->SetHeader("Oscillatore Armonico"); if (g1) leg->AddEntry(g1, txt_f1, "ep"); if (g2) leg->AddEntry(g2, txt_f2, "ep"); if (g3) leg->AddEntry(g3, txt_f3, "ep"); leg->Draw(); c3->Update(); return 0; }