# Plotting from ttree with constrain

**URL:** <https://root-forum.cern.ch/t/plotting-from-ttree-with-constrain/52682>\
**Category:** Newbie\
**Tags:** tree, hist, root\
**Created:** [December 7, 2022, 1:33pm UTC](https://root-forum.cern.ch/t/plotting-from-ttree-with-constrain/52682 "2022-12-07T13:33:06Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![gini](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/gini/32/29467_2.png) [@gini](https://root-forum.cern.ch/u/gini)\
**Post date:** [December 7, 2022, 1:33pm UTC](https://root-forum.cern.ch/t/plotting-from-ttree-with-constrain/52682/1 "2022-12-07T13:33:06Z")

</div>

Hi,  
I want to plot a 2D histogram (TKE:M\_Tot) from a TTree with the constraint condition “Psd\_All[i]\>=1000 && Psd\_All[i]\<=2500”, where i =34 as Psd\_All is an array of size 34. The following is the small peice of code :

```auto
void Noconstrain_3gauss_try()
{
Double_t s1;

// DEFINING CANVAS
TCanvas *c1 = new TCanvas("c1","Without constraint",800,600);

gStyle->SetOptStat(2211);
gStyle->SetOptFit(1111);

/////////////////// Tree opening \\\\\\\\\\\\\\\\\\\\\\\\\\\\\

TFile *file1=TFile::Open("RAW1_treeFF01.root");
TTree *tree1=(TTree*)file1->Get("Analysis");

   TH2D*h1 = new TH2D("h1","With_Constraint",81.,49.5,130.5, 101., 79.5, 180.5); 
  
c1->cd();
for (int i=0; i<34 ; i++){
   
    TString str1("");
    str1.Form("Psd_All[%d]>=1000.0 && Psd_All[%d] <= 2500.0",i);

   tree1->Draw("Tke:M_Tot>>h1", str1.Data());
}

}

```

but when I run this I get the error shown in the attachment. I want to plot the2D histogram by constarining my Psd\_all, can someone tell me what is my mistake ??

Thank you !

 ![err](https://root-forum.cern.ch/uploads/default/original/3X/5/5/55530fad81546aa12b2c7a3486e9d327907ff272.png)

---

<div class="post-metadata">

**Author:** ![yus](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/y/9fc348/32.png) [@yus](https://root-forum.cern.ch/u/yus)\
**Post date:** [December 7, 2022, 1:40pm UTC](https://root-forum.cern.ch/t/plotting-from-ttree-with-constrain/52682/2 "2022-12-07T13:40:18Z")

</div>

Hi,

```c++
str1.Form("Psd_All[%d]>=1000.0 && Psd_Shift[%d] <= 2500.0",i);

```

should probably be

```c++
str1.Form("Psd_All[%d]>=1000.0 && Psd_Shift[%d] <= 2500.0",i,i);

```

---

<div class="post-metadata">

**Author:** ![gini](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/gini/32/29467_2.png) [@gini](https://root-forum.cern.ch/u/gini)\
**Post date:** [December 7, 2022, 1:43pm UTC](https://root-forum.cern.ch/t/plotting-from-ttree-with-constrain/52682/3 "2022-12-07T13:43:45Z")

</div>

That doesn’t work… I get the same error…

---

<div class="post-metadata">

**Author:** ![yus](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/y/9fc348/32.png) [@yus](https://root-forum.cern.ch/u/yus)\
**Post date:** [December 7, 2022, 1:50pm UTC](https://root-forum.cern.ch/t/plotting-from-ttree-with-constrain/52682/4 "2022-12-07T13:50:44Z")

</div>

Also, you have

```c++
str1.Form("Psd_All[%d]>=1000.0 && Psd_Shift[%d] <= 2500.0",i,i);

```

in the code, but on the screenshot it’s

```c++
str1.Form("Psd_Shift_All[%d]>=1000.0 && Psd_All_Shift[%d] <= 2500.0",i,i);

```

– why is that? Can it make the difference?

---

<div class="post-metadata">

**Author:** ![gini](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/gini/32/29467_2.png) [@gini](https://root-forum.cern.ch/u/gini)\
**Post date:** [December 7, 2022, 1:52pm UTC](https://root-forum.cern.ch/t/plotting-from-ttree-with-constrain/52682/5 "2022-12-07T13:52:43Z")

</div>

Both Psd\_All and Psd\_Shift\_All are the same, I just renamed the variable in the post…

---

<div class="post-metadata">

**Author:** ![yus](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/y/9fc348/32.png) [@yus](https://root-forum.cern.ch/u/yus)\
**Post date:** [December 7, 2022, 2:05pm UTC](https://root-forum.cern.ch/t/plotting-from-ttree-with-constrain/52682/6 "2022-12-07T14:05:58Z")

</div>

And on the screenshot you have `Psd_Shift_All` and `Psd_All_Shift` - are these the same, too?

---

<div class="post-metadata">

**Author:** ![gini](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/gini/32/29467_2.png) [@gini](https://root-forum.cern.ch/u/gini)\
**Post date:** [December 7, 2022, 2:12pm UTC](https://root-forum.cern.ch/t/plotting-from-ttree-with-constrain/52682/7 "2022-12-07T14:12:22Z")

</div>

It is Psd\_All\_Shift. I corrected for the name and its working now…

thank you
