RootTalk![]() |
DavecRoofit |
fnal root tips![]() |
Pad tricks for axes![]() |
These are some command line options you can use when starting ROOT: -b: ROOT session runs in batch mode, without graphics display. This mode is useful in case you do not want to set the DISPLAY. -n: Does not execute the logon script and logoff script as specified in .rootrc. -q: Exits after processing the command line macro files. -l: Does not show the ROOT banner. -a: Displays the ROOT splash screen. -x: Exits on exception. dir: If dir is a valid directory, change to it (cd) before executing ROOT. -?, -h, --help: Prints usage.ROOTSYS and PATH AT CERN, 30 MARCH 2021
~/<1>src/Reco/RecoAnalyzer $ echo $ROOTSYS /cvmfs/cms.cern.ch/slc6_amd64_gcc530/lcg/root/6.06.00-ikhhed4/ echo $PATH /cvmfs/cms.cern.ch/share/overrides/bin: /afs/cern.ch/user/d/davec/CMSSW_8_0_19_patch1/bin/slc6_amd64_gcc530: /afs/cern.ch/user/d/davec/CMSSW_8_0_19_patch1/external/slc6_amd64_gcc530/bin: /cvmfs/cms.cern.ch/slc6_amd64_gcc530/cms/cmssw-patch/CMSSW_8_0_19_patch1/bin/slc6_amd64_gcc530: /cvmfs/cms.cern.ch/slc6_amd64_gcc530/cms/cmssw-patch/CMSSW_8_0_19_patch1/external/slc6_amd64_gcc530/bin: /cvmfs/cms.cern.ch/slc6_amd64_gcc530/cms/cmssw/CMSSW_8_0_19/bin/slc6_amd64_gcc530: /cvmfs/cms.cern.ch/slc6_amd64_gcc530/external/llvm/3.7.1-giojec/bin: /cvmfs/cms.cern.ch/slc6_amd64_gcc530/external/gcc/5.3.0/bin: /cvmfs/cms.cern.ch/slc6_amd64_gcc530/lcg/root/6.06.00-ikhhed4/bin: /afs/cern.ch/cms/caf/scripts: /cvmfs/cms.cern.ch/common: /usr/sue/bin:/usr/lib64/qt-3.3/bin: /usr/local/bin: /usr/bin: /usr/local/sbin: /usr/sbin: /opt/puppetlabs/bin: /afs/cern.ch/project/eos/installation/pro/bin/: /afs/cern.ch/sw/lcg/app/releases/ROOT/6.02.00/x86_64-slc6-gcc48-opt/root//bin: /afs/cern.ch/user/d/davec/MyCode/ROOTSYS and PATH on my MAC, 30 MARCH 2021
HEPMBP069:~ david$ echo $ROOTSYS /Users/david/Downloads/root HEPMBP069:~ david$ echo $PATH /Users/david/Downloads/root/bin: /usr/bin: /bin: /usr/sbin: /sbin: /usr/local/bin: /opt/X11/binOn Ubuntu, 30 March 2021
you dont have to compile ROOT from source on Ubuntu. You can download pre-compiled binaries (https://root.cern/downloading-root) that you just need to uncompress, and a source /path/to/ROOT/bin/thisroot.sh) gives you a working ROOT.Batch mode
TMarker m(.5,.5,22); // x,y, marker type !!!!!!!! m.Draw(); or TMarker tt; tt.SetMarkerStyle(20); tt.SetMarkerSize(0.8); tt.DrawMarker(.5,.5); or, for a point in a 3D plot: { TGraph2D* t = new TGraph2D(1); t->SetPoint(0, 1. , 2. , 3.); t->GetXaxis()->SetLimits(0, 2); t->GetYaxis()->SetLimits(1, 3); t->GetZaxis()->SetLimits(2, 4); // t->GetXaxis()->SetRangeUser(0, 2); // t->GetYaxis()->SetRangeUser(1, 3); t->GetZaxis()->SetRangeUser(2, 4); t->Draw("P0"); }Change title
gStyle->SetOptTitle(0); TPaveLabel *title = new TPaveLabel(.11,.95,.35,.99,"my new title","brndc"); title->Draw();
TLatex tt; tt.DrawLatexNDC(0.01,0.01,"mytext");Convert string to const char * for TFile name
std::string str; const char * c = str.c_str(); //std::string str; const char * c = s.c_str(); //s = "Run-" + s + ".root"; TFile f(c, "RECREATE");Put run number into histo title
string s, s1, s2; TString tsrun; s1 = to_string(328337); s2 = to_string(328338); s = "Runs " + s1 + " and " +s2; cout<< "\n\nString is " << s <<endl; tsrun = s; cout << tsrun << endl; TH2D *EEPlaser1pnratio = new TH2D("EEPlaser1pnratio", tsrun + ", EEP, pn ratio, pn g=16/pn g=1, laser power = 1", 100, 0, 100, 100, 0, 100);TGraph constructors - only these are valid !!!
TGraph (Int_t n, const Int_t *x, const Int_t *y) Graph normal constructor with ints. More... TGraph (Int_t n, const Float_t *x, const Float_t *y) Graph normal constructor with floats. More... TGraph (Int_t n, const Double_t *x, const Double_t *y) Graph normal constructor with doubles. More...TGraph options
Graphs can be drawn with the following options: Option Description "A" Axis are drawn around the graph "I" Combine with option 'A' it draws invisible axis "L" A simple polyline is drawn "F" A fill area is drawn ('CF' draw a smoothed fill area) "C" A smooth Curve is drawn "*" A Star is plotted at each point "P" The current marker is plotted at each point "B" A Bar chart is drawn "1" When a graph is drawn as a bar chart, this option makes the bars start from the bottom of the pad. By default they start at 0. "X+" The X-axis is drawn on the top side of the plot. "Y+" The Y-axis is drawn on the right side of the plot. "PFC" Palette Fill Color: graph's fill color is taken in the current palette. "PLC" Palette Line Color: graph's line color is taken in the current palette. "PMC" Palette Marker Color: graph's marker color is taken in the current palette. "RX" Reverse the X axis. "RY" Reverse the Y axis.
TFile miofile(test.root); TCanvas can = (TCanvas)miofile.Get(c1); TH1D his = ((TH1D)(can->FindObject(MCA))); Double_t xmin = his->GetBinCenter(1); Double_t xmax = his->GetBinCenter(his->GetNbinsX()); his->Fit(gaus, , , xmin, xmax); can->Draw(); TFile miofile(test.root); TCanvas can = (TCanvas)miofile.Get(c1); ((TPad *)can->FindObject(pad1))->GetListOfPrimitives()->ls(); ((TPad *)can->FindObject(pad1))->GetListOfPrimitives()->Print(); TH1D his = ((TH1D)(((TPad *)can->FindObject(pad1))->GetPrimitive(MCA))); Double_t xmin = his->GetBinCenter(1); Double_t xmax = his->GetBinCenter(his->GetNbinsX()); his->Fit(gaus, , , xmin, xmax); can->Draw(); Mr.M Oct '12 Thank you very very much! can->FindObject(MCA) was the trick. :smiley:
I wrote a canvas in a file and now I'd like to get back the histogram from the canvas and do some stuff... This is the 1st script: TFile miofile("Hfra40.root","read"); miofile->ls(); TCanvas *c1 = (TCanvas*)miofile.Get("c1"); c1->Draw(); the c1 canvas is drawn and this is the output: TFile** Hfra40.root TFile* Hfra40.root KEY: TCanvas c1;1 c1
Attilio, In your 2nd script, do; { TFile miofile("Hfra40.root","read"); TH1F *hFra40; TCanvas *c1 = (TCanvas*)miofile.Get("c1"); hFra40 = (TH1F*)c1->GetPrimitive("hFracEtcorr2Part"); c1->Clear(); c1->Draw(); hFra40->Draw(); } or { TFile miofile("Hfra40.root","read"); TH1F *hFra40; TCanvas *c1 = (TCanvas*)miofile.Get("c1"); hFra40 = (TH1F*)c1->GetPrimitive("hFracEtcorr2Part"); TCanvas c2; hFra40->Draw(); } In your original script, the current pad/canvas is c1. So you draw the histogram in this canvas. Rene Brun
TDatime t; t.Set(....);Plot single point
//search points (may be more than 1) Int_t j = 0; for (j=0;j<n;j++) { if (TMath::AreEqualRel(0.5,gr->GetX()[j],1e-6)) { printf(found point %d with x=%g, y=%g\n,j,gr->GetX()[j],gr->GetY()[j]); }Writing/reading TGraphs
Float_t x[5] = {1, 2, 3, 4, 5}; Float_t y[5] = {3, 3, 3, 3 ,3}; Float_t ex[5] = {1, 1, 1, 1, 1}; Float_t ey[5] = {1, 1, 1, 1 ,1}; TFile f("test.root", "RECREATE"); TGraph *g = new TGraphErrors(5, x, y, ex, ey); //g->Draw("AP"); g->SetName("hello"); g->Write(); f.Close(); delete f;
As suggested above, you can write your graphs one by one to the file or alternatively add the graphs to the list of objects associated to the file with f->GetList()->Add(gr1); f->GetList()->Add(gr2); //etc f->Write(); //will write all the objects in the list In this case, you better give a name to your graphs When you want to draw the graph in a file -via TBrowser, double click on the graph. If you use a version >= 4.02 the graph will be drawn by default with option alp. you can change this default option by setting TGraph::BrowseOption: alp in the file .rootrc or $ROOTSYS/etc/system.rootrc -via the command TGraph gr1 = (TGraph)f->Get(name of gr1); gr1->Draw(alp); //or any other option
Only TH1 and TTree objects are automatically registered to gDirectory->GetList(). TGraphs are not. You can register any TObject derived object (eg TGraph) to this list with a statement like gDirectory->Append(mygraph); where TGraph *mygraph is an already existing TGraph. You can replace your saveAll function with gDirectory->Write(); This statement will write all objects registered in the directory. To see the list of objects in a directory, do dir->ls(), eg gDirectory->ls(); Rene BrunGetting global variables from another file
#include "sic-btcp-lcf-info.C" void Drawlcps() { ...........can now use the variables listed in sic-btcp-lcf-info.C, they are now global variables } and in root: > .L corr-fac-vs-int-lumi-300-with-extraps.C // the file which has the function: void Drawlcps() {...} > Drawlcps()
Text discussion![]() |
atlas logon![]() |
gROOT->ProcessLine(".! echo $ROOTSYS"); | ProcessLine to execute other code from within one program |
gROOT->Reset() | When running an un-named script over again, and this is frequently the case since un-named scripts are used to prototype, one should reset the global environment to clear the variables with gROOT->Reset() |
TString | Examples, use of the TString class![]() |
TGraph | TGraph methods![]() |
> ===== > void SetTitleFont(Style_t font, Option_t *axis) > if axis =="x" set the X axis title font > if axis =="y" set the Y axis title font > if axis =="z" set the Z axis title font > any other value of axis will set the pad title font > if axis="xyz" set all 3 axes > ==== > > gStyle->SetTitleFont(62); > gStyle->SetTitleFontSize(0.09);Also note that if you select a font style in pixels (63), ie: gStyle->SetTitleFont(63,"");
precision = 0 fast hardware fonts (steps in the size) precision = 1 scalable and rotatable hardware fonts (see below) precision = 2 scalable and rotatable hardware fonts precision = 3 scalable and rotatable hardware fonts. Text size is given in pixels. List of the currently supported fonts: Font number X11 Names Win32/TTF Names 1 : times-medium-i-normal "Times New Roman" 2 : times-bold-r-normal "Times New Roman" 3 : times-bold-i-normal "Times New Roman" 4 : helvetica-medium-r-normal "Arial" 5 : helvetica-medium-o-normal "Arial" 6 : helvetica-bold-r-normal "Arial" 7 : helvetica-bold-o-normal "Arial" 8 : courier-medium-r-normal "Courier New" 9 : courier-medium-o-normal "Courier New" 10 : courier-bold-r-normal "Courier New" 11 : courier-bold-o-normal "Courier New" 12 : symbol-medium-r-normal "Symbol" 13 : times-medium-r-normal "Times New Roman" 14 : "Wingdings" 15 : Symbol italic (derived from Symbol)Font images
Unix.*.Root.UseTTFonts: true It is possible to check the TTF are in use in a Root session with the command: gEnv->Print(); If the TTF are in use, the following line will appear at the beginning of the printout given by this command: Unix.*.Root.UseTTFonts: true [Global]TStyle Class Reference link
Canvas Pad Histogram axis Lines Fill areas Text Markers Functions Histogram Statistics and TitlesSet font number for axis labels. SetLabelFont(): void TStyle::SetLabelFont ( Style_t font = 62, Option_t * axis = "X" )
pad_width = gPad->XtoPixel(gPad->GetX2()); pad_height = gPad->YtoPixel(gPad->GetY1()); if (pad_width < pad_height) charheight = textsize*pad_width; else charheight = textsize*pad_height;If the text precision is equal to 3, the text size doesn't depend on the pad's dimensions. A given textsize value always generates the same absolute size. The text size (charheight) is given in pixels: charheight = textsize;
#include <TH2.h> TH1F *iprod1 = new TH1F("iprod1","iprod distn",12,-2,10); TH2D *iprod2 = new TH2D("iprod2","EE- types",100,0,100,100,0,100); TH2D *iprod3 = new TH2D("iprod3","EE+ types",100,0,100,100,0,100);Getting the bin content at x,y:
(TH1::GetBinContent(TH1::FindBin(x,y)) ie hEE->GetBinContent( hEE->FindBin(x,y) )Get the bin centers for a point (x,y):
x_=h2D->GetXaxis()->GetBinCenter( h2D->GetXaxis()->FindBin(x) ); y_=h2D->GetYaxis()->GetBinCenter( h2D->GetYaxis()->FindBin(y) );Using more than one TFile:
cout << "Histos available on disk, before TFiles: " << endl; gDirectory->ls("-d"); cout << "Histos available in memory, before TFiles: " << endl; gDirectory->ls("-m"); cout << "\n\n" << endl; TFile f ("histo-reco.root"); //f.cd("demo"); //h002->Draw(); cout << "Histos available on disk, after TFile f (histo-reco.root) : " << endl; gDirectory->ls("-d"); cout << "Histos available in memory, after TFile f (histo-reco.root) : " << endl; gDirectory->ls("-m"); cout << "\n\n" << endl; TFile ff ("result-of-adding.root"); TFile fff ("EBdata.root"); ......... f.cd("demo"); TH1 *EEMcs2 = (TH1*)EEMcs->Clone("EEMcs2"); // EE- status ........ ff.cd(""); TH1 *iprod22 = (TH1*)iprod2->Clone("iprod22"); fff.cd(""); TH1 *EBiprod2 = (TH1*)EBiprod->Clone("EBiprod2"); // btcp = 1, SIC = 2, preprod = 3 TH1 *EBiproddist2 = (TH1*)EBiproddist->Clone("EBiproddist2"); // btcp = 1, SIC = 2, preprod = 3 ..........Plotting multiple graphs
TGraph *g[10]; Double_t x[10] = {0,1,2,3,4,5,6,7,8,9}; Double_t y[10] = {1,2,3,4,5,6,4,3,2,1}; TMultiGraph *mg = new TMultiGraph(); for (int i=0; i<10; i++) { g[i] = new TGraph(10, x, y); y[i] = y[i]+1; mg->Add(g[i]); } mg->Draw("AL"); // will auto draw x-y ranges to fit all graphs // changing mg ranges // need GetXaxis for x-axis range <-- with decimal not integer numbers !!!!!!! // need SetMinimum and SetMaximum for y-axis range <-- with decimal not integer numbers !!!!!!! mg3040->GetXaxis()->SetLimits(690., 720.) root [4] mg3040->SetMinimum(-1.5) root [5] mg3040->SetMaximum(1.1)Saving histos to a file:
save the histograms to a file root[14] TFile* file = new TFile("exercise0.root","RECREATE") and now we can write the histograms and close the file root[15] myhisto.Write() root[16] histo->Write() root[17] file->Close() root[18] delete file Now try to read the histograms back in: root[19] TFile* fileagain = new TFile("exercise0.root","READ") root[20] TH1F * histoagain = (TH1F*)fileagain->Get("histo");Histo methods
TH1F *h = (TH1F*)h1->Clone("h"); virtual Bool_t Add (const TH1 *h1, Double_t c1=1) Performs the operation: this = this + c1*h1 If errors are defined (see TH1::Sumw2), errors are also recalculated. More... virtual Bool_t Add (const TH1 *h, const TH1 *h2, Double_t c1=1, Double_t c2=1) Replace contents of this histogram by the addition of h1 and h2. More... Bool_t TH1::Divide ( TF1 * f1, Double_t c1 = 1 ) Performs the operation: this = this/(c1*f1) if errors are defined (see TH1::Sumw2), errors are also recalculated. Bool_t TH1::Divide ( const TH1 * h1 ) Divide this histogram by h1. this = this/h1 if errors are defined (see TH1::Sumw2), errors are also recalculated. Bool_t TH1::Divide ( const TH1 * h1, const TH1 * h2, Double_t c1 = 1, Double_t c2 = 1, Option_t * option = "" ) Replace contents of this histogram by the division of h1 by h2. this = c1*h1/(c2*h2) If errors are defined (see TH1::Sumw2), errors are also recalculatedNote: //(*EETPmean)=(*EETPrad)/(*EETPradhits); // operations can be done with histogram pointers
TH1F *EETPmean = new TH1F("EETPmean","EE testpulse mean vs radius, sqrt(ix**2 + iy**2); Radius, sqrt(x*x + y*y)", 60, 0., 60.); EETPmean->TH1::Add(EETPrad); EETPmean->TH1::Divide(EETPradhits); EETPmean->SetTitle("EE testpulse mean vs radius, sqrt(ix**2 + iy**2) " ) ; EETPmean->SetXTitle("Radius, sqrt(x*x + y*y)" );Int_t TH1::FindBin ( Double_t x, Double_t y = 0, Double_t z = 0 )
Replace contents of this histogram by multiplication of h1 by h2. this = (c1*h1)*(c2*h2) If errors of this are available (TH1::Sumw2), errors are recalculatedSetting the bin content:
virtual void SetBinContent (Int_t bin, Double_t content) Set bin content see convention for numbering bins in TH1::GetBingROOT->ls() ; // to get to objects in memory !!!!!!!!!!!
Classes begin with T: TLine, TTree Non-class types end with _t: Int_t Data members begin with f: fTree Member functions begin with a capital: Loop() Constants begin with k: kInitialSize, kRed Global variables begin with g: gEnv Static data members begin with fg: fgTokenClient Enumeration types begin with E: EColorLevel Locals and parameters begin with a lower case: nbytes Getters and setters begin with Get and Set: SetLast(), GetFirst()Machine Independent Types
Char_t Signed Character 1 byte UChar_t Unsigned Character 1 byte Short_t Signed Short integer 2 bytes UShort_t Unsigned Short integer 2 bytes Int_t Signed integer 4 bytes UInt_t Unsigned integer 4 bytes Long64_t Portable signed long integer 8 bytes ULong64_t Portable unsigned long integer 8 bytes Float_t Float 4 bytes Double_t Float 8 bytes Double32_t Double 8 bytes in memory, written as a Float 4 bytes Bool_t Boolean (0=false, 1=true)
Extra .dat file: basic-vpts-john-wood-linux-data.dat |
Tree-vpts-types-in-dat-file.c | basic-vpts-with-types.dat |
Tree-test.C | basic.dat |
tree->Branch("Ev_Branch",&event,"temp/F:ntrack/I:nseg:nvtex:flag/i "); The first parameter is the branch name. The second parameter is the address from which the first variable is to be read. In the code above, event is a structure with one float and three integers and one unsigned integer. You should not assume that the compiler aligns the elements of a structure without gaps. To avoid alignment problems, you need to use structures with same length members. If your structure does not qualify, you need to create one branch for each element of the structure. The leaf name is NOT used to pick the variable out of the structure, but is only used as the name for the leaf. This means that the list of variables needs to be in a structure in the order described in the third parameter. The third parameter is a string describing the leaf list. Each leaf has a name and a type separated by a "/". It is separated from the next leaf by a ":".Examples:
{ TFile *f = new TFile("peter.root","recreate"); Int_t nPhot; Float_t E[500]; TTree* nEmcPhotons = new TTree("nEmcPhotons","EMC Photons"); nEmcPhotons->Branch("nPhot",&nPhot,"nPhot/I"); nEmcPhotons->Branch("E",E,"E[nPhot]/F"); }List of tree variable types:
The symbols used for the type are: C: a character string terminated by the 0 character B: an 8 bit signed integer b: an 8 bit unsigned integer S: a 16 bit signed integer s: a 16 bit unsigned integer I: a 32 bit signed integer i: a 32 bit unsigned integer L: a 64 bit signed integer l: a 64 bit unsigned integer F: a 32 bit floating point D: a 64 bit floating point
Create a dynamic array of pointers TH1F** h = new TH1F*[m]; for (int i=0;i<m;i++) { h[i] = new TH1F(name,title... Rene Brunsee root faq
TH1F *h = (TH1F*)gDirectory->GetList()->FindObject("myHist");
gDirectory->pwd() | gDirectory points to the subdirectory. It prints the current directory location with pwd |
gFile->pwd() | gFile points to the file's top directory, and prints the directory location with pwd |
f1.cd(); | changes the current directory back to f1 |
.pwd | CINT shortcut for gDirectory->pwd() |
.ls | CINT shortcut for gDirectory->ls() |
gROOT->cd() | Back to Rint:/, the topmost directory, and prints "(unsigned char)1" or "true" |
then gROOT->pwd() gives Rint:/ | |
gDirectory->ls("-d") | lists the objects on disk in the current directory |
gDirectory->ls("-m") | list objects in memory |
gDirectory->ls("-d") | list objects on disk |
gDirectory->ls() | list objects in memory and then objects on disk |
f->cd("Wed011003") | Change to subdirectory Wed011003 |
gDirectory->GetFile() | From a subdirectory, get a pointer to the file containing the subdirectory |
TFile * file = new TFile("afile","RECREATE"); file->mkdir("a"); file->cd("a"); gDirectory->mkdir("b"); gDirectory->cd("b"); gDirectory->mkdir("d");Clone histogrammes
TObject* TH1::Clone(const char* newname) const { TH1* obj = (TH1*)IsA()->GetNew()(0); Copy(*obj); .................. TH1F *h = (TH1F*)h1->Clone("h"); TH1F *hnew = (TH1F*)h->Clone("hnew")If 'newname' is set, the copy's name will be set to that name. Fits to data, with error bars
i.e. a function defined in f1, a TF1 histogramme: TF1 *f1 = new TF1("f1","[0]+[1]*x+[2]*x*x",90.,210.); Some data, gr gr = new TGraphErrors(np,volts,data,ex,ey); Fit the data in gr, to the function in f1 gr->Fit(f1); Place the data points gr->Draw("P");Overlay a canvas on top of an existing PDF (pre-made plot)
void canvasimage() { //create a canvas with a picture (.png,.jpg,.gif, etc) in the background TImage *img = TImage::Open("rose512.jpg"); //put your own picture here TCanvas *c1 = new TCanvas("c1"); img->Draw("x"); //Create a transparent pad filling the full canvas TPad *p = new TPad("p","p",0,0,1,1); p->SetFillStyle(4000); p->SetFrameFillStyle(4000); p->Draw(); p->cd();Make Class
root[] TFile f ("staff.root") root[] f.ls() returns: TFile** staff.root TFile* staff.root KEY: TTree T;1 staff data from ascii file Look at T, and make appropriate class TTree *MyTree = T T->Print(); T->MakeClass("MyClass") returns: Files: MyClass.h and MyClass.C generated from Tree: T root[] MyTree->Scan(); will print the first 8 variables of the tree. root[] MyTree->Scan("*"); will print all the variable of the tree. Specific variables of the tree can be explicit selected by list them in column separated list: root[] MyTree->Scan("var1:var2:var3"); will print the values of var1, var2 and var3. A selection can be applied in the second argument: root[] MyTree->Scan("var1:var2:var3","var1==0"); will print the values of var1, var2 and var3 for the entries where var1 is exactly 0.
root -b | run in batch mode without graphics display |
root -l | do not show the startup panel |
gROOT->Reset() | Add at start of un-named scripts. Clears the variables, clears the global scope to the state |
gROOT::GetListOf"TAB" | For other gROOT actions |
TFile f("staff.root"); | read in file staff.root |
TFile *_file0 = TFile::Open("root://castorcms//castor/cern.ch/user/n/ndefilip/Paper/data/roottree_leptons_DoubleElectron_Run2012A-22Jan2013-v1.root") | |
Note the new formalism from Feb 2014, for castorcms | |
f->ls() or f.ls() | list file contents, write out list of trees |
T->Show(10); | show values of variables, 10th entry |
T->Print() | Print list of variables/structure for tree T |
T->Scan("Cost:Age:Children") | values for a list of leaves |
T->StartViewer() | Get a viewer to look at the tree |
T->Draw("Cost:Age") | plot cost versus age |
TDatime t(unixTime_); | Convert unix time to ordinary time |
t.Print(); | Print the time |
UnZoom | rh click on axis concerned, in drop down menu, click on UnZoom |
ZoomOut | rh click on axis concerned, in drop down menu, click on ZoomOut |
The TPad::SetTicks() method specifies the type of tick marks on the axis. | Let tx=gPad->GetTickx() and ty=gPad->GetTicky(). |
Axes | TGaxis to draw more axes![]() |
Axes | Paint axis with chopt comments![]() |
tx = 1; | tick marks on top side are drawn (inside) |
tx = 2; | tick marks and labels on top side are drawn |
ty = 1; | tick marks on right side are drawn (inside) |
ty = 2; | tick marks and labels on right side are drawn |
tx=ty=0 | by default only the left Y axis and X bottom axis are drawn |
Use TPad::SetTicks(tx,ty) to set these options. | See also the methods of TAxis that set specific axis attributes. |
If multiple color-filled histograms are drawn on the same pad, the fill area may hide the axis tick marks. | |
One can force the axis redrawing over all the histograms by calling: gPad->RedrawAxis() | |
TFile *f800 = new TFile("histo-run152847.root"); | read in root file |
f800->ls(); | list contents of f800 |
hadd | hadd newfile.root file_0.root file-1.root file-2.root file_3.root to add histos to a file |
ifstream inFile ("cms_ecal_dcs_07_ECAL_HVM_0.csv"); | read in csv file |
f800->cd("demo"); | go to histogram directory |
gDirectory->ls("-d"); | list the objects in the directory |
h006->Draw(); | draw one of the histos |
h167 = new TH2D ("h167"," ", 102, 0.,102., 102, 0.,102.); | new 2D histo |
h65 = new TH1F ("h65"," ",100,0.,2.); | new 1D histo |
h65->SetTitle(" EE+, Ratios for Va750/Va800, B=0T, 31Jan2011"); | Set title |
hpx->GetXaxis()->SetTitle("Anode volts (Dynode = +600V) "); | set x-axis title |
h167->Fill(i+1,j+1,1.0); | Fill histo |
gPad->SetLogx(1) | Set log scale, x-axis |
gPad->SetLogy(1) | Set log scale, y-axis |
hpx->SetMaximum(5.); | set Max Y axis |
hpx->Draw(); | Draw histo |
hpx->Fit("gaus"); | FIT FIT, contents of hpx with a gaussian |
hpx->Fit("pol5"); | FIT FIT, contents of hpx with a polynomial to power 5 (x**5) |
hpx->Fit("expo"); | FIT FIT, contents of hpx with an exponential |
hpx->Fit("landau"); | FIT FIT, contents of hpx to a landau |
gr3->Fit("expo"); | Fit an exponential to set of points gr3, where gr3 is a TGraph or TGraphErrors gr3(n,x,y,ex,ey); |
gr3->Fit("pol0"); | etc |
gStyle->SetOptStat(0); | no stats box |
gStyle->SetOptStat(1); | to print fit values in the plot stats box |
gStyle->SetOptFit(1011); | what fit values to print out |
mode = pcev (default = 0111) | |
p = 1 print probability | |
c = 1 print Chi-square/number of degrees of freedom | |
e = 1 print errors (if e=1, v must be 1) | |
v = 1 print name/values of parameters | |
ph700[i][j] = h006->GetBinContent(i+1,j+1); | get bin content |
hsimple.C | generates hpx and hpxpy histos and writes them to hsimple.root |
from C: root tutorials |
// File print1.h: int iy; // this will be a GLOBAL variable, accessible by other files !!! void print1(int i, double ran1, int ib, double ran2, double probib, int icountib) { cout << " i= " << i << ", ran1 = " << ran1 << ", ib = " << ib << " ran2 = " << ran2 << ", prob[ib] = " << probib << " icount[ib] = " << icountib << endl; } void prtchisq(double data[],int icount[],int bincount[],double chisqj, int bintot, int nevents, int ictot) { cout << endl; for(int i=0; i<5; i++) { double chisqbin = ( (data[i]-double(icount[i]))**2) / icount[i] ; cout << "i = " << i << ", bincount = " << bincount[i] << " data = " << data[i] << ", icount = " << icount[i] << ", chisqbin = " << chisqbin << endl; } cout << "bintot = " << bintot << ", nevents = " << nevents << ", ictot = " << ictot << ", chisq = " << chisqj << "\n\n " << endl;}Don't include type defs in function call !!!!!!
From file: res-500-2p2.C { // if eta, lumiref,, lumi set for type in rescalc then DON'T do it here if have done .L rescalc.C before .x res-500-2p2.C!!!!!!!!!!!!! //double eta = 2.2; //double lumiref = 500.; //double lumi = 3000.; //double lumi = 500.; eta=2.2; lumiref=500.; lumi=500; rescalc(eta, lumiref, lumi); // NOT NOT rescalc(double eta, double lumiref, double lumi); !!!!!!!!!!!!
Double_t ph = 45.63; Int_t ix=36; Int_t iy=45; Int_t nb = hxy->FindBin(Double_t(ix), Double_t(iy), 0); // returns a GLOBAL bin number //SetBinContent(Int_t bin, Double_t content) hxy->SetBinContent(nb, ph);How to print a number and/or a string of text on the plot:
Double_t dg = 0.; dg=100*(fit[1]+2*fit[2]*v )/(fit[0]+fit[1]*v+fit[2]*v*v); char textToPlot[80]; sprintf(textToPlot,"%5.3f%%",dg); // %% prints the % sign cout << textToPlot << endl; TLatex t; t.SetTextAlign(22); t.SetTextSize(0.05); t.DrawLatex(800,0.12,textToPlot); t.DrawLatex(800,0.105,"per volt");How to get run number etc into the histogramme title:
string run; string side; string volts; run = iConfig.getUntrackedParameter<string>("runnum"); side = iConfig.getUntrackedParameter<string>("EEside"); volts = iConfig.getUntrackedParameter<string>("volts"); TString tsrun, tsside, tsvolts, tsinfo; tsside = side; tsvolts = volts; tsrun = run; tsinfo = "Run " + run + ", " + side + ", " + volts; h901 = fs->make<TH2D>("h901", tsinfo + ", digiph (ADC counts) versus rechit ph (ADC counts)", 100,-0.5,1000., 100,-0.5, 1000. ); ******** and in config file: run = ['158931','EE+','400/200'] process.demo = cms.EDAnalyzer('HVAnalyzer', runnum = cms.untracked.string(run[0]), EEside = cms.untracked.string(run[1]), volts = cms.untracked.string(run[2]) )
ROOT tutorials![]() |
Histo tutorials![]() |
Canvas setup instructions with picture![]() |
ROOT_for_beginners_Day1 |
ROOT_for_beginners_Day2 |
ROOT_for_beginners_Day3 |
ROOT_for_beginners_Day4 |
ROOT_for_beginners_Day5 |
TPad![]() |
Click on Tpad in middle of screen to link to the Tpad class |
TFile![]() |
Open a root file It is recommended to specify fname1 as " |
classTFile.html![]() |
NEW or CREATE: Create a new file and open it for writing if the file already exists the file is not opened. |
RECREATE: Create a new file, if the file already exists it will be overwritten. |
UPDATE: Open an existing file for writing. If no file exists, it is created. | |
READ: Open an existing file for reading (default). | |
If option = "" (default), READ is assumed. The file can be specified as a URL of the form: file:///user/rdm/bla.root or file:/user/rdm/bla.root ![]() |
|
file->Write(); | Save all histograms in memory to file |
The file can also be a member of an archive, in which case it is specified as: multi.zip#file.root or multi.zip#0 which will open file.root which is a member of the file multi.zip archive or member from the archive. See TArchiveFile class ![]() |
|
TFile and its remote access plugins can also be used to open any file, i.e. also non ROOT files, using: file.tar?filetype=raw | |
TStyle![]() |
This class lists the functions to set the following object attributes: Canvas - Pad - Histogram axis - Lines - Fill areas - Text - Markers - Functions - Histogram Statistics and Titles, TStyle::SetLabelColor SetLabelOffset(Float_t offset=0.005, Option_t *axis="X"); SetTitleFont(Style_t font=62, Option_t *axis="X"); SetTitleX(Float_t x=0); void SetTitleY(Float_t y=0.985) |
TCanvas![]() |
TCanvas construct and destruct syntax TCanvas source code ![]() TCanvas.cxx.html |
TPad![]() |
The TPad class is the most important graphics class in the ROOT system. TPad source code ![]() ie TPad::TPad(const char *name, const char *title, Double_t xlow, Double_t ylow, Double_t xup, Double_t yup, Color_t color, Short_t bordersize, Short_t bordermode) :TVirtualPad(name,title,xlow,ylow,xup,yup,color,bordersize,bordermode) |
TAttPad![]() |
Manage TPad attributes, referenced by TStyle, ie SetMargin(Float_t left, Float_t right, Float_t bottom, Float_t top) etc |
void SetTitleOffset (Float_t offset = 1, Option_t* axis = "X") Specify a parameter offset to control the distance between the axis and the axis title. offset = 1 means : use the default distance offset = 1.2 means: the distance will be 1.2*(default distance) offset = 0.8 means: the distance will be 0.8*(default distance) axis specifies which axis ("x","y","z"), default = "x" if axis="xyz", set all 3 axesExample of the SetTitleSize member
void SetTitleSize (Float_t size = 0.02, Option_t* axis = "X") if axis =="x" set the X axis title size if axis =="y" set the Y axis title size if axis =="z" set the Z axis title size *Any other value of axis will set the PAD title size!!!* if axis="xyz" set all 3 axes
Comment | ROOT |
---|---|
Most common error, no ";" at end of line | This MAY NOT cause a compiler error and so is most frustrating! |
Canvases |
A canvas has a default pad which has the name of the canvas itself |
TCanvas(const char* name, const char* title, Int_t wtopx, Int_t wtopy, Int_t ww, Int_t wh) | Set pos'n on screen and size |
TCanvas(const char *name, const char *title, Int_t ww, Int_t wh) | Set size, random position on screen |
TCanvas(const char* name, const char* title = "", Int_t form = 1) | Sets up a canvas, of size/pos for 5 forms, 1-5 |
TCanvas(const char *name, Int_t ww, Int_t wh, Int_t winid) | Create an embedded canvas |
TLine * l; |
define a pointer to a line object, called l |
l = new TLine(x1,y1,x2,y2); l->SetLineStyle(2); l->Draw(); | l is new so stays on heap??, line style 'dash' |
TArrow * ar; |
define a pointer to an arrow object, called ar |
ar = new TArrow(200, 0.08, 200, 0.03, 0.01, ">"); ar->Draw(); | x1, y1, x2, y2, size, "arrow head", also "<", "|>", "<|", "<>", "<|>" |
char textToPlot[80]; dg = 0.0148; | printing text and values of variables to the canvas using latex |
sprintf(textToPlot,"%5.4f%%",dg); cout << textToPlot << endl; | using sprintf to combine text and variable, note the format spec %5.4f and the percentage sign enterred with %% |
TLatex t; |
t.SetTextAlign(22); t.SetTextSize(0.05); |
t.SetTextAlign(Align); horiz 1=left, 2=centre, 3=rt, vert 1=bott, 2=centre, 3=top, ie Align=23 | |
t.DrawLatex(195,0.105,textToPlot); t.DrawLatex(195,0.09,"per volt"); | |
Histo titles etc |
f1->SetTitle("Dynode scan, anode 800V, 3.8T"); |
f1->GetXaxis()->SetTitle("Anode-Dynode voltage (V)"); x-axis title | |
f1->GetXaxis()->CenterTitle(); places x-axis title at centre across plot | |
f1->GetYaxis()->SetTitle("VPT response (arb. units)"); y-axis title | |
f1->GetYaxis()->CenterTitle(); places y-axis title at centre along y | |
f1->GetYaxis()->SetRangeUser(0.894,1.01); set user range for the Y axis | |
f1->GetXaxis()->SetRangeUser(90.,210.); set user range for the X axis | |
gStyle->SetTickLength(0.02,"y"); | |
gStyle->SetTickLength(0.05,"x"); | |
gStyle->SetTitleSize(0.05,"x"); | |
gStyle->SetTitleSize(0.05,"y"); | |
gStyle->SetTitleOffset(1.1,"y"); | y-axis title, distance from y axis |
gStyle->SetTitleOffset(1.,"x"); | x-axis title, distance from x axis |
gStyle->SetTitleAlign(13); | histo title position along x, global set for all histos following |
gStyle->SetPadBottomMargin(0.15); | Set distance from histo x-axis to bottom of pad |
gROOT->ProcesssLine(".x myprog.C"); |
to execute a script inside another script |
max 20 value, Set max value on Y axis for 1D hist #20 | h20->SetMaximum(value); |
min 20 value, Set min value on Y axis for 1D hist #20 | h20->SetMinimum(value); |
Get max entries from histo h1 | h1->GetMaximum(); |
Get min entries from histo h1 | h1->GetMinimum(); |
histo/del 10 | h10->Delete(); |
Increase # of points plotted along a fn, f1 (default is 100 only) | f1.SetNpx(200); |
h/fill, | h1->Fill(x); (note the capital F!) |
Set y-axis range on a 2D function plot | f1->GetYaxis()->SetRangeUser(min, max); |
Data from fits | func->GetParameter(0); |
Data from fits | func->GetParameter(1); |
Data from fits | func->GetParameter(2); etc |
Error on data from fits | func->GetParameterError(0); etc |
Set up a function, transfer the coefficients, draw | TF1 *f1 = new TF1("f1","[0]+[1]*x+[2]*x*x",360.,860.); x from 360 to 860 |
f1->SetParameters(fit[0],fit[1],fit[2]); f1->Draw(); | |
f1->SetParameter(0,value_first_parameter); | |
f1->SetParameter(1,value_second_parameter); | |
Line characteristics | f1->SetLineColor(2); 1=blk 2=red 3=bright grn 4=bright blue 5=yell 6=pink 7=aqua 8=grn 9=blue |
f1->SetLineWidth(2); // range from 1-15 | |
f1->SetTitle("Anode scan, anode-dynode 200V, 0T"); | |
int_t lwid = 0; lwid = f1->GetLineWidth(); | |
int_t lcol = 0; lcol = f1->GetLineColor(); | |
Un-named scripts | must start and end with {....} |
with #include statements inside the { | |
All statements executed in the interpreters global scope | |
Variables are in the global scope and are available on the cmd line, even after script has done executing | |
gROOT->Reset() | place at top of un-named script, to reset global environment/clear the variables |
calls destructor of objects, if object created on stack | |
If object was created on the heap, with 'new', it is not deleted | |
Must call the destructor explicitly (or get Memory leak warnings) | |
Named scripts | #include statements, then function statements, no {....} from start to end |
Objects are created on the stack, and are deleted upon exit, including histos | |
To avoid histo dissappearing, create it on the heap using 'new' | |
Histos and trees auto deleted when the current directory is closed | |
ie file script.C: | |
#include <iostream.h> | |
int run() | |
{cout << " Hello" << endl; return 0; } | |
root[0] .L script.C | |
root[1] run () |
I | Attachment | History | Action | Size | Date | Who | Comment |
---|---|---|---|---|---|---|---|
![]() |
Tree-test.C | r1 | manage | 3.0 K | 2015-06-02 - 13:34 | DavidCockerill | |
![]() |
Tree-vpts-types-in-dat-file.c | r1 | manage | 3.7 K | 2015-06-04 - 11:15 | DavidCockerill | |
![]() |
basic-vpts-john-wood-linux-data.dat | r1 | manage | 2.4 K | 2015-06-04 - 11:15 | DavidCockerill | |
![]() |
basic-vpts-with-types.dat | r1 | manage | 3.1 K | 2015-06-04 - 11:15 | DavidCockerill | |
![]() |
basic.dat | r1 | manage | 0.1 K | 2015-06-02 - 13:34 | DavidCockerill | |
![]() |
dynode-scan-fit.C | r1 | manage | 4.0 K | 2013-12-02 - 10:38 | DavidCockerill | |
![]() |
fit-protons-with-eta.C | r1 | manage | 2.8 K | 2015-06-17 - 13:54 | DavidCockerill | |
![]() |
pict1_TColor_002.png | r1 | manage | 55.0 K | 2019-09-26 - 10:49 | DavidCockerill |