// Example of program using root code
// by P. Gutierrez 

// Root stuff
#include <TROOT.h>
#include <TChain.h>
#include <TFile.h>
#include "TH2.h"

#include "root_template.hpp"

TFile *rootFile;
TTree *BTree;

RBJPSI Rjpsi;
RBK0 Rk0;

int main(){
  // Main program
  bookRoot();
  
  // Fill variables
  Rjpsi.run  = xxx;
  Rjpsi.evt  = yyy;
  Rjpsi.Mass = zzz;

  for (int iLoop=0; iLoop<10000; iLoop++){

    Rk0.k0num = bbb;
    for (int i=0, i<20, i++){
      Rk0.k0Mass[i] = aaa;
    }
    BTree->Fill();
  }
  closeRoot();
}

void bookRoot(){ 
 // Initialize ROOT
  rootFile = new TFile("output.root","recreate");
  BTree = new TTree("BTree", "B->J/psi + K* + pi");
  BTree->SetDirectory(rootFile);

  BTree->Branch("JPSI", &Rjpsi, "run/I:evt/I:psiMass/F:psiPx/F");
  BTree->Branch("K0", &Rk0, "k0num/I:k0id[20]/I:k0Mass[20]/F");
}

void closeRoot(){
  // Close and output the root file
  rootFile->cd();
  BTree->Write();
  BTree->Print();
  rootFile->Close();
  cout << " End Close Root " << endl;
}