ProjectionY for each bin

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

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!

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.

1 Like

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

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

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.

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;
              }
}

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)?

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, "");


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.

If I’m not mistaken you keep calling

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.

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.

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

Thank you

.

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

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