I have two programs. The first program fills histograms and saves them to Histos.root. The second program opens Histos.root and uses the histograms for analysis.
In the second program, it is convenient for me to know exactly how many histograms Histos.root contains.
Is there a way to get this number?
Is there any way to know how many histograms are in the file without doing this? I want to implement a function that creates a matrix (vector>) and contains in each row the binning of each of the histograms in the file. In order to fill the vector, I need the numbers of rows which would be equal to the number of histograms in the file.
thanks for reviving the thread after 6 years.
Can’t you start from the solution of bertrand to identify the histograms in the file starting from the associated TKey and fill your vector/matrix with their numbers of bins?
Thanks to you for paying me attention!! What you are saying has a lot of sense, however, I will still need to figure out until what number should the first buckle goes. I don’t have any idea about how to do it. I tried to find out if any of the TFile or TKey member functions can give actually how many objects of a specific kind they contain, but I didn’t find anyone. I am rather new in the business, so all the help you can give me is more than welcome.
I am not sure what you are trying to achieve. You have in hands a recipe to extract the number of bins of all histograms in a file. Is there anything else you would like to do?
You’re right, I know I can do the extraction of all the bins but this is not what I want, What I want is something like the piece of code below.
vector<vector<double>> bin_matrix; //this should contain as many rows as histogram I have in the root file
for ( int i = 0; i < Total_number_histo; i++) {//here I am setting the row of my matrix
vector<double>temp;
for(int j = 1; j < Number_of_bins_Histo; j++){
//... some code for taking the name of the histogram and the lowedge of its bins ...
temp.push_back(lowedge);
}
bin_matrix.pus_back(temp);
}
I don’t know if it’s clear now, as you can see, I will need to know the number of histograms in the file, in principle, I can run the recipe and get this total number and then put it in my code, but I will be running twice, because of I use later this piece of code to move over all the histograms in the root file.
Well, from every histogram in the file, I want to get a vector that contains the name of the histogram as its first element and the rest of the elements should be the edge of every bin. Later I need to loop over these vectors, to fill other histograms with different data, but the same name and bins.
so why not having a vector of strings for the names and a second deque of vectors of doubles with the bin edges? (vectors of vectors are bad data strucures…)
The procedure to find the histograms is in place, you just need to fill the data structures…
ok, I think it sounds better, also I realize that a vector of vector wouldn’t work, because of the number of bins could be different, so I would never have a matrix. BTW, do you have any specific reasons for
when you extend a vector, it could reallocate. If it reallocates, elements must be copied. The copies can have a price, e.g. linked to further allocations. This kind of patterns brings to degradation of performance.
Hello, sorry for reviving this old thread. I was wondering if someone could explain what some of lines of @bellenot codesnippet do. Especially in the block which starts with:
TIter next((TList *)f->GetListOfKeys());
while (key = (TKey *)next()).......
I have a hard time understanding what this keys stuff does.