Belle II Software  light-2212-foldex
memcheck.py
1 #!/usr/bin/env python3
2 # -*- coding: utf-8 -*-
3 
4 
11 
12 """This script is used in the nightly build to check for memory issues with valgrind.
13 It is run as a test to make sure the memcheck does not fail because of issues in the script."""
14 
15 from b2test_utils import skip_test_if_light
16 skip_test_if_light() # light builds don't contain simulation, reconstruction etc; skip before trying to import # noqa
17 
18 from basf2 import set_random_seed, set_log_level, LogLevel, create_path, Module, find_file, process, statistics, conditions
19 from simulation import add_simulation
20 from reconstruction import add_reconstruction
21 from mdst import add_mdst_output
22 from ROOT import Belle2
23 from modularAnalysis import reconstructDecay, rankByHighest, buildRestOfEvent, buildContinuumSuppression, matchMCTruth, \
24  variablesToNtuple, getAnalysisGlobaltag
25 from stdCharged import stdPi, stdMu
26 from vertex import treeFit, TagV
27 from flavorTagger import flavorTagger
28 from rawdata import add_packers, add_unpackers
29 import glob
30 import sys
31 import os
32 
33 # set the random seed to get reproducible results
34 set_random_seed(1)
35 
36 # suppress messages and warnings during processing:
37 set_log_level(LogLevel.ERROR)
38 
39 # add the analysis globaltag (necessary for flavor tagging)
40 conditions.prepend_globaltag(getAnalysisGlobaltag())
41 
42 # create path
43 main = create_path()
44 
45 # specify number of events to be generated
46 main.add_module('EventInfoSetter')
47 
48 
49 class StopModule(Module):
50  """Module to stop the event processing in case the argument InitOnly is given"""
51 
52  def event(self):
53  """Set the event meta data to end of data"""
54 
55  eventMetaData = Belle2.PyStoreObj('EventMetaData')
56  eventMetaData.setEndOfData()
57 
58 
59 if len(sys.argv) > 1 and sys.argv[1] == 'InitOnly':
60  main.add_module(StopModule())
61 
62 # print event numbers
63 evtmetainfo = main.add_module('EventInfoPrinter')
64 evtmetainfo.set_log_level(LogLevel.INFO)
65 
66 # generate BBbar events, with Bsig -> J/psi K0S
67 main.add_module('EvtGenInput', userDECFile=find_file('decfiles/dec/1111440100.dec'))
68 
69 # detector simulation
70 bg = None
71 if 'BELLE2_BACKGROUND_DIR' in os.environ:
72  bg = glob.glob(os.environ['BELLE2_BACKGROUND_DIR'] + '/*.root')
73 add_simulation(main, bkgfiles=bg)
74 
75 # reconstruction
76 add_reconstruction(main)
77 
78 # mdst output
79 add_mdst_output(main, True)
80 
81 # dst output
82 main.add_module('RootOutput', outputFileName='dst.root')
83 
84 
85 # use standard final state particle lists
86 stdPi('loose', path=main)
87 stdMu('loose', path=main)
88 
89 # reconstruct Ks -> pi+ pi- decay and keep only candidates with 0.4 < M(pipi) < 0.6 GeV
90 reconstructDecay('K_S0:pipi -> pi+:loose pi-:loose', cut='0.4 < M < 0.6', path=main)
91 
92 # reconstruct J/psi -> mu+ mu- decay and keep only candidates with 3.0 < M(mumu) < 3.2 GeV
93 reconstructDecay('J/psi:mumu -> mu+:loose mu-:loose', cut='3.0 < M < 3.2', path=main)
94 
95 # reconstruct B0 -> J/psi Ks decay and keep only candidates with 5.2 < M(J/PsiKs) < 5.4 GeV
96 reconstructDecay('B0:jpsiks -> J/psi:mumu K_S0:pipi', cut='5.2 < M < 5.4', path=main)
97 
98 # perform B0 kinematic vertex fit and keep candidates only passing C.L. value of the fit > 0.0 (no cut)
99 treeFit('B0:jpsiks', 0.0, path=main)
100 
101 # order candidates by chi2 probability
102 rankByHighest('B0:jpsiks', 'chiProb', path=main)
103 
104 # build the rest of the event associated to the B0
105 buildRestOfEvent('B0:jpsiks', path=main)
106 
107 # calculate continuum suppression variables
108 buildContinuumSuppression('B0:jpsiks', 'all', path=main)
109 
110 # perform MC matching (MC truth association).
111 matchMCTruth('B0:jpsiks', path=main)
112 
113 # do flavor tagging
114 flavorTagger('B0:jpsiks', path=main)
115 
116 # calculate the Tag Vertex and Delta t (in ps), breco: type of MC association.
117 TagV('B0:jpsiks', 'breco', path=main)
118 
119 # select variables that we want to store to ntuple
120 fs_vars = ['kaonID', 'muonID', 'dr', 'dz', 'pValue', 'isSignal', 'mcErrors', 'genMotherID']
121 b_vars = ['nTracks', 'Mbc', 'deltaE', 'p', 'E', 'useCMSFrame(p)', 'useCMSFrame(E)',
122  'isSignal', 'mcErrors', 'nROE_KLMClusters', 'qrOutput(FBDT)', 'TagVLBoost', 'TagVz', 'TagVzErr', 'mcDeltaT'] + \
123  ['daughter(0,daughter(0,%s))' % var for var in fs_vars]
124 
125 # save variables to ntuple
126 variablesToNtuple('B0:jpsiks', variables=b_vars, filename='ntuple.root', treename='B0tree', path=main)
127 
128 # do raw data packing and unpacking
129 add_packers(main)
130 add_unpackers(main)
131 for module in main.modules():
132  if module.type() == 'SVDUnpacker':
133  module.param('silentlyAppend', True)
134 
135 # gather profiling information
136 main.add_module('Profile', outputFileName='vmem_profile.png', rssOutputFileName='rss_profile.png').set_log_level(LogLevel.INFO)
137 
138 # execute all
139 process(main)
140 
141 # Print call statistics
142 print(statistics)
a (simplified) python wrapper for StoreObjPtr.
Definition: PyStoreObj.h:67
def event(self)
Definition: memcheck.py:52