# 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:** 5\
**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 17, 2022, 10:48am UTC](https://root-forum.cern.ch/t/how-to-merge-tfiles-created-over-different-threads/48834/1 "2022-02-17T10:48:51Z")

</div>

Hi, I am writing multiple TFiles created over different threads. Now in the end I want to merge all these files into one to be processed in another script. What would be the best way to do that??

Thanks.

_ROOT Version:_ 6.22.08  
_Platform:_ Ubuntu  
_Compiler:_

* * *

---

<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 17, 2022, 10:59am UTC](https://root-forum.cern.ch/t/how-to-merge-tfiles-created-over-different-threads/48834/2 "2022-02-17T10:59:15Z")

</div>

Hi @Siddharth_Parashari ,  
[TBufferMerger](https://root.cern/doc/master/classROOT_1_1TBufferMerger.html) exists for the purpose of writing to the same file from multiple threads.  
See [ROOT: tutorials/multicore/mt103\_fillNtupleFromMultipleThreads.C File Reference](https://root.cern/doc/master/mt103__fillNtupleFromMultipleThreads_8C.html) .

If instead you want to write to different files and then merge them, you can use the `hadd` command line tool that comes with ROOT (see `hadd --help`).

Cheers,  
Enrico

---

<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 17, 2022, 11:24am UTC](https://root-forum.cern.ch/t/how-to-merge-tfiles-created-over-different-threads/48834/3 "2022-02-17T11:24:20Z")

</div>

Thank you so much for this quick suggestion. I think using TBufferMerger would be a good idea for me.

Thanks

---

<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;
}

```

---

<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, 11:47am UTC](https://root-forum.cern.ch/t/how-to-merge-tfiles-created-over-different-threads/48834/5 "2022-02-28T11:47:36Z")

</div>

Hi @Siddharth_Parashari ,  
sorry for the high latency, I was off last week.

You should not use TThread, it’s a very old interface. As in the tutorial I linked above, you should use `std::thread`s. Can you try starting from the tutorial code and modifying to do what you want? Then if things break please post a full reproducer and the full error message or stack trace.

Cheers,  
Enrico
