Merging histograms of different bin number into one histogram

how to merge histograms with different bin inside one root file into a single histogram

If the binnings are different then some extra knowledge defining how to merge them is required. Therefore I think you will need to do this merging yourself by looping on the bins of the two histograms and filling the result histogram.

@couet,actually problem is i have histograms with different bin e.g. 0-5,5-10, and so on as separate plots under single root file…now i want to merge all of them into one single plot showing variation of the plot say from 0 to 25…how will i do that?

If you are happy to have the plot result only, you can first draw a frame covering the full X range with gPad->DrawFrame() and then plot the histograms on top of it using the option SAME.

pTJPsi_0_1->Write()
pTJPsi_1_2->Write(),
pTJPsi_2_3->Write()
pTJPsi_3_4->Write()
pTJPsi_4_5->Write()

the o/p of above is 5 different plots. What i want to do is to have all this plot in a single canvas. Any hints please.

Write() save individual object in a file. To draw an histogram you should use Draw().

pTJPsi_0_1->Draw();
pTJPsi_1_2->Draw("SAME");
pTJPsi_2_3->Draw("SAME");
pTJPsi_3_4->Draw("SAME");
pTJPsi_4_5->Draw("SAME");

i have define histograms as follows:
TH1F *x_0_1=new TH1F(“x_0_1”,“x”, 50, 0., 50.);
TH1F *x_1_2=new TH1F(“x_1_2”,“y”, 50, 0., 50.);
TH1F *x_2_3=new TH1F(“x_2_3”,“z”, 50, 0., 50.);
TH1F *x_3_4=new TH1F(“x_3_4”,“a”, 50, 0., 50.);
TH1F *x_4_5=new TH1F(“x_4_5”,“b”, 50, 0., 50.);


x_0_1->Fill();
x_1_2->Fill();
x_2_3->Fill();
x_3_4->Fill();
x_4_5->Fill();

With "SAME " IT is not coming in a single canvas.what to do?

try:

{
   TH1F *x_0_1=new TH1F("x_0_1","x", 50, 0., 50.);
   TH1F *x_1_2=new TH1F("x_1_2","y", 50, 0., 50.);
   TH1F *x_2_3=new TH1F("x_2_3","z", 50, 0., 50.);
   TH1F *x_3_4=new TH1F("x_3_4","a", 50, 0., 50.);
   TH1F *x_4_5=new TH1F("x_4_5","b", 50, 0., 50.);

   x_0_1->Fill(1);
   x_1_2->Fill(2);
   x_2_3->Fill(3);
   x_3_4->Fill(4);
   x_4_5->Fill(5);

   x_0_1->Draw();
   x_1_2->Draw("SAME");
   x_2_3->Draw("SAME");
   x_3_4->Draw("SAME");
   x_4_5->Draw("SAME");
}

still no success, is it something wrong with bin size

Can you post some running macro showing what is wrong ?

there is nothing wrong with code. its running perfectly but the o/p have plots on different canvases but i want all in single canvas.main91.cc (11.6 KB)

In this code the only place where you are drawing is:

      pTJPsi_0_1->Draw();
      pTJPsi_1_2->Draw("SAME");
      pTJPsi_2_3->Draw("SAME");
      pTJPsi_3_4->Draw("SAME");
      pTJPsi_4_5->Draw("SAME");
      pTJPsi_5_6->Draw("SAME");
      pTJPsi_6_7->Draw("SAME");
      pTJPsi_7_8->Draw("SAME");
      pTJPsi_8_9->Draw("SAME");
      pTJPsi_9_10->Draw("SAME");

I can ensure you that after this calls all the histograms are on the same canvas like with the small example I sent your … Now I am not sure we are talking about the same thing when you say that “the plots are not on the same canvas” … how do you look at the “plots” ?

Move all “Draw” statements to a place after “pythia.stat();” (so that they are outside of the “for” loop). Note that you do not save/print your canvas so it will automatically disappear as soon as your “main” finishes (maybe you want to add “theApp.Run();” right before “return 0;”).
Last but not least, instead of drawing each histogram separately, try:

  THStack *hs = new THStack("hs", "pT of J/Psi;X axis name;Y axis name");
  hs->Add(pTJPsi_0_1);
  hs->Add(pTJPsi_1_2);
  hs->Add(pTJPsi_2_3);
  hs->Add(pTJPsi_3_4);
  hs->Add(pTJPsi_4_5);
  hs->Add(pTJPsi_5_6);
  hs->Add(pTJPsi_6_7);
  hs->Add(pTJPsi_7_8);
  hs->Add(pTJPsi_8_9);
  hs->Add(pTJPsi_9_10);
  hs->Draw(""); // "" or "nostack"
  
  theApp.Run(); // "canvas main menu" -> "File" -> "Quit ROOT"
  
  // Done.
  return 0;

@Wile_E_Coyote, Following error appears,
"main91.cc:392:3: error: ‘THStack’ was not declared in this scope
THStack *hs = new THStack(“hs”, “pT of J/Psi;X axis name;Y axis name”);
^
main91.cc:392:12: error: ‘hs’ was not declared in this scope
THStack *hs = new THStack(“hs”, “pT of J/Psi;X axis name;Y axis name”);
^
main91.cc:392:21: error: expected type-specifier before ‘THStack’
THStack *hs = new THStack(“hs”, “pT of J/Psi;X axis name;Y axis name”);
^
Makefile:120: recipe for target ‘main91’ failed
make: *** [main91] Error 1 "

What to do add something in include.

#include "THStack.h"

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