# Applying cut conditions in Draw-\>("", ... ,"")

**URL:** <https://root-forum.cern.ch/t/applying-cut-conditions-in-draw/51769>\
**Category:** Newbie\
**Tags:** root\
**Created:** [September 29, 2022, 6:17pm UTC](https://root-forum.cern.ch/t/applying-cut-conditions-in-draw/51769 "2022-09-29T18:17:14Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![thegame](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/t/e495f1/32.png) [@thegame](https://root-forum.cern.ch/u/thegame)\
**Post date:** [September 29, 2022, 6:17pm UTC](https://root-forum.cern.ch/t/applying-cut-conditions-in-draw/51769/1 "2022-09-29T18:17:14Z")

</div>

Hi all, I’m trying to plot a (7,4) canvas using a branch of the tree. To be more specific, my tree looks something like this Tree[i][j] where **i** takes 7 values and for each i we have 4 values of **j** and in each section, I wanna plot let’s say X2[i][j] **:** X1[i][j] with a specific cut “m1\<X[i][j]\<m2” ; (m1, m2-\> double).  
but the m2 and m1 for every coordinate of the canvas are different. For example, (0,0) coordinate of the canvas has cut condition m1\<X[i][j]\<m2; whereas, (0,1) has cut condition m3\<X[i][j]\<m4; similarly, (3,4) has cut condition m5\<X[i][j]\<m6, etc.  
Right now what I’m doing is :

c1-\>cd(1);  
tree-\>Draw(“X2[0][0]:X1[0][0]”, “m2\<X[0][0]\<m1”, “colz”)  
then again,  
c1-\>cd(2);  
tree-\>Draw(“X2[0][1]:X1[0][1]”, “m3\<X[0][1]\<m4”, “colz”)  
.  
.  
.

c1-\>cd(24);  
tree-\>Draw(“X2[6][3]:X1[6][3]”, “m3\<X[6][3]\<m4”, “colz”)

It works well but time consuming and unnecessary long plotting routine AND amateur. So, I was wondering if there is some way where I can have the cut conditions in one file and then someway incorporate them conditions values inside tree-\>Draw() command within a **for loop**.

Thanks for your time. please do let me know if you need any more info to assist me.

---

<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:** [September 30, 2022, 7:36am UTC](https://root-forum.cern.ch/t/applying-cut-conditions-in-draw/51769/2 "2022-09-30T07:36:42Z")

</div>

You should define several [TCut](https://root.cern/doc/master/classTCut.html).  
Then, to make a for loop on `tree->Draw()` you can do something like:

```auto
   int j,k,l,m
   for (int i=1; i<=24; i++) {
      ...
      j = ...;
      k = ...;
      l = ...;
      m = ...;
      tree->Draw(Form("X2[%d][%d]:X1[%d][%d]",j,k,l,m), Form(...), "colz");
      ...
   }

```

---

<div class="post-metadata">

**Author:** ![FoxWise](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/foxwise/32/37588_2.png) [@FoxWise](https://root-forum.cern.ch/u/FoxWise)\
**Post date:** [October 1, 2022, 12:54am UTC](https://root-forum.cern.ch/t/applying-cut-conditions-in-draw/51769/3 "2022-10-01T00:54:57Z")

</div>

From the example you gave I would do something like this:

```cxx

TCanvas* canvas = new TCanvas();
canvas->Divide(7, 4);

vector<string> cuts = {"m1", "m2", "m3", "m4", ..., "m54"};
int cut_idx = 0;
for(int i=0; i<7; ++i){
    for(int j=0; j<4; ++j){
        canvas->cd(1 + j + i*4);
        string var = Form("X2[%d][%d]:X1[%d][%d]", i, j, i, j);
        string cut = Form("%s<X[%d][%d]<%s", cuts[cut_idx++], i, j, cuts[cut_idx ++]);
        tree->Draw(var, cut, "colz");
    }
}

```

cheers,  
Bohdan

---

<div class="post-metadata">

**Author:** ![thegame](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/t/e495f1/32.png) [@thegame](https://root-forum.cern.ch/u/thegame)\
**Post date:** [October 2, 2022, 1:25am UTC](https://root-forum.cern.ch/t/applying-cut-conditions-in-draw/51769/4 "2022-10-02T01:25:13Z")

</div>

@FoxWise thanks for your note. I’m not able to understand how the following line from your routine will going to work:

` string cut = Form("%s<X[%d][%d]<%s", cuts[cut_idx++], i, j, cuts[cut_idx ++]);`

`cuts[cut_idx++] and cut[cut_idx ++] for %s<X<%s,` I also don’t understand how that iterates over the subsequent values inside the ''cut" vector string. Can you please explain that? looking forward to hearing from you. Thanks.

p.s: this is the error I’m getting upon compiling:

```auto
/home/Desktop/caliboutput/correlationplot.c:73:55: error: cannot pass non-trivial object of type 'value_type' (aka 'std::__cxx11::basic_string<char>') to variadic function;
      expected type from format string was 'char *' [-Wnon-pod-varargs]
                string cut = Form("%s<PPACTSumX[%d][%d]<%s", cuts[cut_idx++], i, j, cuts[cut_idx ++]);
                                   ~~^~~~~~~~~~~~~~~
/home/Desktop/caliboutput/correlationplot.c:73:55: note: did you mean to call the c_str() method?
                string cut = Form("%s<PPACTSumX[%d][%d]<%s", cuts[cut_idx++], i, j, cuts[cut_idx ++]);
                                                             ^
                                                                            .c_str()
/home/Desktop/caliboutput/correlationplot.c:73:78: error: cannot pass non-trivial object of type 'value_type' (aka 'std::__cxx11::basic_string<char>') to variadic function;
      expected type from format string was 'char *' [-Wnon-pod-varargs]
                string cut = Form("%s<PPACTSumX[%d][%d]<%s", cuts[cut_idx++], i, j, cuts[cut_idx ++]);
                                                        ~~^~~~~~~~~~~~~~~~
/home/Desktop/caliboutput/correlationplot.c:73:78: note: did you mean to call the c_str() method?
                string cut = Form("%s<PPACTSumX[%d][%d]<%s", cuts[cut_idx++], i, j, cuts[cut_idx ++]);
                                                                                    ^
                                                                                                    .c_str()
/home/Desktop/caliboutput/correlationplot.c:73:67: warning: multiple unsequenced modifications to 'cut_idx' [-Wunsequenced]
                string cut = Form("%s<PPACTSumX[%d][%d]<%s", cuts[cut_idx++], i, j, cuts[cut_idx ++]);Preformatted text

```

---

<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:** [October 2, 2022, 6:32am UTC](https://root-forum.cern.ch/t/applying-cut-conditions-in-draw/51769/5 "2022-10-02T06:32:08Z")

</div>

Try to replace “`string`” with “`TString`”.

---

<div class="post-metadata">

**Author:** ![FoxWise](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/foxwise/32/37588_2.png) [@FoxWise](https://root-forum.cern.ch/u/FoxWise)\
**Post date:** [October 2, 2022, 5:29pm UTC](https://root-forum.cern.ch/t/applying-cut-conditions-in-draw/51769/6 "2022-10-02T17:29:13Z")

</div>

Sorry, I didn’t test the code myself.

But the error gives you a hint, what is going wrong.

`/home/Desktop/caliboutput/correlationplot.c:73:78: note: did you mean to call the c_str() method?`

I used `std::string`, while it expects C-style string `const char*`. So you can do the conversion using `cuts[cut_idx++].c_str()` instead of `cuts[cut_idx++]`.

Or, even better, as @Wile_E_Coyote proposed, use `TString` which I guess would do the job for you.

`cuts[cut_idx++]` is equivalent to:

```auto
cuts[cut_idx];
cut_idx = cut_idx + 1;

```

so, every time it is called, you will get the next element. As in your example

> m1\<X[i][j]\<m2; whereas, (0,1) has cut condition m3\<X[i][j]\<m4; similarly, (3,4) has cut condition m5\<X[i][j]\<m6, etc.

---

<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:** [October 16, 2022, 5:30pm UTC](https://root-forum.cern.ch/t/applying-cut-conditions-in-draw/51769/7 "2022-10-16T17:30:13Z")

</div>

Closed on October 16, 2022, 5:30pm UTC

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