# How to add multiple files created over multi threads in TThread into one file

**URL:** <https://root-forum.cern.ch/t/how-to-add-multiple-files-created-over-multi-threads-in-tthread-into-one-file/48918>\
**Category:** Newbie\
**Tags:** tree, root\
**Created:** [February 23, 2022, 9:07am UTC](https://root-forum.cern.ch/t/how-to-add-multiple-files-created-over-multi-threads-in-tthread-into-one-file/48918 "2022-02-23T09:07: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:** [February 23, 2022, 9:07am UTC](https://root-forum.cern.ch/t/how-to-add-multiple-files-created-over-multi-threads-in-tthread-into-one-file/48918/1 "2022-02-23T09:07:39Z")

</div>

Hi,  
I am using TThreads to perform a calibration process over multiple threads. My primary input file is around 5 GB and I am spawning my threads to fill the histograms over 1024 channels quickly and then calibrating them separately. In the end, I am able to write multiple TFiles (depends on number of threads) in output with calibrated values. I want to know if I can merge all these files into one in the same code itself. I tried to use TBufferMerger but it leaves me in an infinite loop every time. Is there any efficient way to do this??  
The code is attached below.

Kindly check the function

void\* write\_outfile(void\* ptr){  
gROOT-\>cd();  
Long64\_t\* N = (Long64\_t\*) ptr;  
gROOT-\>SetBatch();  
ROOT::EnableThreadSafety();  
// std::string fileName = oname;  
// ROOT::Experimental::TBufferMerger merger(“testfile.root”, “RECREATE”);  
// auto file = merger.GetFile();

```
for(auto j=N[1]; j<N[2]; j++){
   
    // auto tree = new TTree("data","data");
    // tree->Branch("q",&qcalib,"qcalib[1024]/F");
    // tree->Branch("t",&tcalib,"tcalib[1024]/l");
    chain[N[0]]->GetEntry(j);
    for(CHID=channel_start;CHID<=channel_end;CHID++){
        if(CHID >63 && CHID <320){continue;}
        if(q[CHID]>0 ){
            //Float_t S= pow(q[CHID],par[CHID][2]);
            //qcalib[CHID]=par[CHID][0]*pow(par[CHID][1],S)+par[CHID][3]*q[CHID]-par[CHID][0];
            qcalib[CHID] = q[CHID]*S[CHID];
        }
        else{continue;}
    }
    tree[N[0]]->Fill();
}
outfile[N[0]]->Write();
return 0;

```

}

void write\_outfile\_over\_threads()  
{  
array\<TThread\*,nth\> VT1;  
array\<Long64\_t[3],nth\> par1;

```
for(int i=0; i<nth; i++){
    par1[i][0] = i;
    par1[i][1] = i*(N/nth);
    par1[i][2] = (i+1)*(N/nth);
    VT1[i] = new TThread((string("T")+to_string(i)).data(), write_outfile, (void*) par1[i]);
}

for(int i=0;i<nth;i++) VT1[i]->Run();
for(int i=0;i<nth;i++) VT1[i]->Join();

```

}

_ROOT Version:_ 6.22.08  
_Platform:_ Ubuntu 20.04 LTS

* * *

---

<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:** [February 28, 2022, 12:12pm UTC](https://root-forum.cern.ch/t/how-to-add-multiple-files-created-over-multi-threads-in-tthread-into-one-file/48918/2 "2022-02-28T12:12:53Z")

</div>

This seems a duplicate of [How to merge TFiles created over different threads - #4 by Siddharth\_Parashari](https://root-forum.cern.ch/t/how-to-merge-tfiles-created-over-different-threads/48834/4) , let’s continue there.

---

<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:** [February 28, 2022, 12:12pm UTC](https://root-forum.cern.ch/t/how-to-add-multiple-files-created-over-multi-threads-in-tthread-into-one-file/48918/3 "2022-02-28T12:12:58Z")

</div>

Closed on February 28, 2022, 12:12pm UTC


