/* FakePhysicist.com admin@fakephysicist.com 13 April 2017 compile with: g++ v-i_basic.cpp -o v-i_basic `root-config --cflags --libs` */ #include #include #include #include #include #include /****** Include the required ROOT Classes *****/ #include "TF1.h" #include "TStyle.h" #include "TPad.h" #include "TCanvas.h" #include "TGraph.h" #include "TPostScript.h" #include "TLatex.h" #include "TGaxis.h" #include "TAxis.h" using namespace std; int main() { ifstream infile("/media/surya/Surya_1/Online/FakePhysicist/1.v-i_basic/vidata_basic.txt"); // Input file name vector v_voltage, v_current; // Vector array to store data v_voltage.clear();v_current.clear(); while(!infile.eof()) { double voltage, current; infile >> voltage >> current; cout << " " << voltage << " " << current << endl; if(infile.eof()) {break;} // this one is for extra security. If the last line is not reading then add a extra empty line to the data file. v_voltage.push_back(voltage); v_current.push_back(current); } /************** Now starts the ROOT part ************/ /* Note. Distance units within the canvas are in % of the plots size. Any point on the canvas can be defined by a (x,y) where (0,0) is at the bottom left corner. And the value of x & y varies from 0. to 1. or the fraction of the plot dimension. */ // Setting the Pad/Plot Margins gStyle->SetPadBottomMargin(0.14); gStyle->SetPadTopMargin(0.09); gStyle->SetPadLeftMargin(0.14); gStyle->SetPadRightMargin(0.10); gStyle->SetOptFit(0); // Use 1111 in place of 0 to print statiscis box on the plot TGaxis::SetMaxDigits(3); // Create pointer of a scatter plot, named as TGraph in ROOT // using the above vector array TGraph *viplot = new TGraph(v_voltage.size(),&v_current[0],&v_voltage[0]); // Here, arguments are number of points, x data, y data // Now, we need to create a empty Canvas on which // the plots will be drawn TCanvas *can1 = new TCanvas("V-I Plot","viplot",1200,900); // Arguments: name, title, width and height of canvas // No space for title // Now create a ps file where the plots will be saved TPostScript *pss = new TPostScript("viplot.ps",111); // Now create a new page in that ps file pss->NewPage(); // Select the canvas on which you want to draw can1->cd(); // Then draw the graph viplot->Draw("AP*"); // AP* means it will print all the points as * // Now some cosmetic decoration viplot->SetTitle("Voltage VS Current Plot"); viplot->GetXaxis()->SetTitle("Current in A"); viplot->GetXaxis()->SetTitleSize(0.06); viplot->GetXaxis()->SetTitleOffset(1.0); viplot->GetXaxis()->CenterTitle(); viplot->GetXaxis()->SetLabelSize(0.06); viplot->GetXaxis()->SetLabelOffset(0.001); viplot->GetYaxis()->SetTitle("Voltage in V"); viplot->GetYaxis()->SetTitleSize(0.06); viplot->GetYaxis()->SetTitleOffset(0.9); viplot->GetYaxis()->CenterTitle(); viplot->GetYaxis()->SetLabelSize(0.06); viplot->GetYaxis()->SetLabelOffset(0.001); // Then fit the plot using straight line viplot->Fit("pol1"); // polN is a root defined N order polinomial function // Retreive the resistance value which is the slope // First, get the fit function from the fitted plot TF1 *fitfn1 = (TF1*)viplot->GetFunction("pol1"); // Then get the slope value and error value which is the // second constant/parameter double resistance = fitfn1->GetParameter(1); double reserror = fitfn1->GetParError(1); // Print the resistance value on the plot char tempchar[100]; sprintf(tempchar,"R = (%.0f #pm %.1f) #Omega",resistance,reserror); TLatex *Tl = new TLatex(0.4,0.25,tempchar); // Bottom left corner position Tl->SetNDC(kTRUE); Tl->Draw(); // Update the canvas, otherwise plot will not be written in ps can1->Update(); // Do not forget to close the ps file pss->Close(); return 0; // Now compile the code with the command mentioned above }