"Dynamic" TH1 creation

I’m writing some some code, which is designed to take as input two vectors containing between 1 and 10 non-empty sets of data, each of which is to be plotted in a separate TH1. These histos are filled, and plotted “ontop” of one another using the “SAME” drawing command.

Currently, I simply define 10 TH1’s. I have 10 for loops for filling the histograms, which run only if there exists a corresponding dataset (e.g. the 10th loop will run only if there are 10 sets of XY data). After filling, I check each histogram, drawing only those with at least one entry.

As you can imagine, this is a bit messy and cumbersome. I imagine there must be a better way?

I have something like this:

void my_messy_function(vector<vector<double>> data){

  TH1 *h1 = new TH1D(...);
  TH1 *h2 = new TH1D(...);
  TH1 *h3 = new TH1D(...);


  if(data.size() > 1)

  if(data.size()> 2)


  if(h2->GetEntries() > 0)




I guess instead of

you can define an array of histograms, and then fill them all in the same loop. Do you think it will work for you?

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.