Getting the same histograms after cuts

Hi developers,

I am applying different cuts on 2D histogram and divide it by another 2D histogram without cuts. But I am getting the same results of all the three histograms. plz help

macro is

void ratio_cuts()
{
   TFile *f = new TFile("May26_2.root");

   TTree *t2 = (TTree*)f->Get("demo/AnaTree");

 //  TH2F *h2 = new TH2F("h2","hist_vetoMu",100,0,300,100,-3,3);
 //  TH2F *h1 = new TH2F("h1","hist_SelMu",100,0,300,100,-3,3);
 //  TH2F *h = new TH2F("h3","hist_ratio",100,0,300,100,-3,3);

//   h2->GetXaxis()->SetTitle("VetoMu_pt");
//   h2->GetYaxis()->SetTitle("VetoMu_eta");
//   h1->GetXaxis()->SetTitle("SelMu_pt");
//   h1->GetYaxis()->SetTitle("SelMu_eta");

   t2->Draw("selNonIsoMu_eta:selNonIsoMu_pt >> h2","","goff");
   t2->Draw("selNonIsoMu_eta:selNonIsoMu_pt >> h1","","goff");

   TCanvas *c1 = new TCanvas("c1","c1",800,600) ;
   c1->Divide(2,2);
   c1->cd(1);
 
{
 
float selNonIsoMu_pt, selNonIsoMu_eta;
int selNonIsoMu_idTight, selNonIsoMu_ismuon, selNonIsoMu_iso, selNonIsoMu;

      for(int m=0; m<selNonIsoMu;m++)
        {

if(selNonIsoMu_pt[m]>20) continue;
if(selNonIsoMu_eta[m]<2.5) continue;
if(selNonIsoMu_idTight[m]==1) continue;
if(selNonIsoMu_ismuon[m]==1) continue;
if(selNonIsoMu_iso[m]<0.12) continue;
}

  double bins_x[] = {10, 20, 40, 70, 100};
  double bins_y[] = {0, 1, 1.5, 2.5};
  TH2F *h1 = new TH2F("tight mu", "tight muons;tight_Pt_{T}^{} [GeV/c];#eta",
                     (sizeof(bins_x) / sizeof(double) - 1), bins_x,
                     (sizeof(bins_y) / sizeof(double) - 1), bins_y);
  for (int i = 1; i <= h1->GetNbinsX(); i++)
    for (int j = 1; j <= h1->GetNbinsY(); j++)
      h1->SetBinContent(i, j, (i * i + j * j * j));

  h1->Draw("colz texte");
}  

   h1->Draw("colz texte");
   c1->cd(2);

{
  double bins_x[] = {10, 20, 40, 70, 100};
  double bins_y[] = {0, 1, 1.5, 2.5};
  TH2F *h2 = new TH2F("loose mu", "loose muons;loose_Pt_{T}^{} [GeV/c];#eta",

                     (sizeof(bins_x) / sizeof(double) - 1), bins_x,
                     (sizeof(bins_y) / sizeof(double) - 1), bins_y);
  for (int i = 1; i <= h2->GetNbinsX(); i++)
    for (int j = 1; j <= h2->GetNbinsY(); j++)
      h2->SetBinContent(i, j, (i * i + j * j * j));

  h2->Draw("colz texte");

}

   h2->Draw("colz texte");

   h = (TH2F*)h1->Clone();
//   h->GetXaxis()->SetTitle("pt ");
//   h->GetYaxis()->SetTitle("eta ");
//   h->SetTitle("fake_h1/h2_without cuts");
   h->Divide(h2);
   c1->cd(3);
   h->Draw("colz texte");
{
  double bins_x[] = {10, 20, 40, 70, 100};
  double bins_y[] = {0, 1, 1.5, 2.5};
  TH2F *h = new TH2F("ratio", "tight/loose;Pt_{T}^{} [GeV/c];#eta",
                     (sizeof(bins_x) / sizeof(double) - 1), bins_x,
                     (sizeof(bins_y) / sizeof(double) - 1), bins_y);
  for (int i = 1; i <= h->GetNbinsX(); i++)
    for (int j = 1; j <= h->GetNbinsY(); j++)
      h->SetBinContent(i, j, (i * i + j * j * j));

  h->Draw("colz texte");
}
c1->SaveAs("ratio.png");
}

source file path is /afs/cern.ch/user/n/nmajeed/public

Remark on the way to post code snippets:

When you post pieces of code on the forum enclose it with ``` . That makes your post much more readable (I systematically edit your posts that way) . So do:

```
// this is a C++ piece of code.
```

And it will display as:

// this is a C++ piece of code.
1 Like

How do you expect this part to work:

To me this looks like undefined behaviour. You just initialize a normal variable, e.g. selNonIsoMu_pt. Then for example selNonIsoMu_pt[1] is whatever comes after that reserved float in memory, interpreted as a float. There could be anything there (although, probably there will be the float selNonIsoMu_eta there, by coincidence, which might be initialized to zero).

The only thing saving you here is that when you initialize int selNonIsoMu it will be initialized to zero, so your whole for loop never runs, because m is also zero, so never less than selNonIsoMu.

If all you want is the ratio between two cuts, you can just do:

void ratio_cuts()
{
   TFile *f = new TFile("May26_2.root");

   TTree *t2 = (TTree*)f->Get("demo/AnaTree");

   double bins_x[] = {10, 20, 40, 70, 100};
   double bins_y[] = {0, 1, 1.5, 2.5};
   TH2F *tight_mu = new TH2F("tight_mu", "tight muons;tight_Pt_{T}^{} [GeV/c];#eta", (sizeof(bins_x) / sizeof(double) - 1), bins_x, (sizeof(bins_y) / sizeof(double) - 1), bins_y);
   tight_mu->Sumw2(kTRUE);
   TH2F *loose_mu = new TH2F("loose_mu", "loose muons;loose_Pt_{T}^{} [GeV/c];#eta", (sizeof(bins_x) / sizeof(double) - 1), bins_x, (sizeof(bins_y) / sizeof(double) - 1), bins_y);
   loose_mu->Sumw2(kTRUE);

   TCut tight = TCut("tight", "selNonIsoMu_pt > 20 && selNonIsoMu_eta < 2.5 && selNonIsoMu_idTight && selNonIsoMu_ismuon==1 && selNonIsoMu_iso < 0.12"); // or whatever your cuts really are
   TCut loose = TCut("loose", "1"); // or whatever your cuts really are

   t2->Draw("selNonIsoMu_eta:selNonIsoMu_pt >> tight_mu",tight,"goff");
   t2->Draw("selNonIsoMu_eta:selNonIsoMu_pt >> loose_mu",loose,"goff");

   TCanvas *c1 = new TCanvas("c1","c1",800,600) ;
   c1->Divide(2,2);
   c1->cd(1);
   loose_mu->Draw("colz text e");
   c1->cd(2);
   tight_mu->Draw("colz text e");

   c1->cd(3);
   TH2F * h_ratio = (TH2F*) tight_mu->Clone();
   h_ratio->Divide(loose_mu);
   h_ratio->SetTitle("Ratio");
   h_ratio->Draw("colz text e");
   c1->SaveAs("ratio.png");
}
1 Like

fakeratio_tqz1.cc (1.8 KB)

1 Like

Thank you so much Graipher.

Thank you While_E_Coyote.

You’re welcome. Note that Project(hist_name, expression), which Wile uses in his example, is slightly faster than Draw("expression>>hist_name"), which I used in my example. The difference is small, though.

Right sir. But both will give the same result.

Yes, they will. The Project way just saves the parsing of the expression string for a histogram name after the ">>".

Yes exactly, thank you

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