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