Ciao everyone!
I am desperately trying to add arrows and text in a canvas.
I don’t know why, but it’s not working.
I tried several ways, however none of them seems to be proper.
In the following code, arrow tries are indicated with //----> comments, while text tries are indicated with //~~~ comments.
The code I am using is:
# include "TCanvas.h"
# include "TROOT.h"
# include "TGraphErrors.h"
# include "TStyle.h"
# include "TMultiGraph.h"
# include "TF1.h"
# include "TLegend.h"
# include "TPaveStats.h"
# include "TArrow.h"
# include "TLatex.h"
# include "TPaveText.h"
# include "TText.h"
# include "TPavesText.h"
void gain(){
TCanvas *mycanvas = new TCanvas("c","c",600, 400);
mycanvas->SetFillColor(5);
mycanvas->SetFrameFillColor(10);
TMultiGraph * mg = new TMultiGraph("gain curve","GAIN CURVE");
// The values on the X,Y axes and error on Y axis
const int n_Fe=20;
double x_Fe[n_Fe]={560,565,570,575,580,585,590,595,600,605,610,615,620,625,630,635,640,645,650,655};
double y_Fe[n_Fe]={1056,1200,1392,1584,1824,2064,2304,2688,3024,3456,3984,4512,5088,5856,6624,7488,8640,9792,11280,12624};
double errorY_Fe[n_Fe];
for (int i=0; i<n_Fe; i++){
errorY_Fe[i]=192;
}
const int n_alpha=9;
double x_alpha[n_alpha]={500,535,540,545,550,555,560,565,570};
double y_alpha[n_alpha]={220,490,556,622,710,754,849,959,1098};
double errorY_alpha[n_alpha];
for (int i=0; i<n_alpha; i++){
errorY_alpha[i]=30;
}
gROOT->SetStyle("Plain");
// See: http://root.cern.ch/root/html/TStyle.html#TStyle:SetOptFit
gStyle->SetOptFit(1111);
TGraphErrors * gr1 = new TGraphErrors( n_Fe, x_Fe, y_Fe, NULL, errorY_Fe );
gr1->SetName("gr1");
gr1->SetTitle("Fe");
gr1->SetMarkerStyle(20);
gr1->SetMarkerColor(4);
gr1->SetDrawOption("AP");
gr1->SetLineColor(4);
gr1->SetLineWidth(1);
gr1->SetFillStyle(0);
TGraphErrors * gr2 = new TGraphErrors( n_alpha, x_alpha, y_alpha, NULL, errorY_alpha);
gr2->SetName("gr2");
gr2->SetTitle("alpha");
gr2->SetMarkerStyle(20);
gr2->SetMarkerColor(2);
gr2->SetDrawOption("P");
gr2->SetLineColor(2);
gr2->SetLineWidth(1);
gr2->SetFillStyle(0);
gr1->Fit("expo");
gr1->GetFunction("expo")->SetLineColor(gr1->GetLineColor());
gr1->GetFunction("expo")->SetLineWidth(2);
//gr1->Print("all");
gr2->Fit("expo");
gr2->GetFunction("expo")->SetLineColor(gr2->GetLineColor());
gr2->GetFunction("expo")->SetLineWidth(2);
mg->Add(gr1);
mg->Add(gr2);
mg->Draw("AP");
mg->SetTitle("Gain Curve for Fe X-rays and Po-210 alpha particles;Vmesh[V];Gain");
mycanvas->BuildLegend(0.15, 0.7, 0.4, 0.9,"Ar-CO2-->70%-30%");
mycanvas->SetLogy(1);
mycanvas->Modified(); mycanvas->Update(); // make sure it's really (re)drawn
TPaveStats *st = ((TPaveStats*)(gr1->GetListOfFunctions()->FindObject("stats")));
if (st) {
st->SetTextColor(gr1->GetLineColor());
st->SetX1NDC(0.64); st->SetX2NDC(0.99);
st->SetY1NDC(0.4); st->SetY2NDC(0.6);
}
st = ((TPaveStats*)(gr2->GetListOfFunctions()->FindObject("stats")));
if (st) {
st->SetTextColor(gr2->GetLineColor());
st->SetX1NDC(0.64); st->SetX2NDC(0.99);
st->SetY1NDC(0.15); st->SetY2NDC(0.35);
}
mycanvas->Modified(); mycanvas->Update(); // make sure it's really (re)drawn
//Draw an arrow in canvas
//---->TArrow* arrow = new TArrow(2,100,-1,50,0.01,"|>") ;
//---->arrow->SetLineColor(kRed) ;
//---->arrow->SetFillColor(kRed) ;
//---->arrow->SetLineWidth(3) ;
//---->mycanvas->addObject(arrow) ;
//---->TArrow *arrow = new TArrow(32,169,55,74,0.03,"|>");
//---->arrow->SetFillColor(1);
//---->arrow->SetFillStyle(1001);
//---->arrow->Draw();
//---->TArrow *arrow =new TArrow(0,0,50,50);
//---->arrow->Draw();
//---->TArrow ar1(0.1,0.1,0.1,0.7);
//---->ar1.Draw();
//---->TArrow ar2(0.2,0.1,0.2,0.7,0.05,"|>");
//---->ar2.SetAngle(40);
//---->ar2.SetLineWidth(2);
//---->ar2.Draw();
//---->TArrow arrow(0, 0, 0.5, 0.5, 0.05, ">")
//---->arrow.DrawClone();
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//Add some text in canvas
//~~~TLatex text(8.2,7.5,"#splitline{low rate}{sparks}");
//~~~text.DrawClone();
//~~~TLatex text(8.2,7.5,"#splitline{low rate}{sparks}");
//~~~text.Draw();
//~~~TPaveText *pt = new TPaveText(.05,.1,.95,.8);
//~~~pt->AddText("sparks");
//~~~pt->Draw();
//~~~t21 = new TText(0.05,0.8,"sparks");
//~~~t21->SetTextSize(0.1);
//~~~t21->Draw();
//~~~TPavesText text(0.5,0.5,4.5,3,5,"");
//~~~text.AddText("sparks");
//~~~text.Draw();
mycanvas->Modified(); mycanvas->Update(); // make sure it's really (re)drawn
//---->return mycanvas;
mycanvas->Print("Fe-alpha gain curves.pdf");
}