Tree splitter macro problem

Hello everybody. I found online a macro that splits a given tree with N entries into two identical ones with N/2 entries each. A year ago I used it without any problems but now I don’t seem to get it working.

the macro is:
void splitter(TFile *full_file){

      TTree* full_tree = full_file->Get("mytreecrrct");

      string full_file_name = full_file->GetName();
      int lastindex = full_file_name.find_last_of(".");
      TString rawname = full_file_name.substr(0, lastindex);

      half1_file_name = rawname+"_half1.root";
      half2_file_name = rawname+"_half2.root";

      TFile* half1_file = new TFile(half1_file_name, "RECREATE");
      TTree* half1_tree = full_tree->CloneTree(0);

      TFile* half2_file = new TFile(half2_file_name, "RECREATE");
      TTree* half2_tree = full_tree->CloneTree(0);

      TRandom *r3 = new TRandom3();

      for (int i=0; i<full_tree->GetEntries(); i++){
        full_tree->GetEntry(i);
        Double_t rndm = r3->Rndm();

        if (rndm < 0.5) {
          half1_tree->Fill();
        }
        else {
          half2_tree->Fill();
        }
      }
      half1_file->Write();
      half2_file->Write();

      cout << full_file_name << " entries " << full_tree->GetEntries() << endl;
      cout << half1_file_name << " entries " << half1_tree->GetEntries() << endl;
      cout << half2_file_name << " entries " << half2_tree->GetEntries() << endl;
      cout << "1+2 entries: " << half1_tree->GetEntries() + half2_tree->GetEntries() << endl;
      cout << " " << endl;
    }

void tree_splitter() {

          // had
   TFile* full_file_correctsol_had = new TFile("/afs/cern.ch/work/g/gbillis/TMVA/crrct_sol_total.root","READ");
   splitter(full_file_correctsol_had);

 }

and the output when I compile it in ROOT is :

/afs/cern.ch/work/g/gbillis/TMVA/./tree_splitter.C:26:11: error: cannot initialize a variable of type ‘TTree *’ with
an rvalue of type 'TObject '
TTree
full_tree = full_file->Get(“mytreecrrct”);
^ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/afs/cern.ch/work/g/gbillis/TMVA/./tree_splitter.C:32:4: error: use of undeclared identifier 'half1_file_name’
half1_file_name = rawname+"_half1.root";
^
/afs/cern.ch/work/g/gbillis/TMVA/./tree_splitter.C:33:4: error: use of undeclared identifier 'half2_file_name’
half2_file_name = rawname+"_half2.root";
^
/afs/cern.ch/work/g/gbillis/TMVA/./tree_splitter.C:35:34: error: use of undeclared identifier 'half1_file_name’
TFile* half1_file = new TFile(half1_file_name, “RECREATE”);
^
/afs/cern.ch/work/g/gbillis/TMVA/./tree_splitter.C:38:34: error: use of undeclared identifier 'half2_file_name’
TFile* half2_file = new TFile(half2_file_name, “RECREATE”);
^
/afs/cern.ch/work/g/gbillis/TMVA/./tree_splitter.C:58:12: error: use of undeclared identifier 'half1_file_name’
cout << half1_file_name << " entries " << half1_tree->GetEntries() << endl;
^
/afs/cern.ch/work/g/gbillis/TMVA/./tree_splitter.C:59:12: error: use of undeclared identifier 'half2_file_name’
cout << half2_file_name << " entries " << half2_tree->GetEntries() << endl;

Can anyone help?

Try casting the tree, like

TTree *full_tree = (TTree *) full_file->Get(“mytreecrrct”);
1 Like
TTree *full_tree; full_file->GetObject("mytreecrrct", full_tree);
if (!full_tree) std::cout << "NO mytreecrrct!" << std::endl;

FYI:

Thank you everybody, the solution was that I missed the (TTree*). Now a new issue occurred. I have a tree that has 36965800 events and when I try to split it, the resulting files come with 2 trees names mytreewrng;27 and mytreewrng;28. The same thing did not happen when I split a much smaller file of 1000 events…anyone has any idea what is happening? I am mentioning the number of events because it is the only difference in the two files.

cheers
George

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