# Array of TH1F* problem

I’m trying to merge some histograms into one (I have to fit a background distribution compounded by several channels). For each bin, I take the max value among the various histos. I’m accustomed to use STL so I would like to use vectors.

``````typedef vector<TH1F> HistVector_t;
typedef vector<TH1F>::const_iterator HistVectorItr;

HistVector_t bkg_vec;
bkg_vec.push_back(&h_j1);
.
.
.
bkg_vec.push_back(&h_j8);
TH1F* h_bkg = MergeHistograms(bkg_vec, hmodel, sout);``````

Here it is MergeHistogram:

``````TH1F* MergeHistograms(HistVector_t &hvec, TH1F* hmodel, ostream& sout) {

TH1F* h_bkg = (TH1F*)hmodel->Clone("h_bkg");
sout << endl << "Merging "<< hvec.size() << " background histograms." << endl;

for(Int_t binc = 1; binc <h_bkg>GetNbinsX(); binc++) {    //for each bin

vector<Float_t> values;
sout << "Bin " << binc <<endl>GetBinContent(binc);   // fill values vector
//sout << "value = " << vtemp << endl;
values.push_back(vtemp);
}
//find max element
sout << "Finding max el.." << endl;
vector<Float_t>::iterator melement = max_element( values.begin(), values.end() );
//sout << "Bin: " << binc << ", value= " << melement <<endl>SetBinContent(binc, (*melement) );
}

return h_bkg;
}``````

When I launch the macro, ROOT says:
*** Break *** bus error
Debugging the code, the error is here:

I’ve got another error if I remove brackets, or if I use double deference **.

Can you, please, attach small macro, reproducing you problem?
Telepathy does not exist, so it’s difficult to understand, what’s in your missed code and code, broken by this forum’s formatting.

By the way, to find max/min bin content, you do not need to copy bin content
from hist into vector, and lookup in this vector with std::max/min element - at least it looks strange. Use hist specific:
TH1F::GetMaximum if you want max value or TH1F::GetMaximumBin if you want index.

I am very sorry for this ugly misformatting. In attachment you can find a macro with the same problem.
BTW, I’m using ROOT v5.04/00 20 September 2005 on a Mac Mini PPC.
testmacro.C (1.76 KB)

Forum’s formatting is not you fault )

1. You take an adress of h2 and h3 - thats wrong, they are pointers already, you should push them directly.
2. include algorithm heade to use max_element, and use qualification
std::max_element
3. My previous letter about GetMaximum can be ignored (that’s becuase of formatting ) )
4. Must be (*it)->GetBinContent(…), not (*it).GetBinContent().
expression *it has type TH1F *.
5. (advice) Do not create vector inside a loop, create it outside and call
values.clear() before you start filling it in the inner loop
``````vector<Float_t> values;
for(....) {    //for each bin
values.clear();
for(...) {
///////
values.push_back(vtemp);
}
}``````

Ok, thank you very much. Now it’s working properly. All the mess started with this wrong push_back. Cheers