#include #include "TCanvas.h" #include "TProfile.h" #include "TROOT.h" #include "TGraph.h" #include "TH2.h" #include "TGraphErrors.h" #include "TStyle.h" #include "TColor.h" #include "TRandom3.h" void rando() { gStyle->SetPalette(53); gStyle->SetErrorX(0.0001); gStyle->SetOptStat(0); const int nbin = 30, n_entr = 1000; double xmin = -5., xmax = 5., y1 = 0., y2 =10., x[n_entr], y[n_entr], sum, sumSqr, xval, yval; // for filling TH2F and TPtofile int counts; double vx[nbin], vy[nbin], sigma2Y[nbin], errX[nbin], errY[nbin]; //necessary for my own averaging code for TGraphErrors TRandom3 r; TH2F *hist = new TH2F("hist", "2D Hist", nbin, xmin, xmax, nbin, y1, y2); //2D hist TProfile *prof = new TProfile("prof", "Prof", nbin, xmin, xmax, y1, y2); //Profile TCanvas *canv = new TCanvas("canv", "Hist and Prof", 0, 0, 1000, 500); canv->Divide(3,1); double dx = (xmax - xmin)/n_entr; // step size double bin_width = (xmax-xmin)/nbin; // bin width needed for the last part (TGraphErrors) for(int i = 0; i <= n_entr; ++i) // loop filling TH2F and TProfile { x[i] = xmin + i*dx + dx/2; //arrays were not necessary but I needed them for another purpose y[i] = 10-r.Exp(4); hist->Fill(x[i], y[i]); prof->Fill(x[i], y[i]); } // drawing TH2F------------------------------------------ canv->cd(1); hist->SetTitle("TH2F"); hist->Draw("ALP"); hist->Draw("C"); canv->Modified(); canv->Update(); //drawing TProfile--------------------------------------- //Fails to connect poibts by smooth curve or even just "L" canv->cd(2); prof->SetTitle("TProfile"); prof->Draw("e1"); //prof->Draw("C"); prof->Draw("L"); prof->SetMarkerSize(0.5); prof->SetMarkerStyle(8); prof->SetLineStyle(1); prof->SetLineWidth(1); canv->Modified(); canv->Update(); //THIS PART IS NECESSARY TO FILL A TGRAPHERROR TO SHOW THAT IN THIS CASE A //SMOOTH CURVE OR ANY LINE WORKS JUST FINE //my averaging code-------------------------------------------- for(int j = 0; j < nbin; ++j) { counts = 0; sum = 0; //the y average sumSqr =0; //mean square for error calc errX[j] = 0; //setting X error to 0 vx[j] = xmin + j*bin_width + bin_width/2.; //vx is my x variable now xval = x[j]; while ( xval < xmin + (j+1)*bin_width ) //looping inside each bin { sum += y[counts+j]; // summing for mean sumSqr += pow(y[j+counts], 2); //summing for mean squared counts++; xval = x[j+counts]; } vy[j] = sum/counts; // average of each bin sigma2Y[j] = (sumSqr/counts-pow(vy[j],2)); //stand. dev. errY[j] = sqrt(sigma2Y[j]/counts); //stand. err. } //feeding vx and vy to TGraphErrors and //drawing----------------------------------------------------- TGraphErrors *gr = new TGraphErrors(nbin, vx, vy, errX, errY); canv->cd(3); gr->SetTitle("TGraph"); gr->Draw("AP"); gr->Draw("C"); // notice in this case "C" works gr->SetMarkerSize(0.5); gr->SetMarkerStyle(8); gr->SetLineStyle(1); gr->SetLineWidth(1); canv->Modified(); canv->Update(); }