Tag Archive | Root

A Simple C++ Simulation For Beginners

Phenomenology in physics, mostly deals with the simulation of events and obtaining data from simulations to compare it with real time event datas. Obviously during event processing, it’s not necessary to give extra effort to visuality. So one should not confuse it with visual simulations. We’r just making event based calculations. Therefore you can ask what exactly do we simulate? or can any calculation be a simulation? Notice that in scientific experiments, you always need a satisfactory amount of statistics. So basically you should have a scenario for gathering statistics in simulations. Here i’d like to present a calculation of “Pi” number as a simulation sample. Here we are collecting statistics via producing random numbers which is included a circle with r=1.

Calculation: Pi number
The method: Monte Carlo Simulation
Fundamental Formulas: (pi)r 2 and x2+y2 = 1 (Note that radius of circle is unit 1.)

1-) Write below code and compile it writing “g++ pi.cpp -o pi.x”

#include<iostream>
#include<math.h>
#include<stdlib.h>
#include<time.h>

using namespace std;
int main(){
	int jmax=1000; // maximum value of HIT number. (Length of output file)
	int imax=1000; // maximum value of random numbers for producing HITs.
	double x,y;    // Coordinates
	int hit;       // storage variable of number of HITs
	srand(time(0));
	for (int j=0;j<jmax;j++){
		hit=0;
		x=0; y=0;
		for(int i=0;i<imax;i++){
			x=double(rand())/double(RAND_MAX);
			y=double(rand())/double(RAND_MAX);
		if(y<=sqrt(1-pow(x,2))) hit+=1; }          //Choosing HITs according to analytic formula of circle
	cout<<""<<4*double(hit)/double(imax)<<endl; }  // Print out Pi number
}

2-) To understand the code: We have just 2 loops here. The inner loop produce random number (<1) and uses these numbers for coordinates x,y. “If” condition increases hit number if this (x,y) point locates in the area of quarter circle (Look at the figure below.)

Hit Production Area

The outer loop resets our variables and print out Pi number according to formula: Area of Quarter Circle/ Area of Square = (1/4)πr2/r2 = (1/4)π= accepted hits / total hits = hits / imax.

3-) Run it as “./pi.x > pi.dat”

4-) Draw output file.

Pi Graph

I used below “root macro” to read and convert it to a .root file.

{
	gROOT->Reset();
	ifstream in;
	in.open("pi.dat");
	Float_t x; Int_t nlines = 0;
	TFile *f = new TFile("pi1.root","RECREATE");
	TH1F *h1 = new TH1F("h1","pi_grafik",100,2.5,4.0);
	TNtuple *ntuple = new TNtuple("ntuple","pi","x");
	for (nlines=0; nlines<10000; nlines++) {
		in >> x;
		if (!in.good()) {break;}
		if (nlines < 5) {printf("x=%5f\n",x);}
		h1->Fill(x);
		ntuple->Fill(x);
		nlines++;
	}
	in.close();
	f->Write();
	printf("%d deger bulundu\n",nlines);
	h1->SetXTitle("pi");
	h1->SetYTitle("Olay");
	h1->Draw();
}
  • Paste above root macro in a C file and name it as “pintuple.c”
  • Open your root analysis program in the same directory you saved pintuple.c : “root”
  • Execute the file: “root> .x pintuple.c”
  • You’ll get an ntuple file called “pi1.root”
  • Write “TBrowser g” in root.
  • Open pi1.root file and you’ll get the above histogram. Congratulations 🙂
Advertisements

Running ROOT on Lxplus Server of CERN

Mac OsX terminal window has powerful features than any other operating systems but if you are working on a remote server you need to know a little more about it.

If you are using MacOSX and if you are connecting lxplus@cern using regular command ssh, the first thing you will observe is that your system cannot open X11 window which is needed by ROOT. To overcome this problem, you should connect using either

ssh -X username@lxplus.cern.ch
or
ssh -Y username@lxplus.cern.ch

X option, provides you a ssh connection forwarding X11. And Y option, provides a ssh connection Forwarding trusted X11.

After you logged in using your user name and password, you have 2 options to run ROOT.

1 -) Entering ROOT variables below:

export ROOTSYS=/afs/cern.ch/sw/lcg/app/releases/ROOT/5.28.00/i686-slc5-gcc43-opt/root
export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:$ROOTSYS/lib
export PATH=$PATH:$ROOTSYS/bin

2-) Or just source Athena Framework and setup root from Athena: (Note that Athena consists much more than ROOT)

export AtlasSetup=/afs/cern.ch/atlas/software/releases/16.6.0/AtlasSetup
alias asetup='source $AtlasSetup/scripts/asetup.sh'
asetup 16.6.0

–Recently this procedure has been changed and became eaiser. Please google for sourcing Athena framework–

Then just write “root” and start your analysis.

Draw Feynman Diagrams Using ROOT

It is needless to say that ROOT Data Analysis Framework provides tons of great tools to write a scientific article. But it happened one more time again and i had a great respect for ROOT after i learned that one can also draw Feynman Diagrams as well.

Although there are alternative ways of drawing the diagrams of your scientific article, why not drawing them using the tool by which you made the analysis ? It’s easier!

Here i am presenting a little code to modify or to use directly just changing some variables. Actually i have just drown a simple S-Channel scattering and i’m leaving the rest of the work to you.

USAGE:

  • Copy the code below and paste it on a text file.
  • Click “Save as” and name the file as workDiagram.C (which is a name for ROOT macro)
  • Open your console and run ROOT writing “root” at the directory you saved workDiagram.C file
  • .L workDiagram.C (Load the file)
  • simpleSCh() (Run the function)
  • You will see something like a compton scattering diagram but the labels are wrong. You can change labels right clicking on them.
  • FEATURES:
    After running the script on the canvas that ROOT opened for you;

  • You can change the labels and their positions by right clicking on
  • You can rearrange the size of the picture by dragging the edge of the window
  • You can right click on the line and choose “SetLineAttributes” and at the “shape” tab you can rearrange its direction.
  • By Drag&Dropping you can change any of the elements on the screen
  • Click “File” and choose “Save as”. You will see that you can save your diagram almost any formats: ps, eps, svc, pdf, jpg, gif, xml, tiff, ..etc.
  • TIPS:

  • Drawing Higss Boson: Right Click on the line and choose “SetLineAttributes” and then make the line dashed choosing at combo box options.
  • Drawing Photon, Ws and Zs: Run script using the variables simpleSCh(“zigzag”,”zigzag”,”zigzag”,”zigzag”,”zigzag”). The first and second variables represents the incoming particles. The third one represents middle propagator and the last two one is outgoing particles.
  • Drawing gluons: Run script using the variables simpleSCh(“curly”,”curly”,”curly”,”curly”,”curly”).
  • Drawing fermions: Run script with default variables simpleSCh() or simpleSCH (“”,””,””,””,””)
    void simpleSCh(char *a="",char *b="",char *c="",char *d="",char *e="")
    {
    //Draw Feynman diagrams
    //Open Source ROOT Script Library
    //Author: Sinan Kuday
    
    TCanvas *c1 = new TCanvas("c1", "A canvas", 10,10, 500, 300);
    c1->SetFillColor(0);
    c1->Range(0, 0, 140, 60);
    Int_t linsav = gStyle->GetLineWidth();
    gStyle->SetLineWidth(3);
    TLatex t;
    t.SetTextAlign(20);
    t.SetTextSize(0.1);
    switch(a){
    case "zigzag":
    TCurlyLine *lt = new TCurlyLine(10,50,40,30); lt->SetWavy();
    break;
    case "curly":
    TCurlyLine *lt = new TCurlyLine(10,50,40,30);
    break;
    default:
    lt = new TArrow(10, 50, 40, 30, 0.03,"->|-"); 			break; 	} 	lt->Draw();
    
    switch (b) {
    case "zigzag":
    TCurlyLine *lb = new TCurlyLine(10, 10, 40, 30); lb->SetWavy();
    break;
    case "curly":
    TCurlyLine *lb = new TCurlyLine(10, 10, 40, 30);
    break;
    default:
    TArrow *lb; lb = new TArrow(10, 10, 40, 30, 0.03,"-|>-");
    break;
    }
    lb->Draw();
    
    t.DrawLatex(24,10,"e^{-}");
    t.DrawLatex(24,46,"e^{+}");
    
    switch (c) {
    case "zigzag":
    TCurlyLine *middle = new TCurlyLine(40, 30, 100, 30);
    middle->SetWavy();
    break;
    case "curly":
    TCurlyLine *middle = new TCurlyLine(40, 30, 100, 30);
    break;
    default:
    TArrow *middle; middle = new TArrow(40, 30, 100, 30, 0.03,"-|>-");
    break;
    }
    middle->Draw();
    
    t.DrawLatex(72,36,"#gamma");
    
    switch (d) {
    case "zigzag":
    TCurlyLine *rt = new TCurlyLine(100, 30, 130, 10);
    rt->SetWavy();
    break;
    case "curly":
    TCurlyLine *rt = new TCurlyLine(100, 30, 130, 10);
    break;
    default:
    TArrow *rt; rt = new TArrow(100, 30, 130, 10, 0.03,"-<|-"); 			break; 	} 	rt->Draw();
    
    switch (e) {
    case "zigzag":
    TCurlyLine *rb = new TCurlyLine(100, 30, 130, 50);
    rb->SetWavy();
    break;
    case "curly":
    TCurlyLine *rb = new TCurlyLine(100, 30, 130, 50);
    break;
    default:
    TArrow *rb; rb = new TArrow(100, 30, 130, 50, 0.03,"-<|-"); 			break; 	} 	rb->Draw();
    
    t.DrawLatex(113,10,"#bar{q}");
    t.DrawLatex(113,46,"q");
    
    c1->Update();
    gStyle->SetLineWidth(linsav);
    }
    
  • Installing Scientific Packages on Mac OS X

    If you decided to buy a Macbook, the first thing you need to know may be that “It’s not a Big Mac at all but rather it’s a Mac”. Don’t even think you can eat all the Mac Universe that goes around and is almost parallel to our universe. It even may take quite a lot of time to figure out what kind of a trouble you have, especially if you are someone in the scientific era. Don’t forget that some says “People who buy Macs are the same people who said BETA is better than VHS 15 years ago”.

    For an average physicist like me, it may be long term run to build a useful system on Macbook.
    Here is a list of softwares that a scientist may need:

    Compilers:
    gcc, g++, gfortran, g77, f77,Java, …etc.

    Package Managers:

    MacPorts, Fink, Darwin, RPM, Apt-get, …etc.

    Analysis Softwares:

    ROOT, Jas3, Aida, Octave, …etc.

    Computing Softwares:

    Mathematica, Matlab, …etc.

    Physics Phenomenology Softwares:

    Comphep, Calchep, Pythia, Fluka, Geant4, Madx,
    Isajet, Prospino …etc.

    Depended Packages:

    Cernlib, Openmotif, X11, Latex …etc.

    Office Tools:

    Excel, Text Editors, Presentation tools, PDF, DVI, PS viewers, …etc.

    Web Browsers

    Mozilla Firefox, Internet Explorer, Safari, Opera, Netscape, …etc.

    Next the main question comes up! How to install these softwares on Mac Os X 10.5.x ??

    Here is a prescription for beginners:
    (1). First of all, install compilers
    (2). Install some package managers
    (3). Install other software packages using compilers and installed package managers.

    (1)

    GCC Compiler Installation: Use Macbook installation DVD, you will find a package called Xcode in the DVD, if you search for it. Just install Xcode development tool and your gcc compiler will also comes with it. Then open a terminal window and just write “gcc”. If its error turns like “no input file”, it means you successfully installed the gcc compiler.
    Or you can check its location by writing “which gcc” on the command line.

    G77 Compiler Installation: Unfortunately you may not find g77 support combining with any other packages or package managers. So you should install it manually:

    • Download tar file which is most suitable for your computer g77-intel-bin.tar.gz (Intel Mac only) or g77-bin.tar.gz (PowerPC only)
    • Open a terminal window and write “gunzip g77-bin.tar.gz”
    • Write “sudo tar -xvf g77-bin.tar -C /.” for install the spesific files into your usr/bin/ folder.

    Gfortran Compiler Installation: There’s an dmg installation file for gfortran compiler. So you should download and install it by just clicking on it.

    (2)

    MacPorts Installation:

    MacPorts provides an open-source software for managing, compiling, installing, upgrading packages which include libraries, utilities on the Mac Os X operating system. One can easily install MacPorts downloading its dmg file and just click on it. After installation you can open a terminal and learn basic Macport commands writing “man port” . Recently for Mac users, MacPorts seem to be the most powerful package manager with almost 8000 packages. Some useful MacPorts commands are;

    • port list : Get the lists of all packages so that one can choose the package name before installation.
    • port upgrade <pkgname> : Upgrades the specific packages with newest versions.
    • port search <pkgname>: Search the package name (or a part of it) in the complete list of packages. You can make wild card searches like *root* or *X11*..etc.
    • port install <pkgname>: –Most used MacPort command- downloads, compiles, stages and installs the package you entered.
    • port installed: Shows the installed packages on your computer.
    • port uninstall <pkgname>: Deactivate the package.
    • port deps: Shows you the dependencies of a package with other packages.
    For complete list of commands, please read the documentation of MacPorts here.

    FINK Installation:

    Fink automates the process of downloading the binary package, or downloading source package, applying a patch, compling, and installing it. As a package manager, fink is the most advanced unix based software and one can easily find its documentations through web. There is an impressive number of applications you can install via fink. Fink was designed carefully as to not disturb or modify the system. It can be uninstalled with a single command ‘sudo rm -r /sw’ You definitely want f77, imagemagick, ghostscript for X, ispell, xdvi, and gimp if you want to edit graphics or xv to preview them.

    To download Fink visit http://www.finkproject.org and install its dmg file on your Macbook.

    • Open a terminal window and write “fink” to learn its usage and options.
    • Write “apt-get” (Fink also installs apt-get package)
    • Fink downloads packages from mirror servers around the world. So I strictly recommend you to configure mirror settings. Write “fink configure” to configure mirror options. Fink will start to ask you questions about configurations. Press “Enter” for default values. When it asks a question about mirror choose “4- Nearest mirrors from your continent”. Choose your country. And answer all other questions to complete configuration definitions. If you cannot download packages from chosen mirrors while installations, you should reconfigure these settings.
    • Fink supports stable packages as well as the unstable ones which are not tested or has some exceptions during their running process. To see the supported package list write “fink list” on your command line.
    • To be able to install unstable packages write “fink configure” and activate unstable package option when it asks you about stable/unstable packages. Press “Enter” to give default answers to other questions.
    • To download and install a package write “sudo fink install packagename” on the command line. (ex. “sudo fink install cernlib”)
    • WARNING: Fink automatically finds the dependencies of packages and usually downloads a set of package. That may probably extand your installation time. So i do not recommend anyone to download huge packages -ex. ROOT- via fink. Because it may take quite a lot of time with the related other packages like gcc4, X11, openmotif, …etc.

    (3)

    CERNLIB Installation:

    • First, make sure you installed fink and the compilers gcc, g77.
    • Open a terminal window and write “sudo fink install cernlib”
    • Fink will download and install cernlib in a few minutes.
    • Write “cernlib” on terminal window and see related cernlib locations. If you get an error message, make sure you installed fink and the compilers properly.

    OR you may prefer to install Cernlib Manually to avoid non-standard installation.

    COMPHEP Installation:

    • Visit Comphep’s website, be a member and then download its .tar files.
    • Make sure you installed the compilers gcc and g77 properly.
    • Open a terminal window in the folder that comphep’s .tar file exits.
    • Write “sudo tar -xvf comphep-4.5.1.tar”
    • Write “cd comphep-4.5.1”
    • Write “./configure”
    • Follow the commands and write “make” to complete installation.
    • Write “make setup WDIR=calculations” to create a user file called calculations.
    • To run the application write “cd calculations” and “./comphep”

    ROOT Installation:

    • Download ROOT analysis framewok visiting ROOT official web site at CERN. If you’r obsessed to perfection, you should download the latest version for your system. (Mac OS X intel or power pc)
    • To open the related .tar file write “sudo tar -xvf root_v5-1.24.00.macosx105-i386-gcc-4.0.tar” on the command line.
    • Write “cd rroot_v5-1.24.00.macosx105-i386-gcc-4.0”
    • Write “./confgure –help” and “./configure” to make installation settings.
    • Write “make” and complete the installation.

    You can also set environmental variables to run root for anytime you open a terminal window by writing “root” command. To edit bash and add environmental variables;

    • Open a termianl window and write “cd /” to go root directory.
    • Write “sudo nano etc/bashrc” to open a editor to edit bash profile.
    • Add the following lines in to bashrc file, save and exit (CTRL-O and CTRL-X)
      • export ROOTSYS=/usr/local/root
      • export DYLD_LIBRARY_PATH=$ROOTSYS/lib
      • export PATH=$PATH:$ROOTSYS/bin
    • You should change $ROOTSYS variable (first line) if you installed ROOT framework in to another directory.

    General Instructions for Installations:

    • Download the software in tarball.
    • Decompress tarball writing “sudo tar -zxvf <software>.tar”
    • Cd into folder and edit Makefile. DON’T FORGET TO EDIT IT IN COMPATIBLE WITH YOUR PATHS and COMPILERS or UPDATE YOUR COMPILERS.
    • Write “./install” or “./configure” if you have such shell files to run.
    • Write “make” and start compiling.
    • Trace errors if you have any.

    I strongly recommend you to register Apple’s developer website http://developer.apple.com/ for recent updates and development tools.