# How to merge TFiles created over different threads

**URL:** <https://root-forum.cern.ch/t/how-to-merge-tfiles-created-over-different-threads/48834>\
**Category:** Newbie\
**Tags:** root\
**Created:** [February 17, 2022, 10:48am UTC](https://root-forum.cern.ch/t/how-to-merge-tfiles-created-over-different-threads/48834 "2022-02-17T10:48:50Z")\
**Posts on this page:** 1\
**Showing post:** 4

<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 22, 2022, 2:13pm UTC](https://root-forum.cern.ch/t/how-to-merge-tfiles-created-over-different-threads/48834/4 "2022-02-22T14:13:36Z")

</div>

Hi @eguiraud ,  
I have written a piece of code where I pass a pointer from different TThreads and trying to write a merged file from the files written over multiple TThreads.  
Kindly have a look. I am facing some issue, I mean it is not working.

```cpp
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->Fill();
    }
    file->Write();
    return 0;
}

```

---

_[View the full topic](https://root-forum.cern.ch/t/how-to-merge-tfiles-created-over-different-threads/48834)._
