TH1F - bin access

Do we gave to a function in the TH1F class to

void someFunc(TH1F *h1) {
	int x; //the bin to determine
	float y=0.5; //a known bin content value
	
	x =??; // In the histogram h1, find the bin of some value x with content y
	
	return;
}
Any elegant way to do this? Thanks

You should spend a little time to think about what you want this function to achieve. What should happen if there are multiple occurrences of the value you search for in the histogram? Since you use TH1F in your example code, how do you plan to check bin contents for equality? TH1F bin contents are floats which could be the results of rescalings so that mathematically equal values might not compare as equal. Do you plan to use this code only on histograms with integer bin contents (TH1S, TH1C and TH1I)?

Only after that you can try to understand what you mean by “elegant” and why that is an important requirement. Are you trying to write a piece of software which can be understood and maintained by your fellow HEP collaborators (or yourself in a couple of weeks), or are you working on a portfolio to apply for a job at a tech company? In the first case I would suggest a simple loop over the bin contents using TH1::GetBinContent and a check for equality, while in the latter case an implementation using a combination of TH1::GetArray, std::find and std::distance seems tempting (and don’t forget that a TH1 has GetNbinsX()+2 bins).