# ProjectionY for each bin

**URL:** <https://root-forum.cern.ch/t/projectiony-for-each-bin/42920>\
**Category:** Newbie\
**Created:** [January 3, 2021, 1:59pm UTC](https://root-forum.cern.ch/t/projectiony-for-each-bin/42920 "2021-01-03T13:59:53Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![iftikhar\_safi](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/iftikhar_safi/32/21696_2.png) [@iftikhar\_safi](https://root-forum.cern.ch/u/iftikhar_safi)\
**Post date:** [January 3, 2021, 1:59pm UTC](https://root-forum.cern.ch/t/projectiony-for-each-bin/42920/1 "2021-01-03T13:59:53Z")

</div>

I am having trouble with getting the ProjectionY for all the bins and then Drawing them?

1. For specific range of TH2 I do like this

histo-\>ProjectionY(“slices”,lowbin,highbin);

1. For doing it bin by bin, I do it like this

```auto

for (int64_t ibin = 1; ibin <= histo->GetXaxis ()->GetNbins (); ++ibin)
{
 TH1D *htmp;
 htmp= histo->ProjectionY (Form ("slices_%zu", ibin), ibin, ibin, "");
}

```

It gives me all the YProjections, However,I do not know how to draw all of them in a Canvas ?

Any help will be appreciated.  
Thanks in Advance  
Happy New Year Everyone!

---

<div class="post-metadata">

**Author:** ![jalopezg](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/jalopezg/32/25844_2.png) [@jalopezg](https://root-forum.cern.ch/u/jalopezg)\
**Post date:** [January 3, 2021, 5:36pm UTC](https://root-forum.cern.ch/t/projectiony-for-each-bin/42920/2 "2021-01-03T17:36:34Z")

</div>

Hi @iftikhar_safi,

First of all, Happy New Year! I will invite @moneta to this thread; I am sure he can help with this.

Cheers,  
J.

---

<div class="post-metadata">

**Author:** ![iftikhar\_safi](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/iftikhar_safi/32/21696_2.png) [@iftikhar\_safi](https://root-forum.cern.ch/u/iftikhar_safi)\
**Post date:** [January 4, 2021, 2:45pm UTC](https://root-forum.cern.ch/t/projectiony-for-each-bin/42920/3 "2021-01-04T14:45:39Z")

</div>

Hello Everyone,  
After some time of trying I got what I wanted. So the way to draw all the projections in a canvas is given as

```auto
TCanvas *c1 = new TCanvas ("c1", "c1", 1800, 1200);
c1->Divide(6,6); //or any number of projections that you need.
for (int64_t ibin = 1; ibin <= histo->GetXaxis ()->GetNbins (); ++ibin)
{
TH1D *htmp= histo->ProjectionY (Form ("slices_%zu", ibin), ibin, ibin, "");
c1->cd(ibin+1);
histo->Draw("HIST");
}

```

Thanks

---

<div class="post-metadata">

**Author:** ![iftikhar\_safi](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/iftikhar_safi/32/21696_2.png) [@iftikhar\_safi](https://root-forum.cern.ch/u/iftikhar_safi)\
**Post date:** [January 7, 2021, 11:09am UTC](https://root-forum.cern.ch/t/projectiony-for-each-bin/42920/4 "2021-01-07T11:09:29Z")

</div>

Hello Everyone,  
I have a small problem after making the projections. I will attach the relevant script here.  
I have two files, one is the background data while the other is the data file.  
The first thing I want to do is open the background file and make a projection of 25 background Runs, from which I get the projection. Then I integrate a specific region as shown from 401, 500. This one will be used for scaling.  
The next thing I want to do is to make the projection of the data runs and subtract from each data run the 25 background Runs.  
Then I performed the subtraction from the data run-background run, Also, I draw them and I see that the subtraction has been performed.  
Here I have the problem, I want to take an integral for the data run-background run for the specific region, However the integral that I take only gives me the data run counts and not counts from the subtracted one. More specifically from the code, I want to see the counts\_integral1 taken from h2(after background subtraction) and not from the px.  
Any help will be appreciated.  
Sorry for the long post.

```auto
void run_slices ()
{
        TFile *f =TFile::Open("/home/iftikhar/Templates/UAr_70d_mt2_SLAD_v3_5_0_Hist.root");
        TFile *w =TFile::Open("/home/iftikhar/Templates/UAr_500d_SLAD_v3_5_0_merged_v4_Hist.root");
        TH2F *m;
        TH2F *h;
        m = (TH2F *) w->Get("hNePerRun_bkg");
        h = (TH2F *) f->Get("hNePerRun");
        TH1D *bkg=m->ProjectionY("slice1",6470,6495);
        double bkg_counts=bkg->Integral(401,500);
        for (int64_t ibin = 1; ibin <= h->GetXaxis()->GetNbins(); ++ibin)
        { TH1D *px = h->ProjectionY (Form ("slices_%zu", ibin), ibin, ibin, "");
                bkg->Scale(px->Integral(401,500)/bkg_counts); 
                px->Add(bkg,-1);
                TH1D *h2 = (TH1D*)px->Clone("h2");
                h2->Draw("HIST");
                double counts_integral1 =h2->Integral (h2->FindBin (6.), h2->FindBin (19.));
                cout<<counts_integral1<<endl;
              }
}

```

---

<div class="post-metadata">

**Author:** ![Axel](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/axel/32/11084_2.png) [@Axel](https://root-forum.cern.ch/u/Axel)\
**Post date:** [January 7, 2021, 2:56pm UTC](https://root-forum.cern.ch/t/projectiony-for-each-bin/42920/5 "2021-01-07T14:56:24Z")

</div>

How do you end up concluding

> However the integral that I take only gives me the data run counts and not counts from the subtracted one.

Can you share the plots of `h->ProjectionY (Form ("slices_%zu", ibin), ibin, ibin, "");` and `h2` for one single `ibin`? And the corresponding `counts_integral1` for that bin?

How does `(h2->FindBin (6.), h2->FindBin (19.))` relate to `(401,500)`?

---

<div class="post-metadata">

**Author:** ![iftikhar\_safi](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/iftikhar_safi/32/21696_2.png) [@iftikhar\_safi](https://root-forum.cern.ch/u/iftikhar_safi)\
**Post date:** [January 7, 2021, 7:52pm UTC](https://root-forum.cern.ch/t/projectiony-for-each-bin/42920/6 "2021-01-07T19:52:55Z")

</div>

Hello,

> How do you end up concluding.

Because the output (counts\_integral1) gives me the same result before and after background subtraction.

for the range 401 to 500 is because the Y axis bins are 500,0,100, I choose the same range of Background and data, so that I can scale them later.

`double counts_integral1 =h2->Integral(h2->FindBin (6.), h2->FindBin (19.));`

It is because I want to know the number of counts in the peak in the data runs. so the FindBin(6.,19.) and (401,500) are not related to each other.  
Below I will show some of the projections in the canvas c3 below for

`h->ProjectionY (Form ("slices_%zu", ibin), ibin, ibin, "");`

 ![c3](https://root-forum.cern.ch/uploads/default/original/3X/0/9/091dde0be7432c031e4623492ce13704c45edd0a.png)  
The corresponding counts\_integral1 are the same before and after the background subtraction.  
0  
6  
10  
0  
4  
7  
2  
The issue is that counts\_integral1 should give me the counts after subtraction but it gives me the counts before the subtraction. Or maybe somehow the subtraction is not working correctly, I recently noticed that the before and after the background spectra looks the same but after the subtractions the entries are less.  
Thanks.  
Let me know if you need some more information.

---

<div class="post-metadata">

**Author:** ![Axel](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/axel/32/11084_2.png) [@Axel](https://root-forum.cern.ch/u/Axel)\
**Post date:** [January 12, 2021, 10:13am UTC](https://root-forum.cern.ch/t/projectiony-for-each-bin/42920/7 "2021-01-12T10:13:44Z")

</div>

If I’m not mistaken you keep calling

> [@iftikhar\_safi](#):
>
> ` bkg->Scale(px->Integral(401,500)/bkg_counts);`

until it vanishes, because you’ve scaled it down to 0?

You can also move the `Scale()` of all the `px` slices out, by simply scaling `h` once.

---

<div class="post-metadata">

**Author:** ![iftikhar\_safi](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/iftikhar_safi/32/21696_2.png) [@iftikhar\_safi](https://root-forum.cern.ch/u/iftikhar_safi)\
**Post date:** [January 12, 2021, 9:05pm UTC](https://root-forum.cern.ch/t/projectiony-for-each-bin/42920/8 "2021-01-12T21:05:22Z")

</div>

Hi,  
I want to scale the background and then subtract it from all the slices (px). So I have to scale the same background with all the slices.  
what do you mean by scaling the h once, Its a TH2 and the background is TH1?  
So I am not sure if it will work.

> [@Axel](#):
>
> You can also move the `Scale()` of all the `px` slices out, by simply scaling `h` once.

For scaling, I only know this way to scale, before it worked for me but it does not work for the slices(px)

> [@iftikhar\_safi](#):
>
> bkg-\>Scale(px-\>Integral(401,500)/bkg\_counts);

Thank you

.

---

<div class="post-metadata">

**Author:** ![iftikhar\_safi](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/iftikhar_safi/32/21696_2.png) [@iftikhar\_safi](https://root-forum.cern.ch/u/iftikhar_safi)\
**Post date:** [January 21, 2021, 1:09pm UTC](https://root-forum.cern.ch/t/projectiony-for-each-bin/42920/9 "2021-01-21T13:09:58Z")

</div>

Here is the end result after some efforts for the background subtraction after slicing the 2d Histograms.

```auto
void run_slices ()
{
        TFile *f =TFile::Open("/home/iftikhar/Templates/UAr_70d_mt2_SLAD_v3_5_0_Hist.root");
        TFile *w=TFile::Open("/home/iftikhar/Templates/UAr_500d_SLAD_v3_5_0_merged_v4_Hist.root");
        TH2F *m;
        TH2F *h;
        m = (TH2F *) w->Get("hNePerRun_bkg");
        h = (TH2F *) f->Get("hNePerRun");
        TH1D *bkg=m->ProjectionY("slice1",6470,6495);//shows the bin number for my runs. Modify*
        TCanvas *c2 = new TCanvas ("c2", "background subtraction with scaling", 1800, 1200);
        c2->Divide(3,3);
        for (int64_t ibin = 1; ibin <=h->GetXaxis()->GetNbins(); ++ibin)
        { TH1D *five_hundred_days = (TH1D*)bkg->Clone("five_hundred_days");//This line was the fix for my problem
                 TH1D *px = h->ProjectionY (Form ("slices_%zu", ibin), ibin, ibin, "");
                 //Scaling the background 
                 Double_t xlow = 300; Double_t xhigh = 500;
                 Double_t scale = px->Integral(xlow,xhigh)/five_hundred_days->Integral(xlow,xhigh);
                 five_hundred_days->Scale(scale);
                 px->Add(five_hundred_days,-1);
                 c2->cd(ibin+1)->SetLogy();
                 five_hundred_days->Draw();
                 bkg->Draw("same");
                 px->Draw("same");
}

                

```

Thanks.  
Happy coding
