//Create and Draw a TMultiGraph //Author:: Rene Brun #include "TFile.h" #include "TCanvas.h" #include "TStyle.h" #include "TH1.h" #include "TH2.h" #include "TH3.h" #include "TGaxis.h" #include "TRandom.h" #include "TLegend.h" #include "TPaveStats.h" #include "TGraph.h" void multigraph() { gStyle->SetOptFit(); TCanvas *c36 = new TCanvas("c36","multigraph",700,500); //c36->SetGrid(); gStyle->SetOptFit(0001); // draw a frame to define the range TMultiGraph *mg = new TMultiGraph(); TGraph *gr1 = new TGraph("dati3.txt","%lg %*lg %lg %*lg %*lg %*lg %*lg %*lg %*lg %*lg"); gr1->SetMarkerColor(kBlue); gr1->SetMarkerStyle(21); gr1->Fit("pol1","q"); gr1->SetTitle("Energia primario- densita'"); gr1->GetXaxis()->SetTitle("Energia (GeV)"); gr1->GetYaxis()->SetTitle("Densita'(#/m^2)"); mg->Add(gr1); // create second graph TGraph*gr2 = new TGraph("dati3.txt","%lg %*lg %*lg %lg %*lg %*lg %*lg %*lg %*lg %*lg"); gr2->SetMarkerColor(kYellow); gr2->SetMarkerStyle(20); gr2->Fit("pol1","q"); mg->Add(gr2); mg->Draw("ap"); //force drawing of canvas to generate the fit TPaveStats c36->Update(); TLegend* leg = new TLegend(0.2, 0.7, .4, .85); leg->SetNColumns(1); leg->AddEntry(gr1, "distanza 10 m", "l"); leg->AddEntry(gr2, "distanza 100 m", "l"); leg->Draw("same"); TPaveStats *stats1 = (TPaveStats*)gr1->GetListOfFunctions()->FindObject("stats"); TPaveStats *stats2 = (TPaveStats*)gr2->GetListOfFunctions()->FindObject("stats"); if (stats1) { stats1->SetTextColor(kBlue); stats1->SetX1NDC(0.12); stats1->SetX2NDC(0.32); stats1->SetY1NDC(0.75); } if (stats2) { stats2->SetTextColor(kYellow); stats2->SetX1NDC(0.72); stats2->SetX2NDC(0.92); stats2->SetY1NDC(0.78); } c36->Modified(); c36.Print("gamma_ene-dens.pdf"); }