Problem when creating array of histograms

Dear rooters,

I am facing the following issue : I want to create a vector of histograms contained in a root file.
MY code is

[code]std::vector<TH1F*> hists;//Raw signals

//Load File
TFile *f = new TFile(TString::Format("HPGe_%d_%d_%d.root",runfirst, runlast, last_segment));
if (f->IsOpen()==kFALSE) {cout << "File " << f->GetName() << " not found" << endl;}

for (int run = runfirst; run <= runlast; run++){
	for (int seg = first_segment; seg <= last_segment; seg++) {
		cout << "Run     : " << run << endl;
		cout << "Segment :      " << seg << endl;
		cout << "-----------------" << endl;
		
		TH1D *hSignal     = (TH1D*)f->Get(TString::Format("h_HPGe_%d_%d_%d", detn, run, seg));
		if(!hSignal){cout << "***Segment not found. Moving on." << endl;	continue;}
		hists.push_back(hSignal);	
		hSignal->Delete();
	}//End of loop over segments
}//End of Loop over runs[/code]

I am loading and compiling this macro and I get the following error

[quote]error: no matching function for call to ‘std::vector<TH1F*, std::allocator<TH1F*> >::push_back(TH1D*&)’ /usr/lib/gcc/x86_64-redhat-linux/4.4.7/../../../../include/c++/4.4.7/bits/stl_vector.h:733: note: candidates are: void std::vector<_Tp, _Alloc>::push_back(const _Tp&) [with _Tp = TH1F*, _Alloc = std::allocator<TH1F*>][/quote]

It seems like the histograms in the file are addresses? I am so confused…

Any ideas?

Hi,

you are using a vector of TH1F* and pushing TH1D*.
You should use a vector of TH1D*.

Danilo

Try: [code]#include “TFile.h”
#include “TH1.h”
#include “TString.h”
#include “TDirectory.h”
#include “TROOT.h”

#include

std::vector<TH1D*> *myhists(int runfirst, int runlast,
int first_segment, int last_segment,
int detn)
{
TDirectory cwd = gDirectory;
std::vector<TH1D
> hists = new std::vector<TH1D>; // Raw signals

// Load File
TString f_name = TString::Format(“HPGe_%d_%d_%d.root”,runfirst, runlast, last_segment);
TFile *f = TFile::Open(f_name);
if ((!f) || (f->IsZombie())) {
std::cout << “File " << f_name << " could not be opened.” << std::endl;
delete f;
if (cwd) cwd->cd();
return hists;
}

for (int run = runfirst; run <= runlast; run++) {
for (int seg = first_segment; seg <= last_segment; seg++) {
std::cout << "Run : " << run << std::endl;
std::cout << "Segment : " << seg << std::endl;
std::cout << “-----------------” << std::endl;
TH1D *hSignal;
f->GetObject(TString::Format(“h_HPGe_%d_%d_%d”, detn, run, seg), hSignal);
if(!hSignal) { std::cout << “***Segment not found. Moving on.” << std::endl; continue; }
hSignal->SetDirectory(cwd); // (cwd) … or … (0) … or … (gROOT)
hists->push_back(hSignal);
} // End of loop over segments
} // End of Loop over runs

delete f;
if (cwd) cwd->cd();
return hists;
}[/code]