Scatter plots with different cuts

Hello,

I am trying to plot two scatter plots with different cuts from the same TTree. The relevant part of my code is the following:

tree->SetMarkerStyle(1);
tree->Draw("X:Z",cut1);
TGraph *mygraph1 = (TGraph*)gPad->GetPrimitive("Graph");
tree->SetMarkerStyle(2);
tree->Draw("X:Z",cut2);
TGraph *mygraph2 = (TGraph*)gPad->GetPrimitive("Graph");

auto legend = new TLegend(0.1,0.7,0.48,0.9);
legend->SetHeader("title","C"); 
legend->AddEntry("mygraph1","g1","p");
legend->AddEntry("mygraph2","g2","p");
legend->Draw();

The main issue comes when plotting the legend. Apparently retrieving the TGraph via gPad is not enough, as both markers are plotted with the same style in the legend. I would like to know if there is a way of obtaining two different graphs, one for each of my cuts (that is, one for each time I do tree->Draw(…) ) so I can modify them separately.

To create a TGraph from Draw, search for “How to obtain more info from TTree::Draw” in the TTree documentation (ROOT: TTree Class Reference). And to draw 2 or more graphs on the same pad/canvas, it’s safer to add them to a TMultiGraph to ensure the axis scales are large enough to include all graphs.
Also, remove the quotes for “mygraph1/2” in legend->AddEntry()

legend->AddEntry(mygraph1,"g1","p");  // same for mygraph2

Example:

    T->Draw("X:Z",cut1,"goff");  // "goff" so it is not actually drawn
    TGraph *mygraph1 = new TGraph(T->GetSelectedRows(),T->GetV2(),T->GetV1());
    T->Draw("X:Z",cut2,"goff");
    TGraph *mygraph2 = new TGraph(T->GetSelectedRows(),T->GetV2(),T->GetV1());

    mygraph1->SetMarkerStyle(1);
    mygraph2->SetMarkerStyle(2);

    TMultiGraph *mg = new TMultiGraph();
    mg->Add(mygraph1);
    mg->Add(mygraph2);
    mg->Draw("ap");

    auto legend = new TLegend(0.1,0.7,0.48,0.9);
    legend->SetHeader("title","C"); 
    legend->AddEntry(mygraph1,"g1","p");
    legend->AddEntry(mygraph2,"g2","p");
    legend->Draw();

https://root.cern/doc/master/treegetval_8C.html

That indeed worked! Thanks.