Merge more histograms

Hi, I’ve a root file. In this file are saved histograms about particle densities in function of distance. In the root file there are 4 histograms (one about photons, one about electrons, one about muons and one about hadrons), but the distance values are the same in the 4 histograms.

I use this code tog get the histograms, for example about muons:

  TH1F *hmuolatg = (TH1F *)g->Get("hmuolat");
   if (hmuolatg == 0) {
      printf("Error getting an histogram from the file!\n");
      return;
   }

I need to make a histogram about all particle densities (i.e. a histogram having the sum of densities about muons, hadrons, electrons and photons)

I tried

  TH1F *hmuolatg = (TH1F *)g->Get("hmuolat","helelat","hhadlat", "hgamlat");
   if (hmuolatg == 0) {
      printf("Error getting an histogram from the file!\n");
      return;
   }

but it doesn’t work.
Is there a way?


ROOT Version ( 5.34/36):
Platform, compiler ( Devc++):


You should get the histograms the following ways:

TH1F *hmuolatg = (TH1F *)g->Get("hmuolat");
TH1F *helelat  = (TH1F *)g->Get("helelat");
TH1F *hhadlat  = (TH1F *)g->Get("hhadlat");
TH1F *hgamlat  = (TH1F *)g->Get("hgamlat");

And then you combine them as you wish.

Hi couet thank you for your reply.

I know this code

TH1F *hmuolatg = (TH1F *)g->Get("hmuolat");
TH1F *helelat  = (TH1F *)g->Get("helelat");
TH1F *hhadlat  = (TH1F *)g->Get("hhadlat");
TH1F *hgamlat  = (TH1F *)g->Get("hgamlat");

and I used it in another plot. But in this way I have 4 histograms… Instead…I need only one histogram overlapping all histograms

Yes but that’s the way to get object from a file. Now you have the four histograms you can combine them . For instance you can put them in a THStack and draw them in one go.

I don’t know if you understood what I mean… Look.

I can make this plot

But in this plot there are 4 histograms (one for each particle)

I wanna make only one histogram that is the sum of the four histograms. For example at d=0 densities are about 2 * 10^(-5), 5,5 * 10^(-6), 6 * 10^(-7), 2 * 10^(-7), then I wanna make a histogram having at d=0 density 2 * 10^(-5) + 5,5 * 10^(-6)+ 6 * 10^(-7)+ 2 * 10^(-7) = . 0,0000263 and same thing for each distance. Is it possible?

what about using a THStack?

Hi, I tried this code (see attachment) but it doesn’t work (I get error).

distrlat.c (3.9 KB)

Try:

   TH1F *hsumg = (TH1F*)hgamlatg->Clone("hsumg");
   hsumg->Add(helelatg);
   hsumg->Add(hmuolatg);
   hsumg->Add(hhadlatg);
   
   TH1F *hsump = (TH1F*)hgamlatp->Clone("hsump");
   hsump->Add(helelatp);
   hsump->Add(hmuolatp);
   hsump->Add(hhadlatp);

Thank you! It worked!

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