Failed filling branch when applying the BDT result

Hi, I am new to TMVA and ROOT. I am trying to use two separate BDTGs on my dataset, and I encounter the error shown in the screenshot when I run my evaluation script.

Attached is my script

Thanks!

#include

void ApplyBDTG() {
// Open the file with the trained model
TFile *input1 = TFile::Open(“TMVA_11_Down.root”);
TFile *input2 = TFile::Open(“TMVA_11_Down_NeutralVariables.root”);
TMVA::Reader *reader1 = new TMVA::Reader(“!Color:!Silent”);
TMVA::Reader *reader2 = new TMVA::Reader(“!Color:!Silent”);

// Add variables to the reader, same as you added to dataloader
Double_t Bu_PTASY_1_5, Bach_PT, Bu_RHO_BPV, Bu_DIRA_BPV, D0_DIRA_BPV, D0_RHO_BPV;
Double_t Bach_IPCHI2_OWNPV, Bu_IPCHI2_OWNPV, Bu_MAXDOCA, D0_IPCHI2_OWNPV, D0_MAXDOCA, Bu_FDCHI2_OWNPV;
Double_t Bach_P, D0_VTXCHI2DOF, pi0_CL_gamma0, pi0_CL_gamma1, pi0_PP_IsNotE_gamma0, pi0_PP_IsNotE_gamma1, pi0_PP_IsNotE_gamma01, Bu_M, D0_M;

Float_t f_Bu_PTASY_1_5, f_Log_Bach_PT, f_Log_Bu_RHO_BPV, f_Log10_Bu_DIRA_BPV, f_Log_BU_constD0pi0PV_D0_PT, f_Log10_D0_DIRA_BPV, f_Log_D0_RHO_BPV, f_Log_Bu_constD0pi0PV_P;
Float_t f_Log_Bu_FDCHI2_OWNPV, f_Log_Bach_IPCHI2_OWNPV, f_Log_Bu_IPCHI2_OWNPV, f_Bu_MAXDOCA, f_Log_D0_IPCHI2_OWNPV, f_D0_MAXDOCA, f_Bu_FDCHI2_OWNPV, f_Log_BU_constD0pi0PV_PT;
Float_t f_Log_Bu_constD0pi0PV_D0_P, f_Log_Bach_P, f_Log_D0_VTXCHI2DOF, f_pi0_CL_gamma0, f_pi0_CL_gamma1, f_pi0_PP_IsNotE_gamma0, f_pi0_PP_IsNotE_gamma1, f_pi0_PP_IsNotE_gamma01, f_Bu_M, f_D0_M;
Float_t Bu_constD0pi0PV_D0_PX, Bu_constD0pi0PV_D0_PY, Bu_constD0pi0PV_P, Bu_constD0pi0PV_PX, Bu_constD0pi0PV_PY, Bu_constD0pi0PV_D0_P;

reader1->AddVariable("Bu_PTASY_1.5", &f_Bu_PTASY_1_5);
reader1->AddVariable("TMath::Log(Bach_PT)", &f_Log_Bach_PT);
reader1->AddVariable("TMath::Log(Bu_RHO_BPV)", &f_Log_Bu_RHO_BPV);
reader1->AddVariable("TMath::Log10(1-Bu_DIRA_BPV)", &f_Log10_Bu_DIRA_BPV);
reader1->AddVariable("TMath::Log(sqrt(Bu_constD0pi0PV_D0_PX*Bu_constD0pi0PV_D0_PX+Bu_constD0pi0PV_D0_PY*Bu_constD0pi0PV_D0_PY))", &f_Log_BU_constD0pi0PV_D0_PT);
reader1->AddVariable("TMath::Log10(1-D0_DIRA_BPV)", &f_Log10_D0_DIRA_BPV);
reader1->AddVariable("TMath::Log(D0_RHO_BPV)", &f_Log_D0_RHO_BPV);
reader1->AddVariable("TMath::Log(Bu_constD0pi0PV_P)", &f_Log_Bu_constD0pi0PV_P);
reader1->AddVariable("TMath::Log(Bu_FDCHI2_OWNPV)", &f_Log_Bu_FDCHI2_OWNPV);
reader1->AddVariable("TMath::Log(Bach_IPCHI2_OWNPV)", &f_Log_Bach_IPCHI2_OWNPV);
reader1->AddVariable("TMath::Log(Bu_IPCHI2_OWNPV)", &f_Log_Bu_IPCHI2_OWNPV);
reader1->AddVariable("Bu_MAXDOCA", &f_Bu_MAXDOCA);
reader1->AddVariable("TMath::Log(D0_IPCHI2_OWNPV)", &f_Log_D0_IPCHI2_OWNPV);
reader1->AddVariable("D0_MAXDOCA", &f_D0_MAXDOCA);
reader1->AddVariable("Bu_FDCHI2_OWNPV", &f_Bu_FDCHI2_OWNPV);
reader1->AddVariable("TMath::Log(sqrt(Bu_constD0pi0PV_PX*Bu_constD0pi0PV_PX+Bu_constD0pi0PV_PY*Bu_constD0pi0PV_PY))", &f_Log_BU_constD0pi0PV_PT);
reader1->AddVariable("TMath::Log(Bu_constD0pi0PV_D0_P)", &f_Log_Bu_constD0pi0PV_D0_P);
reader1->AddVariable("TMath::Log(Bach_P)", &f_Log_Bach_P);
reader1->AddVariable("TMath::Log(D0_VTXCHI2DOF)", &f_Log_D0_VTXCHI2DOF);
reader1->AddSpectator("pi0_CL_gamma0", &f_pi0_CL_gamma0);
reader1->AddSpectator("pi0_CL_gamma1", &f_pi0_CL_gamma1);
reader1->AddSpectator("pi0_PP_IsNotE_gamma0", &f_pi0_PP_IsNotE_gamma0);
reader1->AddSpectator("pi0_PP_IsNotE_gamma1", &f_pi0_PP_IsNotE_gamma1);
reader1->AddSpectator("pi0_PP_IsNotE_gamma0*pi0_PP_IsNotE_gamma1", &f_pi0_PP_IsNotE_gamma01);
reader1->AddSpectator("Bu_M",&f_Bu_M);
reader1->AddSpectator("D0_M",&f_D0_M);
reader2->AddVariable("pi0_CL_gamma0", &f_pi0_CL_gamma0);
reader2->AddVariable("pi0_CL_gamma1", &f_pi0_CL_gamma1);
reader2->AddVariable("pi0_PP_IsNotE_gamma0", &f_pi0_PP_IsNotE_gamma0);
reader2->AddVariable("pi0_PP_IsNotE_gamma1", &f_pi0_PP_IsNotE_gamma1);
reader2->AddVariable("pi0_PP_IsNotE_gamma0*pi0_PP_IsNotE_gamma1", &f_pi0_PP_IsNotE_gamma01);
reader2->AddSpectator("Bu_M",&f_Bu_M);
reader2->AddSpectator("D0_M",&f_D0_M);

// Load the trained model
reader1->BookMVA("BDTG1 method", "dataset_11_Down/weights/TMVAClassification_BDTG.weights.xml");
reader2->BookMVA("BDTG2 method", "dataset/weights/TMVAClassification_BDTG.weights.xml");

// Open the non-sideband data file and get the TTree
TFile *dataFile = TFile::Open("/data/lhcb/users/stanislauss/B2D0hpi0/data/raw/merged/NewSamples_NeutralVars/Bu2Dhpi0_D2hh_11_Down.root");
TTree *dataTree = (TTree*)dataFile->Get("B2D0PiPi0Resolved_D2KPi/DecayTree");

// Prepare the variables and link them to the branches
dataTree->SetBranchAddress("Bu_PTASY_1.5", &Bu_PTASY_1_5);
dataTree->SetBranchAddress("Bach_PT", &Bach_PT);
dataTree->SetBranchAddress("Bu_RHO_BPV", &Bu_RHO_BPV);
dataTree->SetBranchAddress("Bu_DIRA_BPV", &Bu_DIRA_BPV);
dataTree->SetBranchAddress("Bu_constD0pi0PV_D0_PX", &Bu_constD0pi0PV_D0_PX);
dataTree->SetBranchAddress("Bu_constD0pi0PV_D0_PY", &Bu_constD0pi0PV_D0_PY);
dataTree->SetBranchAddress("D0_DIRA_BPV", &D0_DIRA_BPV);
dataTree->SetBranchAddress("D0_RHO_BPV", &D0_RHO_BPV);
dataTree->SetBranchAddress("Bu_constD0pi0PV_P", &Bu_constD0pi0PV_P);
dataTree->SetBranchAddress("Bu_FDCHI2_OWNPV", &Bu_FDCHI2_OWNPV);
dataTree->SetBranchAddress("Bach_IPCHI2_OWNPV", &Bach_IPCHI2_OWNPV);
dataTree->SetBranchAddress("Bu_IPCHI2_OWNPV", &Bu_IPCHI2_OWNPV);
dataTree->SetBranchAddress("Bu_MAXDOCA", &Bu_MAXDOCA);
dataTree->SetBranchAddress("D0_IPCHI2_OWNPV", &D0_IPCHI2_OWNPV);
dataTree->SetBranchAddress("D0_MAXDOCA", &D0_MAXDOCA);
dataTree->SetBranchAddress("Bu_FDCHI2_OWNPV", &Bu_FDCHI2_OWNPV);
dataTree->SetBranchAddress("Bu_constD0pi0PV_PX", &Bu_constD0pi0PV_PX);
dataTree->SetBranchAddress("Bu_constD0pi0PV_PY", &Bu_constD0pi0PV_PY);
dataTree->SetBranchAddress("Bu_constD0pi0PV_D0_P", &Bu_constD0pi0PV_D0_P);
dataTree->SetBranchAddress("Bach_P", &Bach_P);
dataTree->SetBranchAddress("D0_VTXCHI2DOF", &D0_VTXCHI2DOF);
dataTree->SetBranchAddress("pi0_CL_gamma0", &pi0_CL_gamma0);
dataTree->SetBranchAddress("pi0_CL_gamma1", &pi0_CL_gamma1);
dataTree->SetBranchAddress("pi0_PP_IsNotE_gamma0", &pi0_PP_IsNotE_gamma0);
dataTree->SetBranchAddress("pi0_PP_IsNotE_gamma1", &pi0_PP_IsNotE_gamma1);
dataTree->SetBranchAddress("Bu_M",&Bu_M);
dataTree->SetBranchAddress("D0_M",&D0_M);

// Create a new branch to store the BDTG scores
Float_t BDTG1_score;
Float_t BDTG2_score;
TBranch *bdtg1Branch = dataTree->Branch("BDTG1_score", &BDTG1_score, "BDTG1_score/F");
TBranch *bdtg2Branch = dataTree->Branch("BDTG2_score", &BDTG2_score, "BDTG2_score/F");

// Loop over the non-sideband data and calculate BDTG scores
for (Long64_t i = 0; i < dataTree->GetEntries(); i++) {
    dataTree->GetEntry(i);

    f_Bu_PTASY_1_5 = static_cast<Float_t>(Bu_PTASY_1_5);
    f_Log_Bach_PT = static_cast<Float_t>(log(Bach_PT));
    f_Log_Bu_RHO_BPV = static_cast<Float_t>(log(Bu_RHO_BPV));
    f_Log10_Bu_DIRA_BPV = static_cast<Float_t>(log10(1-Bu_DIRA_BPV));
    f_Log_BU_constD0pi0PV_D0_PT = static_cast<Float_t>(log(sqrt(Bu_constD0pi0PV_D0_PX*Bu_constD0pi0PV_D0_PX+Bu_constD0pi0PV_D0_PY*Bu_constD0pi0PV_D0_PY)));
    f_Log10_D0_DIRA_BPV = static_cast<Float_t>(log10(1-D0_DIRA_BPV));
    f_Log_D0_RHO_BPV = static_cast<Float_t>(log(D0_RHO_BPV));
    f_Log_Bu_constD0pi0PV_P = static_cast<Float_t>(log(Bu_constD0pi0PV_P));
    f_Log_Bu_FDCHI2_OWNPV = static_cast<Float_t>(log(Bu_FDCHI2_OWNPV));
    f_Log_Bach_IPCHI2_OWNPV = static_cast<Float_t>(log(Bach_IPCHI2_OWNPV));
    f_Log_Bu_IPCHI2_OWNPV = static_cast<Float_t>(log(Bu_IPCHI2_OWNPV));
    f_Bu_MAXDOCA = static_cast<Float_t>(Bu_MAXDOCA);
    f_Log_D0_IPCHI2_OWNPV = static_cast<Float_t>(log(D0_IPCHI2_OWNPV));
    f_D0_MAXDOCA = static_cast<Float_t>(D0_MAXDOCA);
    f_Bu_FDCHI2_OWNPV = static_cast<Float_t>(Bu_FDCHI2_OWNPV);
    f_Log_BU_constD0pi0PV_PT = static_cast<Float_t>(log(sqrt(Bu_constD0pi0PV_PX*Bu_constD0pi0PV_PX+Bu_constD0pi0PV_PY*Bu_constD0pi0PV_PY)));
    f_Log_Bu_constD0pi0PV_D0_P = static_cast<Float_t>(log(Bu_constD0pi0PV_D0_P));
    f_Log_Bach_P = static_cast<Float_t>(log(Bach_P));
    f_Log_D0_VTXCHI2DOF = static_cast<Float_t>(log(D0_VTXCHI2DOF));
    f_pi0_CL_gamma0 = static_cast<Float_t>(pi0_CL_gamma0);
    f_pi0_CL_gamma1 = static_cast<Float_t>(pi0_CL_gamma1);
    f_pi0_PP_IsNotE_gamma0 = static_cast<Float_t>(pi0_PP_IsNotE_gamma0);
    f_pi0_PP_IsNotE_gamma1 = static_cast<Float_t>(pi0_PP_IsNotE_gamma1);
    f_pi0_PP_IsNotE_gamma01 = static_cast<Float_t>(pi0_PP_IsNotE_gamma0*pi0_PP_IsNotE_gamma1);
    f_Bu_M = static_cast<Float_t>(Bu_M);
    f_D0_M = static_cast<Float_t>(D0_M);

    BDTG1_score = reader1->EvaluateMVA("BDTG1 method");
    BDTG2_score = reader2->EvaluateMVA("BDTG2 method");
    dataTree->Fill();
    //bdtg1Branch->Fill();
    //bdtg2Branch->Fill();
}

// Save the new data with BDTG scores into a new file
TFile *outputFile = new TFile("Evaluation.root", "RECREATE");
dataTree->Write();
outputFile->Close();

// Clean up
delete reader1;
delete reader2;
input1->Close();
input2->Close();
dataFile->Close();

}
This is my code

This is a part of the error, I don’t know why I cannot add the full error message here, and it says new users cannot put links in posts…
This error is symptomatic of a Tree created as a memory-resident Tree
Instead of doing:
TTree *T = new TTree(…)
TFile *f = new TFile(…)
you should do:
TFile *f = new TFile(…)
TTree *T = new TTree(…)

You are getting the tree from a TFile that you open as read-only, and you want to write that same tree to another file. Also, note that when a TTree is created, it is associated to the last file that was opened (that’s why the error message says you should first create the TFile and then the TTree). A safe option is to first create your output TFile, then clone the original tree into a new TTree (which, thus, belongs to the new TFile), and work on this clone, then save the clone. Something like:

  // ...

  // Open the non-sideband data file and get the "original" TTree
  TFile *dataFile = TFile::Open("/data/lhcb/users/stanislauss/B2D0hpi0/data/raw/merged/NewSamples_NeutralVars/Bu2Dhpi0_D2hh_11_Down.root");
  TTree *dataTreeOrig = (TTree*)dataFile->Get("B2D0PiPi0Resolved_D2KPi/DecayTree");

  // new TFile and new TTree
  TFile *outputFile = new TFile("Evaluation.root", "RECREATE");
  TTree *dataTree = dataTreeOrig->CloneTree();
  // Prepare the variables and link them to the branches
  dataTree->SetBranchAddress("Bu_PTASY_1.5", &Bu_PTASY_1_5);
  // ...

  // in the loop over the entries, fill the TBranches (not the TTree):
    //dataTree->Fill();
    bdtg1Branch->Fill();
    bdtg2Branch->Fill();
  }

  // at the end, write the TTree (or the TFile)
  outputFile->cd();     // for safety, if doing dataTree->Write()
  dataTree->Write();    // or outputFile->Write();
  outputFile->Close();
}

Hi dastudillo,

Thank you for helping me and I think you made a good point. However, I am still getting the same error even after I created the cloned tree. Could you please help me again? Here’s my code now:

#include

void ApplyBDTG() {
// Open the file with the trained model
TFile *input1 = TFile::Open(“TMVA_11_Down.root”);
TFile *input2 = TFile::Open(“TMVA_11_Down_NeutralVariables.root”);
TMVA::Reader *reader1 = new TMVA::Reader(“!Color:!Silent”);
TMVA::Reader *reader2 = new TMVA::Reader(“!Color:!Silent”);

// Add variables to the reader, same as you added to dataloader
Double_t Bu_PTASY_1_5, Bach_PT, Bu_RHO_BPV, Bu_DIRA_BPV, D0_DIRA_BPV, D0_RHO_BPV;
Double_t Bach_IPCHI2_OWNPV, Bu_IPCHI2_OWNPV, Bu_MAXDOCA, D0_IPCHI2_OWNPV, D0_MAXDOCA, Bu_FDCHI2_OWNPV;
Double_t Bach_P, D0_VTXCHI2DOF, pi0_CL_gamma0, pi0_CL_gamma1, pi0_PP_IsNotE_gamma0, pi0_PP_IsNotE_gamma1, pi0_PP_IsNotE_gamma01, Bu_M, D0_M;

Float_t f_Bu_PTASY_1_5, f_Log_Bach_PT, f_Log_Bu_RHO_BPV, f_Log10_Bu_DIRA_BPV, f_Log_BU_constD0pi0PV_D0_PT, f_Log10_D0_DIRA_BPV, f_Log_D0_RHO_BPV, f_Log_Bu_constD0pi0PV_P;
Float_t f_Log_Bu_FDCHI2_OWNPV, f_Log_Bach_IPCHI2_OWNPV, f_Log_Bu_IPCHI2_OWNPV, f_Bu_MAXDOCA, f_Log_D0_IPCHI2_OWNPV, f_D0_MAXDOCA, f_Bu_FDCHI2_OWNPV, f_Log_BU_constD0pi0PV_PT;
Float_t f_Log_Bu_constD0pi0PV_D0_P, f_Log_Bach_P, f_Log_D0_VTXCHI2DOF, f_pi0_CL_gamma0, f_pi0_CL_gamma1, f_pi0_PP_IsNotE_gamma0, f_pi0_PP_IsNotE_gamma1, f_pi0_PP_IsNotE_gamma01, f_Bu_M, f_D0_M;
Float_t Bu_constD0pi0PV_D0_PX, Bu_constD0pi0PV_D0_PY, Bu_constD0pi0PV_P, Bu_constD0pi0PV_PX, Bu_constD0pi0PV_PY, Bu_constD0pi0PV_D0_P;

reader1->AddVariable("Bu_PTASY_1.5", &f_Bu_PTASY_1_5);
reader1->AddVariable("TMath::Log(Bach_PT)", &f_Log_Bach_PT);
reader1->AddVariable("TMath::Log(Bu_RHO_BPV)", &f_Log_Bu_RHO_BPV);
reader1->AddVariable("TMath::Log10(1-Bu_DIRA_BPV)", &f_Log10_Bu_DIRA_BPV);
reader1->AddVariable("TMath::Log(sqrt(Bu_constD0pi0PV_D0_PX*Bu_constD0pi0PV_D0_PX+Bu_constD0pi0PV_D0_PY*Bu_constD0pi0PV_D0_PY))", &f_Log_BU_constD0pi0PV_D0_PT);
reader1->AddVariable("TMath::Log10(1-D0_DIRA_BPV)", &f_Log10_D0_DIRA_BPV);
reader1->AddVariable("TMath::Log(D0_RHO_BPV)", &f_Log_D0_RHO_BPV);
reader1->AddVariable("TMath::Log(Bu_constD0pi0PV_P)", &f_Log_Bu_constD0pi0PV_P);
reader1->AddVariable("TMath::Log(Bu_FDCHI2_OWNPV)", &f_Log_Bu_FDCHI2_OWNPV);
reader1->AddVariable("TMath::Log(Bach_IPCHI2_OWNPV)", &f_Log_Bach_IPCHI2_OWNPV);
reader1->AddVariable("TMath::Log(Bu_IPCHI2_OWNPV)", &f_Log_Bu_IPCHI2_OWNPV);
reader1->AddVariable("Bu_MAXDOCA", &f_Bu_MAXDOCA);
reader1->AddVariable("TMath::Log(D0_IPCHI2_OWNPV)", &f_Log_D0_IPCHI2_OWNPV);
reader1->AddVariable("D0_MAXDOCA", &f_D0_MAXDOCA);
reader1->AddVariable("Bu_FDCHI2_OWNPV", &f_Bu_FDCHI2_OWNPV);
reader1->AddVariable("TMath::Log(sqrt(Bu_constD0pi0PV_PX*Bu_constD0pi0PV_PX+Bu_constD0pi0PV_PY*Bu_constD0pi0PV_PY))", &f_Log_BU_constD0pi0PV_PT);
reader1->AddVariable("TMath::Log(Bu_constD0pi0PV_D0_P)", &f_Log_Bu_constD0pi0PV_D0_P);
reader1->AddVariable("TMath::Log(Bach_P)", &f_Log_Bach_P);
reader1->AddVariable("TMath::Log(D0_VTXCHI2DOF)", &f_Log_D0_VTXCHI2DOF);
reader1->AddSpectator("pi0_CL_gamma0", &f_pi0_CL_gamma0);
reader1->AddSpectator("pi0_CL_gamma1", &f_pi0_CL_gamma1);
reader1->AddSpectator("pi0_PP_IsNotE_gamma0", &f_pi0_PP_IsNotE_gamma0);
reader1->AddSpectator("pi0_PP_IsNotE_gamma1", &f_pi0_PP_IsNotE_gamma1);
reader1->AddSpectator("pi0_PP_IsNotE_gamma0*pi0_PP_IsNotE_gamma1", &f_pi0_PP_IsNotE_gamma01);
reader1->AddSpectator("Bu_M",&f_Bu_M);
reader1->AddSpectator("D0_M",&f_D0_M);
reader2->AddVariable("pi0_CL_gamma0", &f_pi0_CL_gamma0);
reader2->AddVariable("pi0_CL_gamma1", &f_pi0_CL_gamma1);
reader2->AddVariable("pi0_PP_IsNotE_gamma0", &f_pi0_PP_IsNotE_gamma0);
reader2->AddVariable("pi0_PP_IsNotE_gamma1", &f_pi0_PP_IsNotE_gamma1);
reader2->AddVariable("pi0_PP_IsNotE_gamma0*pi0_PP_IsNotE_gamma1", &f_pi0_PP_IsNotE_gamma01);
reader2->AddSpectator("Bu_M",&f_Bu_M);
reader2->AddSpectator("D0_M",&f_D0_M);

// Load the trained model
reader1->BookMVA("BDTG1 method", "dataset_11_Down/weights/TMVAClassification_BDTG.weights.xml");
reader2->BookMVA("BDTG2 method", "dataset/weights/TMVAClassification_BDTG.weights.xml");

// Open the non-sideband data file and get the TTree
TFile *dataFile = TFile::Open("/data/lhcb/users/stanislauss/B2D0hpi0/data/raw/merged/NewSamples_NeutralVars/Bu2Dhpi0_D2hh_11_Down.root");
TTree *dataTreeOrig = (TTree*)dataFile->Get("B2D0PiPi0Resolved_D2KPi/DecayTree");

TFile *outputFile = new TFile("Evaluation.root", "RECREATE");
TTree *dataTree = dataTreeOrig->CloneTree();


// Prepare the variables and link them to the branches
dataTree->SetBranchAddress("Bu_PTASY_1.5", &Bu_PTASY_1_5);
dataTree->SetBranchAddress("Bach_PT", &Bach_PT);
dataTree->SetBranchAddress("Bu_RHO_BPV", &Bu_RHO_BPV);
dataTree->SetBranchAddress("Bu_DIRA_BPV", &Bu_DIRA_BPV);
dataTree->SetBranchAddress("Bu_constD0pi0PV_D0_PX", &Bu_constD0pi0PV_D0_PX);
dataTree->SetBranchAddress("Bu_constD0pi0PV_D0_PY", &Bu_constD0pi0PV_D0_PY);
dataTree->SetBranchAddress("D0_DIRA_BPV", &D0_DIRA_BPV);
dataTree->SetBranchAddress("D0_RHO_BPV", &D0_RHO_BPV);
dataTree->SetBranchAddress("Bu_constD0pi0PV_P", &Bu_constD0pi0PV_P);
dataTree->SetBranchAddress("Bu_FDCHI2_OWNPV", &Bu_FDCHI2_OWNPV);
dataTree->SetBranchAddress("Bach_IPCHI2_OWNPV", &Bach_IPCHI2_OWNPV);
dataTree->SetBranchAddress("Bu_IPCHI2_OWNPV", &Bu_IPCHI2_OWNPV);
dataTree->SetBranchAddress("Bu_MAXDOCA", &Bu_MAXDOCA);
dataTree->SetBranchAddress("D0_IPCHI2_OWNPV", &D0_IPCHI2_OWNPV);
dataTree->SetBranchAddress("D0_MAXDOCA", &D0_MAXDOCA);
dataTree->SetBranchAddress("Bu_FDCHI2_OWNPV", &Bu_FDCHI2_OWNPV);
dataTree->SetBranchAddress("Bu_constD0pi0PV_PX", &Bu_constD0pi0PV_PX);
dataTree->SetBranchAddress("Bu_constD0pi0PV_PY", &Bu_constD0pi0PV_PY);
dataTree->SetBranchAddress("Bu_constD0pi0PV_D0_P", &Bu_constD0pi0PV_D0_P);
dataTree->SetBranchAddress("Bach_P", &Bach_P);
dataTree->SetBranchAddress("D0_VTXCHI2DOF", &D0_VTXCHI2DOF);
dataTree->SetBranchAddress("pi0_CL_gamma0", &pi0_CL_gamma0);
dataTree->SetBranchAddress("pi0_CL_gamma1", &pi0_CL_gamma1);
dataTree->SetBranchAddress("pi0_PP_IsNotE_gamma0", &pi0_PP_IsNotE_gamma0);
dataTree->SetBranchAddress("pi0_PP_IsNotE_gamma1", &pi0_PP_IsNotE_gamma1);
dataTree->SetBranchAddress("Bu_M",&Bu_M);
dataTree->SetBranchAddress("D0_M",&D0_M);

// Create a new branch to store the BDTG scores
Float_t BDTG1_score;
Float_t BDTG2_score;
TBranch *bdtg1Branch = dataTree->Branch("BDTG1_score", &BDTG1_score, "BDTG1_score/F");
TBranch *bdtg2Branch = dataTree->Branch("BDTG2_score", &BDTG2_score, "BDTG2_score/F");

// Loop over the non-sideband data and calculate BDTG scores
for (Long64_t i = 0; i < dataTree->GetEntries(); i++) {
    dataTree->GetEntry(i);

    f_Bu_PTASY_1_5 = static_cast<Float_t>(Bu_PTASY_1_5);
    f_Log_Bach_PT = static_cast<Float_t>(log(Bach_PT));
    f_Log_Bu_RHO_BPV = static_cast<Float_t>(log(Bu_RHO_BPV));
    f_Log10_Bu_DIRA_BPV = static_cast<Float_t>(log10(1-Bu_DIRA_BPV));
    f_Log_BU_constD0pi0PV_D0_PT = static_cast<Float_t>(log(sqrt(Bu_constD0pi0PV_D0_PX*Bu_constD0pi0PV_D0_PX+Bu_constD0pi0PV_D0_PY*Bu_constD0pi0PV_D0_PY)));
    f_Log10_D0_DIRA_BPV = static_cast<Float_t>(log10(1-D0_DIRA_BPV));
    f_Log_D0_RHO_BPV = static_cast<Float_t>(log(D0_RHO_BPV));
    f_Log_Bu_constD0pi0PV_P = static_cast<Float_t>(log(Bu_constD0pi0PV_P));
    f_Log_Bu_FDCHI2_OWNPV = static_cast<Float_t>(log(Bu_FDCHI2_OWNPV));
    f_Log_Bach_IPCHI2_OWNPV = static_cast<Float_t>(log(Bach_IPCHI2_OWNPV));
    f_Log_Bu_IPCHI2_OWNPV = static_cast<Float_t>(log(Bu_IPCHI2_OWNPV));
    f_Bu_MAXDOCA = static_cast<Float_t>(Bu_MAXDOCA);
    f_Log_D0_IPCHI2_OWNPV = static_cast<Float_t>(log(D0_IPCHI2_OWNPV));
    f_D0_MAXDOCA = static_cast<Float_t>(D0_MAXDOCA);
    f_Bu_FDCHI2_OWNPV = static_cast<Float_t>(Bu_FDCHI2_OWNPV);
    f_Log_BU_constD0pi0PV_PT = static_cast<Float_t>(log(sqrt(Bu_constD0pi0PV_PX*Bu_constD0pi0PV_PX+Bu_constD0pi0PV_PY*Bu_constD0pi0PV_PY)));
    f_Log_Bu_constD0pi0PV_D0_P = static_cast<Float_t>(log(Bu_constD0pi0PV_D0_P));
    f_Log_Bach_P = static_cast<Float_t>(log(Bach_P));
    f_Log_D0_VTXCHI2DOF = static_cast<Float_t>(log(D0_VTXCHI2DOF));
    f_pi0_CL_gamma0 = static_cast<Float_t>(pi0_CL_gamma0);
    f_pi0_CL_gamma1 = static_cast<Float_t>(pi0_CL_gamma1);
    f_pi0_PP_IsNotE_gamma0 = static_cast<Float_t>(pi0_PP_IsNotE_gamma0);
    f_pi0_PP_IsNotE_gamma1 = static_cast<Float_t>(pi0_PP_IsNotE_gamma1);
    f_pi0_PP_IsNotE_gamma01 = static_cast<Float_t>(pi0_PP_IsNotE_gamma0*pi0_PP_IsNotE_gamma1);
    f_Bu_M = static_cast<Float_t>(Bu_M);
    f_D0_M = static_cast<Float_t>(D0_M);

    BDTG1_score = reader1->EvaluateMVA("BDTG1 method");
    BDTG2_score = reader2->EvaluateMVA("BDTG2 method");
    //dataTree->Fill();
    bdtg1Branch->Fill();
    bdtg2Branch->Fill();
}

// Save the new data with BDTG scores into a new file
outputFile->cd();
dataTree->Write();
outputFile->Close();

// Clean up
delete reader1;
delete reader2;
input1->Close();
input2->Close();
dataFile->Close();

}