Hi,
Consider the attached histogram. How would one go about normalising the Y-axis into yield%. I guess I want to divide the data points on Y-axis by the total number of counts (entries). But is there a way to do it in ROOT?
Thanks!
Edit: Sorry, here is the a snip:
Just added it. Thanks for letting me know.
Thanks, but I don’t seem to find a way to put everything together. Here’s my code:
{TFile *file = new TFile("myFile.root");
TTree *myBranch= (TTree *) file->Get("myBranch");
myBranch->Draw("myBranch>>h")
}
This will only give the attached photo, so how to link normalisation method to it?
P.S. I want to normalise to 100 (percentage).
{
// warning: due to a long-standing known bug in ROOT 6, all variables
// need to be defined right in the beginning of an "unnamed macro"
TFile *f;
TTree *t;
TH1D *h;
Double_t integral;
f = TFile::Open("myFile.root");
if ((!f) || f->IsZombie()) {delete f; return;} // just a precaution
f->GetObject("myTree", t);
if (!t) {delete f; return;} // just a precaution
gROOT->cd(); // all newly created histograms should be placed here
h = new TH1D("h", "number of particles;n;yield [%]", 110, 0., 55.);
h->Sumw2(kTRUE);
t->Project("h", "nParticles");
delete f; // no longer needed (automatically deletes "t", too)
integral = h->Integral();
if (integral != 0) h->Scale(100. / integral); // "normalize" to 100
h->Draw();
}
Thank you. It is working well. And here is the result:
If you don’t mind, I have one more question. If you want to rescale Y-axis to a certain value, how would you approach that? For instance, I want to know the percentage of every mass number (0-55). Mathematically, it should be:
counts * 100 / total counts.
Counts: each counts reading on Y-axis.
Not sure what you mean. Isn’t it what this line does?
if (integral != 0) h->Scale(100. / integral);
Oh yes indeed. I confused it with another histogram I have. It all right thank you again.