Upload
others
View
18
Download
0
Embed Size (px)
Citation preview
TutorialonROOTandDataAnalysis
ArunNayakIns5tuteofPhysics
Bhubaneswar
Objec5ve• ThistutorialisaboutdoingdataanalysisusingROOTTree
• Wediscusswri5ngandreadingeventsto/fromtree,wheretreesaredefinedinvariousformats
• Westartfromsimplerexamplesandslowlyaddmorefeaturesthatmakesdatastorageandanalysiseasier.
• Theprogramsfortheexamplesdiscussedhereareavailableinthetutorialdirectory.Youneedtolookatthoseprograms,whenwediscussthem,trytounderstandandusethemforprac5ce.
• Pleasedon’thesitatetoaskanyques5onsorreportyourproblemsanddifficul5es
• Oncewegothroughtheexamples,therearesomeexercisesforyoutoworkon.
• Reference:hNps://root.cern.ch/root/htmldoc/guides/users-guide/ROOTUsersGuide.html
2
Tree
3
ROOTTree(TTreeclass)isdesignedtostorelargequan55esofsame-classobjects,andisop5mizedtoreducediskspaceandenhanceaccessspeed.Itcanholdallkindofdata,suchasobjectsorarrays,inaddi5ontosimpletypes.Anexampleforcrea5ngasimpleTree
Runthemacro:root-ltree_example1.C
MoreonTBranch
4
• TheclassforabranchiscalledTBranch
• AvariableinaTBranchiscalledaleaf(TLeaf)
• Iftwovariablesareindependenttheyshouldbeplacedonseparatebranches.However,iftheyarerelateditisefficienttoputthemonsamebranch
• TTree::Branch()methodisusedtoaddaTBranchtoTTree
• Thebranchtypeisdecidedbytheobjectstoredinit
• Abranchcanholdanen5reobject,alistofsimplevariables,contentsofafolder,contentsofaTList,oranarrayofobjects.
Forexample:tree->Branch("Ev_Branch",&event,"temp/F:ntrack/I:nseg:nvtex:flag/i");
Where“event”isstructurewithonefloat,threeintegers,andoneunsignedinteger.
AccessingTTree
5
Showanentry:root[]TFilef("myfile.root")root[]T->Show(10)======>EVENT:10px=0.680243py=0.198578pt=0.708635random=0.586894
PrinttheTreestructure:root[]T->Print()
**************************************************************Tree:T:simpletree**Entries:10000:Total=202834bytesFileSize=169658**::Treecompressionfactor=1.19***************************************************************Br0:px:px/F**Entries:10000:TotalSize=40594bytesFileSize=37262**Baskets:2:BasketSize=32000bytesCompression=1.08**............................................................................**Br1:py:py/F**Entries:10000:TotalSize=40594bytesFileSize=37265**Baskets:2:BasketSize=32000bytesCompression=1.08**............................................................................**Br2:pt:pt/F**Entries:10000:TotalSize=40594bytesFileSize=35874**Baskets:2:BasketSize=32000bytesCompression=1.12**............................................................................**Br3:random:random/D**Entries:10000:TotalSize=80696bytesFileSize=58600**Baskets:3:BasketSize=32000bytesCompression=1.37**............................................................................*
Scanafewvariables:
root[]T->Scan("px:py:pt")****************************************Row*px*py*pt*****************************************0*0.8966467*-1.712815*1.9333161**1*1.5702210*0.5797516*1.6738297**2*0.6975117*0.1442547*0.7122724**3*0.0616207*-1.009907*1.0117853**4*-0.054552*1.3832200*1.3842953**5*-2.017178*1.4682819*2.4949667**6*0.8903368*2.5101616*2.6633834**7*-1.098390*-0.318103*1.1435256**8*0.3865155*0.0235152*0.3872301**9*1.8970719*1.9546536*2.7238855**10*0.6802427*0.1985776*0.7086347
AccessingTTree
6
OpenandDrawbranchesusingTBrowser:
UsingTTree::Draw()T->Draw("pt")
2Dplot:T->Draw(“py:px”)
SimpleAnalysisusingTTree::Draw()
7
Applyingcuts:T->Draw(“pt”,“px>0&&py>0”)Forapplying“weights”tothedistribu5on:Selec5on=“weight*(booleanexpression)”Where“weight”isaevent-by-eventweightstoredasabranchinthentuple(e.g.crosssec5on*luminosityforthenormaliza5on)
UsingTCuts:TCutc1=“px>0”TCutc2=“py<0”TCutc3=c1&&c2T->Draw(“pt”,c3)T->Draw(“pt”,c1&&c2)T->Draw(“pt”,c1&&“py>0”)TCutc4=c1||c2T->Draw(“pt”,c4)T->Draw(“pt”,c1||c2)
FillingHistograms:TH1F*hist=newTH1F("hist","",100,0.,2.);T->Draw("pt>>hist");hist->SetLineColor(2);hist->Draw("same")
ASimpleAnalyzer
8
Runthemacro:root-ltree_example2.C
Task:AddhistogramaNributes(axis5tle,linecoloretc..)
AddingBranchwithArrays
9
RunMacro:root-ltree_example3.C
Seethecontents:
ReadingBranchwithArrays
10
BranchwithVectors
11
YougetonlypointerstovectorsinTTree::Show()methodTTree::Draw()worksonlyforbasictypevectors
Advantage:NoneedtodefinearrayswithMAXSIZE
Exercises
12
• Therearetworootfilesinyourtutorialdirectory:“ntuple_array.root”and“ntuple_vector.root”
• TheycontaineventssimulatedwiththeprocessppàZàe+e-orµ+µ-
• Thetreecontainsbranchesformomentum,par5cleIDetc..forgeneratedelectron/muon(branchnameslikegenPar5clePxetc..),momentum,iden5fica5on,isola5onvariablesetc..forreconstructedelectronsandmuons(branchnameslikemuonPx,electronPxetc..)
• File“ntuple_array.root”containstreewithbranchesasvariablearrays• File“ntuple_vector.root”containstreewithbranchesasvectors
• Writeyourownmacro/code(twoseparatemacrofortwoinputfiles)toreadthetreesfromthefiles
• Selecteventswhereyouhavetwogeneratedmuons(orelectrons)ofoppositecharge.Combinethemomentaofthegeneratedµ+µ-(e+e-)pairtogetthemomentumofZboson.PlotthemassofthegeneratedZboson
• Selecteventswhereyouhavetworeconstructedmuons(orelectrons)ofoppositecharge.Combinethemomentaofthereconstructedµ+µ-(e+e-)pairtogetthemomentumofZboson.PlotthemassofthereconstructedZboson
BranchwithEventClass–I
13
FirstwriteyourownEventclassinaseparateheaderfile.Anexample“MyEvent.h”:
Createasharedobjectfile,usingthecommandinrootprompt:.LMyEvent.h+Itwillcreatea.sofile,likeMyEvent_h.so,inthesamedirectory.Note:ItusesACLiCtogeneratedic5onariesOR,youcanusetherootcint.Forthat,usetheMakefilegiveninthetutorialdirectory.Justusethecommand“make”
BranchwithEventClass-II
14
Torunthisprogram,youneedtofirstloadthelibrarycontainingtheclassdefini5on,asfollows:root–l.LlibMyEvent.so.Ltree_example8.Ctree_example8()
SeethetreecontentusingTTree::Show()andinTBrowser
BranchwithEventClass-III
15
Runthisprogram,asfollows:root–l.LlibMyEvent.so.Ltree_example9.Ctree_example9()
Lookattheproducedplots.Doyougetthesamedistribu5onsasexample2?
BranchwithEventClass-IV
16
Inthepreviousexample,youhaveoneobject(par5cle)perentry.However,younormallyhavemanypar5clesperevent.Inthiscase,youcanusevector<MyEvent>asthebranch.
Runthisprogram,asfollows:root–l.LlibMyEvent.so.Ltree_example10.Ctree_example10()
BranchwithEventClass-V
17
Runthisprogram,asfollows:root–l.LlibMyEvent.so.Ltree_example11.Ctree_example11()
Lookattheproducedplots.Doyougetthesamedistribu5onsasexample7?
Exercises
18
• ReadtheTreefrom“ntuple_array.root”• Writeyourownclassforeachpar5cletype,likeMyGenPar5cle.h,MyElectron.h,
MyMuon.h• Createanewtreewithbrancheslikevector<MyGenPar5cle>,vector<myMuon>,
vector<MyElectron>• Writetheeventsfrom“ntuple_array.root”tonewtreeandwritethenewtreeto
anewfile.
• Readyournewtreefromthenewlycreatedfileanddothefollowingexercisesasbefore.
• Selecteventswhereyouhavetwogeneratedmuons(orelectrons)ofoppositecharge.Combinethemomentaofthegeneratedµ+µ-(e+e-)pairtogetthemomentumofZboson.PlotthemassofthegeneratedZboson
• Selecteventswhereyouhavetworeconstructedmuons(orelectrons)ofoppositecharge.Combinethemomentaofthereconstructedµ+µ-(e+e-)pairtogetthemomentumofZboson.PlotthemassofthereconstructedZboson
Analysisusing“MakeClass”method
19
ItisausefulmethodprovidedbyROOTtododataanalysisusingTree.(Youcanalsolookat“MakeSelector”method,whichisnotdiscussedhere)
Forexample:produceatreeusingthetree_example8.Cprogram,tostartfromasimpleroneLoadthefileinrootandusefollowingcommand:root-lTFilef("myfile.root").lsT->MakeClass("MyClass")
ItcreatestwofilesMyClass.handMyClass.CEditMyClass.C(modifyingMyClass::Loop()func5on)toaddhistograms,doeventselec5ons,savethehistogramstofileThen,runtheprogramasfollowing:root–l.LMyClass.CMyClassmm.Loop()
Analysisusing“MakeClass”method
20
voidMyClass::Loop(){//Add/defineoutputfile,histogramshere,like:TH2F*h_pxpy=newTH2F("h_pxpy","pyVspx",100,-2.0,2.0,100,-2.0,2.0);TH1F*h_pt=newTH1F("h_pt","pt",100,0.,5.0);if(fChain==0)return;Long64_tnentries=fChain->GetEntriesFast();Long64_tnbytes=0,nb=0;for(Long64_tjentry=0;jentry<nentries;jentry++){Long64_tientry=LoadTree(jentry);if(ientry<0)break;nb=fChain->GetEntry(jentry);nbytes+=nb;//if(Cut(ientry)<0)con5nue;//Addeventselec5onandhistogramfillinghereFloat_thPt=0;Int_th_index=-1;if(pt->size()>0){for(Int_tj=0;j<pt->size();j++){if((*pt)[j]>hPt){hPt=(*pt)[j];h_index=j;}}h_pxpy->Fill((*px)[h_index],(*py)[h_index]);h_pt->Fill((*pt)[h_index]);}}//Plotthehistogramshereh_pxpy->Draw("colz");}
LorentzVector
21
ROOTprovidesgeneralfour-vectorclassesthatcanbeusedeitherforthedescrip5onofposi5onand5me(x,y,z,t)ormomentumandenergy(px,py,pz,E)
TLorentzVector• Itprovidesageneralfourvectorclassthathasvariousmethodsto
ini5alize,setandgetvariousquan55esofafourvector(e.g.,pt,eta,phi,massetc..)
• Performarithme5copera5ons,Lorentztransforma5ons• Computeangularsepara5onbetweenfourvectors• AsmallexampleofusingTLorentzVectorisgiveninnextslide• Moredetails:hNps://root.cern.ch/doc/master/classTLorentzVector.html
Moreadvancedfour-vectorclassesareprovidedbyROOTMathCorelibrary:hNps://root.cern.ch/root/html518/MATHCORE_Index.htmlForexample:ROOT::Math::LorentzVector<ROOT::Math::PxPyPzE4D<double>>
ExampleforTLorentzVector
22
ExampleforTLorentzVector
23
Runthemacro:root-ltree_example12.C