How to plot different projections of 2D histograms in one plot

Hello experts, I have a 2D histos of different particle species and i want to plot its projections in one histo. i’m doing it by TStack class but it just showed me the last histo that i added in it. I didn’t get what i"m doing wrong please tell me how can i do it is there anyother way to show it. this is the code:

THStack *hs=new THStack("hs"," ");


    TFile *f = TFile::Open ("Input.root");
    TList *l =(TList*) f ->Get("Input1");
    TH2F *h =(TH2F*) l ->FindObject("hDeuteron");
    TH2F *hP=(TH2F*) l ->FindObject("hProtons")
 TH1F *histo1= (TH1F*)h ->ProjectionY("2.2-2.6 GeV",110,130,"");
    histo1 ->SetLineColor(kRed);
    histo1 -> SetLineWidth(1);
    histo1 ->SetMarkerStyle(7); 
    hs ->Add(histo1);
 
     TH1F *histo2= (TH1F*)hP ->ProjectionY("2.2-2.6 GeV",110,130,"");
    histo2 ->SetLineColor(kBlue);
    histo2 -> SetLineWidth(1);
    hs->Add(histo2);
 hs->Add(histo2);
 TCanvas *c1 =new TCanvas ("c1","",900,900);
    c1 ->SetLogy();
    hs->Draw("nostack");

it shows me just one last histo.although i used the same class for the histos that filled with data, it worked but not with projections. please tell me what shoild i do. thank you.

Regards,
Sana Shahid

Expect the fact you are adding twice histo2 in the stack I do not see anything wrong. Can you provide an example (reproducing the problem) we can run ?

TOFP.C (1.3 KB)
i’m uploading my real code. it just give me last histo.please tell me whats wrong with it

the root file is quite big in size its not uploaded here.

It is needed to run your macro. Can you make it available else where ?

what else are the options? its size is 5.9MB
although mymacro doesn’t seems wrong right?


this is what i got when i run it. shows three lines but of same plot which is last one .

Dropbox ?

hard to tell if we cannot run it.

i don’t have dropbox in laptop so can you please send me your gmail id so that ican send you or share it through drive.

You MUST set different names when calling TH2::ProjectionY (note also that this method returns a “TH1D*”, not a “TH1F*”).

i set different names like histo1 histo2 … and i also changed it from TH1F to TH1D but the result is same.

This name: “2.2-2.6 GeV”

yesssss it works now thank you so much .i’m really greatfull to you thank you.

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