Belle II Software light-2406-ragdoll
variablesToNtuple.py
1#!/usr/bin/env python3
2
3
10
11# The VariablesToNtuple module saves variables from the VariableManager
12# to a candidate-based (or event-based) TTree
13#
14# For full documentation please refer to https://software.belle2.org
15# Anything unclear? Ask questions at https://questions.belle2.org
16
17import basf2
18import modularAnalysis as ma # a shorthand for the analysis tools namespace
19
20mypath = basf2.Path() # create a new path
21
22# add input data and ParticleLoader modules to the path
23ma.inputMdstList([basf2.find_file('analysis/tests/mdst.root')], path=mypath)
24ma.fillParticleLists([('K-', 'kaonID > 0.2'), ('pi+', 'pionID > 0.2')], path=mypath)
25ma.reconstructDecay('D0 -> K- pi+', '1.750 < M < 1.95', path=mypath)
26ma.matchMCTruth('D0', path=mypath)
27
28# Add the VariablesToNtuple module explicitly.
29# This will write out one row per candidate in the D0 list
30mypath.add_module('VariablesToNtuple',
31 particleList='D0',
32 variables=['dM', 'isSignal', 'mcErrors', 'p', 'E',
33 'daughter(0, kaonID)', 'daughter(1, pionID)'],
34 fileName='CandidateVariables.root')
35
36# Add another instance of the VariablesToNtuple module.
37# If the particle list is empty one row per event is written to the Ntuple,
38# but all the variables to specify have to be event-based
39mypath.add_module('VariablesToNtuple',
40 particleList='',
41 variables=['nTracks', 'isMC', 'year'],
42 fileName='EventVariables.root')
43
44# One can also call the VariablesToNtuple module in the roe_path to store the ROE particles' variables.
45# Build ROE and append a mask for cleanup
46ma.buildRestOfEvent('D0', path=mypath)
47cleanMask = ('cleanMask', # mask name
48 'nCDCHits > 0 and useCMSFrame(p)<=3.2', # criteria on tracks
49 'p >= 0.05 and useCMSFrame(p)<=3.2' # criteria on ECL-clusters
50 )
51ma.appendROEMasks('D0', mask_tuples=[cleanMask], path=mypath)
52
53# Make another path that is called in for_each loop over ROE objects
54roe_path = basf2.Path()
55ma.fillParticleList('pi+:inRoe', 'isInRestOfEvent > 0 and passesROEMask(cleanMask)', path=roe_path)
56ma.variablesToNtuple(decayString='pi+:inRoe',
57 variables=['p', 'E'],
58 filename='CandidateVariables.root',
59 treename='roe',
60 signalSideParticleList='D0', # index of D0 is stored in the branch '__signalSideCandidate__'
61 path=roe_path)
62mypath.for_each('RestOfEvent', 'RestOfEvents', roe_path)
63
64
65# you might also like to uncomment the following, and read the help for the
66# convenient wrapper function:
67# print(help(ma.variablesToNtuple))
68
69# process the data
70basf2.process(mypath)
71print(basf2.statistics)