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

**URL:** <https://root-forum.cern.ch/t/importing-2d-large-data-from-text-file-in-root-and-plotting-it-performing-pattern-recognition-on-charge-cluster-formation/34752>\
**Category:** Newbie\
**Created:** [June 21, 2019, 5:40pm UTC](https://root-forum.cern.ch/t/importing-2d-large-data-from-text-file-in-root-and-plotting-it-performing-pattern-recognition-on-charge-cluster-formation/34752 "2019-06-21T17:40:45Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![sanchitsharma](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/s/439d5e/32.png) [@sanchitsharma](https://root-forum.cern.ch/u/sanchitsharma)\
**Post date:** [June 21, 2019, 5:40pm UTC](https://root-forum.cern.ch/t/importing-2d-large-data-from-text-file-in-root-and-plotting-it-performing-pattern-recognition-on-charge-cluster-formation/34752/1 "2019-06-21T17:40:46Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Dilicus](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/dilicus/32/14338_2.png) [@Dilicus](https://root-forum.cern.ch/u/Dilicus)\
**Post date:** [June 23, 2019, 9:19pm UTC](https://root-forum.cern.ch/t/importing-2d-large-data-from-text-file-in-root-and-plotting-it-performing-pattern-recognition-on-charge-cluster-formation/34752/2 "2019-06-23T21:19:19Z")

</div>

Hi,  
try something like this,

```auto
//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

---

<div class="post-metadata">

**Author:** ![sanchitsharma](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/s/439d5e/32.png) [@sanchitsharma](https://root-forum.cern.ch/u/sanchitsharma)\
**Post date:** [June 24, 2019, 2:40pm UTC](https://root-forum.cern.ch/t/importing-2d-large-data-from-text-file-in-root-and-plotting-it-performing-pattern-recognition-on-charge-cluster-formation/34752/3 "2019-06-24T14:40:39Z")

</div>

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!

 ![asasas](https://root-forum.cern.ch/uploads/default/original/3X/7/a/7a26c2a81f82d582f8a8f410b30316b710af8d2f.png)

> [@Dilicus](#):
>
> THD2 \*h2d= new THD2(“h2d”,“My histogram”,256,0,256,256,0,256);

---

<div class="post-metadata">

**Author:** ![Dilicus](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/dilicus/32/14338_2.png) [@Dilicus](https://root-forum.cern.ch/u/Dilicus)\
**Post date:** [June 24, 2019, 2:44pm UTC](https://root-forum.cern.ch/t/importing-2d-large-data-from-text-file-in-root-and-plotting-it-performing-pattern-recognition-on-charge-cluster-formation/34752/4 "2019-06-24T14:44:05Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![sanchitsharma](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/s/439d5e/32.png) [@sanchitsharma](https://root-forum.cern.ch/u/sanchitsharma)\
**Post date:** [June 24, 2019, 2:58pm UTC](https://root-forum.cern.ch/t/importing-2d-large-data-from-text-file-in-root-and-plotting-it-performing-pattern-recognition-on-charge-cluster-formation/34752/5 "2019-06-24T14:58:43Z")

</div>

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](https://root-forum.cern.ch/uploads/default/original/3X/b/7/b7c225aac2e7cf60f492c80733229e86b9511456.png)

---

<div class="post-metadata">

**Author:** ![Dilicus](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/dilicus/32/14338_2.png) [@Dilicus](https://root-forum.cern.ch/u/Dilicus)\
**Post date:** [June 24, 2019, 3:27pm UTC](https://root-forum.cern.ch/t/importing-2d-large-data-from-text-file-in-root-and-plotting-it-performing-pattern-recognition-on-charge-cluster-formation/34752/6 "2019-06-24T15:27:36Z")

</div>

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.

> [@Reading files, Output files](https://root-forum.cern.ch/t/reading-files-output-files/34493):
>
> if my Files are ordered from file-000.txt to file-100.mca My files path is “/home/Baja/Downloads/file-000.mca, …” How can modify the attached code to read the files one by one, and also save the output results. and how can i make the output files.txt with the same name, and automatic saves in a folder ! for example if the input file is “/home/Baja/Downloads/InputFolder/file-000.mca, …”, the output will be " “/home/Baja/Downloads/ResultsFolder/file-000.txt …” [BAJA.C](https://root-forum.cern.ch/uploads/default/original/3X/8/5/85da94075079e0015682b98bdcb073a31e441d13.C) (367 Bytes)

---

<div class="post-metadata">

**Author:** ![sanchitsharma](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/s/439d5e/32.png) [@sanchitsharma](https://root-forum.cern.ch/u/sanchitsharma)\
**Post date:** [June 24, 2019, 3:42pm UTC](https://root-forum.cern.ch/t/importing-2d-large-data-from-text-file-in-root-and-plotting-it-performing-pattern-recognition-on-charge-cluster-formation/34752/7 "2019-06-24T15:42:33Z")

</div>

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

Here is my code:

```auto

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.

---

<div class="post-metadata">

**Author:** ![Wile\_E\_Coyote](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/wile_e_coyote/32/12417_2.png) [@Wile\_E\_Coyote](https://root-forum.cern.ch/u/Wile_E_Coyote)\
**Post date:** [June 24, 2019, 3:57pm UTC](https://root-forum.cern.ch/t/importing-2d-large-data-from-text-file-in-root-and-plotting-it-performing-pattern-recognition-on-charge-cluster-formation/34752/8 "2019-06-24T15:57:08Z")

</div>

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 …”.

---

<div class="post-metadata">

**Author:** ![sanchitsharma](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/s/439d5e/32.png) [@sanchitsharma](https://root-forum.cern.ch/u/sanchitsharma)\
**Post date:** [June 24, 2019, 4:10pm UTC](https://root-forum.cern.ch/t/importing-2d-large-data-from-text-file-in-root-and-plotting-it-performing-pattern-recognition-on-charge-cluster-formation/34752/9 "2019-06-24T16:10:52Z")

</div>

Hello,

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

```auto
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](https://root-forum.cern.ch/uploads/default/original/3X/4/5/45f515a76de0dee3902d9f535ccf27c961964cd7.png)

---

<div class="post-metadata">

**Author:** ![Wile\_E\_Coyote](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/wile_e_coyote/32/12417_2.png) [@Wile\_E\_Coyote](https://root-forum.cern.ch/u/Wile_E_Coyote)\
**Post date:** [June 24, 2019, 4:18pm UTC](https://root-forum.cern.ch/t/importing-2d-large-data-from-text-file-in-root-and-plotting-it-performing-pattern-recognition-on-charge-cluster-formation/34752/10 "2019-06-24T16:18:45Z")

</div>

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

```auto
{ // 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)

```
