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

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.

You should define several TCut.
Then, to make a for loop on tree->Draw() you can do something like:

   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");
      ...
   }

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


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

@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:

/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

Try to replace “string” with “TString”.

1 Like

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:

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.

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