Gaussian fitting of mutiple peaks in the case of unknown number of peaks in histogram generated from .tex file (for example: test.tex). What are the change required in following code (this code is for 5 peaks)?

It behaves abnormally, sometimes I get the error stating “par” and other undeclared identifiers and sometimes it runs giving the attached result.

This is what I want to do, as in this example (the only difference is that 1. I have .tex file, not the root file and 2. no. of peaks are not known, they need to be automatically detected. ) I’m attaching my sample histo

whose Gaussian peak fitting is planned.

Maybe you can help, hopefully !!