How to add histograms

I want to add 2 root histos in one (not superimpose, add the bins, they have the same caracteristics).

Which is the simplest way to do it?

The “add histos” from HOWTO’s doesn’t help, doesn’t even compile!

:open_mouth: :unamused:

Did you read the documentation of the histogram main class TH1?
You will see functions like

 void	Add(const TH1* h1, Double_t c1 = 1)
 void	Add(TF1* h1, Double_t c1 = 1, Option_t* option = "")
 void	Add(const TH1* h, const TH1* h2, Double_t c1 = 1, Double_t c2 = 1)

see: root.cern.ch/root/html/TH1.html#TH1:Add

example:

  TH1 *h1, *h2
  h1->Add(h2);

see also utility hadd in $ROOTSYS/bin/hadd to add files containing histograms and Trees (root.cern.ch/root/HowtoMerge.html)

Rene

Do I need a makefile to do that? Or to load some libraries maybe?

I am trying to use
void TH1::Add(const TH1 *M_LQ1, const TH1 *M_LQ2, Double_t c1, Double_t c2)

and I get:

./add.cxx: line 1: syntax error near unexpected token `('
./add.cxx: line 1: `void TH1::Add(const TH1 *M_LQ1, const TH1 *M_LQ2, Double_t c1, Double_t c2)'

add.cxx is my file, it is in my ~/public, if you can check:
/afs/cern.ch/user/e/evgenia/public
Thanks.

Evgenia

I cannot find the file add.cxx in your afs public dir

Rene

Sorry for your trouble, it must be there now…

The name is: M_LQ.C
I am trying to eecute it inside root, but I get:
Error: illegal pointer to class object h_common 0x0 459 M_LQ.C:11:
*** Interpreter error recovered ***

Do you have any ideas?

Thanks,
Evgenia

Could you put your data file referenced by
TFile f0("/afs/cern.ch/user/e/evgenia/testarea/12.0.6/PhysicsAnalysis/AnalysisCommon/UserAnalysis/run/LQmj.800.hist.root");

in your afs public area too ?

If you get the message “h_common” does not exist, it means that
the object name “MLQ1” is not in the file.

Rene

Sure, it’s already there, thanks!

Your objects MLQ1 and MLQ2 are in subdirectory “Muon”. You should do:

  TH1F *h_common = (TH1F*)f0.Get("Muon/MLQ1");
  TH1F *h_second = (TH1F*)f0.Get("Muon/MLQ2");

Rene

Ok, looks like it’s running, but shouldn’t I be getting a histo named “h_common” as output?

I don’t get any histo as output!

What seems to be the problem?

I don't get any histo as output!
As output of what?

Could you post your code?

Rene

My code is:

void M_LQ()
{
  gROOT->Reset();

  TCanvas *c1 = new TCanvas("c1", "Adding histograms", 10,10, 800, 800);
 
  TFile f0("/afs/cern.ch/user/e/evgenia/testarea/12.0.6/PhysicsAnalysis/AnalysisCommon/UserAnalysis/run/LQmj.800.hist.root");

  TH1F *h_common = (TH1F*)f0.Get("Muon/MLQ1");
  TH1F *h_second = (TH1F*)f0.Get("Muon/MLQ2");
   
  h_common->Add(h_second);
 
  h_common->Draw();
 } 

Once I run in ROOT “.x M_LQ.C” I should have a histo drawn, right?
A browser opens, but I have no histo!

Your problem has nothing to do with TH1::Add, but with C++ scoping rules.
When your function terminates, the object TFile f0 is deleted and all together the histograms associated with this file. For more details see chapter “Object ownership” in the users guide.
Replace your code by:

void M_LQ() 
{ 
//gROOT->Reset(); do not use Reset in a named macro

TCanvas *c1 = new TCanvas("c1", "Adding histograms", 10,10, 800, 800); 

TFile *f0 = new TFile("/afs/cern.ch/user/e/evgenia/testarea/12.0.6/PhysicsAnalysis/AnalysisCommon/UserAnalysis/run/LQmj.800.hist.root"); 

TH1F *h_common = (TH1F*)f0->Get("Muon/MLQ1"); 
TH1F *h_second = (TH1F*)f0->Get("Muon/MLQ2"); 

h_common->Add(h_second); 

h_common->Draw(); 
}

Rene

1 Like

Ok, it’s running fine and I have my histos, thank you!

Now what if I want to add two histos with the same name that are in different root files?

You can find them in :
/afs/cern.ch/user/e/evgenia/public

Thanks again!

Evgenia

TH1::AddDirectory(kFALSE);
TH1F *h_common = (TH1F*)f1.Get("name"); 
TH1F *h_second = (TH1F*)f2.Get("name"); 
h_common->Add(h_second); 

Rene

That’s ok, I found what I was doing wrong!

Thank you so much for all your help again, Rene!

Evgenia

Suppose I want to superimpose two or three histos coming from two or three different root files, not add them, show them in the same canvas together with different colors each.

How do I do that by the command line? Is it possible?

Thanks!

Evgenia

We show several examples in the tutorials. Look for instance at:
$ROOTSYS/tutorials/hist/hstack.C
$ROOTSYS/tutorials/hist/multicolor.C
$ROOTSYS/tutorials/math/limit.C
$ROOTSYS/tutorials/tree/cernstaff.C

Rene

Isn’t there a simpler way from the command line?

How can I multiply a histo with a number?