Trouble writing to file w/ TMVA

The following is my code for a BDT using TMVA. I need it to write the BDT response to a file, but that just is not happening… I suspect I’m not using the dataloader correctly, but I have messed with things with no success for a few days now. Any help would be appreciated.

signal_file=TFile('signal.root')    #signal
background_file=TFile('background.root')    #background
tree_s=signal_file.Get('microtuple')
tree_b=background_file.Get('microtuple')

# why not writing to file?
out_file=TFile('alllcuts_BDT.root', 'RECREATE')

factory = TMVA.Factory("TMVAClassification",out_file,"V:!Silent:Color:Transformations=I:DrawProgressBar:AnalysisType=Classification"); 

dataloader = TMVA.DataLoader()

dataloader.AddVariable('jj_mass', 'F')
dataloader.AddVariable('met_et', 'F')

dataloader.AddSignalTree(tree_s)
dataloader.AddBackgroundTree(tree_b)

# additional cuts
signal_cut=TCut('jj_mass>1000000 && met_et>150000')
background_cut=TCut('jj_mass>1000000 && met_et>150000')

dataloader.PrepareTrainingAndTestTree(signal_cut, background_cut, ':'.join(['nTrain_Signal=0', 'nTrain_Background=0',
                        'SplitMode=Random', 'NormMode=NumEvents', '!V']))

method = factory.BookMethod(dataloader, TMVA.Types.kBDT, 'BDTG', ':'.join(['!H', '!V', 'NTrees=850',
                     'MaxDepth=3', 'BoostType=Grad', 'Shrinkage=1.0',
                    'SeparationType=GiniIndex', 'nCuts=20', 'PruneMethod=NoPruning']))


factory.TrainAllMethods()
factory.TestAllMethods()
factory.EvaluateAllMethods()
print('done')

Hi,

Can you try running with

// Save the output
   out_file->Close();

in the very end? This should make sure the file and all contents gets written to disk.

Hi, Thanks for the suggestion! I tried that and it didn’t work. In the file that is created (allcuts_BDT.root), there is only one folder named default;1 … this corresponds to the argument i feed the dataloader constructer TMVA.Dataloader(). If I do dataloader = TMVA.DataLoader(“foldername”) then the output root file also has that folder. Except it’s blank…

Ok, too bad :confused:

Could you try running from c++ instead of pyROOT just to make sure that it is unrelated to that?

What is the output of TMVA when you run your script?

Success!!! I realized I was doing output_file.close() instead of output_file.Close()… My bad. Thank you so much, it works fine now!

Glad I could help! Please mark this post as solved to make it easier for people to find answers in the future :slight_smile:

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.