229 def reconstruct(self) -> pybasf2.Path:
230 """
231 Returns pybasf2.Path which creates the training data for the given particles
232 """
233 import ROOT
234 path = basf2.create_path()
235
236 for particle in self.particles:
238 nSignal = self.mc_counts[pdgcode]['sum']
239 print(f"FEI-core: TrainingData: nSignal for {particle.name}: {nSignal}")
240
241
242 if pdgcode > 400:
243 nSignal /= 1000
244
245 if pdgcode > 500:
246 nSignal /= 10000
247
248 for channel in particle.channels:
249 weightfile = f'{channel.label}.xml'
250 if basf2_mva.available(weightfile):
251 B2INFO(f"FEI-core: Skipping preparing Training Data for {weightfile}, already available")
252 continue
253 filename = 'training_input.root'
254
255
256 nBackground = self.mc_counts[0]['sum'] * channel.preCutConfig.bestCandidateCut
257 inverseSamplingRates = {}
258
259
260 if nBackground > Teacher.MaximumNumberOfMVASamples and not channel.preCutConfig.noBackgroundSampling:
261 inverseSamplingRates[0] = max(
262 1, int((int(nBackground / Teacher.MaximumNumberOfMVASamples) + 1) * channel.preCutConfig.bkgSamplingFactor))
263 elif channel.preCutConfig.bkgSamplingFactor > 1:
264 inverseSamplingRates[0] = int(channel.preCutConfig.bkgSamplingFactor)
265
266 if nSignal > Teacher.MaximumNumberOfMVASamples and not channel.preCutConfig.noSignalSampling:
267 inverseSamplingRates[1] = int(nSignal / Teacher.MaximumNumberOfMVASamples) + 1
268
269 spectators = [channel.mvaConfig.target] + list(channel.mvaConfig.spectators.keys())
270 if channel.mvaConfig.sPlotVariable is not None:
271 spectators.append(channel.mvaConfig.sPlotVariable)
272
273 if self.config.monitor:
274 hist_variables = ['mcErrors', 'mcParticleStatus'] + channel.mvaConfig.variables + spectators
275 hist_variables_2d = [(x, channel.mvaConfig.target)
276 for x in channel.mvaConfig.variables + spectators if x is not channel.mvaConfig.target]
277 hist_filename = os.path.join(self.config.monitoring_path, 'Monitor_TrainingData.root')
278 ma.variablesToHistogram(channel.name, variables=config.variables2binnings(hist_variables),
279 variables_2d=config.variables2binnings_2d(hist_variables_2d),
280 filename=hist_filename,
281 ignoreCommandLineOverride=True,
282 directory=config.removeJPsiSlash(f'{channel.label}'), path=path)
283
284 teacher = basf2.register_module('VariablesToNtuple')
285 teacher.set_name(f'VariablesToNtuple_{channel.name}')
286 teacher.param('fileName', filename)
287 teacher.param('treeName', ROOT.Belle2.MakeROOTCompatible.makeROOTCompatible(f'{channel.label} variables'))
288 teacher.param('variables', channel.mvaConfig.variables + spectators)
289 teacher.param('particleList', channel.name)
290 teacher.param('sampling', (channel.mvaConfig.target, inverseSamplingRates))
291 teacher.param('ignoreCommandLineOverride', True)
292 path.add_module(teacher)
293 return path
294
295