Creating multiple histograms

Hello Rooters,

I am currently trying to write a code that will read in a general data set. I can get it read into a general array, such that the number of lines and columns are arbitrary.
The next step however is to create histograms of each read in column. In trying to do this I ran into a problem, namely I cannot create the histograms until I know how many columns of data there are, but I have to write the code not knowing how many columns of data. So I tried creating a general histogram so that I could create a loop and thus create as many as needed. However I cannot get it to work. Any thoughts would be useful. I also need to be able to do this with canvas’.

Thanks
-Paul

Here is a sample code

{
       gROOT->Reset();

       // these arrays would become 2 dimensional for multiple names
       char h0[] = "hist";  
	char histogram_name[] = "histogram";
    
       // this would go in a loop to create as many as needed
	TH1F h0(histogram_name, histogram_name, 100, -3, 3);
	
       //there would also need to be any given number of these
	TCanvas *Canvas1= new TCanvas("Canvas1", "Canvas 1", 1);
       Canvas1->Divide(1,2);

       Canvas1->cd(1);
       h0->Draw();
}

It is not clear what you are trying to do!
Do you want to read an ascii file?
If yes, may be have a look at TTree::readFile or TNtuple::ReadFile
see: root.cern.ch/root/htmldoc/TTree. … e:ReadFile

Rene

Hi Again,

no I understand how to read in a ascii file. My question relates to creating TH1F’s
I read in an ascii file and I get an nxm array of type Float where n is the number of rows and m is the number of columns. Then I want to create a loop (for, while…) to create m TH1F s all with diffrent names so that each one corresponds to a column. However I cannot simply start creating the TH1F’s as follows:

TH1F *h0 = new TH1F(“Histo 0”, “Histo 0”, 10, 0, 100);
TH1F *h1 = new TH1F(“Histo 1”, “Histo 1”, 10, 0, 100);
TH1F *h2 = new TH1F(“Histo 2”, “Histo 2”, 10, 0, 100);
.
.
.
TH1F *hm = new TH1F(“Histo m”, “Histo m”, 10, 0, 100);

I cannot do it that way because I do not know how many I want to create until after I have read in the text file as the number changes from file to file. so what I tried doing was something more along the lines of:

char h[] = “h”;

for( i=0;
i <=m;
i = i +1)
{
h[i] = “h”;
strcat(h[i], (char)m); // so now h[i] would be hm
TH1F *h[i] = new TH1F(Histo[i], Histo[i], 10, 0, 100);
}

this way I could vary m and Histo[i]
my problem comes in when I try to vary *h[i] no matter how I try it always gives me an error of some sort.

I hope that makes things a little more clear. Sorry for the confusion earlier.

-Paul

Create a dynamic array of pointers

  TH1F** h = new TH1F*[m];
  for (int i=0;i<m;i++) {
     h[i] = new TH1F(name,title...

Rene

Dear brun,
This code does not work at all.
Hai

Hi,

Could you be more precise? What doesn’t work? Could you tell us what you’re trying to do, and if possible with a piece of code showing the problem?

Cheers, Bertrand.

I tried the code written as follows:

 int m=5;
 Th1F** h=new TH1F*[m];
 for (int i=0; i<m;i++){
  h[i] = new TH1F("h0","event",1000,0,1000);
}


Running code in Root, it returns:

Warning in <ROOT::Append>: Replacing existing TH1: h0 (Potential memory leak).
Warning in <ROOT::Append>: Replacing existing TH1: h0 (Potential memory leak).
Warning in <ROOT::Append>: Replacing existing TH1: h0 (Potential memory leak).
Warning in <ROOT::Append>: Replacing existing TH1: h0 (Potential memory leak).

Could you let me know the problem?
Thanks!
Hai

Try:

h[i] = new TH1F(TString::Format("h0_%d", i), "event", 1000, 0., 1000.);
3 Likes

It works well! Thank you very much!