TCutG polygon created, trying to cout all the points in this polygon

Dear everyone,

I am a beginner on Root. I draw a TGraph first, which is the scint_dedx2:scint_dedx1. Then I created a polygon by using TCutG. I also saved mycut as mycut1.C. I know in order to obtain the points in the polygon, I need to use IsInside(). But my nightmare started from IsInside(). I read all the topics I can find which may relate to my questions. I also tried them all but I still could not make my script run correctly. I know Rene answered a similar question long time ago, but I could not finish the script by myself after reviewing his suggestion (Filling 2D histogram with TCutG condition). Please, please, can anyone read my scripts and correct the IsInside() part of it? I uploaded both my script and txt files. All the line above 159 are correct, as you can delete the rest lines to run it. All my problems start from the line 160 (to the end). Thank you so much if anyone can save me! I suffered this problem so much all this week. Any help is so appreciated!!!

mycut1.C (6.4 KB)
hz001.txt (168.9 KB)

ROOT Version: 6.28
Platform: Not Provided
Compiler: Not Provided

I saved all my data to a TTree with different branches, and then I draw a TGraph of detector 2(my y-axis) vs detector1(my x-axis). I was trying to pick up the points which located in my polygon only, then printed (saved to a .root file) these points out for my further research. I hope I can obtain a subtree which only includes the points inside of my polygon but with all the branches. Detector 2 (my y-axis) and detector 1 (my x-axis) are just two branches of this TTree. Can I obtain the points which include all the branches information i.e. not just the x and y coordinates but all other information like detector 3, detector4, cal … So appreciated if anyone can give me a hand!

you have:

            if(!cutg->IsInside(x,y)) continue;
            else  break;
            cout << "x=" << x << "y=" << y <<endl;

which will never work, with if-continue and else-break you never reach the cout line; you may want the cout inside the if condition ( if (...) { cout ... ; }) and remove the else break, as you want to see all points inside. For the condition to find whether the point (x,y) is inside, you’d do

if (cutg->IsInside(x,y)) {
 cout << x << ", " << y << " is inside"  << endl;

Anyway, most of your macro should be changed. If you want a tree with all variables for those points, you don’t need a graph, just fill the second tree while reading the points in the first place (make sure you define the TCutG before this point!); e.g.

    int countinside = 0;     // if you want to count how many points are inside
    while (in.good()) {
      in >> clock >> spill >> scint_trigger >> scint_dedx1 >> scint_dedx2 >> scint_dedx3 >> scint_dedx4 >> Cal >> clk >> clk2 >> clk3;
      if (in.good()) {
        //Fill the histogram for the x and y axis (det1 and det2)
        // fill second tree with selected points only
        if ( cutg->IsInside(scint_dedx1, scint_dedx2) ) {    // or whatever variables are x and y
          //cout << "x=" << scint_dedx1 << " y=" << scint_dedx2 << endl;

(Supposing you also want to keep the “full tree” tree_targetout. The smaller tree tree_filtered should have the same branches if you want to keep them --that is, you also need tree_filtered->Branch("clock",&clock,"clock/D" etc.)

1 Like

Thank you so much!!! I response late as I posted this question at midnight, and I just saw your response this morning! I am sooooooooooooooooo appreciated!!! Hope you have a good day!!

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