C++ Question


I’m trying to automatically fill a large number of histograms. I have a set of histograms named { I1, I2, I3 , … }. I loop over a a large number of events, and for each event:


char histname[50];

for (int i = 0; i < nbins; i++) {
sprintf(histname, “I%d”,(i+1));

if (pt > bin[i] && pt < bin[i+1]) {histname->Fill(mass);}



I get an error “expression must have pointer-to-class type”.
How should I go about doing this?


Your code piece has no sense whatsoever.

Please describe all the variables used. Especially how you create histograms.

What I noticed is that histname has to be a pointer to a histogram, not a char string.

and instead of %d you should use %i.

other than that - hard to say.

You could also use a 2-dimensional histogram, pt vs mass. This would simplify the filling. In case you need the 1-dimensional pt distributions, you can easily get them by doing a projection.


[quote]char histname[50];

You are getting confused by the CINT extension that allow him to parse a variable name and automatic defined and load it if is find it in a file.
Instead you should have

[quote]char histname[50];
TH1F * hist = new TH1F(histname,“generic title”,nbins,min,max);
(Or some other way to declare and define the histogram).


Excellent, thanks!