Skip to main content

Posts

Showing posts with the label root

How to Install CERN ROOT on Ubuntu: A Step-by-Step Guide

If you're involved in scientific research or data analysis, you've likely heard of CERN ROOT. Developed by CERN (the European Organization for Nuclear Research), ROOT is an essential toolkit for high-energy physics data analysis. In this guide, we'll walk you through the process of installing CERN ROOT on your Ubuntu machine.  Prerequisites Before we dive into the installation process, make sure you have the following prerequisites in place: A working Ubuntu machine (this guide is tested on Ubuntu 23.04, but it should work on other versions as well). Sudo privileges to install packages. Downloading CERN ROOT Start by downloading the latest version of CERN ROOT from the official website  or open your terminal and use the following command to download the source code: wget https://root.cern/download/root_vX.YY.Z.source.tar.gz Replace X.YY.Z with the version number you want to install (e.g., 6.22.08). Installing Dependencies CERN ROOT relies on several libraries and tools. T...

Use of TClonesArray Class

void write { TClonesArray * arr = new TClonesArray ( " TVector3 " ); TClonesArray & ar = * arr ; TFile * file = new TFile ( " file.root " , " recreate " ); TTree * tree = new TTree ( " tcl " , " tcl " ); tree -> Branch ( " array " , & arr ); TRandom2 * rand = new TRandom2 ( 1 ); for ( int i = 0 ; i < 100 ; i ++) { arr -> Clear (); for ( int j = 0 ; j < 1000 ; j ++) { double x = rand -> Rndm (); double y = rand -> Rndm (); double z = rand -> Rndm (); new ( ar [ j ]) TVector3 ( x , y , z ); } tree -> Fill (); } file -> Write (); file -> Close (); } void read () { TFile * file = new TFile ( " file.root " ); TTree * tree = ( TTree *) file -> Get ( " tcl " ); TClonesArray * arr = new ...

Example displaying two histograms and their ratio

// Example displaying two histograms and their ratio. void ratioplot() { // Define two gaussian histograms. Note the X and Y title are defined // at booking time using the convention "Hist_title ; X_title ; Y_title" TH1F *h1 = new TH1F ( "h1" , "Two gaussian plots and their ratio;x title; h1 and h2 gaussian histograms" , 100, -5, 5); TH1F *h2 = new TH1F ( "h2" , "h2" , 100, -5, 5); h1-> FillRandom ( "gaus" ); h2-> FillRandom ( "gaus" ); // Define the Canvas TCanvas *c = new TCanvas ( "c" , "canvas" , 800, 800); // Upper plot will be in pad1 TPad *pad1 = new TPad ( "pad1" , "pad1" , 0, 0.3, 1, 1.0); pad1-> SetBottomMargin (0); // Upper and lower plot are joined pad1-> SetGridx (); // Vertical grid pad1-> Draw (); // Draw the upper pad: pad1 pad1-> cd (); // pad1 becomes the c...

To build a polar graph in a square Canvas

void polar() { TCanvas* c = new TCanvas("myCanvas","myCanvas",600,600); double rmin=0; double rmax=TMath::Pi()*6; const int npoints=1000; Double_t r[npoints]; Double_t theta[npoints]; for (Int_t ipt = 0; ipt < npoints; ipt++)  { r[ipt] = ipt*(rmax-rmin)/npoints+rmin; theta[ipt] = TMath::Sin(r[ipt]); } TGraphPolar grP1 (npoints,r,theta); grP1.SetTitle("A Fan"); grP1.SetLineWidth(3); grP1.SetLineColor(2); grP1.DrawClone("AOL"); } The graph of a fan obtained with ROOT : Add caption

Builds a graph with errors, displays it and saves it as image

// Builds a graph with errors, displays it and saves it // as image. First, include some header files (within, // CINT these will be ignored). #include "TCanvas.h" #include "TROOT.h" #include "TGraphErrors.h" #include "TF1.h" #include "TLegend.h" #include "TArrow.h" #include "TLatex.h" void macro1() { // The values and the errors on the Y axis const int n_points=10; double x_vals[n_points]={1,2,3,4,5,6,7,8,9,10}; double y_vals[n_points]={6,12,14,20,22,24,35,45,44,53}; double y_errs[n_points]={5,5,4.7,4.5,4.2,5.1,2.9,4.1,4.8,5.43}; // Instance of the graph TGraphErrors graph(n_points,x_vals,y_vals,NULL,y_errs); graph.SetTitle("Measurement XYZ;lenght [cm];Arb.Units"); // Make the plot looks better graph.SetMarkerStyle(kOpenCircle); graph.SetMarkerColor(kBlue); graph.SetLineColor(kBlue); // The canvas on which we'll draw the graph TCanvas* mycanvas = new TCanvas(); // Draw the graph ! graph.DrawClone(...

Drawing the interference pattern of light falling on a grid

// Example drawing the interference pattern of light // falling on a grid with n slits and ratio r of slit // width over distance between slits. // function code in C double single(double *x, double *par) { double const pi=4*atan(1.); return pow(sin(pi*par[0]*x[0])/(pi*par[0]*x[0]),2); } double nslit0(double *x,double *par) { double const pi=4*atan(1.); return pow(sin(pi*par[1]*x[0])/sin(pi*x[0]),2); } double nslit(double *x, double *par) { return single(x,par) * nslit0(x,par); } // This is the main program void slits() { float r,ns; // request user input cout << "slit width / g ? "; scanf("%f",&r); cout << "# of slits ? "; scanf("%f",&ns); cout <<"interference pattern for "<< ns <<" slits, width/distance: "<<r<<endl; // define function and set options TF1 *Fnslit = new TF1("Fnslit",nslit,-5.001,5.,2); Fnslit->SetNpx(500); /...