# About Multithreading for reading and analyse root file in a loop

**URL:** <https://root-forum.cern.ch/t/about-multithreading-for-reading-and-analyse-root-file-in-a-loop/44329>\
**Category:** Newbie\
**Tags:** tree, hist, root\
**Created:** [April 8, 2021, 12:17pm UTC](https://root-forum.cern.ch/t/about-multithreading-for-reading-and-analyse-root-file-in-a-loop/44329 "2021-04-08T12:17:38Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Siddharth\_Parashari](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/siddharth_parashari/32/25501_2.png) [@Siddharth\_Parashari](https://root-forum.cern.ch/u/Siddharth_Parashari)\
**Post date:** [April 8, 2021, 12:17pm UTC](https://root-forum.cern.ch/t/about-multithreading-for-reading-and-analyse-root-file-in-a-loop/44329/1 "2021-04-08T12:17:38Z")

</div>

Hi,  
I am reading a branch from a root file. Then using the branch to create a histogram in a loop and doing some calculations. So every time I need to make a histogram the first loop is for a variable and another loop over entries to fill the histogram. So it is taking too much time over my system and using only one CPU. My PC has 24 CPUs and I want to utilize all of them. I have tried implicit MT using ROOT::EnableImplicitMT(nThreads); but no benefit as it is then taking more time to perform. So is there a way so that I can distribute my loop over multiple threads. The loop looks like this…

```auto
TFile *input = new TFile(Form("%s",fname), "READ");
TTree *data = (TTree*) input->Get("data");
data->SetBranchAddress("q", &q);
int entries = data->GetEntries();

for(CHID=1;CHID<384;CHID++)
{
       TH1F* h1 = new TH1F("h1", "Spectrum of Gamma Rays", nbins, 0, 100);
        if(CHID >64 && CHID < 320){continue;}
        cout<<"Performing fit for Channel ID ="<<CHID<<"\n"; 		
		for(j=1;j<=entries1;j++)
		{
                 data->GetEntry(j);
		 if(q[CHID]>0)
		        {
                         h1->Fill(q[CHID]);
			}
			else{continue;}
                  }	
		h1->Draw("L")
}

```

_ROOT Version:_ 6.22/08

* * *

---

<div class="post-metadata">

**Author:** ![eguiraud](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/eguiraud/32/32217_2.png) [@eguiraud](https://root-forum.cern.ch/u/eguiraud)\
**Post date:** [April 13, 2021, 8:21am UTC](https://root-forum.cern.ch/t/about-multithreading-for-reading-and-analyse-root-file-in-a-loop/44329/2 "2021-04-13T08:21:10Z")

</div>

Hi @Siddharth_Parashari ,  
closing this post in favor of its duplicate [About Multithreading for reading and analyse root file](https://root-forum.cern.ch/t/about-multithreading-for-reading-and-analyse-root-file/44331) .

Cheers,  
Enrico

---

<div class="post-metadata">

**Author:** ![eguiraud](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/eguiraud/32/32217_2.png) [@eguiraud](https://root-forum.cern.ch/u/eguiraud)\
**Post date:** [April 13, 2021, 8:21am UTC](https://root-forum.cern.ch/t/about-multithreading-for-reading-and-analyse-root-file-in-a-loop/44329/3 "2021-04-13T08:21:14Z")

</div>

Closed on April 13, 2021, 8:21am UTC


