Dear gsl help, This question concerns a c++ program I am writing that makes use of gsl, compiled with g++.
I have a dataset organized as several million rows (events) with about 12 numbers (columns) associated to each event, and I'd like to create 1D and 2D histograms that bin those events based on the values in one or two of those columns. I've successfully created such 1D histograms using gsl n-tuples and histograms. My question is about finding an efficient way to create 2D histograms this way. Based on the online documentation, gsl_ntuple_project() seems defined for 1d histograms, and not 2d histograms. To work around this I've created a loop in which I create as many 1D histograms as I require, each using the "horizontal" axis binning, to successively assemble the second (vertical) dimension of my histogram, using the selection function to apply my binning criteria for the vertical binning dimension during each pass through the loop. The loop in question looks like this: for (int iy = 0; iy < num_vertical_bins; iy++) { .... S.params = &bin_params; // vertical binning criteria enter here big_ntuple = gsl_ntuple_open (filename, &ntuple_row, sizeof (ntuple_row)); gsl_histogram_reset(horizontal_histogram); gsl_ntuple_project(horizontal_histogram, big_ntuple,&V,&S); gsl_histogram_fprintf(outfile, horiontal_histogram, "%g", "%g"); gsl_ntuple_close(big_ntuple); } This provides the desired behavior. However, calling gsl_ntuple_open and gsl_ntuple_close during each pass through the loop slows things down significantly. If I try to move the two lines that call gsl_ntuple_open and gsl_ntuple_close outside the loop (before and after, respectively), the code executes much faster, but it no longer correctly fills the vertical dimension of the histogram: only the first histogram written out (during the first pass through the loop) has nonzero bin counts, and the subsequent histograms that are written out are filled with zero counts. My question is, is it indeed necessary that I call the gsl_ntuple_open and gsl_ntuple_close functions from inside the loop? If not, how can I avoid the behavior I'm seeing where all the except for the first get filled with zero bin counts? Or, is there generally a better way to create the sort of 2D histogram I am describing? Thank you, Nathan