From root to c++ environment

I have a quick question… can I write the following code in c++? If there is any way, can you point me out which tutorial shoul I look at and which root libraries should I use?

I appreciate your comments.

The following code is very simple. It reads a file that contain two column of numbers and then plots those values and do a linear fit.

and example of the dat value that I have is something like this: (filename: “plotDt21rn5.dat”)

//begin of file:do not consider this line
3173 -19.105
3175 -18.868
3176 -18.510
3177 -18.003
3179 -17.480
//end of file: do not consider this line

this is my code in ROOT

{
gROOT->Reset();
const Int_t n = 5;

char fname[100], message[50];
ifstream in;
Int_t ring=2;
Int_t run=3173;
Int_t chann=21;
Double_t p, q;
Double_t x[n], y[n];
TF1 *myfit;

sprintf(fname,"/home/mosquera/analysis/ddvDAT/plotDt%drn5.dat",chann);
in.open(fname);

c1 = new TCanvas(“c1”,“Transmission”,20,50,1200,800);

c1->SetFillColor(kWhite);
c1->SetGrid();
myfit = new TF1(“myfit”,“pol2”, 3170, 3181);

for (Int_t i=0;i<n;i++){
in >> p >> q;
x[i] = p;
y[i] = q;
printf(" i %i %f %f \n",i,x[i],y[i]);
}

gr = new TGraph(n,x,y);
gr->SetLineColor(1);

gr->SetLineWidth(1);
gr->SetMarkerColor(4);
gr->SetMarkerStyle(25);
sprintf(message,“detector %d”,chann);

gr->Fit(“myfit”,“R”);//change
myfit->Draw(); //change

gr->SetTitle(message);
gr->GetXaxis()->SetTitle(“runs”);
gr->GetYaxis()->SetTitle(“Signal Output”);
gr->Draw(“CP”); //change

// TCanvas::Update() draws the frame, after
// which one can change it
c1->Update();
c1->GetFrame()->SetFillColor(kWhite);
c1->GetFrame()->SetBorderSize(2);
c1->Modified();
in.close();
}

I apreciate your comments and guidance,

never mind… I got it to work. I include:

#include “TH1.h”
#include “TF1.h”
#include “TProfile.h”
#include “TNtuple.h”
#include “TRandom.h”
#include “TGraph.h”
#include “TCanvas.h”
#include “TFrame.h”

and I remove the Reset() line and now its working as I wanted. Nice :slight_smile:

cheers,

Note that you can simplify your script as shown below.

Rene

void fpol2() {
Int_t chann=21;

c1 = new TCanvas(“c1”,“Transmission”,20,50,1200,800);

c1->SetFillColor(kWhite);
c1->SetGrid();
TF1 *myfit = new TF1(“myfit”,"[0]+[1]*x", 3170, 3181);

gr = new TGraph(“fpol2.dat”);
gr->SetLineColor(1);
gr->SetLineWidth(1);
gr->SetMarkerColor(4);
gr->SetMarkerStyle(25);

gr->Fit(“myfit”,“R”);
myfit->Draw();
myfit->SetTitle(Form(“detector %d”,chann));
myfit->GetXaxis()->SetTitle(“runs”);
myfit->GetYaxis()->SetTitle(“Signal Output”);
gr->Draw(“CP”);

// TCanvas::Update() draws the frame, after
// which one can change it
c1->Update();
c1->GetFrame()->SetFillColor(kWhite);
c1->GetFrame()->SetBorderSize(2);
c1->Modified();
}

Thanks for the advice… I found that TGraph class very useful!

Krisfrajer