# Tree splitter macro problem

**URL:** <https://root-forum.cern.ch/t/tree-splitter-macro-problem/27614>\
**Category:** ROOT\
**Created:** [January 18, 2018, 12:04pm UTC](https://root-forum.cern.ch/t/tree-splitter-macro-problem/27614 "2018-01-18T12:04:45Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Geo](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/geo/32/15552_2.png) [@Geo](https://root-forum.cern.ch/u/Geo)\
**Post date:** [January 18, 2018, 12:04pm UTC](https://root-forum.cern.ch/t/tree-splitter-macro-problem/27614/1 "2018-01-18T12:04:45Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![fhoenig](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/f/bb73d2/32.png) [@fhoenig](https://root-forum.cern.ch/u/fhoenig)\
**Post date:** [January 18, 2018, 12:51pm UTC](https://root-forum.cern.ch/t/tree-splitter-macro-problem/27614/2 "2018-01-18T12:51:47Z")

</div>

Try casting the tree, like

```auto
TTree *full_tree = (TTree *) full_file->Get(“mytreecrrct”);

```

---

<div class="post-metadata">

**Author:** ![Wile\_E\_Coyote](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/wile_e_coyote/32/12417_2.png) [@Wile\_E\_Coyote](https://root-forum.cern.ch/u/Wile_E_Coyote)\
**Post date:** [January 18, 2018, 1:01pm UTC](https://root-forum.cern.ch/t/tree-splitter-macro-problem/27614/3 "2018-01-18T13:01:15Z")

</div>

```auto
TTree *full_tree; full_file->GetObject("mytreecrrct", full_tree);
if (!full_tree) std::cout << "NO mytreecrrct!" << std::endl;

```

---

<div class="post-metadata">

**Author:** ![ksmith](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/ksmith/32/12567_2.png) [@ksmith](https://root-forum.cern.ch/u/ksmith)\
**Post date:** [January 18, 2018, 2:49pm UTC](https://root-forum.cern.ch/t/tree-splitter-macro-problem/27614/4 "2018-01-18T14:49:43Z")

</div>

FYI:

> [@Getting the same histograms after cuts](https://root-forum.cern.ch/t/getting-the-same-histograms-after-cuts/24719/2):
>
> Remark on the way to post code snippets: When you post pieces of code on the forum enclose it with ``` . That makes your post much more readable (I systematically edit your posts that way) . So do: ``` // this is a C++ piece of code. ``` And it will display as: // this is a C++ piece of code.

---

<div class="post-metadata">

**Author:** ![Geo](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/geo/32/15552_2.png) [@Geo](https://root-forum.cern.ch/u/Geo)\
**Post date:** [January 19, 2018, 1:29pm UTC](https://root-forum.cern.ch/t/tree-splitter-macro-problem/27614/5 "2018-01-19T13:29:12Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Wile\_E\_Coyote](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/wile_e_coyote/32/12417_2.png) [@Wile\_E\_Coyote](https://root-forum.cern.ch/u/Wile_E_Coyote)\
**Post date:** [January 19, 2018, 1:49pm UTC](https://root-forum.cern.ch/t/tree-splitter-macro-problem/27614/6 "2018-01-19T13:49:45Z")

</div>

> [@Aux;1 aux;2 aux3;](https://root-forum.cern.ch/t/aux-1-aux-2-aux3/13345/2):
>
> Search for “cycle” and/or “namecycle”, for example in: In a Nutshell: [Input and Output](https://root.cern.ch/d/input-and-output) . ROOT User’s Guide: [Input/Output](https://root.cern.ch/root/htmldoc/guides/users-guide/ROOTUsersGuide.html#inputoutput) : [Retrieving Objects from Disk](https://root.cern.ch/root/htmldoc/guides/users-guide/ROOTUsersGuide.html#retrieving-objects-from-disk) , [Subdirectories and Navigation](https://root.cern.ch/root/htmldoc/guides/users-guide/ROOTUsersGuide.html#subdirectories-and-navigation) . Just for reference: [some additional notes about GetObject, Get, FindObjectAny, FindKeyAny, ReadObj, ReadObjAny](https://root-forum.cern.ch/t/broken-tdirectoryfile-findobjectany-and-tdirectoryfile-findkeyany/20943/16) .

---

<div class="post-metadata">

**Author:** ![system](https://root-forum.cern.ch/uploads/default/original/3X/e/f/efe4f24398fd8ffeb76bccb6ecc46d9d9c003376.png) [@system](https://root-forum.cern.ch/u/system)\
**Post date:** [February 2, 2018, 1:49pm UTC](https://root-forum.cern.ch/t/tree-splitter-macro-problem/27614/7 "2018-02-02T13:49:57Z")

</div>

Closed on February 2, 2018, 1:49pm UTC

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