Make cuts on tree without changing the displayed axes

Hi,

I project a TTree onto a histogram, and apply cuts to them. If I use a specific cut, the displayed histogram axes limits change too, which is not what I want. I would only like the data points that are displayed to change based on the cuts I apply.

I’ve tried to use SetLimits() and SetRange() but that doesn’t force the axes to remain independent of the cuts.

Any help?

Do you have a small reproducer ? You create the histogram before projecting ? can you show the commands you are using ?

Hi,
These are .root files that are required to run smallReproducible.C

You may notice that the longitude axis is not starting from -180ish to 180ish. This causes the map of the Earth to be mirrored.
The archerTree object and its data points are included as it may have a bearing on the graph that is plotted.

ArcherTree.root (170.2 KB)
ElevationB.root (447.6 KB)
smallReproducible.C (1.7 KB)

Thanks for you example.
First replace SURF5 by SURF3 to avoid the warnings.

In your code I see:

   archerTree->Project( "h", "(-1*BathDepth):(longit):latit" );

And before you have:

   //Cuts that I would like to use but produce desired axes limits
   TCut cutDepth2030 = "(CaCO3 != -999 && BathDepth > 2000 && BathDepth < 3000)*(abs(CaCO3))";
   TCut cutInd = "longit >= 30 && longit <120 && latit >= -55 && latit < 15";

You are not using the cuts in the project command. Why ?

I think there is a line of code that I have commented out which uses that. I just commented that out initially to show the difference between using a cut and without it.

May be the following will help:

void smallReproducible()
{
   TFile* rootFile = TFile::Open("ElevationB.root");
   TFile* treeFile = TFile::Open("ArcherTree.root");

   TGraph2D* BSeaLevel = new TGraph2D(360*720); //2D array with N rows and M cols
   rootFile->GetObject("Bathymetry_overlay", BSeaLevel);

   TTree* archerTree = new TTree("archerTree", "archerTree"); // Tree with branches such as "CaCO3" and "BathDepth", and are used in TCut
   treeFile->GetObject("ARCHERTREE", archerTree);

   //Cuts that I would like to use but produce desired axes limits
   TCut cutDepth2030 = "(CaCO3 != -999 && BathDepth > 2000 && BathDepth < 3000)*(abs(CaCO3))";
   TCut cutInd = "longit >= 30 && longit <120 && latit >= -55 && latit < 15";

   TCanvas* c2 = new TCanvas( "c2", "Elevation Overlay", 1500, 500 );
   c2->Divide(3,1);

   TH3F* h = new TH3F( "h", "BathDepth vs longitude vs latitude", 100, 0., -1., 100, 0., -1., 100, 0., -1. );
   archerTree->Project( "h", "(-1*BathDepth):longit:latit", cutDepth2030 && cutInd );

   c2->cd(1); h->Draw("BOX");
   gPad->Update();

   BSeaLevel->SetMaximum(h->GetZaxis()->GetXmax());
   BSeaLevel->SetMinimum(h->GetZaxis()->GetXmin());
   BSeaLevel->GetXaxis()->SetRangeUser(h->GetXaxis()->GetXmin(), h->GetXaxis()->GetXmax());
   BSeaLevel->GetYaxis()->SetRangeUser(h->GetYaxis()->GetXmin(), h->GetYaxis()->GetXmax());
   c2->cd(2); BSeaLevel->Draw("SURF3");

   c2->cd(3); BSeaLevel->Draw("SURF3"); h->Draw("BOX SAME");
}

Does this fix make the TGraph2D change size according to the cuts made on the TTree?

Yes because the TGraph2D limits are defined from the histogram limits (see the code).

@couet In your example, you twice execute “new” and then “GetObject”. It seems to me that they result in memory leaks. :face_with_raised_eyebrow:

To be honest I did not look at the beginning of the macro… I concentrated on the graphics part. I will check .

void smallReproducible()
{
   TFile* rootFile = TFile::Open("ElevationB.root");
   TFile* treeFile = TFile::Open("ArcherTree.root");

   TGraph2D *BSeaLevel = (TGraph2D*)rootFile->Get("Bathymetry_overlay");
   TTree *archerTree = (TTree*)treeFile->Get("ARCHERTREE");

   //Cuts that I would like to use but produce desired axes limits
   TCut cutDepth2030 = "(CaCO3 != -999 && BathDepth > 2000 && BathDepth < 3000)*(abs(CaCO3))";
   TCut cutInd = "longit >= 30 && longit <120 && latit >= -55 && latit < 15";

   TCanvas* c2 = new TCanvas( "c2", "Elevation Overlay", 1500, 500 );
   c2->Divide(3,1);

   TH3F* h = new TH3F( "h", "BathDepth vs longitude vs latitude", 100, 0., -1., 100, 0., -1., 100, 0., -1. );
   archerTree->Project( "h", "(-1*BathDepth):longit:latit", cutDepth2030 && cutInd );

   c2->cd(1); h->Draw("BOX");
   gPad->Update();

   BSeaLevel->SetMaximum(h->GetZaxis()->GetXmax());
   BSeaLevel->SetMinimum(h->GetZaxis()->GetXmin());
   BSeaLevel->GetXaxis()->SetRangeUser(h->GetXaxis()->GetXmin(), h->GetXaxis()->GetXmax());
   BSeaLevel->GetYaxis()->SetRangeUser(h->GetYaxis()->GetXmin(), h->GetYaxis()->GetXmax());
   c2->cd(2); BSeaLevel->Draw("SURF3");

   c2->cd(3); BSeaLevel->Draw("SURF3"); h->Draw("BOX SAME");
}

Using “Get” should be prohibited by law.

   TGraph2D *BSeaLevel; rootFile->GetObject("Bathymetry_overlay", BSeaLevel);
   TTree *archerTree; treeFile->GetObject("ARCHERTREE", archerTree);
1 Like

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