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++){
hsCombined->Add((TH1)hThresh_particlesCPV1[i]);
}
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.