//example of macro illustrating how to superimpose two histograms #include "TGaxis.h" #include "TRandom.h" #include #include #include #include #include #include #include #include #include #include "TROOT.h" #include "TGraphErrors.h" #include "TLegend.h" #include "TArrow.h" #include "TLatex.h" void plotfile() { //example of macro illustrating how to superimpose two histograms //with different scales in the "same" pad. // To see the output of this macro, click begin_html here end_html //Author: Rene Brun const Int_t n_lines = 5; // const Int_t n_lines = 445; double_t time_vals[n_lines]; double_t volt_vals[n_lines]; double_t volt_errs[n_lines]; double_t curr_vals[n_lines]; double_t curr_errs[n_lines]; //TFile *h = new TFile("File.root","RECREATE"); // FILE *f = fopen("2016-06-06_09-12-40_LeakageTest-FoilID_57_Long.dat","r"); FILE *f = fopen("example.dat","r"); Int_t i=0; while (!feof(f)){ Int_t rt = fscanf(f,"%lf %lf %lf %lf %lf\n",&time_vals[i],&volt_vals[i],&volt_errs[i],&curr_vals[i],&curr_errs[i]); i++; } TCanvas *c1 = new TCanvas("c1","hists with different scales",600,400); //create/fill draw h1 gStyle->SetOptStat(kFALSE); TGraphErrors * graph_volt = new TGraphErrors(n_lines,time_vals,volt_vals,nullptr,volt_errs); graph_volt->SetTitle("2016-06-06_09-12-40_LeakageTest-FoilID_57_Long;Time [s];Volts[V]"); // Aesthetics of graph_volt graph_volt->SetMarkerStyle(1); graph_volt->SetMarkerColor(kBlue); graph_volt->SetLineColor(kBlue); graph_volt->Draw("APEL"); graph_volt->SetMinimum(0); // be sure next graph is visible c1->Update(); TAxis * yaxis = graph_volt->GetHistogram()->GetYaxis(); Double_t ymax = yaxis->GetXmax(); Double_t ymin = yaxis->GetXmin(); printf("min %f max %f\n", ymin, ymax); // Instance of the second graph, for the current values TGraphErrors * graph_curr = new TGraphErrors(n_lines,time_vals,curr_vals,nullptr,curr_errs); // Aesthetics of graph_curr graph_curr->SetMarkerStyle(1); graph_curr->SetMarkerColor(kRed); graph_curr->SetLineColor(kRed); //scale hint1 to the pad coordinates // Float_t rightmax = 1.20; Double_t xx, yy; Double_t scale = 100; Double_t rightmax = ymax / scale; for (Int_t i=0; iGetN(); i++) { graph_curr->GetPoint(i, xx, yy); printf("%f %f\n", xx, yy); graph_curr->SetPoint(i, xx, yy*scale); xx = graph_curr->GetErrorX(i); yy = graph_curr->GetErrorY(i); graph_curr->SetPointError(i, xx, yy*scale); } graph_curr->Draw("PLE"); //draw an axis on the right side TGaxis *axis = new TGaxis(gPad->GetUxmax(),gPad->GetUymin(), gPad->GetUxmax(), gPad->GetUymax(),0,rightmax,510,"+L"); axis->SetLineColor(kRed); axis->SetLabelColor(kRed); axis->Draw(); c1->SetGridx(); c1->SetGridy(); c1->Modified(); c1->Update(); }