Bender Tutorial Vanya Belyaev LAPPAnnecyleVieux ITEPMoscow Outline BenderPython
Bender Tutorial Vanya Belyaev LAPP/Annecy-le-Vieux & ITEP/Moscow
Outline • Bender/Python overview • Job configuration Bender is not frozen! • Data access • Histograms & N-Tuples • Algorithms • Please keep Lo. Ki manual with you Significant improvements in Bender semantics are expected (mainly according to the feedback from you) • • Especially for Function/Cuts tables $LHCBRELEASES/BENDER_v 4 r 1/Doc/Lo. Ki. Doc/v 3 r 5/doc/Lo. Ki’*’. (ps, pdf) Doxygen for Lo. Ki is also useful $LHCBRELEASES/BENDER_v 4 r 1/Doc/Lo. Ki. Doc/v 3 r 5/doc/html/index. html 14 Oct'2 k+4 Tutorial Bender: Python based physics analysis 2
Environment (I) • Bender v 4 r 1 ( based on Da. Vinci v 12 r 2 ) • The package Tutorial/Bender. Tutor v 1 r 0 • Only few essential features of Bender • Out of Tutorial scope • • visualization of histograms visualization of event and detector data CMT-free mode batch jobs 14 Oct'2 k+4 Tutorial Bender: Python based physics analysis 3
get the Tutorial package lbcmt Bender. Env v 4 r 1 cd $HOME/cmtuser getpack Tutorial/Bender. Tutor v 1 r 0 cd Tutorial/Bender. Tutor/v 1 r 0/cmt make source setup. csh ( or. setup. sh ) To be sub sti tut ed by Be nd er • + cm t. py Environment (II) 14 Oct'2 k+4 Tutorial Bender: Python based physics analysis 4
• • Python scripts could be executed as “scripts” > > python My. Bender. Script. py Python scripts could be executed from the command prompt ( explicit interactivity!) > python >>> import My. Bender. Script Python scripts could be executed with the command prompt (interactivity with “pawlogon. kumac” ) > python –i My. Bender. Script. py 14 Oct'2 k+4 Tutorial Bender: Python based physics analysis Common start-up script is possible, Pere has a lot of nice ideas! • Bender/Python tips 5
Structure of Gaudi Job Each “Job” contains 4 essential part Configuration of Job environment • • <Project. Env> scripts, CMT • Configuration of Job’s components • • • Bender: cmt. py Gaudi. Python + Top Level algorithms properties of Algorithms/Services/Tools Input/output • “Analysis Algorithm” coding • Job steering 14 Oct'2 k+4 Tutorial Bender Gaudi. Python + Bender: Python based physics analysis 6
2 approaches • Start from pure python prompt Attractive, define everything from Python but not practical Make a “smooth” transition from Da. Vinci/Lo. Ki start with existing configuration substitute it element by element • • Choice for tutorial 14 Oct'2 k+4 Tutorial Bender: Python based physics analysis 7
Minimal Analysis Job • Bender could be used with “no Bender” • Execute some “Da. Vinci” configuration • The actual configuration from ‘*’. opts file • Da. Vinci: Da. Vinci My. Options. File. opts 14 Oct'2 k+4 Tutorial Bender: Python based physics analysis 8
Minimal Bender script from bendermodule import * import benderconfig as bender To be improved Application and Components Configuration def configure() : bender. config( files = [‘My. Options. File. opts’]) return SUCCESS Job steering if __name__ == ‘__main__’ : configure() g. run(100) #g. exit() “g” → “gaudi” ? “app. Mgr”? . . /solutions/Minimalistic. py 14 Oct'2 k+4 Tutorial Bender: Python based physics analysis 9
• • “Hello, World!” (I) The simplest possible “algorithm” Follow Lo. Ki’s style: • • inherit the algorithm from useful base class (re)implement the “analyse” method class Hello. World(Algo) : def analyse( self ) : print ‘Hello, World!’ return SUCCESS. . /solutions/Hello. World. py 14 Oct'2 k+4 Tutorial Bender: Python based physics analysis 10
• One needs to instantiate the algorithm alg = Hello. World( ‘Hello’ ) Add it to the list of ‘active’ algorithms g. Top. Alg = [ ‘Hello’ ] Execute Application Configuration • • “Hello, World!” (II) Part of job steering block g. run(10) . . /solutions/Hello. World. py 14 Oct'2 k+4 Tutorial Bender: Python based physics analysis 11
Access to the data • C++: Gaudi. Algorithm/Lo. Ki const MCParticles* mcps = get<MCParticles>(‘MC/Particles’ ) • • • Python: Semantics to be improved Bender Get as ‘native’ object: mcps = self. get( address = ‘MC/Particles’) Get as std: : vector or Python’s list: mcps = self. get( address = vector = mcps = self. get( address = list = ‘MC/Particles’ , TRUE ) . . /solutions/Data. Access. py 14 Oct'2 k+4 Tutorial Bender: Python based physics analysis 12
Attributes and (python) loops for mcp in mcps : MCParticle print ‘ID=‘ , name. From. PID( mcp. particle. ID() ) print ‘PX=‘ , mcp. momentum(). px() print ‘PY=‘ , mcp. momentum(). py() • • From Dictionaries To know the available attributes: help( obj ) help( type( obj )) ON-LINE help for ALL Python/Bender functions/classes, sometimes it is VERY useful. . /solutions/Data. Access. py 14 Oct'2 k+4 Tutorial Bender: Python based physics analysis 13
Hands-on (I) • Simple algorithm which gets MCVertices from the Gaudi Transient Store and prints number of MCVertices and some information (e. g. x/y/z-position) for some of them Hints: The ‘*’. opts file, which could be used • • $BENDERTUTOROPTS/Bender. Tutor. opts • The analogous example for MCParticles: • . . /solutions/Data. Access. py • The actual solution is • . . /solutions/Hands. On 1. py 14 Oct'2 k+4 Tutorial Bender: Python based physics analysis 14
Lets start with physics analysis • >95% of Lo. Ki’s idioms are in Bender • The semantic is VERY similar • • In spite of different languages few ‘obvious’ exceptions • In the game: • All Functions/Cuts s s • All • a bit more round braces are required • All (v, mcv)select methods • loops , plots • for N-Tuples the functionality is a bit limited • A lack of template methods, • ‘farray’ need to be validated Start from MC-truth (requires no special configurations) 14 Oct'2 k+4 Tutorial Bender: Python based physics analysis 15
MCselect statement • Selection of MCParticles which satisfy the certain criteria: LUG, Tab. 13. 4, p. 84 Select m+ & mmcmu = self. mcselect( tag = ‘mcmu’ , cuts = ‘mu+’ == MCABSID ) beauty = self. mcselect( tag = ‘beauty’ , cuts = BEAUTY ) • Refine criteria: Everything which has b or b b mu. From. B = self. mcselect ( tag = ‘mu. From. C’, Everything from “decay” trees source = mcmu , (incl. decayon-flight) cuts = FROMMCTREE( beauty ) ) mu. PT = self. mcselect( tag = ‘with. PT’ , source = mu. From. B , cuts = ( MCPT > ( 1 * Ge. V ) )). . /solutions/MCmuons. py 14 Oct'2 k+4 Tutorial Bender: Python based physics analysis 16
Change input data • Get and configure Event. Selector evt. Sel = g. evt. Sel() evt. Sel. open( “file”) OR List of input files evt. Sel. open( [ “file 1”, “file 2”] ) • e. g. evt. Sel. open ( ‘LFN: /lhcb/production/DC 04/v 1/DST/00000543_00000017_5. dst’) 14 Oct'2 k+4 Tutorial Bender: Python based physics analysis 17
Hands On (II, II. 5) • Simple algorithm which evaluates the fractions of events which contains of at least B or beauty baryons which contains of at least Bs or beauty baryons Hints Relevant MCParticle functions LUG, Tab. 13. 4, p. 84 -87 • MCID, MCABSID , BEAUTY , BARYON • The most trivial “counter” is • The analogous algorithm is • . . /solutions/MCmuons. py • The real solution is if not Bs. empty() : self. Warning( message = ‘Bs’ ) • . . /solutions/Hands. On 2. py • . . /solutions/Hands. On 2. 5. py 14 Oct'2 k+4 Tutorial Bender: Python based physics analysis 18
Find MC–tree ( IMCDecay. Finder ) Brilliant tool from O. Dormond find the MC-decay trees: Container(“Range”) of • mc = self. mctruth() MCParticles trees = mc. find( decay = ‘[B_s 0 -> (J/psi(1 S) -> mu+ mu-) phi(1020)]cc’ ) • find MC-decay tree components: Container(“Range”) of MCParticles phis = mc. find( decay = ‘ phi(1020) : [B_s 0 -> (J/psi(1 S) -> mu+ mu-) phi(1020)]cc’ ) • extract ‘marked’ MC-decay tree components: mus = mc. find( decay = ‘ [B_s 0 -> (J/psi(1 S) -> mu+ ^mu-) phi(1020)]cc’ ) . . /solutions/MCTrees. py 14 Oct'2 k+4 Tutorial Bender: Python based physics analysis 19
Add simple histos! for mu in mus : self. plot ( title value high = = = ‘PT of muon from J/psi’ , MCPT( mu ) / Ge. V , 10 ) MCParticle The default values : low = 0, bins = 100, weight = 1 • Configuration for histograms: g. Histogram. Persistency = ‘HBOOK’ hsvc = g. service(‘Histogram. Persistency. Svc’) hsvc. Output. File = ‘myhistos. hbook’ . . /solutions/MCTrees. py 14 Oct'2 k+4 Tutorial Bender: Python based physics analysis 20
Add the simple N-Tuple tup = self. n. Tuple( title = ‘My N-Tuple’ ) z. Orig = MCVXFUN( MCVZ ) for mu in mus : tup. column( name = ‘PT’, value = MCPT( mu ) / Ge. V ) tup. column( name = ‘P’ , value = MCP( mu ) / Ge. V ) tup. column( name = ‘Z’ , value = z. Orig( mu ) / mm) tup. write() • Configuration: my. Alg = g. algorithm( ‘Mc. Tree’ ) my. Alg. NTuple. LUN = ‘MC’ ntsvc = g. service(‘NTuple. Svc’) ntsvc. Output = [“MC DATAFILE=‘tuples. hbook’ TYP=‘HBOOK’ OPT=‘NEW’ ”] . . /solutions/MCTrees. py 14 Oct'2 k+4 Tutorial Bender: Python based physics analysis 21
• Algorithms Component Properties My. Alg. NTuple. LUN = “LUNIT” ; alg = g. algorithm(‘My. Alg’) alg. NTuple. LUN = ‘LUNIT’ • Services Histogram. Persistency. Svc. Output. File = “histo. file”; hsvc = g. service(‘Histogram. Persistency. Svc’) hsvc. Output. File = ‘histo. file’ • Tools • My. Alg. Phys. Desktop. Input. Locations = {“/Event/Phys/Charged”}; tool = g. property(‘My. Alg. Phys. Desktop’) tool. Input. Locations = [‘/Event/Phys/Charged’] Everything prop = gaudi. i. Property(‘Holder. Name ’) Property. Name = Value 14 Oct'2 k+4 Tutorial Holder. Name. Property. Name = Value ; Bender: Python based physics analysis 22
Hands On (III) • The algorithm which gets the kaons from the decay Bs →J/y ( f →K+ K- ) , fill histo and N-Tuple Hints One need to define input MC files for this decay • • see. . /solutions/MCTrees. py • The similar algorithm • . . /solutions/MCTrees. py • The actual solution • . . /solutions/Hands. On 3. py 14 Oct'2 k+4 Tutorial Bender: Python based physics analysis 23
Go from MC to RC data • At this moment one knows how to: • Deal with MC trees, decays, particles • Perform simple (python) loops • Deal with histograms & N-Tuples • Some knowledge of ‘configuration’ • For RC data one must perform non-trivial algorithm configuration to be able to run • Input for RC particles (or Particle. Maker) • Dependency on ‘other’ algorithms ( ‘Pre. Load’ ) 14 Oct'2 k+4 Tutorial Bender: Python based physics analysis 24
Pre-Load Charged Particles (I) g. Top. Alg += [ ‘Lo. Ki. Pre. Load/Charged’] desktop = g. property(‘Charged. Phys. Desktop’) desktop. Particle. Maker. Type = ‘Combined. Particle. Maker’ “Universal” configuration suitable almost for all everything maker = g. property(‘Charged. Phys. Desktop. Combined. Paticle. Maker’) maker. Exclusive. Selection = 1>2 maker. Particles = [ ‘muon’ , ‘electron’ , ‘kaon’ , ‘proton’ , ‘pion’] Very loose cuts, to be refined in the algorithm maker. Muon. Selection maker. Electron. Selection maker. Kaon. Selection = [ maker. Proton. Selection maker. Pion. Selection = [ “det=‘MUON’ mu-pi=‘-10. 0’ ”] = [ “det=‘CALO’ e-pi=‘-2. 0’ ”] “det=‘RICH’ k-pi=‘-5. 0’ k-p=‘-5. 0’ ”] = [ “det=‘RICH’ p-pi=‘-5. 0’ ”] = [ “det=‘RICH pi-k=‘-5. 0’ ”] Complicated? ? 14 Oct'2 k+4 Tutorial Bender: Python based physics analysis 25
Pre-Load Charged Particles (II) Could be done a bit easier: g. Top. Alg += [ ‘Lo. Ki. Pre. Load/Charged’] import bender. Pre. Load as preload $BENDERPYTHON/bender. Pre. Load. py . . /solutions/RCSelect. py preload. Charged( Name=‘Charged’ , Kaons = [ “det=‘RICH’ k-pi=‘-5. 0’ k-p=‘-5. 0’” ] , Pions = [ “det=‘RICH’ pi-k=‘-5. 0’” ] ) • Alternatively (only hadrons, no e /m ) ± ± preload. Hadrons( Name=‘Charged’ , Kaons = [ “det=‘RICH’ k-pi=‘-5. 0’ k-p=‘-5. 0’” ] , Pions = [ “det=‘RICH’ pi-k=‘-5. 0’” ] Also for leptons (e±/m±) 14 Oct'2 k+4 Tutorial Bender: Python based physics analysis 26
Algorithm configuration desktop = g. property(‘My. Alg. Phys. Desktop’) desktop. Input. Locations = [ “/Event/Phys/Charged”] • Similar semantic in configuration ( ‘*’. opts ) files: My. Alg. Phys. Desktop. Input. Locations={“/Event/Phys/Charged”} ; . . /solutions/RCSelect. py 14 Oct'2 k+4 Tutorial Bender: Python based physics analysis 27
select/loop statements LUG, Tab. 13. 2, p. 62 -77 muons = self. select ( tag = ‘mu’ , cuts = ( ‘mu+’== ABSID ) & ( PT > (1*Ge. V) ) ) kaons = self. select ( tag = ‘K’ , cuts = ( ‘K+’== ABSID ) & ( PIDK > 0 ) ) • Loops: psis=self. loop(formula=‘mu mu’, pid=‘J/psi(1 S)’) phis=self. loop(formula=‘K K’, pid=‘phi(1020’) . . /solutions/RCSelect. py 14 Oct'2 k+4 Tutorial Bender: Python based physics analysis 28
Inside the loops (I) dmcut = ADMASS(‘J/psi(1 S)’) < ( 50 * Me. V ) for psi in psis : if not 2. 5*Ge. V< psi. mass(1, 2) <3. 5*Ge. V : continue Sq = 0 if not 0 == SUMQ( psi ) : continue c 2 VX < 49 if not 0 <= VCHI 2( psi ) < 49 : continue self. plot ( title = “ di-muon invariant mass” , value = M( psi ) / Ge. V , low = 2. 5 , high = 3. 50 ) |DM|<50 Me. V/c 2 if not dmcut( psi ) : continue psi. save(‘psi’) psis = self. selected(‘psi’) print ‘# of selected J/psi candidates: ‘, psis. size(). . /solutions/RCSelect. py 14 Oct'2 k+4 Tutorial Bender: Python based physics analysis 29
Inside the loops (II) dmcut = ADMASS(‘phi(1020’) < ( 12 * Me. V ) for phi in phis : if not phi. mass(1, 2) < 1050*Me. V : continue Sq = 0 if not 0 == SUMQ( phi ) : continue if not 0 <= VCHI 2( phi ) < 49 : continue c 2 VX < 49 self. plot ( title = “ di-kaon invariant mass” , value = M( phi ) / Ge. V , low = 1. 0 , high = 1. 050 ) |DM|<12 Me. V/c 2 if not dmcut( phi ) : continue phi. save(‘phi’) phis = self. selected(‘phi’) print ‘# of selected phi candidates: ‘, phis. size(). . /solutions/RCSelect. py 14 Oct'2 k+4 Tutorial Bender: Python based physics analysis 30
Inside the loops (III) dmcut = ADMASS(‘B_s 0’ ) < ( 100 * Me. V ) bs = self. loop ( formula = ‘psi phi’ , pid = ‘B_s 0’ ) for B in bs : if not 4. 5*Ge. V < B. mass(1, 2) < 6. 5*Ge. V : continue if not 0 <= VCHI 2( B ) < 49 : continue self. plot ( title = “ J/psi phi invariant mass” , value = M( B ) / Ge. V , low = 5. 0 , high = 6. 0 ) if not dmcut( B ) : continue B. save(‘Bs’) Bs = self. selected(‘Bs’) print ‘# of selected Bs candidates: ‘, Bs. size() if not Bs. empty() : self. set. Filter. Passed ( TRUE ). . /solutions/RCSelect. py 14 Oct'2 k+4 Tutorial Bender: Python based physics analysis 31
The last step: MC-truth match • The simplest case: check if RC particle originates from the certain MC-(sub)tree • The most frequent case • Check for efficiencies • Resolution • The opposite task: what MC particle “corresponds” to RC particle • similar ( MCTRUTH → RCTRUTH ) NB: Lo. Ki (and Bender) uses own concept of MC “loose” matching • LUG, chapter 15 14 Oct'2 k+4 Tutorial Bender: Python based physics analysis 32
MC-truth match • Select MC-particles mc = self. mctruth(‘MCdecay. Match’) mc. Bs = mc. find( decay = ‘ [B_s 0 -> (J/psi(1 S) -> mu+ mu-) phi(1020)]cc ’ ) mc. Phi = mc. find( decay = ‘ phi(1020) : [B_s 0 -> (J/psi(1 S) -> mu+ mu-) phi(1020)]cc ’ ) mc. Psi = mc. find( decay = ‘ J/psi(1 S) : [B_s 0 -> (J/psi(1 S) -> mu+ mu-) phi(1020)]cc ’ ) • Prepare ‘MC-Truth cuts’ mc. Cut. Bs mc. Cut. Phi mc. Cut. Psi = = = MCTRUTH ( ( ( mc mc mc , , , mc. Bs mc. Phi mc. Psi ) ) ) . . /solutions/RCMCSelect. py 14 Oct'2 k+4 Tutorial Bender: Python based physics analysis 33
The last step: MC-truth match for psi in psis : if not mc. Cut. Psi ( psi ) : continue … for phi in phis : if not mc. Cut. Phi ( phi ) : continue … for B in bs : if not mc. Cut. Bs ( B ) : continue … . . /solutions/RCMCSelect. py • Alternatively : for B in bs : psi = B(1) phi = B(2) … tup. column ( name = ‘mcpsi’ , value = mc. Cut. Psi( psi ) ) tup. column ( name = ‘mcphi’ , value = mc. Cut. Phi( phi ) ) tup. column ( name = ‘mc’ , value = mc. Cut. Bs ( B ) ) tup. write() 14 Oct'2 k+4 Tutorial Bender: Python based physics analysis 34
Hands On (IV) • Simple algorithm which selects kaons, plot di-kaon invariant mass with and without MC-truth flags with different PIDK ( = DLL(K-p) ) values (& fill N-Tuple with such information) Hints The relevant functions/cuts • • PIDK, MCTRUTH • The analogous algorithm • . . /solutions/RCMCSelect. py • The actual solution • . . /solutions/Hands. On 4. py 14 Oct'2 k+4 Tutorial Bender: Python based physics analysis 35
Histo visualization • get the histogram hsvc = g. hist. Svc() histo = hsvc[‘/stat/My. Alg/1’] from bender<XXX> import plotter • <XXX> = ROOT, Pi. Root, Pi. Hippo • Panoramix/La. Joconde – not for today plotter. plot(histo) • for N-tuples: <XXX> = ROOT g. Histogram. Persistency = ‘ROOT’ 14 Oct'2 k+4 Tutorial Bender: Python based physics analysis 36
Everything can be combined Hippo. Draw Panoramix/La. Joconde PI/ROOT Bender/Python prompt 14 Oct'2 k+4 Tutorial Bender: Python based physics analysis 37
Lo. Ki Bender • Loki is a god of wit and mischief in Norse mythology • Loops & Kinematics • • • Ostap Suleiman Berta Maria Bender-bei The cult-hero of books by I. Il’f & E. Petrov: “The 12 chairs” , “The golden calf” The title: “The great schemer” Attractive & brilliant cheater Essential for successful and good physics analysis 14 Oct'2 k+4 Tutorial Bender: Python based physics analysis 38
- Slides: 38