Importing 2D large data from text file in root and plotting it, performing pattern recognition on charge cluster formation

Hi,
I am very new to ROOT. My task is to read a series text files with 256 X 256 data matrix and do pattern recognition analysis in each of the files.

Can you please suggest me the things I need to focus to achieve this also, an example that imports a 256 X 256, 2D histogram of data to root and plots it as well will be hugely appreciated.

Thanks very much!

Regards,
Sanchit Sharma

Hi,
try something like this,

//open the file in rading mode
FILE *file=fopen("mytext.txt","r");
//create a 256x256 histogram
TH2D *h2d= new TH2D("h2d","My histogram",256,0,256,256,0,256);
double z;
int i,j;
//for loops to read 256x256 file
for(i=1; i<=256; i++)
for(j=1; j<=256; j++)
{

//read the number
fscanf(file,"%lf",&z);
//set the bin content i,j to value z just red
hd2->SetBinContent(i,j,z);

}

//drawing the histogram 
h2d->Draw("colz")

Cheers,
Stefano

1 Like

Hi I am getting error while using your method. Please let me know what am I doing wrong, I have just started using root? Any Ideas about implementing a pattern recognition code?

Thanks!

My bad the class is TH2D and not THD2.
And after the for parenthesis you don’t need the ;

Sorry, I will edit the post above

Hi,

Thanks for the update. However it still gives me an error (pic attached).

Also I have 7000 files that I need to run one by one Can we do it in a for loop. The files are names like a1.txt . . . . . a7000.txt

as2

I think the last tow errors are due to a missing ; in the last line.

and maybe try to add an { before the second for
and a } after the hd2->SetBinContent(i,j,z);

Have a look at the post below for the file question.

1 Like

Okay I got the file part. But the plotting part still gives me error.

Here is my code:


FILE *file=fopen("100VAmBeBack_004938.txt","r");
//create a 256x256 histogram
TH2D *h2d= new TH2D("h2d","My histogram",256,0,256,256,0,256);
double z;
int i,j;
//for loops to read 256x256 file
for(i=1; i<=256; i++)
{
	for(j=1; j<=256; j++)
	{
	//read the number
		fscanf(file,"%lf",&z);
		//set the bin content i,j to value z just red
		hd2->SetBinContent(i,j,z);
	}
}

//drawing the histogram 
h2d->Draw("colz");

//drawing the histogram 
h2d->Draw("colz");

I get the same errors as mentioned above.

It’s like the good old Wile Shake Spear wrote “To h2d, or to hd2, that is the question: Whether 'tis nobler in the mind …”.

1 Like

Hello,

I have tried with that rectification too, still doesn’t works.

FILE *file=fopen("100VAmBeBack_004938.txt","r");
//create a 256x256 histogram
TH2D *h2d= new TH2D("h2d","My histogram",256,0,256,256,0,256);
double z;
int i,j;
//for loops to read 256x256 file
for(i=1; i<=256; i++)
{
	for(j=1; j<=256; j++)
	{
	//read the number
		fscanf(file,"%lf",&z);
		//set the bin content i,j to value z just red
		h2d->SetBinContent(i,j,z);
	}
}

//drawing the histogram 
h2d->Draw("colz");

as2

It seems to me that you are missing the proper C++ “ROOT unnamed macro” structure:

{ // begin of an unnamed macro (the first line of your script)
  // ...
  FILE *file=fopen("100VAmBeBack_004938.txt","r");
  // ...
  fclose(file); // after the "for" loop
  // ...
  h2d->Draw("colz");
  // ...
} // end of an unnamed macro (the last line of your script)