1 / 16

ROOT TUTORIAL

PEDRAM BOGHRAT NOV 15, 2006. ROOT TUTORIAL. 1-TURN ASCII TEXT FILE INTO A ROOT TNTUPLE OBJECT. 2-OPEN NTUPLE AND PLOT EVENTS. 3-FIT DATA. 1-TURN ASCII TEXT FILE INTO A ROOT TNTUPLE OBJECT.

balfour
Download Presentation

ROOT TUTORIAL

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. PEDRAM BOGHRAT NOV 15, 2006 ROOT TUTORIAL 1-TURN ASCII TEXT FILE INTO A ROOT TNTUPLE OBJECT. 2-OPEN NTUPLE AND PLOT EVENTS. 3-FIT DATA.

  2. 1-TURN ASCII TEXT FILE INTO A ROOT TNTUPLE OBJECT.

  3. YOUR EVENT INFORMATION IS TRANSFERED TO THE COMPUTER VIA YOUR DAQ. IT IS STORED AS AN ASCII FILE ON THE COMPUTER HARD DRIVE. THE FOLLOWING IS AN EXAMPLE: DOWN DECAY TIME UP DECAY TIME UTCTIME -1 69 1163539665 26 -1 1163539684 -1 87 1163539735 -1 155 1163539739 94 -1 1163539779 -1 26 1163539812 -1 191 1163539816 -1 8 1163539827 -1 207 1163539993 121 -1 1163540010 -1 38 1163540021 118 -1 1163540132 27 -1 1163540156 -1 848 1163540168 317 -1 1163540172 170 -1 1163540214 239 -1 1163540241 -1 232 1163540323 Note a -1 corresponds to failure. ROOT MAKES ANALYSIS QUICKER AND EASIER, BUT ROOT REQUIRES THAT WE HAVE OUR DATA STORED IN AN NTUPLE.

  4. IMPORTANT!!

  5. CLICK ON TUTORIALS AND WE GET THE FOLLOWING: WITH A BIT OF MODIFICATION WE CAN PUT THIS PREMADE SCRIPT TO WORK FOR US.

  6. HOW TO RUN ROOT SCRIPTS: THE NEXT PAGE IS THE ROOT SCRIPT WE CAN USE TO CHANGE OUR ASCII FILE INTO A ROOT FILE. INORDER TO RUN THE SCRIPT TYPE THE FOLLOWING ON THE COMMAND LINE: $root IN.C WHERE IN.C IS THE NAME GIVEN TO THE FILE. YOU CAN COPY AND PASTE THIS CODE INTO A TEXT EDITOR LIKE emacs. THEN SAVE THE FILE AS IN.C. NOTE THAT THE $ SIGN ABOVE IS NOT TYPED IN BY YOU, ITS JUST THERE TO REPRESENT THE COMMAND LINE. SO IF I HAVE A FILE CALLED DATA.txt AND I RUN IN.C MY OUTPUT WILL LOOK LIKE: boghrat@localhost ~/Desktop/180f_ROOT_LECTURE/CODE $ root IN.C running .rootlogon.C root [0] Processing IN.C... up=-1, down=69, utctime=1163539665 up=26, down=-1, utctime=1163539684 up=-1, down=87, utctime=1163539735 up=-1, down=155, utctime=1163539739 up=94, down=-1, utctime=1163539779 found 41 points (Int_t)0 root [1] .q THE FIRST FIVE LINES MATCH THE FIRST FIVE LINES IN THE ASCII FILE. GOOD.

  7. //AUTHOR: PEDRAM BOGHRAT NOV 16, 2006 //THIS ROOT SCRIPT READS IN VALUES FROM AN ASCII FILE AND CREATES //AN NTUPLE Int_t IN() { gROOT->Reset();//RESET INTERPRETER ENVIRONMENT ifstream in;//in WILL BE THE NAME OF OUR STREAM INTERFACE in.open("DATA.txt");//WE'RE ASSUMING THAT DATA.txt EXISTS IN OUR DIRECTORY AND IT HAS 3 COLUMNS int up = 0;//VARIABLE USED TO STORE DECAY TIME FOR UP GOING PARTICLE int down = 0;//VARIABLE USED TO STORE DECAY TIME FOR DOWN GOING PARTICLE long int utctime = 0;//UTC TIME int nlines = 0;//VARIABLE USED TO STORE NUMBER OF EVENTS IN OUR DATA FILE TFile *f = new TFile("DATA.root","RECREATE");//CREATE A FILE OBJECT CALLED DATA.root THAT f WILL POINT AT TNtupleD *ntuple = new TNtupleD("ntuple","data from ascii file","up:down:utctime");//INSIDE DATA.root CREATE AN NTUPLE OBJECT THAT WILL STORE OUR EVENT DATA AND POINT TO IT WITH ntuple while (1) {//WHILE WE'RE NOT AT END OF FILE in >> up >> down >> utctime;//READ IN ONE LINE FROM DATA.txt AND STORE THE THREE VALUES IN THE VARIABLES up, down AND utctime if (!in.good()) break;//TERMINATE LOOP IF WE'RE AT THE END OF THE ASCII FILE if (nlines < 5) printf("up=%d, down=%d, utctime=%d\n",up,down,utctime);//PRINT OUT THE FIRST FIVE LINES OF THE ASCII FILE ntuple->Fill((double)up,(double)down,(double)utctime);//FILL OUR NTUPLE WITH VALUES. THE (double) IS THERE TO TYPE CAST OUR INT VARIABLES TO DOUBLE BECAUSE A TNtupleD OBJECT STORES DOUBLES. nlines++; } printf(" found %d points\n",nlines);//PRINT THE NUMBER OF EVENTS. in.close();//CLOSE STREAM INTERFACE f->Write();//WRITE THE NTUPLE TO FILE return 0; }

  8. HOW TO OPEN OUR NTUPLE AND LOOK AT DATA: AFTER RUNNING THE SCRIPT IF ALL WENT WELL YOU SHOULD NOW HAVE A FILE DATA.root IN YOUR DIRECTORY. IF YOU WANT TO LOOK AT THIS DATA TYPE THE FOLLOWING ON THE COMMAND LINE: $root DATA.root NOW, FROM INSIDE ROOT LETS MAKE A BROWSER OBJECT BY TYPING: TBrowser foo WHERE foo WILL BE THE NAME OF THE BROWSER. NOW YOU SHOULD GET A NICE GUI INTERFACE THAT LETS YOU NAVIGATE THROUGH THE FILE SYSTEM EASILY. CLICK ON DATA.root THEN CLICK ON ntuple AND YOU WILL SEE A LIST OF ALL YOUR FIELDS; IN OUR CASE up, down AND utctime. YOU CAN PLOT A GIVEN FIELD BY CLICKING ON ITS NAME.

  9. 2-OPEN NTUPLE AND PLOT EVENTS.

  10. IN 1882 SIMON NEWCOMB WAS ATTEMPTING TO CALCULATE THE SPEED OF LIGHT BY MEASURING HOW LONG IT TOOK FOR LIGHT TO TRAVEL FROM HIS LAB ON THE PATOMAC RIVER TO THE WASHINGTON MONUMENT AND BACK; EACH WAY WAS 7400 METERS. HE MADE 66 MEASUREMENTS OF THIS TIME WHICH WAS AROUND 24,800 ns. Cexp=2*7400m/24800ns~5.97E8 m/s WE NOW KNOW ITS ~ 3E8 m/s. LETS PLOT HIS DATA, ASSUMING NO ERROR BIAS THE VALUES HE MEASURED SHOULD BE A GAUSSIAN AROUND THE MEAN. LETS PLOT THE DATA FIRST. THE ROOT SCRIPT USED TO DO THIS IS ON THE NEXT PAGE; ITS CALLED PLOTTER.C. THIS SCRIPT TAKES THE FILE FROM WHICH YOU WANT TO EXTRACT YOUR DATA AS AN INPUT, HERE IS HOW YOU RUN A ROOT SCRIPT THAT CONTAINS INPUT PARAMETERS: $ root 'PLOTTER.C("NEWCOMB_LIGHT_SPEED_DATA.root")' NOTE THE SINGAL QUOTES, THEY'RE REQUIRED.

  11. //AUTHOR: PEDRAM BOGHRAT //NOV 14, 2006 void PLOTTER(const char *INFILE){ gROOT->SetStyle("Plain"); gStyle->SetPalette(1); gStyle->SetOptFit(); TFile inFile(Form("%s",INFILE)); TNtuple* inNtuple = (TNtuple*) inFile.Get("ntuple"); if(inNtuple == NULL) { cout<<"can't get data ntuple from file"<<endl; return 1; } const int nEntries = inNtuple->GetEntries(); cout<<"nEntries "<<nEntries<<endl; Float_t x; inNtuple->SetBranchAddress("up", &x); TH1F *h1 = new TH1F("h1","x",24,-60,60); for(int i = 0; i<nEntries; i++){ inNtuple->GetEntry(i); if(i<10){cout<<"i "<<i<<" x "<<x<<endl;} h1->Fill(x); } h1->SetXTitle("time(ns)-24800(ns)"); h1->SetYTitle("N"); h1->Draw(); c1->Print("lighttimehistogram.gif"); } THE FILE WHOSE DATA WE WANT TO PLOT IS AN INPUT JUST SETTING SOME GLOBAL ROOT VARS. CREATE A TFILE OBJECT. IN THE CODE IT WILL BE CALLED inFile; IT REFERS TO THE ROOT FILE CONTAINING YOUR DATA OPEN THE NTUPLE AND MAKE SURE ITS NOT EMPTY. RETURN ERROR MESSAGE IF IT IS EMPTY. GET NUMBER OF ENTRIES I CALLED THE TIME FIELD IN MY NTUPLE “up”.THIS FIELDS VALUES WILL BE CALLED “x” IN THIS SCRIPT CREATE A HISTOGRAM OBJECT LOOP OVER ALL ENTRIES AND WITH EACH ONE FILL THE HISTOGRAM CALLED “h1” SET LABELS FOR PLOT DRAW AND SAVE THE PLOT

  12. lighttimehistogram.gif IS THE NAME OF THE OUTPUT PLOT. HERE IS WHAT IT LOOKS LIKE. NEWCOMB DECIDED THIS WAS AN OUTLIER AND OMITTED IT BECAUSE HE BELIEVED THE DISTRIBUTION OF DATA SHOULD BE GAUSSIAN.

  13. 3-FIT DATA.

  14. I DIDN'T HAVE TIME TO TAKE OUT THE OUTLIERS, I SHOULD HAVE TAKEN THEM OUT. . . IN ORDER TO FIT A GAUSSIAN TO OUR DATA WE'RE GOING TO MAKE A MODIFICATION TO PLOTTER.C. WE NEED TO ADD THE FOLLOWING LINES: TF1 *f1 = new TF1("f1","gaus",-60 ,60); h1->Fit("f1","RQ"); THE FIRST LINE IS THE CONSTRUCTOR FOR A TF1 OBJECT (ITS THE LINE THAT WE'RE GOING TO FIT) WHILE THE 2ND LINE SAYS THAT THE DATA IN THE h1 HISTOGRAM SHOULD BE FIT WITH f1. I'VE CHOSEN f1 TO BE A GAUSSIAN; IF YOU WERE FITTING DATA THAT LOOKED EXPONENTIAL YOU WOULD USE expo INSTEAD OF gaus.

  15. //AUTHOR: PEDRAM BOGHRAT //NOV 14, 2006 void PLOTTER_FIT(const char *INFILE){ gROOT->SetStyle("Plain"); gStyle->SetPalette(1); gStyle->SetOptFit(); TFile inFile(Form("%s",INFILE)); TNtuple* inNtuple = (TNtuple*) inFile.Get("ntuple"); if(inNtuple == NULL) { cout<<"can't get data ntuple from file"<<endl; return 1; } const int nEntries = inNtuple->GetEntries(); cout<<"nEntries "<<nEntries<<endl; Float_t x; inNtuple->SetBranchAddress("up", &x); TH1F *h1 = new TH1F("h1","x",24,-60,60); for(int i = 0; i<nEntries; i++){ inNtuple->GetEntry(i); if(i<10){cout<<"i "<<i<<" x "<<x<<endl;} h1->Fill(x); } TF1 *f1 = new TF1("f1","gaus", -60, 60); h1->SetXTitle("time(ns)-24800(ns)"); h1->SetYTitle("N"); h1->Fit("f1","RQ"); h1->Draw(); c1->Print("lighttimehistogram.gif"); } THESE TWO LINES WILL ALLOW US TO FIT OUR DATA WITH A GAUSSIAN.

More Related