Calculating area under the curve and superimpose two .root files

I have attached a .root file which contains a canvas. I need to find out the area under the curve. I also need to superimpose the two . root files .CosPADistribution_MCdataonly.root (16.4 KB)
CosPADistribution_realdataonly.root (16.5 KB)

Thanks in advance.

Integral() can help:
for TF1, TH1 or TGraph

Thanks for reply. But the root files contain canvas so how we can use this in this case.

The canvas contains the list of primitives. First do canvas->ls() to see what kind of object holds the curve . Then uses GetListOfPrimitives to retrieve the TList containing this object. Then using FindObject on this TList you will be able to retrieve the object itself.

superimpose_realandMC.C (818 Bytes)
The attached code is not working and when I use GetPrimitive instead of GetListOfPrimitives the only first data points are shown.

{
   auto *f = TFile::Open("CosPADistribution_realdataonly.root");
   auto c = (TCanvas*)f->Get("c");
   auto l = (TList*)c->GetListOfPrimitives();
   auto h = (TH1F*)l->FindObject("h_CosPAReal_ptbin[1]");

   auto C = new TCanvas ("C","C");
   C->cd();
   h->SetLineColor(2);
   h->Draw("hist");
}

Thanks a lot for replying but again it shows only first point . I would like to mention that the .root file is superimposition of many histograms in one canvas saved in .root format.

I have not look in details all the histograms in the canvas. But yes this particular one seems to have only one bin.
When you do β€˜ls’ on the canvas you get:

root [1] c->ls()
Canvas Name=c Title=c: Option=
 TCanvas fXlowNDC=0 fYlowNDC=0 fWNDC=1 fHNDC=1 Name= c Title= c: Option=
  OBJ: TList	TList	Doubly linked list : 0
   TFrame  X1= 0.950000 Y1=1.608724 X2=1.000000 Y2=5.187303
   OBJ: TH1F	h_CosPAReal_ptbin[0]	h_CosPAReal_ptbin[0] : 1 at: 0x7fae6bc854d0
   OBJ: TH1F	h_CosPAReal_ptbin[1]	h_CosPAReal_ptbin[1] : 1 at: 0x7fae6bcbbbb0
   OBJ: TH1F	h_CosPAReal_ptbin[2]	h_CosPAReal_ptbin[2] : 1 at: 0x7fae6bcbc0d0
   OBJ: TH1F	h_CosPAReal_ptbin[0]	h_CosPAReal_ptbin[0] : 1 at: 0x7fae6bcbc610
   OBJ: TH1F	h_CosPAReal_ptbin[1]	h_CosPAReal_ptbin[1] : 1 at: 0x7fae6bcbcb50
   OBJ: TH1F	h_CosPAReal_ptbin[2]	h_CosPAReal_ptbin[2] : 1 at: 0x7fae6bcbd090
   OBJ: TH1F	h_CosPAReal_ptbin[0]	h_CosPAReal_ptbin[0] : 1 at: 0x7fae6bcbd5d0
   OBJ: TH1F	h_CosPAReal_ptbin[1]	h_CosPAReal_ptbin[1] : 1 at: 0x7fae6bcbdb10
   OBJ: TH1F	h_CosPAReal_ptbin[2]	h_CosPAReal_ptbin[2] : 1 at: 0x7fae6bcbe050
   OBJ: TH1F	h_CosPAReal_ptbin[0]	h_CosPAReal_ptbin[0] : 1 at: 0x7fae6bcbe590
   OBJ: TH1F	h_CosPAReal_ptbin[1]	h_CosPAReal_ptbin[1] : 1 at: 0x7fae6bcbead0
   OBJ: TH1F	h_CosPAReal_ptbin[2]	h_CosPAReal_ptbin[2] : 1 at: 0x7fae6bcbf010
   OBJ: TH1F	h_CosPAReal_ptbin[0]	h_CosPAReal_ptbin[0] : 1 at: 0x7fae6bcbf550
   OBJ: TH1F	h_CosPAReal_ptbin[1]	h_CosPAReal_ptbin[1] : 1 at: 0x7fae6bcbfa90
   OBJ: TH1F	h_CosPAReal_ptbin[2]	h_CosPAReal_ptbin[2] : 1 at: 0x7fae6bcbffd0
   OBJ: TH1F	h_CosPAReal_ptbin[0]	h_CosPAReal_ptbin[0] : 1 at: 0x7fae6bcc0510
   OBJ: TH1F	h_CosPAReal_ptbin[1]	h_CosPAReal_ptbin[1] : 1 at: 0x7fae6bcc0a50
   OBJ: TH1F	h_CosPAReal_ptbin[2]	h_CosPAReal_ptbin[2] : 1 at: 0x7fae6bcc0f90
   OBJ: TH1F	h_CosPAReal_ptbin[0]	h_CosPAReal_ptbin[0] : 1 at: 0x7fae6bcc14d0
   OBJ: TH1F	h_CosPAReal_ptbin[1]	h_CosPAReal_ptbin[1] : 1 at: 0x7fae6bcc1a10
   OBJ: TH1F	h_CosPAReal_ptbin[2]	h_CosPAReal_ptbin[2] : 1 at: 0x7fae6bcc1f50
   OBJ: TH1F	h_CosPAReal_ptbin[0]	h_CosPAReal_ptbin[0] : 1 at: 0x7fae6bcc2490
   OBJ: TH1F	h_CosPAReal_ptbin[1]	h_CosPAReal_ptbin[1] : 1 at: 0x7fae6bcc29d0
   OBJ: TH1F	h_CosPAReal_ptbin[2]	h_CosPAReal_ptbin[2] : 1 at: 0x7fae6bcc2f10
   OBJ: TH1F	h_CosPAReal_ptbin[0]	h_CosPAReal_ptbin[0] : 1 at: 0x7fae6bcc3450
   OBJ: TH1F	h_CosPAReal_ptbin[1]	h_CosPAReal_ptbin[1] : 1 at: 0x7fae6bcc3990
   OBJ: TH1F	h_CosPAReal_ptbin[2]	h_CosPAReal_ptbin[2] : 1 at: 0x7fae6bcc3ed0
   OBJ: TH1F	h_CosPAReal_ptbin[0]	h_CosPAReal_ptbin[0] : 1 at: 0x7fae6bcc4410
   OBJ: TH1F	h_CosPAReal_ptbin[1]	h_CosPAReal_ptbin[1] : 1 at: 0x7fae6bcc4950
   OBJ: TH1F	h_CosPAReal_ptbin[2]	h_CosPAReal_ptbin[2] : 1 at: 0x7fae6bcc4e90
   OBJ: TPaveText	title  	X1= 0.957558 Y1=5.341657 X2=0.992442 Y2=5.612259
root [2] 

so several histograms appear to have the same name. It seems that the points you see under the same name when you draw the canvas are indeed spread over several histograms (with the same name) …

Do you have access to the program which created this canvas ?

I apologize for the delay. I am sending you the program which created this canvas. Actually many histograms have been superimposed. superimpose_realdata.C (10.3 KB)

Thanks. I will ook. Note that Scale is not a TCanvas Method.

You get some histograms with the same names from different files. For instance the histogram h_CosPAReal_ptbin[0]. When you draw them in the sams canvas they appear as listed in my previous post. Therefore it is not easy to retrieve them individually from the canvas. One way to make it easier would be to rename the histograms differently just after having done the Get from the files. The method to rename an histogram is SetName.

Thanks. One more thing, is it possible to superimpose so many histograms to make again a histogram? Because I need to scale the histogram.

The best will be to add them.

Thanks. Yeah I was thinking of it . But thirty histograms ::frowning:

A for loop should make it easy.

Okay. Thank you I will try.

It’ done. Thank you so much :smile:

1 Like

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