# Getting the same histograms after cuts

**URL:** <https://root-forum.cern.ch/t/getting-the-same-histograms-after-cuts/24719>\
**Category:** ROOT\
**Created:** [May 10, 2017, 4:20pm UTC](https://root-forum.cern.ch/t/getting-the-same-histograms-after-cuts/24719 "2017-05-10T16:20:36Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![nab](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/n/ce73a5/32.png) [@nab](https://root-forum.cern.ch/u/nab)\
**Post date:** [May 10, 2017, 4:20pm UTC](https://root-forum.cern.ch/t/getting-the-same-histograms-after-cuts/24719/1 "2017-05-10T16:20:37Z")

</div>

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

```auto
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

---

<div class="post-metadata">

**Author:** ![couet](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/couet/32/12037_2.png) [@couet](https://root-forum.cern.ch/u/couet)\
**Post date:** [May 11, 2017, 5:17am UTC](https://root-forum.cern.ch/t/getting-the-same-histograms-after-cuts/24719/2 "2017-05-11T05:17:50Z")

</div>

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

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

```

---

<div class="post-metadata">

**Author:** ![Graipher](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/graipher/32/11624_2.png) [@Graipher](https://root-forum.cern.ch/u/Graipher)\
**Post date:** [May 11, 2017, 8:08am UTC](https://root-forum.cern.ch/t/getting-the-same-histograms-after-cuts/24719/3 "2017-05-11T08:08:11Z")

</div>

How do you expect this part to work:

> [@nab](#):
>
> ```auto
> 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;
> }
> 
> ```

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`.

---

<div class="post-metadata">

**Author:** ![Graipher](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/graipher/32/11624_2.png) [@Graipher](https://root-forum.cern.ch/u/Graipher)\
**Post date:** [May 11, 2017, 8:16am UTC](https://root-forum.cern.ch/t/getting-the-same-histograms-after-cuts/24719/4 "2017-05-11T08:16:16Z")

</div>

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

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

```

---

<div class="post-metadata">

**Author:** ![Wile\_E\_Coyote](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/wile_e_coyote/32/12417_2.png) [@Wile\_E\_Coyote](https://root-forum.cern.ch/u/Wile_E_Coyote)\
**Post date:** [May 11, 2017, 8:39am UTC](https://root-forum.cern.ch/t/getting-the-same-histograms-after-cuts/24719/5 "2017-05-11T08:39:26Z")

</div>

[fakeratio\_tqz1.cc](https://root-forum.cern.ch/uploads/default/original/2X/0/0145a90dfc8dfd971deecf259599ea8827077d23.cc) (1.8 KB)

---

<div class="post-metadata">

**Author:** ![nab](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/n/ce73a5/32.png) [@nab](https://root-forum.cern.ch/u/nab)\
**Post date:** [May 11, 2017, 2:24pm UTC](https://root-forum.cern.ch/t/getting-the-same-histograms-after-cuts/24719/6 "2017-05-11T14:24:13Z")

</div>

Thank you so much Graipher.

---

<div class="post-metadata">

**Author:** ![nab](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/n/ce73a5/32.png) [@nab](https://root-forum.cern.ch/u/nab)\
**Post date:** [May 11, 2017, 2:24pm UTC](https://root-forum.cern.ch/t/getting-the-same-histograms-after-cuts/24719/7 "2017-05-11T14:24:44Z")

</div>

Thank you While\_E\_Coyote.

---

<div class="post-metadata">

**Author:** ![Graipher](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/graipher/32/11624_2.png) [@Graipher](https://root-forum.cern.ch/u/Graipher)\
**Post date:** [May 11, 2017, 2:27pm UTC](https://root-forum.cern.ch/t/getting-the-same-histograms-after-cuts/24719/8 "2017-05-11T14:27:11Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![nab](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/n/ce73a5/32.png) [@nab](https://root-forum.cern.ch/u/nab)\
**Post date:** [May 11, 2017, 2:29pm UTC](https://root-forum.cern.ch/t/getting-the-same-histograms-after-cuts/24719/9 "2017-05-11T14:29:54Z")

</div>

Right sir. But both will give the same result.

---

<div class="post-metadata">

**Author:** ![Graipher](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/graipher/32/11624_2.png) [@Graipher](https://root-forum.cern.ch/u/Graipher)\
**Post date:** [May 11, 2017, 2:31pm UTC](https://root-forum.cern.ch/t/getting-the-same-histograms-after-cuts/24719/10 "2017-05-11T14:31:09Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![nab](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/n/ce73a5/32.png) [@nab](https://root-forum.cern.ch/u/nab)\
**Post date:** [May 11, 2017, 2:32pm UTC](https://root-forum.cern.ch/t/getting-the-same-histograms-after-cuts/24719/11 "2017-05-11T14:32:51Z")

</div>

Yes exactly, thank you

---

<div class="post-metadata">

**Author:** ![system](https://root-forum.cern.ch/uploads/default/original/3X/e/f/efe4f24398fd8ffeb76bccb6ecc46d9d9c003376.png) [@system](https://root-forum.cern.ch/u/system)\
**Post date:** [May 25, 2017, 2:40pm UTC](https://root-forum.cern.ch/t/getting-the-same-histograms-after-cuts/24719/12 "2017-05-25T14:40:45Z")

</div>

Closed on May 25, 2017, 2:40pm UTC

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