# Random behavior when drawing tree on multiple pads

**URL:** <https://root-forum.cern.ch/t/random-behavior-when-drawing-tree-on-multiple-pads/38736>\
**Category:** Newbie\
**Created:** [April 6, 2020, 11:29am UTC](https://root-forum.cern.ch/t/random-behavior-when-drawing-tree-on-multiple-pads/38736 "2020-04-06T11:29:08Z")\
**Posts on this page:** 15\
**Page:** 1

<div class="post-metadata">

**Author:** ![Excelsior](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/excelsior/32/21460_2.png) [@Excelsior](https://root-forum.cern.ch/u/Excelsior)\
**Post date:** [April 6, 2020, 11:29am UTC](https://root-forum.cern.ch/t/random-behavior-when-drawing-tree-on-multiple-pads/38736/1 "2020-04-06T11:29:08Z")

</div>

Hi,  
I am trying to draw a tree on multiple pads with different options. However, the results varied when I run the SAME code several times. The first one is what I expected:

 ![0](https://root-forum.cern.ch/uploads/default/original/3X/7/8/78fdd996d38883fd1a27af1c3f06dcab9cdc7dc3.png)  
But for most of the time, I got the latter one.  
Here is the macro I wrote to perform the drawing:

```auto
auto DrawT(TTree *const tree, const std::vector<size_t>& nEnts){	
	auto TC = new TCanvas();
	TC->Divide(2, 2);
	int colorInd = 0;
	size_t tcInd = 0;
	auto trd = tree->CloneTree();
	auto cBr = trd->Branch("color", &colorInd, "color/i");
	std::vector<const char*> nameV;
	for(auto branch: *(tree->GetListOfBranches()))	
		nameV.push_back(branch->GetName());
	for(size_t nEnt: nEnts){
		for(size_t i = 0; i != nEnt; ++i)
			cBr->Fill();
		++colorInd;
	} //Draw different trees with different colors
	trd->SetMarkerStyle(8);
	do{
		std::stringstream ss;
		ss<<nameV[0]<<':'<<nameV[1]<<':'<<nameV[2]<<':'<<"color";
		TC->cd(++tcInd); //Draw different combinations on different pads
		trd->Draw(ss.str().c_str(), "1");
	}while(next_combination(nameV.begin(), nameV.begin()+3, nameV.end()));
	return TC;
}

```

`next_combination` is a function I copied from [StackOverflow](https://stackoverflow.com/a/1617797) which generates combinations of elements in a collection. I assume it worked just fine since I have tested it in ROOT prompt separately.

Here is the code I entered in the prompt:

```auto
.L $PATH_TO_THE_MACRO
TTree *tDat = new TTree("train","data from csv file");
tDat->ReadFile("$PATH_TO_THE_CSV_FILE", "var0/D:var1/D:var2/D:var3/D", ',');
auto cdr = DrawT(tDat, {40, 40, 40}) //(TCanvas *) @0x7fffc008ea00

```

Additional information may help:  
I noticed that 4 graphs were drawn (the expected behavior) only when something was drawn previously, i.e. a TF1 function, but sometimes there were still 3 graphs drawn in this condition.  
_ROOT Version:_ 6.20/02  
_Platform:_ Arch Linux on WSL  
_Compiler:_ I installed the package from Arch community repository.

I have just noticed that the function I called in the prompt is an overloaded one of the macro I posted. I wrote it to pass initializer list literal instead of a vector. To avoid confusion, the overloaded one is posted here:

```auto
auto DrawT(TTree *const tree, const std::initializer_list<size_t>& nEnts){
	return DrawT(tree, std::vector<size_t>(nEnts));
}

```

---

<div class="post-metadata">

**Author:** ![couet](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/couet/32/12037_2.png) [@couet](https://root-forum.cern.ch/u/couet)\
**Post date:** [April 6, 2020, 11:39am UTC](https://root-forum.cern.ch/t/random-behavior-when-drawing-tree-on-multiple-pads/38736/2 "2020-04-06T11:39:58Z")

</div>

I do not see why you would get different plots if you do exactly the same thing several times: If you plot the same variables from the same tree using the same selections you should get the same plots. Do you have you a reproducer we can run ?

---

<div class="post-metadata">

**Author:** ![Excelsior](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/excelsior/32/21460_2.png) [@Excelsior](https://root-forum.cern.ch/u/Excelsior)\
**Post date:** [April 6, 2020, 12:12pm UTC](https://root-forum.cern.ch/t/random-behavior-when-drawing-tree-on-multiple-pads/38736/3 "2020-04-06T12:12:42Z")

</div>

I tried to put all the codes in a single file but things changed again. When I load the macro file and run the macro file with command `root rep.cc`, sometimes 3 plots were drawn but sometimes 2. Anyway this condition resembles the one I described initially, so I just post the macro here.

Please notice that the macro includes a ~~CSV~~ txt file.  
[rep.cc](https://root-forum.cern.ch/uploads/short-url/zx1SRNNZ62Ig6EkkZwdqewbpeZO.cc) (1.8 KB) [iris\_train.txt](https://root-forum.cern.ch/uploads/short-url/8Wl9ajvublH6fHn932EQ0VOJTp0.txt) (2.6 KB)

---

<div class="post-metadata">

**Author:** ![couet](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/couet/32/12037_2.png) [@couet](https://root-forum.cern.ch/u/couet)\
**Post date:** [April 6, 2020, 12:50pm UTC](https://root-forum.cern.ch/t/random-behavior-when-drawing-tree-on-multiple-pads/38736/4 "2020-04-06T12:50:13Z")

</div>

Your macro seems to have a problem. Can you fix ?

```auto
root [0] .x rep.cc
Processing rep.cc...
In file included from input_line_8:1:
/Users/couet/Downloads/rep.cc:35:1: error: 'auto' return without trailing return type; deduced return types are a C++14 extension
auto DrawT(TTree *const tree, const std::vector<size_t>& nEnts){        
^
/Users/couet/Downloads/rep.cc:61:9: error: cannot initialize return object of type 'int' with an lvalue of type 'TCanvas *'
        return TC;
               

```

---

<div class="post-metadata">

**Author:** ![Excelsior](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/excelsior/32/21460_2.png) [@Excelsior](https://root-forum.cern.ch/u/Excelsior)\
**Post date:** [April 6, 2020, 1:02pm UTC](https://root-forum.cern.ch/t/random-behavior-when-drawing-tree-on-multiple-pads/38736/5 "2020-04-06T13:02:37Z")

</div>

The problem was possibly caused by the different ROOT compile options. I learned that the ROOT provided by Arch community repository is complied with C++17 option enabled so the macro works on my machine. I edited my macro to give `DrawT()` an explicit return type and this should fix. However, I can’t make sure about the condition on your machine.  
[rep.cc](https://root-forum.cern.ch/uploads/short-url/4iBPL7KZxgw9WKJfFt5hczs0U4b.cc) (1.8 KB)

---

<div class="post-metadata">

**Author:** ![couet](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/couet/32/12037_2.png) [@couet](https://root-forum.cern.ch/u/couet)\
**Post date:** [April 6, 2020, 1:21pm UTC](https://root-forum.cern.ch/t/random-behavior-when-drawing-tree-on-multiple-pads/38736/6 "2020-04-06T13:21:49Z")

</div>

You macro runs now. I get many warnings:

```auto
root [0] 
Processing rep.cc...
Info in <TCanvas::MakeDefCanvas>: created default TCanvas with name c1
Warning in <TTree::ReadStream>: Ignoring trailing ",1,0,0" while reading line 1
Warning in <TTree::ReadStream>: Ignoring trailing ",1,0,0" while reading line 2
Warning in <TTree::ReadStream>: Ignoring trailing ",1,0,0" while reading line 3
Warning in <TTree::ReadStream>: Ignoring trailing ",1,0,0" while reading line 4
Warning in <TTree::ReadStream>: Ignoring trailing ",1,0,0" while reading line 5
Warning in <TTree::ReadStream>: Ignoring trailing ",1,0,0" while reading line 6
Warning in <TTree::ReadStream>: Ignoring trailing ",1,0,0" while reading line 7
Warning in <TTree::ReadStream>: Ignoring trailing ",1,0,0" while reading line 8
[...]

```

and the two following plots:

 ![Screenshot 2020-04-06 at 15.20.39](https://root-forum.cern.ch/uploads/default/original/3X/f/a/fad5e1239d8391417fe9452b4ba7408803391254.png)

What is wrong ?

---

<div class="post-metadata">

**Author:** ![Excelsior](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/excelsior/32/21460_2.png) [@Excelsior](https://root-forum.cern.ch/u/Excelsior)\
**Post date:** [April 6, 2020, 1:28pm UTC](https://root-forum.cern.ch/t/random-behavior-when-drawing-tree-on-multiple-pads/38736/7 "2020-04-06T13:28:46Z")

</div>

I didn’t read the entire row of the txt file so the trailing value caused the warnings. It’s pretty weird that on my machine I got this:

 ![20200406212528](https://root-forum.cern.ch/uploads/default/original/3X/2/c/2c2a5fbdd01fba2abb9cbc8a7fe9b2d0a572c521.png)

---

<div class="post-metadata">

**Author:** ![Excelsior](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/excelsior/32/21460_2.png) [@Excelsior](https://root-forum.cern.ch/u/Excelsior)\
**Post date:** [April 6, 2020, 1:29pm UTC](https://root-forum.cern.ch/t/random-behavior-when-drawing-tree-on-multiple-pads/38736/8 "2020-04-06T13:29:24Z")

</div>

And this for another try:

 ![20200406212618](https://root-forum.cern.ch/uploads/default/original/3X/8/4/8421b0686464c13e3720062e85ecd4abebd3d34f.png)

---

<div class="post-metadata">

**Author:** ![couet](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/couet/32/12037_2.png) [@couet](https://root-forum.cern.ch/u/couet)\
**Post date:** [April 6, 2020, 1:33pm UTC](https://root-forum.cern.ch/t/random-behavior-when-drawing-tree-on-multiple-pads/38736/9 "2020-04-06T13:33:43Z")

</div>

> [@Excelsior](#):
>
> And this for another try:

You mean restarting ROOT ? or an other try in the same ROOT session ?  
I just tried several times restarting ROOT. I get always the same.

---

<div class="post-metadata">

**Author:** ![Excelsior](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/excelsior/32/21460_2.png) [@Excelsior](https://root-forum.cern.ch/u/Excelsior)\
**Post date:** [April 6, 2020, 1:37pm UTC](https://root-forum.cern.ch/t/random-behavior-when-drawing-tree-on-multiple-pads/38736/10 "2020-04-06T13:37:36Z")

</div>

I run the macro by passing it as a command line argument: `root rep.cc`, so I do restarted ROOT each time and I never got a complete version with all 4 plots. It seems to be a platform related problem?

---

<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:** [April 6, 2020, 1:42pm UTC](https://root-forum.cern.ch/t/random-behavior-when-drawing-tree-on-multiple-pads/38736/11 "2020-04-06T13:42:17Z")

</div>

Try to add, right before “`return TC;`”:  
`TC->cd(0); TC->Modified(); TC->Update();`

---

<div class="post-metadata">

**Author:** ![Excelsior](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/excelsior/32/21460_2.png) [@Excelsior](https://root-forum.cern.ch/u/Excelsior)\
**Post date:** [April 6, 2020, 2:27pm UTC](https://root-forum.cern.ch/t/random-behavior-when-drawing-tree-on-multiple-pads/38736/12 "2020-04-06T14:27:39Z")

</div>

I tried but the problem remained. Thank you for your attention anyway.

---

<div class="post-metadata">

**Author:** ![couet](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/couet/32/12037_2.png) [@couet](https://root-forum.cern.ch/u/couet)\
**Post date:** [April 6, 2020, 2:30pm UTC](https://root-forum.cern.ch/t/random-behavior-when-drawing-tree-on-multiple-pads/38736/13 "2020-04-06T14:30:14Z")

</div>

Which ROOT version ? On which machine ?

---

<div class="post-metadata">

**Author:** ![Excelsior](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/excelsior/32/21460_2.png) [@Excelsior](https://root-forum.cern.ch/u/Excelsior)\
**Post date:** [April 7, 2020, 2:22am UTC](https://root-forum.cern.ch/t/random-behavior-when-drawing-tree-on-multiple-pads/38736/14 "2020-04-07T02:22:44Z")

</div>

> _ROOT Version:_ 6.20/02  
> _Platform:_ Arch Linux on WSL  
> _Compiler:_ I installed the package from Arch community repository.

And this is what `uname -a` command prints:  
`Linux $HOSTNAME 4.4.0-18362-Microsoft #476-Microsoft Fri Nov 01 16:53:00 PST 2019 x86_64 GNU/Linux`

---

<div class="post-metadata">

**Author:** ![Excelsior](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/excelsior/32/21460_2.png) [@Excelsior](https://root-forum.cern.ch/u/Excelsior)\
**Post date:** [April 7, 2020, 5:12am UTC](https://root-forum.cern.ch/t/random-behavior-when-drawing-tree-on-multiple-pads/38736/15 "2020-04-07T05:12:57Z")

</div>

I solved the problem in a even more weird way. After I separate the loop which generates combinations of strings I noticed that the problem occurred within that loop. It had nothing to do with the drawing but the combinations generated were incomplete. I modified my code as below and it worked. Still confusing about what caused the problem though.

```auto
auto DrawT(TTree *const tree, const std::vector<size_t>& nEnts){	
	auto TC = new TCanvas();
	TC->Divide(2, 2);
	int colorInd = 0;
	size_t tcInd = 0;
	auto trd = tree->CloneTree();
	auto cBr = trd->Branch("color", &colorInd, "color/i");
    //std::vector<const char*> nameV;
	std::vector<std::string> nameV;
	for(auto branch: *(tree->GetListOfBranches()))	
        //nameV.push_back(branch->GetName());
		nameV.emplace_back(branch->GetName());
	for(size_t nEnt: nEnts){
		for(size_t i = 0; i != nEnt; ++i)
			cBr->Fill();
		++colorInd;
	}
	trd->SetMarkerStyle(8);
	do{
		std::stringstream ss;
		ss<<nameV[0]<<':'<<nameV[1]<<':'<<nameV[2]<<':'<<"color";
		TC->cd(++tcInd);
		trd->Draw(ss.str().c_str(), "1");
	}while(next_combination(nameV.begin(), nameV.begin()+3, nameV.end()));
	return TC;
}

```
