How to add histograms

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?

Multiply only y axis contents, not x!

see TH1::Scale

Rene

Is it possible to assign resulting histogram to another object without changing any of adding histograms? This, of course, does not work:

TH1F* diff = new TH1F( "diff", "diff", 100, 0, 100 );
diff = h1->Add( h2, -1 );
diff->Add( h1, h2, 1., -1. );

There are overloaded arithmetic operators https://root.cern.ch/doc/master/classTH1F.html#friends

auto diff = *h1 - *h2;

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:

./upload.Cxx: line 1: syntax blunders close to surprising token (’ ./upload.Cxx: line 1:void TH1::Add(const TH1 *M_LQ1, const TH1 *M_LQ2, Double_t c1, Double_t c2)’

upload.Cxx is my record, it’s miles in my ~/public, if you may check:
/afs/cern.Ch/person/e/evgenia/public
Thanks.

Is there a way to continuously add histograms to each other? For example: create an empty histogram and then use a for loop that keeps adding histograms to this empty histogram?

You can call TH1::Add as many times as you want.
You can draw the “updated” histogram(s) like in these tutorials:
${ROOTSYS}/tutorials/hist/hsum.C
${ROOTSYS}/tutorials/hist/hsumTimer.C