Fast and easy storing of histograms (std::map or else?)

In order to manage the large amount of histograms, I am using a
map:

This allows me to store histograms by name

AND saving them using a loop.

Since I have more than one Monte Carlo type of data I end up creating

which leads to string manipulations (which are not as easy in C++):

for (short int i = 0; i < mcsize; i++) {
		string histname;
			histname = mclabel[i] + "_" + "neutrino_pz";
		hists_[histname.c_str()] = new TH1F(histname.c_str(), "reconstructed p_z(#nu)", 50, -500, 500);
		histname = mclabel[i] + "_" + "neutrino_pz_mc";
		hists_[histname.c_str()] = new TH1F(histname.c_str(), "reconstructed p_z(#nu)", 50, -500, 500);;
		histname = mclabel[i] + "_" + "recoWmass";
		hists_[histname.c_str()] = new TH1F(histname.c_str(), "reconstructed leptonic W mass", 50, 0, 500);
	}
for ( std::map<string, TH1F*>::const_iterator iter = hists_.begin();
	      iter != hists_.end(); ++iter )
		iter->second->Sumw2();
}

Now I would like to store them as:

to store them as

This would be fine, but since I heard that maps are SLOW in C++ and
use a lot of memory,
I wonder if there is a better method to do so.

Is TMap much better?
For this I would only need to use TString instead of std::string, right?

Thank you in advance.

Hi,

I’d use the following approach: define two enums:

[code]enum EMCType {
kWhatever,

kNumMCTypes
};
enum EHist {
kNeutrinoPz,

kNumHists
};
const char* mcnames[] = { “Whatever”, … }
const char* histnames[] = { “neutrino_pz”, … }

vector<vector<TH1*> > hists;
hists.resize(EMCType);
for (int mctype = 0; mctype < kNumMCTypes; ++mctype)
hists[mctype].resize(kNumHists);
for (int mctype = 0; mctype < (int) kNumMCTypes; ++i)
for (int ihist = 0; ihist < kNumHists; ++ihist)
hist[mctype][ihist] = new TH1F(TString(mcnames[mctype]) + histnames[ihist], …);
[/code]

You will get rid of all the string manipulations to access the histograms, and you use vectors instead of maps which makes it much faster.

Cheers, Axel.

Hi Alex, thanks for your response.

So your approach means, I can access them via:

Not using strings for index, but still clear names.
And the best of all: since the current implementation is already a
vector, I don’t need to change much to start working with it.

Cheers,
Luke