THStack::Add members of histogram vector

Hello - I’m trying to loop through vector of TH1D histograms and add each to a THStack, but I receive an error. I’m learning c++ as I go, so this may be a problem of syntax. Here is the code:

// This works fine
vector<TH1D> hThresh_particlesCPV1(11);
   for (unsigned int i=0; i<11; i++) {
      hThresh_particlesCPV1[i]=TH1D(hThresh_names[i], hThresh_titles[i], bins,0,5);
// The Add below is causing the error  
THStack *hsCombined = new THStack("hStacked_threshold", "Charged particles above each energy threshold");
for (unsigned int i=0;i<11;i++){

And here is the error:




you should pass a pointer to the histogram and not the histogram itself. I believe something like


may yield better results.


Benedikt - Yes! That’s it! I was thinking I had to pass a pointer, but I couldn’t figure out how. I understand your syntax clearly. Thank you so much!

Sure. The difference between


is that in the first case you deal with an object directly, while in the second case you deal with a pointer to a TH1. That’s the function of the *.

Now coming to the &. The value contained within a pointer is actually the physical address of the object you point at. And the C++ syntax to retrieve the address of an object is &theObject.

So in combination you do the following. you have an object and you need to pass in a pointer to the object. You do &hThresh_particlesCPV1[i] to retrieve the address and with (TH1*) you let the compiler know that the address you got points actually to an object of the kind TH1. It may even be that you can remove the (TH1*) in your statement if the compiler is smart enough to guess what you mean.

Hope that helps,